<div dir="ltr"><div>Dear more experienced cmake user,</div><div><br></div><div>we are 
running a Fortran code containing several dozen modules. These are build in 
two versions, differentiated by #ifdef statements. The archive files of 
the two versions are being kept apart by different file names, but the .mod 
files have fixed names, based on the name of the module. During a 
parallel build, a .mod file of one version may be overwritten by the 
partner .mod file while the first one is still required by other bits of
 ode that depends on it.</div><div><br></div><div>To circumvent it, I wrote .mod files of each variety in their own directory using</div><div><br></div><div>target_compile_option(moduleTarget PUBLIC -JdirectoryName)</div><div><br></div><div>but
 then, during the copy phase, cmake cannot find the module, because I 
circumvented the cmake way with the -J option of gfortran, which gives the option to direct .mod files to a different directory, which works, but when those modules are actually used, I get:</div><div><br></div><div><div>Error copying Fortran module "modulename.mod. Tried "MODULENAME.mod" and "modulename.mod"</div><div><br></div><div>To
 illustrate the problem I have written a simple test case that is 
conceptually the same, that consist of two tiny Fortran snippets and the
 CMakeLists.txt failing to build it. In this simple case, the program is idiotic. It has a cat version and a dog version, and it does the cat variety with -Dcat, otherwise it does the dog variety. cmake runs well, but making shows cmake in trouble:</div><div><br></div><div>>make VERBOSE=1</div><div>.</div><div>.</div><div>/usr/bin/cmake -E cmake_copy_f90_mod pets.mod CMakeFiles/dogModule.dir/pets.mod.stamp GNU<br>Error copying Fortran module "pets.mod".  Tried "PETS.mod" and "pets.mod".</div><div><br></div><div>So in short, how can the location of these .mod files in a target specific version, in stead of with the generic</div><div>CMAKE_Fortran_MODULE_DIRECTORY that will just cause the /mod files to overwrite each other in a different directory?<br></div><div><br></div><div>Also adding the directories where the mod files are as target specific include directories does not help cmake...<br></div><div><br></div><div>I am very curious what would be the cmake way of doing this properly.<br></div><div><br></div><div><div>Thank you very much for your time and expertise!</div><div><br></div><div>Lukas</div></div><div><br></div><div>############ the module file; pets.F</div><div><br></div><div>module pets<br>implicit none<br>contains<br><br>#ifdef cat<br>subroutine cattalk()<br>write(*,*) "meow"<br>#else<br>subroutine dogtalk()<br>write(*,*) "woof"<br>#endif<br>end subroutine<br><br>end module</div><div><br></div><div>############ the main.F</div><div><br></div><div>program main<br>#ifdef cat<br>use pets, only: cattalk<br>implicit none<br>call cattalk()<br>#else<br>use pets, only: dogtalk<br>implicit none<br>call dogtalk()<br>#endif<br>end program</div><div><br></div><div>############ and CMakeLists.txt</div><div><br></div><div>project(pets LANGUAGES Fortran)<br><br>file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/catdir)<br>file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/dogdir)<br><br>set(catdir ${CMAKE_BINARY_DIR}/catdir)<br>set(dogdir ${CMAKE_BINARY_DIR}/dogdir)<br><br>set(CMAKE_Fortran_SOURCE_FILES_EXTENSIONS F)<br>set(CMAKE_Fortran_FLAGS "-cpp -std=f2008 -ffree-form")<br><br>add_library(catModule STATIC "src/pets.F")<br>target_compile_options(catModule PUBLIC "-Dcat -J${catdir}")<br><br>add_library(dogModule STATIC "src/pets.F")<br>target_compile_options(dogModule PUBLIC "-J${dogdir}")<br><br>add_executable(cat "src/main.F")<br>add_dependencies(cat catModule)<br>target_link_libraries(cat catModule)<br>target_compile_options(cat PUBLIC "-Dcat")<br><br>add_executable(dog "src/main.F")<br>add_dependencies(dog dogModule)<br>target_link_libraries(dog dogModule)</div><div><br></div><div>####################################</div><div><br></div></div></div>