aherrmann / rules_zig

Bazel build rules for Zig

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to work with include paths

persososo opened this issue · comments

I am trying to figure out very basic header import of an existing cpp library, let's call it FOO//:bar.
FOO//:bar happens to publish header b/c/d/file.h.

I am able to create a simple cpp binary that just includes as such:

cc_binary(
    name = "my-bin-cc",
    srcs = [ "hello.cc" ],
    deps = ["@FOO//:bar",],
)

Any of the following uncommented includes works fine:

// This path is not found.
// #include "file.h"
#include "d/file.h"
#include "c/d/file.h"
// This path is not found.
// #include "b/c/d/file.h"
#include "external/b/c/d/file.h"

#include <cstdio>

int main (void) {
    printf("Hello!\n");
    return 0;
}

Now moving to zig, I have:

zig_binary(
    name = "my-bin-zig",
    main = "hello.zig",
    deps = ["@FOO//:bar",], // hacked myself something here to work while #146 is available
)

I am unable to include any of the headers:

pub usingnamespace @cImport({
    // All of these fail.
    // @cInclude("file.h");
    // @cInclude("d/file.h");
    // @cInclude("c/d/file.h");
    // @cInclude("b/c/d/file.h");
    // @cInclude("external/b/c/d/file.h");
});

const std = @import("std");
pub fn main() void {
    std.debug.print("Hello, {s}!\n", .{"World"});
}

I get:

hello.zig:3:20: error: C import failed
bazel-out/darwin_arm64-fastbuild/bin/.zig-cache/local/my-bin-zig/o/197c224361425e2a178a4b6d5d58d80f/cimport.h:1:10: error: 'b/file.h' file not found

I am unsure whether this is bad usage on my part of whether I need to specify include paths (if so, how).
The fact that the cc_binary example works makes me think there may be something missing here.

The intention would be that in the future zig_binary|library|test can depend on headers provided by a cc_library target in the same way as discussed in #15 / #146. In the meantime you may be able to achieve the same by using the extra_srcs attribute and spelling out the full include path in the source file.