diff --git a/Modules/CMakeAddFortranSubdirectory.cmake b/Modules/CMakeAddFortranSubdirectory.cmake index 70e6a42443..b948af38df 100644 --- a/Modules/CMakeAddFortranSubdirectory.cmake +++ b/Modules/CMakeAddFortranSubdirectory.cmake @@ -237,6 +237,12 @@ function(cmake_add_fortran_subdirectory subdir) "${CMAKE_CURRENT_BINARY_DIR}/${${dir_var}}" ABSOLUTE) endif() endforeach() + # list the import libraries produced by the external project so that + # generators like Ninja know which rule creates them + set(byproducts) + foreach(lib IN LISTS libraries) + list(APPEND byproducts "${library_dir}/lib${lib}.lib") + endforeach() # create build and configure wrapper scripts _setup_mingw_config_and_build("${source_dir}" "${build_dir}") # create the external project @@ -248,6 +254,7 @@ function(cmake_add_fortran_subdirectory subdir) BUILD_COMMAND ${CMAKE_COMMAND} -P ${build_dir}/build_mingw.cmake BUILD_ALWAYS 1 + BUILD_BYPRODUCTS ${byproducts} INSTALL_COMMAND "" ) # create imported targets for all libraries diff --git a/Modules/CMakeAddFortranSubdirectory/build_mingw.cmake.in b/Modules/CMakeAddFortranSubdirectory/build_mingw.cmake.in index 55b271a2d0..ee8bf0ec65 100644 --- a/Modules/CMakeAddFortranSubdirectory/build_mingw.cmake.in +++ b/Modules/CMakeAddFortranSubdirectory/build_mingw.cmake.in @@ -1,2 +1,11 @@ set(ENV{PATH} "@MINGW_PATH@\;$ENV{PATH}") -execute_process(COMMAND "@CMAKE_COMMAND@" --build . ) +# rebuild from scratch if a byproduct was removed, e.g. by a clean of +# the outer project, because the inner build would consider itself up to date +set(byproducts "@byproducts@") +set(clean_first "") +foreach(byproduct IN LISTS byproducts) + if(NOT EXISTS "${byproduct}") + set(clean_first --clean-first) + endif() +endforeach() +execute_process(COMMAND "@CMAKE_COMMAND@" --build . ${clean_first})