rules_rust icon indicating copy to clipboard operation
rules_rust copied to clipboard

build -sys packages with rules_foreign_cc targtes

Open ashi009 opened this issue 1 year ago • 0 comments

When using rules_rust to build glib-sys, the package uses system-deps to find system dependencies, which then calls pkg-config to find extra CCFLAGS, LDFLAGS and LIBS from the dependencies.

The output of foreign_cc targets includes pkgconfig files (.pc) that other foreign_cc targets can recognize. This is done through a script wrapper from foreign_cc before running the action tool. This is not done in a separate action because those pkgconfig files contain absolute paths, and foreign_cc makes them portable to allow caching, and it needs to be made absolute before being consumed by pkg-config.

We need a way to allow cargo_build_script to work with the foreign_cc, and gracefully handle those pkgconfig files.

manually modified build file:

cargo_build_script(
    name = "glib-sys_bs",
    srcs = glob(
        include = ["**/*.rs"],
        allow_empty = False,
    ),
    build_script_env = {
        "PKG_CONFIG_PATH": "$${pwd}/bazel-out/darwin_arm64-fastbuild-ST-b86257f8ac8a/bin/external/glib/glib_/lib/pkgconfig:$${pwd}/bazel-out/darwin_arm64-fastbuild-ST-b86257f8ac8a/bin/external/libpcre2/libpcre2/lib/pkgconfig",
    },
    crate_name = "build_script_build",
    crate_root = "build.rs",
    data = glob(
        include = ["**"],
        allow_empty = True,
        exclude = [
            "**/* *",
            ".tmp_git_root/**/*",
            "BUILD",
            "BUILD.bazel",
            "WORKSPACE",
            "WORKSPACE.bazel",
        ],
    ) + [
        "@glib",
        "@rules_foreign_cc//toolchains:current_pkgconfig_toolchain",
    ],
    edition = "2021",
    rustc_flags = [
        "--cap-lints=allow",
    ],
    tags = [
        "cargo-bazel",
        "crate-name=glib-sys",
        "manual",
        "noclippy",
        "norustfmt",
    ],
    toolchains = ["@rules_foreign_cc//toolchains:current_pkgconfig_toolchain"],
    version = "0.18.1",
    visibility = ["//visibility:private"],
    deps = [
        "@crate_index__system-deps-6.2.2//:system_deps",
    ],
)

alias(
    name = "build_script_build",
    actual = ":glib-sys_bs",
    tags = ["manual"],
)

logs:

bazel build @crate_index__glib-sys-0.18.1//:glib_sys --sandbox_debug
INFO: Invocation ID: 7d7f00e2-4570-4f7b-b587-a42b4e159daa
INFO: Streaming build results to: http://localhost:8080/invocation/7d7f00e2-4570-4f7b-b587-a42b4e159daa
INFO: Analyzed target @@crate_index__glib-sys-0.18.1//:glib_sys (1 packages loaded, 19 targets configured).
ERROR: /private/var/tmp/<redacted>/external/crate_index__glib-sys-0.18.1/BUILD.bazel:91:19: Running Cargo build script glib-sys failed: (Exit 1): sandbox-exec failed: error executing CargoBuildScriptRun command
  (cd /private/var/tmp/<redacted>/sandbox/darwin-sandbox/3123/execroot/<redacted> && \
  exec env - \
    AR=external/llvm_toolchain/bin/llvm-ar \
    CARGO_CRATE_NAME=glib_sys \
    CARGO_ENCODED_RUSTFLAGS='--sysroot=${pwd}/bazel-out/darwin_arm64-fastbuild/bin/external/rust_darwin_aarch64__aarch64-apple-darwin__nightly_tools/rust_toolchain\x1f--cap-lints=allow' \
    CARGO_MANIFEST_DIR=bazel-out/darwin_arm64-opt-exec-ST-a828a81199fe/bin/external/crate_index__glib-sys-0.18.1/glib-sys_bs_.runfiles/crate_index__glib-sys-0.18.1/ \
    CARGO_PKG_NAME=glib-sys \
    CARGO_PKG_VERSION=0.18.1 \
    CARGO_PKG_VERSION_MAJOR=0 \
    CARGO_PKG_VERSION_MINOR=18 \
    CARGO_PKG_VERSION_PATCH=1 \
    CARGO_PKG_VERSION_PRE='' \
    CC=external/llvm_toolchain/bin/cc_wrapper.sh \
    CFLAGS='-U_FORTIFY_SOURCE --target=aarch64-apple-macosx -U_FORTIFY_SOURCE -fstack-protector -fno-omit-frame-pointer -fcolor-diagnostics -Wall -Wthread-safety -Wself-assign -mmacosx-version-min=14.5 --sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -no-canonical-prefixes -Wno-builtin-macro-redefined -D__DATE__="redacted" -D__TIMESTAMP__="redacted" -D__TIME__="redacted" -fdebug-prefix-map=external/llvm_toolchain_llvm/=__bazel_toolchain_llvm_repo__/' \
    CXX=external/llvm_toolchain/bin/cc_wrapper.sh \
    CXXFLAGS='-U_FORTIFY_SOURCE --target=aarch64-apple-macosx -U_FORTIFY_SOURCE -fstack-protector -fno-omit-frame-pointer -fcolor-diagnostics -Wall -Wthread-safety -Wself-assign -std=c++17 -stdlib=libc++ -Xclang -fno-cxx-modules -mmacosx-version-min=14.5 --sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -no-canonical-prefixes -Wno-builtin-macro-redefined -D__DATE__="redacted" -D__TIMESTAMP__="redacted" -D__TIME__="redacted" -fdebug-prefix-map=external/llvm_toolchain_llvm/=__bazel_toolchain_llvm_repo__/' \
    DEBUG=true \
    HOST=aarch64-apple-darwin \
    LD=external/llvm_toolchain/bin/cc_wrapper.sh \
    LDFLAGS='-mmacosx-version-min=14.5 -no-canonical-prefixes -fobjc-link-runtime --target=aarch64-apple-macosx -lm -no-canonical-prefixes -headerpad_max_install_names -fobjc-link-runtime -L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib -lc++ -lc++abi -Bstatic -lunwind -Bdynamic -Lexternal/llvm_toolchain_llvm/lib -lSystem -lresolv --sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk' \
    NUM_JOBS=1 \
    OPT_LEVEL=0 \
    PKG_CONFIG=bazel-out/darwin_arm64-opt-exec-ST-a828a81199fe/bin/external/rules_foreign_cc/toolchains/private/pkgconfig/bin/pkg-config \
    PKG_CONFIG_PATH='${pwd}/bazel-out/darwin_arm64-fastbuild-ST-b86257f8ac8a/bin/external/glib/glib_/lib/pkgconfig:${pwd}/bazel-out/darwin_arm64-fastbuild-ST-b86257f8ac8a/bin/external/libpcre2/libpcre2/lib/pkgconfig' \
    PROFILE=debug \
    RUSTC=bazel-out/darwin_arm64-fastbuild/bin/external/rust_darwin_aarch64__aarch64-apple-darwin__nightly_tools/rust_toolchain/bin/rustc \
    TARGET=aarch64-apple-darwin \
    TMPDIR=/var/folders/67/vhyv_gwn0pjd0fjm5jxd_6fh0000gn/T/ \
    ZERO_AR_DATE=1 \
  /usr/bin/sandbox-exec -f /private/var/tmp/<redacted>/sandbox/darwin-sandbox/3123/sandbox.sb /var/tmp/_bazel_xiaoyishi/install/abee1bc4a5caa87f8299faf78a2e8b97/process-wrapper '--timeout=0' '--kill_delay=15' '--stats=/private/var/tmp/<redacted>/sandbox/darwin-sandbox/3123/stats.out' bazel-out/darwin_arm64-opt-exec-ST-a828a81199fe/bin/external/rules_rust/cargo/cargo_build_script_runner/cargo_build_script_runner bazel-out/darwin_arm64-opt-exec-ST-a828a81199fe/bin/external/crate_index__glib-sys-0.18.1/glib-sys_bs_ '' bazel-out/darwin_arm64-fastbuild/bin/external/crate_index__glib-sys-0.18.1/glib-sys_bs.out_dir bazel-out/darwin_arm64-fastbuild/bin/external/crate_index__glib-sys-0.18.1/glib-sys_bs.env bazel-out/darwin_arm64-fastbuild/bin/external/crate_index__glib-sys-0.18.1/glib-sys_bs.flags bazel-out/darwin_arm64-fastbuild/bin/external/crate_index__glib-sys-0.18.1/glib-sys_bs.linkflags bazel-out/darwin_arm64-fastbuild/bin/external/crate_index__glib-sys-0.18.1/glib-sys_bs.linksearchpaths bazel-out/darwin_arm64-fastbuild/bin/external/crate_index__glib-sys-0.18.1/glib-sys_bs.depenv bazel-out/darwin_arm64-fastbuild/bin/external/crate_index__glib-sys-0.18.1/glib-sys_bs.stdout.log bazel-out/darwin_arm64-fastbuild/bin/external/crate_index__glib-sys-0.18.1/glib-sys_bs.stderr.log '')
Build script process failed with exit code 1
--stdout:
cargo:rerun-if-env-changed=GLIB_2.0_NO_PKG_CONFIG
cargo:rerun-if-env-changed=PKG_CONFIG_aarch64-apple-darwin
cargo:rerun-if-env-changed=PKG_CONFIG_aarch64_apple_darwin
cargo:rerun-if-env-changed=HOST_PKG_CONFIG
cargo:rerun-if-env-changed=PKG_CONFIG
cargo:rerun-if-env-changed=PKG_CONFIG_PATH_aarch64-apple-darwin
cargo:rerun-if-env-changed=PKG_CONFIG_PATH_aarch64_apple_darwin
cargo:rerun-if-env-changed=HOST_PKG_CONFIG_PATH
cargo:rerun-if-env-changed=PKG_CONFIG_PATH
cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_aarch64-apple-darwin
cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_aarch64_apple_darwin
cargo:rerun-if-env-changed=HOST_PKG_CONFIG_LIBDIR
cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR
cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_aarch64-apple-darwin
cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_aarch64_apple_darwin
cargo:rerun-if-env-changed=HOST_PKG_CONFIG_SYSROOT_DIR
cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
cargo:warning=Could not run `PKG_CONFIG_PATH=/private/var/tmp/<redacted>/sandbox/darwin-sandbox/3123/execroot/<redacted>/bazel-out/darwin_arm64-fastbuild-ST-b86257f8ac8a/bin/external/glib/glib_/lib/pkgconfig:/private/var/tmp/<redacted>/sandbox/darwin-sandbox/3123/execroot/<redacted>/bazel-out/darwin_arm64-fastbuild-ST-b86257f8ac8a/bin/external/libpcre2/libpcre2/lib/pkgconfig PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=1 bazel-out/darwin_arm64-opt-exec-ST-a828a81199fe/bin/external/rules_foreign_cc/toolchains/private/pkgconfig/bin/pkg-config --libs --cflags glib-2.0 glib-2.0 >= 2.56`
The pkg-config command could not be found.

Most likely, you need to install a pkg-config package for your OS.
Try `brew install pkg-config` if you have Homebrew.

If you've already installed it, ensure the pkg-config command is one of the
directories in the PATH environment variable.

If you did not expect this build to link to a pre-installed system library,
then check documentation of the glib-sys crate for an option to
build the library from source, or disable features or dependencies
that require pkg-config.

--stderr:

Target @@crate_index__glib-sys-0.18.1//:glib_sys failed to build
Use --verbose_failures to see the command lines of failed build steps.

The actual output from the pkg-config:

PKG_CONFIG_PATH=/private/var/tmp/<redacted>/sandbox/darwin-sandbox/3123/execroot/<redacted>/bazel-out/darwin_arm64-fastbuild-ST-b86257f8ac8a/bin/external/glib/glib_/lib/pkgconfig:/private/var/tmp/<redacted>/sandbox/darwin-sandbox/3123/execroot/<redacted>/bazel-out/darwin_arm64-fastbuild-ST-b86257f8ac8a/bin/external/libpcre2/libpcre2/lib/pkgconfig PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=1 bazel-out/darwin_arm64-opt-exec-ST-a828a81199fe/bin/external/rules_foreign_cc/toolchains/private/pkgconfig/bin/pkg-config --libs --cflags glib-2.0 glib-2.0 >= 2.56
Variable 'EXT_BUILD_ROOT' not defined in '/private/var/tmp/<redacted>/sandbox/darwin-sandbox/3123/execroot/<redacted>/bazel-out/darwin_arm64-fastbuild-ST-b86257f8ac8a/bin/external/glib/glib_/lib/pkgconfig/glib-2.0.pc'

ashi009 avatar Jul 24 '24 22:07 ashi009