Add Visual C++ property sheets using CMake
Asked Answered
I

4

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?

Intersect answered 4/6, 2010 at 10:27 Comment(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.Sleep
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...Previdi
C
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).

Capricecapricious answered 26/1, 2017 at 20:39 Comment(5)
The property has been renamed to VS_USER_PROPSGoodfornothing
Feb 2018 update: This feature is now part of CMake since version 3.8 (if my intel is right). It works fairly well.Ehlers
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.Ehlers
So what happens when you try to use this CMake setup for any build system other than msbuild?Urbanite
in my master props file: xml <ImportGroup Label="PropertySheets"> <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> </ImportGroup> Imaginable
P
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.

Patriapatriarch answered 10/10, 2015 at 20:26 Comment(0)
V
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.

Voccola answered 12/3, 2013 at 13:55 Comment(3)
@kfsone Your point 1 is rather irrelevant as this is a 5 year old answer. And linking to "non-official" pages is not forbidden.Austine
@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.Bourse
@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.Bourse
I
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")
Imaginable answered 1/6, 2022 at 9:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.