uber / hermetic_cc_toolchain

Bazel C/C++ toolchain for cross-compiling C/C++ programs
MIT License
300 stars 45 forks source link

macos: missing resolv.h #131

Closed sluongng closed 11 months ago

sluongng commented 1 year ago

See https://buildbuddy.buildbuddy.io/invocation/c6420695-476d-426b-8a7d-579220dc612f?actionDigest=648cda0569553603f5dec696187ed931ff0c521bad72caa9cfbf3c32dab687a5%2F183&actionResultDigest=dfac351eeb1fd8186001dcf2088eec77d38f8b39576bdcf5f9d895c6cca643a0%2F183#action for detail of failing action.

ERROR: /private/var/tmp/_bazel_sluongng/06e573a93bc2d6a9cad4ad41f00b4310/external/io_bazel_rules_go/BUILD.bazel:42:7: GoStdlib external/io_bazel_rules_go/stdlib_/pkg [for tool] failed: (Exit 1): builder failed: error executing command (from target @io_bazel_rules_go//:stdlib)
  (cd /private/var/tmp/_bazel_sluongng/06e573a93bc2d6a9cad4ad41f00b4310/execroot/buildbuddy && \
  exec env - \
    CC=external/zig_sdk/tools/aarch64-macos-none/c++ \
    CGO_CFLAGS='-no-canonical-prefixes -Wno-builtin-macro-redefined -D__DATE__="redacted" -D__TIMESTAMP__="redacted" -D__TIME__="redacted" -mcpu=apple_m1 -O2 -DNDEBUG' \
    CGO_ENABLED=1 \
    CGO_LDFLAGS='-Wl,-headerpad_max_install_names -O2 -DNDEBUG' \
    GOARCH=arm64 \
    GOEXPERIMENT=nocoverageredesign \
    GOOS=darwin \
    GOPATH='' \
    GOROOT=external/go_sdk_darwin_arm64 \
    GOROOT_FINAL=GOROOT \
    GOTOOLCHAIN=local \
    PATH=external/zig_sdk/tools:external/zig_sdk/tools/aarch64-macos-none:/bin:/usr/bin \
  bazel-out/macos_arm64-opt-exec-5213AA54-ST-6d27f6f190b5/bin/external/go_sdk_darwin_arm64/builder_reset/builder stdlib -sdk external/go_sdk_darwin_arm64 -installsuffix darwin_arm64 -out bazel-out/macos_arm64-opt-exec-5213AA54-ST-6d27f6f190b5/bin/external/io_bazel_rules_go/stdlib_ -package std -package runtime/cgo -gcflags '')
# Configuration: fc8217665648e5b159391c23607348f6fa3f0b1fbff682755523ba4fafcb3d0c
# Execution platform: //platforms:macos_arm64
# net
bazel-out/macos_arm64-opt-exec-5213AA54-ST-6d27f6f190b5/bin/external/io_bazel_rules_go/stdlib_/src/net/cgo_unix_cgo_darwin.go:10:10: fatal error: 'resolv.h' file not found
#include <resolv.h>
         ^~~~~~~~~~

Possibly related to https://github.com/ziglang/zig/issues/16674 and/or https://github.com/ziglang/zig/issues/12628

motiejus commented 1 year ago

Looking at https://github.com/ziglang/zig/pull/17152, looks like resolv.h was removed by design. Is there something we can do besides #10?

Today's zig source (https://github.com/ziglang/zig/commit/7c5d01b95e3cb47187726c52dae4abb2e5b4faaf) shows that there is no MacOS-related resolv.h:

$ git ls-files | grep resolv.h
lib/libc/include/generic-glibc/resolv.h
lib/libc/include/generic-musl/resolv.h
lib/libc/musl/include/resolv.h
lib/libc/musl/src/include/resolv.h
lib/libc/wasi/libc-top-half/musl/include/resolv.h
lib/libc/wasi/libc-top-half/musl/src/include/resolv.h
$
motiejus commented 1 year ago

Also, https://github.com/ziglang/zig/issues/12628 is linux-specific and is only linking step, so unrelated here.

sluongng commented 1 year ago

We were using v2.1.2 of this repo, so we are only on Zig v0.11.0 and not yet affected by the change in https://github.com/ziglang/zig/pull/17152 ?

I am not sure if #10 is the only solution but it might be one solution.

> fd resolv.h /Library/Developer/CommandLineTools
/Library/Developer/CommandLineTools/SDKs/MacOSX14.0.sdk/usr/include/resolv.h
/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/resolv.h
/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/resolv.h
motiejus commented 1 year ago

Same with 0.11.0:

$ git ls-files | grep resolv.h
lib/libc/include/generic-glibc/resolv.h
lib/libc/include/generic-musl/resolv.h
lib/libc/musl/include/resolv.h
lib/libc/musl/src/include/resolv.h
lib/libc/wasi/libc-top-half/musl/include/resolv.h
lib/libc/wasi/libc-top-half/musl/src/include/resolv.h
$ 
JonathanHope commented 1 year ago

I can into this as well. As near as I can tell there is no easy way to cross compile Go programs for macos because of this. It always fails with the error:

/nix/store/dwmb0qcai52d0zkgpm6f5ifx2a8yvsdg-go-1.21.3/share/go/src/net/cgo_unix_cgo_darwin.go:10:10: fatal error: 'resolv.h' file not found
#include <resolv.h>
         ^~~~~~~~~~
1 error generate

It would definitely be nice to be able to use the Zig compiler to handle the CGo cross compile duties.

JonathanHope commented 1 year ago

One thing I tried here was to use native Go network stack: -tags=netgo. Unfortunately that just gave me a different error:

/nix/store/dwmb0qcai52d0zkgpm6f5ifx2a8yvsdg-go-1.21.3/share/go/pkg/tool/linux_amd64/link: running zig failed: exit status 1
error: unable to find Dynamic system library 'resolv' using strategy 'paths_first'. searched paths: none
oh-tarnished commented 11 months ago

I guess we need to wait out until zig-12 gets released.

https://github.com/ziglang/zig/milestone/23

motiejus commented 11 months ago

As mentioned in #10, there are big, known problems when compiling to OSX. I clarified the link to #10 in #135, so this issue does not bring bring any extra value.