[CMake] copying fortran .mod files in different directories

Lukas van de Wiel lukas.drinkt.thee at gmail.com
Wed Mar 4 11:14:03 EST 2020


Hi Bill,

Thanks a lot for your quick response! Using your idea I could set the
Fortran module directory per module target, and add those directories as
target_include_directories to the executables. Worked like a charm!

Lukas

PS. As example, I adapted my cat/dog CMakeLists.txt to this to get to work,
in parallel build 'n all:

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")
set_target_properties(catModule PROPERTIES Fortran_MODULE_DIRECTORY
${catdir})

add_library(dogModule STATIC "src/pets.F")
set_target_properties(dogModule PROPERTIES Fortran_MODULE_DIRECTORY
${dogdir})

add_executable(cat "src/main.F")
target_include_directories(cat PUBLIC ${catdir})
add_dependencies(cat catModule)
target_link_libraries(cat catModule)
target_compile_options(cat PUBLIC "-Dcat")

add_executable(dog "src/main.F")
target_include_directories(dog PUBLIC ${dogdir})
add_dependencies(dog dogModule)
target_link_libraries(dog dogModule)







On Wed, Mar 4, 2020 at 11:10 AM Bill Somerville <bill at classdesign.com>
wrote:

> On 04/03/2020 09:24, Lukas van de Wiel wrote:
> > 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)
> >
> Hi Lukas,
>
> we do something similar. We have it working by setting the target
> property Fortran_MODULE_DIRECTORY, in our case that is on a static
> library which contains the Fortran object code. We have two versions,
> one built with OpenMP and another without. One library has a default
> module directory and for the other we set the property above to a
> sub-directory of the binary directory, e.g.
> "set_target_properties(<target>, Fortran_MODULE_DIRECTORY,
> ${CMAKE_BINARY_DIR}/fortran_modules_omp)". We also set the property on
> the final executables where required so I assume it does not propagate
> outwards from the static library to executables linking it :(
>
> Regards
> Bill Somerville.
>
> --
>
> Powered by kitware.com/cmake
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit https://cmake.org/services
>
> Visit other Kitware open-source projects at
> https://www.kitware.com/platforms
>
> Follow this link to subscribe/unsubscribe:
> https://cmake.org/mailman/listinfo/cmake
>
> This mailing list is deprecated in favor of https://discourse.cmake.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20200304/ef661b11/attachment-0001.html>


More information about the CMake mailing list