From 468a6953451a94fedecb04123cb6531f431c848c Mon Sep 17 00:00:00 2001 From: Aleksey Kliger Date: Sun, 22 Jan 2023 20:17:10 -0500 Subject: [PATCH 1/8] replace magic constants by strlen(LITERAL) Let the C compiler do the math --- src/mono/mono/metadata/assembly.c | 4 ++-- src/mono/mono/metadata/mono-debug.c | 2 +- src/mono/mono/mini/monovm.c | 7 +++++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/mono/mono/metadata/assembly.c b/src/mono/mono/metadata/assembly.c index adb2ae0d5306ef..6375754f97f04e 100644 --- a/src/mono/mono/metadata/assembly.c +++ b/src/mono/mono/metadata/assembly.c @@ -1460,8 +1460,8 @@ bundled_assembly_match (const char *bundled_name, const char *name) return TRUE; /* if they want a .dll and we have the matching .webcil, return it */ if (g_str_has_suffix (bundled_name, ".webcil") && g_str_has_suffix (name, ".dll")) { - size_t bprefix = strlen (bundled_name) - 7; - size_t nprefix = strlen (name) - 4; + size_t bprefix = strlen (bundled_name) - strlen (".webcil"); + size_t nprefix = strlen (name) - strlen (".dll"); if (bprefix == nprefix && strncmp (bundled_name, name, bprefix) == 0) return TRUE; } diff --git a/src/mono/mono/metadata/mono-debug.c b/src/mono/mono/metadata/mono-debug.c index 958b33657d43a9..993a6fd1edfbec 100644 --- a/src/mono/mono/metadata/mono-debug.c +++ b/src/mono/mono/metadata/mono-debug.c @@ -1105,7 +1105,7 @@ bsymfile_match (BundledSymfile *bsymfile, const char *assembly_name) #ifdef ENABLE_WEBCIL const char *p = strstr (assembly_name, ".webcil"); /* if assembly_name ends with .webcil, check if aname matches, with a .dll extension instead */ - if (p && *(p + 7) == 0) { + if (p && *(p + strlen(".webcil")) == 0) { size_t n = p - assembly_name; if (!strncmp (bsymfile->aname, assembly_name, n) && !strcmp (bsymfile->aname + n, ".dll")) diff --git a/src/mono/mono/mini/monovm.c b/src/mono/mono/mini/monovm.c index 63e25581ac7a61..3730d6b256da56 100644 --- a/src/mono/mono/mini/monovm.c +++ b/src/mono/mono/mini/monovm.c @@ -134,8 +134,11 @@ mono_core_preload_hook (MonoAssemblyLoadContext *alc, MonoAssemblyName *aname, c #ifdef ENABLE_WEBCIL else { /* /path/foo.dll -> /path/foo.webcil */ - size_t n = strlen (fullpath) - 4; - char *fullpath2 = g_malloc (n + 8); + size_t n = strlen (fullpath); + if (n < strlen(".dll")) + continue; + n -= strlen(".dll"); + char *fullpath2 = g_malloc (n + strlen(".webcil") + 1); g_strlcpy (fullpath2, fullpath, n + 1); g_strlcpy (fullpath2 + n, ".webcil", 8); if (g_file_test (fullpath2, G_FILE_TEST_IS_REGULAR)) { From 290b14be3f218f504d5c0e7455c9b87178353cd3 Mon Sep 17 00:00:00 2001 From: Aleksey Kliger Date: Sun, 22 Jan 2023 20:27:08 -0500 Subject: [PATCH 2/8] Use CopyIfDifferent for webcil converter in WasmAppBuilder --- src/tasks/WasmAppBuilder/WasmAppBuilder.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/tasks/WasmAppBuilder/WasmAppBuilder.cs b/src/tasks/WasmAppBuilder/WasmAppBuilder.cs index 8dd76eeab79525..74d7f1e82ea4d5 100644 --- a/src/tasks/WasmAppBuilder/WasmAppBuilder.cs +++ b/src/tasks/WasmAppBuilder/WasmAppBuilder.cs @@ -198,7 +198,8 @@ private bool ExecuteInternal () var webcilWriter = Microsoft.WebAssembly.Build.Tasks.WebcilConverter.FromPortableExecutable(inputPath: assembly, outputPath: tmpWebcil, logger: Log); webcilWriter.ConvertToWebcil(); var finalWebcil = Path.ChangeExtension(assembly, ".webcil"); - FileCopyChecked(tmpWebcil, Path.Combine(asmRootPath, Path.GetFileName(finalWebcil)), "Assemblies"); + if (Utils.CopyIfDifferent(tmpWebcil, finalWebcil, useHash: true)) + _fileWrites.Add(finalWebcil); } else { @@ -284,7 +285,8 @@ private bool ExecuteInternal () var webcilWriter = Microsoft.WebAssembly.Build.Tasks.WebcilConverter.FromPortableExecutable(inputPath: fullPath, outputPath: tmpWebcil, logger: Log); webcilWriter.ConvertToWebcil(); var finalWebcil = Path.ChangeExtension(name, ".webcil"); - FileCopyChecked(tmpWebcil, Path.Combine(directory, finalWebcil), "SatelliteAssemblies"); + if (Utils.CopyIfDifferent (tmpWebcil, finalWebcil, useHash: true)) + _fileWrites.Add (finalWebcil); config.Assets.Add(new SatelliteAssemblyEntry(finalWebcil, culture)); } else From d86aec9ab4e3e1f4d4e1155f64cabc0c6a7d5990 Mon Sep 17 00:00:00 2001 From: Aleksey Kliger Date: Sun, 22 Jan 2023 20:28:09 -0500 Subject: [PATCH 3/8] WasmAppHost: serve .webcil as application/octet-stream --- src/mono/wasm/host/WebServerStartup.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mono/wasm/host/WebServerStartup.cs b/src/mono/wasm/host/WebServerStartup.cs index e2e0be4a39c760..26fe8e0b11f195 100644 --- a/src/mono/wasm/host/WebServerStartup.cs +++ b/src/mono/wasm/host/WebServerStartup.cs @@ -82,7 +82,7 @@ public void Configure(IApplicationBuilder app, provider.Mappings[".cjs"] = "text/javascript"; provider.Mappings[".mjs"] = "text/javascript"; - foreach (string extn in new string[] { ".dll", ".pdb", ".dat", ".blat" }) + foreach (string extn in new string[] { ".dll", ".pdb", ".dat", ".blat", ".webcil" }) { provider.Mappings[extn] = "application/octet-stream"; } From 32f99cbb487f4da9ada17073c60a26211e18b604 Mon Sep 17 00:00:00 2001 From: Aleksey Kliger Date: Mon, 23 Jan 2023 09:10:21 -0500 Subject: [PATCH 4/8] fixup don't drop the output directory for finalWebcil --- src/tasks/WasmAppBuilder/WasmAppBuilder.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tasks/WasmAppBuilder/WasmAppBuilder.cs b/src/tasks/WasmAppBuilder/WasmAppBuilder.cs index 74d7f1e82ea4d5..bcb1155e472a3e 100644 --- a/src/tasks/WasmAppBuilder/WasmAppBuilder.cs +++ b/src/tasks/WasmAppBuilder/WasmAppBuilder.cs @@ -197,7 +197,7 @@ private bool ExecuteInternal () var tmpWebcil = Path.GetTempFileName(); var webcilWriter = Microsoft.WebAssembly.Build.Tasks.WebcilConverter.FromPortableExecutable(inputPath: assembly, outputPath: tmpWebcil, logger: Log); webcilWriter.ConvertToWebcil(); - var finalWebcil = Path.ChangeExtension(assembly, ".webcil"); + var finalWebcil = Path.Combine(asmRootPath, Path.ChangeExtension(assembly, ".webcil")); if (Utils.CopyIfDifferent(tmpWebcil, finalWebcil, useHash: true)) _fileWrites.Add(finalWebcil); } @@ -284,7 +284,7 @@ private bool ExecuteInternal () var tmpWebcil = Path.GetTempFileName(); var webcilWriter = Microsoft.WebAssembly.Build.Tasks.WebcilConverter.FromPortableExecutable(inputPath: fullPath, outputPath: tmpWebcil, logger: Log); webcilWriter.ConvertToWebcil(); - var finalWebcil = Path.ChangeExtension(name, ".webcil"); + var finalWebcil = Path.Combine(directory, Path.ChangeExtension(name, ".webcil")); if (Utils.CopyIfDifferent (tmpWebcil, finalWebcil, useHash: true)) _fileWrites.Add (finalWebcil); config.Assets.Add(new SatelliteAssemblyEntry(finalWebcil, culture)); From db9bce2b2eb95ce98c9523b871058d0a7bacb28b Mon Sep 17 00:00:00 2001 From: Aleksey Kliger Date: Mon, 23 Jan 2023 09:12:28 -0500 Subject: [PATCH 5/8] Add mime types for webcil in two more places --- src/mono/sample/wasm/Directory.Build.targets | 1 + src/mono/sample/wasm/simple-server/Program.cs | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/mono/sample/wasm/Directory.Build.targets b/src/mono/sample/wasm/Directory.Build.targets index e9447a94400133..9ef8935b4969c0 100644 --- a/src/mono/sample/wasm/Directory.Build.targets +++ b/src/mono/sample/wasm/Directory.Build.targets @@ -19,6 +19,7 @@ <_ServeMimeTypes>$(_ServeMimeTypes) --mime .mjs=text/javascript <_ServeMimeTypes>$(_ServeMimeTypes) --mime .cjs=text/javascript <_ServeMimeTypes>$(_ServeMimeTypes) --mime .js=text/javascript + <_ServeMimeTypes>$(_ServeMimeTypes) --mime .webcil=application/octet-stream Date: Mon, 23 Jan 2023 11:23:08 -0500 Subject: [PATCH 6/8] fixup path for output --- src/tasks/WasmAppBuilder/WasmAppBuilder.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tasks/WasmAppBuilder/WasmAppBuilder.cs b/src/tasks/WasmAppBuilder/WasmAppBuilder.cs index bcb1155e472a3e..d8b5fbbb338b76 100644 --- a/src/tasks/WasmAppBuilder/WasmAppBuilder.cs +++ b/src/tasks/WasmAppBuilder/WasmAppBuilder.cs @@ -197,7 +197,7 @@ private bool ExecuteInternal () var tmpWebcil = Path.GetTempFileName(); var webcilWriter = Microsoft.WebAssembly.Build.Tasks.WebcilConverter.FromPortableExecutable(inputPath: assembly, outputPath: tmpWebcil, logger: Log); webcilWriter.ConvertToWebcil(); - var finalWebcil = Path.Combine(asmRootPath, Path.ChangeExtension(assembly, ".webcil")); + var finalWebcil = Path.Combine(asmRootPath, Path.ChangeExtension(Path.GetFileName(assembly), ".webcil")); if (Utils.CopyIfDifferent(tmpWebcil, finalWebcil, useHash: true)) _fileWrites.Add(finalWebcil); } From 33e3448a0a33ace85ae6b40321963b8ff90c3fad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksey=20Kliger=20=28=CE=BBgeek=29?= Date: Mon, 23 Jan 2023 15:14:22 -0500 Subject: [PATCH 7/8] Always add to _fileWrites But log whether we actually copied anything or not Co-authored-by: Ankit Jain --- src/tasks/WasmAppBuilder/WasmAppBuilder.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/tasks/WasmAppBuilder/WasmAppBuilder.cs b/src/tasks/WasmAppBuilder/WasmAppBuilder.cs index d8b5fbbb338b76..a88e2cabcde193 100644 --- a/src/tasks/WasmAppBuilder/WasmAppBuilder.cs +++ b/src/tasks/WasmAppBuilder/WasmAppBuilder.cs @@ -199,7 +199,10 @@ private bool ExecuteInternal () webcilWriter.ConvertToWebcil(); var finalWebcil = Path.Combine(asmRootPath, Path.ChangeExtension(Path.GetFileName(assembly), ".webcil")); if (Utils.CopyIfDifferent(tmpWebcil, finalWebcil, useHash: true)) - _fileWrites.Add(finalWebcil); + Log.LogMessage(MessageImportance.Low, $"Generated {finalWebcil} ."); + else + Log.LogMessage(MessageImportance.Low, $"Skipped generating {finalWebcil} as the contents are unchanged."); + _fileWrites.Add(finalWebcil); } else { @@ -285,8 +288,11 @@ private bool ExecuteInternal () var webcilWriter = Microsoft.WebAssembly.Build.Tasks.WebcilConverter.FromPortableExecutable(inputPath: fullPath, outputPath: tmpWebcil, logger: Log); webcilWriter.ConvertToWebcil(); var finalWebcil = Path.Combine(directory, Path.ChangeExtension(name, ".webcil")); - if (Utils.CopyIfDifferent (tmpWebcil, finalWebcil, useHash: true)) - _fileWrites.Add (finalWebcil); + if (Utils.CopyIfDifferent(tmpWebcil, finalWebcil, useHash: true)) + Log.LogMessage(MessageImportance.Low, $"Generated {finalWebcil} ."); + else + Log.LogMessage(MessageImportance.Low, $"Skipped generating {finalWebcil} as the contents are unchanged."); + _fileWrites.Add(finalWebcil); config.Assets.Add(new SatelliteAssemblyEntry(finalWebcil, culture)); } else From 88d5c5d66afbe4a61b568b37688ab42883ae49d7 Mon Sep 17 00:00:00 2001 From: Aleksey Kliger Date: Mon, 23 Jan 2023 15:22:29 -0500 Subject: [PATCH 8/8] short name in assets for SatelliteAssemblyEntry --- src/tasks/WasmAppBuilder/WasmAppBuilder.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tasks/WasmAppBuilder/WasmAppBuilder.cs b/src/tasks/WasmAppBuilder/WasmAppBuilder.cs index a88e2cabcde193..026dc272b3f29b 100644 --- a/src/tasks/WasmAppBuilder/WasmAppBuilder.cs +++ b/src/tasks/WasmAppBuilder/WasmAppBuilder.cs @@ -293,7 +293,7 @@ private bool ExecuteInternal () else Log.LogMessage(MessageImportance.Low, $"Skipped generating {finalWebcil} as the contents are unchanged."); _fileWrites.Add(finalWebcil); - config.Assets.Add(new SatelliteAssemblyEntry(finalWebcil, culture)); + config.Assets.Add(new SatelliteAssemblyEntry(Path.GetFileName(finalWebcil), culture)); } else {