Skip to content

Commit 604b426

Browse files
kateinoigakukunsffc
authored andcommitted
ICU-22838 Add WebAssembly/WASI cross-compilation support
* Add config/mh-wasi and update configure script. WebAssembly/WASI does not support threads and dynamic linking, so the new mh-wasi file disables those features in ICU. * Teach putilimp.h not to define U_TZSET, U_TIMEZONE, and U_TZNAME for WASI WASI does not define timezone-related functionalities, and wasi-libc does not provide tzset, timezone, and tzname. This change teaches putilimp.h not to define U_TZSET, U_TIMEZONE, and U_TZNAME for WASI. * Add GitHub Actions workflow to cross-compile ICU4C for WebAssembly/WASI
1 parent 1e0a11b commit 604b426

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-0
lines changed

.github/workflows/icu4c.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,50 @@ jobs:
532532
set PATH=C:\tools\msys64\mingw64\bin;%PATH%
533533
.\icuinfo.exe
534534
535+
# WebAssembly build using the WASI SDK.
536+
wasm-wasi-sdk:
537+
runs-on: ubuntu-22.04 # Updated in BRS
538+
steps:
539+
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
540+
541+
- name: Install WASI SDK
542+
env:
543+
WASI_SDK_DOWNLOAD_URL: https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-27/wasi-sdk-27.0-x86_64-linux.deb
544+
WASI_SDK_DOWNLOAD_SHA256: 3ed68fc89e3299c0e2333360efac89ec60fbc01870732a486ab386267355cc80
545+
run: |
546+
wget -O /tmp/wasi-sdk.deb "${WASI_SDK_DOWNLOAD_URL}"
547+
echo "${WASI_SDK_DOWNLOAD_SHA256} /tmp/wasi-sdk.deb" | sha256sum -c -
548+
sudo apt-get install /tmp/wasi-sdk.deb
549+
echo "WASI_SDK_PATH=/opt/wasi-sdk" >> $GITHUB_ENV
550+
551+
- name: Build tools for host system
552+
run: |
553+
TOOLS_BUILD_DIR="$PWD/build/tools";
554+
mkdir -p "$TOOLS_BUILD_DIR";
555+
pushd "$TOOLS_BUILD_DIR";
556+
../../icu4c/source/configure
557+
make -j$(nproc)
558+
echo "TOOLS_BUILD_DIR=$TOOLS_BUILD_DIR" >> $GITHUB_ENV
559+
560+
- name: Cross-build for WebAssembly with WASI SDK
561+
run: |
562+
CROSS_BUILD_DIR="$PWD/build/cross";
563+
mkdir -p "$CROSS_BUILD_DIR";
564+
pushd "$CROSS_BUILD_DIR";
565+
../../icu4c/source/configure --host=wasm32-unknown-wasip1 \
566+
--with-cross-build="$TOOLS_BUILD_DIR" \
567+
--enable-static --disable-shared \
568+
--disable-tools --disable-tests \
569+
--disable-samples --disable-extras \
570+
--with-data-packaging=static \
571+
--prefix / \
572+
CC="$WASI_SDK_PATH/bin/clang" CXX="$WASI_SDK_PATH/bin/clang++" \
573+
AR="$WASI_SDK_PATH/bin/llvm-ar" RANLIB="$WASI_SDK_PATH/bin/llvm-ranlib" \
574+
CFLAGS="-D_WASI_EMULATED_SIGNAL" \
575+
CXXFLAGS="-fno-exceptions -D_WASI_EMULATED_SIGNAL"
576+
make -j$(nproc) PKGDATA_OPTS="--without-assembly -O $CROSS_BUILD_DIR/data/icupkg.inc"
577+
make install DESTDIR="$CROSS_BUILD_DIR/install"
578+
535579
# Run ICU4C tests with stubdata.
536580
run-with-stubdata:
537581
runs-on: ubuntu-22.04 # Updated in BRS

icu4c/source/acinclude.m4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ powerpc*-apple-darwin*) icu_cv_host_frag=mh-darwin-ppc ;;
8585
*-dec-osf*) icu_cv_host_frag=mh-alpha-osf ;;
8686
*-*-nto*) icu_cv_host_frag=mh-qnx ;;
8787
*-ncr-*) icu_cv_host_frag=mh-mpras ;;
88+
*-wasi*) icu_cv_host_frag=mh-wasi ;;
8889
*) icu_cv_host_frag=mh-unknown ;;
8990
esac
9091
]

icu4c/source/common/putilimp.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ typedef size_t uintptr_t;
107107
/* not defined */
108108
#elif U_PLATFORM == U_PF_HAIKU
109109
/* not defined */
110+
#elif defined(__wasi__)
111+
/* not defined */
110112
#else
111113
# define U_TZSET tzset
112114
#endif
@@ -132,6 +134,8 @@ typedef size_t uintptr_t;
132134
/* not defined */
133135
#elif U_PLATFORM == U_PF_IPHONE
134136
/* not defined */
137+
#elif defined(__wasi__)
138+
/* not defined */
135139
#else
136140
# define U_TIMEZONE timezone
137141
#endif
@@ -147,6 +151,8 @@ typedef size_t uintptr_t;
147151
/* not defined */
148152
#elif U_PLATFORM == U_PF_HAIKU
149153
/* not defined, (well it is but a loop back to icu) */
154+
#elif defined(__wasi__)
155+
/* not defined */
150156
#else
151157
# define U_TZNAME tzname
152158
#endif

icu4c/source/config/mh-wasi

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
## -*-makefile-*-
2+
## Copyright (C) 2024 and later: Unicode, Inc. and others.
3+
## License & terms of use: http://www.unicode.org/copyright.html
4+
## WASI-specific setup
5+
6+
## Commands to generate dependency files
7+
GEN_DEPS.c= $(CC) -E -MM $(DEFS) $(CPPFLAGS)
8+
GEN_DEPS.cc= $(CXX) -E -MM $(DEFS) $(CPPFLAGS) $(CXXFLAGS)
9+
10+
## Flags for position independent code
11+
SHAREDLIBCFLAGS = -fPIC
12+
SHAREDLIBCXXFLAGS = -fPIC
13+
SHAREDLIBCPPFLAGS = -DPIC
14+
15+
## Additional flags when building libraries and with threads
16+
# WASI Preview 1 without wasi-threads does not support threads
17+
THREADSCPPFLAGS =
18+
LIBCPPFLAGS =
19+
20+
## Compiler switch to embed a runtime search path
21+
LD_RPATH=
22+
LD_RPATH_PRE =
23+
24+
## These are the library specific LDFLAGS
25+
LDFLAGSICUDT=-nodefaultlibs -nostdlib
26+
27+
## Shared object suffix
28+
SO = so
29+
## Non-shared intermediate object suffix
30+
STATIC_O = ao
31+
32+
## Compilation rules
33+
%.$(STATIC_O): $(srcdir)/%.c
34+
$(call SILENT_COMPILE,$(strip $(COMPILE.c) $(STATICCPPFLAGS) $(STATICCFLAGS)) -o $@ $<)
35+
%.o: $(srcdir)/%.c
36+
$(call SILENT_COMPILE,$(strip $(COMPILE.c) $(DYNAMICCPPFLAGS) $(DYNAMICCFLAGS)) -o $@ $<)
37+
38+
%.$(STATIC_O): $(srcdir)/%.cpp
39+
$(call SILENT_COMPILE,$(strip $(COMPILE.cc) $(STATICCPPFLAGS) $(STATICCXXFLAGS)) -o $@ $<)
40+
%.o: $(srcdir)/%.cpp
41+
$(call SILENT_COMPILE,$(strip $(COMPILE.cc) $(DYNAMICCPPFLAGS) $(DYNAMICCXXFLAGS)) -o $@ $<)
42+
43+
44+
## Dependency rules
45+
%.d: $(srcdir)/%.c
46+
$(call ICU_MSG,(deps)) $<
47+
@$(SHELL) -ec '$(GEN_DEPS.c) $< \
48+
| sed '\''s%\($*\)\.o[ :]*%\1.o $@ : %g'\'' > $@; \
49+
[ -s $@ ] || rm -f $@'
50+
51+
%.d: $(srcdir)/%.cpp
52+
$(call ICU_MSG,(deps)) $<
53+
@$(SHELL) -ec '$(GEN_DEPS.cc) $< \
54+
| sed '\''s%\($*\)\.o[ :]*%\1.o $@ : %g'\'' > $@; \
55+
[ -s $@ ] || rm -f $@'
56+
57+
## Versioned libraries rules
58+
59+
%.$(SO).$(SO_TARGET_VERSION_MAJOR): %.$(SO).$(SO_TARGET_VERSION)
60+
$(RM) $@ && ln -s ${<F} $@
61+
%.$(SO): %.$(SO).$(SO_TARGET_VERSION_MAJOR)
62+
$(RM) $@ && ln -s ${*F}.$(SO).$(SO_TARGET_VERSION) $@
63+
64+
## Bind internal references
65+
66+
# LDflags that pkgdata will use
67+
BIR_LDFLAGS= -Wl,-Bsymbolic
68+
69+
# Dependencies [i.e. map files] for the final library
70+
BIR_DEPS=
71+
72+
## Remove shared library 's'
73+
STATIC_PREFIX_WHEN_USED =
74+
STATIC_PREFIX =
75+
76+
## End WASI-specific setup

icu4c/source/configure

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5833,6 +5833,7 @@ powerpc*-apple-darwin*) icu_cv_host_frag=mh-darwin-ppc ;;
58335833
*-dec-osf*) icu_cv_host_frag=mh-alpha-osf ;;
58345834
*-*-nto*) icu_cv_host_frag=mh-qnx ;;
58355835
*-ncr-*) icu_cv_host_frag=mh-mpras ;;
5836+
*-wasi*) icu_cv_host_frag=mh-wasi ;;
58365837
*) icu_cv_host_frag=mh-unknown ;;
58375838
esac
58385839

0 commit comments

Comments
 (0)