Skip to content
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

Substitute ${SDKROOT} and ${DEVELOPER_DIR} by their environment variables on darwin. #2619

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

thb-sb
Copy link
Contributor

@thb-sb thb-sb commented Apr 18, 2024

Substitute ${SDKROOT} and ${DEVELOPER_DIR} by their environment variables on darwin.

This PR attempts to fix an issue with bindgen when used on darwin with clang
compiled from source (i.e. from @llvm-project).

When using clang compiled from source on darwin, libclang (used through clang-sys) failed
to find system header files (such as stdarg.h or stdbool.h) because these
files are actually living under the SDK path (under <SDK_PATH>/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/ and <SDK_PATH>/usr/include).

Similar to what Bazel did in their xcrunwrapper.sh, we must have a way to tell bindgen's libclang
where to find these system headers.

In this commit, I add the following two substitutes variables:

  • ${SDKROOT}: replaced by std::env::var("SDKROOT") if found.
  • ${DEVELOPER_DIR}: replaced by std::env::var("DEVELOPER") if found.

This is similar to what it is currently done for ${pwd}.

The following rule should now work on darwin:

// mylib.h
#include <stdarg.h>
# BUILD
cc_library(
    name = "mylib",
    srcs = ["mylib.c"],
    hdrs = ["mylib.h"],
)

rust_bindgen_library(
    name = "mylib_bindgen",
    cc_lib = ":mylib",
    clang_flags = select({
        "@platforms//os:macos": [
            "--sysroot=${SDKROOT}",
            "-isystem${SDKROOT}/usr/include",
            "-isystem${SDKROOT}/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/",
        ],
        "//conditions:default": [],
    }),
    crate_name = "mylib",
    header = "mylib.h",
)

It may also fix #1834.

…ariables on darwin.

This PR attempts to fix an issue with bindgen when used on darwin with clang
compiled from source (i.e. from `@llvm-project`).

When using clang compiled from source on darwin, libclang (used through `clang-sys`) failed
to find system header files (such as `stdarg.h` or `stdbool.h`) because these
files are actually living under the SDK path (under `<SDK_PATH>/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/` and `<SDK_PATH>/usr/include`).

Similar to what Bazel did in their [`xcrunwrapper.sh`](https://github.com/bazelbuild/bazel/blob/d8c27bfcd37a74dfbf1bdb9a1e3df13af8360a01/tools/objc/xcrunwrapper.sh#L17), we must have a way to tell bindgen's libclang
where to find these system headers.

In this commit, I add the following two substitutes variables:
  - `${SDKROOT}`: replaced by `std::env::var("SDKROOT")` if found.
  - `${DEVELOPER_DIR}`: replaced by `std::env::var("DEVELOPER")` if found.

This is similar to what it is currently done for `${pwd}`.

The following rule should now work on darwin:

```c
// mylib.h
#include <stdarg.h>
```

```starlark
# BUILD
cc_library(
    name = "mylib",
    srcs = ["mylib.c"],
    hdrs = ["mylib.h"],
)

rust_bindgen_library(
    name = "mylib_bindgen",
    cc_lib = ":mylib",
    clang_flags = select({
        "@platforms//os:macos": [
            "--sysroot=${SDKROOT}",
            "-isystem${SDKROOT}/usr/include",
            "-isystem${SDKROOT}/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/",
        ],
        "//conditions:default": [],
    }),
    crate_name = "mylib",
    header = "mylib.h",
)
```

It may also fix bazelbuild#1834.
@thb-sb thb-sb marked this pull request as ready for review April 18, 2024 14:55
Copy link
Collaborator

@illicitonion illicitonion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems pretty reasonable, thanks! Is it possible to add in a test which would fail without this change?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants