Skip to content

A separate function to link the libraries #84

@paveloom

Description

@paveloom

Hello there.

Long story short, here's what I've done to override one of the packages with project-specific options inside build.zig:

// <...>

// Create an option to enable Tracy integration
const tracy_enabled_option = b.option(
    bool,
    "tracy",
    "Enable Tracy integration",
) orelse false;
// Create an option to override Tracy's default call stack capture depth
const tracy_depth_option = b.option(
    c_int,
    "tracy-depth",
    "Override Tracy's default call stack capture depth",
) orelse 0;
// Add these options to a separate group of options
const tracy_options = b.addOptions();
exe.addOptions("tracy_options", tracy_options);
tracy_options.addOption(bool, "tracy", tracy_enabled_option);
tracy_options.addOption(c_int, "tracy_depth", tracy_depth_option);
// Link the Tracy package to the executable
if (deps.pkgs.tracy.pkg) |tracy_pkg| {
    // Add the options as a dependency to the Tracy package
    exe.addPackage(std.build.Pkg{
        .name = "tracy",
        .source = .{ .path = tracy_pkg.source.path },
        .dependencies = &[_]std.build.Pkg{
            .{ .name = "tracy_options", .source = tracy_options.getSource() },
        },
    });
    // If Tracy integration is enabled, link the libraries
    if (tracy_enabled_option) {
        // Gotta call this snippet until there is a nicer way
        inline for (comptime std.meta.declarations(deps.package_data)) |decl| {
            const pkg = @as(deps.Package, @field(deps.package_data, decl.name));
            for (pkg.system_libs) |item| {
                exe.linkSystemLibrary(item);
            }
            inline for (pkg.c_include_dirs) |item| {
                exe.addIncludePath(@field(deps.dirs, decl.name) ++ "/" ++ item);
            }
            inline for (pkg.c_source_files) |item| {
                exe.addCSourceFile(@field(deps.dirs, decl.name) ++ "/" ++ item, pkg.c_source_flags);
            }
        }
    }
}

As you can see, I called the following part of the addAllTo function manually:

zigmod/src/cmd/fetch.zig

Lines 56 to 80 in b47ad60

\\ var llc = false;
\\ var vcpkg = false;
\\ inline for (comptime std.meta.declarations(package_data)) |decl| {
\\ const pkg = @as(Package, @field(package_data, decl.name));
\\ for (pkg.system_libs) |item| {
\\ exe.linkSystemLibrary(item);
\\ llc = true;
\\ }
\\ for (pkg.frameworks) |item| {
\\ if (!std.Target.current.isDarwin()) @panic(exe.builder.fmt("a dependency is attempting to link to the framework {s}, which is only possible under Darwin", .{item}));
\\ exe.linkFramework(item);
\\ llc = true;
\\ }
\\ inline for (pkg.c_include_dirs) |item| {
\\ exe.addIncludePath(@field(dirs, decl.name) ++ "/" ++ item);
\\ llc = true;
\\ }
\\ inline for (pkg.c_source_files) |item| {
\\ exe.addCSourceFile(@field(dirs, decl.name) ++ "/" ++ item, pkg.c_source_flags);
\\ llc = true;
\\ }
\\ vcpkg = vcpkg or pkg.vcpkg;
\\ }
\\ if (llc) exe.linkLibC();
\\ if (builtin.os.tag == .windows and vcpkg) exe.addVcpkgPaths(.static) catch |err| @panic(@errorName(err));

I would like to suggest to create a separate function here for linking libraries, so users can call it separately in the case they need to mess around with packages.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions