#Get the result of a command at comptime ?

1 messages · Page 1 of 1 (latest)

tropic ether
#

Hi,

I'm making a library tester for my C library. I'd like to be able to detect which function have been implemented yet in the archive (.a) file. The goal would be to use this information to skip compiling the tests for the functions that do not exists yet and thus avoiding the builder to crash because it can't link them.

I found that nm mylib.a -f 'just-symbols' is the perfect command just for that, but I can't find a way to get this command's output during the build process.

For now I have this:

    var get_function_list = b.addSystemCommand(&[_][]const u8{ "nm", libft_archive_path, "-f", "just-symbols" });
    _ = get_function_list.captureStdOut();
    exe.step.dependOn(&get_function_list.step);

    const exe_options = b.addOptions();
    exe_options.addOption([]const u8, "libft-path", libft_path);
    exe_options.addOption([]const u8, "function-list", if (get_function_list.captured_stdout) |out| out.basename else "something");

    exe.root_module.addOptions("config", exe_options);

It does run the command but I can't find a way to read the output at the time I need it to.

Thanks for reading !

P.S: If there is a direct way to detect if a function is defined or not it would be much better !

tropic ether
#

Okay, After some debacle with Copilot I managed to make something like this:

-> Build the mylib.a
-> Run the nm command
-> Copy the output to a file function_list.txt
-> Build a micro module that uses @embedFile to embed the .txt with some helper function. (Had to use @setEvalBranchQuota(5000); because the compiler wasn't really happy lol)
-> At comptime use an if to include or not the tests

alpine pawn
#

you can use @hasDecl in ur program e.g.

header.h

#pragma once
void    fn(void);

main.zig

const std = @import("std");
const header = @cImport({
    @cInclude("./header.h");
});

test {
    try std.testing.expectEqual(true,  @hasDecl(header, "fn"));
    try std.testing.expectEqual(false, @hasDecl(header, "meow"));
}

tested with zig test main.zig -I. -lc

tropic ether
alpine pawn
#

its a static lib no?

tropic ether
#

Yes

alpine pawn
#

the linker should emit an error for undefined symbols

tropic ether
#

@hasDecl returns true as long as it is in the header file. It does not check if the linker has access to it

tropic ether
#

I wanted to handle this gracefully and simply mark tests as skipped but continue compiling

#

I'll be sharing this to my classmates for them to use it when making the library. And I want it to have as little developer friction as possible. Because they're learning C and it would be quite the struggle if they had to worry about linking issues with Zig lol.

alpine pawn
#

since theres not a way to exit/skip tests afaik

#

you would have to not include the tests in the build script