#Adding subdirectory of dependency path as include directory

1 messages · Page 1 of 1 (latest)

amber kite
#

As a minimal example of what I'm asking about, suppose I have the following C project in zig 0.12.0-dev.415+5af5d87ad:

// build.zig

const std = @import("std");

const Build = std.Build;

pub fn build(b: *Build) void {
    const freetype_dep = b.dependency("freetype", .{});
    const freetype_lib = freetype_dep.artifact("freetype");

    const exe = b.addExecutable(.{ .name = "a" });
    exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &.{} });
    exe.linkLibrary(freetype_lib);
    b.installArtifact(exe);
}
// main.c

#include <freetype.h>

int main()
{
        FT_Library library;
        if(FT_Init_FreeType(&library)) { return 1; }
        return 0;
}

This fails to build with a missing <freetype.h> file. This is because the actual freetype.h header file is installed under subdirectory freetype/ in the dependency install path. Obviously, I could just change the include directive to <freetype/freetype.h> (which does build), but this is only a minimal example and in the actual project there are lots of includes like this, and since it's actually an intermediate dependency I'd rather not edit the code if I don't have to.

So then my question is if there is a way in build.zig to add the subdirectory freetype/ to the include path.

I tried looking into some of the internal fields of freetype_lib (which is a *std.Build.Step.Compile) but I'm not sure how I might use those to do what I want to do:

  • installed_path seems to always be null
  • installed_headers seems promising but it's just a list of *Step which I think are already type erased at this point?
vernal terrace
#

Without looking at/knowing the freetype dependency your using, it might be a bit tricky to say exactly what you need to do, but I'm pretty sure what you want is freetype_lib.getEmittedBinDirectory() which should give you a LazyPath for the directory containing the header.

amber kite
#

Thanks for the response. I tried using getEmittedBinDirectory() but it doesn't seem to work. Inspecting the actual path indicates it only contains the generated library file (.a) but not the headers.

#

In the end I managed to fix this by using the install_path of the dependency Build. Something like this:

exe.addIncludePath(.{ .path = b.pathJoin(&.{ freetype_lib.step.owner.install_path, "include/freetype" }) });
#

It feels kind of hacky though :/

sour ginkgo
#

if you install a header via the Step.Compile then they're pulled along as an include when you link to it

amber kite
#

Yes, but my problem was that I needed to add an include path that is a subdirectory of the installed include, so I kept getting errors of headers not found.

vernal terrace