Find out the "Bit"ness of the current OS in MSBuild
Asked Answered
S

5

6

I have a build script that needs to hard code a path to an executable. The path is:

  • C:\Program Files\Microsoft Visual Studio 9.0\SmartDevices\SDK\SDKTools\cabwiz.exe

This has worked fine, but now I am running on a 64 bit OS (but my coworker and build server are on 32 bit still).

I need the path to be this for me:

  • C:\Program Files (x86)\Microsoft Visual Studio 9.0\SmartDevices\SDK\SDKTools\cabwiz.exe

But use the normal path for the others.

Here is how I set it up:

<PropertyGroup>
    <CabWiz>"C:\Program Files\Microsoft Visual Studio 9.0\SmartDevices\SDK\SDKTools\cabwiz.exe"</CabWiz>
</PropertyGroup>

Is there a condition I can put on that so that I can set it if the OS (not the current build configuration) is 64 bit?

Simp answered 17/8, 2010 at 17:41 Comment(0)
K
7

There is a registry key that will tell you the bit-edness of the current OS. Here are the properties I use in my MSBuild files:

<PropertyGroup>
        <MachineProcessorArchitecture>$(registry:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment@PROCESSOR_ARCHITECTURE)</MachineProcessorArchitecture>
        <Is32Bit>False</Is32Bit>
        <Is32Bit Condition="'$(MachineProcessorArchitecture)' == 'x86'">True</Is32Bit>
        <Is64Bit>False</Is64Bit>
        <Is64Bit Condition="'$(MachineProcessorArchitecture)' == 'AMD64'">True</Is64Bit>
</PropertyGroup>
Kiddush answered 2/12, 2010 at 23:33 Comment(0)
C
4

You're using the bitness to try and guess the correct Program Files folder, but there's no guarantee that it's on the C drive, or even called "Program Files". You would be better using the $(MSBuildProgramFiles32) property (in MSBuild 4.0).

Coagulum answered 7/12, 2010 at 20:37 Comment(0)
C
3

On a 64-bit OS, the following variables are defined:

ProgramFiles=C:\Program Files
ProgramFiles(x86)=C:\Program Files (x86)

So just test for ProgramFiles(x86) and if it's empty, use ProgramFiles.

Coheman answered 17/8, 2010 at 17:52 Comment(0)
C
1

If you're always running the 32-bit version of MSBuild, regardless of the platform, then it's easy: just substitute '$(ProgramFiles)' for 'C:\Program Files'. Whether on a 32-bit or 64-bit OS, '$(ProgramFiles)' should expand to the correct folder location (the location of all 32-bit programs).

If you're running the 64-bit version of MSBuild on 64-bit platforms (which is unlikely), then it gets a bit trickier. The '%ProgramFiles(x86)%' environment variable would seem to be what you want, but good luck dealing with those parentheses. Easier would probably be to use the '%PROCESSOR_ARCHITECTURE%' environment variable in a condition.

Catharina answered 17/8, 2010 at 18:23 Comment(1)
Running the 64-bit version of MSBuild is no longer unlikely at all, because it's the default for Team Build 2010. See comments here: connect.microsoft.com/VisualStudio/feedback/details/591133/…Nazarene
O
0

The solution we used relies on latest features of MSBuild; it is possible to invoke

[System.Environment]::Is64BitOperatingSystem

to understand the bitness of the Operating System:

  • Condition="'$([System.Environment]::Is64BitOperatingSystem)' == 'False'" for 32 bit OSes
  • Condition="'$([System.Environment]::Is64BitOperatingSystem)' == 'True'" for 64 bit OSes

Tested on Windows, it can works on Linux too. Here the full project.

Ozellaozen answered 25/10, 2023 at 19:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.