Trouble with compiling `tlse`

Hey everyone - it’s been a while and I hope all is well :slight_smile:

I’ve been building a lot of C-Libraries lately and I’ve found one that is puzzling. I want to wrap tlse in a zig library for some backwards compatibility with TLS 1.2 but I keep running into roadblocks with this one. I’m hoping that some of our build system wizards here can point me in the right direction.

For context, I’m compiling on MacOS and using Zig 0.13.0.

Here’s the library: GitHub - eduardsui/tlse: Single C file TLS 1.2/1.3 implementation, using tomcrypt as crypto library

I went for this library because it looks like it should be a snap - there’s about 3 files to care about - tlse.c, tlse.h, and libtomcrypt.c.

The header and source pair are as you’d expect, and you can link in libtomcrypt.c using a flag -DTLS_AMALGAMATION.

Here’s how I am putting this together:

const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const tlse_src = b.dependency("tlse_src", .{ .target = target, .optimize = optimize });

const lib = b.addStaticLibrary(.{
    .name = "tlse_lib",
    .root_source_file = b.path("src/root.zig"),
    .target = target,
    .optimize = optimize,
    .link_libc = true,
});

lib.addIncludePath(tlse_src.path("."));

lib.addCSourceFile(.{
    .file = tlse_src.path("tlse.c"),
    .flags = &.{ "-DTLS_AMALGAMATION", "-DTLS_CURVE25519" },
});

b.installArtifact(lib);

const module = b.addModule("tlse", .{
    .root_source_file = b.path("src/mod.zig"),
    .link_libc = true,
    .imports = &.{.{
        .name = "tlse_lib"
        .module = &lib.root_module,
    }},
});

There may be typo in there but sadly this laptop doesn’t have Zig installed on it. I’m sure if you probably get the idea…

Anyhow, this deceptively compiles. The moment it runs, I hit a trap and it kills the program. I’m curious if someone would be willing to take a crack at building this library and would be willing to share their thoughts.

Thanks!

Also - this builds fine when using the makefile and all the examples work.

Have you run a debugger and seen where it is getting trapped? Zig enables some clang sanitizers by default which might be the cause, you could try building in ReleaseFast or add a flag to disable the sanitizer causing the issue, e.g. -fno-sanitize=undefined.

Thanks for the suggestion - I actually did run it in release fast. It turns out that there are a lot of missing dependencies that this is bringing in and using zig translate-c reveals a lot of them. The build system was not giving me a lot of useful information from the error messages.

That said, this is a really good counterpoint to the c-compatibility Zig build system. The angle bracket includes are murder.