From 2b21d9c2b50e0ca9a2c11f59959a122d506f1315 Mon Sep 17 00:00:00 2001 From: Anuraag Agrawal Date: Fri, 16 Dec 2022 16:26:25 +0900 Subject: [PATCH] Add docker image that can be used to build projects with wasi-sdk sysroot --- .github/workflows/main.yml | 34 +++++++++++++++++++++++++++ .gitignore | 2 ++ sdk.Dockerfile | 46 +++++++++++++++++++++++++++++++++++++ sdk.Dockerfile.dockerignore | 1 + sdk.docker.cmake | 27 ++++++++++++++++++++++ 5 files changed, 110 insertions(+) create mode 100644 sdk.Dockerfile create mode 100644 sdk.Dockerfile.dockerignore create mode 100644 sdk.docker.cmake diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 296a7a4..e824379 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -7,6 +7,7 @@ on: branches: - main pull_request: + workflow_dispatch: jobs: build: @@ -93,6 +94,27 @@ jobs: - uses: actions/checkout@v1 with: submodules: true + + - uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - uses: docker/setup-qemu-action@v2 + - uses: docker/setup-buildx-action@v2 + + - uses: docker/metadata-action@v4 + id: meta + with: + images: ghcr.io/${{ github.repository }}/wasi-sdk + tags: | + type=schedule + type=ref,event=branch + type=ref,event=tag + type=ref,event=pr + type=sha + - name: Run docker_build script run: ./docker_build.sh - name: Upload artifacts @@ -101,3 +123,15 @@ jobs: # Upload the dist folder. Give it a name according to the OS it was built for. name: dist-ubuntu-xenial path: dist + + - name: Build and push wasi-sdk docker image + uses: docker/build-push-action@v3 + with: + context: . + file: sdk.Dockerfile + push: ${{ github.event_name != 'pull_request' }} + platforms: linux/amd64,linux/arm64 + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=gha + cache-to: type=gha,mode=max diff --git a/.gitignore b/.gitignore index 9d0b71a..e144103 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ build dist +.idea +.vscode \ No newline at end of file diff --git a/sdk.Dockerfile b/sdk.Dockerfile new file mode 100644 index 0000000..87a5818 --- /dev/null +++ b/sdk.Dockerfile @@ -0,0 +1,46 @@ +# Docker image with a build toolchain and environment variables set to use +# the wasi-sdk sysroot. The SDK distribution must have first been built, +# for example using docker_build.sh + +# Use ubuntu to use official repository with newer cmake packages +FROM ubuntu:22.04 + +ENV LLVM_VERSION 15 + +# Install build toolchain including clang, ld, make, autotools, ninja, and cmake +RUN apt-get update && \ + # Temporarily install to setup apt repositories + apt-get install -y curl gnupg && \ +\ + curl -sS https://apt.llvm.org/llvm-snapshot.gpg.key | gpg --dearmor > /etc/apt/trusted.gpg.d/llvm.gpg && \ + 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 && \ + 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 && \ +\ + curl -sS https://apt.kitware.com/keys/kitware-archive-latest.asc | gpg --dearmor > /etc/apt/trusted.gpg.d/kitware.gpg && \ + echo "deb [signed-by=/etc/apt/trusted.gpg.d/kitware.gpg] https://apt.kitware.com/ubuntu/ jammy main" >> /etc/apt/sources.list.d/kitware.list && \ + echo "deb-src [signed-by=/etc/apt/trusted.gpg.d/kitware.gpg] https://apt.kitware.com/ubuntu/ jammy main" >> /etc/apt/sources.list.d/kitware.list && \ +\ + apt-get update && \ + apt-get install -y clang-15 lld-15 cmake ninja-build make autoconf autogen automake libtool && \ + rm -rf /var/lib/apt/lists/* + +ADD dist/wasi-sysroot-*.*.tar.gz / +# The path to the rt directory contains the LLVM patch version which is not reflected in the LLVM apt repository +# or package. To make adding the RT robust to changing patch versions without needing to duplicate the folder +# content, we symlink after extracting using a bash glob to resolve the patch version +ADD dist/libclang_rt.builtins-wasm32-wasi-*.*.tar.gz /wasi-sysroot-clang_rt +RUN ln -s /wasi-sysroot-clang_rt/lib/wasi/ $(echo /usr/lib/llvm-${LLVM_VERSION}/lib/clang/${LLVM_VERSION}.*)/lib/wasi + +ADD sdk.docker.cmake /usr/share/cmake/wasi-sdk.cmake +ENV CMAKE_TOOLCHAIN_FILE /usr/share/cmake/wasi-sdk.cmake +ADD cmake/Platform/WASI.cmake /usr/share/cmake/Modules/Platform/WASI.cmake + +ENV CC clang-15 +ENV CXX clang++-15 +ENV LD wasm-ld-15 +ENV AR llvm-ar-15 +ENV RANLIB llvm-ranlib-15 + +ENV CFLAGS --target=wasm32-wasi --sysroot=/wasi-sysroot +ENV CXXFLAGS --target=wasm32-wasi --sysroot=/wasi-sysroot +ENV LDFLAGS --target=wasm32-wasi --sysroot=/wasi-sysroot diff --git a/sdk.Dockerfile.dockerignore b/sdk.Dockerfile.dockerignore new file mode 100644 index 0000000..586e3d7 --- /dev/null +++ b/sdk.Dockerfile.dockerignore @@ -0,0 +1 @@ +!dist \ No newline at end of file diff --git a/sdk.docker.cmake b/sdk.docker.cmake new file mode 100644 index 0000000..b87c948 --- /dev/null +++ b/sdk.docker.cmake @@ -0,0 +1,27 @@ +# Cmake toolchain description file for the wasi-sdk docker image + +# This is arbitrary, AFAIK, for now. +cmake_minimum_required(VERSION 3.4.0) + +# To make sure it recognizes the WASI platform +list(APPEND CMAKE_MODULE_PATH /usr/share/cmake/Modules) + +set(CMAKE_SYSTEM_NAME WASI) +set(CMAKE_SYSTEM_VERSION 1) +set(CMAKE_SYSTEM_PROCESSOR wasm32) +set(triple wasm32-wasi) + +set(CMAKE_C_COMPILER /usr/bin/clang-15) +set(CMAKE_CXX_COMPILER /usr/bin/clang++-15) +set(CMAKE_AR /usr/bin/llvm-ar-15) +set(CMAKE_RANLIB /usr/bin/llvm-ranlib-15) +set(CMAKE_C_COMPILER_TARGET ${triple}) +set(CMAKE_CXX_COMPILER_TARGET ${triple}) +SET(CMAKE_SYSROOT /wasi-sysroot) + +# Don't look in the sysroot for executables to run during the build +set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) +# Only look in the sysroot (not in the host paths) for the rest +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)