Repository navigation
Description
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
-targetand-mcpu. - Finds an appropriate
zig-cache/directory to put some wrapper scripts. - Creates a
bindirectory in there and prepends it to thePATHenvironment variable. - Creates wrapper scripts for
zig-cc,zig-c++,zig-ar,zig-ranlib, etc.- The wrapper scripts will have the
-targetand-mcpuand other relevant args "hard coded" in there, so that users of them can treat them as drop-in replacements.
- The wrapper scripts will have the
- Sets
CC=zig-ccandCXX=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
mikdusan commentedon Jun 2, 2021
perhaps the toolchain instead creates
cc,c++,ar,ranlibwrappers and then the user is only needs to deal withPATHand 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)
andrewrk commentedon Jun 2, 2021
💪
can you elaborate?
mikdusan commentedon Jun 2, 2021
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]) ==
ccthenzig cc ...if basename(argv[1]) ==
arthenzig ar ...one source file to rule them all
andrewrk commentedon Jun 2, 2021
In this case there would need to be no exes, just soft-links or hard-links to the main
zigexecutable.mikdusan commentedon Jun 2, 2021
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.
daurnimator commentedon Jun 3, 2021
How would you do that?
Avoid hard links; symlinks are sufficient for the use case.
mikdusan commentedon Jun 3, 2021
oh this seems to preclude just linking back to the main
zigexecutableikskuh commentedon Jun 3, 2021
Important note:
Windows doesn't like both sym- and hardlinks, you need admin rights for them, which might be unwanted.
mikdusan commentedon Jun 3, 2021
shouldn't be too bad...
jayschwa commentedon Jun 4, 2021
This is a cool idea. Here are some mock commands that feel intuitive to me.
Show environment
Setup environment in current shell
Save environment to file for later
Install symlinks to a specific, more permanent location
Use as cross-compiler for autohell (after installing symlinks)
charlieman commentedon Jun 5, 2021
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.
Jan200101 commentedon Jun 5, 2021
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
create miniscripts for CC and CXX
5 remaining items
jayschwa commentedon Nov 13, 2021
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.)
daurnimator commentedon Nov 13, 2021
The
.isn't, the<()is.I guess the cross platform way to do it would be sending to a file first.
At that point yeah I guess
evalis better despite its downsidesmk12 commentedon Nov 19, 2021
There is lots of prior art on changing the shell environment (virtualenv, rvm, nvm, etc.).
But will people really use
zig toolchaininteractively the way they use those tools? I think it would be a lot simpler to implement it for one-off commands, like this:Then, for the cgo issue #7342, you'd do:
# before: CC="zig cc" go build main.go zig toolchain go build main.goAnd if you really want an interactive shell, you can launch one yourself:
I'm unfamiliar with Windows so I'm not sure how well this works there.
daurnimator commentedon Nov 19, 2021
The problem is dealing with the many tools that expect a single command (and don't pass along arguments).
Jan200101 commentedon Nov 19, 2021
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
mk12 commentedon Nov 19, 2021
@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 writezig toolchain go build .... If you're using some system where that only lets you tweak aar_pathparameter (for example), then under my suggestion you'd go a level up and runzig 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 fishif they prefer fish but haven't configured it as their login shell. The main point is thatzig toolchainwouldn't have to worry about supporting a bunch of shells or detecting the current shell.Jan200101 commentedon Nov 19, 2021
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"
iacore commentedon Jan 4, 2022
Some CMake projects completely ignore
CCandCXX, and useCMAKE_C_COMPILERinstead. I feel like this should be provided as helper scripts for each shell.Bash version 5.1.12 doesn't do it either.
Andoryuuta commentedon Jan 4, 2022
CMake's
CMAKE_C_COMPILERenvvar is generated from the value provided inCCwhen first creating the cmake cache, so I don't think that needs to be a concern here.See the
CCdoc 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."