zig build: convenience API for setting config variables #226

Open
andrewrk opened this Issue Jan 31, 2017 · 1 comment

Comments

Projects
None yet
1 participant
@andrewrk
Member

andrewrk commented Jan 31, 2017

@compileVar currently only gives access to the builtin compile variables - is_release, os, arch, and a couple others.

It should be possible to specify compile variables via the command line, and it should allow more than type bool.

Once we have the zig build system then it should be possible to programmatically add compile variables in the build script.

@andrewrk andrewrk added this to the 0.1.0 milestone Jan 31, 2017

@andrewrk

This comment has been minimized.

Show comment
Hide comment
@andrewrk

andrewrk May 1, 2017

Member

Compile variables are the same thing as importing a generated .zig file, which inspires this idea:

Instead of @compileVar("name"), have @import("compile_vars").name. Maybe we make a special name for zig compile vars such as @import("@config").

This solves #220 as well. Instead of @Os it would be @import("@config").Os.

Then for user compile variables we need a way to specify packages on the command line, which we already want, something like -Ppkg_name=path/to/index.zig. So then if you wanted to pass compile variables to zig code, you put them in e.g. config.zig and then do zig build_exe main.zig -Pconfig=config.zig, and then import them with @import("config"). Example config.zig:

pub const include_horse_galloping_feature = false;
pub const default_allocator = @import("std").mem.IncrementingAllocator.init(10 * 1024 * 1024);

Now the only thing left is to add compile variables in the build script. For this we can provide an API that has an interface roughly like this:

const exe = b.addExe("app", "app.zig");
const config = b.addConfig();
config.add("include_horse_galloping_feature", false);
config.add("number_of_things", b.option("number-of-things", u32, "Set the number of things"));
config.add("name", "blah blah");
exe.addConfig("config", config);

This would generate config.zig and then pass the -Pconfig=config.zig parameter when building. Of course it's not necessary to use this API; you could alternately provide or write your own .zig file and then use the API to add a package directly.

Member

andrewrk commented May 1, 2017

Compile variables are the same thing as importing a generated .zig file, which inspires this idea:

Instead of @compileVar("name"), have @import("compile_vars").name. Maybe we make a special name for zig compile vars such as @import("@config").

This solves #220 as well. Instead of @Os it would be @import("@config").Os.

Then for user compile variables we need a way to specify packages on the command line, which we already want, something like -Ppkg_name=path/to/index.zig. So then if you wanted to pass compile variables to zig code, you put them in e.g. config.zig and then do zig build_exe main.zig -Pconfig=config.zig, and then import them with @import("config"). Example config.zig:

pub const include_horse_galloping_feature = false;
pub const default_allocator = @import("std").mem.IncrementingAllocator.init(10 * 1024 * 1024);

Now the only thing left is to add compile variables in the build script. For this we can provide an API that has an interface roughly like this:

const exe = b.addExe("app", "app.zig");
const config = b.addConfig();
config.add("include_horse_galloping_feature", false);
config.add("number_of_things", b.option("number-of-things", u32, "Set the number of things"));
config.add("name", "blah blah");
exe.addConfig("config", config);

This would generate config.zig and then pass the -Pconfig=config.zig parameter when building. Of course it's not necessary to use this API; you could alternately provide or write your own .zig file and then use the API to add a package directly.

andrewrk added a commit that referenced this issue May 1, 2017

@andrewrk andrewrk changed the title from ability to set compile variables at the command line and via build script to zig build: convenience API for setting config variables May 1, 2017

@andrewrk andrewrk modified the milestones: 0.2.0, 0.1.0 May 1, 2017

@andrewrk andrewrk added the userland label Aug 27, 2017

andrewrk referenced this issue in tiehuis/zig-raytrace Oct 12, 2017

@andrewrk andrewrk modified the milestones: 0.2.0, 0.3.0 Jan 18, 2018

@andrewrk andrewrk modified the milestones: 0.3.0, 0.4.0 Feb 28, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment