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

Matthew LeRoy MLeRoy at minitab.com
Tue Nov 8 16:56:11 EST 2011


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.

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.


Any ideas?


More information about the CMake mailing list