From bfcfa4b2521b4d5d876576cfeaaccebd3fa8f5b1 Mon Sep 17 00:00:00 2001 From: ADD-SP Date: Fri, 18 Oct 2024 04:04:10 +0000 Subject: [PATCH] refactor(build): hermetic install of some rocks --- build/build_system.bzl | 19 ++++++++++++++++++- build/libexpat/BUILD.libexpat.bazel | 2 -- build/luarocks/templates/luarocks_exec.sh | 14 ++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/build/build_system.bzl b/build/build_system.bzl index 66f6a1657538..a0130e6d5ff8 100644 --- a/build/build_system.bzl +++ b/build/build_system.bzl @@ -74,7 +74,23 @@ def _render_template(ctx, output): substitutions = dict(ctx.attr.substitutions) for l in ctx.attr.srcs + ctx.attr.tools: if OutputGroupInfo in l and "gen_dir" in l[OutputGroupInfo]: # usualy it's foreign_cc target - p = l[OutputGroupInfo].gen_dir.to_list()[0].path + gen_dirs = l[OutputGroupInfo].gen_dir.to_list() + # foreign_cc target usually has only one element in gen_dirs + if len(gen_dirs) == 1: + # foreign_cc target usually has the similar path structure + # bazel-out/k8-fastbuild/bin/external//copy_/ + segments = gen_dirs[0].path.split("/") + if len(segments) >= 2 and segments[-2] == "copy_" + segments[-1]: + # go up to the copy_ directory and then go down to the directory + # For example, + # bazel-out/k8-fastbuild/bin/external/openssl/copy_openssl/openssl -> + # bazel-out/k8-fastbuild/bin/external/openssl/openssl + p = gen_dirs[0].path + "/../../" + segments[-1] + else: + fail("expect a target of rules_foreign_cc") + else: + fail("expect a target of rules_foreign_cc") + else: # otherwise it's usually output from gen_rule, file_group etc files = l.files.to_list() p = files[0].path @@ -325,6 +341,7 @@ def _kong_install_impl(ctx): outputs.append(output) if full_path.find(".so.") >= 0 and ctx.attr.create_dynamic_library_symlink: + # libX.so.2.3.4 -> ["libX", "so", "2", "3", "4"] el = full_path.split(".") si = el.index("so") sym_paths = [] diff --git a/build/libexpat/BUILD.libexpat.bazel b/build/libexpat/BUILD.libexpat.bazel index e443b86afac7..f8c57829460f 100644 --- a/build/libexpat/BUILD.libexpat.bazel +++ b/build/libexpat/BUILD.libexpat.bazel @@ -34,8 +34,6 @@ configure_make( "//conditions:default": {}, }), lib_source = ":all_srcs", - out_include_dir = "include/libexpat", # don't install headers - # out_lib_dir = "lib", out_shared_libs = select({ "@platforms//os:macos": [ "libexpat.1.dylib", diff --git a/build/luarocks/templates/luarocks_exec.sh b/build/luarocks/templates/luarocks_exec.sh index 27c81953f24e..b8d1cec720a6 100644 --- a/build/luarocks/templates/luarocks_exec.sh +++ b/build/luarocks/templates/luarocks_exec.sh @@ -29,6 +29,20 @@ EXPAT_DIR=$root_path/$libexpat_path LIBXML2_DIR=$root_path/$libxml2_path OPENSSL_DIR=$root_path/$openssl_path +# The Bazel rules doesn't export the `libexpat.so` file, +# it only exports something like `libexpat.so.1.6.0`, +# but the linker expects `libexpat.so` to be present. +# So we create a symlink to the actual file +# if it doesn't exist. +if ! test -e $EXPAT_DIR/lib/libexpat.so; then + so=$(ls $EXPAT_DIR/lib/libexpat.*) + if [[ -z $so ]]; then + echo "No expat library found in $EXPAT_DIR/lib" + exit 1 + fi + ln -s $so $EXPAT_DIR/lib/libexpat.so +fi + # we use system libyaml on macos if [[ "$OSTYPE" == "darwin"* ]]; then YAML_DIR=$(HOME=~$(whoami) PATH=/opt/homebrew/bin:$PATH brew --prefix)/opt/libyaml