|
| 1 | +PROJECT_DIR := $(realpath .) |
| 2 | +BUILD_DIR := $(PROJECT_DIR)/build |
| 3 | + |
| 4 | +# Specify a different wasi-sdk with `make WASI_SDK_DIR=...`. |
| 5 | +WASI_SDK_DIR := /opt/wasi-sdk/latest |
| 6 | +WASI_CC := $(WASI_SDK_DIR)/bin/clang |
| 7 | +WASI_SYSROOT := $(WASI_SDK_DIR)/share/wasi-sysroot |
| 8 | +TARGET_FLAGS=--target=wasm32-wasi-threads -pthread |
| 9 | +WASI_LIBC_FLAGS=-D_WASI_EMULATED_SIGNAL -D_WASI_EMULATED_PROCESS_CLOCKS |
| 10 | +SYSROOT_FLAGS=--sysroot=$(WASI_SYSROOT) |
| 11 | + |
| 12 | +# Specify a different location for the OpenMP sources with `make OPENMP_DIR=...`. |
| 13 | +OPENMP_DIR := $(PROJECT_DIR)/llvm-project/openmp |
| 14 | +OPENMP_LIB := $(BUILD_DIR)/runtime/src/libomp.a |
| 15 | + |
| 16 | +# Specify a different Wasm engine to invoke with `make WASM_ENGINE=...`. |
| 17 | +WASM_ENGINE=WASMTIME_LOG=wasmtime_runtime::memory=trace,wasmtime_wasi_threads=trace ../wasmtime/target/release/wasmtime |
| 18 | + |
| 19 | +# The default target--builds and runs everything. |
| 20 | +all: run |
| 21 | + |
| 22 | +# Run the compiled example in a WebAssembly engine. |
| 23 | +run: example.wasm |
| 24 | + $(WASM_ENGINE) -W threads -S threads $^ |
| 25 | + |
| 26 | +# Compile the example; note how we will need the `libomp.a` runtime. |
| 27 | +example.wasm: example.c $(OPENMP_LIB) |
| 28 | + make check-wasi-sdk |
| 29 | + $(WASI_CC) -fopenmp=libomp -g --sysroot=$(WASI_SYSROOT) --target=wasm32-wasi-threads \ |
| 30 | + -I$(BUILD_DIR)/runtime/src -I$(WASI_SYSROOT)/include -pthread \ |
| 31 | + -Wl,--import-memory,--export-memory,--max-memory=67108864 example.c \ |
| 32 | + -L$(BUILD_DIR)/runtime/src -lomp \ |
| 33 | + -lwasi-emulated-getpid \ |
| 34 | + -o example.wasm |
| 35 | + |
| 36 | +# Compile the `libomp.a` runtime library to WebAssembly. This uses the PR changes from |
| 37 | +# https://github.com/llvm/llvm-project/pull/71297. |
| 38 | +libomp: $(OPENMP_LIB) |
| 39 | +$(OPENMP_LIB): $(OPENMP_DIR) |
| 40 | + make check-wasi-sdk |
| 41 | + cmake \ |
| 42 | + -DCMAKE_BUILD_TYPE=Debug \ |
| 43 | + -DCMAKE_C_COMPILER="$(WASI_SDK_DIR)/bin/clang" \ |
| 44 | + -DCMAKE_C_FLAGS="$(TARGET_FLAGS) $(WASI_LIBC_FLAGS) $(SYSROOT_FLAGS)" \ |
| 45 | + -DCMAKE_CXX_COMPILER="$(WASI_SDK_DIR)/bin/clang++" \ |
| 46 | + -DCMAKE_CXX_FLAGS="$(TARGET_FLAGS) $(WASI_LIBC_FLAGS) $(SYSROOT_FLAGS)" \ |
| 47 | + -DCMAKE_LINKER="${WASI_SDK}/bin/ld.lld" \ |
| 48 | + -B $(BUILD_DIR) \ |
| 49 | + -S $(OPENMP_DIR) |
| 50 | + cmake --build $(BUILD_DIR) |
| 51 | +$(OPENMP_DIR): |
| 52 | + $(error cannot find $@; retrieve the submodule with `git submodule update --init` or rerun with `make OPENMP_DIR=...`) |
| 53 | + |
| 54 | +# Check that the parts of wasi-sdk we need are present; if they're not, fail. |
| 55 | +check-wasi-sdk: $(WASI_CC) $(WASI_SYSROOT) |
| 56 | +$(WASI_CC) $(WASI_SYSROOT): |
| 57 | + $(error cannot find $@; install wasi-sdk from https://github.com/WebAssembly/wasi-sdk/releases and rerun with `make WASI_SDK_DIR=...`) |
| 58 | + |
| 59 | +clean: |
| 60 | + rm -f *.wasm |
| 61 | + rm -rf $(BUILD_DIR) |
0 commit comments