Skip to content

Commit e9527e7

Browse files
committed
build: add recipes for installing WASI libc
1 parent f2d01d6 commit e9527e7

File tree

7 files changed

+257
-7
lines changed

7 files changed

+257
-7
lines changed

tools/make/common.mk

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -496,11 +496,23 @@ DEPS_LLVM_BUILD_OUT ?= $(DEPS_BUILD_DIR)/llvm
496496
# Define the LLVM version:
497497
DEPS_LLVM_VERSION ?=
498498

499-
# Define the path to clang compiler:
499+
# Define the path to the LLVM clang compiler:
500500
DEPS_LLVM_CLANG ?= $(DEPS_LLVM_BUILD_OUT)/build/bin/clang
501501

502-
# Define the path to LLVM static compiler:
503-
DEPS_LLVM_LLC ?= $(DEPS_LLVM_BUILD_OUT)/build/bin/llc
502+
# Define the path to the LLVM archiver:
503+
DEPS_LLVM_AR ?= $(DEPS_LLVM_BUILD_OUT)/build/bin/llvm-ar
504+
505+
# Define the path to the LLVM tool for listing LLVM bitcode and object file symbol tables:
506+
DEPS_LLVM_NM ?= $(DEPS_LLVM_BUILD_OUT)/build/bin/llvm-nm
507+
508+
# Define the output path when building WASI libc:
509+
DEPS_WASI_LIBC_BUILD_OUT ?= $(DEPS_BUILD_DIR)/wasi-libc
510+
511+
# Define the WASI libc version:
512+
DEPS_WASI_LIBC_VERSION ?=
513+
514+
# Define the path to WASI libc sysroot:
515+
DEPS_WASI_LIBC_SYSROOT ?= $(DEPS_WASI_LIBC_BUILD_OUT)/sysroot
504516

505517
# Define the output path when building the Emscripten SDK:
506518
DEPS_EMSDK_BUILD_OUT ?= $(DEPS_BUILD_DIR)/emsdk

tools/make/lib/install/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ include $(TOOLS_MAKE_LIB_DIR)/install/python_deps.mk
4646
include $(TOOLS_MAKE_LIB_DIR)/install/r_deps.mk
4747
include $(TOOLS_MAKE_LIB_DIR)/install/shellcheck.mk
4848
include $(TOOLS_MAKE_LIB_DIR)/install/wabt.mk
49+
include $(TOOLS_MAKE_LIB_DIR)/install/wasi_libc.mk
4950

5051

5152
# RULES #

tools/make/lib/install/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ This directory contains [`make`][make] rules for running the project's installat
4343
- [R](#r)
4444
- [ShellCheck](#shellcheck)
4545
- [WebAssembly Binary Toolkit](#wabt)
46+
- [WASI libc](#wasi-libc)
4647

4748
</section>
4849

@@ -598,6 +599,36 @@ Removes [WebAssembly Binary Toolkit][wabt] installation tests.
598599
$ make clean-deps-wabt-tests
599600
```
600601

602+
* * *
603+
604+
<a name="wasi-libc"></a>
605+
606+
### WASI libc
607+
608+
#### install-deps-wasi-libc
609+
610+
Installs a [WASI libc][wasi-libc] implementation for WebAssembly.
611+
612+
```bash
613+
$ make install-deps-wasi-libc
614+
```
615+
616+
#### clean-deps-wasi-libc
617+
618+
Removes an installed [WASI libc][wasi-libc] distribution.
619+
620+
```bash
621+
$ make clean-deps-wasi-libc
622+
```
623+
624+
#### clean-deps-wasi-libc-tests
625+
626+
Removes [WASI libc][wasi-libc] installation tests.
627+
628+
```bash
629+
$ make clean-deps-wasi-libc-tests
630+
```
631+
601632
</section>
602633

603634
<!-- /.usage -->
@@ -638,6 +669,8 @@ $ make clean-deps-wabt-tests
638669

639670
[wabt]: https://github.com/WebAssembly/wabt
640671

672+
[wasi-libc]: https://github.com/WebAssembly/wasi-libc
673+
641674
</section>
642675

643676
<!-- /.links -->

tools/make/lib/install/wabt.mk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ deps-install-wabt: $(DEPS_WABT_BUILD_OUT) deps-prerequisites-wabt
170170
.PHONY: deps-install-wabt
171171

172172
#/
173-
# Updates the WebAssemby Binary Toolkit (WABT).
173+
# Updates the WebAssembly Binary Toolkit (WABT).
174174
#
175175
# @private
176176
#
@@ -204,7 +204,7 @@ deps-test-wabt: $(DEPS_WABT_TEST_INSTALL_WAT_OUT) $(DEPS_WABT_TEST_INSTALL_WASM_
204204
.PHONY: deps-test-wabt
205205

206206
#/
207-
# Installs the WebAsembly Binary Toolkit (WABT).
207+
# Installs the WebAssembly Binary Toolkit (WABT).
208208
#
209209
# @example
210210
# make install-deps-wabt

tools/make/lib/install/wasi_libc.mk

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
#/
2+
# @license Apache-2.0
3+
#
4+
# Copyright (c) 2024 The Stdlib Authors.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#/
18+
19+
# VARIABLES #
20+
21+
# Define the path to an executable for checking git:
22+
DEPS_CHECK_GIT ?= $(TOOLS_DIR)/scripts/check_git
23+
24+
# Define the download URL:
25+
DEPS_WASI_LIBC_URL ?= https://github.com/WebAssembly/wasi-libc
26+
27+
# Determine the basename for the download:
28+
deps_wasi_libc_basename := wasi_libc
29+
30+
# Define the path to the file containing a checksum to verify a download:
31+
DEPS_WASI_LIBC_CHECKSUM ?= $(shell $(CAT) $(DEPS_CHECKSUMS_DIR)/$(subst .,_,$(deps_wasi_libc_basename))/sha256)
32+
33+
# Define the output path when downloading:
34+
DEPS_WASI_LIBC_DOWNLOAD_OUT ?= $(DEPS_TMP_DIR)/$(deps_wasi_libc_basename)
35+
36+
# Define the output path after extracting:
37+
deps_wasi_libc_extract_out := $(DEPS_BUILD_DIR)/wasi_libc_extracted
38+
39+
40+
# RULES #
41+
42+
#/
43+
# Downloads WASI libc.
44+
#
45+
# @private
46+
#/
47+
$(DEPS_WASI_LIBC_DOWNLOAD_OUT): | $(DEPS_TMP_DIR)
48+
$(QUIET) echo 'Downloading WASI libc...' >&2
49+
$(QUIET) $(GIT) clone --depth=1 $(DEPS_WASI_LIBC_URL) $(DEPS_WASI_LIBC_DOWNLOAD_OUT)
50+
51+
#/
52+
# Extracts a WASI libc download.
53+
#
54+
# @private
55+
#/
56+
$(DEPS_WASI_LIBC_BUILD_OUT): | $(DEPS_BUILD_DIR) $(DEPS_WASI_LIBC_DOWNLOAD_OUT)
57+
$(QUIET) echo 'Extracting WASI libc...' >&2
58+
$(QUIET) $(CP) -a $(DEPS_WASI_LIBC_DOWNLOAD_OUT) $(deps_wasi_libc_extract_out)
59+
$(QUIET) mv $(deps_wasi_libc_extract_out) $(DEPS_WASI_LIBC_BUILD_OUT)
60+
61+
#/
62+
# Compiles a WASI libc sysroot.
63+
#
64+
# @private
65+
#/
66+
$(DEPS_WASI_LIBC_SYSROOT): $(DEPS_LLVM_CLANG) $(DEPS_LLVM_AR) $(DEPS_LLVM_NM) $(DEPS_WASI_LIBC_BUILD_OUT)
67+
$(QUIET) cd $(DEPS_WASI_LIBC_BUILD_OUT) && \
68+
$(MAKE) CC="$(DEPS_LLVM_CLANG)" AR="$(DEPS_LLVM_AR)" NM="$(DEPS_LLVM_NM)"
69+
70+
#/
71+
# Downloads WASI libc.
72+
#
73+
# @private
74+
#
75+
# @example
76+
# make deps-download-wasi-libc
77+
#/
78+
deps-download-wasi-libc: $(DEPS_WASI_LIBC_DOWNLOAD_OUT)
79+
80+
.PHONY: deps-download-wasi-libc
81+
82+
#/
83+
# Verifies a WASI libc download.
84+
#
85+
# @private
86+
#
87+
# @example
88+
# make deps-verify-wasi-libc
89+
#/
90+
deps-verify-wasi-libc: deps-download-wasi-libc
91+
$(QUIET) echo 'Verifying download...' >&2
92+
$(QUIET) echo 'Nothing to verify.' >&2
93+
94+
.PHONY: deps-verify-wasi-libc
95+
96+
#/
97+
# Extracts a WASI libc download.
98+
#
99+
# @private
100+
#
101+
# @example
102+
# make deps-extract-wasi-libc
103+
#/
104+
deps-extract-wasi-libc: $(DEPS_WASI_LIBC_BUILD_OUT)
105+
106+
.PHONY: deps-extract-wasi-libc
107+
108+
#/
109+
# Checks a host system for WASI libc installation prerequisites.
110+
#
111+
# @private
112+
#
113+
# @example
114+
# make deps-prerequisites-wasi-libc
115+
#/
116+
deps-prerequisites-wasi-libc:
117+
$(QUIET) $(DEPS_CHECK_GIT)
118+
119+
.PHONY: deps-prerequisites-wasi-libc
120+
121+
#/
122+
# Installs WASI libc.
123+
#
124+
# @private
125+
#
126+
# @example
127+
# make deps-install-wasi-libc
128+
#/
129+
deps-install-wasi-libc: deps-prerequisites-wasi-libc $(DEPS_WASI_LIBC_BUILD_OUT) $(DEPS_WASI_LIBC_SYSROOT)
130+
131+
.PHONY: deps-install-wasi-libc
132+
133+
#/
134+
# Updates WASI libc.
135+
#
136+
# @private
137+
#
138+
# @example
139+
# make deps-update-wasi-libc
140+
#/
141+
deps-update-wasi-libc: deps-install-wasi-libc
142+
143+
.PHONY: deps-update-wasi-libc
144+
145+
#/
146+
# Tests a WASI libc installation.
147+
#
148+
# @private
149+
#
150+
# @example
151+
# make deps-test-wasi-libc
152+
#/
153+
deps-test-wasi-libc:
154+
$(QUIET) echo '' >&2
155+
$(QUIET) test -d $(DEPS_WASI_LIBC_SYSROOT) >&2
156+
$(QUIET) echo '' >&2
157+
$(QUIET) echo 'Success.' >&2
158+
159+
.PHONY: deps-test-wasi-libc
160+
161+
#/
162+
# Installs WASI libc.
163+
#
164+
# @example
165+
# make install-deps-wasi-libc
166+
#/
167+
install-deps-wasi-libc: deps-download-wasi-libc deps-verify-wasi-libc deps-extract-wasi-libc deps-install-wasi-libc deps-test-wasi-libc
168+
169+
.PHONY: install-deps-wasi-libc
170+
171+
#/
172+
# Removes a WASI libc install (but does not remove a download if one exists).
173+
#
174+
# @example
175+
# make clean-deps-wasi-libc
176+
#/
177+
clean-deps-wasi-libc: clean-deps-wasi-libc-tests
178+
$(QUIET) $(DELETE) $(DELETE_FLAGS) $(DEPS_WASI_LIBC_BUILD_OUT)
179+
180+
.PHONY: clean-deps-wasi-libc
181+
182+
#/
183+
# Removes WASI libc installation tests.
184+
#
185+
# @example
186+
# make clean-deps-wasi-libc-tests
187+
#/
188+
clean-deps-wasi-libc-tests:
189+
$(QUIET) echo '' >&2
190+
191+
.PHONY: clean-deps-wasi-libc-tests

tools/make/lib/wasm/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ wasm: $(NODE_MODULES)
5959
NODE="$(NODE)" \
6060
NODE_PATH="$(NODE_PATH)" \
6161
SRC_FOLDER="$(SRC_FOLDER)" \
62+
CLANG_COMPILER="$(DEPS_LLVM_CLANG)" \
63+
CLANG_SYSROOT="$(DEPS_WASI_LIBC_SYSROOT)" \
6264
EMCC_COMPILER="$(DEPS_EMSDK_EMSCRIPTEN_EMCC)" \
6365
WASM2WAT="$(DEPS_WABT_WASM2WAT)" \
6466
WASM2JS="$(DEPS_EMSDK_EMSCRIPTEN_WASM2JS)" \

tools/scripts/compile_wasm

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
#
3030
# NODE Command for running Node.js. Default: `node`.
3131
# NODE_PATH Path for resolving node modules.
32-
# EMCCC_COMPILER Emscripten C compiler. Default: `emcc`.
32+
# CLANG_COMPILER LLVM Clang compiler. Default: `clang`.
33+
# CLANG_SYSROOT Path to a C standard library.
34+
# EMCC_COMPILER Emscripten C compiler. Default: `emcc`.
3335
# WASM2WAT Command for converting WebAssembly binary format to text format. Default. `wasm2wat`.
3436
# WASM2JS Command for converting WebAssembly binary format to JavaScript. Default. `wasm2js`.
3537
# SRC_FOLDER Folder containing source files. Default: `src`.
@@ -56,6 +58,12 @@ fi
5658
# Define the node path:
5759
node_path="${NODE_PATH}"
5860

61+
# Define the path to the LLVM `clang` compiler:
62+
clang_compiler="${CLANG_COMPILER}"
63+
if [[ -z "${clang_compiler}" ]]; then
64+
clang_compiler='clang'
65+
fi
66+
5967
# Define the path to the Emscripten `emcc` compiler:
6068
emcc_compiler="${EMCC_COMPILER}"
6169
if [[ -z "${emcc_compiler}" ]]; then
@@ -80,6 +88,9 @@ if [[ -z "${src_folder}" ]]; then
8088
src_folder='src'
8189
fi
8290

91+
# Define a path to a C standard library:
92+
clang_sysroot="${CLANG_SYSROOT}"
93+
8394
# Define a list of external `include` directories:
8495
include="${INCLUDE}"
8596

@@ -201,7 +212,7 @@ resolve_libpaths() {
201212
#
202213
# $1 - source directory
203214
compile() {
204-
cd "$1" && EMCC_COMPILER="${emcc_compiler}" WASM2WAT="${wasm_to_wat}" WASM2JS="${wasm_to_js}" INCLUDE="${include}" SOURCE_FILES="${source_files}" LIBRARIES="${libraries}" LIBPATH="${libpath}" make wasm 2>&1
215+
cd "$1" && CLANG_COMPILER="${clang_compiler}" CLANG_SYSROOT="${clang_sysroot}" EMCC_COMPILER="${emcc_compiler}" WASM2WAT="${wasm_to_wat}" WASM2JS="${wasm_to_js}" INCLUDE="${include}" SOURCE_FILES="${source_files}" LIBRARIES="${libraries}" LIBPATH="${libpath}" make wasm 2>&1
205216
if [[ "$?" -ne 0 ]]; then
206217
echo 'Error when attempting to compile WebAssembly.' >&2
207218
return 1

0 commit comments

Comments
 (0)