Skip to content

Commit a6b5a3d

Browse files
committed
Windows: Use pre-installed CMake and Ninja
Teach build.ps1 to search for a pre-installed CMake and Ninja from the PATH instead of only using one installed in Visual Studio. If one is not installed or on the path, the script will attempt to use one installed in Visual Studio. Preference is given to the executable found in the path environment variable.
1 parent 3860023 commit a6b5a3d

File tree

1 file changed

+36
-17
lines changed

1 file changed

+36
-17
lines changed

utils/build.ps1

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,31 @@ $vswhere = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.e
396396
$VSInstallRoot = & $vswhere -nologo -latest -products "*" -all -prerelease -property installationPath
397397
$msbuild = "$VSInstallRoot\MSBuild\Current\Bin\$BuildArchName\MSBuild.exe"
398398

399+
function Get-CMake {
400+
try {
401+
return (Get-Command "cmake.exe" -ErrorAction Stop).Source)
402+
} catch {
403+
if (Test-Path "${VSInstallRoot}\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin") {
404+
return "${VSInstallRoot}\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe"
405+
}
406+
}
407+
throw "CMake not found on Path nor in the Visual Studio Installation. Please Install CMake to continue."
408+
}
409+
410+
function Get-Ninja {
411+
try {
412+
return (Get-Command "Ninja.exe" -ErrorAction Stop).Source
413+
} catch {
414+
if (Test-Path "${VSInstallRoot}\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja") {
415+
return "${VSInstallRoot}\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja\ninja.exe"
416+
}
417+
}
418+
throw "Ninja not found on Path nor in the Visual Studio Installation. Please Install Ninja to continue."
419+
}
420+
421+
$cmake = Get-CMake
422+
$ninja = Get-Ninja
423+
399424
$NugetRoot = "$BinaryCache\nuget"
400425
$LibraryRoot = "$ImageRoot\Library"
401426

@@ -1222,14 +1247,6 @@ function Build-CMakeProject {
12221247
}
12231248

12241249
if ($Platform.OS -eq [OS]::Android) {
1225-
$vswhere = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"
1226-
$VSInstallPath = & $vswhere -nologo -latest -products * -property installationPath
1227-
if (Test-Path "${VSInstallPath}\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin") {
1228-
$env:Path = "${VSInstallPath}\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin;${VSInstallPath}\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja;${env:Path}"
1229-
Add-KeyValueIfNew $Defines CMAKE_MAKE_PROGRAM "${VSInstallPath}\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja\ninja.exe"
1230-
} else {
1231-
throw "Missing CMake and Ninja in the visual studio installation that are needed to build Android"
1232-
}
12331250
$androidNDKPath = Get-AndroidNDKPath
12341251
Add-KeyValueIfNew $Defines CMAKE_C_COMPILER (Join-Path -Path $androidNDKPath -ChildPath "toolchains\llvm\prebuilt\windows-x86_64\bin\clang.exe")
12351252
Add-KeyValueIfNew $Defines CMAKE_CXX_COMPILER (Join-Path -Path $androidNDKPath -ChildPath "toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe")
@@ -1428,6 +1445,9 @@ function Build-CMakeProject {
14281445
Add-KeyValueIfNew $Defines CMAKE_INSTALL_PREFIX $InstallTo
14291446
}
14301447

1448+
$ninja_bin = Get-NinjaPath
1449+
Add-KeyValueIfNew $Defines CMAKE_MAKE_PROGRAM "$ninja"
1450+
14311451
# Generate the project
14321452
$cmakeGenerateArgs = @("-B", $Bin, "-S", $Src, "-G", $Generator)
14331453
if ($CacheScript) {
@@ -1467,26 +1487,25 @@ function Build-CMakeProject {
14671487
} elseif ($UsePinnedCompilers.Contains("Swift")) {
14681488
$env:Path = "$(Get-PinnedToolchainRuntime);${env:Path}"
14691489
}
1470-
14711490
if ($ToBatch) {
14721491
Write-Output ""
1473-
Write-Output "echo cmake.exe $cmakeGenerateArgs"
1492+
Write-Output "echo $cmake $cmakeGenerateArgs"
14741493
} else {
1475-
Write-Host "cmake.exe $cmakeGenerateArgs"
1494+
Write-Host "$cmake $cmakeGenerateArgs"
14761495
}
1477-
Invoke-Program cmake.exe @cmakeGenerateArgs
1496+
Invoke-Program $cmake @cmakeGenerateArgs
14781497

14791498
# Build all requested targets
14801499
foreach ($Target in $BuildTargets) {
14811500
if ($Target -eq "default") {
1482-
Invoke-Program cmake.exe --build $Bin
1501+
Invoke-Program $cmake --build $Bin
14831502
} else {
1484-
Invoke-Program cmake.exe --build $Bin --target $Target
1503+
Invoke-Program $cmake --build $Bin --target $Target
14851504
}
14861505
}
14871506

14881507
if ($BuildTargets.Length -eq 0 -and $InstallTo) {
1489-
Invoke-Program cmake.exe --build $Bin --target install
1508+
Invoke-Program $cmake --build $Bin --target install
14901509
}
14911510
}
14921511

@@ -2270,7 +2289,7 @@ function Build-ExperimentalRuntime {
22702289
Invoke-VsDevShell $BuildPlatform
22712290

22722291
Push-Location "${SourceCache}\swift\Runtimes"
2273-
Start-Process -Wait -WindowStyle Hidden -FilePath cmake.exe -ArgumentList @("-P", "Resync.cmake")
2292+
Start-Process -Wait -WindowStyle Hidden -FilePath $cmake -ArgumentList @("-P", "Resync.cmake")
22742293
Pop-Location
22752294
}
22762295

@@ -2681,7 +2700,7 @@ function Test-LLBuild {
26812700
# Build additional llvm executables needed by tests
26822701
Invoke-IsolatingEnvVars {
26832702
Invoke-VsDevShell $BuildPlatform
2684-
Invoke-Program ninja.exe -C (Get-ProjectBinaryCache $BuildPlatform BuildTools) FileCheck not
2703+
Invoke-Program $ninja -C (Get-ProjectBinaryCache $BuildPlatform BuildTools) FileCheck not
26852704
}
26862705

26872706
Invoke-IsolatingEnvVars {

0 commit comments

Comments
 (0)