ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
32.02k stars 2.34k forks source link

zig 0.9.1 compilation error: 'curl/curl.h' file not found on macOS #10946

Open messense opened 2 years ago

messense commented 2 years ago

Zig Version

0.9.1

Steps to Reproduce

  1. Install homebrew
  2. Update zig formuae to 0.9.1
diff --git a/Formula/zig.rb b/Formula/zig.rb
index 7219aa707b6feb..3d2d46e236219b 100644
--- a/Formula/zig.rb
+++ b/Formula/zig.rb
@@ -4,8 +4,8 @@ class Zig < Formula
   license "MIT"

   stable do
-    url "https://ziglang.org/download/0.9.0/zig-0.9.0.tar.xz"
-    sha256 "cd1be83b12f8269cc5965e59877b49fdd8fa638efb6995ac61eb4cea36a2e381"
+    url "https://ziglang.org/download/0.9.1/zig-0.9.1.tar.xz"
+    sha256 "38cf4e84481f5facc766ba72783e7462e08d6d29a5d47e3b75c8ee3142485210"

     depends_on "llvm"
   end
  1. brew install --build-from-source zig
  2. brew install --build-from-source fastfec errors

Homebrew maintainer suggests we report issue here just in case it's a regression of zig.

Expected Behavior

Build successful.

Actual Behavior

❯ brew install --build-from-source fastfec
==> Downloading https://github.com/washingtonpost/FastFEC/archive/refs/tags/0.0.4.tar.gz
Already downloaded: /Users/messense/Library/Caches/Homebrew/downloads/2daf14e6ef92015df24ccb0b6db22ebd4c8c8ca1ed9f3b379c12388d71bbd8be--FastFEC-0.0.4.tar.gz
==> zig build -Dvendored-pcre=false
Last 15 lines from /Users/messense/Library/Logs/Homebrew/fastfec/01.zig:
zig
build
-Dvendored-pcre=false

error(compilation): clang failed with stderr: In file included from /private/tmp/fastfec-20220220-94559-1v2xpjh/FastFEC-0.0.4/src/main.c:1:
/private/tmp/fastfec-20220220-94559-1v2xpjh/FastFEC-0.0.4/src/urlopen.h:4:10: fatal error: 'curl/curl.h' file not found

error(compilation): clang failed with stderr: /private/tmp/fastfec-20220220-94559-1v2xpjh/FastFEC-0.0.4/src/urlopen.c:52:10: fatal error: 'curl/curl.h' file not found

/private/tmp/fastfec-20220220-94559-1v2xpjh/FastFEC-0.0.4/src/main.c:1:1: error: unable to build C object: clang exited with code 1
/private/tmp/fastfec-20220220-94559-1v2xpjh/FastFEC-0.0.4/src/urlopen.c:1:1: error: unable to build C object: clang exited with code 1
fastfec...The following command exited with error code 1:
/opt/homebrew/Cellar/zig/0.9.1/bin/zig build-exe -lc -lcurl -lpcre -cflags -std=c11 -pedantic -Wall -W -Wno-missing-field-initializers -- /private/tmp/fastfec-20220220-94559-1v2xpjh/FastFEC-0.0.4/src/buffer.c /private/tmp/fastfec-20220220-94559-1v2xpjh/FastFEC-0.0.4/src/memory.c /private/tmp/fastfec-20220220-94559-1v2xpjh/FastFEC-0.0.4/src/encoding.c /private/tmp/fastfec-20220220-94559-1v2xpjh/FastFEC-0.0.4/src/csv.c /private/tmp/fastfec-20220220-94559-1v2xpjh/FastFEC-0.0.4/src/writer.c /private/tmp/fastfec-20220220-94559-1v2xpjh/FastFEC-0.0.4/src/fec.c -cflags -std=c11 -pedantic -Wall -W -Wno-missing-field-initializers -- /private/tmp/fastfec-20220220-94559-1v2xpjh/FastFEC-0.0.4/src/urlopen.c /private/tmp/fastfec-20220220-94559-1v2xpjh/FastFEC-0.0.4/src/main.c --cache-dir /private/tmp/fastfec-20220220-94559-1v2xpjh/FastFEC-0.0.4/zig-cache --global-cache-dir /private/tmp/fastfec-20220220-94559-1v2xpjh/FastFEC-0.0.4/.brew_home/.cache/zig --name fastfec -I /opt/homebrew/Cellar/pcre/8.45/include -L /opt/homebrew/Cellar/pcre/8.45/lib --enable-cache
error: the following build command failed with exit code 1:
/private/tmp/fastfec-20220220-94559-1v2xpjh/FastFEC-0.0.4/zig-cache/o/0eec347ab0fa7a1d401b17d76f7925cd/build /opt/homebrew/Cellar/zig/0.9.1/bin/zig /private/tmp/fastfec-20220220-94559-1v2xpjh/FastFEC-0.0.4 /private/tmp/fastfec-20220220-94559-1v2xpjh/FastFEC-0.0.4/zig-cache /private/tmp/fastfec-20220220-94559-1v2xpjh/FastFEC-0.0.4/.brew_home/.cache/zig -Dvendored-pcre=false
carlocab commented 2 years ago

As seen in Homebrew/homebrew-core#95485, this can seemingly be fixed by installing Homebrew curl and then passing the equivalent of --search-prefix "$(brew --prefix curl)" to zig build.

However, this isn't really a proper fix because, while the error about not being able to find the curl.h error goes away, you then produce binaries that link with /usr/lib/libcurl.4.dylib, which is strongly indicative of usage of Homebrew curl headers but linkage with a non-Homebrew libcurl.

kubkon commented 5 months ago

@messense can you confirm this is still an issue with latest zig master?

yamashitax commented 1 month ago

@kubkon I am going to hijack this issue since it might at least be somewhat related.

I am encountering issues when using installHeadersDirectory and installHeaders where header files are not properly installed.

build.zig

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const lib = b.addStaticLibrary(.{
        .name = "zig-include-headers-repro",
        .target = target,
        .optimize = optimize,
    });

    lib.installHeader(b.path("headers/header.h"), "header.h");
    lib.addCSourceFile(.{
        .file = b.path("main.c"),
    });

    b.installArtifact(lib);
}

const std = @import("std");

main.c

#include "header.h"
#include <stdio.h>

int main()
{
  printf("Hello %d", VERSION);
  return 0;
}

headers/header.h

#define VERSION 20240514

zig build produces the following stacktrace

yamashita@yamashitas-MacBook-Air> zig build
install
└─ install zig-include-headers-repro
   └─ zig build-lib zig-include-headers-repro Debug native 1 errors
/Users/yamashita/Projects/zig-include-headers-repro/main.c:1:10: error: 'header.h' file not found
#include "header.h"
         ^~~~~~~~~~~
error: the following command failed with 1 compilation errors:
/nix/store/3x34axfakg6cwlzrj4nbc4mygwmaxn34-zig-0.13.0-dev.39+f6f7a47aa/bin/zig build-lib /Users/yamashita/Projects/zig-include-headers-repro/main.c -ODebug -Mroot --cache-dir /Users/yamashita/Projects/zig-include-headers-repro/zig-cache --global-cache-dir /Users/yamashita/.cache/zig --name zig-include-headers-repro -static --listen=-
Build Summary: 0/3 steps succeeded; 1 failed (disable with --summary none)
install transitive failure
└─ install zig-include-headers-repro transitive failure
   └─ zig build-lib zig-include-headers-repro Debug native 1 errors
error: the following build command failed with exit code 1:
/Users/yamashita/Projects/zig-include-headers-repro/zig-cache/o/a162f7a33bd7f3dcdcd5696b1d6ca721/build /nix/store/3x34axfakg6cwlzrj4nbc4mygwmaxn34-zig-0.13.0-dev.39+f6f7a47aa/bin/zig /Users/yamashita/Projects/zig-include-headers-repro /Users/yamashita/Projects/zig-include-headers-repro/zig-cache /Users/yamashita/.cache/zig --seed 0x433abc0d -Z21d1e8ed1ec422ee

Running zig cc produces the desired result:

yamashita@yamashitas-MacBook-Air> zig cc main.c -I./headers && ./a.out
Hello 20240514%

~You should be able to reproduce it using https://github.com/andrewrk/ffmpeg. Also relevant: https://github.com/andrewrk/ffmpeg/issues/14~. EDIT: This was an oversight by me, PRCTL and Metal flags are not properly set in that repo for building on Darwin which was generating errors. Trying to build with appropriate flags fails with undefined reference to symbol as observed here: https://github.com/andrewrk/ffmpeg/pull/11#issuecomment-1861013547.

Running macOS 14.4.1 (23E224) on Apple M1. Tested on 0.13.0-dev.39+f6f7a47aa.