xref: /qemu/tests/docker/dockerfiles/emsdk-wasm32-cross.docker (revision c5f122fdcc280a82e7c5f31de890f985aa7ba773)
1# syntax = docker/dockerfile:1.5
2
3ARG EMSDK_VERSION_QEMU=3.1.50
4ARG ZLIB_VERSION=1.3.1
5ARG GLIB_MINOR_VERSION=2.84
6ARG GLIB_VERSION=${GLIB_MINOR_VERSION}.0
7ARG PIXMAN_VERSION=0.44.2
8ARG FFI_VERSION=v3.4.7
9ARG MESON_VERSION=1.5.0
10
11FROM emscripten/emsdk:$EMSDK_VERSION_QEMU AS build-base
12ARG MESON_VERSION
13ENV TARGET=/builddeps/target
14ENV CPATH="$TARGET/include"
15ENV PKG_CONFIG_PATH="$TARGET/lib/pkgconfig"
16ENV EM_PKG_CONFIG_PATH="$PKG_CONFIG_PATH"
17ENV CFLAGS="-O3 -pthread -DWASM_BIGINT"
18ENV CXXFLAGS="$CFLAGS"
19ENV LDFLAGS="-sWASM_BIGINT -sASYNCIFY=1 -L$TARGET/lib"
20RUN apt-get update && apt-get install -y \
21    autoconf \
22    build-essential \
23    libglib2.0-dev \
24    libtool \
25    pkgconf \
26    ninja-build \
27    python3-pip
28RUN pip3 install meson==${MESON_VERSION} tomli
29RUN mkdir /build
30WORKDIR /build
31RUN mkdir -p $TARGET
32RUN <<EOF
33cat <<EOT > /cross.meson
34[host_machine]
35system = 'emscripten'
36cpu_family = 'wasm32'
37cpu = 'wasm32'
38endian = 'little'
39
40[binaries]
41c = 'emcc'
42cpp = 'em++'
43ar = 'emar'
44ranlib = 'emranlib'
45pkgconfig = ['pkg-config', '--static']
46EOT
47EOF
48
49FROM build-base AS zlib-dev
50ARG ZLIB_VERSION
51RUN mkdir -p /zlib
52RUN curl -Ls https://zlib.net/zlib-$ZLIB_VERSION.tar.xz | \
53    tar xJC /zlib --strip-components=1
54WORKDIR /zlib
55RUN emconfigure ./configure --prefix=$TARGET --static
56RUN emmake make install -j$(nproc)
57
58FROM build-base AS libffi-dev
59ARG FFI_VERSION
60RUN mkdir -p /libffi
61RUN git clone https://github.com/libffi/libffi /libffi
62WORKDIR /libffi
63RUN git checkout $FFI_VERSION
64RUN autoreconf -fiv
65RUN emconfigure ./configure --host=wasm32-unknown-linux \
66    --prefix=$TARGET --enable-static \
67    --disable-shared --disable-dependency-tracking \
68    --disable-builddir --disable-multi-os-directory \
69    --disable-raw-api --disable-docs
70RUN emmake make install SUBDIRS='include' -j$(nproc)
71
72FROM build-base AS pixman-dev
73ARG PIXMAN_VERSION
74RUN mkdir /pixman/
75RUN git clone  https://gitlab.freedesktop.org/pixman/pixman /pixman/
76WORKDIR /pixman
77RUN git checkout pixman-$PIXMAN_VERSION
78RUN <<EOF
79cat <<EOT >> /cross.meson
80[built-in options]
81c_args = [$(printf "'%s', " $CFLAGS | sed 's/, $//')]
82cpp_args = [$(printf "'%s', " $CFLAGS | sed 's/, $//')]
83objc_args = [$(printf "'%s', " $CFLAGS | sed 's/, $//')]
84c_link_args = [$(printf "'%s', " $LDFLAGS | sed 's/, $//')]
85cpp_link_args = [$(printf "'%s', " $LDFLAGS | sed 's/, $//')]
86EOT
87EOF
88RUN meson setup _build --prefix=$TARGET --cross-file=/cross.meson \
89    --default-library=static \
90    --buildtype=release -Dtests=disabled -Ddemos=disabled
91RUN meson install -C _build
92
93FROM build-base AS glib-dev
94ARG GLIB_VERSION
95ARG GLIB_MINOR_VERSION
96RUN mkdir -p /stub
97WORKDIR /stub
98RUN <<EOF
99cat <<'EOT' > res_query.c
100#include <netdb.h>
101int res_query(const char *name, int class,
102              int type, unsigned char *dest, int len)
103{
104    h_errno = HOST_NOT_FOUND;
105    return -1;
106}
107EOT
108EOF
109RUN emcc ${CFLAGS} -c res_query.c -fPIC -o libresolv.o
110RUN ar rcs libresolv.a libresolv.o
111RUN mkdir -p $TARGET/lib/
112RUN cp libresolv.a $TARGET/lib/
113
114RUN mkdir -p /glib
115RUN curl -Lks https://download.gnome.org/sources/glib/${GLIB_MINOR_VERSION}/glib-$GLIB_VERSION.tar.xz | \
116    tar xJC /glib --strip-components=1
117
118COPY --link --from=zlib-dev /builddeps/ /builddeps/
119COPY --link --from=libffi-dev /builddeps/ /builddeps/
120
121WORKDIR /glib
122RUN <<EOF
123CFLAGS="$CFLAGS -Wno-incompatible-function-pointer-types" ;
124cat <<EOT >> /cross.meson
125[built-in options]
126c_args = [$(printf "'%s', " $CFLAGS | sed 's/, $//')]
127cpp_args = [$(printf "'%s', " $CFLAGS | sed 's/, $//')]
128objc_args = [$(printf "'%s', " $CFLAGS | sed 's/, $//')]
129c_link_args = [$(printf "'%s', " $LDFLAGS | sed 's/, $//')]
130cpp_link_args = [$(printf "'%s', " $LDFLAGS | sed 's/, $//')]
131EOT
132EOF
133RUN meson setup _build --prefix=$TARGET --cross-file=/cross.meson \
134    --default-library=static --buildtype=release --force-fallback-for=pcre2 \
135    -Dselinux=disabled -Dxattr=false -Dlibmount=disabled -Dnls=disabled \
136    -Dtests=false -Dglib_debug=disabled -Dglib_assert=false -Dglib_checks=false
137# FIXME: emscripten doesn't provide some pthread functions in the final link,
138#        which isn't detected during meson setup.
139RUN sed -i -E "/#define HAVE_POSIX_SPAWN 1/d" ./_build/config.h
140RUN sed -i -E "/#define HAVE_PTHREAD_GETNAME_NP 1/d" ./_build/config.h
141RUN meson install -C _build
142
143FROM build-base
144COPY --link --from=glib-dev /builddeps/ /builddeps/
145COPY --link --from=pixman-dev /builddeps/ /builddeps/
146