[CMake] Automatically Detecting Basic Fortran Dependencies

sindimo at gmail.com sindimo at gmail.com
Wed May 25 08:18:53 EDT 2011


Dear Brad,

              Thanks for your thorough explanation, I wasn't aware that the
dependency scanning only worked within a single target, it makes sense now.

Currently I am using a UNIX Make files for building but we're moving to
CMake to have the application run on Windows as well. I have a python script
that I feed Fortran files and it spits it out file dependencies (similar to
what your  "output_required_files" command should do for C files). I tweaked
it a bit and I got it to generate CMake ADD_DEPENDENCIES statements which I
append to a file and then include that file in my main CMakeLists.txt file
after adding the subdirectories. For the time being it's working fine.

I also like your suggestion of having all source files in one directory and
just build a giant target and let CMake figure out the dependencies within
that target, I tried it with this simple example and it worked fine as well.
I wonder if this approach has any drawbacks though?

Thanks again for all of your help, I really appreciate it!

M. Sindi



On Tue, May 24, 2011 at 3:44 PM, Brad King <brad.king at kitware.com> wrote:

> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20110525/1c15d283/attachment-0001.htm>


More information about the CMake mailing list