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

std.Build.Step.ConfigHeader: improve handling of autoconf style headers #22941

Merged
merged 4 commits into from
Feb 22, 2025

Conversation

Techatrix
Copy link
Contributor

  1. allow using autoconf values multiple times

While trying to port libelf to the zig build system, I noticed that their config.h.in has #undef HAVE_MEMSET twice in the same file which makes it incompatible with Zig's std.Build.Step.ConfigHeader utility. It is valid to redefine a macro if the definitions are identical.

  1. skip trailing whitespace in autoconf header

While trying to port GNU Readline to the zig build system, I noticed that their config.h.in has trailing whitespace in their definition of HAVE_WCHAR_T (and other macros) which makes it incompatible with Zig's std.Build.Step.ConfigHeader utility.

  1. permit inserting enum values

This change not specific to autoconf style headers but applies to std.Build.Step.ConfigHeader in general. I can split this into a separate PR if needed. I kept them together to duplicate the test coverage

One use case that would benefit from this is when inserting an enum value into a config header that is determined at runtime due to relying on the build system target.

Example from libelf
pub fn build(b: *std.Build) !void {
    const config_header = b.addConfigHeader(.{ .style = .{ .autoconf = upstream.path("config.h.in") } }, .{
        /// ...
        .__libelf_i64_t = zigIntegerToC(i64, target.result),
        .__libelf_u64_t = zigIntegerToC(u64, target.result),
        .__libelf_i32_t = zigIntegerToC(i32, target.result),
        .__libelf_u32_t = zigIntegerToC(u32, target.result),
        .__libelf_i16_t = zigIntegerToC(i16, target.result),
        .__libelf_u16_t = zigIntegerToC(u16, target.result),
        /// ...
    });
}

fn zigIntegerToC(comptime Int: type, target: std.Target) enum {
    short,
    @"unsigned short",
    int,
    @"unsigned int",
    long,
    @"unsigned long",
    @"long long",
    @"unsigned long long",
} {
    // uses std.Target.cTypeBitSize and @typeInfo(Int)
}

@andrewrk andrewrk merged commit a7467b9 into ziglang:master Feb 22, 2025
9 checks passed
@andrewrk
Copy link
Member

Thanks!

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.

2 participants