From 17bb4b548cd384a0f9959ac6ff56dc8481720a64 Mon Sep 17 00:00:00 2001 From: Sebastien Pouliot Date: Fri, 23 Apr 2021 20:42:04 -0400 Subject: [PATCH] [dotnet] Allow choosing the ICU data file to be used for globalization Defaults to include all globalization data. Depending on the target audience it might be possible to reduce the app size. Draft doc: https://github.com/xamarin/xamarin-macios/wiki/Globalization --- dotnet/targets/Xamarin.Shared.Sdk.targets | 10 +++++++++- runtime/runtime.m | 5 ++++- tests/dotnet/Makefile | 2 +- tools/dotnet-linker/LinkerConfiguration.cs | 4 ++++ tools/dotnet-linker/Steps/GenerateMainStep.cs | 5 +++++ 5 files changed, 23 insertions(+), 3 deletions(-) diff --git a/dotnet/targets/Xamarin.Shared.Sdk.targets b/dotnet/targets/Xamarin.Shared.Sdk.targets index b5a651001483..8df48190cddb 100644 --- a/dotnet/targets/Xamarin.Shared.Sdk.targets +++ b/dotnet/targets/Xamarin.Shared.Sdk.targets @@ -143,6 +143,10 @@ true + + icudt.dat + + true @@ -221,6 +225,9 @@ + + + <_CustomLinkerOptionsFile>$([System.IO.Path]::GetFullPath('$(IntermediateOutputPath)custom-linker-options.txt')) @@ -245,6 +252,7 @@ DeploymentTarget=$(_MinimumOSVersion) @(_BundlerEnvironmentVariables -> 'EnvironmentVariable=%(Identity)=%(Value)') @(_XamarinFrameworkAssemblies -> 'FrameworkAssembly=%(Filename)') + GlobalizationDataFile=$(GlobalizationDataFile) Interpreter=$(MtouchInterpreter) IntermediateLinkDir=$(IntermediateLinkDir) InvariantGlobalization=$(InvariantGlobalization) @@ -702,7 +710,7 @@ + Condition="'$(_PlatformName)' != 'macOS' And '$(InvariantGlobalization)' != 'true' And '%(Filename)%(Extension)' == '$(GlobalizationDataFile)'" /> diff --git a/runtime/runtime.m b/runtime/runtime.m index 6d269d766d8d..b63700176da7 100644 --- a/runtime/runtime.m +++ b/runtime/runtime.m @@ -55,6 +55,9 @@ #endif int xamarin_log_level = 0; const char *xamarin_executable_name = NULL; +#if DOTNET +const char *xamarin_icu_dat_file_path = NULL; +#endif #if MONOMAC || TARGET_OS_MACCATALYST NSString * xamarin_custom_bundle_name = @"MonoBundle"; #endif @@ -2440,7 +2443,7 @@ -(void) xamarinSetFlags: (enum XamarinGCHandleFlags) flags; const char *propertyValues[] = { xamarin_get_bundle_path (), pinvokeOverride, - "icudt.dat", + xamarin_icu_dat_file_path, }; static_assert (sizeof (propertyKeys) == sizeof (propertyValues), "The number of keys and values must be the same."); diff --git a/tests/dotnet/Makefile b/tests/dotnet/Makefile index c90c03c3741e..129406d9e2e4 100644 --- a/tests/dotnet/Makefile +++ b/tests/dotnet/Makefile @@ -58,5 +58,5 @@ build-oldnet: build-dotnet: $(TARGETS) $(DOTNET6) build size-comparison/MySingleView/dotnet/MySingleView.csproj --runtime ios-arm64 $(COMMON_ARGS) /bl:$@.binlog -run-dotnet: build-dotnet +run-dotnet: $(TARGETS) $(DOTNET6) build -t:Run size-comparison/MySingleView/dotnet/MySingleView.csproj diff --git a/tools/dotnet-linker/LinkerConfiguration.cs b/tools/dotnet-linker/LinkerConfiguration.cs index 14a6524840e7..a5fffa8a4bb7 100644 --- a/tools/dotnet-linker/LinkerConfiguration.cs +++ b/tools/dotnet-linker/LinkerConfiguration.cs @@ -21,6 +21,7 @@ public class LinkerConfiguration { public string CacheDirectory { get; private set; } public Version DeploymentTarget { get; private set; } public HashSet FrameworkAssemblies { get; private set; } = new HashSet (); + public string GlobalizationDataFile { get; private set; } public string IntermediateLinkDir { get; private set; } public bool InvariantGlobalization { get; private set; } public string ItemsDirectory { get; private set; } @@ -229,6 +230,9 @@ public static LinkerConfiguration GetInstance (LinkContext context, bool createI throw new InvalidOperationException ($"Invalid XamarinRuntime '{value}' in {linker_file}"); Application.XamarinRuntime = rv; break; + case "GlobalizationDataFile": + GlobalizationDataFile = value; + break; case "InvariantGlobalization": InvariantGlobalization = string.Equals ("true", value, StringComparison.OrdinalIgnoreCase); break; diff --git a/tools/dotnet-linker/Steps/GenerateMainStep.cs b/tools/dotnet-linker/Steps/GenerateMainStep.cs index d0e7536728a6..5f9630a55a00 100644 --- a/tools/dotnet-linker/Steps/GenerateMainStep.cs +++ b/tools/dotnet-linker/Steps/GenerateMainStep.cs @@ -29,10 +29,15 @@ protected override void TryEndProcess () var contents = new StringBuilder (); contents.AppendLine ("#include "); + contents.AppendLine (); + contents.AppendLine ("extern \"C\" const char *xamarin_icu_dat_file_path;"); + contents.AppendLine (); contents.AppendLine ("static void xamarin_initialize_dotnet ()"); contents.AppendLine ("{"); if (Configuration.InvariantGlobalization) { contents.AppendLine ("\tsetenv (\"DOTNET_SYSTEM_GLOBALIZATION_INVARIANT\", \"1\", 1);"); + } else { + contents.AppendLine ($"\txamarin_icu_dat_file_path = \"{Configuration.GlobalizationDataFile}\";"); } contents.AppendLine ("}"); contents.AppendLine ();