Skip to content

Commit

Permalink
Merge pull request #168 from aherrmann/sys-header-deps
Browse files Browse the repository at this point in the history
feat: Enable libc/libc++ linking to expose system headers
  • Loading branch information
aherrmann authored Dec 28, 2023
2 parents 0d57ff3 + 4a37ae6 commit 795bdea
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import %workspace%/.bazelrc.remote
# To update these lines, execute
# `bazel run @rules_bazel_integration_test//tools:update_deleted_packages`
# docs: https://bazel.build/reference/command-line-reference#flag--deleted_packages
build --deleted_packages=e2e/workspace,e2e/workspace/c-sources,e2e/workspace/configure-mode,e2e/workspace/configure-target,e2e/workspace/configure-threaded,e2e/workspace/data-dependencies,e2e/workspace/embed-file,e2e/workspace/include-dependencies,e2e/workspace/include-dependencies/zig-include,e2e/workspace/link-dependencies,e2e/workspace/link-dependencies/static-library,e2e/workspace/linker-script,e2e/workspace/multiple-sources-and-packages-test,e2e/workspace/multiple-sources-binary,e2e/workspace/simple-binary,e2e/workspace/simple-library,e2e/workspace/simple-shared-library,e2e/workspace/simple-test,e2e/workspace/transitive-zig-packages-binary,e2e/workspace/transitive-zig-packages-binary/hello-world,e2e/workspace/transitive-zig-packages-binary/hello-world/data,e2e/workspace/transitive-zig-packages-binary/hello-world/data/hello,e2e/workspace/transitive-zig-packages-binary/hello-world/data/world,e2e/workspace/transitive-zig-packages-binary/hello-world/io,e2e/workspace/zig-package-binary,e2e/workspace/zig-package-binary/data,e2e/workspace/zig-package-binary/io,zig/tests/integration_tests/workspace
query --deleted_packages=e2e/workspace,e2e/workspace/c-sources,e2e/workspace/configure-mode,e2e/workspace/configure-target,e2e/workspace/configure-threaded,e2e/workspace/data-dependencies,e2e/workspace/embed-file,e2e/workspace/include-dependencies,e2e/workspace/include-dependencies/zig-include,e2e/workspace/link-dependencies,e2e/workspace/link-dependencies/static-library,e2e/workspace/linker-script,e2e/workspace/multiple-sources-and-packages-test,e2e/workspace/multiple-sources-binary,e2e/workspace/simple-binary,e2e/workspace/simple-library,e2e/workspace/simple-shared-library,e2e/workspace/simple-test,e2e/workspace/transitive-zig-packages-binary,e2e/workspace/transitive-zig-packages-binary/hello-world,e2e/workspace/transitive-zig-packages-binary/hello-world/data,e2e/workspace/transitive-zig-packages-binary/hello-world/data/hello,e2e/workspace/transitive-zig-packages-binary/hello-world/data/world,e2e/workspace/transitive-zig-packages-binary/hello-world/io,e2e/workspace/zig-package-binary,e2e/workspace/zig-package-binary/data,e2e/workspace/zig-package-binary/io,zig/tests/integration_tests/workspace
build --deleted_packages=e2e/workspace,e2e/workspace/c-sources,e2e/workspace/configure-mode,e2e/workspace/configure-target,e2e/workspace/configure-threaded,e2e/workspace/data-dependencies,e2e/workspace/embed-file,e2e/workspace/include-dependencies,e2e/workspace/include-dependencies/zig-include,e2e/workspace/include-dependencies/zig-std-include,e2e/workspace/link-dependencies,e2e/workspace/link-dependencies/static-library,e2e/workspace/linker-script,e2e/workspace/multiple-sources-and-packages-test,e2e/workspace/multiple-sources-binary,e2e/workspace/simple-binary,e2e/workspace/simple-library,e2e/workspace/simple-shared-library,e2e/workspace/simple-test,e2e/workspace/transitive-zig-packages-binary,e2e/workspace/transitive-zig-packages-binary/hello-world,e2e/workspace/transitive-zig-packages-binary/hello-world/data,e2e/workspace/transitive-zig-packages-binary/hello-world/data/hello,e2e/workspace/transitive-zig-packages-binary/hello-world/data/world,e2e/workspace/transitive-zig-packages-binary/hello-world/io,e2e/workspace/zig-package-binary,e2e/workspace/zig-package-binary/data,e2e/workspace/zig-package-binary/io,zig/tests/integration_tests/workspace
query --deleted_packages=e2e/workspace,e2e/workspace/c-sources,e2e/workspace/configure-mode,e2e/workspace/configure-target,e2e/workspace/configure-threaded,e2e/workspace/data-dependencies,e2e/workspace/embed-file,e2e/workspace/include-dependencies,e2e/workspace/include-dependencies/zig-include,e2e/workspace/include-dependencies/zig-std-include,e2e/workspace/link-dependencies,e2e/workspace/link-dependencies/static-library,e2e/workspace/linker-script,e2e/workspace/multiple-sources-and-packages-test,e2e/workspace/multiple-sources-binary,e2e/workspace/simple-binary,e2e/workspace/simple-library,e2e/workspace/simple-shared-library,e2e/workspace/simple-test,e2e/workspace/transitive-zig-packages-binary,e2e/workspace/transitive-zig-packages-binary/hello-world,e2e/workspace/transitive-zig-packages-binary/hello-world/data,e2e/workspace/transitive-zig-packages-binary/hello-world/data/hello,e2e/workspace/transitive-zig-packages-binary/hello-world/data/world,e2e/workspace/transitive-zig-packages-binary/hello-world/io,e2e/workspace/zig-package-binary,e2e/workspace/zig-package-binary/data,e2e/workspace/zig-package-binary/io,zig/tests/integration_tests/workspace

# Load any settings specific to the current user.
# .bazelrc.user should appear in .gitignore so that settings are not shared with team members
Expand Down
8 changes: 4 additions & 4 deletions docs/rules.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions e2e/workspace/include-dependencies/zig-std-include/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
load("@rules_zig//zig:defs.bzl", "zig_binary", "zig_library", "zig_test")

zig_binary(
name = "binary",
cdeps = ["@rules_zig//zig/lib:libc"],
main = "main.zig",
)

zig_library(
name = "library",
cdeps = ["@rules_zig//zig/lib:libc"],
main = "main.zig",
)

zig_test(
name = "test",
size = "small",
cdeps = ["@rules_zig//zig/lib:libc"],
main = "main.zig",
)

build_test(
name = "build",
size = "small",
targets = [
":binary",
":library",
":test",
],
)

genrule(
name = "output",
outs = ["output.actual"],
cmd = "$(execpath :binary) > $(OUTS)",
tools = [":binary"],
)

diff_test(
name = "output_test",
size = "small",
file1 = ":output.expected",
file2 = ":output.actual",
)
16 changes: 16 additions & 0 deletions e2e/workspace/include-dependencies/zig-std-include/main.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const std = @import("std");
const c = @cImport({
@cInclude("math.h");
});

pub fn main() !void {
const one = c.ceil(0.5);
const two = c.ceil(1.5);
try std.io.getStdOut().writer().print("{d}\n", .{one + two});
}

test "One plus two equals three" {
const one = c.ceil(0.5);
const two = c.ceil(1.5);
try std.testing.expectEqual(@as(f64, 3), one + two);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3
1 change: 1 addition & 0 deletions zig/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ filegroup(
":repositories.bzl",
":toolchain.bzl",
"//zig/config:all_files",
"//zig/lib:all_files",
"//zig/private:all_files",
"//zig/settings:all_files",
"//zig/target:all_files",
Expand Down
39 changes: 39 additions & 0 deletions zig/lib/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Helper targets to enable libc or libc++ linking.
The Zig compiler ships with various versions of libc and libc++. However, it
does not link against these libraries by default, and accordingly does not
expose standard library headers for C imports by default. To enable the import
of C standard library headers the compiler must be instructed to link against
libc or libc++ as appropriate.
The targets provided in this package can be used to accomplish this. Simply add
them to the `cdeps` attribute of your Zig target to enable libc or libc++
linking. E.g.
```
zig_binary(
name = "binary",
main = "main.zig",
cdeps = ["@rules_zig//zig/lib:libc"],
)
```
"""

cc_library(
name = "libc",
linkopts = ["-lc"],
visibility = ["//visibility:public"],
)

cc_library(
name = "libc++",
linkopts = ["-lc++"],
visibility = ["//visibility:public"],
)

# Execute `bazel run //util:update_filegroups` to update this target.
filegroup(
name = "all_files",
srcs = [":BUILD.bazel"],
visibility = ["//zig:__pkg__"],
)
13 changes: 12 additions & 1 deletion zig/private/common/zig_build.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,18 @@ ATTRS = {
providers = [ZigPackageInfo],
),
"cdeps": attr.label_list(
doc = "C dependencies providing headers to include and libraries to link against, typically `cc_library` targets.",
doc = """
C dependencies providing headers to include and libraries to link against, typically `cc_library` targets.
Note, if you need to include C or C++ standard library headers and encounter errors of the following form:
```
note: libc headers not available; compilation does not link against libc
error: 'math.h' file not found
```
Then you may need to list `@rules_zig//zig/lib:libc` or `@rules_zig//zig/lib:libc++` in this attribute.
""",
mandatory = False,
providers = [CcInfo],
),
Expand Down

0 comments on commit 795bdea

Please sign in to comment.