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

Commits on Apr 18, 2024

  1. Substitute ${SDKROOT} and ${DEVELOPER_DIR} by their environment v…

    …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 committed Apr 18, 2024
    Configuration menu
    Copy the full SHA
    6a24010 View commit details
    Browse the repository at this point in the history