Skip to content

Commit 8c6a049

Browse files
authored
Enable using the Ninja generator instead of Visual Studio on Windows builds. (#41897)
* Move DacTableGen out of the CMake build and into tools-local. * Attempt: usenmakefiles means ninja. * Make changes to Windows build to support non-VS generators like ninja. * Use CMake 3.16 PCHs. * Remove explicit process count. * Build and run DacTableGen via the .NET CLI we pull down for the build instead of via CMake. * Update configurecompiler.cmake to specify language (needed for Ninja). Signed-off-by: Jeremy Koritzinsky <[email protected]> * Get Ninja build working again. * Pass configuration to cmake configure step for Ninja build. * Rename flag * Fix configure step for cross-arch native build Signed-off-by: Jeremy Koritzinsky <[email protected]> * Support building via Ninja for coreclr via an MSBuild property Signed-off-by: Jeremy Koritzinsky <[email protected]> * Implement support for opening the CoreCLR solution in VS via the -vs flag. * Fix setting generator to Ninja. * Merge libraries gen-buildsys-win.bat with runtime gen-buildsys.cmd to share windows cmake configure infra. * Enable building libraries native build with ninja. * Centralize setting CMAKE_SYSTEM_VERSION for windows builds. Signed-off-by: Jeremy Koritzinsky <[email protected]> * Update host build to use shared gen-buildsys.cmd * Fix multi-config generator detection. Signed-off-by: Jeremy Koritzinsky <[email protected]> * Exit with success when we build successfully. Signed-off-by: Jeremy Koritzinsky <[email protected]> * Enable and fix up Ninja build for hosts. Signed-off-by: Jeremy Koritzinsky <[email protected]> * Only link delayimp.lib on Windows Signed-off-by: Jeremy Koritzinsky <[email protected]> * Remove "overriding /W3 with /W1" warning in Ninja build of hosts by turning on /W3 and disabling any new warnings we hit. * Reduce command line override warnings by adopting the MSVC_RUNTIME_LIBRARY abstraction feature in CMake 3.14+ (below the minimum requirement on Windows). * Use the delayed expansion syntax to correctly pass in the config to cmake. * Move all usages of MSVC runtime library selection to the CMake abstraction to reduce console spew about overridden flags. Signed-off-by: Jeremy Koritzinsky <[email protected]> * Condition the rest of the compiler options correctly so they don't show up as invalid parameter warnings for assembly builds. * Correctly build DIALib and DacTableGen as AnyCPU * Fix WRITE_BARRIER_CHECK define Signed-off-by: Jeremy Koritzinsky <[email protected]> * Fix passing arch for MSBuild build. Signed-off-by: Jeremy Koritzinsky <[email protected]> * Configure ASM compiler command line format in configurecompiler.cmake and use CMake's assembly compilation step to build ASM files for ARM/ARM64 instead of manually building them and adding the generated files as additional sources. * Cleanup after fixing ARM/ARM64 support. * Rename variable * Condition PCH's for C++-only as needed to fix using them on non-Windows (where we compile C as C instead of as C++). * Fix CoreCLR native test build on windows to point to new gen-buildsys location. * Add missing compilation options in Ninja build that are implicitly defined in the MSBuild build. * Disable analyzers for DacTableGen. Remove /MAP linker flag (that's for local testing). * Upgrade to CMake 3.16.4 * Don't warn on unused variables in CMake (these usually vary on platform/config) Signed-off-by: Jeremy Koritzinsky <[email protected]> * Manually compile ARM and ARM64 asm files when using the VS generator since it doesn't support emitting MARMASM item types in projects (and as a result cannot compile ARM or ARM64 asm without assistance). * Enable CMake policy in test build to use CMAKE_MSVC_RUNTIME_LIBRARY variable. * Update initially disabled warnings in host. * Add -ninja arg to root scripts to enable using Ninja. * Enable using Ninja in CoreCLR CI. * Try to fix passing the -ninja arg in yaml. Signed-off-by: Jeremy Koritzinsky <[email protected]> * Add ninja to the Windows invocation * Fix 2-phase cross targeting to do one phase at a time and initialize the VC++ environment for the correct build tools each time instead of interleaving them and relying on Visual Studio to handle the differing targets. * Restore old /Ox optimization setting for Release. * Fix typos, don't pass exception flags to C files. * Remove unneeded pragma. * Fix .pgd file installation. Signed-off-by: Jeremy Koritzinsky <[email protected]> * Add mention of the `-ninja` flag in the Building CoreCLR documentation. Signed-off-by: Jeremy Koritzinsky <[email protected]> * Mention installation locations for Ninja and warn while building if CMake is older than 3.16.0. * Update docs/workflow/requirements/windows-requirements.md
1 parent 33056a9 commit 8c6a049

File tree

76 files changed

+17952
-768
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+17952
-768
lines changed

docs/workflow/building/coreclr/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ CoreCLR also supports a 'checked' build type which has asserts enabled like 'deb
2222
./build.sh -subset clr -configuration checked
2323
```
2424

25+
If you want to use Ninja to drive the native build instead of Visual Studio MSBuild (on Windows) or Make (on non-Windows), you can pass the `-ninja` flag to the build script as follows:
26+
```
27+
./build.cmd -subset clr -ninja
28+
```
29+
30+
We recommend using Ninja for building the project on Windows since it more efficiently uses the build machine's resources for the native runtime build in comparison to Visual Studio's MSBuild.
31+
2532
To pass extra compiler/linker flags to the coreclr build, set the environment variables `EXTRA_CFLAGS`, `EXTRA_CXXFLAGS` and `EXTRA_LDFLAGS` as needed. Don't set `CFLAGS`/`CXXFLAGS`/`LDFLAGS` directly as that might lead to configure-time tests failing.
2633

2734
This will produce outputs as follows:

docs/workflow/requirements/freebsd-requirements.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,12 @@ Install the following packages:
5757
- lttng-ust
5858
- krb5
5959
- openssl (optional)
60+
- ninja (optional, enables building native code with ninja instead of make)
6061

6162
The lines to install all the packages above using package manager.
6263

6364
```sh
64-
sudo pkg install --yes libunwind icu libinotify lttng-ust krb5 cmake autoconf automake openssl
65+
sudo pkg install --yes libunwind icu libinotify lttng-ust krb5 cmake autoconf automake openssl ninja
6566
```
6667

6768
Additionally, working dotnet cli with SDK is needed. On other platforms this would be downloaded automatically during build but it is not currently available for FreeBSD.

docs/workflow/requirements/linux-requirements.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Install the following packages for the toolchain:
5050
- libkrb5-dev
5151
- libnuma-dev (optional, enables numa support)
5252
- zlib1g-dev
53+
- ninja (optional, enables building native code with ninja instead of make)
5354

5455
The following dependencies are needed if Mono Runtime is enabled (default behavior):
5556

@@ -61,7 +62,7 @@ The following dependencies are needed if Mono Runtime is enabled (default behavi
6162
sudo apt-get install -y cmake llvm-9 clang-9 autoconf automake \
6263
libtool build-essential python curl git lldb-6.0 liblldb-6.0-dev \
6364
libunwind8 libunwind8-dev gettext libicu-dev liblttng-ust-dev \
64-
libssl-dev libnuma-dev libkrb5-dev zlib1g-dev
65+
libssl-dev libnuma-dev libkrb5-dev zlib1g-dev ninja
6566
```
6667

6768
You now have all the required components.

docs/workflow/requirements/macos-requirements.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ Install the following packages:
2828
- openssl 1.1
2929
- pkg-config
3030
- python3
31+
- ninja (optional, enables building native code with ninja instead of make)
3132

32-
You can install all the packages above using Homebrew by running this command in the repository root:
33+
You can install all the required packages above using Homebrew by running this command in the repository root:
3334

3435
```
3536
brew bundle --no-lock --file eng/Brewfile

docs/workflow/requirements/windows-requirements.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,13 @@ The dotnet/runtime repository requires at least Visual Studio 2019 16.6.
4242
- Add its location (e.g. C:\Program Files (x86)\CMake\bin) to the PATH environment variable.
4343
The installation script has a check box to do this, but you can do it yourself after the fact following the instructions at [Adding to the Default PATH variable](#adding-to-the-default-path-variable).
4444

45-
The dotnet/runtime repository requires at least CMake 3.15.5.
45+
The dotnet/runtime repository recommends using CMake 3.16.0 or newer, but works with CMake 3.15.5.
46+
47+
## Ninja (optional)
48+
49+
- Install Ninja in one of the two following ways
50+
- [Download the executable](https://github.com/ninja-build/ninja/releases) and add its location to [the Default PATH variable](#adding-to-the-default-path-variable).
51+
- [Install via a package manager](https://github.com/ninja-build/ninja/wiki/Pre-built-Ninja-packages), which should automatically add it to the PATH environment variable.
4652

4753
## Python
4854

eng/Subsets.props

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
<ItemGroup>
8787
<!-- CoreClr -->
8888
<SubsetName Include="Clr" Description="The CoreCLR runtime, LinuxDac, CoreLib (+ native), tools and packages." />
89+
<SubsetName Include="Clr.DacTools" Description="Managed tools that support building a runtime that supports debugging (such as DacTableGen)." />
8990
<SubsetName Include="Clr.Runtime" Description="The CoreCLR .NET runtime." />
9091
<SubsetName Include="Clr.PalTests" Description="The CoreCLR PAL tests." />
9192
<SubsetName Include="Clr.PalTestList" Description="Generate the list of the CoreCLR PAL tests. When using the command line, use Clr.PalTests instead." />
@@ -148,6 +149,12 @@
148149
<ProjectToBuild Include="$(CoreClrProjectRoot)src\System.Private.CoreLib\System.Private.CoreLib.csproj" Category="clr" />
149150
</ItemGroup>
150151

152+
<ItemGroup Condition="'$(TargetOS)' == 'Windows_NT' and ($(_subset.Contains('+clr.dactools+')) or $(_subset.Contains('+clr.runtime+')))">
153+
<ProjectToBuild Include="$(CoreClrProjectRoot)src\ToolBox\SOS\DacTableGen\DacTableGen.csproj;
154+
$(CoreClrProjectRoot)src\ToolBox\SOS\DIALib\DIALib.ilproj"
155+
Category="clr"/>
156+
</ItemGroup>
157+
151158
<PropertyGroup Condition="$(_subset.Contains('+clr.runtime+'))">
152159
<ClrRuntimeBuildSubsets>$(ClrRuntimeBuildSubsets);ClrRuntimeSubset=true;ClrJitSubset=true</ClrRuntimeBuildSubsets>
153160
</PropertyGroup>

eng/build.ps1

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Param(
1616
[ValidateSet("Debug","Release","Checked")][string][Alias('rc')]$runtimeConfiguration,
1717
[ValidateSet("Debug","Release")][string][Alias('lc')]$librariesConfiguration,
1818
[ValidateSet("CoreCLR","Mono")][string][Alias('rf')]$runtimeFlavor,
19+
[switch]$ninja,
1920
[Parameter(ValueFromRemainingArguments=$true)][String[]]$properties
2021
)
2122

@@ -74,6 +75,9 @@ function Get-Help() {
7475
Write-Host " -testscope Scope tests, allowed values: innerloop, outerloop, all."
7576
Write-Host ""
7677

78+
Write-Host "Native build settings:"
79+
Write-Host " -ninja Use Ninja instead of MSBuild to run the native build."
80+
7781
Write-Host "Command-line arguments not listed above are passed through to MSBuild."
7882
Write-Host "The above arguments can be shortened as much as to be unambiguous."
7983
Write-Host "(Example: -con for configuration, -t for test, etc.)."
@@ -117,9 +121,28 @@ if ($subset -eq 'help') {
117121
}
118122

119123
if ($vs) {
120-
. $PSScriptRoot\common\tools.ps1
121-
122-
if (-Not (Test-Path $vs)) {
124+
if ($vs -ieq "coreclr.sln") {
125+
# If someone passes in coreclr.sln (case-insensitive),
126+
# launch the generated CMake solution.
127+
$archToOpen = $arch[0]
128+
$configToOpen = $configuration[0]
129+
if ($runtimeConfiguration) {
130+
$configToOpen = $runtimeConfiguration
131+
}
132+
$vs = Split-Path $PSScriptRoot -Parent | Join-Path -ChildPath "artifacts\obj\coreclr" | Join-Path -ChildPath "Windows_NT.$archToOpen.$((Get-Culture).TextInfo.ToTitleCase($configToOpen))" | Join-Path -ChildPath "CoreCLR.sln"
133+
if (-Not (Test-Path $vs)) {
134+
$repoRoot = Split-Path $PSScriptRoot -Parent
135+
Invoke-Expression "& `"$repoRoot/src/coreclr/build-runtime.cmd`" -configureonly -$archToOpen -$configToOpen"
136+
if ($lastExitCode -ne 0) {
137+
Write-Error "Failed to generate the CoreCLR solution file."
138+
exit 1
139+
}
140+
if (-Not (Test-Path $vs)) {
141+
Write-Error "Unable to find the CoreCLR solution file at $vs."
142+
}
143+
}
144+
}
145+
elseif (-Not (Test-Path $vs)) {
123146
$solution = $vs
124147

125148
if ($runtimeFlavor -eq "Mono") {
@@ -153,6 +176,8 @@ if ($vs) {
153176
}
154177
}
155178
}
179+
180+
. $PSScriptRoot\common\tools.ps1
156181

157182
# This tells .NET Core to use the bootstrapped runtime
158183
$env:DOTNET_ROOT=InitializeDotNetCli -install:$true -createSdkLocationFile:$true
@@ -204,6 +229,7 @@ foreach ($argument in $PSBoundParameters.Keys)
204229
"allconfigurations" { $arguments += " /p:BuildAllConfigurations=true" }
205230
"properties" { $arguments += " " + $properties }
206231
"verbosity" { $arguments += " -$argument " + $($PSBoundParameters[$argument]) }
232+
"ninja" { $arguments += " /p:Ninja=$($PSBoundParameters[$argument])" }
207233
# configuration and arch can be specified multiple times, so they should be no-ops here
208234
"configuration" {}
209235
"arch" {}

eng/build.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ usage()
7777
echo " --gccx.y Optional argument to build using gcc version x.y."
7878
echo " --portablebuild Optional argument: set to false to force a non-portable build."
7979
echo " --keepnativesymbols Optional argument: set to true to keep native symbols/debuginfo in generated binaries."
80+
echo " --ninja Optional argument: set to true to use Ninja instead of Make to run the native build."
8081
echo ""
8182

8283
echo "Command line arguments starting with '/p:' are passed through to MSBuild."
@@ -417,6 +418,26 @@ while [[ $# > 0 ]]; do
417418
shift 2
418419
;;
419420

421+
422+
-ninja)
423+
if [ -z ${2+x} ]; then
424+
arguments="$arguments /p:Ninja=true"
425+
shift 1
426+
else
427+
ninja="$(echo "$2" | awk '{print tolower($0)}')"
428+
if [ "$ninja" = true ]; then
429+
arguments="$arguments /p:Ninja=true"
430+
shift 2
431+
elif [ "$ninja" = false ]; then
432+
arguments="$arguments /p:Ninja=false"
433+
shift 2
434+
else
435+
arguments="$arguments /p:Ninja=true"
436+
shift 1
437+
fi
438+
fi
439+
;;
440+
420441
*)
421442
extraargs="$extraargs $1"
422443
shift 1

0 commit comments

Comments
 (0)