From db8ee4d4856d2d21c0f15e1f970e2b10d4a118c1 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Thu, 13 Apr 2023 12:35:45 -0300 Subject: [PATCH] chore(*) install ngx_wasm_rs along with nginx when needed There's a tricky thing going on. Since Wasmer itself is written in Rust and we build using a precompiled libwasmer.a generated with a different Rust version, if we link both libwasmer.a and libngx_wasm_rs.a, we get a link failure with some duplicated incompatible symbols [1][2]. For this reason, when we enable ngx_wasm_rs with Wasmer, we build it as a .so file rather than a statically-linked .a file. With this commit, we can install cdylibs under nginx's `--prefix` at `/lib` (e.g. `/usr/local/nginx/lib`, next to `/usr/local/nginx/sbin/nginx`) and ensure that the `nginx` binary can find it. See here for more info: [1] https://users.rust-lang.org/t/linking-more-than-one-rustc-compiled-static-libraries-in-a-c-project/89778/2 [2] https://github.com/Kong/ngx_wasm_module/pull/259#discussion_r1161359734 --- auto/cargo | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/auto/cargo b/auto/cargo index 2f37e0c7d..d5cc127f8 100644 --- a/auto/cargo +++ b/auto/cargo @@ -24,7 +24,8 @@ if [ $ngx_wasm_rc = 0 ]; then ngx_feature_test="$ngx_wasm_cargo_feature_test" if [ "$ngx_wasm_cargo_crate_type" = "cdylib" ]; then ngx_feature_libs="\ - -Wl,-rpath $ngx_wasm_cargo_target_dir \ + -Wl,-rpath=$ngx_wasm_cargo_target_dir \ + -Wl,-rpath=$NGX_PREFIX/lib \ -L$ngx_wasm_cargo_target_dir \ -l$ngx_wasm_cargo_lib_name" else @@ -41,6 +42,28 @@ if [ $ngx_wasm_rc = 0 ]; then have=$d value=1 . auto/define done + if [ "$ngx_wasm_cargo_crate_type" = "cdylib" ]; then + ngx_wasm_cargo_cdylib="$ngx_wasm_cargo_target_dir/lib$ngx_wasm_cargo_lib_name.so" + ngx_wasm_cargo_cdylibs="$ngx_wasm_cargo_cdylibs $ngx_wasm_cargo_cdylib" + + if ! grep -q ngx_wasm_cargo_cdylib auto/install; then + cat << EOF >> auto/install + +for ngx_wasm_cargo_cdylib in \$ngx_wasm_cargo_cdylibs +do + +cat << END >> \$NGX_MAKEFILE + + test -d '\\\$(DESTDIR)\$NGX_PREFIX/lib' \\ + || mkdir -p '\\\$(DESTDIR)\$NGX_PREFIX/lib' + cp \$ngx_wasm_cargo_cdylib '\\\$(DESTDIR)\$NGX_PREFIX/lib' +END + +done +EOF + fi + fi + else echo "$0: error: failed testing library $ngx_wasm_cargo_lib_name." fi