Skip to content

Commit

Permalink
[NET8] Fix assembly count when satellite assemblies are present
Browse files Browse the repository at this point in the history
Context: #8790

The old method of satellite assembly counting relied on the
`RelativePath` MSBuild item metadata, which appears to have gone
missing somewhere in .NET8+.  Update the code to check for presence
of the following metadata, in order given, to determine assembly's
culture, if any:

  * `Culture`
  * `RelativePath`
  * `DestinationSubDirectory`

Failure to count satellite assemblies can, and sometimes will,
result in a segfault since we generate a number of native code
arrays based on the assembly count and the runtime assumes that
what the build told it is true.
  • Loading branch information
grendello committed Mar 20, 2024
1 parent 5c04944 commit f64dd7b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,16 @@ void AddEnvironment ()
HashSet<string> archAssemblyNames = null;
HashSet<string> uniqueAssemblyNames = new HashSet<string> (StringComparer.OrdinalIgnoreCase);
Action<ITaskItem> updateAssemblyCount = (ITaskItem assembly) => {
// We need to use the 'RelativePath' metadata, if found, because it will give us the correct path for satellite assemblies - with the culture in the path.
string? relativePath = assembly.GetMetadata ("RelativePath");
string assemblyName = String.IsNullOrEmpty (relativePath) ? Path.GetFileName (assembly.ItemSpec) : relativePath;
string? culture = MonoAndroidHelper.GetAssemblyCulture (assembly);
string fileName = Path.GetFileName (assembly.ItemSpec);
tring assemblyName;

if (String.IsNullOrEmpty (culture)) {
assemblyName = fileName;
} else {
assemblyName = $"{culture}/{fileName}";
}

if (!uniqueAssemblyNames.Contains (assemblyName)) {
uniqueAssemblyNames.Add (assemblyName);
}
Expand Down
32 changes: 32 additions & 0 deletions src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -643,5 +643,37 @@ public static string GetNativeLibsRootDirectoryPath (string androidBinUtilsDirec
string relPath = GetToolsRootDirectoryRelativePath (androidBinUtilsDirectory);
return Path.GetFullPath (Path.Combine (androidBinUtilsDirectory, relPath, "lib"));
}

public static string? GetAssemblyCulture (ITaskItem assembly)
{
// The best option
string? culture = assembly.GetMetadata ("Culture");
if (!String.IsNullOrEmpty (culture)) {
return TrimSlashes (culture);
}

// ...slightly worse
culture = assembly.GetMetadata ("RelativePath");
if (!String.IsNullOrEmpty (culture)) {
return TrimSlashes (Path.GetDirectoryName (culture));
}

// ...not ideal
culture = assembly.GetMetadata ("DestinationSubDirectory");
if (!String.IsNullOrEmpty (culture)) {
return TrimSlashes (culture);
}

return null;

string? TrimSlashes (string? s)
{
if (String.IsNullOrEmpty (s)) {
return null;
}

return s.TrimEnd ('/').TrimEnd ('\\');
}
}
}
}

0 comments on commit f64dd7b

Please sign in to comment.