Skip to content

zig cc: unsupported darwin linker args #4841

Closed

Description

@opti-mix

Thanks a lot for adding zig cc and zig c++ functionality. It is really useful.

I tried to use zig c++ instead of the standard LLVM toolchain for one of my projects. I build it on MacOS. In general, it mostly works, but I've discovered the following issues:

  1. There is a number of flags passed to the linker using -Wl,my_linker_option. They are important for proper linking of the project. But some of them are not supported yet.
warning: unsupported linker arg: -search_paths_first
warning: unsupported linker arg: -headerpad_max_install_names
warning: unsupported linker arg: -force_load
warning: unsupported linker arg: -force_load
  1. If I configure my CMake-based project to use make and then run make, it seems to work fine as long as make is using just a single thread. But if I use e.g. make -j 2 I start seeing some issues:
zig -c test.cpp -std=c++14 -ffast-math -fno-finite-math-only -g -I/path/to/include -emit-llvm -O0 -o test.o
Invalid argument: -c

It seems as if there is a race condition somewhere. As you can seen in zig's command line it misses c++ after zig somehow.

  1. If I configure my CMake-based project to use ninja and then run ninja, I see issues with dependency tracking supported by clang. Specifically, there seems to be an issue with handling of -MF flags (see below). Same project can be build just fine if I use the vanilla LLVM toolchain.
zig: error: no such file or directory: 'test.cpp.o.d'

The following command failed when running ninja:
zig clang -c -MD -MV -MF zig-cache/tmp/eamNMvLXEZYC-test.o.d  -MD -MT -MF test.cpp.o.d -o zig-cache/tmp/eamNMvLXEZYC-test.o test.cpp

It would be nice if these issues could be resolved.

Please let me know if I can provide any additional information that would allow to fix these issues.

Activity

  1. added
    zig cc
    on Mar 28, 2020
  2. opti-mix commented on Mar 28, 2020

    @opti-mix
    Author

    Oh, one more thing I forgot to mention:

    1. If I use CXX="zig c++" cmake it runs into some problems for some targets that play with the compiler name. It seems like cmake does not correctly handle the space between zig and c++ in some cases. So, I created a small shell-wrapper called zigc++ which looks like this:
    #!/bin/bash
    zig c++ $@
    

    If I then use it like CXX="zigc++" cmake, then the issue disappears. So, maybe it is a good idea to provide such wrappers for zig cc and zig c++ as part of Zig pre-built binaries?

  3. andrewrk commented on Mar 28, 2020

    @andrewrk
    Member
    zig -c test.cpp -std=c++14 -ffast-math -fno-finite-math-only -g -I/path/to/include -emit-llvm -O0 -o test.o
    Invalid argument: -c
    

    you're missing c++ here. zig c++ -c ...

    edit: oops, I see that you noticed this. can you confirm that it's a problem on zig's end though?

  4. added this to the 0.6.0 milestone on Mar 28, 2020
  5. opti-mix commented on Mar 28, 2020

    @opti-mix
    Author

    edit: oops, I see that you noticed this. can you confirm that it's a problem on zig's end though?

    The thing is: if I use a single-threaded make, it does not happen. It seems to happen only with make -j N, where N > 1. Which indicates that command-line options are passed correctly to zig in a single-threaded make.

  6. andrewrk commented on Mar 28, 2020

    @andrewrk
    Member

    You could verify this with strace -f -e trace=execve make ... and then see what argv is being passed to zig

  7. opti-mix commented on Mar 28, 2020

    @opti-mix
    Author

    You could verify this with strace -f -e trace=execve make ... and then see what argv is being passed to zig

    Seems like it is a make issue, because make dumps a wrong command line in verbose mode. I tried to use the zigc++ wrapper instead of zig c++ and the issue disappears. Seems like the whitespace between zig and c++ is an issue for CMake or make or both of them.

  8. andrewrk commented on Apr 2, 2020

    @andrewrk
    Member

    (2) is a make issue as noted above
    (3) is solved in e4edc6d
    (4) there is no plan to do this; I consider that to be out of scope of the project

    As for (1) I am repurposing this issue to address it. If you run into any more independent problems, let's handle those in new issues.

  9. changed the title [-]Some issues with zig c++[/-] [+]zig cc: unsupported darwin linker args[/+] on Apr 2, 2020
  10. andrewrk commented on Apr 2, 2020

    @andrewrk
    Member

    Alright so the remaining issue has to do with the fact that Zig uses LLD as the linker, and until very recently the mach-o LLD code was unmaintained. It does not support these flags.

    However now I expect this new team to quickly bring LLD up to speed for mach-o, and so I expect this issue to be resolved with the release of LLVM 11 (in 6 months).

  11. modified the milestones: 0.6.0, 0.7.0 on Apr 2, 2020
  12. opti-mix commented on Apr 2, 2020

    @opti-mix
    Author

    @andrewrk BTW, I was about to download the latest builds from https://ziglang.org/download/#release-master, but it seems to be not updated since yesterday for some reason. Don't know if it is expected or not.

  13. opti-mix commented on Apr 2, 2020

    @opti-mix
    Author

    Alright so the remaining issue has to do with the fact that Zig uses LLD as the linker, and until very recently the mach-o LLD code was unmaintained. It does not support these flags.

    I don't quite understand this comment. Couldn't Zig just pass the provided -Wl,my_linker_option to a linker "as is", without doing anything about these flags?

    Or do you say that the error messages I see are from the LLD linker and not from Zig?

  14. 12 remaining items

  15. opti-mix commented on Apr 3, 2020

    @opti-mix
    Author

    Thanks for the report. Solved in f8cc6a1.

    Great! I can confirm that -MT issues are solved now. My project can be build almost correctly, except for the -Wl,-force-load issue mentioned before, which results in the wrongly linked executables missing some parts.

  16. andrewrk commented on Apr 3, 2020

    @andrewrk
    Member

    Good to know. Let's revisit the linker issues once LLVM 11 is a bit further along in the release cycle.

  17. modified the milestones: 0.7.0, 0.8.0 on Oct 4, 2020
  18. modified the milestones: 0.8.0, 0.9.0 on Nov 6, 2020
  19. Mouvedia commented on Nov 15, 2020

    @Mouvedia
  20. kubkon commented on Dec 22, 2020

    @kubkon
    Member

    I can shed some more light on (1):

    There is a number of flags passed to the linker using -Wl,my_linker_option. They are important for proper linking of the project. But some of them are not supported yet.
    warning: unsupported linker arg: -search_paths_first
    warning: unsupported linker arg: -headerpad_max_install_names
    warning: unsupported linker arg: -force_load
    warning: unsupported linker arg: -force_load

    All of the flags with the exception of force_load are not currently supported by LLD, however, there is an ongoing effort in LLVM to replace now dated Mach-O backend with a new one that supports these options. Having said that, LLVM11 still uses the old backend by default, and while it is technically possible to switch to the new one already, it currently fails to even build Zig on x86_64 (aarch64 support is not even started). FYI currently supported flags by the (old) backend are listed here.

    While not a permanent fix, it is now possible to fall back to the system linker on macOS by setting export ZIG_SYSTEM_LINKER_HACK=1 env var before linking.

  21. added
    upstream
    on Dec 22, 2020
  22. modified the milestones: 0.9.0, 0.10.0 on May 19, 2021
  23. mikdusan commented on Jan 1, 2023

    @mikdusan
    Member
    warning: unsupported linker arg: -search_paths_first
    warning: unsupported linker arg: -headerpad_max_install_names
    warning: unsupported linker arg: -force_load
    

    These linker args are now supported by zld.

    $ sw_vers
    ProductName:		macOS
    ProductVersion:		13.1
    BuildVersion:		22C65
    $ zig version
    0.11.0-dev.1023+1c711b0a6
    $ zig c++ -o main main.cpp -Wl,-search_paths_first -Wl,-headerpad_max_install_names -Wl,-force_load -Wl,/opt/local-2022.10/lib/libz.a -v
    MachO Flush... zig ld -dynamic -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk -search_paths_first -headerpad_max_install_names /opt/local-2022.10/lib/libz.a /Users/mike/project/zig/work/main/zig-cache/o/a8ee99607d7d9ab78e53561974b39bdd/main.o /Users/mike/project/zig/work/main/zig-cache/o/d56d9e695295e64a617c9618d425bcc5/libcompiler_rt.a /Users/mike/project/zig/work/main/zig-cache/o/1c116bd4f79c02580e72b1128a68a868/libc++abi.a /Users/mike/project/zig/work/main/zig-cache/o/5b02b02b8b0a4913d1f1edf075b37d0c/libc++.a -o main -lSystem -lc -L/usr/lib -F/System/Library/Frameworks -force_load /opt/local-2022.10/lib/libz.a
  24. modified the milestones: 0.12.0, 0.11.0 on Jan 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    os-macosupstreamzig cc

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions