[CMake] Automatically Detecting Basic Fortran Dependencies

sindimo at gmail.com sindimo at gmail.com
Mon May 23 14:15:01 EDT 2011


Dear All,

I have the below directory structure for Fortran code which has very basic
dependencies, I am not able to get CMake to recognize the dependencies
automatically without me explicitly having to specify it using
add_dependencies or target_link_libraries:

src/

    CMakeLists.txt

    lib1/CMakeLists.txt
    lib1/lib1.f

    lib2/CMakeLists.txt
    lib2/lib2.f

    lib3/CMakeLists.txt
    lib3/lib3.f

    main/CMakeLists.txt
    main/myMain.f


Dependencies are:
myMain uses lib3
lib3 uses lib2
lib2 uses lib1


My top directory CMakeLists.txt looks like this:
[sindimo at lnx src]$ cat CMakeLists.txt
CMAKE_MINIMUM_REQUIRED (VERSION 2.8)
PROJECT(Fortran-Example-Dependency-Failure Fortran)

INCLUDE_DIRECTORIES(/workspace/Fortran-Example-Dependency-Failure/obj/linux_gnu_release_64-bit)

set (EXECUTABLE_OUTPUT_PATH
"/workspace/Fortran-Example-Dependency-Failure/bin/linux_gnu_release_64-bit")
set (LIBRARY_OUTPUT_PATH
"/workspace/Fortran-Example-Dependency-Failure/obj/linux_gnu_release_64-bit")
set (CMAKE_Fortran_MODULE_DIRECTORY
"/workspace/Fortran-Example-Dependency-Failure/obj/linux_gnu_release_64-bit")

ADD_SUBDIRECTORY(lib3)
ADD_SUBDIRECTORY(lib2)
ADD_SUBDIRECTORY(lib1)
ADD_SUBDIRECTORY(main)


The subdirectory CMakeLists.txt files look like:
[sindimo at lnx src]$ cat lib1/CMakeLists.txt
ADD_LIBRARY(lib1 SHARED lib1.f)

[sindimo at lnx src]$ cat lib2/CMakeLists.txt
ADD_LIBRARY(lib2 SHARED lib2.f)

[sindimo at lnx src]$ cat lib3/CMakeLists.txt
ADD_LIBRARY(lib3 SHARED lib3.f)

[sindimo at lnx src]$ cat main/CMakeLists.txt
ADD_EXECUTABLE(Fortran-Example-Dependency-Failure.exe myMain.f)
TARGET_LINK_LIBRARIES(Fortran-Example-Dependency-Failure.exe lib1 lib2 lib3)


When I run cmake followed by make I get the below error which apparently
seems to be due to CMake not figuring out the correct dependencies in
Fortran and doing the wrong build sequence:
[sindimo at lnx build_linux_gnu_release_64-bit]$ cmake ../src
-- The CXX compiler identification is GNU
-- The Fortran compiler identification is GNU
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working Fortran compiler: /usr/bin/gfortran
-- Check for working Fortran compiler: /usr/bin/gfortran -- works
-- Detecting Fortran compiler ABI info
-- Detecting Fortran compiler ABI info - done
-- Checking whether /usr/bin/gfortran supports Fortran 90
-- Checking whether /usr/bin/gfortran supports Fortran 90 -- yes
-- Configuring done
-- Generating done
-- Build files have been written to:
/workspace/Fortran-Example-Dependency-Failure/build_linux_gnu_release_64-bit

[sindimo at lnx build_linux_gnu_release_64-bit]$ make
Scanning dependencies of target lib3
[ 25%] Building Fortran object lib3/CMakeFiles/lib3.dir/lib3.f.o
 In file /workspace/Fortran-Example-Dependency-Failure/src/lib3/lib3.f:6

            use m_lib2
                                                                       1
Fatal Error: Can't open module file 'm_lib2.mod' for reading at (1): No such
file or directory
make[2]: *** [lib3/CMakeFiles/lib3.dir/lib3.f.o] Error 1
make[1]: *** [lib3/CMakeFiles/lib3.dir/all] Error 2
make: *** [all] Error 2


To my understanding CMake should be able to handle such simple Fortran
dependencies (as I recall, it uses similar intelligence to that of
makedepf90 to detect dependencies), am I doing something wrong here?

If I add the below explicit dependency statements manually to the top level
CMakeLists.txt file, things go well:
ADD_DEPENDENCIES(lib2 lib1)
ADD_DEPENDENCIES(lib3 lib2)

Also if I sort the ADD_SUBDIRECTORY commands in the correct build sequence,
that obviously solves the problem as well.

However our actual application that we need to use CMake with consists of
hundreds of libraries, so adding these dependencies manually or sorting them
in the correct build sequence is not feasible.

I would appreciate any feed back or help with this.

Many thanks!

M. Sindi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20110523/55b14f12/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Fortran-Example-Dependency-Failure.zip
Type: application/zip
Size: 4739 bytes
Desc: not available
URL: <http://www.cmake.org/pipermail/cmake/attachments/20110523/55b14f12/attachment.zip>


More information about the CMake mailing list