let std.Build.add{Test,Executable}
take a root module and target/optimize options
#22284
Labels
enhancement
Solving this issue will likely involve adding new logic or components to the codebase.
proposal
This issue suggests modifications. If it also has the "accepted" label then it is planned.
zig build system
std.Build, the build runner, `zig build` subcommand, package management
Milestone
The recent std.Build API enhancement in #20388 allows (and will require once deprecated options are remove) passing an explicit root module when using
addTest
oraddExecutable
. I think there is a footgun with the new API for packages that export a module to dependents viaaddModule
and also want to use them as the root module of a test or other executable with different target and optimize options. This use-case would benefit from being able to take advantage of the new API by passing thestd.Build.Module
returned byaddModule
toaddTest
andaddExecutable
, making build scripts more straightforward and avoiding the footgun described below simultaneously.Consider this
build.zig
using the old API:This allows the package to export the
foo
module to dependents so that it will inherit thetarget
andoptimize
options from the parent module while also running tests with differenttarget
andoptimize
options.The new API uses a
root_module
option rather thanroot_source_module
inaddTest
so the proper way to upgrade thisbuild.zig
to the new API is:which means you need to duplicate any setup the
foo
module needs between the module exported to dependents and the module passed toaddTest
. The footgun mentioned above is that I think it's quite likely that people will end up doing this if they're not paying attention/aware of the difference:leading to the
foo
module being compiled with native target and Debug optimize mode unless every dependency in the chain between the end user and the foo package correctly passing-Doptimize
and-Dtarget
to their dependencies.I think an improvement to the API would allow setting the
target
andoptimize
options in test options so you instead writeThis API has the benefit that it allows sharing all the setup needed for the
foo
module, making the correct way to upgrade the originalbuild.zig
above the easiest:and making it less likely that transitive dependencies end up being erroneously compiled with native target and debug optimization mode.
This would require some changes to how these options are handled - we wouldn't want to mutate
foo
in theaddTest
call to set the target and optimization modes, and we don't want to clone the module to make a new one with different options. We may need to set these options the compile step and have the settings resolved when the build system hands things off to the compiler, either overriding the options on the root module or providing a default when the module settings are unset while erroring when set on both the root module and compile step.The text was updated successfully, but these errors were encountered: