Skip to content

Commit dee4ddb

Browse files
committed
C#: Only set UseSharedCompilation=false in autobuilder when needed
Since we are now able to trace shared compilation builds on Linux and macOS (starting from .NET Core 3), and always were able to on Windows, there is no need to set `UseSharedCompilation=false` in those cases. This may have a positive performance impact, as shared compilation is generally faster then non-shared compilation.
1 parent ee185ea commit dee4ddb

File tree

6 files changed

+118
-50
lines changed

6 files changed

+118
-50
lines changed

csharp/autobuilder/Semmle.Autobuild.Tests/BuildScripts.cs

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public class BuildScriptTests
185185
// Records the arguments passed to StartCallback.
186186
IList<string> StartCallbackIn = new List<string>();
187187

188-
void StartCallback(string s)
188+
void StartCallback(string s, bool silent)
189189
{
190190
StartCallbackIn.Add(s);
191191
}
@@ -194,7 +194,7 @@ void StartCallback(string s)
194194
IList<string> EndCallbackIn = new List<string>();
195195
IList<int> EndCallbackReturn = new List<int>();
196196

197-
void EndCallback(int ret, string s)
197+
void EndCallback(int ret, string s, bool silent)
198198
{
199199
EndCallbackReturn.Add(ret);
200200
EndCallbackIn.Add(s);
@@ -203,7 +203,7 @@ void EndCallback(int ret, string s)
203203
[Fact]
204204
public void TestBuildCommand()
205205
{
206-
var cmd = BuildScript.Create("abc", "def ghi", null, null);
206+
var cmd = BuildScript.Create("abc", "def ghi", false, null, null);
207207

208208
Actions.RunProcess["abc def ghi"] = 1;
209209
cmd.Run(Actions, StartCallback, EndCallback);
@@ -216,7 +216,7 @@ public void TestBuildCommand()
216216
[Fact]
217217
public void TestAnd1()
218218
{
219-
var cmd = BuildScript.Create("abc", "def ghi", null, null) & BuildScript.Create("odasa", null, null, null);
219+
var cmd = BuildScript.Create("abc", "def ghi", false, null, null) & BuildScript.Create("odasa", null, false, null, null);
220220

221221
Actions.RunProcess["abc def ghi"] = 1;
222222
cmd.Run(Actions, StartCallback, EndCallback);
@@ -230,7 +230,7 @@ public void TestAnd1()
230230
[Fact]
231231
public void TestAnd2()
232232
{
233-
var cmd = BuildScript.Create("odasa", null, null, null) & BuildScript.Create("abc", "def ghi", null, null);
233+
var cmd = BuildScript.Create("odasa", null, false, null, null) & BuildScript.Create("abc", "def ghi", false, null, null);
234234

235235
Actions.RunProcess["abc def ghi"] = 1;
236236
Actions.RunProcess["odasa "] = 0;
@@ -250,7 +250,7 @@ public void TestAnd2()
250250
[Fact]
251251
public void TestOr1()
252252
{
253-
var cmd = BuildScript.Create("odasa", null, null, null) | BuildScript.Create("abc", "def ghi", null, null);
253+
var cmd = BuildScript.Create("odasa", null, false, null, null) | BuildScript.Create("abc", "def ghi", false, null, null);
254254

255255
Actions.RunProcess["abc def ghi"] = 1;
256256
Actions.RunProcess["odasa "] = 0;
@@ -266,7 +266,7 @@ public void TestOr1()
266266
[Fact]
267267
public void TestOr2()
268268
{
269-
var cmd = BuildScript.Create("abc", "def ghi", null, null) | BuildScript.Create("odasa", null, null, null);
269+
var cmd = BuildScript.Create("abc", "def ghi", false, null, null) | BuildScript.Create("odasa", null, false, null, null);
270270

271271
Actions.RunProcess["abc def ghi"] = 1;
272272
Actions.RunProcess["odasa "] = 0;
@@ -375,7 +375,7 @@ public void TestDefaultCSharpAutoBuilder()
375375
Actions.RunProcess["cmd.exe /C dotnet --info"] = 0;
376376
Actions.RunProcess["cmd.exe /C dotnet clean test.csproj"] = 0;
377377
Actions.RunProcess["cmd.exe /C dotnet restore test.csproj"] = 0;
378-
Actions.RunProcess[@"cmd.exe /C C:\odasa\tools\odasa index --auto dotnet build --no-incremental /p:UseSharedCompilation=false test.csproj"] = 0;
378+
Actions.RunProcess[@"cmd.exe /C C:\odasa\tools\odasa index --auto dotnet build --no-incremental test.csproj"] = 0;
379379
Actions.RunProcess[@"cmd.exe /C C:\codeql\tools\java\bin\java -jar C:\codeql\csharp\tools\extractor-asp.jar ."] = 0;
380380
Actions.RunProcess[@"cmd.exe /C C:\odasa\tools\odasa index --xml --extensions config csproj props xml"] = 0;
381381
Actions.FileExists["csharp.log"] = true;
@@ -404,6 +404,9 @@ public void TestLinuxCSharpAutoBuilder()
404404
Actions.RunProcess["dotnet --info"] = 0;
405405
Actions.RunProcess["dotnet clean test.csproj"] = 0;
406406
Actions.RunProcess["dotnet restore test.csproj"] = 0;
407+
Actions.RunProcess["dotnet --list-runtimes"] = 0;
408+
Actions.RunProcessOut["dotnet --list-runtimes"] = @"Microsoft.AspNetCore.App 2.2.5 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
409+
Microsoft.NETCore.App 2.2.5 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]";
407410
Actions.RunProcess[@"C:\odasa/tools/odasa index --auto dotnet build --no-incremental /p:UseSharedCompilation=false test.csproj"] = 0;
408411
Actions.RunProcess[@"C:\codeql\tools\java/bin/java -jar C:\codeql\csharp/tools/extractor-asp.jar ."] = 0;
409412
Actions.RunProcess[@"C:\odasa/tools/odasa index --xml --extensions config csproj props xml"] = 0;
@@ -424,7 +427,7 @@ public void TestLinuxCSharpAutoBuilder()
424427
Actions.LoadXml["test.csproj"] = xml;
425428

426429
var autobuilder = CreateAutoBuilder("csharp", false);
427-
TestAutobuilderScript(autobuilder, 0, 6);
430+
TestAutobuilderScript(autobuilder, 0, 7);
428431
}
429432

430433
[Fact]
@@ -748,7 +751,7 @@ public void TestWindowCSharpMsBuild()
748751
TestAutobuilderScript(autobuilder, 0, 6);
749752
}
750753

751-
[Fact]
754+
[Fact]
752755
public void TestWindowCSharpMsBuildMultipleSolutions()
753756
{
754757
Actions.RunProcess[@"cmd.exe /C C:\odasa\tools\csharp\nuget\nuget.exe restore test1.csproj"] = 0;
@@ -874,6 +877,9 @@ public void TestSkipNugetDotnet()
874877
Actions.RunProcess["dotnet --info"] = 0;
875878
Actions.RunProcess["dotnet clean test.csproj"] = 0;
876879
Actions.RunProcess["dotnet restore test.csproj"] = 0;
880+
Actions.RunProcess["dotnet --list-runtimes"] = 0;
881+
Actions.RunProcessOut["dotnet --list-runtimes"] = @"Microsoft.AspNetCore.App 2.1.3 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
882+
Microsoft.NETCore.App 2.1.3 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]";
877883
Actions.RunProcess[@"C:\odasa/tools/odasa index --auto dotnet build --no-incremental /p:UseSharedCompilation=false --no-restore test.csproj"] = 0;
878884
Actions.RunProcess[@"C:\codeql\tools\java/bin/java -jar C:\codeql\csharp/tools/extractor-asp.jar ."] = 0;
879885
Actions.RunProcess[@"C:\odasa/tools/odasa index --xml --extensions config csproj props xml"] = 0;
@@ -894,7 +900,7 @@ public void TestSkipNugetDotnet()
894900
Actions.LoadXml["test.csproj"] = xml;
895901

896902
var autobuilder = CreateAutoBuilder("csharp", false, dotnetArguments: "--no-restore"); // nugetRestore=false does not work for now.
897-
TestAutobuilderScript(autobuilder, 0, 6);
903+
TestAutobuilderScript(autobuilder, 0, 7);
898904
}
899905

900906
[Fact]
@@ -909,7 +915,10 @@ public void TestDotnetVersionNotInstalled()
909915
Actions.RunProcess[@"C:\Project/.dotnet/dotnet --info"] = 0;
910916
Actions.RunProcess[@"C:\Project/.dotnet/dotnet clean test.csproj"] = 0;
911917
Actions.RunProcess[@"C:\Project/.dotnet/dotnet restore test.csproj"] = 0;
912-
Actions.RunProcess[@"C:\odasa/tools/odasa index --auto C:\Project/.dotnet/dotnet build --no-incremental /p:UseSharedCompilation=false test.csproj"] = 0;
918+
Actions.RunProcess[@"C:\Project/.dotnet/dotnet --list-runtimes"] = 0;
919+
Actions.RunProcessOut[@"C:\Project/.dotnet/dotnet --list-runtimes"] = @"Microsoft.AspNetCore.App 3.0.0 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
920+
Microsoft.NETCore.App 3.0.0 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]";
921+
Actions.RunProcess[@"C:\odasa/tools/odasa index --auto C:\Project/.dotnet/dotnet build --no-incremental test.csproj"] = 0;
913922
Actions.RunProcess[@"C:\codeql\tools\java/bin/java -jar C:\codeql\csharp/tools/extractor-asp.jar ."] = 0;
914923
Actions.RunProcess[@"C:\odasa/tools/odasa index --xml --extensions config csproj props xml"] = 0;
915924
Actions.FileExists["csharp.log"] = true;
@@ -930,21 +939,27 @@ public void TestDotnetVersionNotInstalled()
930939
Actions.LoadXml["test.csproj"] = xml;
931940

932941
var autobuilder = CreateAutoBuilder("csharp", false, dotnetVersion: "2.1.3");
933-
TestAutobuilderScript(autobuilder, 0, 11);
942+
TestAutobuilderScript(autobuilder, 0, 12);
934943
}
935944

936945
[Fact]
937946
public void TestDotnetVersionAlreadyInstalled()
938947
{
939948
Actions.RunProcess["dotnet --list-sdks"] = 0;
940-
Actions.RunProcessOut["dotnet --list-sdks"] = "2.1.3 [C:\\Program Files\\dotnet\\sdks]\n2.1.4 [C:\\Program Files\\dotnet\\sdks]";
949+
Actions.RunProcessOut["dotnet --list-sdks"] = @"2.1.3 [C:\Program Files\dotnet\sdks]
950+
2.1.4 [C:\Program Files\dotnet\sdks]";
941951
Actions.RunProcess[@"curl -L -sO https://dot.net/v1/dotnet-install.sh"] = 0;
942952
Actions.RunProcess[@"chmod u+x dotnet-install.sh"] = 0;
943953
Actions.RunProcess[@"./dotnet-install.sh --channel release --version 2.1.3 --install-dir C:\Project/.dotnet"] = 0;
944954
Actions.RunProcess[@"rm dotnet-install.sh"] = 0;
945955
Actions.RunProcess[@"C:\Project/.dotnet/dotnet --info"] = 0;
946956
Actions.RunProcess[@"C:\Project/.dotnet/dotnet clean test.csproj"] = 0;
947957
Actions.RunProcess[@"C:\Project/.dotnet/dotnet restore test.csproj"] = 0;
958+
Actions.RunProcess[@"C:\Project/.dotnet/dotnet --list-runtimes"] = 0;
959+
Actions.RunProcessOut[@"C:\Project/.dotnet/dotnet --list-runtimes"] = @"Microsoft.AspNetCore.App 2.1.3 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
960+
Microsoft.AspNetCore.App 2.1.4 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
961+
Microsoft.NETCore.App 2.1.3 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
962+
Microsoft.NETCore.App 2.1.4 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]";
948963
Actions.RunProcess[@"C:\odasa/tools/odasa index --auto C:\Project/.dotnet/dotnet build --no-incremental /p:UseSharedCompilation=false test.csproj"] = 0;
949964
Actions.RunProcess[@"C:\codeql\tools\java/bin/java -jar C:\codeql\csharp/tools/extractor-asp.jar ."] = 0;
950965
Actions.RunProcess[@"C:\odasa/tools/odasa index --xml --extensions config csproj props xml"] = 0;
@@ -966,7 +981,7 @@ public void TestDotnetVersionAlreadyInstalled()
966981
Actions.LoadXml["test.csproj"] = xml;
967982

968983
var autobuilder = CreateAutoBuilder("csharp", false, dotnetVersion: "2.1.3");
969-
TestAutobuilderScript(autobuilder, 0, 11);
984+
TestAutobuilderScript(autobuilder, 0, 12);
970985
}
971986

972987
[Fact]
@@ -979,7 +994,7 @@ public void TestDotnetVersionWindows()
979994
Actions.RunProcess[@"cmd.exe /C C:\Project\.dotnet\dotnet --info"] = 0;
980995
Actions.RunProcess[@"cmd.exe /C C:\Project\.dotnet\dotnet clean test.csproj"] = 0;
981996
Actions.RunProcess[@"cmd.exe /C C:\Project\.dotnet\dotnet restore test.csproj"] = 0;
982-
Actions.RunProcess[@"cmd.exe /C C:\odasa\tools\odasa index --auto C:\Project\.dotnet\dotnet build --no-incremental /p:UseSharedCompilation=false test.csproj"] = 0;
997+
Actions.RunProcess[@"cmd.exe /C C:\odasa\tools\odasa index --auto C:\Project\.dotnet\dotnet build --no-incremental test.csproj"] = 0;
983998
Actions.RunProcess[@"cmd.exe /C C:\codeql\tools\java\bin\java -jar C:\codeql\csharp\tools\extractor-asp.jar ."] = 0;
984999
Actions.RunProcess[@"cmd.exe /C C:\odasa\tools\odasa index --xml --extensions config csproj props xml"] = 0;
9851000
Actions.FileExists["csharp.log"] = true;

csharp/autobuilder/Semmle.Autobuild/Autobuilder.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,17 @@ public int AttemptBuild()
214214
if (Options.IgnoreErrors)
215215
script |= BuildScript.Success;
216216

217-
void startCallback(string s) => Log(Severity.Info, $"\nRunning {s}");
218-
void exitCallback(int ret, string msg) => Log(Severity.Info, $"Exit code {ret}{(string.IsNullOrEmpty(msg) ? "" : $": {msg}")}");
217+
void startCallback(string s, bool silent)
218+
{
219+
if (!silent) Log(Severity.Info, $"\nRunning {s}");
220+
}
221+
222+
void exitCallback(int ret, string msg, bool silent)
223+
{
224+
if (!silent)
225+
Log(Severity.Info, $"Exit code {ret}{(string.IsNullOrEmpty(msg) ? "" : $": {msg}")}");
226+
}
227+
219228
return script.Run(Actions, startCallback, exitCallback);
220229
}
221230

csharp/autobuilder/Semmle.Autobuild/BuildActions.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,8 @@ ProcessStartInfo GetProcessStartInfo(string exe, string arguments, string workin
141141
pi.WorkingDirectory = workingDirectory;
142142

143143
// Environment variables can only be used when not redirecting stdout
144-
if (!redirectStandardOutput)
145-
{
146-
pi.Environment["UseSharedCompilation"] = "false";
147-
if (environment != null)
148-
environment.ForEach(kvp => pi.Environment[kvp.Key] = kvp.Value);
149-
}
144+
if (!redirectStandardOutput && environment != null)
145+
environment.ForEach(kvp => pi.Environment[kvp.Key] = kvp.Value);
150146
return pi;
151147
}
152148

csharp/autobuilder/Semmle.Autobuild/BuildScript.cs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public abstract class BuildScript
2525
/// an exit message.
2626
/// </param>
2727
/// <returns>The exit code from this build script.</returns>
28-
public abstract int Run(IBuildActions actions, Action<string> startCallback, Action<int, string> exitCallBack);
28+
public abstract int Run(IBuildActions actions, Action<string, bool> startCallback, Action<int, string, bool> exitCallBack);
2929

3030
/// <summary>
3131
/// Run this build command.
@@ -44,33 +44,36 @@ public abstract class BuildScript
4444
/// </param>
4545
/// <param name="stdout">Contents of standard out.</param>
4646
/// <returns>The exit code from this build script.</returns>
47-
public abstract int Run(IBuildActions actions, Action<string> startCallback, Action<int, string> exitCallBack, out IList<string> stdout);
47+
public abstract int Run(IBuildActions actions, Action<string, bool> startCallback, Action<int, string, bool> exitCallBack, out IList<string> stdout);
4848

4949
class BuildCommand : BuildScript
5050
{
5151
readonly string exe, arguments, workingDirectory;
5252
readonly IDictionary<string, string> environment;
53+
readonly bool silent;
5354

5455
/// <summary>
5556
/// Create a simple build command.
5657
/// </summary>
5758
/// <param name="exe">The executable to run.</param>
5859
/// <param name="argumentsOpt">The arguments to the executable, or null.</param>
60+
/// <param name="silent">Whether this command should run silently.</param>
5961
/// <param name="workingDirectory">The working directory (<code>null</code> for current directory).</param>
6062
/// <param name="environment">Additional environment variables.</param>
61-
public BuildCommand(string exe, string argumentsOpt, string workingDirectory = null, IDictionary<string, string> environment = null)
63+
public BuildCommand(string exe, string argumentsOpt, bool silent, string workingDirectory = null, IDictionary<string, string> environment = null)
6264
{
6365
this.exe = exe;
6466
this.arguments = argumentsOpt ?? "";
67+
this.silent = silent;
6568
this.workingDirectory = workingDirectory;
6669
this.environment = environment;
6770
}
6871

6972
public override string ToString() => exe + " " + arguments;
7073

71-
public override int Run(IBuildActions actions, Action<string> startCallback, Action<int, string> exitCallBack)
74+
public override int Run(IBuildActions actions, Action<string, bool> startCallback, Action<int, string, bool> exitCallBack)
7275
{
73-
startCallback(this.ToString());
76+
startCallback(this.ToString(), silent);
7477
var ret = 1;
7578
var retMessage = "";
7679
try
@@ -83,13 +86,13 @@ public override int Run(IBuildActions actions, Action<string> startCallback, Act
8386
retMessage = ex.Message;
8487
}
8588

86-
exitCallBack(ret, retMessage);
89+
exitCallBack(ret, retMessage, silent);
8790
return ret;
8891
}
8992

90-
public override int Run(IBuildActions actions, Action<string> startCallback, Action<int, string> exitCallBack, out IList<string> stdout)
93+
public override int Run(IBuildActions actions, Action<string, bool> startCallback, Action<int, string, bool> exitCallBack, out IList<string> stdout)
9194
{
92-
startCallback(this.ToString());
95+
startCallback(this.ToString(), silent);
9396
var ret = 1;
9497
var retMessage = "";
9598
try
@@ -102,7 +105,7 @@ public override int Run(IBuildActions actions, Action<string> startCallback, Act
102105
retMessage = ex.Message;
103106
stdout = new string[0];
104107
}
105-
exitCallBack(ret, retMessage);
108+
exitCallBack(ret, retMessage, silent);
106109
return ret;
107110
}
108111

@@ -116,9 +119,9 @@ public ReturnBuildCommand(Func<IBuildActions, int> func)
116119
this.func = func;
117120
}
118121

119-
public override int Run(IBuildActions actions, Action<string> startCallback, Action<int, string> exitCallBack) => func(actions);
122+
public override int Run(IBuildActions actions, Action<string, bool> startCallback, Action<int, string, bool> exitCallBack) => func(actions);
120123

121-
public override int Run(IBuildActions actions, Action<string> startCallback, Action<int, string> exitCallBack, out IList<string> stdout)
124+
public override int Run(IBuildActions actions, Action<string, bool> startCallback, Action<int, string, bool> exitCallBack, out IList<string> stdout)
122125
{
123126
stdout = new string[0];
124127
return func(actions);
@@ -142,7 +145,7 @@ public BindBuildScript(BuildScript s1, Func<int, BuildScript> s2)
142145
this.s2b = s2;
143146
}
144147

145-
public override int Run(IBuildActions actions, Action<string> startCallback, Action<int, string> exitCallBack)
148+
public override int Run(IBuildActions actions, Action<string, bool> startCallback, Action<int, string, bool> exitCallBack)
146149
{
147150
int ret1;
148151
if (s2a != null)
@@ -155,7 +158,7 @@ public override int Run(IBuildActions actions, Action<string> startCallback, Act
155158
return s2b(ret1).Run(actions, startCallback, exitCallBack);
156159
}
157160

158-
public override int Run(IBuildActions actions, Action<string> startCallback, Action<int, string> exitCallBack, out IList<string> stdout)
161+
public override int Run(IBuildActions actions, Action<string, bool> startCallback, Action<int, string, bool> exitCallBack, out IList<string> stdout)
159162
{
160163
var ret1 = s1.Run(actions, startCallback, exitCallBack, out var stdout1);
161164
var ret2 = (s2a != null ? s2a(stdout1, ret1) : s2b(ret1)).Run(actions, startCallback, exitCallBack, out var stdout2);
@@ -171,10 +174,11 @@ public override int Run(IBuildActions actions, Action<string> startCallback, Act
171174
/// Creates a simple build script that runs the specified exe.
172175
/// </summary>
173176
/// <param name="argumentsOpt">The arguments to the executable, or null.</param>
177+
/// <param name="silent">Whether the executable should run silently.</param>
174178
/// <param name="workingDirectory">The working directory (<code>null</code> for current directory).</param>
175179
/// <param name="environment">Additional environment variables.</param>
176-
public static BuildScript Create(string exe, string argumentsOpt, string workingDirectory, IDictionary<string, string> environment) =>
177-
new BuildCommand(exe, argumentsOpt, workingDirectory, environment);
180+
public static BuildScript Create(string exe, string argumentsOpt, bool silent, string workingDirectory, IDictionary<string, string> environment) =>
181+
new BuildCommand(exe, argumentsOpt, silent, workingDirectory, environment);
178182

179183
/// <summary>
180184
/// Creates a simple build script that runs the specified function.

0 commit comments

Comments
 (0)