-
Notifications
You must be signed in to change notification settings - Fork 435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rust_bindgen fails on macOS due to fatal error: 'TargetConditionals.h' file not found #899
Comments
A hacky way to fix the issue, is to teach rust_bindgen(
name = "bindings",
bindgen_flags = [
"--allowlist-function=duk_.*",
"--allowlist-type=duk_.*",
"--allowlist-var=DUK_.*",
],
cc_lib = ":duktapelib",
clang_flags = [
"-isystem",
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include",
],
header = ":src/duktape.h",
visibility = ["//visibility:private"],
) I hope there is a more bazel-style way of fixing it, since the CC Toolchain / cc_library knows the right path already. |
@kiron1 Would it be possible to get a self-contained repro (either a git repo, or a WORKSPACE + BUILD file) that shows the issue? |
@illicitonion sure, here the minimal example: https://github.com/kiron1/rules_rust_bindgen Bazel version: 4.2.0 |
Fantastic, thanks, that was very useful. The problem here is that Bazel sets up a cc_toolchain with all of these extra flags, but the bindgen code completely ignores the toolchain and tries to bring along its own clang and such. I don't have much bindgen context, but I can give a brain-dump of the Bazel side of this:
I wired up the cc_toolchain to grab its clang-wrapper and flags, but this wasn't sufficient to make things Just Work - I'm guessing because of either some handling Unfortunately my bindgen-fu is basically nil, but maybe the below diff can give someone a jumping off point for how to wire these things into bindgen: diff --git a/bindgen/bindgen.bzl b/bindgen/bindgen.bzl
index cf28023..01fa248 100644
--- a/bindgen/bindgen.bzl
+++ b/bindgen/bindgen.bzl
@@ -16,7 +16,12 @@
load("//rust:rust.bzl", "rust_library")
# buildifier: disable=bzl-visibility
-load("//rust/private:utils.bzl", "find_toolchain", "get_preferred_artifact")
+load("//rust/private:utils.bzl", "find_cc_toolchain", "find_toolchain", "get_preferred_artifact")
+
+load(
+ "@bazel_tools//tools/build_defs/cc:action_names.bzl",
+ "CPP_COMPILE_ACTION_NAME",
+)
# TODO(hlopko): use the more robust logic from rustc.bzl also here, through a reasonable API.
def _get_libs_for_static_executable(dep):
@@ -121,6 +126,26 @@ def _rust_bindgen_impl(ctx):
args.add_all(system_include_directories, before_each = "-isystem")
args.add_all(clang_args)
+ cc_toolchain, feature_configuration = find_cc_toolchain(ctx)
+
+ # See https://docs.bazel.build/versions/4.2.0/skylark/lib/cc_common.html#create_compile_variables
+ # Instead of ourselves doing the -iquote and similar adding in the lines above,
+ # we could be passing these as arguments to `crate_compile_variables` and it would set up the correct flags for us.
+ compile_variables = cc_common.create_compile_variables(
+ cc_toolchain = cc_toolchain,
+ feature_configuration = feature_configuration,
+ )
+
+ # This will include e.g. "-isysroot" "__BAZEL_XCODE_SDKROOT__"
+ compile_args_from_toolchain = cc_common.get_memory_inefficient_command_line(
+ feature_configuration = feature_configuration,
+ action_name = CPP_COMPILE_ACTION_NAME,
+ variables = compile_variables,
+ )
+
+ # This points at the clang-wrapper that does env var substitution.
+ clang_bin_from_toolchain = cc_common.get_tool_for_action(feature_configuration = feature_configuration, action_name = CPP_COMPILE_ACTION_NAME)
+
env = {
"CLANG_PATH": clang_bin.path,
"LIBCLANG_PATH": libclang_dir,
@@ -194,12 +219,15 @@ rust_bindgen = rule(
allow_single_file = True,
cfg = "exec",
),
+ "_cc_toolchain": attr.label(default = "@bazel_tools//tools/cpp:current_cc_toolchain"),
},
outputs = {"out": "%{name}.rs"},
toolchains = [
str(Label("//bindgen:bindgen_toolchain")),
str(Label("//rust:toolchain")),
+ "@rules_cc//cc:toolchain_type"
],
+ fragments = ["cpp"],
incompatible_use_toolchain_transition = True,
) |
It looks like this issue is due to Not an uncommon issue, it seems: rust-lang/rust-bindgen#1226 That |
This fixes #899 by putting `XCODE_VERSION_OVERRIDE`, `APPLE_SDK_VERSION_OVERRIDE`, `APPLE_SDK_PLATFORM` in the env.
@kiron1 could you contribute this repo as an example here? |
@sayrer sure I can create a PR for bazelbuild/rules_rust with the example, but what do we want to demonstrate with the example? a) That There is already examples/bindgen. Do we want to extend this example, or add a second example? |
I was thinking of that. I'd just do whatever's easiest in terms of where to put it. |
Something like this #925 ? |
I have a Bazel project where I want to use
rules_rust
withrust_bindgen
to be able to use Duktape in rust.I have a
cc_library(...)
which can successfully compile Duktape, but therust_bindgen(...)
target fails with the following error:duk_config.h:407
When I use
clang
from_bindgen_clang_repositories
($(bazel info execution_root)/external/bindgen_clang_osx/bin/clang -c duktape-sys/src/duktape.c
) I get indeed this error:However, when using
xcrun clang -c duktape-sys/src/duktape.c
, it compiles without problems.Is there a way to teach
rust_bindgen
to useclang
with the includes directories used byxcrun clang
?The text was updated successfully, but these errors were encountered: