From c74d27afd84223fec17f3c48bc62d20574f4fc54 Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Thu, 28 Dec 2023 14:13:11 +0100 Subject: [PATCH 1/6] test: C standard library header include --- .../zig-std-include/BUILD.bazel | 43 +++++++++++++++++++ .../zig-std-include/main.zig | 16 +++++++ .../zig-std-include/output.expected | 1 + 3 files changed, 60 insertions(+) create mode 100644 e2e/workspace/include-dependencies/zig-std-include/BUILD.bazel create mode 100644 e2e/workspace/include-dependencies/zig-std-include/main.zig create mode 100644 e2e/workspace/include-dependencies/zig-std-include/output.expected diff --git a/e2e/workspace/include-dependencies/zig-std-include/BUILD.bazel b/e2e/workspace/include-dependencies/zig-std-include/BUILD.bazel new file mode 100644 index 00000000..9076c395 --- /dev/null +++ b/e2e/workspace/include-dependencies/zig-std-include/BUILD.bazel @@ -0,0 +1,43 @@ +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", + main = "main.zig", +) + +zig_library( + name = "library", + main = "main.zig", +) + +zig_test( + name = "test", + size = "small", + 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", +) diff --git a/e2e/workspace/include-dependencies/zig-std-include/main.zig b/e2e/workspace/include-dependencies/zig-std-include/main.zig new file mode 100644 index 00000000..c5ce540f --- /dev/null +++ b/e2e/workspace/include-dependencies/zig-std-include/main.zig @@ -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); +} diff --git a/e2e/workspace/include-dependencies/zig-std-include/output.expected b/e2e/workspace/include-dependencies/zig-std-include/output.expected new file mode 100644 index 00000000..00750edc --- /dev/null +++ b/e2e/workspace/include-dependencies/zig-std-include/output.expected @@ -0,0 +1 @@ +3 From 9bf4f41fe317f4d57986730765866f497a08ca11 Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Thu, 28 Dec 2023 14:41:09 +0100 Subject: [PATCH 2/6] Helper targets to enable libc/libc++ linking --- zig/lib/BUILD.bazel | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 zig/lib/BUILD.bazel diff --git a/zig/lib/BUILD.bazel b/zig/lib/BUILD.bazel new file mode 100644 index 00000000..9caf2c5d --- /dev/null +++ b/zig/lib/BUILD.bazel @@ -0,0 +1,32 @@ +"""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"], +) From 5f6460779cd7f8dd6d716fec0612587384642469 Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Thu, 28 Dec 2023 14:41:42 +0100 Subject: [PATCH 3/6] Use helper targets in tests --- e2e/workspace/include-dependencies/zig-std-include/BUILD.bazel | 3 +++ 1 file changed, 3 insertions(+) diff --git a/e2e/workspace/include-dependencies/zig-std-include/BUILD.bazel b/e2e/workspace/include-dependencies/zig-std-include/BUILD.bazel index 9076c395..a8dbe0ab 100644 --- a/e2e/workspace/include-dependencies/zig-std-include/BUILD.bazel +++ b/e2e/workspace/include-dependencies/zig-std-include/BUILD.bazel @@ -4,17 +4,20 @@ 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", ) From 04fda483f34d04749d36eba90d0c2bba53261745 Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Thu, 28 Dec 2023 14:47:36 +0100 Subject: [PATCH 4/6] Mention libc/libc++ helper targets in doc strings --- zig/private/common/zig_build.bzl | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/zig/private/common/zig_build.bzl b/zig/private/common/zig_build.bzl index 6ac2676b..c7475981 100644 --- a/zig/private/common/zig_build.bzl +++ b/zig/private/common/zig_build.bzl @@ -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], ), From 40e99d9904f1f1a698e66ba97881c5f250907f3b Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Thu, 28 Dec 2023 14:43:31 +0100 Subject: [PATCH 5/6] Update generated files --- .bazelrc | 4 ++-- docs/rules.md | 8 ++++---- zig/BUILD.bazel | 1 + zig/lib/BUILD.bazel | 7 +++++++ 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/.bazelrc b/.bazelrc index 300e2334..ecb0de8f 100644 --- a/.bazelrc +++ b/.bazelrc @@ -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/configure-mode,e2e/workspace/configure-target,e2e/workspace/configure-threaded,e2e/workspace/c-sources,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/configure-mode,e2e/workspace/configure-target,e2e/workspace/configure-threaded,e2e/workspace/c-sources,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 diff --git a/docs/rules.md b/docs/rules.md index 3c98e602..4d04e618 100644 --- a/docs/rules.md +++ b/docs/rules.md @@ -41,7 +41,7 @@ zig_binary( | deps | Packages required to build the target. | List of labels | optional | `[]` | | srcs | Other Zig source files required to build the target, e.g. files imported using `@import`. | List of labels | optional | `[]` | | data | Files required by the target during runtime. | List of labels | optional | `[]` | -| cdeps | C dependencies providing headers to include and libraries to link against, typically `cc_library` targets. | List of labels | optional | `[]` | +| cdeps | 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. | List of labels | optional | `[]` | | copts | C compiler flags required to build the C sources of the target. | List of strings | optional | `[]` | | csrcs | C source files required to build the target. | List of labels | optional | `[]` | | extra_srcs | Other files required to build the target, e.g. files embedded using `@embedFile`. | List of labels | optional | `[]` | @@ -267,7 +267,7 @@ zig_library( | deps | Packages required to build the target. | List of labels | optional | `[]` | | srcs | Other Zig source files required to build the target, e.g. files imported using `@import`. | List of labels | optional | `[]` | | data | Files required by the target during runtime. | List of labels | optional | `[]` | -| cdeps | C dependencies providing headers to include and libraries to link against, typically `cc_library` targets. | List of labels | optional | `[]` | +| cdeps | 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. | List of labels | optional | `[]` | | copts | C compiler flags required to build the C sources of the target. | List of strings | optional | `[]` | | csrcs | C source files required to build the target. | List of labels | optional | `[]` | | extra_srcs | Other files required to build the target, e.g. files embedded using `@embedFile`. | List of labels | optional | `[]` | @@ -360,7 +360,7 @@ zig_shared_library( | deps | Packages required to build the target. | List of labels | optional | `[]` | | srcs | Other Zig source files required to build the target, e.g. files imported using `@import`. | List of labels | optional | `[]` | | data | Files required by the target during runtime. | List of labels | optional | `[]` | -| cdeps | C dependencies providing headers to include and libraries to link against, typically `cc_library` targets. | List of labels | optional | `[]` | +| cdeps | 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. | List of labels | optional | `[]` | | copts | C compiler flags required to build the C sources of the target. | List of strings | optional | `[]` | | csrcs | C source files required to build the target. | List of labels | optional | `[]` | | extra_srcs | Other files required to build the target, e.g. files embedded using `@embedFile`. | List of labels | optional | `[]` | @@ -406,7 +406,7 @@ zig_test( | deps | Packages required to build the target. | List of labels | optional | `[]` | | srcs | Other Zig source files required to build the target, e.g. files imported using `@import`. | List of labels | optional | `[]` | | data | Files required by the target during runtime. | List of labels | optional | `[]` | -| cdeps | C dependencies providing headers to include and libraries to link against, typically `cc_library` targets. | List of labels | optional | `[]` | +| cdeps | 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. | List of labels | optional | `[]` | | copts | C compiler flags required to build the C sources of the target. | List of strings | optional | `[]` | | csrcs | C source files required to build the target. | List of labels | optional | `[]` | | extra_srcs | Other files required to build the target, e.g. files embedded using `@embedFile`. | List of labels | optional | `[]` | diff --git a/zig/BUILD.bazel b/zig/BUILD.bazel index 8f9aabc7..7b47159b 100644 --- a/zig/BUILD.bazel +++ b/zig/BUILD.bazel @@ -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", diff --git a/zig/lib/BUILD.bazel b/zig/lib/BUILD.bazel index 9caf2c5d..e010d171 100644 --- a/zig/lib/BUILD.bazel +++ b/zig/lib/BUILD.bazel @@ -30,3 +30,10 @@ cc_library( 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__"], +) From 4a37ae607fa7f49ac132e524e814655608cb1458 Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Thu, 28 Dec 2023 14:56:11 +0100 Subject: [PATCH 6/6] fix .bazelrc --- .bazelrc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.bazelrc b/.bazelrc index ecb0de8f..d37b2b41 100644 --- a/.bazelrc +++ b/.bazelrc @@ -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/configure-mode,e2e/workspace/configure-target,e2e/workspace/configure-threaded,e2e/workspace/c-sources,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/configure-mode,e2e/workspace/configure-target,e2e/workspace/configure-threaded,e2e/workspace/c-sources,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 +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