From fed069174b7afa0b078b0aeac5dc61778cd21f89 Mon Sep 17 00:00:00 2001 From: Meri Khamoyan <96171496+mkhamoyan@users.noreply.github.com> Date: Thu, 23 Feb 2023 20:54:12 +0400 Subject: [PATCH] Wasi icu loading (#82444) Added icu loading in WASI --- src/libraries/tests.proj | 1 + src/mono/CMakeLists.txt | 3 +- src/mono/mono/mini/CMakeLists.txt | 2 +- src/mono/wasi/build/WasiApp.Native.targets | 4 +-- src/mono/wasi/runtime/CMakeLists.txt | 11 ++++-- src/mono/wasi/runtime/driver.c | 34 ++++++++++++++++--- src/mono/wasi/wasi.proj | 9 ++--- .../pal_icushim_static.c | 12 +++++-- 8 files changed, 57 insertions(+), 19 deletions(-) diff --git a/src/libraries/tests.proj b/src/libraries/tests.proj index 8e5c828464045..aa30ee2bdb52e 100644 --- a/src/libraries/tests.proj +++ b/src/libraries/tests.proj @@ -541,6 +541,7 @@ + diff --git a/src/mono/CMakeLists.txt b/src/mono/CMakeLists.txt index 6fbfbce25053f..3438291b0898c 100644 --- a/src/mono/CMakeLists.txt +++ b/src/mono/CMakeLists.txt @@ -688,7 +688,8 @@ elseif(HOST_OSX AND NOT HOST_MACCAT) set(ICU_FLAGS "-DTARGET_UNIX -DU_DISABLE_RENAMING -Wno-reserved-id-macro -Wno-documentation -Wno-documentation-unknown-command -Wno-switch-enum -Wno-covered-switch-default -Wno-extra-semi-stmt -Wno-unknown-warning-option -Wno-deprecated-declarations") set(HAVE_SYS_ICU 1) elseif(HOST_WASI) - set(HAVE_SYS_ICU 0) + set(ICU_FLAGS "-DPALEXPORT=\"\" -DU_DISABLE_RENAMING -DHAVE_UDAT_STANDALONE_SHORTER_WEEKDAYS -DHAVE_SET_MAX_VARIABLE -DTARGET_UNIX -Wno-reserved-id-macro -Wno-documentation -Wno-documentation-unknown-command -Wno-switch-enum -Wno-covered-switch-default -Wno-extra-semi-stmt -Wno-unknown-warning-option") + set(HAVE_SYS_ICU 1) set(STATIC_ICU 1) set(ICU_LIBS "icucore") elseif(HOST_BROWSER) diff --git a/src/mono/mono/mini/CMakeLists.txt b/src/mono/mono/mini/CMakeLists.txt index 96887f82bc259..2e2893bc878d9 100644 --- a/src/mono/mono/mini/CMakeLists.txt +++ b/src/mono/mono/mini/CMakeLists.txt @@ -44,7 +44,7 @@ if(HOST_WIN32) endif() # ICU -if(HAVE_SYS_ICU AND NOT HOST_WASI) +if(HAVE_SYS_ICU) if(STATIC_ICU) set(pal_icushim_sources_base pal_icushim_static.c) diff --git a/src/mono/wasi/build/WasiApp.Native.targets b/src/mono/wasi/build/WasiApp.Native.targets index 2b6fff044afd0..98a63f0312e64 100644 --- a/src/mono/wasi/build/WasiApp.Native.targets +++ b/src/mono/wasi/build/WasiApp.Native.targets @@ -169,7 +169,7 @@ <_WasmCommonCFlags Include="-DGEN_PINVOKE=1" /> - + <_WasmCommonCFlags Include="-DINVARIANT_GLOBALIZATION=1" Condition="'$(InvariantGlobalization)' == 'true'"/> @@ -226,9 +226,9 @@ <_WasmPInvokeModules Include="%(_WasmNativeFileForLinking.FileName)" Condition="'%(_WasmNativeFileForLinking.ScanForPInvokes)' != 'false'" /> <_WasmPInvokeModules Include="libSystem.Native" /> + <_WasmPInvokeModules Include="libSystem.Globalization.Native" /> - diff --git a/src/mono/wasi/runtime/CMakeLists.txt b/src/mono/wasi/runtime/CMakeLists.txt index 8e4114f0c31e4..a1320ffbe1c3a 100644 --- a/src/mono/wasi/runtime/CMakeLists.txt +++ b/src/mono/wasi/runtime/CMakeLists.txt @@ -9,13 +9,20 @@ set(CMAKE_EXECUTABLE_SUFFIX ".wasm") add_executable(dotnet driver.c pinvoke.c stubs.c synthetic-pthread.c) target_include_directories(dotnet PUBLIC ${MONO_INCLUDES} ${MONO_OBJ_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR}/include/wasm) +if(INVARIANT_GLOBALIZATION) +target_compile_options(dotnet PUBLIC @${NATIVE_BIN_DIR}/src/wasi-default.rsp @${NATIVE_BIN_DIR}/src/wasi-compile.rsp -DCORE_BINDINGS -DGEN_PINVOKE=1 -DINVARIANT_GLOBALIZATION=1) +else() target_compile_options(dotnet PUBLIC @${NATIVE_BIN_DIR}/src/wasi-default.rsp @${NATIVE_BIN_DIR}/src/wasi-compile.rsp -DCORE_BINDINGS -DGEN_PINVOKE=1) +endif() set_target_properties(dotnet PROPERTIES COMPILE_FLAGS ${CONFIGURATION_WASICC_FLAGS}) target_link_libraries(dotnet - #TODOWASI ${ICU_LIB_DIR}/libicuuc.a - #TODOWASI ${ICU_LIB_DIR}/libicui18n.a + ${CMAKE_SYSROOT}/lib/wasm32-wasi/libc++.a + ${CMAKE_SYSROOT}/lib/wasm32-wasi/libc++abi.a + ${ICU_LIB_DIR}/libicudata.a + ${ICU_LIB_DIR}/libicuuc.a + ${ICU_LIB_DIR}/libicui18n.a ${MONO_ARTIFACTS_DIR}/libmono-component-hot_reload-static.a ${MONO_ARTIFACTS_DIR}/libmono-component-debugger-static.a ${MONO_ARTIFACTS_DIR}/libmono-component-diagnostics_tracing-stub-static.a diff --git a/src/mono/wasi/runtime/driver.c b/src/mono/wasi/runtime/driver.c index 1689d36ab244a..b6b24bd94f2fa 100644 --- a/src/mono/wasi/runtime/driver.c +++ b/src/mono/wasi/runtime/driver.c @@ -9,8 +9,6 @@ #include #include -#define INVARIANT_GLOBALIZATION 1 - #include #include #include @@ -67,6 +65,8 @@ char *mono_method_get_full_name (MonoMethod *method); extern const char* dotnet_wasi_getbundledfile(const char* name, int* out_length); extern void dotnet_wasi_registerbundledassemblies(); extern const char* dotnet_wasi_getentrypointassemblyname(); +int32_t mono_wasi_load_icu_data(const void* pData); +void load_icu_data (void); int mono_wasm_enable_gc = 1; @@ -329,7 +329,33 @@ mono_wasm_register_bundled_satellite_assemblies (void) } } -void mono_wasm_link_icu_shim (void); +void load_icu_data (void) +{ + FILE *fileptr; + unsigned char *buffer; + long filelen; + char filename[256]; + sprintf(filename, "./icudt.dat"); + + fileptr = fopen(filename, "rb"); + if (fileptr == 0) { + printf("Failed to load %s\n", filename); + fflush(stdout); + } + + fseek(fileptr, 0, SEEK_END); + filelen = ftell(fileptr); + rewind(fileptr); + + buffer = (unsigned char *)malloc(filelen * sizeof(char)); + if(!fread(buffer, filelen, 1, fileptr)) { + printf("Failed to load %s\n", filename); + fflush(stdout); + } + fclose(fileptr); + + assert(mono_wasi_load_icu_data(buffer)); +} void cleanup_runtime_config (MonovmRuntimeConfigArguments *args, void *user_data) @@ -344,7 +370,7 @@ mono_wasm_load_runtime (const char *unused, int debug_level) const char *interp_opts = ""; #ifndef INVARIANT_GLOBALIZATION - mono_wasm_link_icu_shim (); + load_icu_data(); #else monoeg_g_setenv ("DOTNET_SYSTEM_GLOBALIZATION_INVARIANT", "true", 1); #endif diff --git a/src/mono/wasi/wasi.proj b/src/mono/wasi/wasi.proj index 387af89db0951..a7f58f0aac4f6 100644 --- a/src/mono/wasi/wasi.proj +++ b/src/mono/wasi/wasi.proj @@ -50,9 +50,7 @@ - @@ -92,8 +90,8 @@ <_WasiFlags Include="@(_WasiCommonFlags)" /> - <_WasiCompileFlags Condition="'$(MonoWasmThreads)' == 'true'" Include="-I$([MSBuild]::NormalizePath('$(PkgMicrosoft_NETCore_Runtime_ICU_Transport)', 'runtimes', 'browser-wasm-threads', 'native', 'include').Replace('\','/'))"/> - <_WasiCompileFlags Condition="'$(MonoWasmThreads)' != 'true'" Include="-I$([MSBuild]::NormalizePath('$(PkgMicrosoft_NETCore_Runtime_ICU_Transport)', 'runtimes', 'browser-wasm', 'native', 'include').Replace('\','/'))"/> + <_WasiCompileFlags Condition="'$(MonoWasmThreads)' == 'true'" Include="-I$([MSBuild]::NormalizePath('$(PkgMicrosoft_NETCore_Runtime_ICU_Transport)', 'runtimes', 'wasi-wasm-threads', 'native', 'include').Replace('\','/'))"/> + <_WasiCompileFlags Condition="'$(MonoWasmThreads)' != 'true'" Include="-I$([MSBuild]::NormalizePath('$(PkgMicrosoft_NETCore_Runtime_ICU_Transport)', 'runtimes', 'wasi-wasm', 'native', 'include').Replace('\','/'))"/> <_WasiCompileFlags Include="-I$([MSBuild]::NormalizePath('$(MonoProjectRoot)', 'wasi', 'include').Replace('\','/'))"/> <_WasiCompileFlags Include="-I$([MSBuild]::NormalizePath('$(MonoProjectRoot)', 'wasi', 'mono-include').Replace('\','/'))"/> <_WasiCompileFlags Include="-I$([MSBuild]::NormalizePath('$(RepoRoot)', 'src', 'native', 'public').Replace('\','/'))"/> @@ -177,11 +175,10 @@ $(CMakeBuildRuntimeConfigureCmd) -DCONFIGURATION_WASM_OPT_FLAGS="@(WasmOptConfigurationFlags, ';')" $(CMakeBuildRuntimeConfigureCmd) -DMONO_INCLUDES="$(MonoArtifactsPath)include/mono-2.0" $(CMakeBuildRuntimeConfigureCmd) -DMONO_OBJ_INCLUDES="$(MonoObjDir.TrimEnd('\/'))" - $(CMakeBuildRuntimeConfigureCmd) -DMONO_ARTIFACTS_DIR="$(MonoArtifactsPath.TrimEnd('\/'))" $(CMakeBuildRuntimeConfigureCmd) -DNATIVE_BIN_DIR="$(NativeBinDir.TrimEnd('\/'))" + $(CMakeBuildRuntimeConfigureCmd) -DINVARIANT_GLOBALIZATION=1 $(CMakeBuildRuntimeConfigureCmd) -DWASM_OPT_ADDITIONAL_FLAGS="--enable-simd" $(CMakeBuildRuntimeConfigureCmd) -DDISABLE_THREADS=0 $(CMakeBuildRuntimeConfigureCmd) -DDISABLE_WASM_USER_THREADS=1 diff --git a/src/native/libs/System.Globalization.Native/pal_icushim_static.c b/src/native/libs/System.Globalization.Native/pal_icushim_static.c index ce64ca70dac5b..69fcbb03ae31a 100644 --- a/src/native/libs/System.Globalization.Native/pal_icushim_static.c +++ b/src/native/libs/System.Globalization.Native/pal_icushim_static.c @@ -45,11 +45,11 @@ static void U_CALLCONV icu_trace_data(const void* context, int32_t fnNumber, int printf("[ICUDT] %s: %s\n", utrace_functionName(fnNumber), buf); } +static int32_t load_icu_data(const void* pData); + #ifdef __EMSCRIPTEN__ #include -static int32_t load_icu_data(const void* pData); - EMSCRIPTEN_KEEPALIVE const char* mono_wasm_get_icudt_name(const char* culture); EMSCRIPTEN_KEEPALIVE const char* mono_wasm_get_icudt_name(const char* culture) @@ -64,7 +64,6 @@ EMSCRIPTEN_KEEPALIVE int32_t mono_wasm_load_icu_data(const void* pData) return load_icu_data(pData); } - /* * driver.c calls this to make sure this file is linked, otherwise * its not, meaning the EMSCRIPTEN_KEEPALIVE functions above @@ -78,6 +77,13 @@ void mono_wasm_link_icu_shim(void) #endif +int32_t mono_wasi_load_icu_data(const void* pData); + +int32_t mono_wasi_load_icu_data(const void* pData) +{ + return load_icu_data(pData); +} + static int32_t load_icu_data(const void* pData) {