-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
use case: ability to use zig as a drop-in replacement for a C compiler #3089
Copy link
Copy link
Closed
Labels
acceptedThis proposal is planned.This proposal is planned.contributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.This issue is limited in scope and/or knowledge of Zig internals.proposalThis issue suggests language modifications. If it also has the "accepted" label then it is planned.This issue suggests language modifications. If it also has the "accepted" label then it is planned.zig ccZig as a drop-in C compiler featureZig as a drop-in C compiler feature
Milestone
Metadata
Metadata
Assignees
Labels
acceptedThis proposal is planned.This proposal is planned.contributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.This issue is limited in scope and/or knowledge of Zig internals.proposalThis issue suggests language modifications. If it also has the "accepted" label then it is planned.This issue suggests language modifications. If it also has the "accepted" label then it is planned.zig ccZig as a drop-in C compiler featureZig as a drop-in C compiler feature
Zig is able to compile C code, however, its command-line interface differs from existing C compilers. This means it cannot be swapped out for other C-compilers as existing build systems wouldn't know how to compile C code with Zig's unique command-line interface.
To remedy this, it is proposed zig add support for existing c compiler command-line interfaces.
Here are some thoughts about how this could be done:
Command-line translation
A tool could be created that takes a set of command-line arguments meant for an existing C compiler like gcc/clang and translates them to the zig equivalent.
I've created a prototype for this here: https://github.com/marler8997/zigcc
The current challenge with this solution is being able to invoke zig's clang entry point with libc options. It's easy enough to invoke
zig cc, but I'm not sure how to properly glean the other clang options I need to compile C code and how to make sure libc is also built.i.e. zig cc -isystem .... If we could support this, then clang's entire command-line interface will be available to be used by this wrapper tool. This seems to scale better rather than trying to expose each clang semantic with something in the zig command-line interface.New Commands
Add new commands to zig, (i.e.
zig gccorzig clang) that emulates the respective compiler's command-line interfaceSymbolic links
Symbolic links could be used to signal zig to run in a particular mode, i.e.
These would essentially do the same thing as the "New Command" mechanism, except it wouldn't require an extra parameter. This might make zig compatible with more projects. On windows this could be done by making
zig-clang.bat/zig-gcc.batbatch scripts that just forward the call to zig.Version Message
Some build systems will run the C-compiler and check the version information to know how to use it. Printing the compiler-version from the other C compiler that zig is emulating could help compatibility in some projects. Furthermore, we may want to leave the door open to supporting multiple versions of different C compiler if the need arises.