Skip to content

Commit 3301e9d

Browse files
authored
[wasm] Use compile rsp instead of link, for compiling native files (dotnet#55848)
.. and fix logging that broke recently. `tasks/Common/Utils.cs`: TaskLoggingHelper Utils.Logger is a static field, which must be set by task else any methods in Utils, eg. RunProcess, silently fail to log any messages. Also, this would be a problem when building multiple projects in parallel, since the logger is a task-specific one. Instead, we pass logger as an arg to all the methods.
1 parent d574b03 commit 3301e9d

File tree

11 files changed

+77
-82
lines changed

11 files changed

+77
-82
lines changed

src/mono/wasm/build/WasmApp.Native.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@
269269
<EmccCompile
270270
Condition="@(_BitCodeFile->Count()) > 0"
271271
SourceFiles="@(_BitCodeFile)"
272-
Arguments="&quot;@$(_EmccDefaultFlagsRsp)&quot; @(_EmccLDFlags->'%(Identity)', ' ')"
272+
Arguments="&quot;@$(_EmccDefaultFlagsRsp)&quot; &quot;@$(_EmccCompileRsp)&quot;"
273273
EnvironmentVariables="@(EmscriptenEnvVars)" />
274274

275275
<ItemGroup>

src/tasks/AndroidAppBuilder/AndroidApkFileReplacerTask.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ public class AndroidApkFileReplacerTask : Task
2626

2727
public override bool Execute()
2828
{
29-
Utils.Logger = Log;
30-
var apkBuilder = new ApkBuilder();
29+
var apkBuilder = new ApkBuilder(Log);
3130
apkBuilder.OutputDir = OutputDir;
3231
apkBuilder.AndroidSdk = AndroidSdk;
3332
apkBuilder.MinApiLevel = MinApiLevel;

src/tasks/AndroidAppBuilder/AndroidAppBuilder.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,9 @@ public class AndroidAppBuilderTask : Task
8787

8888
public override bool Execute()
8989
{
90-
Utils.Logger = Log;
91-
9290
string abi = DetermineAbi();
9391

94-
var apkBuilder = new ApkBuilder();
92+
var apkBuilder = new ApkBuilder(Log);
9593
apkBuilder.ProjectName = ProjectName;
9694
apkBuilder.AppDir = AppDir;
9795
apkBuilder.OutputDir = OutputDir;

src/tasks/AndroidAppBuilder/ApkBuilder.cs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Linq;
88
using System.Text;
99
using Microsoft.Build.Framework;
10+
using Microsoft.Build.Utilities;
1011

1112
public class ApkBuilder
1213
{
@@ -32,6 +33,13 @@ public class ApkBuilder
3233
public string? DiagnosticPorts { get; set; }
3334
public ITaskItem[] Assemblies { get; set; } = Array.Empty<ITaskItem>();
3435

36+
private TaskLoggingHelper logger;
37+
38+
public ApkBuilder(TaskLoggingHelper logger)
39+
{
40+
this.logger = logger;
41+
}
42+
3543
public (string apk, string packageId) BuildApk(
3644
string abi,
3745
string mainLibraryFileName,
@@ -209,7 +217,7 @@ public class ApkBuilder
209217
string cmake = "cmake";
210218
string zip = "zip";
211219

212-
Utils.RunProcess(zip, workingDir: assetsToZipDirectory, args: "-q -r ../assets/assets.zip .");
220+
Utils.RunProcess(logger, zip, workingDir: assetsToZipDirectory, args: "-q -r ../assets/assets.zip .");
213221
Directory.Delete(assetsToZipDirectory, true);
214222

215223
if (!File.Exists(androidJar))
@@ -274,7 +282,7 @@ public class ApkBuilder
274282
// if lib doesn't exist (primarly due to runtime build without static lib support), fallback linking stub lib.
275283
if (!File.Exists(componentLibToLink))
276284
{
277-
Utils.LogInfo($"\nCouldn't find static component library: {componentLibToLink}, linking static component stub library: {staticComponentStubLib}.\n");
285+
logger.LogMessage(MessageImportance.High, $"\nCouldn't find static component library: {componentLibToLink}, linking static component stub library: {staticComponentStubLib}.\n");
278286
componentLibToLink = staticComponentStubLib;
279287
}
280288

@@ -339,8 +347,8 @@ public class ApkBuilder
339347
cmakeBuildArgs += " --config Debug";
340348
}
341349

342-
Utils.RunProcess(cmake, workingDir: OutputDir, args: cmakeGenArgs);
343-
Utils.RunProcess(cmake, workingDir: OutputDir, args: cmakeBuildArgs);
350+
Utils.RunProcess(logger, cmake, workingDir: OutputDir, args: cmakeGenArgs);
351+
Utils.RunProcess(logger, cmake, workingDir: OutputDir, args: cmakeBuildArgs);
344352

345353
// 2. Compile Java files
346354

@@ -369,15 +377,15 @@ public class ApkBuilder
369377
.Replace("%MinSdkLevel%", MinApiLevel));
370378

371379
string javaCompilerArgs = $"-d obj -classpath src -bootclasspath {androidJar} -source 1.8 -target 1.8 ";
372-
Utils.RunProcess(javac, javaCompilerArgs + javaActivityPath, workingDir: OutputDir);
373-
Utils.RunProcess(javac, javaCompilerArgs + monoRunnerPath, workingDir: OutputDir);
374-
Utils.RunProcess(dx, "--dex --output=classes.dex obj", workingDir: OutputDir);
380+
Utils.RunProcess(logger, javac, javaCompilerArgs + javaActivityPath, workingDir: OutputDir);
381+
Utils.RunProcess(logger, javac, javaCompilerArgs + monoRunnerPath, workingDir: OutputDir);
382+
Utils.RunProcess(logger, dx, "--dex --output=classes.dex obj", workingDir: OutputDir);
375383

376384
// 3. Generate APK
377385

378386
string debugModeArg = StripDebugSymbols ? string.Empty : "--debug-mode";
379387
string apkFile = Path.Combine(OutputDir, "bin", $"{ProjectName}.unaligned.apk");
380-
Utils.RunProcess(aapt, $"package -f -m -F {apkFile} -A assets -M AndroidManifest.xml -I {androidJar} {debugModeArg}", workingDir: OutputDir);
388+
Utils.RunProcess(logger, aapt, $"package -f -m -F {apkFile} -A assets -M AndroidManifest.xml -I {androidJar} {debugModeArg}", workingDir: OutputDir);
381389

382390
var dynamicLibs = new List<string>();
383391
dynamicLibs.Add(Path.Combine(OutputDir, "monodroid", "libmonodroid.so"));
@@ -433,21 +441,21 @@ public class ApkBuilder
433441
// NOTE: we can run android-strip tool from NDK to shrink native binaries here even more.
434442

435443
File.Copy(dynamicLib, Path.Combine(OutputDir, destRelative), true);
436-
Utils.RunProcess(aapt, $"add {apkFile} {destRelative}", workingDir: OutputDir);
444+
Utils.RunProcess(logger, aapt, $"add {apkFile} {destRelative}", workingDir: OutputDir);
437445
}
438-
Utils.RunProcess(aapt, $"add {apkFile} classes.dex", workingDir: OutputDir);
446+
Utils.RunProcess(logger, aapt, $"add {apkFile} classes.dex", workingDir: OutputDir);
439447

440448
// 4. Align APK
441449

442450
string alignedApk = Path.Combine(OutputDir, "bin", $"{ProjectName}.apk");
443-
Utils.RunProcess(zipalign, $"-v 4 {apkFile} {alignedApk}", workingDir: OutputDir);
451+
Utils.RunProcess(logger, zipalign, $"-v 4 {apkFile} {alignedApk}", workingDir: OutputDir);
444452
// we don't need the unaligned one any more
445453
File.Delete(apkFile);
446454

447455
// 5. Generate key (if needed) & sign the apk
448456
SignApk(alignedApk, apksigner);
449457

450-
Utils.LogInfo($"\nAPK size: {(new FileInfo(alignedApk).Length / 1000_000.0):0.#} Mb.\n");
458+
logger.LogMessage(MessageImportance.High, $"\nAPK size: {(new FileInfo(alignedApk).Length / 1000_000.0):0.#} Mb.\n");
451459

452460
return (alignedApk, packageId);
453461
}
@@ -460,15 +468,15 @@ private void SignApk(string apkPath, string apksigner)
460468

461469
if (!File.Exists(signingKey))
462470
{
463-
Utils.RunProcess("keytool", "-genkey -v -keystore debug.keystore -storepass android -alias " +
471+
Utils.RunProcess(logger, "keytool", "-genkey -v -keystore debug.keystore -storepass android -alias " +
464472
"androiddebugkey -keypass android -keyalg RSA -keysize 2048 -noprompt " +
465473
"-dname \"CN=Android Debug,O=Android,C=US\"", workingDir: OutputDir, silent: true);
466474
}
467475
else if (Path.GetFullPath(signingKey) != Path.GetFullPath(defaultKey))
468476
{
469477
File.Copy(signingKey, Path.Combine(OutputDir, "debug.keystore"));
470478
}
471-
Utils.RunProcess(apksigner, $"sign --min-sdk-version {MinApiLevel} --ks debug.keystore " +
479+
Utils.RunProcess(logger, apksigner, $"sign --min-sdk-version {MinApiLevel} --ks debug.keystore " +
472480
$"--ks-pass pass:android --key-pass pass:android {apkPath}", workingDir: OutputDir);
473481
}
474482

@@ -499,8 +507,8 @@ public void ReplaceFileInApk(string file)
499507
if (!File.Exists(apkPath))
500508
throw new Exception($"{apkPath} was not found");
501509

502-
Utils.RunProcess(aapt, $"remove -v bin/{Path.GetFileName(apkPath)} {file}", workingDir: OutputDir);
503-
Utils.RunProcess(aapt, $"add -v bin/{Path.GetFileName(apkPath)} {file}", workingDir: OutputDir);
510+
Utils.RunProcess(logger, aapt, $"remove -v bin/{Path.GetFileName(apkPath)} {file}", workingDir: OutputDir);
511+
Utils.RunProcess(logger, aapt, $"add -v bin/{Path.GetFileName(apkPath)} {file}", workingDir: OutputDir);
504512

505513
// we need to re-sign the apk
506514
SignApk(apkPath, apksigner);

src/tasks/AotCompilerTask/MonoAOTCompiler.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,6 @@ public class MonoAOTCompiler : Microsoft.Build.Utilities.Task
194194

195195
public override bool Execute()
196196
{
197-
Utils.Logger = Log;
198-
199197
if (string.IsNullOrEmpty(CompilerBinaryPath))
200198
{
201199
throw new ArgumentException($"'{nameof(CompilerBinaryPath)}' is required.", nameof(CompilerBinaryPath));
@@ -545,15 +543,25 @@ private bool PrecompileLibrary(ITaskItem assemblyItem, string? monoPaths)
545543

546544
Log.LogMessage(MessageImportance.Low, $"AOT compiler arguments: {responseFileContent}");
547545

546+
string args = $"--response=\"{responseFilePath}\"";
547+
548+
// Log the command in a compact format which can be copy pasted
549+
StringBuilder envStr = new StringBuilder(string.Empty);
550+
foreach (KeyValuePair<string, string> kvp in envVariables)
551+
envStr.Append($"{kvp.Key}={kvp.Value} ");
552+
Log.LogMessage(MessageImportance.Low, $"Exec: {envStr}{CompilerBinaryPath} {args}");
553+
548554
try
549555
{
550556
// run the AOT compiler
551-
(int exitCode, string output) = Utils.TryRunProcess(CompilerBinaryPath,
552-
$"--response=\"{responseFilePath}\"",
557+
(int exitCode, string output) = Utils.TryRunProcess(Log,
558+
CompilerBinaryPath,
559+
args,
553560
envVariables,
554561
assemblyDir,
555562
silent: false,
556-
debugMessageImportance: MessageImportance.Low);
563+
debugMessageImportance: MessageImportance.Low,
564+
label: assembly);
557565
if (exitCode != 0)
558566
{
559567
Log.LogError($"Precompiling failed for {assembly}: {output}");

src/tasks/AppleAppBuilder/AppleAppBuilder.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ public string TargetOS
153153

154154
public override bool Execute()
155155
{
156-
Utils.Logger = Log;
157156
bool isDevice = (TargetOS == TargetNames.iOS || TargetOS == TargetNames.tvOS);
158157

159158
if (!File.Exists(Path.Combine(AppDir, MainLibraryFileName)))
@@ -227,7 +226,7 @@ public override bool Execute()
227226

228227
if (GenerateXcodeProject)
229228
{
230-
Xcode generator = new Xcode(TargetOS, Arch);
229+
Xcode generator = new Xcode(Log, TargetOS, Arch);
231230
generator.EnableRuntimeLogging = EnableRuntimeLogging;
232231
generator.DiagnosticPorts = DiagnosticPorts;
233232

@@ -239,7 +238,7 @@ public override bool Execute()
239238
if (isDevice && string.IsNullOrEmpty(DevTeamProvisioning))
240239
{
241240
// DevTeamProvisioning shouldn't be empty for arm64 builds
242-
Utils.LogInfo("DevTeamProvisioning is not set, BuildAppBundle step is skipped.");
241+
Log.LogMessage(MessageImportance.High, "DevTeamProvisioning is not set, BuildAppBundle step is skipped.");
243242
}
244243
else
245244
{

src/tasks/AppleAppBuilder/Xcode.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,38 @@
66
using System.IO;
77
using System.Linq;
88
using System.Text;
9+
using Microsoft.Build.Framework;
10+
using Microsoft.Build.Utilities;
911

1012
internal class Xcode
1113
{
1214
private string RuntimeIdentifier { get; set; }
1315
private string SysRoot { get; set; }
1416
private string Target { get; set; }
1517
private string XcodeArch { get; set; }
18+
private TaskLoggingHelper Logger { get; set; }
1619

17-
public Xcode(string target, string arch)
20+
public Xcode(TaskLoggingHelper logger, string target, string arch)
1821
{
22+
Logger = logger;
1923
Target = target;
2024
XcodeArch = (arch == "x64") ? "x86_64" : arch;
2125
switch (Target)
2226
{
2327
case TargetNames.iOS:
24-
SysRoot = Utils.RunProcess("xcrun", "--sdk iphoneos --show-sdk-path");
28+
SysRoot = Utils.RunProcess(Logger, "xcrun", "--sdk iphoneos --show-sdk-path");
2529
break;
2630
case TargetNames.iOSsim:
27-
SysRoot = Utils.RunProcess("xcrun", "--sdk iphonesimulator --show-sdk-path");
31+
SysRoot = Utils.RunProcess(Logger, "xcrun", "--sdk iphonesimulator --show-sdk-path");
2832
break;
2933
case TargetNames.tvOS:
30-
SysRoot = Utils.RunProcess("xcrun", "--sdk appletvos --show-sdk-path");
34+
SysRoot = Utils.RunProcess(Logger, "xcrun", "--sdk appletvos --show-sdk-path");
3135
break;
3236
case TargetNames.tvOSsim:
33-
SysRoot = Utils.RunProcess("xcrun", "--sdk appletvsimulator --show-sdk-path");
37+
SysRoot = Utils.RunProcess(Logger, "xcrun", "--sdk appletvsimulator --show-sdk-path");
3438
break;
3539
default:
36-
SysRoot = Utils.RunProcess("xcrun", "--sdk macosx --show-sdk-path");
40+
SysRoot = Utils.RunProcess(Logger, "xcrun", "--sdk macosx --show-sdk-path");
3741
break;
3842
}
3943

@@ -145,7 +149,7 @@ public string GenerateXCode(
145149
// if lib doesn't exist (primarly due to runtime build without static lib support), fallback linking stub lib.
146150
if (!File.Exists(componentLibToLink))
147151
{
148-
Utils.LogInfo($"\nCouldn't find static component library: {componentLibToLink}, linking static component stub library: {staticComponentStubLib}.\n");
152+
Logger.LogMessage(MessageImportance.High, $"\nCouldn't find static component library: {componentLibToLink}, linking static component stub library: {staticComponentStubLib}.\n");
149153
componentLibToLink = staticComponentStubLib;
150154
}
151155

@@ -298,7 +302,7 @@ public string GenerateXCode(
298302
.Replace("//%APPLE_RUNTIME_IDENTIFIER%", RuntimeIdentifier)
299303
.Replace("%EntryPointLibName%", Path.GetFileName(entryPointLib)));
300304

301-
Utils.RunProcess("cmake", cmakeArgs.ToString(), workingDir: binDir);
305+
Utils.RunProcess(Logger, "cmake", cmakeArgs.ToString(), workingDir: binDir);
302306

303307
return Path.Combine(binDir, projectName, projectName + ".xcodeproj");
304308
}
@@ -391,7 +395,7 @@ public string BuildAppBundle(
391395
string config = optimized ? "Release" : "Debug";
392396
args.Append(" -configuration ").Append(config);
393397

394-
Utils.RunProcess("xcodebuild", args.ToString(), workingDir: Path.GetDirectoryName(xcodePrjPath));
398+
Utils.RunProcess(Logger, "xcodebuild", args.ToString(), workingDir: Path.GetDirectoryName(xcodePrjPath));
395399

396400
string appPath = Path.Combine(Path.GetDirectoryName(xcodePrjPath)!, config + "-" + sdk,
397401
Path.GetFileNameWithoutExtension(xcodePrjPath) + ".app");
@@ -400,7 +404,7 @@ public string BuildAppBundle(
400404
.EnumerateFiles("*", SearchOption.AllDirectories)
401405
.Sum(file => file.Length);
402406

403-
Utils.LogInfo($"\nAPP size: {(appSize / 1000_000.0):0.#} Mb.\n");
407+
Logger.LogMessage(MessageImportance.High, $"\nAPP size: {(appSize / 1000_000.0):0.#} Mb.\n");
404408

405409
return appPath;
406410
}

0 commit comments

Comments
 (0)