Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 0 additions & 55 deletions src/tasks/Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,61 +48,6 @@ public static bool IsNewerThan(string inFile, string outFile)
=> !File.Exists(inFile) || !File.Exists(outFile) ||
(File.GetLastWriteTimeUtc(inFile) > File.GetLastWriteTimeUtc(outFile));

public static (int exitCode, string output) RunShellCommand(
TaskLoggingHelper logger,
string command,
IDictionary<string, string> envVars,
string workingDir,
bool silent=false,
bool logStdErrAsMessage=false,
MessageImportance debugMessageImportance=MessageImportance.Low,
string? label=null)
{
string scriptFileName = CreateTemporaryBatchFile(command);
(string shell, string args) = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? ("cmd", $"/c \"{scriptFileName}\"")
: ("/bin/sh", $"\"{scriptFileName}\"");

string msgPrefix = label == null ? string.Empty : $"[{label}] ";
logger.LogMessage(debugMessageImportance, $"{msgPrefix}Running {command} via script {scriptFileName}:", msgPrefix);
logger.LogMessage(debugMessageImportance, File.ReadAllText(scriptFileName), msgPrefix);

return TryRunProcess(logger,
shell,
args,
envVars,
workingDir,
silent: silent,
logStdErrAsMessage: logStdErrAsMessage,
label: label,
debugMessageImportance: debugMessageImportance);

static string CreateTemporaryBatchFile(string command)
{
string extn = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".cmd" : ".sh";
string file = Path.Combine(Path.GetTempPath(), $"tmp{Guid.NewGuid():N}{extn}");

using StreamWriter sw = new(file);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// set encoding to UTF-8 -> full Unicode support is needed for usernames -
// `command` contains tmp dir path with the username
sw.WriteLine(@"%SystemRoot%\System32\chcp.com 65001>nul");
sw.WriteLine("setlocal");
sw.WriteLine("set errorlevel=dummy");
sw.WriteLine("set errorlevel=");
}
else
{
// Use sh rather than bash, as not all 'nix systems necessarily have Bash installed
sw.WriteLine("#!/bin/sh");
}

sw.WriteLine(command);
return file;
}
}

public static string RunProcess(
TaskLoggingHelper logger,
string path,
Expand Down
20 changes: 16 additions & 4 deletions src/tasks/WasmAppBuilder/EmccCompile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -209,19 +210,30 @@ bool ProcessSourceFile(string srcFile, string objFile)
string tmpObjFile = Path.GetTempFileName();
try
{
string command = $"\"{CompilerBinaryPath}\" {Arguments} -c -o \"{tmpObjFile}\" \"{srcFile}\"";
string args = $"{Arguments} -c -o \"{tmpObjFile}\" \"{srcFile}\"";
var startTime = DateTime.Now;

// Log the command in a compact format which can be copy pasted
StringBuilder envStr = new StringBuilder(string.Empty);
foreach (var key in envVarsDict.Keys)
envStr.Append($"{key}={envVarsDict[key]} ");
Log.LogMessage(MessageImportance.Low, $"Exec: {envStr}{command}");
(int exitCode, string output) = Utils.RunShellCommand(
Log.LogMessage(MessageImportance.Low, $"Exec: {envStr}\"{CompilerBinaryPath}\" {args}");

// On Windows, the emsdk ships emcc.bat to invoke emcc. Use 'cmd' to execute the batch file.
string processPath = CompilerBinaryPath;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
args = $"/c \"{CompilerBinaryPath}\" {args}";
processPath = "cmd";
}

(int exitCode, string output) = Utils.TryRunProcess(
Log,
command,
processPath,
args,
envVarsDict,
workingDir: Environment.CurrentDirectory,
silent: false,
logStdErrAsMessage: true,
debugMessageImportance: messageImportance,
label: Path.GetFileName(srcFile));
Expand Down