20

I'm currently porting a gcc project to Visual C++. It's defined in a CMake file, and I have created a Visual C++ property sheet to aid in compatibility (GccCompat.props). Everytime the Visual C++ project files are regenerated by CMake, the property sheet has to be added manually, since I don't know how to add it automatically. So, the question is:

How can I tell CMake to add a property sheet to the generated Visual C++ solution?

OregonGhost
  • 23,359
  • 7
  • 71
  • 108
  • 2
    This is one of my few beefs with CMake: it often seems to limit you to the lowest common denominator between tools, or else is esoteric in its support of special features. I'm no expert, but my guess is that you'll have to do some sort of postprocessing on your generated files. – Cheezmeister Jun 24 '11 at 15:15
  • Any idea on how to do post-processing on the generated files? I would have my own script post-process them but it seems that CMake will re-generate as a part of the VS build process, overwriting my changes. There also doesn't seem to be a post-generate hook on CMake... – Allen Pestaluky Apr 18 '13 at 17:13

4 Answers4

11

This functionality has made it into the nightly build of CMake (https://gitlab.kitware.com/cmake/cmake/commit/e390991846825799e619e072a28f1da58b7c89ba), although not into a stable release yet. Theoretically, it will be in the next release, and CMake releases are made relatively frequently.

To use, you would set the VS_USER_PROPS property on a target. Eg. set_target_properties(foo PROPERTIES VS_USER_PROPS "${props_file}").

However, it doesn't appear that you can use multiple property sheets with this option, and, it replaces the default user property file ($(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props). To workaround this, property sheets can include other property sheets, so, you could make a 'master' property sheet which includes any other property sheets that you would like to use (including the default user property sheet).

MuertoExcobito
  • 9,741
  • 2
  • 37
  • 78
  • 1
    The property has been renamed to [`VS_USER_PROPS`](https://cmake.org/cmake/help/v3.8/prop_tgt/VS_USER_PROPS.html) – wally Feb 21 '17 at 17:04
  • Feb 2018 update: This feature is now part of CMake since version 3.8 (if my intel is right). It works fairly well. – kralyk Feb 23 '18 at 12:16
  • Further update: I've found out my MSVC won't load up the props file unless it's copied next to the `.proj` file and so I had to add a copy command (via `configure_file( ... COPYONLY)`) to make that happen in CMake. Also apparently the _order_ of items in the props file matters. – kralyk Feb 23 '18 at 14:14
  • So what happens when you try to use this CMake setup for any build system *other* than msbuild? – Omnifarious Jul 03 '18 at 04:09
  • in my master props file: ```xml ``` – Gavin Gao Jun 01 '22 at 09:25
3

This question is a little bit old but I have recently stumbled upon the same problem while integrating GStreamer into my project. GStreamer comes with a set of extremely well prepared and high quality Property Sheets and I wanted to use them instead of hacking things around in CMake.

Fortunately, this issue is only limited to Windows and Visual Studio. So here's my solution:

The idea is to use Visual Studio's .user file feature. CMake does not generate this file so it's pretty safe to generate it at configure-time. At configure time you may generate a file that has the EXACT name as your project file but ends with a .user extension.

Partial Solution:

If your project file is named my_project.vcxproj, you need to create another file next to it called my_project.vcxproj.user. According to MSDN:

A user file (.vcxproj.user) stores user-specific properties, for example, debugging and deployment settings. The vcxproj.user file applies to all projects for a particular user.

The contents of this file for importing property sheets is something like this:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="/path/to/sheet1.props" />
  <Import Project="/path/to/sheet2.props" />
</Project>

Not flawless, but works until CMake starts supporting property sheets. The file can be created by using CMake's file command at configure-time.

Potential Caveat:

I have noticed when I add property sheets this way, sometimes they do not show in the Property Manager window (might be a bug in Visual Studio Community 2013) but they always are imported properly and dependencies are resolved correctly.

Sepehr
  • 2,051
  • 19
  • 29
0

Not sure which properties you need. A few could be set directly in CMake, like in this example for multiple configurations:

set (CMAKE_CONFIGURATION_TYPES "A;B;C;D" CACHE STRING "Configurations" FORCE)

foreach( OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES} )

    set (CMAKE_CXX_FLAGS_${OUTPUTCONFIG}                "/ZI /Od")
    set (CMAKE_EXE_LINKER_FLAGS_${OUTPUTCONFIG}         "/debug")

endforeach( OUTPUTCONFIG CMAKE_CONFIGURATION_TYPES )

Apart from variables listed here, I think CMake has no possibility to attach property sheets.

Community
  • 1
  • 1
Olha Puzhay
  • 370
  • 2
  • 9
  • 1
    @kfsone Your point 1 is rather irrelevant as this is a 5 year old answer. And linking to "non-official" pages is not forbidden. – Mark Rotteveel Jun 09 '18 at 07:47
  • @MarkRotteveel I didn't suggest it was some form of moral violation, but a significant portion of the answer is firewalled behind a link that *no-longer* points to the official site, erasing the value of the answer, and something which *is* discouraged -- answers are encouraged to quote the relevant bit of the linked page to protect against exactly this. – kfsone Jul 24 '18 at 22:12
  • @MarkRotteveel Specifically - the *only* correct and helpful part of this answer is the link, and it's a deprecated link. If the rest of the answer wasn't inaccurate and unhelpful, then I would have edited the answer to update the link and cite the variables. – kfsone Jul 24 '18 at 22:17
0

in my master props file main.props:

<ImportGroup Label="PropertySheets">
  <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>

then, in CMakeLists.txt:

set_target_properties(foo PROPERTIES VS_USER_PROPS "main.props")
Gavin Gao
  • 309
  • 3
  • 6