Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make dealing with zig.h less problematic #13528

Open
andrewrk opened this issue Nov 12, 2022 · 3 comments
Open

make dealing with zig.h less problematic #13528

andrewrk opened this issue Nov 12, 2022 · 3 comments
Labels
backend-c The C backend (CBE) outputs C source code. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@andrewrk
Copy link
Member

andrewrk commented Nov 12, 2022

Previously, the contents of zig.h were directly prepended to output .c files when using the C backend. It was extracted into an installation file in order to support the use case of editing zig.h without recompilation.

Currently, when using -ofmt=c, the output .c file has the first line of #include <zig.h>.

Within a Zig installation, zig.h can be found in lib/include/zig.h - next to the other C language headers (not to be confused with libc headers).

Problems with this setup:

  • Having it sit next to other C language includes means that using zig cc supports #include <zig.h> without any -I arguments. However, when using any other compiler, such as gcc, clang, or msvc, a -isystem argument would be needed to make the include work. Passing the directory of the zig installation that contains zig.h causes conflicting C language headers to be in the system include path.
  • Versions can become mismatched.

This proposal:

  • Move zig.h from lib/include/zig.h to lib/zig.h.
  • When performing a C backend build, in addition to outputting the .c file, copy zig.h to the same directory, with a suffix of zig version. For example, zig build-exe foo.zig -ofmt=c would produce foo.c and zig-0.10.0.h in the current working directory.
  • Change the include line from #include <zig.h> to #include "zig-$VERSION.h". This both adds a version suffix and makes it look in the same directory as itself.
@andrewrk andrewrk added proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. backend-c The C backend (CBE) outputs C source code. labels Nov 12, 2022
@andrewrk andrewrk added this to the 0.11.0 milestone Nov 12, 2022
@jacobly0
Copy link
Member

I've been considering almost this exact proposal, and seeing it written down I'm not seeing any issues with it. The version number is certainly a nice addition for releases, although I wonder how much it might spam a bunch of unused dev versions of the file during development. This is of course mitigated by the version not changing until the commit changes, so it might be fine. I run git clean frequently enough that I wouldn't expect to be affected by it myself.

@andrewrk
Copy link
Member Author

I do think it's worth considering the alternative of going back to embedding zig.h - it does make some problems go away although it's not as nice for C backend development itself. However, a relevant consideration would be the use case where something compiled with the C backend wants to expose its API to other C code. Does its public API depend on zig.h or not? That seems like a whole other can of worms for which a decision can perhaps be delayed while we focus on other pressing matters.

andrewrk added a commit that referenced this issue Nov 13, 2022
Partially implements #13528. Enough to unblock the wasi-bootstrap
branch.
andrewrk added a commit that referenced this issue Nov 14, 2022
Partially implements #13528. Enough to unblock the wasi-bootstrap
branch.
@andrewrk andrewrk modified the milestones: 0.11.0, 0.12.0 Jun 19, 2023
@Radvendii
Copy link
Sponsor

Is there a workaround for this, or is it not possible to generate a header file and then include it in the compilation of some C files all from within a build.zig?

I tried using std.fs.selfExeDirPath() and appending "/lib" but that doesn't work because at that point in the code zig is not the executable (it's some generated build executable). I also tried b.zig_lib_dir but that's not set unless you pass --zig-lib-dir.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend-c The C backend (CBE) outputs C source code. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

3 participants