Home > Back-end >  Can I set a solution-wide macro from VS a project property sheet?
Can I set a solution-wide macro from VS a project property sheet?

Time:09-03

Short Version:

I need to find a way allow a new VS solution to "override" a build macro used by existing C projects, but only for that solution, without affecting the value of that macro in older solutions with those projects. I can change the existing projects however I need. I can write any new projects (for the new solution) if necessary. But I need to use one common set of project files to achieve this. Is this possible

Long version:

My old solution of 15 C projects directs output to a common folder via one common VS property sheet that they all use. The sheet builds an output path that's a subfolder of the solution using the (SolutionDir), $(Configuration) and $(PlatformToolset) macros

enter image description here

Every C project includes this property sheet and is careful not override the "Output Directory" setting. So all the binaries go to a subfolder of the solution like this

\x64\Release\v143

Now I have a larger, .NET 6 solution that includes those 15 projects and others. The solution will have its own output directory and I want all output to go there (i.e. no file-copying build steps). The output path doesn't use the toolset (e.g. "v143") but rather the standard .NET subfolder, like "net6.0-windows". So the new solution's output subfolder will be

\x64\Release\net6.0-windows"

I want to find a way to make all those C projects send their output to this new subfolder when they are built as part of this new solution. But my restrictions are:

  1. I need the old solution (of just the 15 projects) to still work and send its output where it used to.
  2. I need to use the same set of .VCXPROJ files between the two solutions (I cannot maintain two sets of 15 project files)
  3. I cannot rely on forcing the developer to define some environment variable before opening the solution.

I can make whatever edits I need to the old projects. But when building with the old solution, the output folder must be the same.

This would be really easy if VS property sheets macros could be "conditional" and would then be the same for all projects in the solution (like $(SolutionDir)). That is, if I could make my property sheet only define that macro (or some other) if it was not already defined. But I don't see a way to do that

Is what I want possible? If so, how?

CodePudding user response:

While property sheets macros are not conditional, property sheets themselves could be included conditionally. So you can create a solution-level property sheet with custom output directory and then modify all your projects to include this property sheet if it exists:

<ImportGroup Label="PropertySheets">
    <Import
        Project="$(SolutionDir)outputdir.props"
        Condition="exists('$(SolutionDir)outputdir.props')"
        Label="Outputdir override" />
</ImportGroup>
  • Related