Open ikskuh opened 1 month ago
This would be nice for linking newlib, currently I link it directly with linkLibrary, but then zig doesn't know that libc is linked and stuff like std.heap.c_allocator
doesn't work.
The way you described it with parseLibCFile
wouldn't quite work because contents of the file can't be known LazyPath dependencies are resolved. It's fine with b.path()
, but for a generated file it wouldn't work. It could work to make a step that generates libc.txt or to use pointers to std.Build.LibC
and add dependencies into the struct that get resolved before data in the struct is used, like LazyPath.
Seems related and along the same lines as: https://github.com/ziglang/zig/issues/19340
I currently manually link in the pre-compiled newlib-nano
provided by the arm-none-eabi-gcc
compiler as I'm targeting embedded targets. Being able to generate a custom libC struct would be nice and a lot more clear than what I'm currently doing, which is using arm-none-eabi-gcc
itself to tell me where system paths are:
https://github.com/haydenridd/stm32-zig-porting-guide/blob/main/build.zig
Something else mentioned in the other issue I ran into is even when I do setup my libc
file correctly targeting the bundled newlib-nano
I get the following:
error: ld.lld: unable to find library -lm
error: ld.lld: unable to find library -lpthread
error: ld.lld: unable to find library -lc
error: ld.lld: unable to find library -ldl
error: ld.lld: unable to find library -lrt
error: ld.lld: unable to find library -lutil
Being able to control which libraries are linked in from libc would be nice, as in my case I'm targeting freestanding and don't have pthread
(among others)
which could be used like this then:
I think you have a typo there?
- libc_file: ?LazyPath = null,
+ libc: ?std.Build.LibC = null,
@pfgithub:
The way you described it with
parseLibCFile
wouldn't quite work because contents of the file can't be known LazyPath dependencies are resolved. It's fine withb.path()
, but for a generated file it wouldn't work. It could work to make a step that generates libc.txt or to use pointers tostd.Build.LibC
and add dependencies into the struct that get resolved before data in the struct is used, like LazyPath.
parseLibCFile
is only for statically known libc files, otherwise you can conveniently construct the std.Build.LibC
object as any other Zig structure :)
Right now, the
--libc [file]
command line argument isn't really well exposed instd.Build.Step.Compile
:https://github.com/ziglang/zig/blob/455899668b620dfda40252501c748c0a983555bd/lib/std/Build/Step/Compile.zig#L87
This implementation doesn't really give us the options to pass down ad-hoc compiled libcs like Foundation libc or newlib inside build.zig.
The fields available in a
libc.txt
file are these:These options could be exposed inside a struct
std.Build.LibC
:which could be used like this then:
Pre-defined libc:
Custom libc: