Skip to content

Commit 646cac5

Browse files
authored
Add docker image that can be used to build projects with wasi-sdk sysroot (#271)
1 parent 9173dd9 commit 646cac5

File tree

4 files changed

+118
-3
lines changed

4 files changed

+118
-3
lines changed

.dockerignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# We don't need any files to for build image.
2-
# Without this `docker build` will everything in this directory.
3-
*
1+
# Our docker builds do not require the submodule sources so exclude them as
2+
# they can be very big.
3+
/src

.github/workflows/main.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ on:
77
branches:
88
- main
99
pull_request:
10+
workflow_dispatch:
1011

1112
jobs:
1213
build:
@@ -93,6 +94,27 @@ jobs:
9394
- uses: actions/checkout@v1
9495
with:
9596
submodules: true
97+
98+
- uses: docker/login-action@v2
99+
with:
100+
registry: ghcr.io
101+
username: ${{ github.actor }}
102+
password: ${{ secrets.GITHUB_TOKEN }}
103+
104+
- uses: docker/setup-qemu-action@v2
105+
- uses: docker/setup-buildx-action@v2
106+
107+
- uses: docker/metadata-action@v4
108+
id: meta
109+
with:
110+
images: ghcr.io/${{ github.repository }}
111+
tags: |
112+
type=schedule
113+
type=ref,event=branch
114+
type=ref,event=tag
115+
type=ref,event=pr
116+
type=sha
117+
96118
- name: Run docker_build script
97119
run: ./docker_build.sh
98120
- name: Upload artifacts
@@ -101,3 +123,15 @@ jobs:
101123
# Upload the dist folder. Give it a name according to the OS it was built for.
102124
name: dist-ubuntu-xenial
103125
path: dist
126+
127+
- name: Build and push wasi-sdk docker image
128+
uses: docker/build-push-action@v3
129+
with:
130+
context: .
131+
file: docker/Dockerfile
132+
push: ${{ github.event_name != 'pull_request' }}
133+
platforms: linux/amd64,linux/arm64
134+
tags: ${{ steps.meta.outputs.tags }}
135+
labels: ${{ steps.meta.outputs.labels }}
136+
cache-from: type=gha
137+
cache-to: type=gha,mode=max

docker/Dockerfile

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Docker image with a build toolchain and environment variables set to use
2+
# the wasi-sdk sysroot. The SDK distribution must have first been built,
3+
# for example using docker_build.sh
4+
5+
# Extract built SDK archive to copy out the sysroot. We use an initial build
6+
# stage to do this to make sure it is only the sysroot, not the entire SDK
7+
# with binaries, that is included in the final image since we install those
8+
# separately.
9+
FROM ubuntu:22.04 as dist
10+
11+
ADD dist/wasi-sdk-*.*-linux.tar.gz /
12+
13+
# Move versioned folder to unversioned to using bash glob to allow
14+
# this file to be independent of major version number.
15+
RUN mv /wasi-sdk-* /wasi-sdk
16+
17+
FROM ubuntu:22.04
18+
19+
ENV LLVM_VERSION 15
20+
21+
# Install build toolchain including clang, ld, make, autotools, ninja, and cmake
22+
RUN apt-get update && \
23+
# Temporarily install to setup apt repositories
24+
apt-get install -y curl gnupg && \
25+
\
26+
curl -sS https://apt.llvm.org/llvm-snapshot.gpg.key | gpg --dearmor > /etc/apt/trusted.gpg.d/llvm.gpg && \
27+
echo "deb [signed-by=/etc/apt/trusted.gpg.d/llvm.gpg] http://apt.llvm.org/jammy/ llvm-toolchain-jammy-${LLVM_VERSION} main" >> /etc/apt/sources.list.d/llvm.list && \
28+
echo "deb-src [signed-by=/etc/apt/trusted.gpg.d/llvm.gpg] http://apt.llvm.org/jammy/ llvm-toolchain-jammy-${LLVM_VERSION} main" >> /etc/apt/sources.list.d/llvm.list && \
29+
\
30+
apt-get update && \
31+
apt-get install -y clang-${LLVM_VERSION} lld-${LLVM_VERSION} cmake ninja-build make autoconf autogen automake libtool && \
32+
apt-get remove -y curl gnupg && \
33+
rm -rf /var/lib/apt/lists/*
34+
35+
COPY --from=dist /wasi-sdk/share/wasi-sysroot/ /wasi-sysroot/
36+
# The path to the rt directory contains the LLVM patch version which is not reflected in the LLVM apt repository
37+
# or package. To make adding the RT robust to changing patch versions without needing to duplicate the folder
38+
# content, we symlink after extracting using a bash glob to resolve the patch version
39+
ADD dist/libclang_rt.builtins-wasm32-wasi-*.*.tar.gz /wasi-sysroot-clang_rt
40+
RUN ln -s /wasi-sysroot-clang_rt/lib/wasi/ $(echo /usr/lib/llvm-${LLVM_VERSION}/lib/clang/${LLVM_VERSION}.*)/lib/wasi
41+
42+
ADD docker/wasi-sdk.cmake /usr/share/cmake/wasi-sdk.cmake
43+
ENV CMAKE_TOOLCHAIN_FILE /usr/share/cmake/wasi-sdk.cmake
44+
ADD cmake/Platform/WASI.cmake /usr/share/cmake/Modules/Platform/WASI.cmake
45+
46+
ENV CC clang-${LLVM_VERSION}
47+
ENV CXX clang++-${LLVM_VERSION}
48+
ENV LD wasm-ld-${LLVM_VERSION}
49+
ENV AR llvm-ar-${LLVM_VERSION}
50+
ENV RANLIB llvm-ranlib-${LLVM_VERSION}
51+
52+
ENV CFLAGS --target=wasm32-wasi --sysroot=/wasi-sysroot
53+
ENV CXXFLAGS --target=wasm32-wasi --sysroot=/wasi-sysroot
54+
ENV LDFLAGS --target=wasm32-wasi --sysroot=/wasi-sysroot

docker/wasi-sdk.cmake

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Cmake toolchain description file for the wasi-sdk docker image
2+
3+
# This is arbitrary, AFAIK, for now.
4+
cmake_minimum_required(VERSION 3.4.0)
5+
6+
# To make sure it recognizes the WASI platform
7+
list(APPEND CMAKE_MODULE_PATH /usr/share/cmake/Modules)
8+
9+
set(CMAKE_SYSTEM_NAME WASI)
10+
set(CMAKE_SYSTEM_VERSION 1)
11+
set(CMAKE_SYSTEM_PROCESSOR wasm32)
12+
set(triple wasm32-wasi)
13+
14+
set(CMAKE_C_COMPILER /usr/bin/clang-15)
15+
set(CMAKE_CXX_COMPILER /usr/bin/clang++-15)
16+
set(CMAKE_AR /usr/bin/llvm-ar-15)
17+
set(CMAKE_RANLIB /usr/bin/llvm-ranlib-15)
18+
set(CMAKE_C_COMPILER_TARGET ${triple})
19+
set(CMAKE_CXX_COMPILER_TARGET ${triple})
20+
SET(CMAKE_SYSROOT /wasi-sysroot)
21+
22+
# Don't look in the sysroot for executables to run during the build
23+
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
24+
# Only look in the sysroot (not in the host paths) for the rest
25+
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
26+
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
27+
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

0 commit comments

Comments
 (0)