[CMake] add_custom_command, source_group, and non-existant files with the VS 2010 generator

Michael Hertling mhertling at online.de
Tue Nov 8 19:22:12 EST 2011


On 11/08/2011 10:56 PM, Matthew LeRoy wrote:
> I've got a question related to the way CMake handles files that are non-existant
> at CMake-time, but which are listed as OUTPUTs of an add_custom_command, and are
> included in a source_group. I'm using CMake 2.8.5 and the Visual Studio 2010
> generator.
> 
> I have a Python script which reads a .resx file and writes a C header file
> containing string constants for each of the resources in the .resx file. I've
> implemented this using a custom command in CMake:
> 
> 
>   find_package(PythonInterp REQUIRED)
>   add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIRECTORY}/PropertyNames.h"
>     COMMAND "${PYTHON_EXECUTABLE}" "${TableMaker_SOURCE_DIR}/ResXtoCHeader.py"
>             "${CMAKE_CURRENT_SOURCE_DIRECTORY}/PropertyMapResources.resx"
>             "${CMAKE_CURRENT_BINARY_DIRECTORY}/PropertyNames.h"
>     DEPENDS "${CMAKE_CURRENT_SOURCE_DIRECTORY}/PropertyMapResources.resx"
>     COMMENT "Generating PropertyNames.h from PropertyMapResources.resx...")
> 
> 
> Then, I've got the file that is generated by the command (PropertyNames.h)
> listed in a variable, which is then added to a source_group and included
> in the list of sources for a shared library target:
> 
> 
>   set(Generated_RESOURCES
>     "${CMAKE_CURRENT_BINARY_DIRECTORY}/PropertyNames.h"
>   )
>   ...
>   source_group("\\\\Resource Files\\\\Generated" FILES ${Generated_RESOURCES})
>   ...
>   add_library(MyLibrary SHARED
>     ${SOURCES} 
>     ${HEADERS}
>     ${RESOURCES}
>     ${Generated_RESOURCES}
>     ${Generated_HEADERS}
>     ${Generated_SOURCES}
>   )
> 
> 
> I'm having two problems with this:
> 
> 1.
> When written as above, the .vcxproj file that is generated for the MyLibrary
> target references the generated file as:
> 
>   <ClInclude Include="\\PropertyNames.h" />
> 
> whereas all the other files included in the target are referenced using absolute
> paths on my filesystem. This causes Visual Studio to fail to load the project at
> all, with the following error message:
> 
>   The item metadata "%(FullPath)" cannot be applied to the path
>   "\\PropertyNames.h". The UNC path should be of the form \\server\share.
> 
> 
> 2.
> If I change the references to PropertyNames.h in CMakeLists.txt to relative paths
> (i.e. remove ${CMAKE_CURRENT_BINARY_DIRECTORY}), the generated .vcxproj then
> references the file using an absolute path, and the project loads successfully.

The variables referring to the current source/binary directory are
named CMAKE_CURRENT_{SOURCE,BINARY}_DIR, i.e. they do not end in
*_DIRECTORY. Probably, this is the cause for the file's empty
path. Thus, correct it and report if it works.

> HOWEVER, even though I've added PropertyNames.h to a source_group, it doesn't get
> placed in the correct folder within the solution explorer; it just shows up
> in the automatic "Header Files" folder in the root of the MyLibrary project.
> But, if I build the library (causing the tool the run and actually generate the
> file), then re-run CMake with the generated file actually existing, the file is
> then placed in the correct folder in the project according the source_group.

With VS2008, I recently observed that changes in CMakeLists.txt files
do *not* take effect after "cmake --build .", although the build log
mentions that CMake is re-run automatically before the project is
actually rebuilt. In order to enable the changes, I need to run
"cmake ." first, i.e. reconfigure by hand, as you do. Perhaps, your
issue is related, but I have not made any further investigations yet.

Regards,

Michael


More information about the CMake mailing list