Skip to content

Commit 90cf7f6

Browse files
authored
Latest wasi-libc (#109)
* gh: bump code version * llvm: bump wasi-libc to latest commit * wasi-libc: rebase to latest commit cross-compiling all functions * makefile: update toolchain file to use ldflags w/out quotes in clapack * cmake: add linker flags in wasi toolchain * faasmtools: set the compilationflags and linking flags required to use wasi with emulation * func: set wasi emulated libs in toolchain * zlib: only build static library * clapack and zlib: fix compilation with new wasi-libc * docker: tag llvm image with cpp tag as well * libc: changes to cross-compile cpython * wasi-libc: more changes * wasi-libc: more updates * libc: more changes * wasi-libc: emulated mman * libfake: fix task * func: fix calloc function by passing the right flags to mmap (as required by wasi-libc's calloc implementation * dynlink: temporarily disable one failing check * func: remove PROT_EXEC flag from mmap as it is not supported in wasi-libc * clang-format * wasi-libc: add --purge flag to clean the sysroot and apply patches * wasi-libc: fix patch * docker: make sure to re-install llvm headers after purge * py: make linter happy * wasi-libc: more cleanup * wasi-libc: remove patch-oritented style * cleanup * wasi-libc: prepare for merge * nits: self-review * dynlink: undo patch
1 parent 7198459 commit 90cf7f6

20 files changed

+81
-25
lines changed

.env

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
SYSROOT_VERSION=0.2.1
2-
SYSROOT_CLI_IMAGE=faasm/cpp-sysroot:0.2.1
1+
SYSROOT_VERSION=0.2.2
2+
SYSROOT_CLI_IMAGE=faasm/cpp-sysroot:0.2.2
33
COMPOSE_PROJECT_NAME=cpp-dev

.github/workflows/tests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
if: github.event.pull_request.draft == false
2020
runs-on: ubuntu-latest
2121
container:
22-
image: faasm/cpp-sysroot:0.2.1
22+
image: faasm/cpp-sysroot:0.2.2
2323
steps:
2424
# --- Update code ---
2525
- name: "Checkout code"

LLVM.makefile

+8-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ default: build
2929
clean-libc:
3030
rm -rf $(BUILD_DIR)/libc.BUILT $(WASI_LIBC_DIR)/build
3131

32+
.PHONY: very-clean-libc
33+
very-clean-libc:
34+
# WARNING: this is going to remove _everything_ in FAASM_SYSROOT
35+
cd $(WASI_LIBC_DIR) && SYSROOT=$(FAASM_SYSROOT) make clean
36+
3237
.PHONY: clean-libs
3338
clean-libs: clean-libc
3439
rm -rf $(BUILD_DIR)/compiler-rt $(BUILD_DIR)/compiler-rt.BUILT
@@ -72,9 +77,9 @@ $(BUILD_DIR)/libc.BUILT: $(BUILD_DIR)/llvm.BUILT
7277
mkdir -p $(WASI_LIBC_DIR)/build
7378
cd $(WASI_LIBC_DIR); $(MAKE) \
7479
THREAD_MODEL=faasm \
75-
WASM_CC=$(PREFIX)/bin/clang \
76-
WASM_AR=$(PREFIX)/bin/llvm-ar \
77-
WASM_NM=$(PREFIX)/bin/llvm-nm \
80+
CC=$(PREFIX)/bin/clang \
81+
AR=$(PREFIX)/bin/llvm-ar \
82+
NM=$(PREFIX)/bin/llvm-nm \
7883
SYSROOT=$(FAASM_SYSROOT)
7984
touch $(BUILD_DIR)/libc.BUILT
8085

Makefile.envs

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export WASM_RANLIB=${FAASM_WASM_RANLIB}
1616
export WASM_LD=${FAASM_WASM_CC}
1717
export WASM_LDSHARED=${FAASM_WASM_CC}
1818
export WASM_LDFLAGS="${FAASM_WASM_STATIC_LINKER_FLAGS}"
19+
export WASM_LDFLAGS_NO_QUOTES=${FAASM_WASM_STATIC_LINKER_FLAGS}
1920
export WASM_EXE_LDFLAGS="${FAASM_WASM_EXE_LINKER_FLAGS}"
2021
export WASM_EXE_LDFLAGS_NO_QUOTES=${FAASM_WASM_EXE_LINKER_FLAGS}
2122

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.2.1
1+
0.2.2

WasiToolchain.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ endif()
5858
set(CMAKE_SYSROOT ${FAASM_SYSROOT} CACHE STRING "faasm build")
5959
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${FAASM_COMPILER_FLAGS}" CACHE STRING "faasm build")
6060
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FAASM_COMPILER_FLAGS}" CACHE STRING "faasm build")
61+
set(CMAKE_LINKER_FLAGS $ENV{FAASM_WASM_LINKER_FLAGS} CACHE STRING "faasm build")
6162
set(CMAKE_SHARED_LINKER_FLAGS $ENV{FAASM_WASM_SHARED_LINKER_FLAGS} CACHE STRING "faasm build")
6263
set(CMAKE_EXE_LINKER_FLAGS $ENV{FAASM_WASM_EXE_LINKER_FLAGS} CACHE STRING "faasm build")
6364

docker/cpp-sysroot.dockerfile

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
FROM faasm/llvm:13.0.1 as llvm
1+
# llvm image is not re-built often, so the tag may be behind
2+
FROM faasm/llvm:0.2.2 as llvm
23

34
# faabric-base image is not re-built often, so tag may be behind
45
FROM faasm/faabric-base:0.4.1
@@ -44,8 +45,9 @@ RUN cd /code/cpp \
4445
libfaasmpi --native --shared \
4546
# Install toolchain files
4647
&& inv install \
47-
# First build libc
48-
&& inv llvm.libc \
48+
# Build wasi-libc and reset the sysroot. The second call to LLVM just
49+
# installs some headers that are purged
50+
&& inv llvm.libc --purge llvm \
4951
# Build Faasm WASM libraries
5052
&& inv \
5153
libemscripten \

faasmtools/build.py

+24
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@
8181
"-D__faasm",
8282
]
8383

84+
# Wasi-libc specific flags that are needed to include certain emulation headers
85+
WASM_WASI_LIBC_CFLAGS = [
86+
"-D_WASI_EMULATED_GETPID",
87+
"-D_WASI_EMULATED_PROCESS_CLOCKS",
88+
"-D_WASI_EMULATED_SIGNAL",
89+
]
90+
91+
WASM_CFLAGS += WASM_WASI_LIBC_CFLAGS
92+
8493
WASM_CXXFLAGS = WASM_CFLAGS
8594

8695
# Flags for shared libraries
@@ -112,6 +121,17 @@
112121
"-Xlinker --no-check-features",
113122
]
114123

124+
# Wasi-libc specific libraries we need to link with to enable certain emulated
125+
# functionalities
126+
WASM_WASI_LIBC_LDFLAGS = [
127+
"-lc-printscan-long-double",
128+
"-lwasi-emulated-getpid",
129+
"-lwasi-emulated-process-clocks",
130+
"-lwasi-emulated-signal",
131+
]
132+
133+
WASM_LDFLAGS += WASM_WASI_LIBC_LDFLAGS
134+
115135
# Flags for executables
116136
WASM_EXE_LDFLAGS = [
117137
"-Xlinker --stack-first",
@@ -125,6 +145,8 @@
125145
"-Wl,--initial-memory={}".format(FAASM_WASM_INITIAL_MEMORY_SIZE),
126146
]
127147

148+
WASM_EXE_LDFLAGS += WASM_WASI_LIBC_LDFLAGS
149+
128150
# These are the compiler and linker flags required for functions that will also
129151
# do dynamic linking. We need to export all symbols to make them available to
130152
# the dynamically loaded modules
@@ -133,6 +155,8 @@
133155
"-Xlinker --no-gc-sections",
134156
]
135157

158+
WASM_EXE_LDFLAGS_SHARED += WASM_WASI_LIBC_LDFLAGS
159+
136160
# Flags for shared libraries
137161
# See notes in README about WebAssembly and shared libraries
138162
WASM_LDFLAGS_SHARED = [

func/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Wasm")
2525
# Wasm build
2626
# ----------------------
2727

28-
set(FAASM_FUNC_LIBS faasm c-printscan-long-double)
28+
set(FAASM_FUNC_LIBS faasm)
2929

3030
function(faasm_func_c func_name file_name)
3131
add_executable(${func_name} ${file_name})

func/demo/calloc.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ int main(int argc, char* argv[])
2626
strcpy(callocPtr, s.c_str());
2727

2828
// Add in a call to mmap to deliberately fragment things
29-
char* mmapPtr =
30-
(char*)mmap(nullptr, mmapLen, PROT_WRITE, MAP_ANONYMOUS, -1, 0);
29+
char* mmapPtr = (char*)mmap(
30+
nullptr, mmapLen, PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
31+
if (mmapPtr == MAP_FAILED) {
32+
printf("ERROR - mmap failed: %s\n", strerror(errno));
33+
}
3134
strcpy(mmapPtr, "mmapSpace");
3235

3336
callocPtrs[i] = callocPtr;

func/demo/getcwd.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#include <string.h>
55
#include <unistd.h>
66

7+
// 09/02/2022 - wasi-libc implements getcwd now, so we don't need to implement
8+
// it ourselves in Faasm. Thus, this test is now redundant as it is essentially
9+
// testing wasi-libc's functionality. We keep it for backwards-compatibility.
710
int main(int argc, char* argv[])
811
{
912
char buf[1024];
@@ -14,7 +17,7 @@ int main(int argc, char* argv[])
1417
return 1;
1518
}
1619

17-
const char* expected = "/bin";
20+
const char* expected = "/";
1821
if (strcmp(res, expected) != 0) {
1922
printf("Unexpected working dir (expected %s, got %s)\n", expected, res);
2023
return 1;

func/demo/mmap.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ int main(int argc, char* argv[])
2222
// Map some memory
2323
void* p = mmap(nullptr,
2424
memLen,
25-
PROT_READ | PROT_WRITE | PROT_EXEC,
25+
PROT_READ | PROT_WRITE,
2626
MAP_ANON | MAP_PRIVATE,
2727
-1,
2828
0);

func/demo/mmap_big.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ int main(int argc, char* argv[])
1818
for (int i = 0; i < nChunks; i++) {
1919
void* p = mmap(nullptr,
2020
chunkLen,
21-
PROT_READ | PROT_WRITE | PROT_EXEC,
21+
PROT_READ | PROT_WRITE,
2222
MAP_ANON | MAP_PRIVATE,
2323
-1,
2424
0);

tasks/docker.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from faasmtools.env import (
1111
get_version,
1212
PROJ_ROOT,
13-
LLVM_VERSION,
1413
)
1514

1615
LLVM_IMAGE_NAME = "faasm/llvm"
@@ -25,7 +24,8 @@ def get_sysroot_tag():
2524

2625

2726
def get_llvm_tag():
28-
return "{}:{}".format(LLVM_IMAGE_NAME, LLVM_VERSION)
27+
version = get_version()
28+
return "{}:{}".format(LLVM_IMAGE_NAME, version)
2929

3030

3131
@task(iterable=["c"])

tasks/lib.py

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ def build_faasm_lib(subdir, clean=False, native=False, shared=False):
3737
extras = [
3838
"-DCMAKE_C_COMPILER=/usr/bin/clang-{}".format(llvm_major),
3939
"-DCMAKE_CXX_COMPILER=/usr/bin/clang++-{}".format(llvm_major),
40+
"-DCMAKE_AR=/usr/bin/llvm-ar-{}".format(llvm_major),
41+
"-DCMAKE_NM=/usr/bin/llvm-nm-{}".format(llvm_major),
42+
"-DCMAKE_RANLIB=/usr/bin/llvm-ranlib-{}".format(llvm_major),
4043
]
4144
else:
4245
extras = [

tasks/libfake.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ def fake(ctx, clean=False):
4343
check=True,
4444
env=work_env,
4545
)
46-
run("ninja", shell=True, cwd=build_dir, check=True)
47-
run("ninja install", shell=True, cwd=build_dir, check=True)
46+
run("ninja", shell=True, cwd=build_dir, check=True, env=work_env)
47+
run("ninja install", shell=True, cwd=build_dir, check=True, env=work_env)
4848

4949
# Copy shared object into place
5050
sysroot_files = join(WASM_SYSROOT, "lib", "wasm32-wasi", "libfake*.so")

tasks/llvm.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
FAASM_BUILD_ENV_DICT,
33
FAASM_LOCAL_DIR,
44
WASM_LIB_INSTALL,
5+
WASM_SYSROOT,
56
)
67
from faasmtools.env import (
78
LLVM_DIR,
@@ -24,6 +25,7 @@
2425
"FAASM_LLVM_VERSION": LLVM_VERSION,
2526
"FAASM_LOCAL_DIR_ENV": FAASM_LOCAL_DIR,
2627
"FAASM_WASI_LIBC_DIR": WASI_LIBC_DIR,
28+
"FAASM_SYSROOT": WASM_SYSROOT,
2729
}
2830

2931

@@ -73,11 +75,22 @@ def libs(ctx, clean=False):
7375

7476

7577
@task()
76-
def libc(ctx, clean=False):
78+
def libc(ctx, clean=False, purge=False):
7779
"""
7880
Builds the wasi libc fork in this directory
81+
82+
Clean removes the built targets but does not wipe the sysroot. Purge wipes
83+
the sysroot altogether.
7984
"""
80-
do_llvm_build("libc", "clean-libc" if clean else None)
85+
clean_args = None
86+
if purge:
87+
if clean:
88+
print("WARN: Set --clean and --purge flag. Purge preceeds.")
89+
clean_args = "very-clean-libc"
90+
elif clean:
91+
clean_args = "clean-libc"
92+
93+
do_llvm_build("libc", clean_args)
8194

8295
# Copy the import files into place
8396
copy_cmd = "cp -r sysroot_extras/* {}".format(WASM_LIB_INSTALL)

tasks/zlib.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ def zlib(ctx, clean=False):
4646
configure_cmd = " ".join(configure_cmd)
4747
run(configure_cmd, shell=True, cwd=zlib_dir, check=True)
4848

49-
run("make -j", shell=True, cwd=zlib_dir, check=True)
49+
# Only build the static library, not the examples nor tests
50+
run("make -j libz.a", shell=True, cwd=zlib_dir, check=True)
5051
run("make install", shell=True, cwd=zlib_dir, check=True)
5152

5253
# Move the library from /usr/local/faasm/llvm-sysroot/lib into wasm32-wasi

third-party/wasi-libc

Submodule wasi-libc updated 578 files

0 commit comments

Comments
 (0)