<powerofzero>
Hi. I'm working on wrapping a c library in zig, and I would like to bundle the c library. Are there any good examples of others who have done this?
<marijnfs>
especially serialise_msg, the union case
<marijnfs>
the strangest thing is, if I uncomment line 144, i get a compile error at line 137: serialise.zig:137:17: error: control flow attempts to use compile-time variable at runtime
<ifreund>
marijnfs: that gdb output isn't terribly helpful, could you provide a bt full?
<aconbere>
Hi! I've been playing with using zig as a "pragmatic c" and running into some bumps along the way. From ziglearn it mentions `zig translate-c` as a tool to quickly lift c into zig.
<aconbere>
I took a pretty basic c program and ran it, and translate-c complains about not finding `stdio.h`
<aconbere>
gcc happily finds and builds the program, is there something I have to hint translate-c to about the location of headers?
<ifreund>
aconbere: you need to tell zig to link libc
<ifreund>
getaddrinfo isn't really a syscall, just a libc thing that does some logic and syscalls for you
<ifreund>
ideally std.net would be able to do everything it does
<aconbere>
For what it's worth I don't think that's horrible I just wanted to play with the guts of things
<aconbere>
and found it hard haha
<aconbere>
Understood!
<aconbere>
The pathway here is really something like "oh I heard that zig was a interesting and upcoming c replacement, let's take something I can do in c but isn't much fun, and try it in zig"
<ifreund>
anyhow, you're right that the definition of getaddrinfo in std.c should have the first parameter made optional (PR welcome)
<aconbere>
ifreund: another question while I'm here :-D
<aconbere>
in my journey here I played around in std.net, I've found that I can make obviously breaking changes there, but when I re-compile zig the compiler doesn't error out
<aconbere>
is there a flag to the zig build that's required to compile the std library?
<ifreund>
marijnfs: I haven't been able to glean anything from the stack trace, it looks like you're deadlocking on the stderr mutex which should only happen if something tries to acquire it while it is already held by the same thread
<ifreund>
aconbere: the std (like all zig code) is lazily compiled when it is required by whatever code you have written
* aconbere
nods
<ifreund>
if you were to change something required by the compiler the build would fail
<ifreund>
but if you change stuff the compiler doesn't require and the code still parses fine, you won't get a compile error until trying to use it in a project
<ifreund>
marijnfs: are you doing anything weird with threads?
<ifreund>
putting a breakpoint in the acquire function might help
<aconbere>
ifreund: is there a document that outlines the standard development process for contributors?
<aconbere>
I guess what I'm wondering is, how did the std.net package get tested in the first place :-D
<ifreund>
oh running the standard library tests should build and test it
<aconbere>
my intuition would there would be a flag to either force compilation, or to trigger testing in a way that those units are "used"
<aconbere>
ahh
<ifreund>
if you run zig build --help in the root of the repo you'll see a lot of opitons
<ifreund>
usually I just run zig test /file/im/touching if the changes are limited in scope
<aconbere>
interesting, I've been following the instructions that go through cmake etc.
<ifreund>
oh you need to go through cmake to build the stage1 compiler first
<ifreund>
but once you have that you can use the build.zig to test the std and build the WIP stage2 compiler
<aconbere>
goooooot it
via has quit [Ping timeout: 256 seconds]
<aconbere>
so i'm bootstrapping the compiler
<aconbere>
but typically once bootstrapped folks would use zig to compile zig
<ifreund>
yeah, and the stage1 compiler is actually written partially in zig as well
via has joined #zig
<ifreund>
so it's building a very limited zig0 compiler which is 100% C++ and then using that to build the zig parts of stage1
<aconbere>
cool
<aconbere>
pretty impressive how fast it is
<aconbere>
so used to something like GCC or Rust or LLVM where I expect compilation to take 3 hours
<ifreund>
stage1 is really really slow
<ifreund>
just wait till stage2 is done :P
<aconbere>
haha
notzmv has quit [Ping timeout: 245 seconds]
<aconbere>
oh man
<aconbere>
the CONTRIBUTING section of the readme covers all of this
<aconbere>
sorry for bother you ha
<aconbere>
seems like I'm on a better track now
<ifreund>
heh, no worries :)
<aconbere>
Yay! And the std.net test appropriately fails
<marijnfs>
ifreund: threads are involved. But I manage to get this error also in a simple 'zig build test' that only calls this serialise
<marijnfs>
i don't believe threads are involved there
<ifreund>
marijnfs: are you using zig master?
<ifreund>
more specifically, do you have 89ee4b86?
powerofzero has joined #zig
<marijnfs>
yes, have master from today
<marijnfs>
ifreund: it also doesn't look like a deadlock, because it goes in a crazy printing loop
<ifreund>
oh? what is it printing?
notzmv has joined #zig
<marijnfs>
ifreund: basically, I add a print statement in the beginning of 'serialise_msg', and that keeps printing like crazy
<marijnfs>
but there is no deep callstack or something, which is very confusing
<marijnfs>
maybe it's a recursive callstack, but llvm is smart to optimise it?
<marijnfs>
if I stop the program in gdb, I get this small stacktrace
yyp has quit [Quit: disconnected]
eax has joined #zig
<ifreund>
why is it getting called with void?
dok has joined #zig
forgot-password has quit [Ping timeout: 256 seconds]
ur5us has joined #zig
<marijnfs>
ifreund: not sure, one of the Response enums is void, but not the one I'm testing
forgot-password has joined #zig
<marijnfs>
when i'm trying to print the typeInfo of what i'm serialising, I get a compiler bug:( https://0bin.net/paste/wWd3v1Xi#-2YAdn+s4gHWZj1ekFlU4PeuWxAP/p/VDZ+ZbYhwifd
<marijnfs>
but i guess that's unrelated
forgot-password has quit [Ping timeout: 260 seconds]
supercoven has quit [Ping timeout: 246 seconds]
aconbere has quit [Ping timeout: 265 seconds]
texno has quit [Ping timeout: 264 seconds]
texno has joined #zig
aconbere has joined #zig
aconbere has quit [Ping timeout: 245 seconds]
notzmv has quit [Ping timeout: 260 seconds]
aconbere has joined #zig
<aconbere>
ifreund_: follow up question from earlier (sorry if you're away), curious about the philosophical stance zig takes on c interopt. For instance it's valid to call getaddrinfo with a NULL hint pointer. But the result is the same as if you had specified a handful of hint values (it's basically shorthand for "default")
<aconbere>
Curious if zig would take the stance of being faithful to the original c interface (null is valid, go for it)
<aconbere>
or would take a stronger position on safety and say "look just pass in the hint, we can do things to help with defaults"
<ifreund>
aconbere: passing in a null pointer there isn't unsafe, zig should allow it
<ifreund>
if you want to open a PR making the first argument of std.c.getaddrinfo optional I'll merge it :P
<aconbere>
haha
<aconbere>
well I'm actually from reading the man page
<aconbere>
suggesting a little bit more
<aconbere>
node, service, and hints are all valid to be null
<aconbere>
(although there is a constraint that both node and service cannot be null at the same time)
<ifreund>
marijnfs: that looks like the exact bug you're hitting ^
<ugla>
Haven't looked into it myself but thought it might be related
decentpenguin has quit [Read error: Connection reset by peer]
fputs has quit [Quit: WeeChat 3.1]
decentpenguin has joined #zig
notzmv has joined #zig
riba has quit [Ping timeout: 245 seconds]
drakonis has joined #zig
decentpenguin has quit [Read error: Connection reset by peer]
decentpenguin has joined #zig
<aconbere>
hmmmm
<aconbere>
what am I doing wrong now :P
<aconbere>
`./bin/zig build test-std -Dskip-release -Dforce-link-libc=true` results in an error `error: dependency on libc must be explicitly specified in the build command`
<aconbere>
I tried `./bin/zig build -lc test-std -Dskip-release -Dforce-link-libc=true`
<aconbere>
but that complains `Unrecognized argument: -lc`
<waleee-cl>
aconbere: I think you can only pass -lc with zig build-exe/build-lib
wilsonk_ has quit [Quit: Leaving]
<aconbere>
oh no! haha
<g-w1>
force-link-libc is not what you want
<aconbere>
I'll try anything ;-)
<g-w1>
it tries to use the self-hosted linker to link stuff which is not complete yet
<g-w1>
why do you wanna link libc?
<aconbere>
I'm working on a PR to update getaddrinfo's call signature to match libc's (which allows for passing null points for nodename, service, and hint)
<aconbere>
I figured, I'll write a test for it!
<g-w1>
oh wait nvm thats not what it does, but still not what you want, it forces stage2 to link libc
<aconbere>
but the test depends on libc since that's where getaddrinfo comes from
<g-w1>
hmm, there must be other tests that depend on libc
<aconbere>
anyway so I'm trying to run a test in the stdlib that depends on libc
<aconbere>
yeah! std.net for sure
<aconbere>
:)
<aconbere>
(lib/std/net/test.zig)
<aconbere>
since it in turn depends on std.c
<g-w1>
this means that the test-harness needs to link libc, so you have to update build.zig (I guess no tests have linked libc yet. maybe this is on purpose. you will get feedback on your pr if this is wrong)
<aconbere>
haha
ur5us has quit [Ping timeout: 264 seconds]
<aconbere>
okay well, maybe I can ask my other question then and push and see what happens
<aconbere>
The reason I decided to make a test, beyond it being a good practice
<g-w1>
i would ask in the pr is there any reason why tests don't link libc
<aconbere>
well this other question is a baby zig question (because that's what I am)
<aconbere>
I'm just not confident I'm constructing a null c pointer correctly
<aconbere>
I was hoping I could bang out a quick test to answer that question
<g-w1>
ok, yeah try it
<aconbere>
I'll push the damn PR and learn a bunch haha
<g-w1>
just do test_step.linkLibC() in build.zig
<g-w1>
i would add a -Dno-link-libc option in build.zig tho where it does if !(no-link-libc) test_step.linkLibC() then in the test that needs libc `if (!std.builtin.link_libc) return error.SkipZigTest;`
texno has quit [Ping timeout: 260 seconds]
texno has joined #zig
<aconbere>
hmmmmm
<aconbere>
g-w1: `error: no member named 'linkLibC' in struct 'std.build.Step'`
<aconbere>
looks like test_stage2's type has that function
<aconbere>
I'm so new to this I'm just kinda... trying stuff though hah
decentpenguin has quit [Read error: Connection reset by peer]
<g-w1>
yeah, ill try it too
<aconbere>
maybe it matters that I do both exe and test
powerofzero has quit [Ping timeout: 256 seconds]
decentpenguin has joined #zig
<g-w1>
nah
<g-w1>
its the wrong type, ill find the right one
decentpenguin has quit [Read error: Connection reset by peer]
decentpenguin has joined #zig
<aconbere>
in the meantime I'll put up my rough draft :)
<g-w1>
aconbere: see lib/std/io/c_writer.zig:37-end. the test harness is already smart enough to link libc
forgot-password has quit [Ping timeout: 264 seconds]