zig icon indicating copy to clipboard operation
zig copied to clipboard

`zig cc` fails to compile simple programs on macOS Catalina and Big Sur

Open carlocab opened this issue 4 years ago • 5 comments

Zig Version

0.9.0

Steps to Reproduce

Compile the following program with zig cc hello.c -o hello on macOS Catalina or Big Sur (not Monterey):

// hello.c
#include <stdio.h>
int main() {
  fprintf(stdout, "Hello, world!");
  return 0;
}

Expected Behavior

No error.

Actual Behavior

Compilation fails with the following error.

  /Library/Developer/CommandLineTools/SDKs/MacOSX11.sdk/usr/include/stdio.h:220:5: error: 'TARGET_OS_IPHONE' is not defined, evaluates to 0 [-Werror,-Wundef-prefix=TARGET_OS_]
  #if TARGET_OS_IPHONE
      ^

CI logs available at https://github.com/Homebrew/homebrew-core/actions/runs/1604777034. See Homebrew/homebrew-core#91763.

carlocab avatar Dec 21 '21 12:12 carlocab

Incidentally, this is the same error as in #8999.

carlocab avatar Dec 21 '21 13:12 carlocab

This does not repro for me on Catalina, with a zig built from source, using homebrew-provided LLVM. Can you share any information to help us repro the issue?

andrewrk avatar Dec 23 '21 20:12 andrewrk

Thanks for having a look.

Do you have both the CLT and Xcode.app installed? If you have the CLT installed, can you try doing some combination of

export SDKROOT=/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk
export CPATH=/usr/local/include:/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include

before trying to do zig cc?

You can also try downloading the built bottle from here. Scroll down to "Artifacts" and then click on the one labelled "bottles-10.15".

You can install the bottle with

xattr -c /path/to/zig--0.9.0.catalina.bottle.tar.gz
brew install /path/to/zig--0.9.0.catalina.bottle.tar.gz

and then run our test with brew test zig. (You might need to check out the zig PR with something like cd "$(brew --repo homebrew/core) && gh pr checkout 91763 first.)

I suspect one of the env variables we set in the test environment is causing this. Here's what might be relevant:

PKG_CONFIG_LIBDIR=/usr/local/lib/pkgconfig:/usr/local/share/pkgconfig:/usr/local/Homebrew/Library/Homebrew/os/mac/pkgconfig/12:/usr/lib/pkgconfig
MAKEFLAGS=-j4
CPPFLAGS=-F/usr/local/Frameworks -isysroot/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk
LDFLAGS=-L/usr/local/lib -F/usr/local/Frameworks -Wl,-headerpad_max_install_names -isysroot/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk
CMAKE_FRAMEWORK_PATH=/usr/local/Frameworks:/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk/System/Library/Frameworks
CFLAGS=-Os -w -pipe -march=nehalem -mmacosx-version-min=12 -isysroot/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk
CXXFLAGS=-Os -w -pipe -march=nehalem -mmacosx-version-min=12 -isysroot/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk
OBJCFLAGS=-Os -w -pipe -march=nehalem -mmacosx-version-min=12 -isysroot/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk
OBJCXXFLAGS=-Os -w -pipe -march=nehalem -mmacosx-version-min=12 -isysroot/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk
OBJC=/usr/bin/clang
CC=/usr/bin/clang
OBJCXX=/usr/bin/clang++
CXX=/usr/bin/clang++
LC_CTYPE=C
CPATH=/usr/local/include:/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk/usr/include
SDKROOT=/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk
CMAKE_PREFIX_PATH=/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk/usr

You can see the full list of environment variables by applying this patch to $(brew --repo homebrew/core):

diff --git a/Formula/zig.rb b/Formula/zig.rb
index 9acd19e861d..0670f2ed51b 100644
--- a/Formula/zig.rb
+++ b/Formula/zig.rb
@@ -34,6 +34,9 @@ class Zig < Formula
   end
 
   test do
+    ENV.each do |k,v|
+      puts "#{k}=#{v}"
+    end
     (testpath/"hello.zig").write <<~EOS
       const std = @import("std");
       pub fn main() !void {

carlocab avatar Dec 24 '21 15:12 carlocab

Based on CI at Homebrew/homebrew-core#91763, it seems that it's the CPATH setting that's the culprit. My guess is that it ends up mixing headers from Xcode and the CLT and that breaks things.

carlocab avatar Dec 24 '21 16:12 carlocab

Thanks, this helps a ton! I think on our end we will start scrubbing some of these environment variables before invoking clang_main because Zig is supposed to be in charge of all include paths.

andrewrk avatar Dec 24 '21 20:12 andrewrk

issue resolved: it was caused by an errant CPATH environment variable

mikdusan avatar Jan 02 '23 00:01 mikdusan

issue resolved: it was caused by an errant CPATH environment variable

Did issue get resolved on zig side? Any pointers?

chenrui333 avatar Feb 25 '23 08:02 chenrui333