[CMake] How to make FILE() append to the list of files?

Michael Jackson mike.jackson at bluequartz.net
Thu Nov 27 08:55:40 EST 2008


On Nov 27, 2008, at 1:12 AM, Robert Dailey wrote:

> On Wed, Nov 26, 2008 at 7:27 PM, Bill Hoffman <bill.hoffman at kitware.com 
> > wrote:
> Robert Dailey wrote:
> Hi,
>
> I have about 20 directories that contain source files. However, the  
> FILE() call I make to each directory will have a different glob  
> expression. I need to be able to do FILE( GLOB ..... ) about 20  
> times and each time have it append to the existing variable I give  
> it. Is this possible? What would be the recommended way of  
> accumulating a list of files in all 20 directories?
>
>
> find . -name "*.c" > source_files.txt
> edit source_files.txt and put that list of files exiplicitly into a  
> CMakeLists.txt file.
>
> file(GLOB is a bad way to get source lists for CMake.   CMake has no  
> way of knowing when new files have been put in the directory.
>
> But unless I am missing a fundamental feature somewhere, GLOB still  
> seems to be the better alternative. While it may not intrinsically  
> know when new files have appeared on the filesystem, the programmer  
> can simply re-run the CMake command to get an updated project with  
> the newly added source files without editing the CMakeLists.txt file  
> directly. This is better than the alternative, which is a hard-coded  
> list of project files in the CMake script.

I'll pull from my experience in the last few projects I have worked on  
and agree with Bill on this one. There is more up front work but the  
pay off in the end are more predictable results when you build your  
project.

   And to take this one step further if you hard code the files into  
your cmake files then you can also "act" on those lists to make your  
Visual Studio projects have a better structure.

For example in my project in each sub folder I have a "Sources.cmake"  
file. This file is  included from the higher up CMakeLists.txt file  
(sometimes conditionally based on options). During the inclusion  
process I call a macro that also sets installation properties of the  
files (headers get installed) and also set up source groups for the  
files so that the Visual Studio and Xcode projects are more organized  
that just having a single "folder" with all the files and headers  
mixed in.

EXAMPLE========== Common_SourceList.cmake  ========
SET (MXA_COMMON_SRCS
   ${MXA_SOURCE_DIR}/src/Common/IO/Reader64.cpp
   ${MXA_SOURCE_DIR}/src/Common/IO/Writer64.cpp
)
SET (MXA_COMMON_HEADERS
   ${MXA_SOURCE_DIR}/src/Common/LogTime.h
   ${MXA_SOURCE_DIR}/src/Common/DLLExport.h
   ${MXA_SOURCE_DIR}/src/Common/MXAEndian.h
   ${MXA_SOURCE_DIR}/src/Common/MXATypeDefs.h
   ${MXA_SOURCE_DIR}/src/Common/MXATypes.h
   ${MXA_SOURCE_DIR}/src/Common/MXAErrorDefinitions.h
   ${MXA_SOURCE_DIR}/src/Common/IO/Reader64.h
   ${MXA_SOURCE_DIR}/src/Common/IO/Writer64.h
   ${MXA_SOURCE_DIR}/src/Common/Cast/Cast.h
)

MXA_SOURCE_PROPERTIES(Common "${MXA_COMMON_HEADERS}" "$ 
{MXA_COMMON_SRCS}")

========== =======================

MACRO (MXA_SOURCE_PROPERTIES NAME HEADERS SOURCES)

   INSTALL (FILES ${HEADERS}
             DESTINATION include/MXADataModel/${NAME}
             COMPONENT Headers
   )
   source_group(src\\${NAME} FILES ${HEADERS} ${SOURCES})

#-- The following is needed if we ever start to use OS X
#-- Frameworks but only works on CMake 2.6 and greater
#  set_property(SOURCE ${HEADERS}
#   PROPERTY MACOSX_PACKAGE_LOCATION Headers/${NAME}
#  )

ENDMACRO (MXA_SOURCE_PROPERTIES NAME HEADERS SOURCES)


As Bill stated above, a simple one-off shell script could probably be
created pretty fast to generate those "SourceList.cmake" files.

Just my experience.
_________________________________________________________
Mike Jackson                  mike.jackson at bluequartz.net
             www.bluequartz.net



More information about the CMake mailing list