Has anyone tried build Zig from source on macOS without prebuilt Zig?

It’s the problem only on macOS. I want learn some experience of you guys cuz it’s kind of annoying with these factors together:

  • I don’t want to use Homebrew to fetch libraries for some reason
  • CMake will always find libzstd.a at /opt/homebrew/lib/libzstd.a even though it’s missing there and ever didn’t give an error about that. Well it also didn’t try to locate usr/local/lib/libzstd.a.
  • I tried symlink it to path/to/llvm/lib/libzstd.a but it didn’t work.

Reproduce

I wrote a tiny readable Fish script:

#
# Build Zig from source on macOS
#

# https://github.com/llvm/llvm-project/releases/download/
set prefix $HOME/local/llvm

# It's really annoying that /opt/homebrew/lib/libzstd.a always returned
# though it's missing there (built at /usr/local/lib/libzstd.a instead).
# I tried to _ignore_ the Homebrew prefix but didn't work.
# I guess it's the same problem as for finding LLVM_CONFIG_EXE.
#
# It should work but unfortunately no.


function build_stage3
    cmake .. \
        -DCMAKE_BUILD_TYPE=Release \
        -DCMAKE_PREFIX_PATH=$prerix \
        # cmake/Findllvm.cmake didn't find LLVM_CONFIG_EXE ($prefix/bin/llvm-config)
        # even though CMAKE_PREFIX_PATH specified unless we pass CMAKE_PROGRAM_PATH.
        -DCMAKE_PROGRAM_PATH=$prefix/bin \
        # macOS doesn't ship expected static libraries here in either Xcode or
        # system framewroks.
        -DCMAKE_C_COMPILER=$prefix/bin/clang \
        -DCMAKE_CXX_COMPILER=$prefix/bin/clang \
        -DCMAKE_LINKER=$prefix/bin/lld \
        -DZIG_STATIC_ZSTD=ON \
        -DZIG_NO_LIB=ON \
        -GNinja
    ninja install
end

function build_stage4
    state3/bin/zig build \
        -p stage4 \
        -Denable-llvm \
        -Dno-lib \
        --watch \
        -fincremental
end

mkdir build
cd build
build_stage3
and build_stage4
or echo $status

Edit: remove the symlink and add DZIG_STATIC_ZSTD=ON opton.

Sorry it sounds like it’s been a pain for you!

I do habitually use homebrew so maybe that’s part of why it’s not so bad for me (I don’t remember needing to install anything in particular, although probably it’s finding llvm there?) but anyway my process is just

cd build
cmake .. -DZIG_STATIC_ZSTD=ON
make -j8

and then wait. seems to go fine for me.

Currently it’s because of finding libzstd.a, nothing bad with llvm related now (workround by pass -DCMAKE_PROGRAM_PATH to find llvm-config). I thought it’s because of CMake itself. When to locate libzstd.a, it always chose the path /opt/homebrew/lib/libzstd.a and I want to guide CMake to find it at path /usr/local/bin/libzstd.a.

There are two problems: 1. The /opt/homebrew/lib/libzstd.a is missing there (deliberately) and CMake didn’t throw an error about that 2. It didn’t try to search /usr/local/lib/libzstd.a

if you look carefully, this sounds suspiciously like the problem that the difference between your shell script and my command is designed to fix :slight_smile:

I’ll add a Bash script later.

… girl.

what i am telling you is

“try adding -DZIG_STATIC_ZSTD=ON to your shell script”

sorry that i am doing so sassily, that seems to be causing a misunderstanding.

1 Like

Yeah I see:

//Path to a library.
ZSTD:FILEPATH=/usr/local/lib/libzstd.a

But…

       File: log.txt
   1 + ninja: error: '/opt/homebrew/lib/libzstd.a', needed by 'zig2', missing and no known rul
       e to make it

I did rm -rf build/.

Edit: just remove the symlink.

It seems that ninja still find libzstd.a at path /opt/homebrew/lib/libzstd.a which maybe the problem.