Skip to content

Commit 9f157aa

Browse files
authored
jwt: support native install (#51)
* jwt: support native install * fix requirements
1 parent b3beb98 commit 9f157aa

File tree

2 files changed

+69
-24
lines changed

2 files changed

+69
-24
lines changed

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ black>=22.3.0
22
faasmctl>=0.32.0
33
flake8>=4.0.1
44
invoke>=1.7.1
5+
python-lsp-server[all]>=1.12.0
56
PyYAML>=6.0.1

tasks/jwt.py

Lines changed: 68 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
from os.path import exists, join
55
from shutil import copy, rmtree
66
from subprocess import run
7-
from tasks.env import EXAMPLES_DIR
7+
from tasks.env import EXAMPLES_DIR, in_docker
88

99

1010
@task(default=True)
11-
def build(ctx, clean=False):
11+
def build(ctx, clean=False, native=False):
1212
"""
1313
Compile TLess-JWT library (in Rust) and C++ bindings into a WASM library
1414
"""
@@ -18,20 +18,48 @@ def build(ctx, clean=False):
1818
rmtree(join(jwt_dir, "target"))
1919

2020
# First, cross-compile the rust library to WASM
21-
cargo_cmd = "cargo build --release --target=wasm32-wasip1"
21+
cargo_cmd = "cargo build --release"
22+
if not native:
23+
cargo_cmd += " --target=wasm32-wasip1"
2224
run(cargo_cmd, shell=True, check=True, cwd=jwt_dir)
2325

24-
# Install it in the WASM sysroot
25-
build_env = get_faasm_build_env_dict()
26+
# Install it
27+
lib_dir = "/usr/local/lib/tless-jwt"
28+
header_dir = "/usr/include/tless-jwt"
2629
src_lib = join(
27-
jwt_dir, "target", "wasm32-wasip1", "release", "libtless_jwt.a"
30+
jwt_dir,
31+
"target",
32+
"wasm32-wasip1" if not native else "",
33+
"release",
34+
"libtless_jwt.a",
2835
)
29-
dst_lib = join(build_env["FAASM_WASM_LIB_INSTALL_DIR"], "libtless-jwt.a")
30-
copy(src_lib, dst_lib)
36+
if native:
37+
if not exists(lib_dir):
38+
if in_docker():
39+
makedirs(lib_dir)
40+
else:
41+
run(f"sudo mkdir -p {lib_dir}", shell=True, check=True)
42+
if not exists(header_dir):
43+
if in_docker():
44+
makedirs(header_dir)
45+
else:
46+
run(f"sudo mkdir -p {header_dir}", shell=True, check=True)
47+
dst_lib = join(lib_dir, "libtless_jwt.a")
48+
else:
49+
build_env = get_faasm_build_env_dict()
50+
dst_lib = join(
51+
build_env["FAASM_WASM_LIB_INSTALL_DIR"], "libtless-jwt.a"
52+
)
53+
if in_docker():
54+
copy(src_lib, dst_lib)
55+
else:
56+
run(f"sudo cp {src_lib} {dst_lib}", shell=True, check=True)
3157

3258
# 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")
59+
tles_jwt_cpp_dir = join(jwt_dir, "cpp-bindings")
60+
build_dir = join(
61+
tles_jwt_cpp_dir, "build-native" if native else "build-wasm"
62+
)
3563

3664
if clean and exists(build_dir):
3765
rmtree(build_dir)
@@ -42,28 +70,44 @@ def build(ctx, clean=False):
4270
"cmake",
4371
"-GNinja",
4472
"-DCMAKE_BUILD_TYPE=Release",
45-
"-DCMAKE_TOOLCHAIN_FILE={}".format(CMAKE_TOOLCHAIN_FILE),
46-
rabe_cpp_dir,
73+
(
74+
"-DCMAKE_TOOLCHAIN_FILE={}".format(CMAKE_TOOLCHAIN_FILE)
75+
if not native
76+
else ""
77+
),
78+
tles_jwt_cpp_dir,
4779
]
4880
cmake_cmd = " ".join(cmake_cmd)
4981
print(cmake_cmd)
5082

5183
work_env = environ.copy()
52-
work_env.update(get_faasm_build_env_dict())
53-
print(build_dir)
84+
if not native:
85+
work_env.update(get_faasm_build_env_dict())
5486
run(cmake_cmd, shell=True, check=True, cwd=build_dir, env=work_env)
5587
run("ninja", shell=True, check=True, cwd=build_dir)
5688

5789
# Install the library in the WASM sysroot
5890
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)
91+
if not native:
92+
dst_lib = join(
93+
build_env["FAASM_WASM_LIB_INSTALL_DIR"], "libtless-jwt-cpp.a"
94+
)
95+
else:
96+
dst_lib = join(lib_dir, "libtless-jwt-cpp.a")
97+
if in_docker():
98+
copy(src_lib, dst_lib)
99+
else:
100+
run(f"sudo cp {src_lib} {dst_lib}", shell=True, check=True)
63101

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)
102+
# Install the header too
103+
src_header = join(tles_jwt_cpp_dir, "tless_jwt.h")
104+
if not native:
105+
dst_header = join(
106+
build_env["FAASM_WASM_HEADER_INSTALL_DIR"], "tless_jwt.h"
107+
)
108+
else:
109+
dst_header = join(header_dir, "tless_jwt.h")
110+
if in_docker():
111+
copy(src_header, dst_header)
112+
else:
113+
run(f"sudo cp {src_header} {dst_header}", shell=True, check=True)

0 commit comments

Comments
 (0)