[CMake] Exclude CMakeFiles path from GLOB_RECURSE

Michael Wild themiwi at gmail.com
Thu Jul 1 04:49:15 EDT 2010


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


More information about the CMake mailing list