Zig: use case: ability to use zig as a drop-in replacement for a C compiler

Created on 18 Aug 2019  路  5Comments  路  Source: ziglang/zig

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 gcc or zig clang) that emulates the respective compiler's command-line interface

Symbolic links

Symbolic links could be used to signal zig to run in a particular mode, i.e.

zig-clang > zig
zig-gcc > zig

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.bat batch 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.

accepted contributor friendly proposal zig cc

Most helpful comment

This is now available as of 7ffdf59c441380efd9bbb837de7ad5f2df747a6e. However there are quite a few follow-up tasks to do before this feature is really complete. I'll be opening new issues to track those tasks.

All 5 comments

Transferring discussion to this thread (https://github.com/ziglang/zig/issues/3102#issuecomment-523522664)

The current question is, what is the equivalent of -o on the zig comand-line?

The current question is, what is the equivalent of -o on the zig comand-line?

There isn't one. That would be one of the problems to solve for this issue.

Andrew just did a stream on this issue: https://www.twitch.tv/videos/571523138

note: unsure how long that link will remain valid

Andrew just did a stream on this issue: https://www.twitch.tv/videos/571523138

note: unsure how long that link will remain valid

Now available on youtube: https://www.youtube.com/watch?v=2u2lEJv7Ukw

This is now available as of 7ffdf59c441380efd9bbb837de7ad5f2df747a6e. However there are quite a few follow-up tasks to do before this feature is really complete. I'll be opening new issues to track those tasks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fengb picture fengb  路  3Comments

S0urc3C0de picture S0urc3C0de  路  3Comments

dobkeratops picture dobkeratops  路  3Comments

andrewrk picture andrewrk  路  3Comments

andrewrk picture andrewrk  路  3Comments