From d9fc3acfaa96caebbe585389af79d5e7c0959c90 Mon Sep 17 00:00:00 2001 From: Carlos Date: Tue, 12 Mar 2024 15:43:29 +0000 Subject: [PATCH] LLVM 18 (#123) * gh: bump code version * faasmtools: bump llvm version * llvm: changes for latest wasi-libc and wasi-sdk * llvm: install libclang_rt.builitins-wasm32.a * cmake: change system name from Wasm to WASI to align with wasi-sdk * faasmtools: enable linker feature sign extension as it is the default in llvm>=16 * libs: install in /lib/wasm32-wasi and copy imports appropriately * docker: enable some features * libs: install libraries and headers in the right location * llvm: fix libclanv_rt.builitins installation * mpi: more fixes to the cmake file * wasi: attempt at having two different targets for threads and non-threads * func: comment out temporarily disabled functions * func: move threaded functions to separate top-level directory * tasks: configurable compilation for different targets * threads: support for pthread apis * cpython: changes to make cpython build work * llvm: add c compiler target flag for compiler-rt build * libs: fix use of (auto)conf with different sysroots * more fixes * faasmtools: change default target when building a threaded application * llvm: bump to version 18 rc2 to fix the frontend bug with openmp * llvm: no harm in using clang-18 to build llvm-18 * llvm: use clang-17 for native compilation * nits: fix python formatting * nits: fix cpp formatting * nit: run python formatting * func: build all threads func * gha: skip code formatting with clang-format-17 * clang-format: fix formatting after llvm bump * nits: clang-format passing with llvm 17 * tasks(libs): build threads/non-threads in different directories * docker: fix strange libfaasm overwrite * funcs: set the threads target when appropriate * nits: self-review * libffi: fix build by passing --host flag to configure --- .clang-format | 12 + .env | 4 +- .github/workflows/release.yml | 10 +- .github/workflows/tests.yml | 20 +- LLVM.makefile | 232 ++++++++++-------- VERSION | 2 +- WasiToolchain.cmake | 37 ++- docker/cpp-sysroot.dockerfile | 20 +- docker/llvm.dockerfile | 18 +- docs/{upgrade-llvm.md => llvm.md} | 12 +- faasmtools/build.py | 162 +++++++----- faasmtools/compile_util.py | 8 +- faasmtools/env.py | 6 +- func/CMakeLists.txt | 7 +- func/demo/CMakeLists.txt | 9 +- func/dynlink/CMakeLists.txt | 2 +- func/dynlink/libA/CMakeLists.txt | 2 +- func/dynlink/libB/CMakeLists.txt | 2 +- func/errors/CMakeLists.txt | 2 +- func/mpi/CMakeLists.txt | 2 +- func/omp/CMakeLists.txt | 9 +- func/omp/complex_reduce.cpp | 7 +- func/omp/custom_reduce.cpp | 4 +- func/omp/default_shared.cpp | 6 +- func/omp/inspect_reduction.cpp | 5 +- func/omp/mem_stress.cpp | 6 +- func/omp/pi_faasm.cpp | 5 +- func/omp/pi_native.cpp | 5 +- func/omp/reduction_integral.cpp | 3 +- func/omp/repeated_reduce.cpp | 5 +- func/omp/simple_reduce.cpp | 3 +- func/omp/single_thread_reduce.cpp | 7 +- func/threads/CMakeLists.txt | 20 ++ func/{demo => threads}/threads_check.cpp | 0 func/{demo => threads}/threads_dist.cpp | 0 func/{demo => threads}/threads_local.cpp | 0 func/{demo => threads}/threads_memory.cpp | 0 libemscripten/CMakeLists.txt | 27 +- ...scripten.imports => libemscripten.imports} | 0 libfaasm/CMakeLists.txt | 85 +++---- libfaasm/{faasm.imports => libfaasm.imports} | 0 libfaasmp/CMakeLists.txt | 54 ++-- libfaasmp/README.md | 6 +- .../{faasmp.imports => libfaasmp.imports} | 0 libfaasmpi/CMakeLists.txt | 72 +++--- .../{faasmpi.imports => libfaasmpi.imports} | 0 sysroot_extras/libc.imports | 5 + tasks/clapack.py | 22 +- tasks/docker.py | 4 +- tasks/format_code.py | 4 +- tasks/func.py | 27 +- tasks/lib.py | 23 +- tasks/libemscripten.py | 10 +- tasks/libfaasm.py | 6 +- tasks/libfaasmp.py | 4 +- tasks/libfake.py | 12 +- tasks/libffi.py | 33 +-- tasks/llvm.py | 23 +- tasks/zlib.py | 49 ++-- tests/test_input.cpp | 5 +- third-party/wasi-libc | 2 +- 61 files changed, 649 insertions(+), 478 deletions(-) rename docs/{upgrade-llvm.md => llvm.md} (76%) create mode 100644 func/threads/CMakeLists.txt rename func/{demo => threads}/threads_check.cpp (100%) rename func/{demo => threads}/threads_dist.cpp (100%) rename func/{demo => threads}/threads_local.cpp (100%) rename func/{demo => threads}/threads_memory.cpp (100%) rename libemscripten/{emscripten.imports => libemscripten.imports} (100%) rename libfaasm/{faasm.imports => libfaasm.imports} (100%) rename libfaasmp/{faasmp.imports => libfaasmp.imports} (100%) rename libfaasmpi/{faasmpi.imports => libfaasmpi.imports} (100%) diff --git a/.clang-format b/.clang-format index 0c2dc983..7e8982ac 100644 --- a/.clang-format +++ b/.clang-format @@ -10,4 +10,16 @@ AlwaysBreakAfterReturnType: None IndentWidth: 4 DerivePointerAlignment: false + +# Precise control over braces alignment +BreakBeforeBraces: Custom +BraceWrapping: + AfterClass: true + AfterEnum: true + AfterExternBlock: true + AfterFunction: true + AfterStruct: true + AfterUnion: true + SplitEmptyFunction: false + SplitEmptyRecord: false --- diff --git a/.env b/.env index 932a60f0..b0dcb04a 100644 --- a/.env +++ b/.env @@ -1,3 +1,3 @@ -SYSROOT_VERSION=0.3.1 -SYSROOT_CLI_IMAGE=faasm.azurecr.io/cpp-sysroot:0.3.1 +SYSROOT_VERSION=0.4.0 +SYSROOT_CLI_IMAGE=faasm.azurecr.io/cpp-sysroot:0.4.0 COMPOSE_PROJECT_NAME=cpp-dev diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 6a10b517..757ea4b9 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -12,7 +12,7 @@ jobs: - name: "Prune docker" run: docker system prune -f --all - name: "Get the code" - uses: actions/checkout@v3 + uses: actions/checkout@v4 with: submodules: recursive - name: "Get tag version" @@ -20,18 +20,18 @@ jobs: - name: "Print tag version" run: echo ${{ env.TAG_VERSION }} - name: "Set up QEMU" - uses: docker/setup-qemu-action@v2 + uses: docker/setup-qemu-action@v3 - name: "Set up Docker Buildx" - uses: docker/setup-buildx-action@v2 + uses: docker/setup-buildx-action@v3 - name: "Log in to DockerHub" - uses: docker/login-action@v2 + uses: docker/login-action@v3 with: registry: faasm.azurecr.io username: ${{ secrets.ACR_SERVICE_PRINCIPAL_ID }} password: ${{ secrets.ACR_SERVICE_PRINCIPAL_PASSWORD }} - name: "Build and push cpp-sysroot container" id: docker_build - uses: docker/build-push-action@v3 + uses: docker/build-push-action@v5.1.0 with: push: true file: docker/cpp-sysroot.dockerfile diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 1d934992..e69f8300 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -7,26 +7,24 @@ on: branches: [main] types: [opened, synchronize, reopened, ready_for_review] -jobs: - # Cancel previous running actions for the same PR - cancel_previous: - runs-on: ubuntu-latest - steps: - - name: Cancel Workflow Action - uses: styfle/cancel-workflow-action@0.11.0 +# Cancel previous running actions for the same PR +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: ${{ github.ref != 'refs/heads/main' }} +jobs: checks: if: github.event.pull_request.draft == false runs-on: ubuntu-latest container: - image: faasm.azurecr.io/cpp-sysroot:0.3.1 + image: faasm.azurecr.io/cpp-sysroot:0.4.0 credentials: username: ${{ secrets.ACR_SERVICE_PRINCIPAL_ID }} password: ${{ secrets.ACR_SERVICE_PRINCIPAL_PASSWORD }} steps: # --- Update code --- - name: "Checkout code" - uses: actions/checkout@v3 + uses: actions/checkout@v4 with: submodules: true # See actions/checkout#766 @@ -35,6 +33,8 @@ jobs: # --- Build libraries to wasm --- - name: "Build libfaasm" run: ./bin/inv_wrapper.sh libfaasm + - name: "Build libfaasm with threads support" + run: ./bin/inv_wrapper.sh libfaasm --threads - name: "Build libfaasmp" run: ./bin/inv_wrapper.sh libfaasmp - name: "Build libfaasmpi" @@ -45,6 +45,8 @@ jobs: run: ./bin/inv_wrapper.sh libfake - name: "Build libemscripten" run: ./bin/inv_wrapper.sh libemscripten + - name: "Build libemscripten with threads support" + run: ./bin/inv_wrapper.sh libemscripten --threads - name: "Build zlib" run: ./bin/inv_wrapper.sh zlib # --- Build functions to wasm --- diff --git a/LLVM.makefile b/LLVM.makefile index 42a8597d..c61b5dc8 100644 --- a/LLVM.makefile +++ b/LLVM.makefile @@ -1,13 +1,11 @@ # Project directories LLVM_PROJ_DIR=${FAASM_LLVM_DIR} -TOOLCHAIN_DIR=${FAASM_CPP_PROJ_ROOT} -TOOLCHAIN_FILE=$(TOOLCHAIN_DIR)/WasiToolchain.cmake - # Install dirs FAASM_LOCAL_DIR=${FAASM_LOCAL_DIR_ENV} -PREFIX=$(FAASM_LOCAL_DIR)/toolchain FAASM_SYSROOT=$(FAASM_LOCAL_DIR)/llvm-sysroot +FAASM_TOOLCHAIN_DIR=$(FAASM_LOCAL_DIR)/toolchain +FAASM_TOOLCHAIN_FILE=$(FAASM_TOOLCHAIN_DIR)/tools/WasiToolchain.cmake CLANG_VERSION=${FAASM_LLVM_VERSION} CLANG_VERSION_MAJOR := $(shell echo $(CLANG_VERSION) | cut -f1 -d.) @@ -27,7 +25,7 @@ default: build .PHONY: clean-libc clean-libc: - rm -rf $(BUILD_DIR)/libc.BUILT $(WASI_LIBC_DIR)/build + rm -rf $(BUILD_DIR)/wasi-libc.BUILT $(WASI_LIBC_DIR)/build .PHONY: very-clean-libc very-clean-libc: clean-libc @@ -36,58 +34,86 @@ very-clean-libc: clean-libc .PHONY: clean-libs clean-libs: clean-libc + # compiler-rt clean rm -rf $(BUILD_DIR)/compiler-rt $(BUILD_DIR)/compiler-rt.BUILT + # libcxx clean rm -rf $(BUILD_DIR)/libcxx $(BUILD_DIR)/libcxx.BUILT - rm -rf $(BUILD_DIR)/libcxxabi $(BUILD_DIR)/libcxxabi.BUILT + rm -rf $(FAASM_SYSROOT)/include/wasm32-wasi/c++ + rm -rf $(FAASM_SYSROOT)/include/wasm32-wasi-threads/c++ .PHONY: clean-all clean-all: - rm -rf $(BUILD_DIR) $(PREFIX) + rm -rf $(BUILD_DIR) $(FAASM_TOOLCHAIN_DIR)/bin $(FAASM_TOOLCHAIN_DIR)/libs $(FAASM_SYSROOT) $(BUILD_DIR)/llvm.BUILT: mkdir -p $(BUILD_DIR)/llvm cd $(BUILD_DIR)/llvm; cmake -G Ninja \ - -DCMAKE_C_COMPILER=/usr/bin/clang-$(CLANG_VERSION_MAJOR) \ - -DCMAKE_CXX_COMPILER=/usr/bin/clang++-$(CLANG_VERSION_MAJOR) \ -DCMAKE_BUILD_TYPE=MinSizeRel \ - -DCMAKE_INSTALL_PREFIX=$(PREFIX) \ + -DLLVM_ENABLE_TERMINFO=OFF \ + -DLLVM_ENABLE_ZLIB=OFF \ + -DLLVM_ENABLE_ZSTD=OFF \ + -DLLVM_STATIC_LINK_CXX_STDLIB=ON \ + -DLLVM_HAVE_LIBXAR=OFF \ + -DCMAKE_INSTALL_PREFIX=${FAASM_TOOLCHAIN_DIR} \ + -DLLVM_INCLUDE_TESTS=OFF \ + -DLLVM_INCLUDE_UTILS=OFF \ + -DLLVM_INCLUDE_BENCHMARKS=OFF \ + -DLLVM_INCLUDE_EXAMPLES=OFF \ -DLLVM_TARGETS_TO_BUILD=WebAssembly \ -DLLVM_DEFAULT_TARGET_TRIPLE=wasm32-wasi \ - -DLLVM_EXTERNAL_CLANG_SOURCE_DIR=$(LLVM_PROJ_DIR)/clang \ - -DLLVM_EXTERNAL_OPENMP_SOURCE_DIR=$(LLVM_PROJ_DIR)/openmp \ - -DLLVM_EXTERNAL_LLD_SOURCE_DIR=$(LLVM_PROJ_DIR)/lld \ -DLLVM_ENABLE_PROJECTS="lld;clang;openmp" \ -DDEFAULT_SYSROOT=$(FAASM_SYSROOT) \ + -DLLVM_INSTALL_BINUTILS_SYMLINKS=TRUE \ + -DLLVM_ENABLE_LIBXML2=OFF \ $(LLVM_PROJ_DIR)/llvm ninja -v -C $(BUILD_DIR)/llvm \ install-clang \ install-clang-resource-headers \ install-lld \ - install-llc \ - install-llvm-ar \ + install-llvm-mc \ install-llvm-ranlib \ + install-llvm-strip \ install-llvm-dwarfdump \ - install-llvm-nm \ - install-llvm-size \ - install-llvm-config + install-llvm-symbolizer \ + install-clang-resource-headers \ + install-ar \ + install-ranlib \ + install-strip \ + install-nm \ + install-size \ + install-strings \ + install-objdump \ + install-objcopy \ + install-c++filt \ + llvm-config touch $(BUILD_DIR)/llvm.BUILT # WASI libc -$(BUILD_DIR)/libc.BUILT: $(BUILD_DIR)/llvm.BUILT - mkdir -p $(WASI_LIBC_DIR)/build - cd $(WASI_LIBC_DIR); $(MAKE) \ - THREAD_MODEL=faasm \ - CC=$(PREFIX)/bin/clang \ - AR=$(PREFIX)/bin/llvm-ar \ - NM=$(PREFIX)/bin/llvm-nm \ - SYSROOT=$(FAASM_SYSROOT) - touch $(BUILD_DIR)/libc.BUILT - -$(BUILD_DIR)/compiler-rt.BUILT: $(BUILD_DIR)/libc.BUILT +$(BUILD_DIR)/wasi-libc.BUILT: $(BUILD_DIR)/compiler-rt.BUILT + mkdir -p $(WASI_LIBC_DIR)/build/wasm32-wasi + $(MAKE) -C ${WASI_LIBC_DIR} \ + CC=$(FAASM_TOOLCHAIN_DIR)/bin/clang \ + AR=$(FAASM_TOOLCHAIN_DIR)/bin/llvm-ar \ + NM=$(FAASM_TOOLCHAIN_DIR)/bin/llvm-nm \ + SYSROOT=$(FAASM_SYSROOT) \ + default libc_so + $(MAKE) -C ${WASI_LIBC_DIR} \ + CC=$(FAASM_TOOLCHAIN_DIR)/bin/clang \ + AR=$(FAASM_TOOLCHAIN_DIR)/bin/llvm-ar \ + NM=$(FAASM_TOOLCHAIN_DIR)/bin/llvm-nm \ + SYSROOT=$(FAASM_SYSROOT) \ + THREAD_MODEL=posix + touch $(BUILD_DIR)/wasi-libc.BUILT + +$(BUILD_DIR)/compiler-rt.BUILT: $(BUILD_DIR)/llvm.BUILT mkdir -p $(BUILD_DIR)/compiler-rt cd $(BUILD_DIR)/compiler-rt; cmake -G Ninja \ + -DCMAKE_SYSROOT=${FAASM_SYSROOT} \ + -DCMAKE_C_COMPILER_WORKS=ON \ + -DCMAKE_C_COMPILER_TARGET=wasm32-wasi \ + -DCMAKE_CXX_COMPILER_WORKS=ON \ -DCMAKE_BUILD_TYPE=RelWithDebInfo \ - -DCMAKE_TOOLCHAIN_FILE=$(TOOLCHAIN_FILE) \ + -DCMAKE_TOOLCHAIN_FILE=${FAASM_TOOLCHAIN_FILE} \ -DCOMPILER_RT_BAREMETAL_BUILD=ON \ -DCOMPILER_RT_BUILD_XRAY=OFF \ -DCOMPILER_RT_INCLUDE_TESTS=OFF \ @@ -95,90 +121,104 @@ $(BUILD_DIR)/compiler-rt.BUILT: $(BUILD_DIR)/libc.BUILT -DCOMPILER_RT_ENABLE_IOS=OFF \ -DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON \ -DLLVM_CONFIG_PATH=$(LLVM_CONFIG) \ - -DCMAKE_INSTALL_PREFIX=$(PREFIX)/lib/clang/$(CLANG_VERSION)/ \ + -DCMAKE_INSTALL_PREFIX=$(FAASM_TOOLCHAIN_DIR)/lib/clang/$(CLANG_VERSION)/ \ -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON \ -DCOMPILER_RT_OS_DIR=wasi \ $(LLVM_PROJ_DIR)/compiler-rt/lib/builtins ninja -v -C $(BUILD_DIR)/compiler-rt install - cp -R $(BUILD_DIR)/llvm/lib/clang $(PREFIX)/lib/ + # Install clang-provided headers + cp -R $(BUILD_DIR)/llvm/lib/clang $(FAASM_TOOLCHAIN_DIR)/lib/ + # Install libclang_rt.builtins-wasm32.a + mkdir -p $(FAASM_TOOLCHAIN_DIR)/lib/clang/$(CLANG_VERSION_MAJOR)/lib/wasi + cp \ + $(BUILD_DIR)/compiler-rt/lib/wasi/libclang_rt.builtins-wasm32.a \ + $(FAASM_TOOLCHAIN_DIR)/lib/clang/$(CLANG_VERSION_MAJOR)/lib/wasi/libclang_rt.builtins-wasm32.a touch $(BUILD_DIR)/compiler-rt.BUILT -$(BUILD_DIR)/libcxx.BUILT: $(BUILD_DIR)/compiler-rt.BUILT +# Flags for libcxx and libcxxabi. +# $(1): pthreads ON or OFF +# $(2): shared libraries ON or OFF +LIBCXX_CMAKE_FLAGS = \ + -DCMAKE_C_COMPILER_WORKS=ON \ + -DCMAKE_CXX_COMPILER_WORKS=ON \ + -DCMAKE_AR=$(FAASM_TOOLCHAIN_DIR)/bin/ar \ + -DCMAKE_TOOLCHAIN_FILE=$(FAASM_TOOLCHAIN_FILE) \ + -DCMAKE_STAGING_PREFIX=$(FAASM_SYSROOT) \ + -DCMAKE_POSITION_INDEPENDENT_CODE=$(2) \ + -DLLVM_CONFIG_PATH=$(LLVM_CONFIG) \ + -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON \ + -DCXX_SUPPORTS_CXX11=ON \ + -DLIBCXX_ENABLE_THREADS:BOOL=$(1) \ + -DLIBCXX_HAS_PTHREAD_API:BOOL=$(1) \ + -DLIBCXX_HAS_EXTERNAL_THREAD_API:BOOL=OFF \ + -DLIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY:BOOL=OFF \ + -DLIBCXX_HAS_WIN32_THREAD_API:BOOL=OFF \ + -DLLVM_COMPILER_CHECKED=ON \ + -DCMAKE_BUILD_TYPE=RelWithDebugInfo \ + -DLIBCXX_ENABLE_SHARED:BOOL=$(2) \ + -DLIBCXX_ENABLE_EXPERIMENTAL_LIBRARY:BOOL=OFF \ + -DLIBCXX_ENABLE_EXCEPTIONS:BOOL=OFF \ + -DLIBCXX_ENABLE_FILESYSTEM:BOOL=ON \ + -DLIBCXX_ENABLE_ABI_LINKER_SCRIPT:BOOL=OFF \ + -DLIBCXX_USE_COMPILER_RT=ON \ + -DLIBCXX_CXX_ABI=libcxxabi \ + -DLIBCXX_CXX_ABI_INCLUDE_PATHS=$(LLVM_PROJ_DIR)/libcxxabi/include \ + -DLIBCXX_HAS_MUSL_LIBC:BOOL=ON \ + -DLIBCXX_ABI_VERSION=2 \ + -DLIBCXXABI_ENABLE_EXCEPTIONS:BOOL=OFF \ + -DLIBCXXABI_ENABLE_SHARED:BOOL=$(2) \ + -DLIBCXXABI_SILENT_TERMINATE:BOOL=ON \ + -DLIBCXXABI_ENABLE_THREADS:BOOL=$(1) \ + -DLIBCXXABI_HAS_PTHREAD_API:BOOL=$(1) \ + -DLIBCXXABI_HAS_EXTERNAL_THREAD_API:BOOL=OFF \ + -DLIBCXXABI_BUILD_EXTERNAL_THREAD_LIBRARY:BOOL=OFF \ + -DLIBCXXABI_HAS_WIN32_THREAD_API:BOOL=OFF \ + -DLIBCXXABI_USE_LLVM_UNWINDER=OFF \ + -DLIBCXX_ENABLE_PIC:BOOL=$(2) \ + -DUNIX:BOOL=ON \ + --debug-trycompile + +$(BUILD_DIR)/libcxx.BUILT: $(BUILD_DIR)/llvm.BUILT ${BUILD_DIR}/wasi-libc.BUILT + # Build different libcxx targets mkdir -p $(BUILD_DIR)/libcxx - cd $(BUILD_DIR)/libcxx; cmake -G Ninja \ - -DCMAKE_TOOLCHAIN_FILE=$(TOOLCHAIN_FILE) \ - -DCMAKE_BUILD_TYPE=RelWithDebugInfo \ - -DCMAKE_CXX_COMPILER_WORKS=ON \ - -DCMAKE_C_COMPILER_WORKS=ON \ - -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON \ - -DCMAKE_INSTALL_PREFIX=$(FAASM_SYSROOT) \ - -DLLVM_COMPILER_CHECKED=ON \ - -DLLVM_CONFIG_PATH=$(LLVM_CONFIG) \ - -DLIBCXX_HAVE_CXX_ATOMICS_WITHOUT_LIB=ON \ - -DLIBCXX_USE_COMPILER_RT=ON \ - -DLIBCXX_ENABLE_THREADS:BOOL=ON \ - -DLIBCXX_HAS_PTHREAD_API:BOOL=ON \ - -DLIBCXX_HAS_EXTERNAL_THREAD_API:BOOL=OFF \ - -DLIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY:BOOL=OFF \ - -DLIBCXX_HAS_WIN32_THREAD_API:BOOL=OFF \ - -DLIBCXX_ENABLE_SHARED:BOOL=OFF \ - -DLIBCXX_ENABLE_PIC:BOOL=OFF \ - -DLIBCXX_ENABLE_EXPERIMENTAL_LIBRARY:BOOL=OFF \ - -DLIBCXX_ENABLE_EXCEPTIONS:BOOL=OFF \ - -DLIBCXX_ENABLE_FILESYSTEM:BOOL=OFF \ - -DLIBCXX_CXX_ABI=libcxxabi \ - -DLIBCXX_CXX_ABI_INCLUDE_PATHS=$(LLVM_PROJ_DIR)/libcxxabi/include \ - -DLIBCXX_HAS_MUSL_LIBC:BOOL=ON \ - -DLIBCXX_ABI_VERSION=2 \ + cd $(BUILD_DIR)/libcxx && cmake -G Ninja $(call LIBCXX_CMAKE_FLAGS,OFF,ON) \ + -DCMAKE_SYSROOT=${FAASM_SYSROOT} \ + -DCMAKE_C_FLAGS="--target=wasm32-wasi" \ + -DCMAKE_CXX_FLAGS="--target=wasm32-wasi" \ -DLIBCXX_LIBDIR_SUFFIX=/wasm32-wasi \ - --debug-trycompile \ - $(LLVM_PROJ_DIR)/libcxx + -DLIBCXXABI_LIBDIR_SUFFIX=/wasm32-wasi \ + -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi" \ + $(LLVM_PROJ_DIR)/runtimes + mkdir -p $(BUILD_DIR)/libcxx-threads + cd $(BUILD_DIR)/libcxx-threads && cmake -G Ninja $(call LIBCXX_CMAKE_FLAGS,ON,OFF) \ + -DCMAKE_SYSROOT=${FAASM_SYSROOT} \ + -DCMAKE_C_FLAGS="-pthread --target=wasm32-wasi-threads" \ + -DCMAKE_CXX_FLAGS="-pthread --target=wasm32-wasi-threads" \ + -DLIBCXX_LIBDIR_SUFFIX=/wasm32-wasi-threads \ + -DLIBCXXABI_LIBDIR_SUFFIX=/wasm32-wasi-threads \ + -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi" \ + $(LLVM_PROJ_DIR)/runtimes + # Do the install ninja -v -C $(BUILD_DIR)/libcxx install + mv $(FAASM_WASM_SYSROOT)/include/c++ $(FAASM_WASM_SYSROOT)/include/wasm32-wasi/ + ninja -v -C $(BUILD_DIR)/libcxx-threads install + mv $(FAASM_WASM_SYSROOT)/include/c++ $(FAASM_WASM_SYSROOT)/include/wasm32-wasi-threads/ + # As of this writing, `clang++` will ignore the above include dirs unless this one also exists: + mkdir -p $(FAASM_WASM_SYSROOT)/include/c++/v1 touch $(BUILD_DIR)/libcxx.BUILT -$(BUILD_DIR)/libcxxabi.BUILT: $(BUILD_DIR)/libcxx.BUILT - mkdir -p $(BUILD_DIR)/libcxxabi - cd $(BUILD_DIR)/libcxxabi; cmake -G Ninja \ - -DCMAKE_BUILD_TYPE=RelWithDebugInfo \ - -DCMAKE_C_FLAGS="-I$(FAASM_SYSROOT)/include" \ - -DCMAKE_CXX_FLAGS="-I$(FAASM_SYSROOT)/include/c++/v1" \ - -DCMAKE_TOOLCHAIN_FILE=$(TOOLCHAIN_FILE) \ - -DLLVM_CONFIG_PATH=$(LLVM_CONFIG) \ - -DCMAKE_CXX_COMPILER_WORKS=ON \ - -DCMAKE_C_COMPILER_WORKS=ON \ - -DCMAKE_INSTALL_PREFIX=$(FAASM_SYSROOT) \ - -DLLVM_COMPILER_CHECKED=ON \ - -DLIBCXXABI_USE_COMPILER_RT=ON \ - -DLIBCXXABI_ENABLE_EXCEPTIONS:BOOL=OFF \ - -DLIBCXXABI_ENABLE_SHARED:BOOL=OFF \ - -DLIBCXXABI_SILENT_TERMINATE:BOOL=ON \ - -DLIBCXXABI_ENABLE_THREADS:BOOL=ON \ - -DLIBCXXABI_HAS_PTHREAD_API:BOOL=ON \ - -DLIBCXXABI_HAS_EXTERNAL_THREAD_API:BOOL=OFF \ - -DLIBCXXABI_BUILD_EXTERNAL_THREAD_LIBRARY:BOOL=OFF \ - -DLIBCXXABI_HAS_WIN32_THREAD_API:BOOL=OFF \ - -DLIBCXXABI_ENABLE_PIC:BOOL=OFF \ - -DLIBCXXABI_LIBCXX_PATH=$(LLVM_PROJ_DIR)/libcxx \ - -DLIBCXXABI_LIBCXX_INCLUDES=$(FAASM_SYSROOT)/include/c++/v1 \ - -DUNIX:BOOL=ON \ - -DLIBCXXABI_LIBDIR_SUFFIX=/wasm32-wasi \ - --debug-trycompile \ - $(LLVM_PROJ_DIR)/libcxxabi - ninja -v -C $(BUILD_DIR)/libcxxabi install - touch $(BUILD_DIR)/libcxxabi.BUILT .PHONY: extras -extras: $(BUILD_DIR)/libcxxabi.BUILT - cp $(TOOLCHAIN_DIR)/sysroot_extras/* $(FAASM_SYSROOT)/lib/wasm32-wasi/ +extras: $(BUILD_DIR)/libcxx.BUILT + cp $(FAASM_CPP_PROJ_ROOT)/sysroot_extras/* $(FAASM_SYSROOT)/lib/wasm32-wasi/ + cp $(FAASM_CPP_PROJ_ROOT)/sysroot_extras/* $(FAASM_SYSROOT)/lib/wasm32-wasi-threads/ llvm: $(BUILD_DIR)/llvm.BUILT -libc: $(BUILD_DIR)/libc.BUILT +libc: $(BUILD_DIR)/wasi-libc.BUILT libcxx: $(BUILD_DIR)/libcxx.BUILT -libcxxabi: $(BUILD_DIR)/libcxxabi.BUILT - compiler-rt: $(BUILD_DIR)/compiler-rt.BUILT -build: $(BUILD_DIR)/llvm.BUILT $(BUILD_DIR)/libc.BUILT $(BUILD_DIR)/compiler-rt.BUILT $(BUILD_DIR)/libcxxabi.BUILT $(BUILD_DIR)/libcxx.BUILT extras +build: $(BUILD_DIR)/llvm.BUILT $(BUILD_DIR)/wasi-libc.BUILT $(BUILD_DIR)/compiler-rt.BUILT $(BUILD_DIR)/libcxx.BUILT extras diff --git a/VERSION b/VERSION index 9e11b32f..1d0ba9ea 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.3.1 +0.4.0 diff --git a/WasiToolchain.cmake b/WasiToolchain.cmake index d2db1f5f..d265b69f 100644 --- a/WasiToolchain.cmake +++ b/WasiToolchain.cmake @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.4.0) +cmake_minimum_required(VERSION 3.5.0) # ----------------------------------------- # A useful reference for this file is the wasi-sdk equivalent: @@ -11,15 +11,16 @@ cmake_minimum_required(VERSION 3.4.0) set(INSTALL_DIR $ENV{FAASM_WASM_INSTALL_DIR}) set(FAASM_SYSROOT $ENV{FAASM_WASM_SYSROOT}) -set(EXE_SUFFIX "") - set(UNIX 1) # Note that system name and processor here are crucial # Setting system name automatically switches on cross-compiling -set(CMAKE_SYSTEM_NAME Wasm) +set(CMAKE_SYSTEM_NAME WASI) set(CMAKE_SYSTEM_VERSION 1) set(CMAKE_SYSTEM_PROCESSOR wasm32) +set(WASM_TRIPLE $ENV{FAASM_WASM_TRIPLE}) + +set(WASI_HOST_EXE_SUFFIX "") if(FAASM_BUILD_SHARED) message(STATUS "Faasm building SHARED libraries") @@ -29,19 +30,20 @@ if(FAASM_BUILD_SHARED) else() message(STATUS "Faasm building STATIC libraries") - set(WASM_TRIPLE $ENV{FAASM_WASM_HOST_STATIC}) + # set(WASM_TRIPLE $ENV{WASM_TRIPLE}) endif() -set(CMAKE_C_COMPILER_TARGET ${WASM_TRIPLE} CACHE STRING "faasm build") -set(CMAKE_CXX_COMPILER_TARGET ${WASM_TRIPLE} CACHE STRING "faasm build") -message(STATUS "Faasm building target ${CMAKE_CXX_COMPILER_TARGET}") - # Specify LLVM toolchain set(CMAKE_C_COMPILER $ENV{FAASM_WASM_CC}) set(CMAKE_CXX_COMPILER $ENV{FAASM_WASM_CXX}) +set(CMAKE_ASM_COMPILER $ENV{FAASM_WASM_CC}) set(CMAKE_AR $ENV{FAASM_WASM_AR} CACHE STRING "faasm build") set(CMAKE_NM $ENV{FAASM_WASM_NM} CACHE STRING "faasm build") set(CMAKE_RANLIB $ENV{FAASM_WASM_RANLIB} CACHE STRING "faasm build") +set(CMAKE_C_COMPILER_TARGET $ENV{WASM_TRIPLE} CACHE STRING "faasm build") +set(CMAKE_CXX_COMPILER_TARGET $ENV{WASM_TRIPLE} CACHE STRING "faasm build") +set(CMAKE_ASM_COMPILER_TARGET $ENV{WASM_TRIPLE} CACHE STRING "faasm build") +message(STATUS "Faasm building target $ENV{CMAKE_CXX_COMPILER_TARGET}") # We define dynamic linking functions in Faasm unset(CMAKE_DL_LIBS CACHE) @@ -49,18 +51,16 @@ unset(CMAKE_DL_LIBS CACHE) # Add definition for flagging Faasm add_definitions(-D__faasm) -set(FAASM_COMPILER_FLAGS $ENV{FAASM_WASM_CFLAGS}) - if(FAASM_BUILD_SHARED) - set(FAASM_COMPILER_FLAGS "${FAASM_COMPILER_FLAGS} $ENV{FAASM_WASM_CFLAGS_SHARED}") + set(FAASM_COMPILER_FLAGS "$ENV{FAASM_WASM_CFLAGS} $ENV{FAASM_WASM_CFLAGS_SHARED}") endif() -set(CMAKE_SYSROOT ${FAASM_SYSROOT} CACHE STRING "faasm build") -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${FAASM_COMPILER_FLAGS}" CACHE STRING "faasm build") -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FAASM_COMPILER_FLAGS}" CACHE STRING "faasm build") -set(CMAKE_LINKER_FLAGS $ENV{FAASM_WASM_LINKER_FLAGS} CACHE STRING "faasm build") -set(CMAKE_SHARED_LINKER_FLAGS $ENV{FAASM_WASM_SHARED_LINKER_FLAGS} CACHE STRING "faasm build") -set(CMAKE_EXE_LINKER_FLAGS $ENV{FAASM_WASM_EXE_LINKER_FLAGS} CACHE STRING "faasm build") +set(CMAKE_SYSROOT $ENV{FAASM_SYSROOT} CACHE STRING "faasm build") +set(CMAKE_C_FLAGS "$ENV{CMAKE_C_FLAGS} $ENV{FAASM_WASM_CFLAGS}" CACHE STRING "faasm build") +set(CMAKE_CXX_FLAGS "$ENV{CMAKE_CXX_FLAGS} $ENV{FAASM_WASM_CXXFLAGS}" CACHE STRING "faasm build") +set(CMAKE_LINKER_FLAGS "$ENV{FAASM_WASM_LINKER_FLAGS}" CACHE STRING "faasm build") +set(CMAKE_SHARED_LINKER_FLAGS "$ENV{FAASM_WASM_SHARED_LINKER_FLAGS}" CACHE STRING "faasm build") +set(CMAKE_EXE_LINKER_FLAGS "$ENV{FAASM_WASM_EXE_LINKER_FLAGS}" CACHE STRING "faasm build") # This is important to ensure the right search path set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) @@ -72,4 +72,3 @@ set(CMAKE_C_COMPILER_WORKS ON) set(CMAKE_CXX_COMPILER_WORKS ON) set(FAASM_BLAS_LIBS $ENV{FAASM_WASM_BLAS_LIBS}) - diff --git a/docker/cpp-sysroot.dockerfile b/docker/cpp-sysroot.dockerfile index 7bbcda15..9ae5b972 100644 --- a/docker/cpp-sysroot.dockerfile +++ b/docker/cpp-sysroot.dockerfile @@ -1,8 +1,8 @@ # llvm image is not re-built often, so the tag may be behind -FROM faasm.azurecr.io/llvm:0.2.4 as llvm +FROM faasm.azurecr.io/llvm:0.4.0 as llvm # faabric-base image is not re-built often, so tag may be behind -FROM faasm.azurecr.io/faabric-base:0.4.2 +FROM faasm.azurecr.io/faabric-base:0.15.0 SHELL ["/bin/bash", "-c"] ENV CPP_DOCKER="on" @@ -45,18 +45,22 @@ RUN cd /code/cpp \ # Build wasi-libc and reset the sysroot. The second call to LLVM just # installs some headers that are purged && inv llvm.libc --purge llvm \ - # Build Faasm WASM libraries + # Build Faasm WASM libraries for wasm32-wasi target && inv \ - libemscripten \ libfaasm \ - libfaasmp \ + libemscripten \ libfaasmpi \ + # Build Faasm WASM libraries for wasm32-wasi-threads target + && inv \ + libfaasm --threads \ + libemscripten --threads \ + libfaasmp \ # Lastly, build the libraries that populate the sysroot && inv \ - clapack \ - clapack --clean --shared \ libffi \ - zlib + libffi --threads \ + zlib \ + zlib --threads # CLI setup WORKDIR /code/cpp diff --git a/docker/llvm.dockerfile b/docker/llvm.dockerfile index 3d69e196..a4d0f39e 100644 --- a/docker/llvm.dockerfile +++ b/docker/llvm.dockerfile @@ -3,13 +3,26 @@ FROM ubuntu:22.04 SHELL ["/bin/bash", "-c"] ENV CPP_DOCKER="on" +# Configure APT repositories +ARG LLVM_VERSION_MAJOR +RUN apt update \ + && apt upgrade -y \ + && apt install -y \ + curl \ + gpg \ + software-properties-common \ + wget \ + && wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc \ + && add-apt-repository -y "deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-${LLVM_VERSION_MAJOR} main" + # Install APT dependencies ARG DEBIAN_FRONTEND=noninteractive RUN apt update \ && apt install -y \ autoconf \ build-essential \ - clang-13 \ + # This clang version is needed to help us build LLVM from source below + clang-${LLVM_VERSION_MAJOR} \ curl \ git \ gpg \ @@ -26,7 +39,7 @@ RUN apt remove --purge --auto-remove cmake \ && mkdir -p /setup \ && cd /setup \ && wget -q -O cmake-linux.sh \ - https://github.com/Kitware/CMake/releases/download/v3.24.2/cmake-3.24.2-linux-x86_64.sh \ + https://github.com/Kitware/CMake/releases/download/v3.28.0/cmake-3.28.0-linux-x86_64.sh \ && sh cmake-linux.sh -- --skip-license --prefix=/usr/local \ && apt clean autoclean -y \ && apt autoremove -y @@ -42,6 +55,7 @@ RUN mkdir -p /code \ && ./bin/create_venv.sh \ && source venv/bin/activate \ && inv install \ + # This task builds LLVM from source and reads the version from faasmtools/env.py && inv llvm.build \ && /usr/local/faasm/toolchain/bin/clang --version \ && rm -rf /code diff --git a/docs/upgrade-llvm.md b/docs/llvm.md similarity index 76% rename from docs/upgrade-llvm.md rename to docs/llvm.md index dd017a1e..f6919744 100644 --- a/docs/upgrade-llvm.md +++ b/docs/llvm.md @@ -1,4 +1,14 @@ -# Upgrading LLVM +# LLVM + +We use LLVM to cross-compile C and C++ code to WebAssembly. Our +cross-compilation toolchain consists of a home-built LLVM suite (`clang` and +co.) targetting WebAssembly, together with a sysroot with a set of standard +libraries to link against so that we can cross-compile meaningful applications +(most notably `libc` through [`wasi-libc`]( +https://github.com/faasm/wasi-libc). + +To this extent, our set-up is very similar to that of [`wasi-sdk`]( +https://github.com/WebAssembly/wasi-sdk). To upgrade the underlying LLVM version you need to: diff --git a/faasmtools/build.py b/faasmtools/build.py index 1afa5ffa..b3745172 100644 --- a/faasmtools/build.py +++ b/faasmtools/build.py @@ -1,4 +1,3 @@ -from copy import copy from os.path import join from subprocess import run from os import environ @@ -15,8 +14,6 @@ FAASM_NATIVE_DIR = join(FAASM_LOCAL_DIR, "native") FAASM_CMAKE_ROOT = "/usr/share/cmake-3.24" WASM_SYSROOT = join(FAASM_LOCAL_DIR, "llvm-sysroot") -WASM_HEADER_INSTALL = "{}/include".format(WASM_SYSROOT) -WASM_LIB_INSTALL = "{}/lib/wasm32-wasi".format(WASM_SYSROOT) WASM_TOOLCHAIN_ROOT = join(FAASM_LOCAL_DIR, "toolchain") WASM_TOOLCHAIN_TOOLS = join(WASM_TOOLCHAIN_ROOT, "tools") WASM_TOOLCHAIN_BIN = join(WASM_TOOLCHAIN_ROOT, "bin") @@ -38,6 +35,7 @@ WASM_LDXX = WASM_CXX # Host triple +# TODO(shared-libs): consider removing this WASM_BUILD = "wasm32" WASM_HOST = "wasm32-unknown-wasi" WASM_HOST_STATIC = "wasm32-wasi" @@ -73,7 +71,7 @@ # https://reviews.llvm.org/D59281 WASM_CFLAGS = [ "-O3", - "-mno-atomics", + # TODO: may want to use -mrelaxed-simd instead "-msimd128", "--sysroot={}".format(WASM_SYSROOT), "-m32", @@ -142,7 +140,6 @@ "-Xlinker --export={}".format(FAASM_WASM_CTORS_FUNC_NAME), "-Xlinker --export=__stack_pointer", "-Xlinker --max-memory={}".format(FAASM_WASM_MAX_MEMORY), - "-Xlinker --features=mutable-globals,simd128", "-Wl,-z,stack-size={} -Wl".format(FAASM_WASM_STACK_SIZE), ] @@ -188,7 +185,9 @@ # Env. variables as a dictionary: prefix with FAASM_WASM or FAASM_NATIVE # depending on the build type variables target -FAASM_BUILD_ENV_DICT = { +# WARNING: do NOT import this method directly, instead use the getter method: +# get_faasm_build_env_dict +_FAASM_BUILD_ENV_DICT = { "CMAKE_ROOT": FAASM_CMAKE_ROOT, "FAASM_NATIVE_INSTALL_DIR": FAASM_NATIVE_DIR, "FAASM_WASM_MAKE_TOOLCHAIN_FILE": MAKE_TOOLCHAIN_FILE, @@ -204,6 +203,8 @@ "FAASM_WASM_SYSROOT": WASM_SYSROOT, "FAASM_WASM_CFLAGS": " ".join(WASM_CFLAGS), "FAASM_WASM_CFLAGS_SHARED": " ".join(WASM_CFLAGS_SHARED), + "FAASM_WASM_CXXFLAGS": " ".join(WASM_CXXFLAGS), + "FAASM_WASM_CXXFLAGS_SHARED": " ".join(WASM_CXXFLAGS_SHARED), "FAASM_WASM_EXE_LINKER_FLAGS": " ".join(WASM_EXE_LDFLAGS), "FAASM_WASM_EXE_LINKER_FLAGS_SHARED": " ".join(WASM_EXE_LDFLAGS_SHARED), "FAASM_WASM_SHARED_LINKER_FLAGS": " ".join(WASM_LDFLAGS_SHARED), @@ -222,6 +223,51 @@ } +def get_faasm_build_env_dict(is_threads=False): + """ + This method returns the right set of environment variables needed to use + our toolchain file as well as most cross-compilation scripts in Faasm. + """ + build_env_dicts = _FAASM_BUILD_ENV_DICT + if is_threads: + wasm_triple = "wasm32-wasi-threads" + build_env_dicts["FAASM_WASM_TRIPLE"] = wasm_triple + build_env_dicts["FAASM_WASM_CFLAGS"] += " --target={} -pthread".format( + wasm_triple + ) + build_env_dicts[ + "FAASM_WASM_CXXFLAGS" + ] += " --target={} -pthread".format(wasm_triple) + linker_features = [ + "atomics", + "bulk-memory", + "mutable-globals", + "sign-ext", + "simd128", + ] + else: + wasm_triple = "wasm32-wasi" + build_env_dicts["FAASM_WASM_TRIPLE"] = wasm_triple + linker_features = [ + "bulk-memory", + "mutable-globals", + "sign-ext", + "simd128", + ] + + build_env_dicts["FAASM_WASM_HEADER_INSTALL_DIR"] = join( + WASM_SYSROOT, "include", wasm_triple + ) + build_env_dicts["FAASM_WASM_LIB_INSTALL_DIR"] = join( + WASM_SYSROOT, "lib", wasm_triple + ) + build_env_dicts[ + "FAASM_WASM_EXE_LINKER_FLAGS" + ] += " -Xlinker --features={}".format(",".join(linker_features)) + + return build_env_dicts + + def get_dict_as_cmake_vars(env_dict): return " ".join(["-D{}={}".format(k, env_dict[k]) for k in env_dict]) @@ -230,78 +276,58 @@ def get_dict_as_cmake_vars(env_dict): # Scripts for configure, automake, and autotools # ---------- -# Variables for 'configure' scripts -_BASE_CONFIG_CMD = [ - "CC={}".format(WASM_CC), - "CXX={}".format(WASM_CXX), - "CPP={}".format(WASM_CPP), - "AR={}".format(WASM_AR), - "RANLIB={}".format(WASM_RANLIB), - 'CFLAGS="{}"'.format(" ".join(WASM_CFLAGS)), - 'CPPFLAGS="{}"'.format(" ".join(WASM_CFLAGS)), - 'CXXFLAGS="{}"'.format(" ".join(WASM_CXXFLAGS)), - 'CCSHARED="{}"'.format(WASM_CCSHARED), - 'CXXSHARED="{}"'.format(WASM_CXXSHARED), -] - -_BASE_CONFIG_ARGS = [ - "--build={}".format(WASM_BUILD), - "--host={}".format(WASM_HOST), -] - -_BASE_CONFIG_ARGS_SHARED = [ - "--build={}".format(WASM_BUILD), - "--host={}".format(WASM_HOST_SHARED), -] - -BASE_CONFIG_CMD = _BASE_CONFIG_CMD + [ - "LD={}".format(WASM_LD), - 'LDSHARED="{}"'.format(WASM_LDSHARED), -] - -BASE_CONFIG_CMDXX = _BASE_CONFIG_CMD + [ - "LD={}".format(WASM_LDXX), - 'LDSHARED="{}"'.format(WASM_LDXXSHARED), -] - -BASE_CONFIG_FLAGS = [ - 'CFLAGS="{}"'.format(" ".join(WASM_CFLAGS)), - 'CPPFLAGS="{}"'.format(" ".join(WASM_CXXFLAGS)), - 'LDFLAGS="{}"'.format(" ".join(WASM_LDFLAGS)), -] - -BASE_CONFIG_FLAGS_SHARED = [ - 'CFLAGS="{}"'.format(" ".join(WASM_CFLAGS_SHARED)), - 'CPPFLAGS="{}"'.format(" ".join(WASM_CXXFLAGS_SHARED)), - 'LDFLAGS="{}"'.format(" ".join(WASM_LDFLAGS_SHARED)), -] - -def build_config_cmd(cmd, shared=False, cxx=False, conf_args=True): +def build_config_cmd(env_vars, cmd, shared=False, cxx=False, conf_args=True): """ Wraps an autotools command in the relevant environment variables and - cross-compilation config + cross-compilation config. We need to call this method after we get + the right env. vars using get_faasm_build_env_dict """ - result = copy(BASE_CONFIG_CMDXX if cxx else BASE_CONFIG_CMD) - - result += BASE_CONFIG_FLAGS_SHARED if shared else BASE_CONFIG_FLAGS - - result += cmd - - if conf_args: - result += _BASE_CONFIG_ARGS_SHARED if shared else _BASE_CONFIG_ARGS - - return result - - -def run_autotools(proj_dir): + base_config_cmd = [ + "CC={}".format(env_vars["FAASM_WASM_CC"]), + "CXX={}".format(env_vars["FAASM_WASM_CXX"]), + "AR={}".format(env_vars["FAASM_WASM_AR"]), + "RANLIB={}".format(env_vars["FAASM_WASM_RANLIB"]), + 'CFLAGS="--target={} {}"'.format( + env_vars["FAASM_WASM_TRIPLE"], env_vars["FAASM_WASM_CFLAGS"] + ), + 'CXXFLAGS="--target={} {}"'.format( + env_vars["FAASM_WASM_TRIPLE"], env_vars["FAASM_WASM_CXXFLAGS"] + ), + 'CCSHARED="{}"'.format(env_vars["FAASM_WASM_CFLAGS_SHARED"]), + 'CXXSHARED="{}"'.format(env_vars["FAASM_WASM_CXXFLAGS_SHARED"]), + ] + + if cxx: + base_config_cmd += [ + "LD={}".format(env_vars["FAASM_WASM_CXX"]), + 'LDFLAGS="-target {} {}"'.format( + env_vars["FAASM_WASM_TRIPLE"], + env_vars["FAASM_WASM_STATIC_LINKER_FLAGS"], + ), + ] + else: + base_config_cmd += [ + "LD={}".format(env_vars["FAASM_WASM_CC"]), + 'LDFLAGS="-target {} {}"'.format( + env_vars["FAASM_WASM_TRIPLE"], + env_vars["FAASM_WASM_STATIC_LINKER_FLAGS"], + ), + ] + + base_config_cmd += cmd + + return base_config_cmd + + +def run_autotools(env_vars, proj_dir): """ Runs through autotools set-up on the given directory """ def _run_auto_cmd(cmd): print("Running {}".format(cmd)) - auto_cmd = build_config_cmd([cmd], conf_args=False) + auto_cmd = build_config_cmd(env_vars, [cmd], conf_args=False) auto_cmd = " ".join(auto_cmd) run(auto_cmd, shell=True, check=True, cwd=proj_dir) diff --git a/faasmtools/compile_util.py b/faasmtools/compile_util.py index a3def6af..9b98fec1 100644 --- a/faasmtools/compile_util.py +++ b/faasmtools/compile_util.py @@ -1,6 +1,6 @@ from faasmtools.build import ( CMAKE_TOOLCHAIN_FILE, - FAASM_BUILD_ENV_DICT, + get_faasm_build_env_dict, ) from faasmtools.env import WASM_DIR from os import environ, makedirs @@ -9,7 +9,9 @@ from subprocess import run -def wasm_cmake(src_dir, build_dir, target, clean=False, debug=False): +def wasm_cmake( + src_dir, build_dir, target, clean=False, debug=False, is_threads=False +): cmake_build_type = "Debug" if debug else "Release" if exists(build_dir) and clean: @@ -28,7 +30,7 @@ def wasm_cmake(src_dir, build_dir, target, clean=False, debug=False): print(build_cmd) work_env = environ.copy() - work_env.update(FAASM_BUILD_ENV_DICT) + work_env.update(get_faasm_build_env_dict(is_threads)) res = run(build_cmd, shell=True, cwd=build_dir, env=work_env) if res.returncode != 0: diff --git a/faasmtools/env.py b/faasmtools/env.py index 956e36be..eaf0fa7d 100644 --- a/faasmtools/env.py +++ b/faasmtools/env.py @@ -12,8 +12,10 @@ # Versioning VERSION_FILE = join(PROJ_ROOT, "VERSION") -# LLVM variables -LLVM_VERSION = "13.0.1" +# LLVM version for C/C++ to x86 compilation +LLVM_NATIVE_VERSION = "17.0.6" +# LLVM version for C/C++ to WASM compilation +LLVM_VERSION = "18.1.0-rc2" LLVM_DIR = join("/opt", "llvm-project") LLVM_MAKEFILE = join(PROJ_ROOT, "LLVM.makefile") WASI_LIBC_DIR = join(THIRD_PARTY_DIR, "wasi-libc") diff --git a/func/CMakeLists.txt b/func/CMakeLists.txt index 59a5de82..47f4f86e 100644 --- a/func/CMakeLists.txt +++ b/func/CMakeLists.txt @@ -11,7 +11,7 @@ set(CMAKE_CXX_STANDARD 17) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(FAASM_NATIVE_INSTALL $ENV{FAASM_NATIVE_INSTALL_DIR}) -if (CMAKE_SYSTEM_NAME STREQUAL "Wasm") +if (CMAKE_SYSTEM_NAME STREQUAL "WASI") # For Faasm functions, we add `_faasm_zygote` as an exported symbol set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Xlinker --export=_faasm_zygote" @@ -20,7 +20,7 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Wasm") set(CMAKE_EXECUTABLE_SUFFIX ".wasm") endif () -if (CMAKE_SYSTEM_NAME STREQUAL "Wasm") +if (CMAKE_SYSTEM_NAME STREQUAL "WASI") # ---------------------- # Wasm build # ---------------------- @@ -69,8 +69,9 @@ else () endfunction(faasm_dynamic_func) endif () -add_subdirectory(errors) add_subdirectory(demo) add_subdirectory(dynlink) +add_subdirectory(errors) add_subdirectory(mpi) add_subdirectory(omp) +add_subdirectory(threads) diff --git a/func/demo/CMakeLists.txt b/func/demo/CMakeLists.txt index a1ebc9ad..d6009299 100644 --- a/func/demo/CMakeLists.txt +++ b/func/demo/CMakeLists.txt @@ -87,10 +87,6 @@ demo_func(stdout stdout.cpp) demo_func(stderr stderr.cpp) demo_func(string string.cpp) demo_func(sysconf sysconf.cpp) -demo_func(threads_local threads_local.cpp) -demo_func(threads_check threads_check.cpp) -demo_func(threads_dist threads_dist.cpp) -demo_func(threads_memory threads_memory.cpp) demo_func(time time.cpp) demo_func(time_of_day time_of_day.cpp) demo_func(uname uname.cpp) @@ -99,13 +95,12 @@ demo_func(waitpid waitpid.cpp) demo_func(x2 x2.cpp) demo_func(zygote_check zygote_check.cpp) -# 23/09/2020 - removing simd # WASM-only functions -if (CMAKE_SYSTEM_NAME STREQUAL "Wasm") +if (CMAKE_SYSTEM_NAME STREQUAL "WASI") demo_func(emscripten_check emscripten_check.cpp) target_link_libraries(emscripten_check emscripten) - # TODO - fix BLAS check function + # 12/03/2024 - BLAS (and CLAPACK) has been broken for a while # include_directories(/usr/local/faasm/llvm-sysroot/include/clapack) # demo_func_c(blas_check blas_check.c) # target_link_libraries(blas_check ${FAASM_BLAS_LIBS}) diff --git a/func/dynlink/CMakeLists.txt b/func/dynlink/CMakeLists.txt index d8b71389..4065711d 100644 --- a/func/dynlink/CMakeLists.txt +++ b/func/dynlink/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.8.0) project(dynlink) set(BUILD_SHARED_LIBS ON) diff --git a/func/dynlink/libA/CMakeLists.txt b/func/dynlink/libA/CMakeLists.txt index ceda19f9..b6d081c4 100644 --- a/func/dynlink/libA/CMakeLists.txt +++ b/func/dynlink/libA/CMakeLists.txt @@ -3,7 +3,7 @@ set(PUBLIC_HEADERS ../include/sharedHeader.h libA.h) add_library(fakeLibA SHARED ${PUBLIC_HEADERS} libA.c) set_target_properties(fakeLibA PROPERTIES PUBLIC_HEADER "${PUBLIC_HEADERS}") -if (CMAKE_SYSTEM_NAME STREQUAL "Wasm") +if (CMAKE_SYSTEM_NAME STREQUAL "WASI") install(TARGETS fakeLibA ARCHIVE DESTINATION lib/wasm32-wasi LIBRARY DESTINATION lib/wasm32-wasi diff --git a/func/dynlink/libB/CMakeLists.txt b/func/dynlink/libB/CMakeLists.txt index 3d5360e8..ae2c06e7 100644 --- a/func/dynlink/libB/CMakeLists.txt +++ b/func/dynlink/libB/CMakeLists.txt @@ -3,7 +3,7 @@ set_target_properties(fakeLibB PROPERTIES PUBLIC_HEADER libB.h) target_link_libraries(fakeLibB fakeLibA) -if (CMAKE_SYSTEM_NAME STREQUAL "Wasm") +if (CMAKE_SYSTEM_NAME STREQUAL "WASI") install(TARGETS fakeLibB ARCHIVE DESTINATION lib/wasm32-wasi LIBRARY DESTINATION lib/wasm32-wasi diff --git a/func/errors/CMakeLists.txt b/func/errors/CMakeLists.txt index 292e4870..1f01b413 100644 --- a/func/errors/CMakeLists.txt +++ b/func/errors/CMakeLists.txt @@ -11,7 +11,7 @@ error_func(open_blocked open_blocked.cpp) error_func(ret_one ret_one.cpp) error_func(stdin_poll stdin_poll.cpp) -if (CMAKE_SYSTEM_NAME STREQUAL "Wasm") +if (CMAKE_SYSTEM_NAME STREQUAL "WASI") error_func(stackoverflow stackoverflow.cpp) endif() diff --git a/func/mpi/CMakeLists.txt b/func/mpi/CMakeLists.txt index b3ed5d2d..b3454fcf 100644 --- a/func/mpi/CMakeLists.txt +++ b/func/mpi/CMakeLists.txt @@ -8,7 +8,7 @@ function(mpi_func exec_name dir_path) target_link_libraries(${exec_name} faasmpi mpi) - if (NOT CMAKE_SYSTEM_NAME STREQUAL "Wasm") + if (NOT CMAKE_SYSTEM_NAME STREQUAL "WASI") target_link_libraries(${exec_name} pthread) endif () diff --git a/func/omp/CMakeLists.txt b/func/omp/CMakeLists.txt index 4b16f50a..18ac0a50 100644 --- a/func/omp/CMakeLists.txt +++ b/func/omp/CMakeLists.txt @@ -1,8 +1,9 @@ set(FAASM_USER omp) -if (CMAKE_SYSTEM_NAME STREQUAL "Wasm") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fopenmp") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp") +if (CMAKE_SYSTEM_NAME STREQUAL "WASI") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --target=$ENV{FAASM_WASM_TRIPLE} -fopenmp") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --target=$ENV{FAASM_WASM_TRIPLE} -fopenmp") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Xlinker --shared-memory") else () find_package(OpenMP REQUIRED) include_directories(${OPENMP_INCLUDE_DIR} ${LLVM_INCLUDE_DIRS}/openmp) @@ -15,7 +16,7 @@ function(omp_func exec_name dir_path) target_link_libraries(${exec_name} faasmp) - if (NOT CMAKE_SYSTEM_NAME STREQUAL "Wasm") + if (NOT CMAKE_SYSTEM_NAME STREQUAL "WASI") target_link_libraries(${exec_name} pthread) endif () diff --git a/func/omp/complex_reduce.cpp b/func/omp/complex_reduce.cpp index e64a0a94..fd533451 100644 --- a/func/omp/complex_reduce.cpp +++ b/func/omp/complex_reduce.cpp @@ -40,10 +40,9 @@ int main(int argc, char* argv[]) FAASM_REDUCE(reducedA, FAASM_TYPE_INT, FAASM_OP_SUM) FAASM_REDUCE(reducedB, FAASM_TYPE_DOUBLE, FAASM_OP_SUM) -#pragma omp parallel for num_threads(nThreads) default(none) \ - private(privateA,privateB) \ - shared(loopSize,sharedA,sharedB,counts) \ - reduction(+ : reducedA,reducedB) +#pragma omp parallel for num_threads(nThreads) default(none) private( \ + privateA, privateB) shared(loopSize, sharedA, sharedB, counts) \ + reduction(+ : reducedA, reducedB) for (int i = 0; i < loopSize; i++) { int threadNum = omp_get_thread_num(); diff --git a/func/omp/custom_reduce.cpp b/func/omp/custom_reduce.cpp index 5637b60b..8fcb7c32 100644 --- a/func/omp/custom_reduce.cpp +++ b/func/omp/custom_reduce.cpp @@ -18,8 +18,8 @@ complex_t complex_add(complex_t a, complex_t b) return c; } -#pragma omp declare reduction(cmplxAdd:complex_t \ - : omp_out = complex_add(omp_out, omp_in)) \ +#pragma omp declare reduction(cmplxAdd:complex_t : omp_out = \ + complex_add(omp_out, omp_in)) \ initializer(omp_priv = { 0, 0 }) int main() diff --git a/func/omp/default_shared.cpp b/func/omp/default_shared.cpp index 5f63f9e3..c15b2288 100644 --- a/func/omp/default_shared.cpp +++ b/func/omp/default_shared.cpp @@ -15,10 +15,8 @@ int main(int argc, char* argv[]) FAASM_REDUCE(count, FAASM_TYPE_INT, FAASM_OP_SUM) -#pragma omp parallel for num_threads(5) \ - default(none) \ - shared(nLoops, flagsStack, flagsHeap) \ - reduction(+ : count) +#pragma omp parallel for num_threads(5) default(none) \ + shared(nLoops, flagsStack, flagsHeap) reduction(+ : count) for (int i = 0; i < nLoops; i++) { int threadNum = omp_get_thread_num(); count += (threadNum + 1); diff --git a/func/omp/inspect_reduction.cpp b/func/omp/inspect_reduction.cpp index d269be87..b23f0d1a 100644 --- a/func/omp/inspect_reduction.cpp +++ b/func/omp/inspect_reduction.cpp @@ -11,9 +11,8 @@ int inspectAdder(int a, int b) return a + b; } -#pragma omp declare reduction(inspectAdd:int \ - : omp_out = inspectAdder(omp_out, omp_in)) \ - initializer(omp_priv = 0) +#pragma omp declare reduction(inspectAdd \ +:int : omp_out = inspectAdder(omp_out, omp_in)) initializer(omp_priv = 0) int main() { diff --git a/func/omp/mem_stress.cpp b/func/omp/mem_stress.cpp index 3341bc67..ced2f355 100644 --- a/func/omp/mem_stress.cpp +++ b/func/omp/mem_stress.cpp @@ -28,7 +28,7 @@ int main(int argc, char** argv) FAASM_REDUCE(totalLoops, FAASM_TYPE_INT, FAASM_OP_SUM) #pragma omp parallel for num_threads(nThreads) default(none) \ - shared(ptrs, counts) reduction(+:totalLoops) + shared(ptrs, counts) reduction(+ : totalLoops) for (int i = 0; i < ITERATIONS; i++) { totalLoops++; @@ -49,7 +49,7 @@ int main(int argc, char** argv) FAASM_REDUCE(totalLoops, FAASM_TYPE_INT, FAASM_OP_SUM) #pragma omp parallel for num_threads(nThreads) default(none) \ - shared(ptrs, counts) reduction(+:totalLoops) + shared(ptrs, counts) reduction(+ : totalLoops) for (int i = 0; i < ITERATIONS; i++) { totalLoops++; ::free(ptrs[i]); @@ -61,7 +61,7 @@ int main(int argc, char** argv) FAASM_REDUCE(totalLoops, FAASM_TYPE_INT, FAASM_OP_SUM) #pragma omp parallel for num_threads(nThreads) default(none) \ - shared(ptrs, counts) reduction(+:totalLoops) + shared(ptrs, counts) reduction(+ : totalLoops) for (int i = 0; i < ITERATIONS; i++) { totalLoops++; size_t mallocSize = (i % 6) * 1024; diff --git a/func/omp/pi_faasm.cpp b/func/omp/pi_faasm.cpp index 035afb7e..503ee57f 100644 --- a/func/omp/pi_faasm.cpp +++ b/func/omp/pi_faasm.cpp @@ -23,9 +23,8 @@ int main(int argc, char** argv) int nTotal = nWorkers * CHUNK_SIZE; FAASM_REDUCE(result, FAASM_TYPE_LONG, FAASM_OP_SUM) -#pragma omp parallel num_threads(nWorkers) default(none) \ - shared(nTotal) \ - reduction(+ : result) +#pragma omp parallel num_threads(nWorkers) default(none) shared(nTotal) \ + reduction(+ : result) { // Different seed per thread std::uniform_real_distribution unif(0, 1); diff --git a/func/omp/pi_native.cpp b/func/omp/pi_native.cpp index 347e6803..ad8d1ceb 100644 --- a/func/omp/pi_native.cpp +++ b/func/omp/pi_native.cpp @@ -25,9 +25,8 @@ int main(int argc, char** argv) printf("Estimating Pi with %i workers\n", nWorkers); -#pragma omp parallel num_threads(nWorkers) default(none) \ - shared(nTotal) \ - reduction(+ : result) +#pragma omp parallel num_threads(nWorkers) default(none) shared(nTotal) \ + reduction(+ : result) { // Different seed per thread std::uniform_real_distribution unif(0, 1); diff --git a/func/omp/reduction_integral.cpp b/func/omp/reduction_integral.cpp index b1189dbf..eac6aa91 100644 --- a/func/omp/reduction_integral.cpp +++ b/func/omp/reduction_integral.cpp @@ -124,7 +124,8 @@ double doBetterReduction() FAASM_REDUCE(sum, FAASM_TYPE_DOUBLE, FAASM_OP_SUM) -#pragma omp parallel for private(x) default(none) shared(nSteps, step) reduction(+:sum) +#pragma omp parallel for private(x) default(none) shared(nSteps, step) \ + reduction(+ : sum) for (i = 0; i < nSteps; ++i) { x = (i + 0.5) * step; sum += 4.0 / (1.0 + x * x); diff --git a/func/omp/repeated_reduce.cpp b/func/omp/repeated_reduce.cpp index 4d861687..66a49a82 100644 --- a/func/omp/repeated_reduce.cpp +++ b/func/omp/repeated_reduce.cpp @@ -20,9 +20,8 @@ bool doReduce() FAASM_REDUCE(reducedA, FAASM_TYPE_INT, FAASM_OP_SUM) FAASM_REDUCE(reducedB, FAASM_TYPE_INT, FAASM_OP_SUM) -#pragma omp parallel for num_threads(nThreads) default(none) \ - shared(counts,loopSize,success) \ - reduction(+ : reducedA,reducedB) +#pragma omp parallel for num_threads(nThreads) default(none) \ + shared(counts, loopSize, success) reduction(+ : reducedA, reducedB) for (int i = 0; i < loopSize; i++) { int threadNum = omp_get_thread_num(); counts[threadNum]++; diff --git a/func/omp/simple_reduce.cpp b/func/omp/simple_reduce.cpp index ed354b1f..d982447b 100644 --- a/func/omp/simple_reduce.cpp +++ b/func/omp/simple_reduce.cpp @@ -14,7 +14,8 @@ int main(int argc, char* argv[]) FAASM_REDUCE(count, FAASM_TYPE_INT, FAASM_OP_SUM) -#pragma omp parallel for num_threads(5) default(none) shared(loopSize) reduction(+ : count) +#pragma omp parallel for num_threads(5) default(none) shared(loopSize) \ + reduction(+ : count) for (int i = 0; i < loopSize; i++) { count += (omp_get_thread_num() + 1); } diff --git a/func/omp/single_thread_reduce.cpp b/func/omp/single_thread_reduce.cpp index 21a6f782..ebc0268a 100644 --- a/func/omp/single_thread_reduce.cpp +++ b/func/omp/single_thread_reduce.cpp @@ -30,10 +30,9 @@ int main(int argc, char* argv[]) FAASM_REDUCE(reducedA, FAASM_TYPE_INT, FAASM_OP_SUM) FAASM_REDUCE(reducedB, FAASM_TYPE_DOUBLE, FAASM_OP_SUM) -#pragma omp parallel for num_threads(1) default(none) \ - private(privateA,privateB) \ - reduction(+ : reducedA,reducedB) \ - shared(loopSize, sharedA) +#pragma omp parallel for num_threads(1) default(none) private(privateA, \ + privateB) \ + reduction(+ : reducedA, reducedB) shared(loopSize, sharedA) for (int i = 0; i < loopSize; i++) { int threadNum = omp_get_thread_num(); diff --git a/func/threads/CMakeLists.txt b/func/threads/CMakeLists.txt new file mode 100644 index 00000000..9deb5411 --- /dev/null +++ b/func/threads/CMakeLists.txt @@ -0,0 +1,20 @@ +set(FAASM_USER threads) + +if (CMAKE_SYSTEM_NAME STREQUAL "WASI") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --target=$ENV{FAASM_WASM_TRIPLE} -pthread") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --target=$ENV{FAASM_WASM_TRIPLE} -pthread") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Xlinker --shared-memory") +endif () + +function(thread_func exec_name dir_path) + faasm_func(${exec_name} ${dir_path}) + set(ALL_THREADS_FUNCS ${ALL_THREADS_FUNCS} ${exec_name} PARENT_SCOPE) +endfunction(thread_func) + +thread_func(threads_local threads_local.cpp) +thread_func(threads_check threads_check.cpp) +thread_func(threads_dist threads_dist.cpp) +thread_func(threads_memory threads_memory.cpp) + +# Custom target for building all functions +add_custom_target(threads_all_funcs DEPENDS ${ALL_THREADS_FUNCS}) diff --git a/func/demo/threads_check.cpp b/func/threads/threads_check.cpp similarity index 100% rename from func/demo/threads_check.cpp rename to func/threads/threads_check.cpp diff --git a/func/demo/threads_dist.cpp b/func/threads/threads_dist.cpp similarity index 100% rename from func/demo/threads_dist.cpp rename to func/threads/threads_dist.cpp diff --git a/func/demo/threads_local.cpp b/func/threads/threads_local.cpp similarity index 100% rename from func/demo/threads_local.cpp rename to func/threads/threads_local.cpp diff --git a/func/demo/threads_memory.cpp b/func/threads/threads_memory.cpp similarity index 100% rename from func/demo/threads_memory.cpp rename to func/threads/threads_memory.cpp diff --git a/libemscripten/CMakeLists.txt b/libemscripten/CMakeLists.txt index 7cc86bbb..8d963cd1 100644 --- a/libemscripten/CMakeLists.txt +++ b/libemscripten/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.8) project(libemscripten) # ------ Support for certain emscripten functions ------ # @@ -6,26 +6,27 @@ project(libemscripten) set(CMAKE_CXX_STANDARD 17) set(PUBLIC_HEADERS - faasm/emscripten.h - ) + faasm/emscripten.h +) set(LIB_FILES - emscripten.c - ) + emscripten.c +) add_library(emscripten STATIC ${LIB_FILES}) set_target_properties(emscripten PROPERTIES PUBLIC_HEADER "${PUBLIC_HEADERS}") -install(TARGETS emscripten - ARCHIVE DESTINATION ${CMAKE_SYSROOT}/lib/wasm32-wasi - LIBRARY DESTINATION ${CMAKE_SYSROOT}/lib/wasm32-wasi - PUBLIC_HEADER DESTINATION ${CMAKE_SYSROOT}/include/faasm - ) +install( + TARGETS emscripten + ARCHIVE DESTINATION $ENV{FAASM_WASM_LIB_INSTALL_DIR} + LIBRARY DESTINATION $ENV{FAASM_WASM_LIB_INSTALL_DIR} + PUBLIC_HEADER DESTINATION $ENV{FAASM_WASM_HEADER_INSTALL_DIR}/faasm +) install( - FILES emscripten.imports - DESTINATION ${CMAKE_SYSROOT}/lib/wasm32-wasi - RENAME libemscripten.imports + FILES libemscripten.imports + DESTINATION $ENV{FAASM_WASM_LIB_INSTALL_DIR} + RENAME libemscripten.imports ) target_include_directories(emscripten PUBLIC ${CMAKE_CURRENT_LIST_DIR}) diff --git a/libemscripten/emscripten.imports b/libemscripten/libemscripten.imports similarity index 100% rename from libemscripten/emscripten.imports rename to libemscripten/libemscripten.imports diff --git a/libfaasm/CMakeLists.txt b/libfaasm/CMakeLists.txt index 7192fdbd..d17616a9 100644 --- a/libfaasm/CMakeLists.txt +++ b/libfaasm/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.8) project(libfaasm) # ------ Faasm C/C++ library ------ # @@ -6,54 +6,54 @@ project(libfaasm) set(CMAKE_CXX_STANDARD 17) set(PUBLIC_HEADERS - faasm/core.h - faasm/host_interface.h - faasm/host_interface_test.h - faasm/array.h - faasm/compare.h - faasm/faasm.h - faasm/files.h - faasm/input.h - faasm/memory.h - faasm/migrate.h - faasm/print.h - faasm/random.h - faasm/shared_mem.h - faasm/state.h - faasm/time.h - ) + faasm/core.h + faasm/host_interface.h + faasm/host_interface_test.h + faasm/array.h + faasm/compare.h + faasm/faasm.h + faasm/files.h + faasm/input.h + faasm/memory.h + faasm/migrate.h + faasm/print.h + faasm/random.h + faasm/shared_mem.h + faasm/state.h + faasm/time.h +) set(LIB_FILES - compare.cpp - core.cpp - files.cpp - input.cpp - print.cpp - random.cpp - state.cpp - time.cpp - zygote.cpp - ) + compare.cpp + core.cpp + files.cpp + input.cpp + print.cpp + random.cpp + state.cpp + time.cpp + zygote.cpp +) - -if (CMAKE_SYSTEM_NAME STREQUAL "Wasm") +if (CMAKE_SYSTEM_NAME STREQUAL "WASI") message(STATUS "Libfaasm WebAssembly build") add_library(faasm STATIC ${LIB_FILES}) set_target_properties(faasm PROPERTIES PUBLIC_HEADER "${PUBLIC_HEADERS}") # Install in WASM sysroot - install(TARGETS faasm - ARCHIVE DESTINATION ${CMAKE_SYSROOT}/lib/wasm32-wasi - LIBRARY DESTINATION ${CMAKE_SYSROOT}/lib/wasm32-wasi - PUBLIC_HEADER DESTINATION ${CMAKE_SYSROOT}/include/faasm - ) + install( + TARGETS faasm + ARCHIVE DESTINATION $ENV{FAASM_WASM_LIB_INSTALL_DIR} + LIBRARY DESTINATION $ENV{FAASM_WASM_LIB_INSTALL_DIR} + PUBLIC_HEADER DESTINATION $ENV{FAASM_WASM_HEADER_INSTALL_DIR}/faasm + ) # Add allowed import files install( - FILES faasm.imports - DESTINATION ${CMAKE_SYSROOT}/lib/wasm32-wasi - RENAME libfaasm.imports + FILES libfaasm.imports + DESTINATION $ENV{FAASM_WASM_LIB_INSTALL_DIR} + RENAME libfaasm.imports ) else () message(STATUS "Libfaasm native build") @@ -66,11 +66,12 @@ else () set_target_properties(faasm PROPERTIES PUBLIC_HEADER "${PUBLIC_HEADERS}") - install(TARGETS faasm - ARCHIVE DESTINATION lib - LIBRARY DESTINATION lib - PUBLIC_HEADER DESTINATION include/faasm - ) + install( + TARGETS faasm + ARCHIVE DESTINATION lib + LIBRARY DESTINATION lib + PUBLIC_HEADER DESTINATION include/faasm + ) endif () target_include_directories(faasm PUBLIC ${CMAKE_CURRENT_LIST_DIR}) diff --git a/libfaasm/faasm.imports b/libfaasm/libfaasm.imports similarity index 100% rename from libfaasm/faasm.imports rename to libfaasm/libfaasm.imports diff --git a/libfaasmp/CMakeLists.txt b/libfaasmp/CMakeLists.txt index d0a33527..3b251b1a 100644 --- a/libfaasmp/CMakeLists.txt +++ b/libfaasmp/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.8) project(libfaasmp) # ------ Faasm OpenMP library ------ # @@ -6,17 +6,20 @@ project(libfaasmp) set(CMAKE_CXX_STANDARD 17) set(PUBLIC_HEADERS - faasmp/faasmp.h - ) + faasmp/faasmp.h +) set(LIB_FILES - faasmp.cpp - ) + faasmp.cpp +) -if (CMAKE_SYSTEM_NAME STREQUAL "Wasm") +if (CMAKE_SYSTEM_NAME STREQUAL "WASI") message(STATUS "Libfaasmp WebAssembly build") + set(LLVM_OMP_SRC_DIR "/opt/llvm-project/openmp/runtime/src") + set(LLVM_OMP_BUILD_SRC_DIR "/opt/llvm-project/build/llvm/projects/openmp/runtime/src") set(LIBOMP_ASMFILES "z_Linux_asm.S") + set(LIBOMP_HEADERS "${LLVM_OMP_BUILD_SRC_DIR}/omp.h") set_source_files_properties(${LIBOMP_ASMFILES} PROPERTIES LANGUAGE C) set(LIB_FILES ${LIBOMP_ASMFILES} ${LIB_FILES}) @@ -24,21 +27,25 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Wasm") set_target_properties(faasmp PROPERTIES PUBLIC_HEADER "${PUBLIC_HEADERS}") # Install in WASM sysroot - install(TARGETS faasmp - ARCHIVE DESTINATION ${CMAKE_SYSROOT}/lib/wasm32-wasi - LIBRARY DESTINATION ${CMAKE_SYSROOT}/lib/wasm32-wasi - PUBLIC_HEADER DESTINATION ${CMAKE_SYSROOT}/include/faasmp - ) + install( + TARGETS faasmp + ARCHIVE DESTINATION $ENV{FAASM_WASM_LIB_INSTALL_DIR} + LIBRARY DESTINATION $ENV{FAASM_WASM_LIB_INSTALL_DIR} + PUBLIC_HEADER DESTINATION $ENV{FAASM_WASM_HEADER_INSTALL_DIR}/faasmmp + ) # Add imports - install(FILES faasmp.imports - DESTINATION ${CMAKE_SYSROOT}/lib/wasm32-wasi - RENAME libfaasmp.imports) - - # Add omp header - install(FILES omp.h - DESTINATION ${CMAKE_SYSROOT}/include) + install( + FILES libfaasmp.imports + DESTINATION $ENV{FAASM_WASM_LIB_INSTALL_DIR} + RENAME libfaasmp.imports + ) + # Add omp.h header copied from clang source + install( + FILES ${LIBOMP_HEADERS} + DESTINATION $ENV{FAASM_WASM_HEADER_INSTALL_DIR} + ) else () message(STATUS "Libfaasmp native build") @@ -50,9 +57,10 @@ else () set_target_properties(faasmp PROPERTIES PUBLIC_HEADER "${PUBLIC_HEADERS}") - install(TARGETS faasmp - ARCHIVE DESTINATION lib - LIBRARY DESTINATION lib - PUBLIC_HEADER DESTINATION include/faasmp - ) + install( + TARGETS faasmp + ARCHIVE DESTINATION lib + LIBRARY DESTINATION lib + PUBLIC_HEADER DESTINATION include/faasmp + ) endif () diff --git a/libfaasmp/README.md b/libfaasmp/README.md index a99b332b..5aa93b92 100644 --- a/libfaasmp/README.md +++ b/libfaasmp/README.md @@ -19,8 +19,8 @@ and the Faasm host interface. LLVM adds GNU compatibility data symbols on some features for synchronisation. The wasm linker needs to be aware of the sizes and the names of the symbols, which are not valid C/C++ identifiers, and as such are resolved in assembly in -`libfaasmp/z_Linux_asm.S` file. +`libfaasmp/z_Linux_asm.S` file. -An error from the linker when compiling new parts of the API implies you need to -you need to add the symbol to this file. +An error from the linker when compiling new parts of the API implies you need +to add the symbol to this file. diff --git a/libfaasmp/faasmp.imports b/libfaasmp/libfaasmp.imports similarity index 100% rename from libfaasmp/faasmp.imports rename to libfaasmp/libfaasmp.imports diff --git a/libfaasmpi/CMakeLists.txt b/libfaasmpi/CMakeLists.txt index 537cb1e2..880c8098 100644 --- a/libfaasmpi/CMakeLists.txt +++ b/libfaasmpi/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.8) project(faasmpi) # ------------------------------------------ @@ -31,48 +31,62 @@ set(FAABRIC_BINARY_DIR ${FAABRIC_SOURCE_DIR}/build) add_subdirectory(${FAABRIC_SOURCE_DIR} ${FAABRIC_BINARY_DIR}) add_dependencies(faasmpi faabricmpi) -if (CMAKE_SYSTEM_NAME STREQUAL "Wasm") +if (CMAKE_SYSTEM_NAME STREQUAL "WASI") message(STATUS "Libfaasmpi WebAssembly build") - install(FILES ${FAABRIC_BINARY_DIR}/libfaabricmpi.a - DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/wasm32-wasi + install( + FILES ${FAABRIC_BINARY_DIR}/libfaabricmpi.a + DESTINATION $ENV{FAASM_WASM_LIB_INSTALL_DIR} ) - install(TARGETS faasmpi - ARCHIVE DESTINATION ${CMAKE_SYSROOT}/lib/wasm32-wasi - LIBRARY DESTINATION ${CMAKE_SYSROOT}/lib/wasm32-wasi - PUBLIC_HEADER DESTINATION ${CMAKE_SYSROOT}/include/faasmpi + install( + TARGETS faasmpi + ARCHIVE DESTINATION $ENV{FAASM_WASM_LIB_INSTALL_DIR} + LIBRARY DESTINATION $ENV{FAASM_WASM_LIB_INSTALL_DIR} + PUBLIC_HEADER DESTINATION $ENV{FAASM_WASM_HEADER_INSTALL_DIR}/faasmmpi ) # Add imports file - install(FILES faasmpi.imports - DESTINATION ${CMAKE_SYSROOT}/lib/wasm32-wasi - RENAME libfaasmpi.imports + install( + FILES libfaasmpi.imports + DESTINATION $ENV{FAASM_WASM_LIB_INSTALL_DIR} + RENAME libfaasmpi.imports ) # Symlink library as libmpi - install(CODE "execute_process( \ + install( + CODE "execute_process( \ + COMMAND + ${CMAKE_COMMAND} -E create_symlink \ + $ENV{FAASM_WASM_LIB_INSTALL_DIR}/libfaabricmpi.a \ + $ENV{FAASM_WASM_LIB_INSTALL_DIR}/libmpi.a \ + )" + ) + + # Symlink mpi.h + install( + CODE "execute_process( \ COMMAND ${CMAKE_COMMAND} -E create_symlink \ - ${CMAKE_SYSROOT}/lib/wasm32-wasi/libfaabricmpi.a \ - ${CMAKE_SYSROOT}/lib/wasm32-wasi/libmpi.a \ + $ENV{FAASM_WASM_LIB_INSTALL_DIR}/include/faabric/mpi/mpi.h \ + $ENV{FAASM_WASM_HEADER_INSTALL_DIR}/mpi.h \ )" ) else() message(STATUS "Libfaasmpi native build") - install(TARGETS faasmpi - ARCHIVE DESTINATION lib - LIBRARY DESTINATION lib - PUBLIC_HEADER DESTINATION include/faasmpi - ) -endif() + install( + TARGETS faasmpi + ARCHIVE DESTINATION lib + LIBRARY DESTINATION lib + PUBLIC_HEADER DESTINATION include/faasmpi + ) -# Symlink header (common to wasm and native builds) -# WARNING - if you use the default CMAKE_INSTALL_PREFIX, this may overwrite the -# existing mpi.h on the system -install(CODE "execute_process( \ - COMMAND ${CMAKE_COMMAND} -E create_symlink \ - ${CMAKE_INSTALL_PREFIX}/include/faabric/mpi/mpi.h \ - ${CMAKE_INSTALL_PREFIX}/include/mpi.h \ - )" -) + # Symlink mpi.h + install( + CODE "execute_process( \ + COMMAND ${CMAKE_COMMAND} -E create_symlink \ + ${CMAKE_INSTALL_PREFIX}/include/faabric/mpi/mpi.h \ + ${CMAKE_INSTALL_PREFIX}/include/mpi.h \ + )" + ) +endif() diff --git a/libfaasmpi/faasmpi.imports b/libfaasmpi/libfaasmpi.imports similarity index 100% rename from libfaasmpi/faasmpi.imports rename to libfaasmpi/libfaasmpi.imports diff --git a/sysroot_extras/libc.imports b/sysroot_extras/libc.imports index 581cc36b..d0ef7134 100644 --- a/sysroot_extras/libc.imports +++ b/sysroot_extras/libc.imports @@ -99,11 +99,16 @@ pthread_cond_destroy pthread_cond_signal pthread_cond_timedwait pthread_cond_init +pthread_condattr_init +pthread_condattr_setclock pthread_equal pthread_mutexattr_init pthread_mutexattr_settype pthread_mutexattr_destroy +__tls_align __tls_base +__tls_size +__wasm_init_tls # Signals signal diff --git a/tasks/clapack.py b/tasks/clapack.py index 823df71d..2e4e2d43 100644 --- a/tasks/clapack.py +++ b/tasks/clapack.py @@ -1,18 +1,12 @@ from copy import copy from faasmtools.env import THIRD_PARTY_DIR, USABLE_CPUS -from faasmtools.build import ( - FAASM_BUILD_ENV_DICT, - WASM_SYSROOT, - WASM_LIB_INSTALL, -) +from faasmtools.build import get_faasm_build_env_dict from invoke import task from os import environ, remove from os.path import join, exists from shutil import rmtree from subprocess import run -HEADERS_DIRS = [join(WASM_SYSROOT, "include", "clapack")] - INSTALLED_LIBS = [ "libcblas", "libf2c", @@ -37,7 +31,7 @@ def build(ctx, clean=False, shared=False): # Set up environment to specify whether we're building static or shared env = copy(environ) env.update({"LIBEXT": ".so" if shared else ".a"}) - env.update(FAASM_BUILD_ENV_DICT) + env.update(get_faasm_build_env_dict()) # Make libf2c first (needed by others) run( @@ -70,14 +64,20 @@ def uninstall(ctx): """ Removes all installed files """ - for headers_dir in HEADERS_DIRS: + build_env = get_faasm_build_env_dict() + + for headers_dir in build_env["FAASM_WASM_HEADER_INSTALL_DIR"]: if exists(headers_dir): print("Removing headers {}".format(headers_dir)) rmtree(headers_dir) for lib_name in INSTALLED_LIBS: - static_path = join(WASM_LIB_INSTALL, "{}.a".format(lib_name)) - shared_path = join(WASM_LIB_INSTALL, "{}.so".format(lib_name)) + static_path = join( + build_env["FAASM_WASM_LIB_INSTALL_DIR"], "{}.a".format(lib_name) + ) + shared_path = join( + build_env["FAASM_WASM_LIB_INSTALL_DIR"], "{}.so".format(lib_name) + ) if exists(static_path): print("Removing static lib {}".format(static_path)) diff --git a/tasks/docker.py b/tasks/docker.py index 02cd0363..a38ee1f5 100644 --- a/tasks/docker.py +++ b/tasks/docker.py @@ -4,8 +4,9 @@ ACR_NAME, ) from faasmtools.env import ( - get_version, + LLVM_VERSION, PROJ_ROOT, + get_version, ) from invoke import task from os.path import join @@ -34,6 +35,7 @@ def build(ctx, c, nocache=False, push=False): build_args = {"SYSROOT_VERSION": get_version()} for ctr in c: if ctr == "llvm": + build_args["LLVM_VERSION_MAJOR"] = LLVM_VERSION.split(".")[0] dockerfile = LLVM_DOCKERFILE tag = get_llvm_tag() elif ctr == "cpp-sysroot": diff --git a/tasks/format_code.py b/tasks/format_code.py index cda26d83..f79a5d6d 100644 --- a/tasks/format_code.py +++ b/tasks/format_code.py @@ -1,3 +1,4 @@ +from faasmtools.env import LLVM_NATIVE_VERSION from invoke import task from tasks.util.env import PROJ_ROOT from subprocess import run @@ -51,8 +52,9 @@ def format(ctx, check=False): ) clang_cmd = [ - "clang-format-13", + "clang-format-{}".format(LLVM_NATIVE_VERSION.split(".")[0]), "--dry-run --Werror" if check else "-i", + "-style=file", " ".join(files_to_check), ] clang_cmd = " ".join(clang_cmd) diff --git a/tasks/func.py b/tasks/func.py index b9b29d46..2461cd6b 100644 --- a/tasks/func.py +++ b/tasks/func.py @@ -15,6 +15,14 @@ NATIVE_FUNC_BUILD_DIR = join(PROJ_ROOT, "build", "native-func") +def _is_threaded_func(user, func): + """ + Work out if the function requires using the wasm32-wasi or the + wasm32-wasi-threads target + """ + return user in ["threads", "omp"] + + def _get_all_user_funcs(user): # Work out all the functions for this user (that we assume will have been # built) @@ -64,7 +72,14 @@ def compile(ctx, user, func, clean=False, debug=False, native=False): ) else: # Build the function (gets written to the build dir) - wasm_cmake(FUNC_DIR, FUNC_BUILD_DIR, func, clean, debug) + wasm_cmake( + FUNC_DIR, + FUNC_BUILD_DIR, + func, + clean, + debug, + _is_threaded_func(user, func), + ) # Copy into place _copy_built_function(user, func) @@ -145,7 +160,14 @@ def user(ctx, user, clean=False, debug=False): """ # Build all funcs for this user (will fail if any builds fail) target = "{}_all_funcs".format(user) - wasm_cmake(FUNC_DIR, FUNC_BUILD_DIR, target, clean, debug) + wasm_cmake( + FUNC_DIR, + FUNC_BUILD_DIR, + target, + clean, + debug, + _is_threaded_func(user, ""), + ) funcs = _get_all_user_funcs(user) for f in funcs: @@ -161,3 +183,4 @@ def local(ctx, clean=False, debug=False): user(ctx, "errors", clean, debug) user(ctx, "mpi", clean, debug) user(ctx, "omp", clean, debug) + user(ctx, "threads", clean, debug) diff --git a/tasks/lib.py b/tasks/lib.py index fb3486f5..1e70f25a 100644 --- a/tasks/lib.py +++ b/tasks/lib.py @@ -1,29 +1,36 @@ from faasmtools.build import ( CMAKE_TOOLCHAIN_FILE, - FAASM_BUILD_ENV_DICT, FAASM_NATIVE_DIR, - WASM_SYSROOT, + get_faasm_build_env_dict, ) -from faasmtools.env import LLVM_VERSION, PROJ_ROOT +from faasmtools.env import LLVM_NATIVE_VERSION, PROJ_ROOT from os.path import exists, join from os import environ, makedirs from shutil import rmtree from subprocess import run -def build_faasm_lib(subdir, clean=False, native=False, shared=False): +def build_faasm_lib( + subdir, clean=False, native=False, shared=False, threads=False +): """ Builds one of the libraries included in this repo """ work_dir = join(PROJ_ROOT, subdir) - install_dir = FAASM_NATIVE_DIR if native else WASM_SYSROOT + install_dir = ( + FAASM_NATIVE_DIR + if native + else get_faasm_build_env_dict(threads)["FAASM_WASM_LIB_INSTALL_DIR"] + ) if native and shared: build_dir = "build-native-shared" elif native: build_dir = "build-native" + elif threads: + build_dir = "build-wasm32-wasi-threads" else: - build_dir = "build-wasm" + build_dir = "build-wasm32-wasi" build_dir = join(work_dir, build_dir) @@ -33,7 +40,7 @@ def build_faasm_lib(subdir, clean=False, native=False, shared=False): makedirs(build_dir, exist_ok=True) if native: - llvm_major = LLVM_VERSION.split(".")[0] + llvm_major = LLVM_NATIVE_VERSION.split(".")[0] extras = [ "-DCMAKE_C_COMPILER=/usr/bin/clang-{}".format(llvm_major), "-DCMAKE_CXX_COMPILER=/usr/bin/clang++-{}".format(llvm_major), @@ -62,7 +69,7 @@ def build_faasm_lib(subdir, clean=False, native=False, shared=False): work_env = environ.copy() if not native: - work_env.update(FAASM_BUILD_ENV_DICT) + work_env.update(get_faasm_build_env_dict(is_threads=threads)) run(build_cmd_str, shell=True, cwd=build_dir, check=True, env=work_env) run("ninja", shell=True, cwd=build_dir, check=True) diff --git a/tasks/libemscripten.py b/tasks/libemscripten.py index 2a034942..7f9e1072 100644 --- a/tasks/libemscripten.py +++ b/tasks/libemscripten.py @@ -3,8 +3,14 @@ @task(default=True) -def build(ctx, clean=False): +def build(ctx, clean=False, threads=False): """ Builds Faasm emscripten support library """ - build_faasm_lib("libemscripten", clean=clean, native=False, shared=False) + build_faasm_lib( + "libemscripten", + clean=clean, + native=False, + shared=False, + threads=threads, + ) diff --git a/tasks/libfaasm.py b/tasks/libfaasm.py index f2d78997..a39f2296 100644 --- a/tasks/libfaasm.py +++ b/tasks/libfaasm.py @@ -3,8 +3,10 @@ @task(default=True) -def build(ctx, clean=False, native=False, shared=False): +def build(ctx, clean=False, native=False, shared=False, threads=False): """ Builds faasm C/C++ lib """ - build_faasm_lib("libfaasm", clean=clean, native=native, shared=shared) + build_faasm_lib( + "libfaasm", clean=clean, native=native, shared=shared, threads=threads + ) diff --git a/tasks/libfaasmp.py b/tasks/libfaasmp.py index 6bb621ac..cd9527fd 100644 --- a/tasks/libfaasmp.py +++ b/tasks/libfaasmp.py @@ -7,4 +7,6 @@ def build(ctx, clean=False, native=False, shared=False): """ Builds Faasm OpenMP library """ - build_faasm_lib("libfaasmp", clean=clean, native=native, shared=shared) + build_faasm_lib( + "libfaasmp", clean=clean, native=native, shared=shared, threads=True + ) diff --git a/tasks/libfake.py b/tasks/libfake.py index c2ed6ceb..49137e8a 100644 --- a/tasks/libfake.py +++ b/tasks/libfake.py @@ -1,7 +1,6 @@ from faasmtools.build import ( CMAKE_TOOLCHAIN_FILE, - FAASM_BUILD_ENV_DICT, - WASM_SYSROOT, + get_faasm_build_env_dict, ) from faasmtools.env import PROJ_ROOT, FAASM_RUNTIME_ROOT from invoke import task @@ -18,6 +17,7 @@ def fake(ctx, clean=False): """ work_dir = join(PROJ_ROOT, "func", "dynlink") build_dir = join(PROJ_ROOT, "build", "libfake") + build_env = get_faasm_build_env_dict() if exists(build_dir) and clean: rmtree(build_dir) @@ -30,12 +30,12 @@ def fake(ctx, clean=False): "-DFAASM_BUILD_SHARED=ON", "-DCMAKE_TOOLCHAIN_FILE={}".format(CMAKE_TOOLCHAIN_FILE), "-DCMAKE_BUILD_TYPE=Release", - "-DCMAKE_INSTALL_PREFIX={}".format(WASM_SYSROOT), + "-DCMAKE_INSTALL_PREFIX={}".format(build_env["FAASM_WASM_SYSROOT"]), work_dir, ] work_env = environ.copy() - work_env.update(FAASM_BUILD_ENV_DICT) + work_env.update(get_faasm_build_env_dict()) run( " ".join(build_cmd), shell=True, @@ -47,7 +47,9 @@ def fake(ctx, clean=False): run("ninja install", shell=True, cwd=build_dir, check=True, env=work_env) # Copy shared object into place - sysroot_files = join(WASM_SYSROOT, "lib", "wasm32-wasi", "libfake*.so") + sysroot_files = join( + build_env["FAASM_WASM_LIB_INSTALL_DIR"], "libfake*.so" + ) runtime_lib_dir = join(FAASM_RUNTIME_ROOT, "lib", "fake") makedirs(runtime_lib_dir, exist_ok=True) diff --git a/tasks/libffi.py b/tasks/libffi.py index 87185f53..19b550ac 100644 --- a/tasks/libffi.py +++ b/tasks/libffi.py @@ -1,34 +1,40 @@ from faasmtools.build import ( build_config_cmd, + get_faasm_build_env_dict, run_autotools, - WASM_SYSROOT, - WASM_LIB_INSTALL, ) -from faasmtools.env import THIRD_PARTY_DIR, USABLE_CPUS, PROJ_ROOT +from faasmtools.env import THIRD_PARTY_DIR, USABLE_CPUS from invoke import task from os.path import join, exists from subprocess import run LIBFFI_DIR = join(THIRD_PARTY_DIR, "libffi") -LIBFFI_LIBS_DIR = join(LIBFFI_DIR, "wasm32-unknown-wasi") @task(default=True) -def build(ctx, clean=False): +def build(ctx, clean=False, threads=False): """ Builds libffi """ if exists(join(LIBFFI_DIR, "Makefile")) and clean: run("make clean", cwd=LIBFFI_DIR, shell=True, check=True) + build_env = get_faasm_build_env_dict(threads) + # Autotools - run_autotools(LIBFFI_DIR) + run_autotools(build_env, LIBFFI_DIR) # Configure configure_cmd = build_config_cmd( + build_env, [ "./configure", - "--prefix={}".format(WASM_SYSROOT), + "--build=wasm32", + "--host=wasm32-wasi", + "--includedir={}".format( + build_env["FAASM_WASM_HEADER_INSTALL_DIR"] + ), + "--libdir={}".format(build_env["FAASM_WASM_LIB_INSTALL_DIR"]), "--disable-multi-os-directory", "--disable-docs", ], @@ -42,16 +48,3 @@ def build(ctx, clean=False): # Run install run("make install", shell=True, check=True, cwd=LIBFFI_DIR) - - # Move lib into correct install dir - for lib_name in ["libffi.a", "libffi.la"]: - src_lib = join(WASM_SYSROOT, "lib", lib_name) - dest_lib = join(WASM_LIB_INSTALL, lib_name) - print("Moving {} to {}".format(src_lib, dest_lib)) - run("mv {} {}".format(src_lib, dest_lib), shell=True, check=True) - - # Copy imports into place - src_imports = join(PROJ_ROOT, "sysroot_extras", "libffi.imports") - dest_imports = join(WASM_LIB_INSTALL, "libffi.imports") - print("Copying {} to {}".format(src_imports, dest_imports)) - run("cp {} {}".format(src_imports, dest_imports), check=True, shell=True) diff --git a/tasks/llvm.py b/tasks/llvm.py index 1f35097d..35a77fd4 100644 --- a/tasks/llvm.py +++ b/tasks/llvm.py @@ -1,8 +1,7 @@ from faasmtools.build import ( - FAASM_BUILD_ENV_DICT, FAASM_LOCAL_DIR, - WASM_LIB_INSTALL, WASM_SYSROOT, + get_faasm_build_env_dict, ) from faasmtools.env import ( LLVM_DIR, @@ -13,10 +12,9 @@ WASI_LIBC_DIR, ) from invoke import task -from os import environ, listdir +from os import environ from os.path import exists, join from subprocess import run -from tasks.util.env import PATCHES_DIR LLVM_BUILD_DIR = join(LLVM_DIR, "build") LLVM_BUILD_ENV_VARS = { @@ -45,11 +43,6 @@ def do_llvm_clone(): git_cmd = " ".join(git_cmd) run(git_cmd, shell=True, check=True) - llvm_patches_dir = join(PATCHES_DIR, "llvm-project") - for patch in listdir(llvm_patches_dir): - git_cmd = "git apply {}".format(join(llvm_patches_dir, patch)) - run(git_cmd, shell=True, check=True, cwd=LLVM_DIR) - def do_llvm_build(target, clean_target=None): build_env = environ.copy() @@ -67,7 +60,7 @@ def do_llvm_build(target, clean_target=None): ) # These env. variables are to actually build LLVM - build_env.update(FAASM_BUILD_ENV_DICT) + build_env.update(get_faasm_build_env_dict()) make_cmd = [ "make", "-j {}".format(USABLE_CPUS), @@ -92,11 +85,9 @@ def build(ctx, clean=False): @task() def libs(ctx, clean=False): """ - Build the compiler-rt, libcxx, and libcxxabi for our LLVM fork + Build the compiler-rt, and libcxx for our LLVM fork """ - do_llvm_build( - "libcxx libcxxabi compiler-rt", "clean-libs" if clean else None - ) + do_llvm_build("libcxx compiler-rt", "clean-libs" if clean else None) @task() @@ -118,6 +109,8 @@ def libc(ctx, clean=False, purge=False): do_llvm_build("libc", clean_args) # Copy the import files into place - copy_cmd = "cp -r sysroot_extras/* {}".format(WASM_LIB_INSTALL) + copy_cmd = "cp -r sysroot_extras/* {}".format( + get_faasm_build_env_dict()["FAASM_WASM_LIB_INSTALL_DIR"] + ) print("\nCopying undefined symbols into place: \n{}".format(copy_cmd)) run(copy_cmd, check=True, shell=True, cwd=PROJ_ROOT) diff --git a/tasks/zlib.py b/tasks/zlib.py index 1f0ec2b4..0957e9b6 100644 --- a/tasks/zlib.py +++ b/tasks/zlib.py @@ -1,45 +1,35 @@ -from faasmtools.env import ( - THIRD_PARTY_DIR, -) -from faasmtools.build import ( - WASM_AR, - WASM_CC, - WASM_CFLAGS, - WASM_CXX, - WASM_LDFLAGS, - WASM_LD, - WASM_LIB_INSTALL, - WASM_NM, - WASM_RANLIB, - WASM_SYSROOT, -) +from faasmtools.env import THIRD_PARTY_DIR +from faasmtools.build import get_faasm_build_env_dict from invoke import task from os.path import join from subprocess import run @task(default=True) -def zlib(ctx, clean=False): +def zlib(ctx, clean=False, threads=False): """ - Compile and install libpng + Compile and install zlib """ zlib_dir = join(THIRD_PARTY_DIR, "zlib") if clean: run("make clean", shell=True, cwd=zlib_dir, check=True) + build_env = get_faasm_build_env_dict(threads) + configure_cmd = [ - "CC={}".format(WASM_CC), - "CXX={}".format(WASM_CXX), - "CFLAGS='{}'".format(" ".join(WASM_CFLAGS)), - "CXXFLAGS='{}'".format(" ".join(WASM_CFLAGS)), - "LD={}".format(WASM_LD), - "LDFLAGS='{}'".format(" ".join(WASM_LDFLAGS)), - "AR={}".format(WASM_AR), - "RANLIB={}".format(WASM_RANLIB), - "NM={}".format(WASM_NM), + "CC={}".format(build_env["FAASM_WASM_CC"]), + "CXX={}".format(build_env["FAASM_WASM_CXX"]), + "CFLAGS='{}'".format(build_env["FAASM_WASM_CFLAGS"]), + "CXXFLAGS='{}'".format(build_env["FAASM_WASM_CXXFLAGS"]), + "LD={}".format(build_env["FAASM_WASM_CC"]), + "LDFLAGS='{}'".format(build_env["FAASM_WASM_STATIC_LINKER_FLAGS"]), + "AR={}".format(build_env["FAASM_WASM_AR"]), + "RANLIB={}".format(build_env["FAASM_WASM_RANLIB"]), + "NM={}".format(build_env["FAASM_WASM_NM"]), "./configure", - "--prefix={}".format(WASM_SYSROOT), + "--includedir={}".format(build_env["FAASM_WASM_HEADER_INSTALL_DIR"]), + "--libdir={}".format(build_env["FAASM_WASM_LIB_INSTALL_DIR"]), "--static", ] @@ -49,8 +39,3 @@ def zlib(ctx, clean=False): # Only build the static library, not the examples nor tests run("make -j libz.a", shell=True, cwd=zlib_dir, check=True) run("make install", shell=True, cwd=zlib_dir, check=True) - - # Move the library from /usr/local/faasm/llvm-sysroot/lib into wasm32-wasi - mv_cmd = "mv {}/lib/libz.a {}".format(WASM_SYSROOT, WASM_LIB_INSTALL) - print(mv_cmd) - run(mv_cmd, shell=True, check=True) diff --git a/tests/test_input.cpp b/tests/test_input.cpp index a8621f9f..a881c5ec 100644 --- a/tests/test_input.cpp +++ b/tests/test_input.cpp @@ -25,7 +25,10 @@ TEST_CASE("Test string input", "[input]") std::string expected; std::string defaultVal = "default"; - SECTION("No input") { expected = defaultVal; } + SECTION("No input") + { + expected = defaultVal; + } SECTION("Input") { diff --git a/third-party/wasi-libc b/third-party/wasi-libc index 16fde82f..dceadb68 160000 --- a/third-party/wasi-libc +++ b/third-party/wasi-libc @@ -1 +1 @@ -Subproject commit 16fde82fe008aa2995cec65685a217cf469e4837 +Subproject commit dceadb68111821fc2ba9ba3a64b95463b21cd9db