From c80964d8d7cf8ca2480d8332cd8022cb0600d421 Mon Sep 17 00:00:00 2001 From: Pat Hickey Date: Wed, 6 Mar 2019 11:18:21 -0800 Subject: [PATCH] initial commit largely based on yury's wasmception --- Makefile | 132 +++++++++++++++++++++++++++++++++++++++++++++++ package.sh | 15 ++++++ version.sh | 5 ++ wasi-sdk.control | 6 +++ wask-sdk.cmake | 33 ++++++++++++ 5 files changed, 191 insertions(+) create mode 100644 Makefile create mode 100755 package.sh create mode 100755 version.sh create mode 100644 wasi-sdk.control create mode 100644 wask-sdk.cmake diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f69aa31 --- /dev/null +++ b/Makefile @@ -0,0 +1,132 @@ +# Any copyright is dedicated to the Public Domain. +# http://creativecommons.org/publicdomain/zero/1.0/ + +ROOT_DIR=${CURDIR} +PREFIX?=/opt/wasi-sdk +CLANG_VERSION=8.0.0 + +VERSION=0.2 +DEBUG_PREFIX_MAP=-fdebug-prefix-map=$(ROOT_DIR)=wasmception://v$(VERSION) + +default: build + echo "Use -fdebug-prefix-map=$(ROOT_DIR)=wasmception://v$(VERSION)" + +clean: + rm -rf build $(PREFIX) + +build/llvm.BUILT: + mkdir -p build/llvm + cd build/llvm; cmake -G "Unix Makefiles" \ + -DCMAKE_BUILD_TYPE=MinSizeRel \ + -DCMAKE_INSTALL_PREFIX=$(PREFIX) \ + -DLLVM_TARGETS_TO_BUILD=WebAssembly \ + -DLLVM_DEFAULT_TARGET_TRIPLE=wasm32-unknown-wasi \ + -DLLVM_EXTERNAL_CLANG_SOURCE_DIR=$(ROOT_DIR)/src/llvm-project/clang \ + -DLLVM_EXTERNAL_LLD_SOURCE_DIR=$(ROOT_DIR)/src/llvm-project/lld \ + -DLLVM_ENABLE_PROJECTS="lld;clang" \ + -DDEFAULT_SYSROOT=$(PREFIX)/share/sysroot \ + $(ROOT_DIR)/src/llvm-project/llvm + cd build/llvm; $(MAKE) -j 8 \ + install-clang \ + install-lld \ + install-llc \ + install-llvm-ar \ + install-llvm-ranlib \ + install-llvm-dwarfdump \ + install-clang-headers \ + install-llvm-nm \ + install-llvm-size \ + llvm-config + touch build/llvm.BUILT + +build/reference-sysroot.BUILT: build/llvm.BUILT + make -C $(ROOT_DIR)/src/reference-sysroot \ + WASM_CC=$(PREFIX)/bin/clang \ + SYSROOT=$(PREFIX)/share/sysroot + touch build/reference-sysroot.BUILT + +build/compiler-rt.BUILT: build/llvm.BUILT + mkdir -p build/compiler-rt + cd build/compiler-rt; cmake -G "Unix Makefiles" \ + -DCMAKE_BUILD_TYPE=RelWithDebInfo \ + -DCMAKE_TOOLCHAIN_FILE=$(ROOT_DIR)/wasmi-sdk.cmake \ + -DCOMPILER_RT_BAREMETAL_BUILD=On \ + -DCOMPILER_RT_BUILD_XRAY=OFF \ + -DCOMPILER_RT_INCLUDE_TESTS=OFF \ + -DCOMPILER_RT_ENABLE_IOS=OFF \ + -DCOMPILER_RT_DEFAULT_TARGET_ONLY=On \ + -DWASM_SDK_PREFIX=$(PREFIX) \ + -DCMAKE_C_FLAGS="-O1 $(DEBUG_PREFIX_MAP)" \ + -DLLVM_CONFIG_PATH=$(ROOT_DIR)/build/llvm/bin/llvm-config \ + -DCOMPILER_RT_OS_DIR=wasi \ + -DCMAKE_INSTALL_PREFIX=$(PREFIX)/lib/clang/$(CLANG_VERSION)/ \ + -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON \ + $(ROOT_DIR)/src/llvm-project/compiler-rt/lib/builtins + cd build/compiler-rt; make -j 8 install + cp -R $(ROOT_DIR)/build/llvm/lib/clang $(PREFIX)/lib/ + touch build/compiler-rt.BUILT + +build/libcxx.BUILT: build/llvm.BUILT build/compiler-rt.BUILT build/reference-sysroot.BUILT + mkdir -p build/libcxx + cd build/libcxx; cmake -G "Unix Makefiles" \ + -DCMAKE_TOOLCHAIN_FILE=$(ROOT_DIR)/wasi-sdk.cmake \ + -DLLVM_CONFIG_PATH=$(ROOT_DIR)/build/llvm/bin/llvm-config \ + -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON \ + -DLIBCXX_HAS_PTHREAD_API:BOOL=ON \ + -DCMAKE_BUILD_TYPE=RelWithDebugInfo \ + -DLIBCXX_ENABLE_SHARED:BOOL=OFF \ + -DLIBCXX_ENABLE_EXPERIMENTAL_LIBRARY:BOOL=OFF \ + -DLIBCXX_ENABLE_EXCEPTIONS:BOOL=OFF \ + -DLIBCXX_CXX_ABI=libcxxabi \ + -DLIBCXX_CXX_ABI_INCLUDE_PATHS=$(ROOT_DIR)/src/llvm-project/libcxxabi/include \ + -DLIBCXX_HAS_MUSL_LIBC:BOOL=ON \ + -DLIBCXX_ABI_VERSION=2 \ + -DWASM_SDK_PREFIX=$(PREFIX) \ + -DCMAKE_C_FLAGS="$(DEBUG_PREFIX_MAP)" \ + -DCMAKE_CXX_FLAGS="$(DEBUG_PREFIX_MAP)" \ + --debug-trycompile \ + $(ROOT_DIR)/src/llvm-project/libcxx + cd build/libcxx; make -j 8 install + # libc++abi.a doesn't do a multiarch install, so fix it up. + mv $(PREFIX)/share/sysroot/lib/libc++.a $(PREFIX)/share/sysroot/lib/wasm32-wasi/ + touch build/libcxx.BUILT + +build/libcxxabi.BUILT: build/libcxx.BUILT build/llvm.BUILT + mkdir -p build/libcxxabi + cd build/libcxxabi; cmake -G "Unix Makefiles" \ + -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON \ + -DCMAKE_CXX_COMPILER_WORKS=ON \ + -DCMAKE_C_COMPILER_WORKS=ON \ + -DLIBCXXABI_ENABLE_EXCEPTIONS:BOOL=OFF \ + -DLIBCXXABI_ENABLE_SHARED:BOOL=OFF \ + -DLIBCXXABI_HAS_PTHREAD_API:BOOL=ON \ + -DCXX_SUPPORTS_CXX11=ON \ + -DLLVM_COMPILER_CHECKED=ON \ + -DCMAKE_BUILD_TYPE=RelWithDebugInfo \ + -DLIBCXXABI_LIBCXX_PATH=$(ROOT_DIR)/src/llvm-project/libcxx \ + -DLIBCXXABI_LIBCXX_INCLUDES=$(PREFIX)/share/sysroot/include/c++/v1 \ + -DLLVM_CONFIG_PATH=$(ROOT_DIR)/build/llvm/bin/llvm-config \ + -DCMAKE_TOOLCHAIN_FILE=$(ROOT_DIR)/wasi-sdk.cmake \ + -DWASM_SDK_PREFIX=$(PREFIX) \ + -DCMAKE_C_FLAGS="$(DEBUG_PREFIX_MAP)" \ + -DCMAKE_CXX_FLAGS="$(DEBUG_PREFIX_MAP)" \ + -DUNIX:BOOL=ON \ + --debug-trycompile \ + $(ROOT_DIR)/src/llvm-project/libcxxabi + cd build/libcxxabi; make -j 8 install + # libc++abi.a doesn't do a multiarch install, so fix it up. + mv $(PREFIX)/share/sysroot/lib/libc++abi.a $(PREFIX)/share/sysroot/lib/wasm32-wasi/ + touch build/libcxxabi.BUILT + +build: build/llvm.BUILT build/reference-sysroot.BUILT build/compiler-rt.BUILT build/libcxxabi.BUILT build/libcxx.BUILT + +strip: build/llvm.BUILT + cd $(PREFIX)/bin; strip clang-8 llc lld llvm-ar + +package: build/package.BUILT + +build/package.BUILT: build + ./package.sh + touch build/package.BUILT + +.PHONY: default clean build strip package diff --git a/package.sh b/package.sh new file mode 100755 index 0000000..a94c51b --- /dev/null +++ b/package.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env sh + +if [ -n "$1" ]; then + export VERSION="$1" +else + export VERSION=`./version.sh` +fi + +rm -rf pkg +mkdir -p pkg/opt +mkdir pkg/DEBIAN +sed -e s/VERSION/$VERSION/ wasi-sdk.control > pkg/DEBIAN/control +cp -R /opt/wasi-sdk pkg/opt/ +dpkg-deb -b pkg wasi-sdk_$VERSION\_amd64.deb +rm -rf pkg diff --git a/version.sh b/version.sh new file mode 100755 index 0000000..85825fc --- /dev/null +++ b/version.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +set -e +GIT_DESCR=$(git describe --long --candidates=999 --match='wasi-sdk-*' --dirty='+m' --abbrev=12) +GIT_PACKAGE_VERSION=$(echo $GIT_DESCR | perl -ne 'if(/^wasi-sdk-(\d+)-(\d+)-g([0-9a-f]{7,12})([+]m)?$/) { print "$1.$2g$3$4"; exit } else { print "could not parse git description"; exit 1 }';) +echo $GIT_PACKAGE_VERSION diff --git a/wasi-sdk.control b/wasi-sdk.control new file mode 100644 index 0000000..0586f37 --- /dev/null +++ b/wasi-sdk.control @@ -0,0 +1,6 @@ +Package: wasi-sdk +Version: VERSION +Architecture: amd64 +Priority: optional +Description: Clang toolchain with wasm32-wasi default target, and the wasi sysroot +Maintainer: Pat Hickey diff --git a/wask-sdk.cmake b/wask-sdk.cmake new file mode 100644 index 0000000..6ec193b --- /dev/null +++ b/wask-sdk.cmake @@ -0,0 +1,33 @@ +# Cmake toolchain description file for the Makefile + +# This is arbitrary, AFAIK, for now. +cmake_minimum_required(VERSION 3.4.0) + +set(CMAKE_SYSTEM_NAME Wasm) +set(CMAKE_SYSTEM_VERSION 1) +set(CMAKE_SYSTEM_PROCESSOR wasm32) +set(triple wasm32-unknown-wasi) + +set(CMAKE_C_COMPILER ${WASM_SDK_PREFIX}/bin/clang) +set(CMAKE_CXX_COMPILER ${WASM_SDK_PREFIX}/bin/clang++) +set(CMAKE_AR ${WASM_SDK_PREFIX}/bin/llvm-ar CACHE STRING "wasi-sdk build") +set(CMAKE_RANLIB ${WASM_SDK_PREFIX}/bin/llvm-ranlib CACHE STRING "wasi-sdk build") +set(CMAKE_C_COMPILER_TARGET ${triple} CACHE STRING "wasi-sdk build") +set(CMAKE_CXX_COMPILER_TARGET ${triple} CACHE STRING "wasi-sdk build") +set(CMAKE_C_FLAGS "-v" CACHE STRING "wasi-sdk build") +set(CMAKE_CXX_FLAGS "-v -std=c++11" CACHE STRING "wasi-sdk build") +set(CMAKE_EXE_LINKER_FLAGS "-Wl,--no-threads" CACHE STRING "wasi-sdk build") + +set(CMAKE_SYSROOT ${WASM_SDK_PREFIX}/share/sysroot CACHE STRING "wasi-sdk build") +set(CMAKE_STAGING_PREFIX ${WASM_SDK_PREFIX}/share/sysroot CACHE STRING "wasi-sdk build") + +# 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) + +# Some other hacks +set(CMAKE_C_COMPILER_WORKS ON) +set(CMAKE_CXX_COMPILER_WORKS ON)