forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBuildEnvironment.cs
More file actions
183 lines (154 loc) · 9.28 KB
/
BuildEnvironment.cs
File metadata and controls
183 lines (154 loc) · 9.28 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
#nullable enable
namespace Wasm.Build.Tests
{
public class BuildEnvironment
{
public string DotNet { get; init; }
public bool IsWorkload { get; init; }
public string DefaultBuildArgs { get; init; }
public IDictionary<string, string> EnvVars { get; init; }
public string DirectoryBuildPropsContents { get; init; }
public string DirectoryBuildTargetsContents { get; init; }
public string LogRootPath { get; init; }
public string WorkloadPacksDir { get; init; }
public string BuiltNuGetsPath { get; init; }
public bool UseWebcil { get; init; }
public bool IsWorkloadWithMultiThreadingForDefaultFramework { get; init; }
public bool IsRunningOnCI => EnvironmentVariables.IsRunningOnCI;
public static readonly string RelativeTestAssetsPath = @"..\testassets\";
public static readonly string TestAssetsPath = Path.Combine(AppContext.BaseDirectory, "testassets");
public static readonly string TestDataPath = Path.Combine(AppContext.BaseDirectory, "data");
public static readonly string TmpPath = Path.Combine(AppContext.BaseDirectory, "wbt artifacts");
public static readonly string DefaultRuntimeIdentifier =
#if TARGET_WASI
"wasi-wasm";
#else
"browser-wasm";
#endif
private static readonly Dictionary<string, string> s_runtimePackVersions = new();
public BuildEnvironment()
{
DirectoryInfo? solutionRoot = new (AppContext.BaseDirectory);
while (solutionRoot != null)
{
if (File.Exists(Path.Combine(solutionRoot.FullName, "NuGet.config")))
{
break;
}
solutionRoot = solutionRoot.Parent;
}
string? sdkForWorkloadPath = EnvironmentVariables.SdkForWorkloadTestingPath;
if (string.IsNullOrEmpty(sdkForWorkloadPath))
{
// Is this a "local run?
string sdkDirName = string.IsNullOrEmpty(EnvironmentVariables.SdkDirName) ? "dotnet-latest" : EnvironmentVariables.SdkDirName;
string probePath = Path.Combine(Path.GetDirectoryName(typeof(BuildEnvironment).Assembly.Location)!,
"..",
"..",
"..",
sdkDirName);
if (Directory.Exists(probePath))
sdkForWorkloadPath = Path.GetFullPath(probePath);
else
throw new Exception($"Environment variable SDK_FOR_WORKLOAD_TESTING_PATH not set, and could not find it at {probePath}");
}
if (!Directory.Exists(sdkForWorkloadPath))
throw new Exception($"Could not find SDK_FOR_WORKLOAD_TESTING_PATH={sdkForWorkloadPath}");
sdkForWorkloadPath = Path.GetFullPath(sdkForWorkloadPath);
Regex runtimePackRegex = new(@"^RUNTIME_PACK_VER(\d+)$");
foreach (DictionaryEntry de in Environment.GetEnvironmentVariables())
{
Match m = runtimePackRegex.Match((string)de.Key);
if (!m.Success)
continue;
int major = int.Parse(m.Groups[1].Value);
s_runtimePackVersions[$"net{major}.0"] = (string)(de.Value ?? string.Empty);
}
DefaultBuildArgs = string.Empty;
WorkloadPacksDir = Path.Combine(sdkForWorkloadPath, "packs");
EnvVars = new Dictionary<string, string>();
bool workloadInstalled = EnvironmentVariables.SdkHasWorkloadInstalled != null && EnvironmentVariables.SdkHasWorkloadInstalled == "true";
if (workloadInstalled)
{
DirectoryBuildPropsContents = s_directoryBuildPropsForWorkloads;
DirectoryBuildTargetsContents = s_directoryBuildTargetsForWorkloads;
IsWorkload = true;
}
else
{
DirectoryBuildPropsContents = s_directoryBuildPropsForLocal;
DirectoryBuildTargetsContents = s_directoryBuildTargetsForLocal;
}
IsWorkloadWithMultiThreadingForDefaultFramework = IsMultiThreadingRuntimePackAvailableFor(BuildTestBase.DefaultTargetFramework);
if (IsWorkload && EnvironmentVariables.IsRunningOnCI && !IsWorkloadWithMultiThreadingForDefaultFramework)
{
throw new Exception(
"Expected the multithreading runtime pack to be available when running on CI." +
$" {nameof(IsRunningOnCI)} is true but {nameof(IsWorkloadWithMultiThreadingForDefaultFramework)} is false.");
}
UseWebcil = EnvironmentVariables.UseWebcil;
if (EnvironmentVariables.BuiltNuGetsPath is null || !Directory.Exists(EnvironmentVariables.BuiltNuGetsPath))
throw new Exception($"Cannot find 'BUILT_NUGETS_PATH={EnvironmentVariables.BuiltNuGetsPath}'");
BuiltNuGetsPath = EnvironmentVariables.BuiltNuGetsPath;
// `runtime` repo's build environment sets these, and they
// mess up the build for the test project, which is using a different
// dotnet
EnvVars["DOTNET_ROOT"] = sdkForWorkloadPath;
EnvVars["DOTNET_INSTALL_DIR"] = sdkForWorkloadPath;
EnvVars["DOTNET_MULTILEVEL_LOOKUP"] = "0";
EnvVars["DOTNET_SKIP_FIRST_TIME_EXPERIENCE"] = "1";
EnvVars["PATH"] = $"{sdkForWorkloadPath}{Path.PathSeparator}{Environment.GetEnvironmentVariable("PATH")}";
EnvVars["EM_WORKAROUND_PYTHON_BUG_34780"] = "1";
if (!UseWebcil)
{
// Default is 'true'
EnvVars["WasmEnableWebCil"] = "false";
}
DotNet = Path.Combine(sdkForWorkloadPath!, "dotnet");
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
DotNet += ".exe";
if (!string.IsNullOrEmpty(EnvironmentVariables.TestLogPath))
{
LogRootPath = Path.GetFullPath(EnvironmentVariables.TestLogPath);
if (!Directory.Exists(LogRootPath))
{
Directory.CreateDirectory(LogRootPath);
}
}
else
{
LogRootPath = Environment.CurrentDirectory;
}
if (Directory.Exists(TmpPath))
Directory.Delete(TmpPath, recursive: true);
Directory.CreateDirectory(TmpPath);
}
public string GetRuntimePackVersion(string tfm = BuildTestBase.DefaultTargetFramework)
=> s_runtimePackVersions.TryGetValue(tfm, out string? version)
? version
: throw new ArgumentException($"No runtime pack version found for tfm={tfm} .");
public string GetRuntimePackDir(string tfm = BuildTestBase.DefaultTargetFramework, RuntimeVariant runtimeType = RuntimeVariant.SingleThreaded)
=> Path.Combine(WorkloadPacksDir,
runtimeType is RuntimeVariant.SingleThreaded
? $"Microsoft.NETCore.App.Runtime.Mono.{DefaultRuntimeIdentifier}"
: $"Microsoft.NETCore.App.Runtime.Mono.multithread.{DefaultRuntimeIdentifier}",
GetRuntimePackVersion(tfm));
public string GetRuntimeNativeDir(string tfm = BuildTestBase.DefaultTargetFramework, RuntimeVariant runtimeType = RuntimeVariant.SingleThreaded)
=> Path.Combine(GetRuntimePackDir(tfm, runtimeType), "runtimes", DefaultRuntimeIdentifier, "native");
public bool IsMultiThreadingRuntimePackAvailableFor(string tfm)
=> IsWorkload && File.Exists(Path.Combine(GetRuntimeNativeDir(tfm, RuntimeVariant.MultiThreaded), "dotnet.native.worker.js"));
public static string WasmOverridePacksTargetsPath = Path.Combine(TestDataPath, "WasmOverridePacks.targets");
protected static string s_directoryBuildPropsForWorkloads = File.ReadAllText(Path.Combine(TestDataPath, "Workloads.Directory.Build.props"));
protected static string s_directoryBuildTargetsForWorkloads = File.ReadAllText(Path.Combine(TestDataPath, "Workloads.Directory.Build.targets"));
protected static string s_directoryBuildPropsForLocal = File.ReadAllText(Path.Combine(TestDataPath, "Local.Directory.Build.props"));
protected static string s_directoryBuildTargetsForLocal = File.ReadAllText(Path.Combine(TestDataPath, "Local.Directory.Build.targets"));
}
}