[CMake] Dynamically add source files

Steven Van Ingelgem steven at vaningelgem.be
Sun Dec 2 07:33:40 EST 2007


Hi all,


I don't know if this has been done already, but I needed something
like this, so I wrote it.

What does it do?

If you have a directory structure like:
/src/defines/Header.h
/src/defines/Header.cpp
/src/dir.h
/src/dir.cpp

It will add all files under /src while maintaining the relative
positions inside your IDE. In your IDE it will look like 1 folder
"defines" with 2 files in it (header.h/cpp) and in the "root" it will
have "dir.h/cpp".
So if you are like me and don't want to modify your CMakeLists.txt
everytime you add a new sourcefile, then this would be something for
you.

It will automatically add all new (and old) files to the project in
the same directory structure as when they are located on the
filesystem.

You can also add platform dependent sources by using the directories
src_msw, src_mac and src_nix. Which would be included only on Windows,
apple, and *nix variants.


Basic usage: (include before the file before the call to
add_executable/library!)
Assumptions: you have a /src directory inside your cmake source root
GET_ALL_SOURCE_FILES(all_sources ${CMAKE_CURRENT_SOURCE_DIR})

ADD_EXECUTABLE(project ${all_sources})

GENERATE_SOURCE_GROUPS(all_sources ${CMAKE_CURRENT_SOURCE_DIR})


Ensure that you use the exact same parameters for both macro's! If you
use ${all_sources} in the second call (generate_source_groups), it
will give you a LOT of weird errors...



If you like this, just send me a message.

If you have any issues with it, please first read the header of the
file. I tried my best to explain it as good as possible. If you have
more issues, just contact me and i'll try to help you as good as
possible.


Have fun with it!
Steven
-------------- next part --------------
A non-text attachment was scrubbed...
Name: dynamically_add_source_files.cmake
Type: application/octet-stream
Size: 9550 bytes
Desc: not available
Url : http://public.kitware.com/pipermail/cmake/attachments/20071202/ff5023b2/dynamically_add_source_files-0001.obj


More information about the CMake mailing list