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

Wasi icu loading #82444

Merged
merged 7 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@
<PlatformManifestFileEntry Include="stubs.c" IsNative="true" />
<PlatformManifestFileEntry Include="synthetic-pthread.c" IsNative="true" />
<!-- ICU-specific files -->
<PlatformManifestFileEntry Include="libc++.a" IsNative="true" />
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved
<PlatformManifestFileEntry Include="libc++abi.a" IsNative="true" />
<PlatformManifestFileEntry Include="libicudata.a" IsNative="true" />
<PlatformManifestFileEntry Include="libicui18n.a" IsNative="true" />
<PlatformManifestFileEntry Include="libicuuc.a" IsNative="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 @@ -46,7 +46,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
2 changes: 1 addition & 1 deletion 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
4 changes: 2 additions & 2 deletions src/mono/wasi/build/WasiApp.targets
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@

<Target Name="_GetWasiGenerateAppBundleDependencies">
<PropertyGroup>
<!--<WasmIcuDataFileName Condition="'$(InvariantGlobalization)' != 'true'">icudt.dat</WasmIcuDataFileName>-->
<WasmIcuDataFileName Condition="'$(InvariantGlobalization)' != 'true'">icudt.dat</WasmIcuDataFileName>

<_HasDotnetWasm Condition="'%(WasmNativeAsset.FileName)%(WasmNativeAsset.Extension)' == 'dotnet.wasm'">true</_HasDotnetWasm>
</PropertyGroup>
Expand All @@ -328,7 +328,7 @@
<!-- If dotnet.{wasm,js} weren't added already (eg. AOT can add them), then add the default ones -->
<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.wasm" Condition="'$(_HasDotnetWasm)' != 'true'" />

<!--<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)$(WasmIcuDataFileName)" Condition="'$(InvariantGlobalization)' != 'true'" />-->
<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)$(WasmIcuDataFileName)" Condition="'$(InvariantGlobalization)' != 'true'" />
<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.timezones.blat" />

<WasmFilesToIncludeInFileSystem Include="@(WasmNativeAsset)" Condition="'%(WasmNativeAsset.FileName)%(WasmNativeAsset.Extension)' == 'dotnet.js.symbols'" />
Expand Down
7 changes: 5 additions & 2 deletions src/mono/wasi/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ target_compile_options(dotnet PUBLIC @${NATIVE_BIN_DIR}/src/wasi-default.rsp @${
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
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved
${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, "%s/%s", ".", "icudt.dat");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is constant string, you don't have to concatenate it here.


fileptr = fopen(filename, "rb");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fopen("./icudt.dat", "rb");

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filename is also used in lines 342 and 352.

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
12 changes: 5 additions & 7 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 @@ -148,7 +146,9 @@
DependsOnTargets="GenerateWasiPropsAndRspFiles;GenerateManagedToNative;BundleTimeZones">

<ItemGroup>
<ICULibNativeFiles Include="$(ICULibDir)/libicuuc.a;
<ICULibNativeFiles Include="$(WASI_SDK_PATH)share/wasi-sysroot/lib/wasm32-wasi/libc++.a;
$(WASI_SDK_PATH)share/wasi-sysroot/lib/wasm32-wasi/libc++abi.a;
$(ICULibDir)/libicuuc.a;
$(ICULibDir)/libicui18n.a;
$(ICULibDir)/libicudata.a" />
<ICULibFiles Include="$(ICULibDir)/*.dat" />
Expand Down Expand Up @@ -177,9 +177,7 @@
<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="'$(WasmEnableSIMD)' == 'true' and '$(Configuration)' == 'Release'">$(CMakeBuildRuntimeConfigureCmd) -DWASM_OPT_ADDITIONAL_FLAGS=&quot;--enable-simd&quot;</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