[CMake] header files with visual studio

Michael Hertling mhertling at online.de
Sat Nov 6 08:39:55 EDT 2010


On 11/06/2010 12:20 PM, Eric Noulard wrote:
> 2010/11/6 Pedro d'Aquino <budsbd at gmail.com>:
>> On Friday, November 5, 2010, Oliver kfsone Smith <osmith at playnet.com>
>>>
>>> Thanks for the detailed response, Michael :)
>>>
>>> So, the question is actually:
>>>
>>> Is there a way to have CMake automatically add included headers to visual studio project files or do you need to use a dependency system to generate the lists by hand?
>>
>> Can't you just include "*.h" "*.hpp"? I find it scales much better
>> that listing each file.
> 
> Initially it may be a pain to list them but after a while its generally better
> to manually keep track of file (dis)appearing in your source tree.
> (which is usually what you do when using an IDE without CMake)
> 
> I.e. if those files are/were added "by hand" into the build system then they
> must but tracked by explicit list in CMakeLists.txt.
> 
> The only case (I see) you may faithfully glob for *.h *.whatever is when
> those are generated files.
> 
> Not doing that means
> 
> "I don't care about source file which are added/removed
>  I just want to compile those"
> 
> **MY** opinion is that this way of looking to source code is wrong.
> 
> That said I fully understand that the to CMake transition may be painful when
> the project is big.

Perhaps, one might come to a compromise:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(FILELIST C)
ADD_CUSTOM_TARGET(filelist
    COMMAND ${CMAKE_COMMAND}
            -DVAR=FILELIST
            -DDIR=${CMAKE_SOURCE_DIR}
            -DFILE=${CMAKE_BINARY_DIR}/filelist.dat
            -P ${CMAKE_SOURCE_DIR}/filelist.cmake
)
IF(NOT EXISTS ${CMAKE_BINARY_DIR}/filelist.dat)
    EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND}
                            -DVAR=FILELIST
                            -DDIR=${CMAKE_SOURCE_DIR}
                            -DFILE=${CMAKE_BINARY_DIR}/filelist.dat
                            -P ${CMAKE_SOURCE_DIR}/filelist.cmake
    )
ENDIF()
INCLUDE(${CMAKE_BINARY_DIR}/filelist.dat)
ADD_EXECUTABLE(main ${FILELIST})

The ${CMAKE_SOURCE_DIR}/filelist.cmake script looks like:

FILE(GLOB FILELIST "${DIR}/*.c")
FILE(WRITE ${FILE} "SET(${VAR} ${FILELIST})\n")

Thus, the initial set of *.c files in CMAKE_SOURCE_DIR is gathered and
stored in the ${CMAKE_BINARY_DIR}/filelist.dat script as an assignment
to the variable FILELIST. Subsequently, this filelist.dat is read via
INCLUDE(), so CMake keeps track of it, i.e. changing the filelist.dat
results in a rebuild. Finally, the filelist custom target regenerates
filelist.dat by executing filelist.cmake. Hence, each time the set of
*.c files in CMAKE_SOURCE_DIR changes, a "make filelist" will update
filelist.dat, and the following "make" will rebuild - taking into
account the refreshed list of source files. Maybe, this approach
can be adapted to platforms other than *nix and large projects.

Regards,

Michael


More information about the CMake mailing list