ziglang / zig

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

macho: support -bundle flag #15868

Open kassane opened 1 year ago

kassane commented 1 year ago

Zig Version

0.11.0-dev.3301+230ea411f

Steps to Reproduce and Observed Behavior

I was trying building cmake using zig (cc/c++).

MacOS linking error: https://github.com/kassane/CMake/actions/runs/5092120988/jobs/9153051617#step:4:1360

[ 30%] Building CXX object Source/kwsys/CMakeFiles/cmsysTestsCxx.dir/testConfigure.cxx.o
error(link): undefined reference to symbol '_main'
error: UndefinedSymbolReference
make[2]: *** [Source/kwsys/libcmsysTestDynload.so] Error 1

How to run test

My fork has only the zigcc and zigcxx scripts in addition to the CI build test. I haven't done any build.zig experiments yet. https://github.com/Kitware/CMake/compare/master...kassane:CMake:zig

Method 1 - bootstrap

git clone https://github.com/kassane/CMake
cd CMake
CXX=$PWD/Scripts/zigcxx.sh CC=$PWD/Scripts/zigcc.sh ./bootstrap && make -j 4

Method 2 - cmake to build cmake

git clone https://github.com/kassane/CMake
cd CMake
cmake -B build -DCMAKE_CXX_COMPILER=$PWD/Scripts/zigcxx.cmd -DCMAKE_C_COMPILER=$PWD/Scripts/zigcc.cmd
cmake --build build --parallel

cc: @kubkon

Expected Behavior

Works build same Linux and Windows. See: https://github.com/kassane/CMake/actions/runs/5092120988

kubkon commented 10 months ago

Here's the issue:

zig ld -dynamic -platform_version macos 13.6.0 13.1 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk -headerpad_max_install_names -dead_strip -e _main CMakeFiles/cmsysTestDynload.dir/testDynload.c.o /Users/jakubkonka/.cache/zig/o/7cc1d883f1250cf3ba0440c375ea16ee/libcompiler_rt.a -o libcmsysTestDynload.so -lSystem -L/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk/usr/lib -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk/System/Library/Frameworks

For some reason, the linker doesn't receive the flag -dylib that we're really linking a shared object and not an executable.

kubkon commented 10 months ago

And here's the reason for the failure:

/Users/jakubkonka/dev/CMake/Scripts/zigcc.sh -O3 -DNDEBUG -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk -bundle -Wl,-headerpad_max_install_names -o libcmsysTestDynload.so CMakeFiles/cmsysTestDynload.dir/testDynload.c.o

CMake wants to generate a -bundle which we don't support yet. I'll move this issue to 0.13 milestone as this will most likely involve a little bit more work.