Discussion:
[CMake] Exclude CMakeFiles path from GLOB_RECURSE
Diablo 666
2010-07-01 07:45:07 UTC
Permalink
Hi,

I'm currently trying to use the following line to include all source files into my build:

file (GLOB_RECURSE Files_CPP *.cpp)
add_executable(test
${Files_CPP}
)

Everything runs fine while using an out-of-source build, but for in-source builds these lines include .cpp files from the CMakeFiles directory. Is there any way to exclude this directory within the file command?

Best regards,
Andreas

_________________________________________________________________
http://redirect.gimas.net/?n=M1007xHM2
Räumen Sie Ihr Postfach auf - ganz einfach mit Hotmail!
Michael Wild
2010-07-01 08:04:50 UTC
Permalink
On 1. Jul, 2010, at 9:45 , Diablo 666 wrote:

>
> Hi,
>
> I'm currently trying to use the following line to include all source files into my build:
>
> file (GLOB_RECURSE Files_CPP *.cpp)
> add_executable(test
> ${Files_CPP}
> )
>
> Everything runs fine while using an out-of-source build, but for in-source builds these lines include .cpp files from the CMakeFiles directory. Is there any way to exclude this directory within the file command?
>
> Best regards,
> Andreas

1. Never use GLOB_RECURSE. It's evil. E.g. if you add or remove source files, CMake has no way of knowing that it should be re-run. Just list all the files in your CMakeLists.txt (or, if you think the list is excessively long, create a file called e.g. files.cmake, put the list in there and INCLUDE it in the CMakeLists.txt file.

2. Never do in-source builds. CMake creates many, many files, and you don't want to delete them all individually for a clean build. And there's no way to implement a safe "make clean" (e.g. your build system might be running a custom utility which generates files CMake knows nothing about).

HTH

Michael
Diablo 666
2010-07-01 08:15:55 UTC
Permalink
Hi,

thanks for your reply.

> 1. Never use GLOB_RECURSE. It's evil. E.g. if you add or remove source files, CMake has no way of knowing that it should be re-run.
I have tested rerunning cmake manually and it worked out well. (Testcase: I added a new .cpp file to the build) I either have to change the CMakeLists.txt or to rerun cmake manually. Seems to be the same effort for me.

> 2. Never do in-source builds. CMake creates many, many files, and you don't want to delete them all individually for a clean build. And there's no way to implement a safe "make clean" (e.g. your build system might be running a custom utility which generates files CMake knows nothing about).

I know these problems. I will never do an in-source-build again :-) But unfortunately, I have to support in-source builds, too.

Best regards,
Andreas

_________________________________________________________________
http://redirect.gimas.net/?n=M1007xHMTL4
Künftig E-Mails über Hotmail ohne Werbung versenden!
Michael Wild
2010-07-01 08:49:15 UTC
Permalink
On 1. Jul, 2010, at 10:15 , Diablo 666 wrote:

>
> Hi,
>
> thanks for your reply.
>
>> 1. Never use GLOB_RECURSE. It's evil. E.g. if you add or remove source files, CMake has no way of knowing that it should be re-run.
> I have tested rerunning cmake manually and it worked out well. (Testcase: I added a new .cpp file to the build) I either have to change the CMakeLists.txt or to rerun cmake manually. Seems to be the same effort for me.

Thing is: will you never forget? And it prevents stray files from being picked up (as you experienced). Also, you get an immediate response if a file is missing, and not just when the compiler complains about a missing header or you get undefined references during the linking stage.

If you insist on using GLOB_RECURSE, don't apply it to CMAKE_CURRENT_SOURCE_DIR, but use GLOB on the CMAKE_CURRENT_SOURCE_DIR and use GLOB_RECURSE on all the subdirectories individually, so you don't get CMake-generated stuff.

Also, you could filter out all the items contained in the CMakeFiles directory:

file(GLOB_RECURSE SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp *.h)
string(REGEX REPLACE "CMakeFiles/[^;]+;?" "" SRCS "${SRCS}")

Michael
Diablo 666
2010-07-01 11:47:28 UTC
Permalink
> Thing is: will you never forget? And it prevents stray files from being picked up (as you experienced). Also, you get an immediate response if a file is missing, and not just when the compiler complains about a missing header or you get undefined references during the linking stage.

Ok, that sounds reasoble. (This should especially convince some lazy people :-D )
Thanks a lot!

_________________________________________________________________
http://redirect.gimas.net/?n=M1007xHM2
Räumen Sie Ihr Postfach auf - ganz einfach mit Hotmail!
Loading...