[CMake] Automatically Detecting Basic Fortran Dependencies

Brad King brad.king at kitware.com
Tue May 24 08:44:10 EDT 2011


On 5/23/2011 2:15 PM, sindimo at gmail.com wrote:
> 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:

What documentation did you read that led you to believe CMake can
find library link dependencies?  The problem is ill-defined in
general because it is possible for two source files in the project
to provide the same symbol.  If those sources go in different
libraries then how can any tool possibly know which one the author
intends to be used?

> 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?

CMake's dependency scanning picks up dependencies within a single
target.  The scanning is purely for rebuilding when preprocessor
dependencies change, and in the case of Fortran for ordering the
build of object files *within* a target correctly to satisfy the
module provider/user dependencies.

CMake will also hook up cross-target F90 .mod dependencies but
it will only look at the targets *linked* into the current target
to satisfy those dependencies.

> 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)

This is expected, but you should use

  target_link_libraries(lib2 lib1)
  target_link_libraries(lib3 lib2)

instead.  This tells CMake that whenever something links to lib2
it needs lib1 also, and similarly that lib3 needs lib2 at link
time.  After expressing these link-time dependencies you can then
link your executable with just

  target_link_libraries(myMain lib3)

to express that direct dependency.  The other lines above tell
CMake to follow the dependencies transitively.

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

This might make it build the first time but it won't hook up the
cross-target .mod dependencies correctly so that objects rebuild
when their modules change.

> 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.

How are you building the application now?  Your current build
system must know the link dependencies.  Alternatively, if you
do not actually need the separate libraries you can just list
all your source files in one giant target.  CMake does not care
if the files lie in different directories.  Within the single
target it will compute the correct order to build your object
files.

-Brad


More information about the CMake mailing list