Skip to content

Conversation

playday3008
Copy link
Contributor

@playday3008 playday3008 commented Sep 25, 2025

TLDR

Simply replaces names with values.

Example

  • Somewhere in target C Header:

    /// @file: magic.h.in
    #define MAGIC_VERSION    X.YY
    
    int magic_version() {
        return MAGIC_VERSION;
    }
  • Somewhere in build.zig

    const magic = b.addConfigHeader(
        .{
            .style = .{
                .custom = upstream.path("src/magic.h.in"),
            },
        },
        .{
            .@"X.YY" = 123456,
        },
    );
  • Resulting C Header:

    /// @file: magic.h
    #define MAGIC_VERSION    123456
    
    int magic_version() {
        return MAGIC_VERSION;
    }

Real world use case

https://github.com/file/file/blob/7ed3febfcd616804a2ec6495b3e5f9ccb6fc5f8f/src/magic.h.in#L120
https://github.com/file/file/blob/7ed3febfcd616804a2ec6495b3e5f9ccb6fc5f8f/src/Makefile.am#L30

@playday3008
Copy link
Contributor Author

The only concern I have is when someone would do something like this:

.{
    .ABCD = 1234, 
    .@"1234" = 5678,
}

This will basically replace ABCD with 5678, but if order is changed this will not occur, maybe it's a bug, but I'd say it's a feature.

@playday3008
Copy link
Contributor Author

Also I'm not sure if my implementation is optimal enough, feel free to correct me or review the code

@playday3008 playday3008 force-pushed the feature/config-header-custom branch 2 times, most recently from 7b07035 to 61fc154 Compare October 7, 2025 14:48
@playday3008 playday3008 force-pushed the feature/config-header-custom branch from 61fc154 to afbfa67 Compare October 8, 2025 08:29
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