Repository: rust-cross/cargo-zigbuild (language: python)

Issue:
Include path weirdness with SDKROOT
Hi there! :wave: 

I'm trying to use `cargo-zigbuild` for cross-compilation, and running into problems that look like headers are being (I think) inappropriately included from the build host's sysroot. **But** I don't really understand what's going on, so I don't even know if the issue is with `cargo-zigbuild` or somewhere else.

## Environment

Build host is Linux on x86-64:

```
~/Projects/scratch/macos-cross-issue took 2s> uname -a
Linux prism 5.13.0-37-generic #42-Ubuntu SMP Tue Mar 15 14:34:06 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
~/Projects/scratch/macos-cross-issue> cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=21.10
DISTRIB_CODENAME=impish
DISTRIB_DESCRIPTION="Ubuntu 21.10"
```

Versions of things:

```
~/Projects/scratch/cross-compilation-is-hard> rustc --version
rustc 1.60.0 (7737e0b5c 2022-04-04)
~/Projects/scratch/cross-compilation-is-hard> cargo --version
cargo 1.60.0 (d1fd9fe 2022-03-01)
~/Projects/scratch/cross-compilation-is-hard> zig version
0.9.1
~/Projects/scratch/cross-compilation-is-hard> which zig
/snap/bin/zig
~/Projects/scratch/cross-compilation-is-hard> cargo-zigbuild --version
cargo-zigbuild 0.8.1
```

I am targeting M1 (aarch64) Mac.

## Minimal reproduction

```
cargo new cross-compilation-is-hard
cd cross-compilation-is-hard/
echo '"libz-sys" = "1.1.3"' >> Cargo.toml
SDKROOT=/opt/MacOSX11.3.sdk cargo zigbuild --target=aarch64-apple-darwin
```

That gives me this output:
<details>
  <summary>Expand for full build output</summary>

    ~/Projects/scratch/cross-compilation-is-hard> SDKROOT=/opt/MacOSX11.3.sdk cargo zigbuild --target=aarch64-apple-darwin
    Compiling pkg-config v0.3.25
    Compiling libc v0.2.122
    Compiling cc v1.0.73
    Compiling libz-sys v1.1.5
    The following warnings were emitted during compilation:

    warning: In file included from src/zlib/adler32.c:8:
    warning: In file included from src/zlib/zutil.h:22:
    warning: In file included from src/zlib/zlib.h:34:
    warning: In file included from src/zlib/zconf.h:418:
    warning: /usr/include/limits.h:26:10: fatal error: 'bits/libc-header-start.h' file not found
    warning: #include <bits/libc-header-start.h>
    warning:          ^~~~~~~~~~~~~~~~~~~~~~~~~~
    warning: 1 error generated.

    error: failed to run custom build command for `libz-sys v1.1.5`

    Caused by:
    process didn't exit successfully: `/home/jeff/Projects/scratch/cross-compilation-is-hard/target/debug/build/libz-sys-20f025d1dcfd6497/build-script-build` (exit status: 1)
    --- stdout
    cargo:rerun-if-env-changed=LIBZ_SYS_STATIC
    cargo:rerun-if-changed=build.rs
    cargo:rerun-if-env-changed=ZLIB_NO_PKG_CONFIG
    cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS_aarch64-apple-darwin
    cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS_aarch64_apple_darwin
    cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_ALLOW_CROSS
    cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS
    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=TARGET_PKG_CONFIG
    cargo:rerun-if-env-changed=PKG_CONFIG
    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=TARGET_PKG_CONFIG_SYSROOT_DIR
    cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
    cargo-warning=pkg-config has not been configured to support cross-compilation.

    Install a sysroot for the target platform and configure it via
    PKG_CONFIG_SYSROOT_DIR and PKG_CONFIG_PATH, or install a
    cross-compiling wrapper for pkg-config and set it via
    PKG_CONFIG environment variable.
    TARGET = Some("aarch64-apple-darwin")
    OPT_LEVEL = Some("0")
    HOST = Some("x86_64-unknown-linux-gnu")
    CC_aarch64-apple-darwin = None
    CC_aarch64_apple_darwin = Some("/home/jeff/.cache/cargo-zigbuild/0.8.1/zigcc-aarch64-apple-darwin.sh")
    CFLAGS_aarch64-apple-darwin = None
    CFLAGS_aarch64_apple_darwin = None
    TARGET_CFLAGS = None
    CFLAGS = None
    CRATE_CC_NO_DEFAULTS = None
    DEBUG = Some("true")
    CARGO_CFG_TARGET_FEATURE = None
    running: "/home/jeff/.cache/cargo-zigbuild/0.8.1/zigcc-aarch64-apple-darwin.sh" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "-arch" "arm64" "-I" "src/zlib" "-fvisibility=hidden" "-DSTDC" "-D_LARGEFILE64_SOURCE" "-D_POSIX_SOURCE" "-D_C99_SOURCE" "-o" "/home/jeff/Projects/scratch/cross-compilation-is-hard/target/aarch64-apple-darwin/debug/build/libz-sys-2dcaba7036833c35/out/lib/src/zlib/adler32.o" "-c" "src/zlib/adler32.c"
    cargo:warning=In file included from src/zlib/adler32.c:8:
    cargo:warning=In file included from src/zlib/zutil.h:22:
    cargo:warning=In file included from src/zlib/zlib.h:34:
    cargo:warning=In file included from src/zlib/zconf.h:418:
    cargo:warning=/usr/include/limits.h:26:10: fatal error: 'bits/libc-header-start.h' file not found
    cargo:warning=#include <bits/libc-header-start.h>
    cargo:warning=         ^~~~~~~~~~~~~~~~~~~~~~~~~~
    cargo:warning=1 error generated.
    exit status: 1

    --- stderr


    error occurred: Command "/home/jeff/.cache/cargo-zigbuild/0.8.1/zigcc-aarch64-apple-darwin.sh" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "-arch" "arm64" "-I" "src/zlib" "-fvisibility=hidden" "-DSTDC" "-D_LARGEFILE64_SOURCE" "-D_POSIX_SOURCE" "-D_C99_SOURCE" "-o" "/home/jeff/Projects/scratch/cross-compilation-is-hard/target/aarch64-apple-darwin/debug/build/libz-sys-2dcaba7036833c35/out/lib/src/zlib/adler32.o" "-c" "src/zlib/adler32.c" with args "zigcc-aarch64-apple-darwin.sh" did not execute successfully (status code exit status: 1).
</details>

The most interesting part seems to be:

```
warning: In file included from src/zlib/adler32.c:8:
warning: In file included from src/zlib/zutil.h:22:
warning: In file included from src/zlib/zlib.h:34:
warning: In file included from src/zlib/zconf.h:418:
warning: /usr/include/limits.h:26:10: fatal error: 'bits/libc-header-start.h' file not found
warning: #include <bits/libc-header-start.h>
warning:          ^~~~~~~~~~~~~~~~~~~~~~~~~~
warning: 1 error generated.
```

...and...

```
cargo-warning=pkg-config has not been configured to support cross-compilation.

Install a sysroot for the target platform and configure it via
PKG_CONFIG_SYSROOT_DIR and PKG_CONFIG_PATH, or install a
cross-compiling wrapper for pkg-config and set it via
PKG_CONFIG environment variable.
```

And indeed if I change the invocation to this:

```
PKG_CONFIG_SYSROOT_DIR=/opt/MacOSX11.3.sdk cargo zigbuild --target=aarch64-apple-darwin
```

Or this:

```
PKG_CONFIG_SYSROOT_DIR=/opt/MacOSX11.3.sdk SDKROOT=/opt/MacOSX11.3.sdk cargo zigbuild --target=aarch64-apple-darwin
```

Then the minimal reproduction builds. So my first question would be: should cargo-zigbuild be settings the `PKG_CONFIG_SYSROOT_DIR` based on `SDKROOT`?

Second, this workaround doesn't work for all dependencies. If I replace "libz-sys" with "ring"...

```
# "libz-sys" = "1.1.3"
ring = "0.16.20"
```

...then I get this:

<details>
  <summary>Expand for full build output</summary>

    cargo clean && PKG_CONFIG_SYSROOT_DIR=/opt/MacOSX11.3.sdk SDKROOT=/opt/MacOSX11.3.sdk cargo zigbuild --target=aarch64-apple-darwin
    Compiling cc v1.0.73
    Compiling untrusted v0.7.1
    Compiling ring v0.16.20
    error: failed to run custom build command for `ring v0.16.20`

    Caused by:
    process didn't exit successfully: `/home/jeff/Projects/scratch/cross-compilation-is-hard/target/debug/build/ring-f696b55488b2d439/build-script-build` (exit status: 101)
    --- stdout
    OPT_LEVEL = Some("0")
    TARGET = Some("aarch64-apple-darwin")
    HOST = Some("x86_64-unknown-linux-gnu")
    CC_aarch64-apple-darwin = None
    CC_aarch64_apple_darwin = Some("/home/jeff/.cache/cargo-zigbuild/0.8.1/zigcc-aarch64-apple-darwin.sh")
    CFLAGS_aarch64-apple-darwin = None
    CFLAGS_aarch64_apple_darwin = None
    TARGET_CFLAGS = None
    CFLAGS = None
    CRATE_CC_NO_DEFAULTS = None
    DEBUG = Some("true")
    CARGO_CFG_TARGET_FEATURE = None
    OPT_LEVEL = Some("0")
    TARGET = Some("aarch64-apple-darwin")
    HOST = Some("x86_64-unknown-linux-gnu")
    CC_aarch64-apple-darwin = None
    CC_aarch64_apple_darwin = Some("/home/jeff/.cache/cargo-zigbuild/0.8.1/zigcc-aarch64-apple-darwin.sh")
    CFLAGS_aarch64-apple-darwin = None
    CFLAGS_aarch64_apple_darwin = None
    TARGET_CFLAGS = None
    CFLAGS = None
    CRATE_CC_NO_DEFAULTS = None
    DEBUG = Some("true")
    CARGO_CFG_TARGET_FEATURE = None
    OPT_LEVEL = Some("0")
    TARGET = Some("aarch64-apple-darwin")
    HOST = Some("x86_64-unknown-linux-gnu")
    CC_aarch64-apple-darwin = None
    CC_aarch64_apple_darwin = Some("/home/jeff/.cache/cargo-zigbuild/0.8.1/zigcc-aarch64-apple-darwin.sh")
    CFLAGS_aarch64-apple-darwin = None
    CFLAGS_aarch64_apple_darwin = None
    TARGET_CFLAGS = None
    CFLAGS = None
    CRATE_CC_NO_DEFAULTS = None
    DEBUG = Some("true")
    CARGO_CFG_TARGET_FEATURE = None
    OPT_LEVEL = Some("0")
    TARGET = Some("aarch64-apple-darwin")
    HOST = Some("x86_64-unknown-linux-gnu")
    CC_aarch64-apple-darwin = None
    CC_aarch64_apple_darwin = Some("/home/jeff/.cache/cargo-zigbuild/0.8.1/zigcc-aarch64-apple-darwin.sh")
    CFLAGS_aarch64-apple-darwin = None
    CFLAGS_aarch64_apple_darwin = None
    TARGET_CFLAGS = None
    CFLAGS = None
    CRATE_CC_NO_DEFAULTS = None
    DEBUG = Some("true")
    CARGO_CFG_TARGET_FEATURE = None
    OPT_LEVEL = Some("0")
    TARGET = Some("aarch64-apple-darwin")
    HOST = Some("x86_64-unknown-linux-gnu")
    CC_aarch64-apple-darwin = None
    CC_aarch64_apple_darwin = Some("/home/jeff/.cache/cargo-zigbuild/0.8.1/zigcc-aarch64-apple-darwin.sh")
    CFLAGS_aarch64-apple-darwin = None
    CFLAGS_aarch64_apple_darwin = None
    TARGET_CFLAGS = None
    CFLAGS = None
    CRATE_CC_NO_DEFAULTS = None
    DEBUG = Some("true")
    CARGO_CFG_TARGET_FEATURE = None
    OPT_LEVEL = Some("0")
    TARGET = Some("aarch64-apple-darwin")
    HOST = Some("x86_64-unknown-linux-gnu")
    CC_aarch64-apple-darwin = None
    CC_aarch64_apple_darwin = Some("/home/jeff/.cache/cargo-zigbuild/0.8.1/zigcc-aarch64-apple-darwin.sh")
    CFLAGS_aarch64-apple-darwin = None
    CFLAGS_aarch64_apple_darwin = None
    TARGET_CFLAGS = None
    CFLAGS = None
    CRATE_CC_NO_DEFAULTS = None
    DEBUG = Some("true")
    CARGO_CFG_TARGET_FEATURE = None
    OPT_LEVEL = Some("0")
    TARGET = Some("aarch64-apple-darwin")
    HOST = Some("x86_64-unknown-linux-gnu")
    CC_aarch64-apple-darwin = None
    CC_aarch64_apple_darwin = Some("/home/jeff/.cache/cargo-zigbuild/0.8.1/zigcc-aarch64-apple-darwin.sh")
    CFLAGS_aarch64-apple-darwin = None
    CFLAGS_aarch64_apple_darwin = None
    TARGET_CFLAGS = None
    CFLAGS = None
    CRATE_CC_NO_DEFAULTS = None
    DEBUG = Some("true")
    CARGO_CFG_TARGET_FEATURE = None
    OPT_LEVEL = Some("0")
    TARGET = Some("aarch64-apple-darwin")
    HOST = Some("x86_64-unknown-linux-gnu")
    CC_aarch64-apple-darwin = None
    CC_aarch64_apple_darwin = Some("/home/jeff/.cache/cargo-zigbuild/0.8.1/zigcc-aarch64-apple-darwin.sh")
    CFLAGS_aarch64-apple-darwin = None
    CFLAGS_aarch64_apple_darwin = None
    TARGET_CFLAGS = None
    CFLAGS = None
    CRATE_CC_NO_DEFAULTS = None
    DEBUG = Some("true")
    CARGO_CFG_TARGET_FEATURE = None
    OPT_LEVEL = Some("0")
    TARGET = Some("aarch64-apple-darwin")
    HOST = Some("x86_64-unknown-linux-gnu")
    CC_aarch64-apple-darwin = None
    CC_aarch64_apple_darwin = Some("/home/jeff/.cache/cargo-zigbuild/0.8.1/zigcc-aarch64-apple-darwin.sh")
    CFLAGS_aarch64-apple-darwin = None
    CFLAGS_aarch64_apple_darwin = None
    TARGET_CFLAGS = None
    CFLAGS = None
    CRATE_CC_NO_DEFAULTS = None
    DEBUG = Some("true")
    CARGO_CFG_TARGET_FEATURE = None
    OPT_LEVEL = Some("0")
    TARGET = Some("aarch64-apple-darwin")
    HOST = Some("x86_64-unknown-linux-gnu")
    CC_aarch64-apple-darwin = None
    CC_aarch64_apple_darwin = Some("/home/jeff/.cache/cargo-zigbuild/0.8.1/zigcc-aarch64-apple-darwin.sh")
    CFLAGS_aarch64-apple-darwin = None
    CFLAGS_aarch64_apple_darwin = None
    TARGET_CFLAGS = None
    CFLAGS = None
    CRATE_CC_NO_DEFAULTS = None
    DEBUG = Some("true")
    CARGO_CFG_TARGET_FEATURE = None

    --- stderr
    running "/home/jeff/.cache/cargo-zigbuild/0.8.1/zigcc-aarch64-apple-darwin.sh" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "-arch" "arm64" "-I" "include" "-Wall" "-Wextra" "-pedantic" "-pedantic-errors" "-Wall" "-Wextra" "-Wcast-align" "-Wcast-qual" "-Wconversion" "-Wenum-compare" "-Wfloat-equal" "-Wformat=2" "-Winline" "-Winvalid-pch" "-Wmissing-field-initializers" "-Wmissing-include-dirs" "-Wredundant-decls" "-Wshadow" "-Wsign-compare" "-Wsign-conversion" "-Wundef" "-Wuninitialized" "-Wwrite-strings" "-fno-strict-aliasing" "-fvisibility=hidden" "-fstack-protector" "-gfull" "-DNDEBUG" "-c" "-o/home/jeff/Projects/scratch/cross-compilation-is-hard/target/aarch64-apple-darwin/debug/build/ring-9e849a6d6158d1f2/out/aesv8-armx-ios64.o" "/home/jeff/.cargo/registry/src/github.com-1ecc6299db9ec823/ring-0.16.20/pregenerated/aesv8-armx-ios64.S"
    running "/home/jeff/.cache/cargo-zigbuild/0.8.1/zigcc-aarch64-apple-darwin.sh" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "-arch" "arm64" "-I" "include" "-Wall" "-Wextra" "-pedantic" "-pedantic-errors" "-Wall" "-Wextra" "-Wcast-align" "-Wcast-qual" "-Wconversion" "-Wenum-compare" "-Wfloat-equal" "-Wformat=2" "-Winline" "-Winvalid-pch" "-Wmissing-field-initializers" "-Wmissing-include-dirs" "-Wredundant-decls" "-Wshadow" "-Wsign-compare" "-Wsign-conversion" "-Wundef" "-Wuninitialized" "-Wwrite-strings" "-fno-strict-aliasing" "-fvisibility=hidden" "-fstack-protector" "-gfull" "-DNDEBUG" "-c" "-o/home/jeff/Projects/scratch/cross-compilation-is-hard/target/aarch64-apple-darwin/debug/build/ring-9e849a6d6158d1f2/out/ghashv8-armx-ios64.o" "/home/jeff/.cargo/registry/src/github.com-1ecc6299db9ec823/ring-0.16.20/pregenerated/ghashv8-armx-ios64.S"
    running "/home/jeff/.cache/cargo-zigbuild/0.8.1/zigcc-aarch64-apple-darwin.sh" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "-arch" "arm64" "-I" "include" "-Wall" "-Wextra" "-pedantic" "-pedantic-errors" "-Wall" "-Wextra" "-Wcast-align" "-Wcast-qual" "-Wconversion" "-Wenum-compare" "-Wfloat-equal" "-Wformat=2" "-Winline" "-Winvalid-pch" "-Wmissing-field-initializers" "-Wmissing-include-dirs" "-Wredundant-decls" "-Wshadow" "-Wsign-compare" "-Wsign-conversion" "-Wundef" "-Wuninitialized" "-Wwrite-strings" "-fno-strict-aliasing" "-fvisibility=hidden" "-fstack-protector" "-gfull" "-DNDEBUG" "-c" "-o/home/jeff/Projects/scratch/cross-compilation-is-hard/target/aarch64-apple-darwin/debug/build/ring-9e849a6d6158d1f2/out/vpaes-armv8-ios64.o" "/home/jeff/.cargo/registry/src/github.com-1ecc6299db9ec823/ring-0.16.20/pregenerated/vpaes-armv8-ios64.S"
    running "/home/jeff/.cache/cargo-zigbuild/0.8.1/zigcc-aarch64-apple-darwin.sh" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "-arch" "arm64" "-I" "include" "-Wall" "-Wextra" "-pedantic" "-pedantic-errors" "-Wall" "-Wextra" "-Wcast-align" "-Wcast-qual" "-Wconversion" "-Wenum-compare" "-Wfloat-equal" "-Wformat=2" "-Winline" "-Winvalid-pch" "-Wmissing-field-initializers" "-Wmissing-include-dirs" "-Wredundant-decls" "-Wshadow" "-Wsign-compare" "-Wsign-conversion" "-Wundef" "-Wuninitialized" "-Wwrite-strings" "-fno-strict-aliasing" "-fvisibility=hidden" "-fstack-protector" "-gfull" "-DNDEBUG" "-c" "-o/home/jeff/Projects/scratch/cross-compilation-is-hard/target/aarch64-apple-darwin/debug/build/ring-9e849a6d6158d1f2/out/armv8-mont-ios64.o" "/home/jeff/.cargo/registry/src/github.com-1ecc6299db9ec823/ring-0.16.20/pregenerated/armv8-mont-ios64.S"
    running "/home/jeff/.cache/cargo-zigbuild/0.8.1/zigcc-aarch64-apple-darwin.sh" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "-arch" "arm64" "-I" "include" "-Wall" "-Wextra" "-pedantic" "-pedantic-errors" "-Wall" "-Wextra" "-Wcast-align" "-Wcast-qual" "-Wconversion" "-Wenum-compare" "-Wfloat-equal" "-Wformat=2" "-Winline" "-Winvalid-pch" "-Wmissing-field-initializers" "-Wmissing-include-dirs" "-Wredundant-decls" "-Wshadow" "-Wsign-compare" "-Wsign-conversion" "-Wundef" "-Wuninitialized" "-Wwrite-strings" "-fno-strict-aliasing" "-fvisibility=hidden" "-fstack-protector" "-gfull" "-DNDEBUG" "-c" "-o/home/jeff/Projects/scratch/cross-compilation-is-hard/target/aarch64-apple-darwin/debug/build/ring-9e849a6d6158d1f2/out/chacha-armv8-ios64.o" "/home/jeff/.cargo/registry/src/github.com-1ecc6299db9ec823/ring-0.16.20/pregenerated/chacha-armv8-ios64.S"
    running "/home/jeff/.cache/cargo-zigbuild/0.8.1/zigcc-aarch64-apple-darwin.sh" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "-arch" "arm64" "-I" "include" "-Wall" "-Wextra" "-pedantic" "-pedantic-errors" "-Wall" "-Wextra" "-Wcast-align" "-Wcast-qual" "-Wconversion" "-Wenum-compare" "-Wfloat-equal" "-Wformat=2" "-Winline" "-Winvalid-pch" "-Wmissing-field-initializers" "-Wmissing-include-dirs" "-Wredundant-decls" "-Wshadow" "-Wsign-compare" "-Wsign-conversion" "-Wundef" "-Wuninitialized" "-Wwrite-strings" "-fno-strict-aliasing" "-fvisibility=hidden" "-fstack-protector" "-gfull" "-DNDEBUG" "-c" "-o/home/jeff/Projects/scratch/cross-compilation-is-hard/target/aarch64-apple-darwin/debug/build/ring-9e849a6d6158d1f2/out/ecp_nistz256-armv8-ios64.o" "/home/jeff/.cargo/registry/src/github.com-1ecc6299db9ec823/ring-0.16.20/pregenerated/ecp_nistz256-armv8-ios64.S"
    running "/home/jeff/.cache/cargo-zigbuild/0.8.1/zigcc-aarch64-apple-darwin.sh" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "-arch" "arm64" "-I" "include" "-Wall" "-Wextra" "-pedantic" "-pedantic-errors" "-Wall" "-Wextra" "-Wcast-align" "-Wcast-qual" "-Wconversion" "-Wenum-compare" "-Wfloat-equal" "-Wformat=2" "-Winline" "-Winvalid-pch" "-Wmissing-field-initializers" "-Wmissing-include-dirs" "-Wredundant-decls" "-Wshadow" "-Wsign-compare" "-Wsign-conversion" "-Wundef" "-Wuninitialized" "-Wwrite-strings" "-fno-strict-aliasing" "-fvisibility=hidden" "-fstack-protector" "-gfull" "-DNDEBUG" "-c" "-o/home/jeff/Projects/scratch/cross-compilation-is-hard/target/aarch64-apple-darwin/debug/build/ring-9e849a6d6158d1f2/out/ghash-neon-armv8-ios64.o" "/home/jeff/.cargo/registry/src/github.com-1ecc6299db9ec823/ring-0.16.20/pregenerated/ghash-neon-armv8-ios64.S"
    running "/home/jeff/.cache/cargo-zigbuild/0.8.1/zigcc-aarch64-apple-darwin.sh" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "-arch" "arm64" "-I" "include" "-Wall" "-Wextra" "-pedantic" "-pedantic-errors" "-Wall" "-Wextra" "-Wcast-align" "-Wcast-qual" "-Wconversion" "-Wenum-compare" "-Wfloat-equal" "-Wformat=2" "-Winline" "-Winvalid-pch" "-Wmissing-field-initializers" "-Wmissing-include-dirs" "-Wredundant-decls" "-Wshadow" "-Wsign-compare" "-Wsign-conversion" "-Wundef" "-Wuninitialized" "-Wwrite-strings" "-fno-strict-aliasing" "-fvisibility=hidden" "-fstack-protector" "-gfull" "-DNDEBUG" "-c" "-o/home/jeff/Projects/scratch/cross-compilation-is-hard/target/aarch64-apple-darwin/debug/build/ring-9e849a6d6158d1f2/out/sha512-armv8-ios64.o" "/home/jeff/.cargo/registry/src/github.com-1ecc6299db9ec823/ring-0.16.20/pregenerated/sha512-armv8-ios64.S"
    running "/home/jeff/.cache/cargo-zigbuild/0.8.1/zigcc-aarch64-apple-darwin.sh" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "-arch" "arm64" "-I" "include" "-Wall" "-Wextra" "-pedantic" "-pedantic-errors" "-Wall" "-Wextra" "-Wcast-align" "-Wcast-qual" "-Wconversion" "-Wenum-compare" "-Wfloat-equal" "-Wformat=2" "-Winline" "-Winvalid-pch" "-Wmissing-field-initializers" "-Wmissing-include-dirs" "-Wredundant-decls" "-Wshadow" "-Wsign-compare" "-Wsign-conversion" "-Wundef" "-Wuninitialized" "-Wwrite-strings" "-fno-strict-aliasing" "-fvisibility=hidden" "-fstack-protector" "-gfull" "-DNDEBUG" "-c" "-o/home/jeff/Projects/scratch/cross-compilation-is-hard/target/aarch64-apple-darwin/debug/build/ring-9e849a6d6158d1f2/out/sha256-armv8-ios64.o" "/home/jeff/.cargo/registry/src/github.com-1ecc6299db9ec823/ring-0.16.20/pregenerated/sha256-armv8-ios64.S"
    running "/home/jeff/.cache/cargo-zigbuild/0.8.1/zigcc-aarch64-apple-darwin.sh" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "-arch" "arm64" "-I" "include" "-Wall" "-Wextra" "-std=c1x" "-Wbad-function-cast" "-Wnested-externs" "-Wstrict-prototypes" "-pedantic" "-pedantic-errors" "-Wall" "-Wextra" "-Wcast-align" "-Wcast-qual" "-Wconversion" "-Wenum-compare" "-Wfloat-equal" "-Wformat=2" "-Winline" "-Winvalid-pch" "-Wmissing-field-initializers" "-Wmissing-include-dirs" "-Wredundant-decls" "-Wshadow" "-Wsign-compare" "-Wsign-conversion" "-Wundef" "-Wuninitialized" "-Wwrite-strings" "-fno-strict-aliasing" "-fvisibility=hidden" "-fstack-protector" "-gfull" "-DNDEBUG" "-c" "-o/home/jeff/Projects/scratch/cross-compilation-is-hard/target/aarch64-apple-darwin/debug/build/ring-9e849a6d6158d1f2/out/aes_nohw.o" "crypto/fipsmodule/aes/aes_nohw.c"
    In file included from crypto/fipsmodule/aes/aes_nohw.c:15:
    In file included from include/GFp/aes.h:52:
    In file included from include/GFp/base.h:66:
    /usr/include/stdint.h:26:10: fatal error: 'bits/libc-header-start.h' file not found
    #include <bits/libc-header-start.h>
            ^~~~~~~~~~~~~~~~~~~~~~~~~~
    1 error generated.
    thread 'main' panicked at 'execution failed', /home/jeff/.cargo/registry/src/github.com-1ecc6299db9ec823/ring-0.16.20/build.rs:656:9
    stack backtrace:
        0: std::panicking::begin_panic
                at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/std/src/panicking.rs:616:12
        1: build_script_build::run_command
                at ./build.rs:656:9
        2: build_script_build::compile
                at ./build.rs:512:13
        3: build_script_build::build_library::{{closure}}
                at ./build.rs:447:18
        4: core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &mut F>::call_once
                at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/core/src/ops/function.rs:280:13
        5: core::option::Option<T>::map
                at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/core/src/option.rs:906:29
        6: <core::iter::adapters::map::Map<I,F> as core::iter::traits::iterator::Iterator>::next
                at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/core/src/iter/adapters/map.rs:103:9
        7: alloc::vec::Vec<T,A>::extend_desugared
                at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/alloc/src/vec/mod.rs:2649:35
        8: <alloc::vec::Vec<T,A> as alloc::vec::spec_extend::SpecExtend<T,I>>::spec_extend
                at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/alloc/src/vec/spec_extend.rs:18:9
        9: <alloc::vec::Vec<T> as alloc::vec::spec_from_iter_nested::SpecFromIterNested<T,I>>::from_iter
                at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/alloc/src/vec/spec_from_iter_nested.rs:43:9
        10: <alloc::vec::Vec<T> as alloc::vec::spec_from_iter::SpecFromIter<T,I>>::from_iter
                at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/alloc/src/vec/spec_from_iter.rs:33:9
        11: <alloc::vec::Vec<T> as core::iter::traits::collect::FromIterator<T>>::from_iter
                at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/alloc/src/vec/mod.rs:2552:9
        12: core::iter::traits::iterator::Iterator::collect
                at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/core/src/iter/traits/iterator.rs:1778:9
        13: build_script_build::build_library
                at ./build.rs:443:16
        14: build_script_build::build_c_code::{{closure}}
                at ./build.rs:416:9
        15: <core::slice::iter::Iter<T> as core::iter::traits::iterator::Iterator>::for_each
                at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/core/src/slice/iter/macros.rs:211:21
        16: build_script_build::build_c_code
                at ./build.rs:415:5
        17: build_script_build::ring_build_rs_main
                at ./build.rs:279:5
        18: build_script_build::main
                at ./build.rs:240:13
        19: core::ops::function::FnOnce::call_once
                at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/core/src/ops/function.rs:227:5
    note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
</details>

Again, the most interesting bit here seems to be this:

```
  In file included from crypto/fipsmodule/aes/aes_nohw.c:15:
  In file included from include/GFp/aes.h:52:
  In file included from include/GFp/base.h:66:
  /usr/include/stdint.h:26:10: fatal error: 'bits/libc-header-start.h' file not found
  #include <bits/libc-header-start.h>
           ^~~~~~~~~~~~~~~~~~~~~~~~~~
```

So I'm left wondering... should anything even be able to include `/usr/include/stdint.h` at all here? And if not, how is that happening?

Also, this might be a red herring, but I found that it seemed to happen for more dependencies if the `zig` on my path was the actual zig compiler rather than the Snap wrapper, which made no sense to me because I couldn't figure out how the Snap wrapper would be altering the environment in any meaningful way. I can dig in and make a minimal reproduction of that weirdness if you think that might be helpful.

Thanks! :blush: 


Discussion / hints:
Thanks for the detailed report, I haven't tried cross compile from Linux to arm64 macOS yet, a minimal reproduction would be helpful.

You can also search [zig's issue tracker](https://github.com/ziglang/zig/issues) since it might be an upstream issue.

Fix patch:
diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml
index bde4371..cb716bd 100644
--- a/.github/workflows/CI.yml
+++ b/.github/workflows/CI.yml
@@ -89,8 +89,10 @@ jobs:
         run: |
           curl -sqL https://github.com/phracker/MacOSX-SDKs/releases/download/11.3/$SDK.tar.xz | tar -Jx
           export SDKROOT=$PWD/$SDK
+          export CFLAGS_aarch64_apple_darwin="-isysroot $SDKROOT"
 
           cargo run zigbuild --target aarch64-apple-darwin --manifest-path tests/hello-tls/Cargo.toml
+          cargo run zigbuild --target aarch64-apple-darwin --manifest-path tests/hello-rustls/Cargo.toml
       - name: Linux - Test x86_64 build
         if: matrix.os == 'ubuntu-latest'
         run: |
diff --git a/src/build.rs b/src/build.rs
index 10c64ef..5026b8c 100644
--- a/src/build.rs
+++ b/src/build.rs
@@ -1,3 +1,4 @@
+use std::env;
 use std::ops::{Deref, DerefMut};
 use std::path::{Path, PathBuf};
 use std::process::{self, Command};
@@ -203,6 +204,17 @@ impl Build {
                         build.env("WINAPI_NO_BUNDLED_LIBRARIES", "1");
                     }
 
+                    if raw_target.contains("apple-darwin") {
+                        if let Some(sdkroot) = env::var_os("SDKROOT") {
+                            if !sdkroot.is_empty()
+                                && env::var_os("PKG_CONFIG_SYSROOT_DIR").is_none()
+                            {
+                                // Set PKG_CONFIG_SYSROOT_DIR for pkg-config crate
+                                build.env("PKG_CONFIG_SYSROOT_DIR", sdkroot);
+                            }
+                        }
+                    }
+
                     // Enable unstable `target-applies-to-host` option automatically for nightly Rust
                     // when target is the same as host but may have specified glibc version
                     if host_target == raw_target
diff --git a/src/zig.rs b/src/zig.rs
index c129e76..e1368fe 100644
--- a/src/zig.rs
+++ b/src/zig.rs
@@ -130,13 +130,22 @@ impl Zig {
         }
 
         if is_macos {
-            if let Ok(sdkroot) = env::var("SDKROOT") {
-                new_cmd_args.extend_from_slice(&[
-                    format!("--sysroot={}", sdkroot),
-                    "-I/usr/include".to_string(),
-                    "-L/usr/lib".to_string(),
-                    "-F/System/Library/Frameworks".to_string(),
-                ]);
+            if let Some(sdkroot) = env::var_os("SDKROOT") {
+                if !sdkroot.is_empty() {
+                    let sdkroot = Path::new(&sdkroot);
+                    new_cmd_args.extend_from_slice(&[
+                        format!("-I{}", sdkroot.join("usr").join("include").display()),
+                        format!("-L{}", sdkroot.join("usr").join("lib").display()),
+                        format!(
+                            "-F{}",
+                            sdkroot
+                                .join("System")
+                                .join("Library")
+                                .join("Frameworks")
+                                .display()
+                        ),
+                    ]);
+                }
             }
         }
 


Test patch:
diff --git a/tests/hello-rustls/Cargo.lock b/tests/hello-rustls/Cargo.lock
index 4726fd5..f6ee6a0 100644
--- a/tests/hello-rustls/Cargo.lock
+++ b/tests/hello-rustls/Cargo.lock
@@ -131,6 +131,7 @@ checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
 name = "hello-rustls"
 version = "0.1.0"
 dependencies = [
+ "libz-sys",
  "reqwest",
  "tokio",
 ]
@@ -269,6 +270,18 @@ version = "0.2.119"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "1bf2e165bb3457c8e098ea76f3e3bc9db55f87aa90d52d0e6be741470916aaa4"
 
+[[package]]
+name = "libz-sys"
+version = "1.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6f35facd4a5673cb5a48822be2be1d4236c1c99cb4113cab7061ac720d5bf859"
+dependencies = [
+ "cc",
+ "libc",
+ "pkg-config",
+ "vcpkg",
+]
+
 [[package]]
 name = "log"
 version = "0.4.14"
@@ -361,6 +374,12 @@ version = "0.1.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
 
+[[package]]
+name = "pkg-config"
+version = "0.3.25"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae"
+
 [[package]]
 name = "proc-macro2"
 version = "1.0.36"
@@ -670,6 +689,12 @@ dependencies = [
  "percent-encoding",
 ]
 
+[[package]]
+name = "vcpkg"
+version = "0.2.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
+
 [[package]]
 name = "want"
 version = "0.3.0"
diff --git a/tests/hello-rustls/Cargo.toml b/tests/hello-rustls/Cargo.toml
index 51187d3..3a31637 100644
--- a/tests/hello-rustls/Cargo.toml
+++ b/tests/hello-rustls/Cargo.toml
@@ -8,3 +8,6 @@ edition = "2018"
 [dependencies]
 reqwest = { version = "0.11.9", default-features = false, features = ["rustls-tls"] }
 tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
+
+[target.'cfg(target_os = "macos")'.dependencies]
+libz-sys = "1.1.5"
diff --git a/tests/hello-rustls/src/main.rs b/tests/hello-rustls/src/main.rs
index 2ee2c92..2ec5bd2 100644
--- a/tests/hello-rustls/src/main.rs
+++ b/tests/hello-rustls/src/main.rs
@@ -1,3 +1,6 @@
+#[cfg(target_os = "macos")]
+use libz_sys as _;
+
 #[tokio::main]
 async fn main() {
     let response = reqwest::Client::new()