Skip to content

new subcommand: zig toolchain #8973

Open

Description

@andrewrk

Occasionally there is a use case for something like zig cc, zig ar, other toolchain commands, but even though the Zig CLI has drop-in compatibility, it still cannot be used, because of the space between zig and the subcommand. What a shame!

Here's an example: "zig cc" doesn't work with cgo (golang)

This is solved in Go 1.17 (looks like they didn't actually merge the PR that fixed it), but until then, the workaround was to manually create a wrapper script, forwarding all the args. This wrapper script could then be invoked without the space problem.

Another example I just ran into while working on the Drone CI script:

samu: job failed: : && /usr/bin/cmake -E rm -f zigcpp/libopt_c_util.a && "/deps/local/bin/zig ar" qc zigcpp/libopt_c_util.a  CMakeFiles/opt_c_util.dir/src/stage1/parse_f128.c.o && "/deps/local/bin/zig ranlib" zigcpp/libopt_c_util.a && :
61s
240/bin/sh: /deps/local/bin/zig ar: not found

#8716 is another proposal which asked to ship standard wrapper scripts with the zig installation. The problem statement in that issue applies here as well. Ultimately, I closed the proposal because there just isn't a way to cleanly provide an installation that supports all the hosts, since there is system-specific logic to deal with.

However, this problem can be overcome, by solving it at runtime!

This proposal is to add a new sub-command, zig toolchain which does the following things:

  • Accepts flags such as -target and -mcpu.
  • Finds an appropriate zig-cache/ directory to put some wrapper scripts.
  • Creates a bin directory in there and prepends it to the PATH environment variable.
  • Creates wrapper scripts for zig-cc, zig-c++, zig-ar, zig-ranlib, etc.
    • The wrapper scripts will have the -target and -mcpu and other relevant args "hard coded" in there, so that users of them can treat them as drop-in replacements.
  • Sets CC=zig-cc and CXX=zig-cxx

So the idea here is that you could, for example, do zig toolchain -target aarch64-linux-musl and then you'd be all set for running configure, make, cmake, whatever, with Zig providing the toolchain.

Unsolved problem: I don't think child processes can set environment variables on posix systems. So we would need some convenient (for the user) way to get those env vars set. One idea would be that this sub-command would execute the command in SHELL with the env vars overridden. But that would make the command less useful in a script.

Activity

  1. added
    proposal
    on Jun 2, 2021
  2. added this to the 0.9.0 milestone on Jun 2, 2021
  3. mikdusan commented on Jun 2, 2021

    @mikdusan
    Member

    perhaps the toolchain instead creates cc, c++, ar, ranlib wrappers and then the user is only needs to deal with PATH and prefix it with the new toolchain dir.

    and while we're at it, scripts? how about exe's? and possibly only 1 exe and hard-linked on posix)

  4. andrewrk commented on Jun 2, 2021

    @andrewrk
    MemberAuthor

    and while we're at it, scripts? how about exe's?

    💪

    and possibly only 1 exe and hard-linked on posix)

    can you elaborate?

  5. mikdusan commented on Jun 2, 2021

    @mikdusan
    Member

    can you elaborate?

    there is a pattern where one exe is deployed, and hard linked to all the "sub command" names.
    the exe looks at argv[0] and then knows what it is supposed to be.

    if basename(argv[0]) == cc then zig cc ...
    if basename(argv[1]) == ar then zig ar ...

    one source file to rule them all

  6. andrewrk commented on Jun 2, 2021

    @andrewrk
    MemberAuthor

    In this case there would need to be no exes, just soft-links or hard-links to the main zig executable.

  7. mikdusan commented on Jun 2, 2021

    @mikdusan
    Member

    I agree. For a moment I was thinking the wrapper would pickup zig from path, but that might be a bad idea. Knowing zig-cache's toolchain is hardwired to a specific zig path is the robust thing to do.

  8. daurnimator commented on Jun 3, 2021

    @daurnimator
    Contributor

    Sets CC=zig-cc and CXX=zig-cxx

    How would you do that?

    hard-linked on posix

    Avoid hard links; symlinks are sufficient for the use case.

  9. mikdusan commented on Jun 3, 2021

    @mikdusan
    Member

    The wrapper scripts will have the -target and -mcpu and other relevant args "hard coded" in there, so that users of them can treat them as drop-in replacements.

    oh this seems to preclude just linking back to the main zig executable

  10. ikskuh commented on Jun 3, 2021

    @ikskuh
    Contributor

    Important note:
    Windows doesn't like both sym- and hardlinks, you need admin rights for them, which might be unwanted.

  11. mikdusan commented on Jun 3, 2021

    @mikdusan
    Member

    Windows doesn't like both sym- and hardlinks

    shouldn't be too bad...

    • base impl: spin an exe for each subcommand - not that expensive, the exe is not expected to be large
    • specialized for posix: use links
  12. jayschwa commented on Jun 4, 2021

    @jayschwa
    Contributor

    This is a cool idea. Here are some mock commands that feel intuitive to me.

    Show environment

    $ zig toolchain env -target i386-clownos
    export CC=i386-clownos-cc
    export LD=i386-clownos-cc
    export PATH=path/to/clownos/symlinks:$PATH
    

    Setup environment in current shell

    $ eval $(zig toolchain env -target i386-clownos)
    

    Save environment to file for later

    $ zig toolchain env -target i386-clownos > ~/send-in-the-clowns
    . ~/send-in-the-clowns
    

    Install symlinks to a specific, more permanent location

    $ zig toolchain install -target i386-clownos ~/bin
    $ ls ~/bin
    i386-clownos-cc
    i386-clownos-ld
    

    Use as cross-compiler for autohell (after installing symlinks)

    ./configure --host=i386-clownos
    checking for i386-clownos-cc... i386-clownos-cc
    checking whether the C compiler works... yes
    
  13. charlieman commented on Jun 5, 2021

    @charlieman
    Contributor

    What tools like python venv do is generate a script with the variables and tell you to source it. They generate scripts for bash, fish, csh/tcsh and powershell for posix; and bat, powershell for Windows (https://docs.python.org/3/library/venv.html).

    For another example, the Chocolatey package manager generates exes to avoid sym/hard-links in Windows. They use a shim generator (https://github.com/chocolatey/shimgen) to put exes in its bin directory that call the actual program.

  14. Jan200101 commented on Jun 5, 2021

    @Jan200101
    Contributor

    Sets CC=zig-cc and CXX=zig-cxx

    How would you do that?

    Same question here but I would suggest the same approach as pythons virtualenv where it puts the files in the right place and creates a profile you can load into your shell setup with everything you need

  15. added a commit that references this issue on Jun 7, 2021
  16. 5 remaining items

  17. jayschwa commented on Nov 13, 2021

    @jayschwa
    Contributor

    Rather than eval, use .:

    . <(zig toolchain env -target i386-clownos)
    

    Is this a bashism? It doesn't work for me in dash. (Not that I use dash under normal circumstances, I was just trying to illustrate using something that should work in any POSIX-compatible shell.)

  18. daurnimator commented on Nov 13, 2021

    @daurnimator
    Contributor

    Is this a bashism?

    The . isn't, the <() is.

    I guess the cross platform way to do it would be sending to a file first.

    zig toolchain env -target i386-clownos > source_me.sh
    . source_me.sh
    rm source_me.sh

    At that point yeah I guess eval is better despite its downsides

  19. mk12 commented on Nov 19, 2021

    @mk12
    Contributor

    There is lots of prior art on changing the shell environment (virtualenv, rvm, nvm, etc.).

    But will people really use zig toolchain interactively the way they use those tools? I think it would be a lot simpler to implement it for one-off commands, like this:

    zig toolchain -target aarch64-linux-musl -- cc hello.c

    Then, for the cgo issue #7342, you'd do:

    # before: CC="zig cc" go build main.go
    zig toolchain go build main.go

    And if you really want an interactive shell, you can launch one yourself:

    zig toolchain $SHELL
    

    I'm unfamiliar with Windows so I'm not sure how well this works there.

  20. daurnimator commented on Nov 19, 2021

    @daurnimator
    Contributor

    But will people really use zig toolchain interactively the way they use those tools?

    The problem is dealing with the many tools that expect a single command (and don't pass along arguments).

  21. Jan200101 commented on Nov 19, 2021

    @Jan200101
    Contributor
    zig toolchain $SHELL
    

    Note on the topic of $SHELL

    $SHELL is not your current shell but the shell assigned to your user set by your login manager.
    The only mainstream shell that I am aware of that sets that variable by default is Bash

  22. mk12 commented on Nov 19, 2021

    @mk12
    Contributor

    @daurnimator Could you give an example? If you're in a context where you're able to write CC="zig cc" go build ..., then you should be able to write zig toolchain go build .... If you're using some system where that only lets you tweak a ar_path parameter (for example), then under my suggestion you'd go a level up and run zig toolchain ./do_stuff.sh. Maybe that's not always possible, but I'm not sure how a virtualenv-like system helps in this case.

    @Jan200101 Ah, thanks for pointing that out, I didn't realize. So users can do zig toolchain fish if they prefer fish but haven't configured it as their login shell. The main point is that zig toolchain wouldn't have to worry about supporting a bunch of shells or detecting the current shell.

  23. Jan200101 commented on Nov 19, 2021

    @Jan200101
    Contributor

    So users can do zig toolchain fish if they prefer fish but haven't configured it as their login shell. The main point is that zig toolchain wouldn't have to worry about supporting a bunch of shells or detecting the current shell.

    Yes but there is a problem with that approach
    its not very good UX and might be problematic on Windows where you cannot simply spawn a cmd "shell"

  24. modified the milestones: 0.9.0, 0.11.0 on Nov 24, 2021
  25. iacore commented on Jan 4, 2022

    @iacore
    Contributor

    Some CMake projects completely ignore CC and CXX, and use CMAKE_C_COMPILER instead. I feel like this should be provided as helper scripts for each shell.

    $SHELL is not your current shell but the shell assigned to your user set by your login manager. The only mainstream shell that I am aware of that sets that variable by default is Bash

    Bash version 5.1.12 doesn't do it either.

  26. Andoryuuta commented on Jan 4, 2022

    @Andoryuuta
    Contributor

    Some CMake projects completely ignore CC and CXX, and use CMAKE_C_COMPILER instead.

    CMake's CMAKE_C_COMPILER envvar is generated from the value provided in CC when first creating the cmake cache, so I don't think that needs to be a concern here.

    See the CC doc page:
    "Will only be used by CMake on the first configuration to determine C compiler, after which the value for CC is stored in the cache as CMAKE_C_COMPILER."

  27. modified the milestones: upcoming, urgent on Jun 26, 2025
  28. added
    enhancement
    and removed
    proposal
    on Apr 22, 2026
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

    enhancement

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions