Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use assembly full path search first #866

Merged
merged 3 commits into from
Jun 25, 2024
Merged
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
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
Loading