Skip to content

Commit

Permalink
Wasi icu loading (#82444)
Browse files Browse the repository at this point in the history
Added icu loading in WASI
  • Loading branch information
mkhamoyan authored Feb 23, 2023
1 parent 7219954 commit fed0691
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/libraries/tests.proj
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@
<SmokeTestProject Include="$(MSBuildThisFileDirectory)System.Collections\tests\System.Collections.Tests.csproj" />
<SmokeTestProject Include="$(MSBuildThisFileDirectory)System.Private.Xml.Linq\tests\Schema\System.Xml.Schema.Extensions.Tests.csproj" />
<SmokeTestProject Include="$(MSBuildThisFileDirectory)System.Runtime.Serialization.Json\tests\System.Runtime.Serialization.Json.Tests.csproj" />
<SmokeTestProject Include="$(MSBuildThisFileDirectory)System.Globalization\tests\System.Globalization.Tests.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(TestNativeAot)' == 'true'">
Expand Down
3 changes: 2 additions & 1 deletion src/mono/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/mono/mono/mini/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/mono/wasi/build/WasiApp.Native.targets
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@

<ItemGroup>
<_WasmCommonCFlags Include="-DGEN_PINVOKE=1" />

<_WasmCommonCFlags Include="-DINVARIANT_GLOBALIZATION=1" Condition="'$(InvariantGlobalization)' == 'true'"/>
<!-- Adding optimization flag at the top, so it gets precedence -->
<!--<_EmccCFlags Include="$(EmccCompileOptimizationFlag)" />-->
<!--<_EmccCFlags Include="-s ASSERTIONS=$(_EmccAssertionLevelDefault)" Condition="'$(_WasmDevel)' == 'true'" />-->
Expand Down Expand Up @@ -226,9 +226,9 @@
<_WasmPInvokeModules Include="%(_WasmNativeFileForLinking.FileName)" Condition="'%(_WasmNativeFileForLinking.ScanForPInvokes)' != 'false'" />

<_WasmPInvokeModules Include="libSystem.Native" />
<_WasmPInvokeModules Include="libSystem.Globalization.Native" />
<!-- FIXME: wasi -->
<!--<_WasmPInvokeModules Include="libSystem.IO.Compression.Native" />-->
<!--<_WasmPInvokeModules Include="libSystem.Globalization.Native" />-->
</ItemGroup>

<PropertyGroup>
Expand Down
11 changes: 9 additions & 2 deletions src/mono/wasi/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 30 additions & 4 deletions src/mono/wasi/runtime/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#include <errno.h>
#include <sys/stat.h>

#define INVARIANT_GLOBALIZATION 1

#include <mono/metadata/appdomain.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/class.h>
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
9 changes: 3 additions & 6 deletions src/mono/wasi/wasi.proj
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@
<ItemGroup>
<WasmPInvokeModule Include="libSystem.Native" />
<WasmPInvokeModule Include="libSystem.IO.Compression.Native" />
<!--
<WasmPInvokeModule Include="libSystem.Globalization.Native" />
-->
<WasmPInvokeAssembly Include="@(LibrariesRuntimeFiles)" Condition="'%(Extension)' == '.dll' and '%(IsNative)' != 'true'" />
</ItemGroup>

Expand Down Expand Up @@ -92,8 +90,8 @@
<ItemGroup>
<_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('\','/'))"/>
Expand Down Expand Up @@ -177,11 +175,10 @@
<CMakeBuildRuntimeConfigureCmd>$(CMakeBuildRuntimeConfigureCmd) -DCONFIGURATION_WASM_OPT_FLAGS=&quot;@(WasmOptConfigurationFlags, ';')&quot;</CMakeBuildRuntimeConfigureCmd>
<CMakeBuildRuntimeConfigureCmd>$(CMakeBuildRuntimeConfigureCmd) -DMONO_INCLUDES=&quot;$(MonoArtifactsPath)include/mono-2.0&quot;</CMakeBuildRuntimeConfigureCmd>
<CMakeBuildRuntimeConfigureCmd>$(CMakeBuildRuntimeConfigureCmd) -DMONO_OBJ_INCLUDES=&quot;$(MonoObjDir.TrimEnd('\/'))&quot;</CMakeBuildRuntimeConfigureCmd>
<!-- TODOWASI
<CMakeBuildRuntimeConfigureCmd>$(CMakeBuildRuntimeConfigureCmd) -DICU_LIB_DIR=&quot;$(ICULibDir.TrimEnd('\/'))&quot;</CMakeBuildRuntimeConfigureCmd>
-->
<CMakeBuildRuntimeConfigureCmd>$(CMakeBuildRuntimeConfigureCmd) -DMONO_ARTIFACTS_DIR=&quot;$(MonoArtifactsPath.TrimEnd('\/'))&quot;</CMakeBuildRuntimeConfigureCmd>
<CMakeBuildRuntimeConfigureCmd>$(CMakeBuildRuntimeConfigureCmd) -DNATIVE_BIN_DIR=&quot;$(NativeBinDir.TrimEnd('\/'))&quot;</CMakeBuildRuntimeConfigureCmd>
<CMakeBuildRuntimeConfigureCmd Condition="'$(InvariantGlobalization)' == 'true'">$(CMakeBuildRuntimeConfigureCmd) -DINVARIANT_GLOBALIZATION=1</CMakeBuildRuntimeConfigureCmd>
<CMakeBuildRuntimeConfigureCmd Condition="'$(WasmEnableSIMD)' == 'true' and '$(Configuration)' == 'Release'">$(CMakeBuildRuntimeConfigureCmd) -DWASM_OPT_ADDITIONAL_FLAGS=&quot;--enable-simd&quot;</CMakeBuildRuntimeConfigureCmd>
<CMakeBuildRuntimeConfigureCmd Condition="'$(MonoWasmThreads)' == 'true'">$(CMakeBuildRuntimeConfigureCmd) -DDISABLE_THREADS=0</CMakeBuildRuntimeConfigureCmd>
<CMakeBuildRuntimeConfigureCmd Condition="'$(MonoWasmThreadsNoUser)' == 'true'">$(CMakeBuildRuntimeConfigureCmd) -DDISABLE_WASM_USER_THREADS=1</CMakeBuildRuntimeConfigureCmd>
Expand Down
12 changes: 9 additions & 3 deletions src/native/libs/System.Globalization.Native/pal_icushim_static.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <emscripten.h>

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)
Expand All @@ -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
Expand All @@ -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)
{

Expand Down

0 comments on commit fed0691

Please sign in to comment.