[CMake] copying fortran .mod files in different directories

Lukas van de Wiel lukas.drinkt.thee at gmail.com
Wed Mar 4 04:24:22 EST 2020


Dear more experienced cmake user,

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.

To circumvent it, I wrote .mod files of each variety in their own directory
using

target_compile_option(moduleTarget PUBLIC -JdirectoryName)

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:

Error copying Fortran module "modulename.mod. Tried "MODULENAME.mod" and
"modulename.mod"

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:

>make VERBOSE=1
.
.
/usr/bin/cmake -E cmake_copy_f90_mod pets.mod
CMakeFiles/dogModule.dir/pets.mod.stamp GNU
Error copying Fortran module "pets.mod".  Tried "PETS.mod" and "pets.mod".

So in short, how can the location of these .mod files in a target specific
version, in stead of with the generic
CMAKE_Fortran_MODULE_DIRECTORY that will just cause the /mod files to
overwrite each other in a different directory?

Also adding the directories where the mod files are as target specific
include directories does not help cmake...

I am very curious what would be the cmake way of doing this properly.

Thank you very much for your time and expertise!

Lukas

############ the module file; pets.F

module pets
implicit none
contains

#ifdef cat
subroutine cattalk()
write(*,*) "meow"
#else
subroutine dogtalk()
write(*,*) "woof"
#endif
end subroutine

end module

############ the main.F

program main
#ifdef cat
use pets, only: cattalk
implicit none
call cattalk()
#else
use pets, only: dogtalk
implicit none
call dogtalk()
#endif
end program

############ and CMakeLists.txt

project(pets LANGUAGES Fortran)

file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/catdir)
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/dogdir)

set(catdir ${CMAKE_BINARY_DIR}/catdir)
set(dogdir ${CMAKE_BINARY_DIR}/dogdir)

set(CMAKE_Fortran_SOURCE_FILES_EXTENSIONS F)
set(CMAKE_Fortran_FLAGS "-cpp -std=f2008 -ffree-form")

add_library(catModule STATIC "src/pets.F")
target_compile_options(catModule PUBLIC "-Dcat -J${catdir}")

add_library(dogModule STATIC "src/pets.F")
target_compile_options(dogModule PUBLIC "-J${dogdir}")

add_executable(cat "src/main.F")
add_dependencies(cat catModule)
target_link_libraries(cat catModule)
target_compile_options(cat PUBLIC "-Dcat")

add_executable(dog "src/main.F")
add_dependencies(dog dogModule)
target_link_libraries(dog dogModule)

####################################
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20200304/57667b40/attachment.html>


More information about the CMake mailing list