[CMake] multiple CMakeLists.txt files in the same directory

Mike Jackson imikejackson at gmail.com
Wed Aug 15 13:20:31 EDT 2007


On 8/15/07, Jon W <knowdat at gmail.com> wrote:
> > > > > > > > > > Is it possible to have multiple CMakeLists.txt files within the same
> > > > > > > > > > directory? (We have projects that span/share multiple directories,
> > > > > > > > > > and currently all of the vcproj files are within a single directory
> > > > > > > > > > for easy editing.)
> > > > > > > > > >
> > > > > > > > > > Or, is the suggested route to create a directory structure such as,
> > > > > > > > > >
> > > > > > > > > > /
> > > > > > > > > > src/foo/foo.cxx
> > > > > > > > > > /apple/apple.cxx
> > > > > > > > > >
> > > > > > > > > > cmake/foo/CMakeLists.txt
> > > > > > > > > > /apple/CMakeLists.txt
> > > > > > > >
> > > > > > > > Every CMakeLists.txt file should have it's target. If you wish to make
> > > > > > > > libraries out of every directory and then link them with main() in the
> > > > > > > > top directory then use separate CMakeLists.txt.
> > > > > > > >
> > > > > > > > Filip Brcic <brcha at users.sourceforge.net>
> > > > > > >
> > > > > > > I'm building foo.dll, apple.dll, some.exe, from which the source is
> > > > > > > scattered throughout the src directory.  There are no foo or apple
> > > > > > > directories, so I can't put a CMakeLists.txt file in those specific
> > > > > > > directories.  What I have been doing is to create a main cmake
> > > > > > > directory (next to src) that then has a directory based upon the
> > > > > > > dll/exe name (foo, apple) with a CMakeLists.txt inside.
> > > > > > >
> > > > > > > src/
> > > > > > > cmake/
> > > > > > >           apple/CMakeLists.txt
> > > > > > >           foo/CMakeLists.txt
> > > > > > >
> > > > > > > One of the developers was hoping that we could bypass the extra
> > > > > > > directory structure and have a general directory that has all of the
> > > > > > > dll/exe cmake files inside.  For example,
> > > > > > >   cmakefiles/
> > > > > > >                   CMakeLists_apple.txt
> > > > > > >                   CMakeLists_foo.txt
> > > > > > >
> > > > > > > But, this doesn't sound as though it will work very well.  Thank you
> > > > > > > for your help.
> > > > > > >
> > > > > > > -Jon
> > > > > >
> > > > > > But you can do just that as Mike Jackson suggested. Make your
> > > > > > cmakefiles/CMakeLists_*.txt files and then include them in some master
> > > > > > CMakeLists.txt, possibly with IF(something) arround that.
> > > > > >
> > > > > > --
> > > > > > Filip Brcic <brcha at users.sourceforge.net>
> > > > >
> > > > > That sounds like it would work fine if all of the options are the
> > > > > same, but may be difficult to separate compiler options,
> > > > > link_directories, include paths, and custom commands, per project.
> > > > > I'm guessing that every CMakeLists_project.txt would then need to IF
> > > > > block every listing that isn't specifically linked to that project's
> > > > > dll/exe.
> > > > >
> > > > > -Jon
> > > > >
> > > >
> > > > Probably what you should be doing is to have all the general cmake
> > > > setup stuff (general sources, binary locations) setup in your top
> > > > level CMake file. Then you will have Apple.cmake (note the extension..
> > > > ) and in there you start listing all your apple specific compiler
> > > > options and other options. In those platform specifc files you also
> > > > define a variable PLATFORM_SRCS that would have your list of platform
> > > > specific sources in it. In your Main CMakeLists.txt file you then do:
> > > >
> > > > SET (SRCS foo.cpp)
> > > >
> > > > IF (APPLE)
> > > > INCLUDE (Apple.cmake)
> > > > ENDIF(APPLE)
> > > >
> > > > IF (WIN32)
> > > > INCLUDE (Win32.cmake)
> > > > ENDIF(WIN32)
> > > >
> > > > SET (SRCS ${SRCS} ${PLATFORM_SRCS} )
> > > >
> > > > ADD_LIBRARY (foo SHARED ${SRCS} )
> > > >
> > > > The gets rid of all the extra directories. Does this help?
> > > > --
> > > > Mike Jackson
> > > > imikejackson _at_ gee-mail dot com
> > >
> > > Sorry, I used a bad reference when specifying the project apple.  With
> > > this question, I am not trying to compile for different platforms, but
> > > trying to compile foo.dll, fooo.exe, foo4.lib, (i.e. separate
> > > projects) and have each project's cmakelists.txt within a single
> > > directory.
> > >
> > > Do you know if this is possible?
> > >
> > > Thank you,
> > > Jon
> > >
> >
> > goto http://titanium.imts.us/viewvc/Task_7/MXADataModel/
> >
> > This is a project that I am working on. The way things are setup is
> > that I have a top level CMakeLists.txt file. All my source code is
> > located in src/. with that directory I have some other directories,
> > but the interesting ones are test/ and examples/. In each of those I
> > have a CMakeLists.txt file that spells out each test program or a
> > sample program that needs to be built. I also add the options in the
> > main CMakeLists.txt file whether or not to actually build those sub
> > projects.
> >
> > Look through the source and see if any of it is a help. The code is
> > all opensource so if you want to grab a tarball and work with it on
> > your computer go ahead. If you have any questions let me know.
> >
> > I think you can do what you want, I am just trying to get my head
> > around what it is you want.. ;-)
> > --
> > Mike Jackson
> > imikejackson _at_ gee-mail dot com
>
> Thanks Mike.  Your cmakelists gave me a couple of great ideas, but I'm
> not sure if they will work 100% for this task.  (Granted I did a very
> quick glance and need to review more.)
>
> Just to explain a bit more...I generate 50+ exes, dlls and libs from ~
> 9000+ files.  Currently I have been creating a cmakelists.txt for each
> project (i.e. dll, exe, lib).  I can't keep these cmakelists.txt files
> within a name related directory in the src dir, as each project spans
> source files throughout the src tree and there aren't generally
> directories named for the produced exe, dll or lib.
>
> It would be nice to have each dll's, exe's, and lib's related
> cmakelist.txt within a single directory, so then one doesn't have to
> search a hierarchy for the cmakelist.txt.  I could probably have a
> main cmakelists.txt that would include all of the other
> cmakelists.txt, but the problem lies that each individual
> cmakelists.txt file specifies specific compiler options, and some have
> custom commands, etc..  So, I would assume that the include wouldn't
> know how to separate the compiler options and custom commands per
> dll/exe/lib.
>
> Hopefully that makes sense.  I'm also new to cmake and just trying to
> learn what is and isn't possible.
>
> Thank you,
> Jon
>

If I were doing this then I would have a top level CMakeLists.txt file
that referenced a bunch of other *.cmake files in a directory located
at the top level of the project.

Project/
    CMakeLists.txt
    Resources/
         Project1.cmake
         Project2.cmake...

Now, within each of those Project*.cmake files you can define a target
(either a library or executable or whatever) with what every custom
commands you need. I believe that you can actually use the
"SET_TARGET_PROPERTIES" command to set target specific compiler
commands.

That should work just fine but I have not actually tried it. This type
of design will allow all the cmake files to stay together in one
place, allow you to include source files from any subdirectory for
each target, and allow you to have specific compilation options for
each target if needed.

Also, I would define something like "MAIN_SOURCE_DIR" in the top level
CMakeLists.txt file, then in each of the Resources/Project*.cmake you
can reference your source files like so:

SET (MORE_SOURCES
 ${MAIN_SOURCE_DIR}/foo/foo.cpp
 ${MAIN_SOURCE_DIR}/bar/bar.cpp
)

INCLUDE_DIRECTORIES ( ${MAIN_SOURCE_DIR}/headers)

what do you think?
-- 
Mike Jackson
imikejackson _at_ gee-mail dot com


More information about the CMake mailing list