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

Set libc.txt file for a whole tree in build.zig #22308

Open
pfgithub opened this issue Dec 24, 2024 · 0 comments
Open

Set libc.txt file for a whole tree in build.zig #22308

pfgithub opened this issue Dec 24, 2024 · 0 comments

Comments

@pfgithub
Copy link
Contributor

pfgithub commented Dec 24, 2024

Targeting android, zig doesn't provide a libc, so my application needs to use a libc.txt file:

// my_app/build.zig:
    const libc_file = b.path("android_libc.txt");
    const harfbuzz_dep = b.dependency("harfbuzz", .{ .target = target, .optimize = optimize });
    my_app.root_module.linkLibrary(harfbuzz_dep.artifact("harfbuzz");
    my_app.setLibCFile(libc_file);
    my_app.linkLibC();

// harfbuzz/build.zig:
    const harfbuzz = b.addStaticLibrary(.{ .name = "harfbuzz", ... });
    harfbuzz.linkLibC();
    b.installArtifact(harfbuzz);

The problem is that the harfbuzz module does not get the libc file, and then fails to compile.

Allowing my_app to pass a libc file would require adding a LazyPath option to harfbuzz and using it to setLibCFile:

// my_app/build.zig:
    const libc_file = b.path("android_libc.txt");
-   const harfbuzz_dep = b.dependency("harfbuzz", .{ .target = target, .optimize = optimize });
+   const harfbuzz_dep = b.dependency("harfbuzz", .{ .target = target, .optimize = optimize, .libc = libc_file });
    my_app.root_module.linkLibrary(harfbuzz_dep.artifact("harfbuzz");
    my_app.setLibCFile(libc_file);
    my_app.linkLibC();

// harfbuzz/build.zig:
+   const libc_file = b.option(std.Build.LazyPath, "libc", "libc file for compilation");
    const harfbuzz = b.addStaticLibrary(.{ .name = "harfbuzz", ... });
+   if(libc_file) |f| harfbuzz.setLibCFile(f);
    harfbuzz.linkLibC();
    b.installArtifact(harfbuzz);

Currently, zero libraries in allyourcodebase do this. This seems like an unresonable ask for libraries to implement.

Proposal

Instead, libc.txt could be part of 'target', so it gets automatically passed down correctly without opt-in from dependencies.

// my_app/build.zig:
-   const libc_file = b.path("android_libc.txt");
+   const target = b.resolveTargetQuery(.{
+       ...
+       .libc = b.path("android_libc.txt"),
+   });
    const harfbuzz_dep = b.dependency("harfbuzz", .{ .target = target, .optimize = optimize });
    my_app.root_module.linkLibrary(harfbuzz_dep.artifact("harfbuzz");
-   my_app.setLibCFile(libc_file);
    my_app.linkLibC();

// harfbuzz/build.zig:
    const harfbuzz = b.addStaticLibrary(.{ .name = "harfbuzz", ... });
    harfbuzz.linkLibC();
    b.installArtifact(harfbuzz);

This way, no changes are needed to any dependencies. It could also possibly replace the --libc option in system integration options if libc file is added to the end of the target quadruple, like aarch64-linux-android-lp64-libc.txt or something

Alternatives

  • Using --libc to set the libc file for the whole build could maybe work, but any tools which want to run on the host system (.target = b.resolveTargetQuery(.{}) + b.addRunArtifact()) get their libc file replaced which makes them fail to build.
  • Update every dependency to add a libc option, like described above.
  • Currently, I'm using a hack where I explore the b.graph.dependency_cache, find every installed artifact, and set the libc file on each one: https://github.com/pfgithub/blockeditor/blob/9c4bf03fe706e3cf679b8ff41dd626637807eb74/packages/beui_impl_android/build.zig#L135-L143
  • b.linkLibC() -> b.linkLibrary(b.standardLibCOption(.{})) & update all b.dependency() to .{ .target, .optimize, .libc = b.standardLibCOption(.{}) }
  • Something else?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant