Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A separate function to link the libraries #84

Closed
paveloom opened this issue Nov 26, 2022 · 0 comments
Closed

A separate function to link the libraries #84

paveloom opened this issue Nov 26, 2022 · 0 comments
Labels
enhancement New feature or request

Comments

@paveloom
Copy link

paveloom commented Nov 26, 2022

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.

@nektro nektro added the enhancement New feature or request label Jan 3, 2023
@nektro nektro closed this as completed in e2d42e8 Nov 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants