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

simpler pch support for zig build #20879

Closed
wants to merge 6 commits into from
Closed

Conversation

xxxbxxx
Copy link
Contributor

@xxxbxxx xxxbxxx commented Jul 31, 2024

This is a follow-up to #17956 (included here as well), that exposes a simpler interface to use precompiled headers.

It is now possible to simply add the name of the header file to precompile:

exe.addCSourceFiles(.{
            .files = &.{"file.c", ...},
            .flags = ...,
            .precompiled_header = .{ .source_header = .{.path = b.path("all.h")} },
        });

In order to do this,

  • I added an extra finalize() function to the build steps,
  • created new build steps not explicitly requested by the user,
  • as well as altered the compile steps after the user as initialised them.
    I don't know if that fits the general design of the build system or if there's an other way to achieve this.

updated example patch for https://github.com/allyourcodebase/cpython:

--- a/build.zig
+++ b/build.zig
@@ -664,7 +664,6 @@ pub fn build(b: *std.Build) void {
         "Modules/getbuildinfo.c",
         "Modules/itertoolsmodule.c",
         "Modules/main.c",
-        "Modules/mathmodule.c",
         "Modules/md5module.c",
         "Modules/sha1module.c",
         "Modules/sha256module.c",
@@ -805,7 +804,19 @@ pub fn build(b: *std.Build) void {
         "-std=c11",
         "-fvisibility=hidden",
         "-DPy_BUILD_CORE",
-    } });
+    }, .precompiled_header = .{ .source_header = .{ .path = b.path("Include/Python.h") } } });
+
+    // files that includes Python.h with NEEDS_PY_IDENTIFIER defined
+    exe.addCSourceFiles(.{ .files = &.{
+        "Modules/mathmodule.c",
+    }, .flags = &.{
+        "-fwrapv",
+        "-std=c11",
+        "-fvisibility=hidden",
+        "-DPy_BUILD_CORE",
+        "-DNEEDS_PY_IDENTIFIER",
+    }, .precompiled_header = .{ .source_header = .{ .path = b.path("Include/Python.h") } } });
+
     exe.addCSourceFiles(.{ .files = &.{
         "Modules/getpath.c",
     }, .flags = &.{

…ction.

It is normally based on the file extension, however:
- it can be ambiguous. for instance,
    ".h" is often used for c headers or c++ headers.
    ".s" (instead of ".S") assembly files may still need the c preprocessor. (ziglang#20655)

- a singular file may be interpreted with different languages depending on the context.
    in "single-file libraries", the source.h file can be both a c-header to include, or compiled as a C file (with a #define as toggle)  (ziglang#19423)
usage example:
        `zig build-pch -lc++ -x c++-header test.h`
        `zig run -lc++ -cflags -include-pch test.pch -- main.cpp`

It builds the file.pch with llvm "-fpch-validate-input-files-content",
so it includes data for better integration with zig caching system.
adds a new mode to the Compile step.
It shares most of the code with previous compile steps, but with extra constraints expressed by assert checks.
It is called after the user has fully configured the steps in the build() function.
a compile step to build the pch file will be automatically created.

To benefit from precompiled headers, it is now possible to simply change
exe.addCSourceFiles(.{
            .files = &.{"file.c"},
            .flags = ...,
        });

into

exe.addCSourceFiles(.{
            .files = &.{"file.c"},
            .flags = ...,
            .precompiled_header = .{ .source_header = .{ path = b.path("rootincludes.h") } },
        });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant