How can I override a (.vsprops) property (UserMacro) with an environment variable? - visual-studio-2005

So, I want to do this (Visual Studio 2005 and/or 2010, with Microsoft and Intel compilers)
1 - New dev checks out code
2 - dev builds on desktop in unknown location with "default" values
No need for environment settings, no need for configuration, the as-submitted code "just works".
3 - build machine overrides "default" values with build-specific parameters
I thought I had this working with .vsprops. Define things like
<UserMacro Name="SHARED_LIBS_HOME" Value="....\shared" />
On the build server, where it's not in ....\shared, I use an environment variable SHARED_LIBS_HOME set to (say) "G:\Shared" and it will use G:\Shared instead of "....\shared" when running.
But this doesn't work: it looks like (with Visual Studio 2005 at least) if you have a property defined as an environment variable AND as a UserMacro in an included .vsprops, the UserMacro takes priority.
I can see a multitude of websites where one can set a .vsprops to override a .vcproj setting, or set a .vsprops to export values to the environment, but I want to set a .vsprops to set a property only if the environment is not already set.
In MSBUILD this is easy: Microsoft even has it listed as a "best practice" at http://msdn.microsoft.com/en-us/library/ee240983.aspx
Any suggestions?

Related

VisualStudio 2019 is it possible te set the build variables from CMAKE. And more generally how to set them up

I am keeping asking questions about VisualStudio but to be honest I do not understand a word from its documentation.
In the json files generated by the VS are placed build variables like ${workspaceRootFolderName}, ${workspaceRoot}, ${env.gccpath} etc etc but I do not know how to set it up.
If the the CMAKE project.
I have two questions:
Is it possible to set those variables from CMAKE files?
If not how can I set them up another way. At the moment project builds but VS generated launch files cannot evaluate the variables
There are target properties that can be used to set these kind of variables in a Visual Studio project. They all begin with VS_. I didn't see any that corresponded with these particular variables in this project.
The alternative seems to be to save them in a User props file and you can set property that incorporates that file into your project.
https://cmake.org/cmake/help/latest/prop_tgt/VS_USER_PROPS.html

Custom macros for configuring a build in Visual C++

Hopefully this isn't a duplicate. I tried to search for an answer to my question, but the word 'macro' just has too many different applications to filter the search results very effectively.
Anyway, I recently noticed in another Visual C++ (VS2010) project that custom macros were used to set up VC++ directories (include, lib) or link libraries. Something like this: "libjpeg-$(JPEG_LIB_VERSION)-static.lib", etc.
How does one go about doing that? I can't remember in what project I saw that technique, so I haven't been able to find it again to investigate, but this would be very useful when building against local builds or particular versions of widely-distributed software (say, Boost, for instance).
There's this (Visual C++ Express and setting env variables solution wide), which I suppose kinda-sorta answers the question, but not really.
The macro being used may be an environment variable, or it may indeed be a custom macro.
If it is an environment variable, you could follow the documentation as mentioned in this MSDN document How to: Use Environment Variables in a Build
However, if it is not an environment variable and you want to create your own, there is another MSDN document for that How to: Add New Property Sheets to C++ Projects
For additional references on creating a custom macro, please see How to add environmental variable to VS solution (.sln )
For additional references on using an environment, please see Macros/Environment variable in .sln and .vcproj files for Visual studio

How/Where are the environment variables in a Visual Studio C++ project set?

There a lots of environment variables in my project properties that I do not understand. Clicking on macros (Is there a list of Visual Studio environment variables?) gives me a list of their values, but I am unable to figure out where some of these are set.
For example, I am trying to figure out where the variable $(IntDir) is being set.
What file is responsible for setting these variables? How can I modify them?
These are not environment variables.
They're just macros defined by the build system that you can use for setting build properties for your project. They automatically expand to things like the target platform ($(Platform)), the path to store intermediate files for your project ($(IntDir)), and the name of your project ($(ProjectName)).
You can't change them directly, but you can change them by modifying your project's properties. The project file (created automatically by Visual Studio when you create a new project) is responsible for setting them.
You already found a link to the big list of 'em, which is helpful in explaining what they are and what they do. As the documentation says, you can use them anywhere in your project's property pages that string values are accepted. They keep you from having to hard-code paths and other information, which is exceptionally useful.
Unlike environment variables, they do not persist or have any meaning independent of your build system. Once your project has been built, they go away. They're not used during debugging or deployment.
.If you want to see actual values for a specific VS instance for both 'standard' and 'custom', see if this answer helps. (Basically, you can use Process Explorer to find that out.)

The specified solution configuration "Debug|BNB" is invalid

In Expression Blend 4, while compiling I get an error saying
The specified solution configuration “Debug|BNB” is invalid. Please specify a valid solution configuration using the Configuration and Platform properties (e.g. MSBuild.exe Solution.sln /p:Configuration=Debug /p:Platform=”Any CPU”) or leave those properties blank to use the default solution configuration.
Done building project “myproject.sln” — FAILED.
Build failed.
Background: At build-time the compiler (no matter if started from Expression Blend or Visual Studio) has to know what the target platform of your code, i.e. the system architecture like X86, 64bit etc.
The problem now is, that a system wide setting of an environment variable called "Platform" seems to override any setting of what Expression Blend uses as target platform, too.
I found some workarounds on other websites and will provide the possible solutions here. Trying one of the two suggestions should help:
Delete the conflicting environment variable "Platform" in "Control Panel -- System -- Advanced -- Environment Variables". Restart Blend and give it a try (maybe you have to restart your computer to make this work).
If this does not help: With the registry editor (Start -- Run -- "Regedit.exe") go to key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment and selected the key Platform. Delete the value BNB. If you do not find the specified key, try repacing "CurrentControlSet" with "ControlSet001", this worked for in one case, too. Then restart your computer.
Please give a comment which of the solutions worked.
I just removed the Platform from Environment variables and it worked.
With Microsoft Visual Studio the following solution should work without restarting your computer. Run the command line (Windows Key + R, type cmd, hit enter) and type the respective commands for your project:
MSVC 2013:
SET MSBUILD="C:\Program Files Path\MSBuild\12.0\Bin\msbuild.exe"
MSVC 2015:
SET MSBUILD="C:\Program Files Path\MSBuild\14.0\Bin\msbuild.exe"
SET SOLUTION="your solution name.sln"
SET PROJECT="your target project name"
SET MS_PLAT="target platform"
; this could be x64 for 64 bit applications or Win32 for 32 bit applications
%MSBUILD% %SOLUTION% /t:%PROJECT% /p:Platform=%MS_PLAT%
For me, PLATFORM = BWS. Running the above script once fixed the problem permanently for my projects, without modifying or deleting PLATFORM.
If still not work try to specify configuration and platform
for example
msbuild testproject.sln /p:Configuration=Debug /p:Platform="Any CPU"
This works for me.

How to set PlatformToolset from custom property sheet in Visual Studio 2010

I am moving now from VS 2005 to VS 2010 with products consisting of few solutions with numerous projects each. I wanted to make use of the property sheets system so our numerous configurations would be easier to maintain.
One of the issues is that we want to use Windows SDK 7.1 (as we need BaseClasses sample - and if we have to use that sample that why not the entire SDK)? But this requires me to change PlatformToolset on each and every single project in all the solutions. Also we will have to remember to change that value for newly added projects. Rather bad idea.
So I wanted to set this (among other things) from a custom property sheet. There is no such option directly from dialog shown when setting properties on property sheet. Adding entries manually to the XML file didn't help as well (but I haven't also seen any error or warning message).
Then how am I supposed to set default PlatformToolset value? At best in a configuration file which can be committed to repository. But if it could be done in some computer local settings then it would be acceptable too.
(Note that I know that I can use BaseClasses differently and avoid that problem at all but I think it is interesting issue in itself.)
I also asked this question at MSDN Formus.
PlatformToolset must be set in project properties at the begin of the file before inclusion of other files so that it can be later used to set up some defaults. If non is set then those inclusions will set it up to some default value.
Resetting it later even if works is pointless as everything was already included/set up. So to change the default value to a different SDK it seems VS configuration files (those which are included) should be changed appropriately.
But this does not have to be a good thing because it is local for the machine.
There is also an option of manual inclusion of property file which sets the PlatformToolset before default inclusions. However MS warns that if project file does not keep proper order (and this would spoil the order) VS GUI tools for project set up might not work properly.
In the end I just manually changed all projects. New projects also have to be changed to the new PlatformToolset.

Resources