|
| 1 | +from faasmtools.build import CMAKE_TOOLCHAIN_FILE, get_faasm_build_env_dict |
| 2 | +from invoke import task |
| 3 | +from os import environ, makedirs |
| 4 | +from os.path import exists, join |
| 5 | +from shutil import copy, rmtree |
| 6 | +from subprocess import run |
| 7 | +from tasks.env import EXAMPLES_DIR |
| 8 | + |
| 9 | + |
| 10 | +@task(default=True) |
| 11 | +def build(ctx, clean=False): |
| 12 | + """ |
| 13 | + Compile TLess-JWT library (in Rust) and C++ bindings into a WASM library |
| 14 | + """ |
| 15 | + jwt_dir = join(EXAMPLES_DIR, "tless-jwt", "jwt-verify") |
| 16 | + |
| 17 | + if clean: |
| 18 | + rmtree(join(jwt_dir, "target")) |
| 19 | + |
| 20 | + # First, cross-compile the rust library to WASM |
| 21 | + cargo_cmd = "cargo build --release --target=wasm32-wasip1" |
| 22 | + run(cargo_cmd, shell=True, check=True, cwd=jwt_dir) |
| 23 | + |
| 24 | + # Install it in the WASM sysroot |
| 25 | + build_env = get_faasm_build_env_dict() |
| 26 | + src_lib = join( |
| 27 | + jwt_dir, "target", "wasm32-wasip1", "release", "libtless_jwt.a" |
| 28 | + ) |
| 29 | + dst_lib = join(build_env["FAASM_WASM_LIB_INSTALL_DIR"], "libtless-jwt.a") |
| 30 | + copy(src_lib, dst_lib) |
| 31 | + |
| 32 | + # Build the CPP bindings library, and cross-compile it to WASM |
| 33 | + rabe_cpp_dir = join(jwt_dir, "cpp-bindings") |
| 34 | + build_dir = join(rabe_cpp_dir, "build") |
| 35 | + |
| 36 | + if clean and exists(build_dir): |
| 37 | + rmtree(build_dir) |
| 38 | + if not exists(build_dir): |
| 39 | + makedirs(build_dir) |
| 40 | + |
| 41 | + cmake_cmd = [ |
| 42 | + "cmake", |
| 43 | + "-GNinja", |
| 44 | + "-DCMAKE_BUILD_TYPE=Release", |
| 45 | + "-DCMAKE_TOOLCHAIN_FILE={}".format(CMAKE_TOOLCHAIN_FILE), |
| 46 | + rabe_cpp_dir, |
| 47 | + ] |
| 48 | + cmake_cmd = " ".join(cmake_cmd) |
| 49 | + print(cmake_cmd) |
| 50 | + |
| 51 | + work_env = environ.copy() |
| 52 | + work_env.update(get_faasm_build_env_dict()) |
| 53 | + print(build_dir) |
| 54 | + run(cmake_cmd, shell=True, check=True, cwd=build_dir, env=work_env) |
| 55 | + run("ninja", shell=True, check=True, cwd=build_dir) |
| 56 | + |
| 57 | + # Install the library in the WASM sysroot |
| 58 | + src_lib = join(build_dir, "libtless-jwt-cpp.a") |
| 59 | + dst_lib = join( |
| 60 | + build_env["FAASM_WASM_LIB_INSTALL_DIR"], "libtless-jwt-cpp.a" |
| 61 | + ) |
| 62 | + copy(src_lib, dst_lib) |
| 63 | + |
| 64 | + # Install the header in the WASM sysroot too |
| 65 | + src_header = join(rabe_cpp_dir, "tless_jwt.h") |
| 66 | + dst_header = join( |
| 67 | + build_env["FAASM_WASM_HEADER_INSTALL_DIR"], "tless_jwt.h" |
| 68 | + ) |
| 69 | + copy(src_header, dst_header) |
0 commit comments