aboutsummaryrefslogtreecommitdiff
path: root/scripts/findAnyMSBuildWithCppPlatformToolset.ps1
blob: f1ef1ae6b3b0b7dd3f08284420c198ae54d0189c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
[CmdletBinding()]
param(
    [Parameter(Mandatory=$False)]
    [switch]$DisableVS2017 = $False,

    [Parameter(Mandatory=$False)]
    [switch]$DisableVS2015 = $False
)

if ($DisableVS2017 -and $DisableVS2015)
{
    throw "Both VS2015 and VS2017 were disabled."
}

function New-MSBuildInstance()
{
    param ($msbuildExePath, $toolsetVersion)

    $instance = new-object PSObject
    $instance | add-member -type NoteProperty -Name msbuildExePath -Value $msbuildExePath
    $instance | add-member -type NoteProperty -Name toolsetVersion -Value $toolsetVersion

    return $instance
}

Write-Verbose "Executing $($MyInvocation.MyCommand.Name) with DisableVS2017=$DisableVS2017, DisableVS2015=$DisableVS2015"
$scriptsDir = split-path -parent $MyInvocation.MyCommand.Definition

$validInstances = New-Object System.Collections.ArrayList

# VS2017
Write-Verbose "`n`n"
Write-Verbose "Checking for MSBuild from VS2017 instances..."
$VisualStudio2017InstallationInstances = & $scriptsDir\findVisualStudioInstallationInstances.ps1
Write-Verbose "VS2017 Candidates: $([system.String]::Join(',', $VisualStudio2017InstallationInstances))"
foreach ($instanceCandidate in $VisualStudio2017InstallationInstances)
{
    $VCFolder= "$instanceCandidate\VC\Tools\MSVC\"

    if (Test-Path $VCFolder)
    {
        $instance = New-MSBuildInstance "$instanceCandidate\MSBuild\15.0\Bin\MSBuild.exe" "v141"
        Write-Verbose "Found $instance"
        $validInstances.Add($instance) > $null
    }
}

# VS2015 - in Program Files
Write-Verbose "`n`n"
Write-Verbose "Checking for MSBuild from VS2015 in Program Files..."
$CandidateProgramFiles = $(& $scriptsDir\getProgramFiles32bit.ps1), $(& $scriptsDir\getProgramFilesPlatformBitness.ps1)
Write-Verbose "Program Files Candidate locations: $([system.String]::Join(',', $CandidateProgramFiles))"
foreach ($ProgramFiles in $CandidateProgramFiles)
{
    $clExe= "$ProgramFiles\Microsoft Visual Studio 14.0\VC\bin\cl.exe"

    if (!(Test-Path $clExe))
    {
        Write-Verbose "$clExe - Not Found"
        continue
    }

    Write-Verbose "$clExe - Found"
    $instance = New-MSBuildInstance "$ProgramFiles\MSBuild\14.0\Bin\MSBuild.exe" "v140"
    Write-Verbose "Found $instance"
    $validInstances.Add($instance) > $null
}

# VS2015 - through the registry
function NewCppRegistryPair()
{
    param ($visualStudioEntry, $msBuildEntry)

    $instance = new-object PSObject
    $instance | add-member -type NoteProperty -Name visualStudioEntry -Value $visualStudioEntry
    $instance | add-member -type NoteProperty -Name msBuildEntry -Value $msBuildEntry

    return $instance
}

Write-Verbose "`n`n"
Write-Verbose "Checking for MSBuild from VS2015 through the registry..."

$registryPairs =
$(NewCppRegistryPair "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\visualstudio\14.0" "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\msbuild\toolsversions\14.0"),
$(NewCppRegistryPair "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\visualstudio\14.0" "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\msbuild\toolsversions\14.0")

foreach ($pair in $registryPairs)
{
    $vsEntry = $pair.visualStudioEntry
    try
    {
        $VS14InstallDir = $(gp $vsEntry InstallDir -erroraction Stop | % { $_.InstallDir })
        Write-Verbose "$vsEntry - Found"
    }
    catch
    {
        Write-Verbose "$vsEntry - Not Found"
        continue
    }

    Write-Verbose "$VS14InstallDir - Obtained from registry"
    # We want "${VS14InstallDir}..\..\VC\bin\cl.exe"
    # Doing Split-path to avoid the ..\.. from appearing in the output
    $clExePath = Split-path $VS14InstallDir -Parent
    $clExePath = Split-path $clExePath -Parent
    $clExePath = "$clExePath\VC\bin\cl.exe"

    if (!(Test-Path $clExePath))
    {
        Write-Verbose "$clExePath - Not Found"
        continue
    }

    Write-Verbose "$clExePath - Found"

    $msbuildEntry = $pair.msBuildEntry
    try
    {
        $MSBuild14 = $(gp $msbuildEntry MSBuildToolsPath -erroraction Stop | % { $_.MSBuildToolsPath })
        Write-Verbose "$msbuildEntry - Found"
    }
    catch
    {
        Write-Verbose "$msbuildEntry - Not Found"
        continue
    }

    Write-Verbose "${MSBuild14} - Obtained from registry"
    $msbuildPath = "${MSBuild14}MSBuild.exe"
    if (!(Test-Path $msbuildPath))
    {
        Write-Verbose "$msbuildPath - Not Found"
        continue
    }

    $instance = New-MSBuildInstance $msbuildPath "v140"
    Write-Verbose "Found $instance"
    $validInstances.Add($instance) > $null
}

Write-Verbose "`n`n`n"
Write-Verbose "The following MSBuild instances were found:"
foreach ($instance in $validInstances)
{
    Write-Verbose $instance
}

# Selecting
foreach ($instance in $validInstances)
{
    if (!$DisableVS2017 -and $instance.toolsetVersion -eq "v141")
    {
        return $instance.msbuildExePath, $instance.toolsetVersion
    }

    if (!$DisableVS2015 -and $instance.toolsetVersion -eq "v140")
    {
        return $instance.msbuildExePath, $instance.toolsetVersion
    }
}


throw "Could not find MSBuild version with C++ support. VS2015 or VS2017 (with C++) needs to be installed."