Skip to content

Commit c2e39c9

Browse files
committed
Add target-prefixed executables to the installation bin directory
Currently the `bin` directory installed by wasi-sdk is not currently suitable for putting in `$PATH` in all cases because it can shadow a system-installed `clang` executable which is intended for native binaries. For Linux distributions with gcc-based cross-compilers I've often seen the pattern where they're installed as `$target-gcc` and so I've taken a leaf out of their books to do that here as well. This commit adds, currently alongside the preexisting `clang` executable, target-prefixed executables such as `wasm32-wasi-clang` and `wasm32-wasi-clang++`. These executables are symlinks to the `clang` executable itself in the same manner that `clang++` is a symlink to `clang` itself. I'll also note that this doesn't fix the problem of "add the wasi-sdk bin dir to PATH" because `clang` and other prominent executables are still there. My hope though is that this opens up a future refactoring for doing so.
1 parent c2f9c10 commit c2e39c9

File tree

3 files changed

+37
-24
lines changed

3 files changed

+37
-24
lines changed

Makefile

+18-3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ override LLVM_CMAKE_FLAGS += -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" \
4242
-DCMAKE_OSX_DEPLOYMENT_TARGET=10.12
4343
endif
4444

45+
TARGETS = wasm32-wasi wasm32-wasip1 wasm32-wasip2 wasm32-wasip1-threads wasm32-wasi-threads
46+
4547
# Only the major version is needed for Clang, see https://reviews.llvm.org/D125860.
4648
CLANG_VERSION=$(shell $(BASH) ./llvm_version_major.sh $(LLVM_PROJ_DIR))
4749
VERSION:=$(shell $(BASH) ./version.sh)
@@ -51,16 +53,29 @@ default: build
5153
@echo "Use -fdebug-prefix-map=$(ROOT_DIR)=wasisdk://v$(VERSION)"
5254

5355
check:
54-
CC="clang --sysroot=$(BUILD_PREFIX)/share/wasi-sysroot" \
55-
CXX="clang++ --sysroot=$(BUILD_PREFIX)/share/wasi-sysroot -fno-exceptions" \
56-
PATH="$(PATH_PREFIX)/bin:$$PATH" tests/run.sh "$(BUILD_PREFIX)" "$(RUNTIME)" "$(ADAPTER)" "$(WASM_TOOLS)"
56+
TARGETS="$(TARGETS)" tests/run.sh "$(BUILD_PREFIX)" "$(RUNTIME)" "$(ADAPTER)" "$(WASM_TOOLS)"
5757

5858
clean:
5959
rm -rf build $(DESTDIR)
6060

61+
# Default symlinks that clang creates to the `clang` executable
62+
CLANG_LINKS_TO_CREATE = clang++ clang-cl clang-cpp
63+
64+
# Add target-prefixed versions of `clang` and `clang++` so they can be used
65+
# without `--target` as it's auto-inferred from the executable name by clang.
66+
CLANG_LINKS_TO_CREATE += $(foreach target,$(TARGETS),$(target)-clang)
67+
CLANG_LINKS_TO_CREATE += $(foreach target,$(TARGETS),$(target)-clang++)
68+
69+
# Small helper to create a `join-with` function that can join elements of a
70+
# list with a defined separator.
71+
noop =
72+
space = $(noop) $(noop)
73+
join-with = $(subst $(space),$1,$(strip $2))
74+
6175
build/llvm.BUILT:
6276
mkdir -p build/llvm
6377
cd build/llvm && cmake -G Ninja \
78+
-DCLANG_LINKS_TO_CREATE="$(call join-with,;,$(CLANG_LINKS_TO_CREATE))" \
6479
-DCMAKE_BUILD_TYPE=MinSizeRel \
6580
-DLLVM_ENABLE_TERMINFO=OFF \
6681
-DLLVM_ENABLE_ZLIB=OFF \

tests/run.sh

+16-18
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@ set -ueo pipefail
66
# <runwasi> is a WASI-capable runtime to run the tests in full compile and
77
# execute mode.
88
#
9-
# By default this script will look for `clang` and `clang++` in $PATH and
10-
# assume that they are correctly configured with the sysroot in the default
11-
# location. Alternatively, exporting $CC and $CXX allow more flexibility. e.g:
12-
#
13-
# export CXX="<wasi-sdk>/bin/clang++ --sysroot <wasi-sdk>/share/wasi-sysroot"
14-
# export CC="<wasi-sdk>/bin/clang --sysroot <wasi-sdk>/share/wasi-sysroot"
15-
#
9+
# The compiler used during testing is loaded from `<path to wasi-sdk>`.
1610
if [ $# -lt 1 ]; then
1711
echo "Path to WASI SDK is required"
1812
exit 1
@@ -37,44 +31,48 @@ else
3731
fi
3832

3933
testdir=$(dirname $0)
40-
CC=${CC:=clang}
41-
CXX=${CXX:=clang++}
4234

43-
echo $CC
44-
echo $CXX
4535
echo "SDK: $wasi_sdk"
4636

47-
for target in wasm32-wasi wasm32-wasip1 wasm32-wasi-threads wasm32-wasip1-threads wasm32-wasip2; do
37+
# NB: all tests are run with the default `clang` and `clang++` executables
38+
# but they're also executed with the target-prefixed `clang` executables to
39+
# ensure that those work as well when the `--target` option is omitted.
40+
41+
for target in $TARGETS; do
4842
echo "===== Testing target $target ====="
4943
cd $testdir/compile-only
5044
for options in -O0 -O2 "-O2 -flto"; do
5145
echo "===== Testing compile-only with $options ====="
5246
for file in *.c; do
5347
echo "Testing compile-only $file..."
54-
../testcase.sh "$target" "" "" "" "$CC" "$options" "$file"
48+
../testcase.sh "$target" "" "" "" "$wasi_sdk/bin/clang" "$options --target=$target" "$file"
49+
../testcase.sh "$target" "" "" "" "$wasi_sdk/bin/$target-clang" "$options" "$file"
5550
done
5651
for file in *.cc; do
5752
echo "Testing compile-only $file..."
58-
../testcase.sh "$target" "" "" "" "$CXX" "$options" "$file"
53+
../testcase.sh "$target" "" "" "" "$wasi_sdk/bin/clang++" "$options --target=$target -fno-exceptions" "$file"
54+
../testcase.sh "$target" "" "" "" "$wasi_sdk/bin/$target-clang++" "$options -fno-exceptions" "$file"
5955
done
6056
done
6157
cd - >/dev/null
62-
58+
6359
cd $testdir/general
6460
for options in -O0 -O2 "-O2 -flto"; do
6561
echo "===== Testing with $options ====="
6662
for file in *.c; do
6763
echo "Testing $file..."
68-
../testcase.sh "$target" "$runwasi" "$adapter" "$wasm_tools" "$CC" "$options" "$file"
64+
../testcase.sh "$target" "$runwasi" "$adapter" "$wasm_tools" "$wasi_sdk/bin/clang" "$options --target=$target" "$file"
65+
../testcase.sh "$target" "$runwasi" "$adapter" "$wasm_tools" "$wasi_sdk/bin/$target-clang" "$options" "$file"
6966
done
7067
for file in *.cc; do
7168
echo "Testing $file..."
72-
../testcase.sh "$target" "$runwasi" "$adapter" "$wasm_tools" "$CXX" "$options" "$file"
69+
../testcase.sh "$target" "$runwasi" "$adapter" "$wasm_tools" "$wasi_sdk/bin/clang++" "$options --target=$target -fno-exceptions" "$file"
70+
../testcase.sh "$target" "$runwasi" "$adapter" "$wasm_tools" "$wasi_sdk/bin/$target-clang++" "$options -fno-exceptions" "$file"
7371
done
7472
done
7573
cd - >/dev/null
7674
done
77-
75+
7876
# Test cmake build system for wasi-sdk
7977
test_cmake() {
8078
local option

tests/testcase.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fi
3737
echo "Testing $input..."
3838

3939
# Compile the testcase.
40-
$compiler --target=$target $pthread_options $options $file_options "$input" -o "$wasm"
40+
$compiler $pthread_options $options $file_options "$input" -o "$wasm"
4141

4242
# If we don't have a runwasi command, we're just doing compile-only testing.
4343
if [ "$runwasi" == "" ]; then
@@ -87,7 +87,7 @@ if [ -e "$input.stdout.expected" ]; then
8787
stdout_expected="$input.$target.stdout.expected"
8888
else
8989
stdout_expected="$input.stdout.expected"
90-
fi
90+
fi
9191

9292
# Apply output filters.
9393
if [ -e "$input.stdout.expected.filter" ]; then
@@ -105,7 +105,7 @@ if [ -e "$input.stderr.expected" ]; then
105105
stderr_expected="$input.$target.stderr.expected"
106106
else
107107
stderr_expected="$input.stderr.expected"
108-
fi
108+
fi
109109

110110
# Apply output filters.
111111
if [ -e "$input.stderr.expected.filter" ]; then

0 commit comments

Comments
 (0)