Skip to content

Commit

Permalink
Merge pull request #867 from unoplatform/mergify/bp/release/stable/8.…
Browse files Browse the repository at this point in the history
…0/pr-866

fix: Use assembly full path search first (backport #866)
  • Loading branch information
jeromelaban authored Jun 25, 2024
2 parents 7f9b9e1 + 53267e2 commit 574814a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/Uno.Wasm.Bootstrap/ShellTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,13 @@ private void RunPackager()

DirectoryCreateDirectory(workAotPath);

var referencePathsParameter = string.Join(" ", _referencedAssemblies.Select(Path.GetDirectoryName).Distinct().Select(r => $"--search-path=\"{AlignPath(r)}\""));
var referencePathsParameter = string.Join(
" "
, _referencedAssemblies.Select(Path.GetDirectoryName).Distinct().Select(r => $"--search-path=\"{AlignPath(r)}\""));

var referenceAssembliesParameter = string.Join(
" "
, _referencedAssemblies.Distinct().Select(r => $"--asm-ref=\"{AlignPath(r)}\""));

var metadataUpdaterPath = Path.Combine(BuildTaskBasePath, "..", "tools", "support", "Uno.Wasm.MetadataUpdater.dll");

Expand Down Expand Up @@ -824,7 +830,7 @@ private void RunPackager()

var enableICUParam = EnableNetCoreICU ? "--icu" : "";
var monovmparams = $"--framework=net5 --runtimepack-dir=\"{AlignPath(MonoWasmSDKPath)}\" {enableICUParam} {pthreadPoolSizeParam} --illinker-path=\"{_linkerBinPath}\"";
var pass1ResponseContent = $"{runtimeConfigurationParam} {appDirParm} {monovmparams} --zlib {debugOption} {referencePathsParameter} \"{AlignPath(TryConvertLongPath(Path.GetFullPath(Assembly)))}\"";
var pass1ResponseContent = $"{runtimeConfigurationParam} {appDirParm} {monovmparams} --zlib {debugOption} {referenceAssembliesParameter} {referencePathsParameter} \"{AlignPath(TryConvertLongPath(Path.GetFullPath(Assembly)))}\"";

var packagerPass1ResponseFile = Path.Combine(workAotPath, "packager-pass1.rsp");
File.WriteAllText(packagerPass1ResponseFile, pass1ResponseContent);
Expand Down Expand Up @@ -935,6 +941,7 @@ private void RunPackager()
packagerParams.Add(EmccLinkOptimization ? "--emcc-link-optimization" : "");
packagerParams.Add(MonoILLinker ? "--linker --link-mode=all" : "");
packagerParams.Add(referencePathsParameter);
packagerParams.Add(referenceAssembliesParameter);
packagerParams.Add(GenerateAOTProfile ? "--profile=aot" : "");
packagerParams.Add(EnableLogProfiler ? "--profile=log" : "");
packagerParams.Add(!string.IsNullOrEmpty(WasmTunerBinPath) ? $"\"--wasm-tuner-path={AlignPath(Path.GetFullPath(WasmTunerBinPath))}\"" : "");
Expand Down Expand Up @@ -1008,6 +1015,11 @@ private void RunPackager()

var assemblyPath = Path.Combine(linkerInput, Path.GetFileName(Assembly));

// Use explicit assemby references first
var referenceAssemblyPaths = _referencedAssemblies.Distinct().Select(p => $"-reference \"{p}\" ");
linkerParams.AddRange(referenceAssemblyPaths);

// Then search for other known paths
var linkerSearchPaths = _referencedAssemblies.Select(Path.GetDirectoryName).Distinct().Select(p => $"-d \"{p}\" ");
linkerParams.AddRange(linkerSearchPaths);
linkerParams.Add($"-d \"{_bclPath}\"");
Expand Down
24 changes: 24 additions & 0 deletions src/Uno.Wasm.Packager/CapturingAssemblyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
class CapturingAssemblyResolver : DefaultAssemblyResolver
{
private List<Dictionary<string, string>> _files;
private List<string> _assembly_references;

public CapturingAssemblyResolver(List<string> assembly_references)
{
_assembly_references = assembly_references;
}

protected override AssemblyDefinition SearchDirectory(AssemblyNameReference name, IEnumerable<string> directories, ReaderParameters parameters)
{
Expand All @@ -19,6 +25,24 @@ protected override AssemblyDefinition SearchDirectory(AssemblyNameReference name
// paying the cost of File.Exists

_files = new List<Dictionary<string, string>>();

// Fill all the known references first, so they get resolved
// using compilation paths.
var explicitAssembliesPaths = new Dictionary<string, string>();
foreach (var asmReferencePath in _assembly_references)
{
if (Path.GetFileNameWithoutExtension(asmReferencePath) is { } asmName)
{
if (!explicitAssembliesPaths.ContainsKey(asmName))
{
explicitAssembliesPaths.Add(asmName, asmReferencePath);
}
}
}

_files.Add(explicitAssembliesPaths);

// Use all other search paths
string[] extensions = new[] { ".winmd", ".dll", ".exe", ".dll" };

foreach (var directory in directories.Where(Directory.Exists))
Expand Down
6 changes: 5 additions & 1 deletion src/Uno.Wasm.Packager/packager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class Driver {
static List<string> file_list = new List<string> ();
static HashSet<string> assemblies_with_dbg_info = new HashSet<string> ();
static List<string> root_search_paths = new List<string>();
static List<string> assembly_references = new List<string>();
static CapturingAssemblyResolver resolver;

const string BINDINGS_ASM_NAME_MONO = "WebAssembly.Bindings";
Expand Down Expand Up @@ -278,7 +279,7 @@ static void Import (string ra, AssemblyKind kind) {

if (resolver == null)
{
resolver = new CapturingAssemblyResolver();
resolver = new CapturingAssemblyResolver(assembly_references);
root_search_paths.ForEach(resolver.AddSearchDirectory);
foreach (var prefix in bcl_prefixes)
resolver.AddSearchDirectory(prefix);
Expand Down Expand Up @@ -554,6 +555,7 @@ int Run (string[] args) {
{ "template=", s => runtimeTemplate = s },
{ "asset=", s => assets.Add(s) },
{ "search-path=", s => root_search_paths.Add(s) },
{ "asm-ref=", s => assembly_references.Add(s) },
{ "profile=", s => profilers.Add (s) },
{ "copy=", s => copyTypeParm = s },
{ "aot-assemblies=", s => aot_assemblies = s },
Expand Down Expand Up @@ -1584,6 +1586,7 @@ int Run (string[] args) {
ninja.WriteLine ("rule linker");
var linkerBin = "dotnet \'$linker_dir/illink.dll\'";

var assemblyRefPaths = assembly_references.Distinct().Select(p => $"-reference \"{p}\" ");
var linkerSearchPaths = root_search_paths.Concat(bcl_prefixes).Distinct().Select(p => $"-d \"{p}\" ");

var tunerBinary = string.IsNullOrEmpty(wasm_tuner_path)
Expand All @@ -1597,6 +1600,7 @@ int Run (string[] args) {
linker_args.Add($"-out ./linker-out --deterministic --disable-opt unreachablebodies");
linker_args.Add($"--strip-link-attributes");
linker_args.Add(extra_linkerflags);
linker_args.AddRange(assemblyRefPaths);
linker_args.AddRange(linkerSearchPaths);

ninja.WriteLine ($" command = {tools_shell_prefix} {linkerBin} \'@{linkerResponse}\' {exitCommand}; {tunerCommand} --gen-empty-assemblies \'@$builddir/tuner.rsp\'");
Expand Down

0 comments on commit 574814a

Please sign in to comment.