diff --git a/.bazelrc b/.bazelrc
index b7684f9a..2664b1a5 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/include-dependencies/zig-include-define,e2e/workspace/include-dependencies/zig-include-isystem,e2e/workspace/include-dependencies/zig-std-include,e2e/workspace/link-dependencies,e2e/workspace/link-dependencies/shared-library,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,zig/tests/integration_tests/workspace/custom_interpreter
-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-include-define,e2e/workspace/include-dependencies/zig-include-isystem,e2e/workspace/include-dependencies/zig-std-include,e2e/workspace/link-dependencies,e2e/workspace/link-dependencies/shared-library,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,zig/tests/integration_tests/workspace/custom_interpreter
+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-include-define,e2e/workspace/include-dependencies/zig-include-isystem,e2e/workspace/include-dependencies/zig-std-include,e2e/workspace/link-dependencies,e2e/workspace/link-dependencies/shared-library,e2e/workspace/link-dependencies/static-library,e2e/workspace/linker-script,e2e/workspace/location-expansion,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,zig/tests/integration_tests/workspace/custom_interpreter
+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-include-define,e2e/workspace/include-dependencies/zig-include-isystem,e2e/workspace/include-dependencies/zig-std-include,e2e/workspace/link-dependencies,e2e/workspace/link-dependencies/shared-library,e2e/workspace/link-dependencies/static-library,e2e/workspace/linker-script,e2e/workspace/location-expansion,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,zig/tests/integration_tests/workspace/custom_interpreter
# 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/MODULE.bazel b/MODULE.bazel
index 0fdc92ce..6abb1c6f 100644
--- a/MODULE.bazel
+++ b/MODULE.bazel
@@ -4,6 +4,7 @@ module(
compatibility_level = 1,
)
+bazel_dep(name = "aspect_bazel_lib", version = "2.0.0")
bazel_dep(name = "bazel_skylib", version = "1.5.0")
bazel_dep(name = "platforms", version = "0.0.8")
@@ -15,7 +16,6 @@ register_toolchains("@rules_zig//zig/target:all")
register_toolchains("@zig_toolchains//:all")
-bazel_dep(name = "aspect_bazel_lib", version = "2.1.0", dev_dependency = True)
bazel_dep(name = "stardoc", version = "0.6.2", dev_dependency = True, repo_name = "io_bazel_stardoc")
bazel_dep(name = "gazelle", version = "0.35.0", dev_dependency = True, repo_name = "bazel_gazelle")
bazel_dep(name = "bazel_skylib_gazelle_plugin", version = "1.5.0", dev_dependency = True)
diff --git a/docs/rules.md b/docs/rules.md
index 4d04e618..16687c3b 100644
--- a/docs/rules.md
+++ b/docs/rules.md
@@ -42,7 +42,7 @@ zig_binary(
| 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.
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 | `[]` |
+| copts | C compiler flags required to build the C sources of the target. Subject to location expansion. | 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 | `[]` |
| linker_script | Custom linker script for the target. | Label | optional | `None` |
@@ -268,7 +268,7 @@ zig_library(
| 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.
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 | `[]` |
+| copts | C compiler flags required to build the C sources of the target. Subject to location expansion. | 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 | `[]` |
| linker_script | Custom linker script for the target. | Label | optional | `None` |
@@ -361,7 +361,7 @@ zig_shared_library(
| 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.
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 | `[]` |
+| copts | C compiler flags required to build the C sources of the target. Subject to location expansion. | 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 | `[]` |
| linker_script | Custom linker script for the target. | Label | optional | `None` |
@@ -407,7 +407,7 @@ zig_test(
| 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.
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 | `[]` |
+| copts | C compiler flags required to build the C sources of the target. Subject to location expansion. | 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 | `[]` |
| linker_script | Custom linker script for the target. | Label | optional | `None` |
diff --git a/e2e/workspace/location-expansion/BUILD.bazel b/e2e/workspace/location-expansion/BUILD.bazel
new file mode 100644
index 00000000..0daf38ed
--- /dev/null
+++ b/e2e/workspace/location-expansion/BUILD.bazel
@@ -0,0 +1,14 @@
+load("@rules_zig//zig:defs.bzl", "zig_test")
+
+zig_test(
+ name = "test",
+ size = "small",
+ copts = [
+ '-DRLOCATIONPATH="$(rlocationpath :data.txt)"',
+ '-DTARGET="$(TARGET)"',
+ '-DZIG_TARGET="$(ZIG_TARGET)"',
+ ],
+ csrcs = ["main.c"],
+ data = ["data.txt"],
+ main = "main.zig",
+)
diff --git a/e2e/workspace/location-expansion/data.txt b/e2e/workspace/location-expansion/data.txt
new file mode 100644
index 00000000..e69de29b
diff --git a/e2e/workspace/location-expansion/main.c b/e2e/workspace/location-expansion/main.c
new file mode 100644
index 00000000..09121ed1
--- /dev/null
+++ b/e2e/workspace/location-expansion/main.c
@@ -0,0 +1,3 @@
+const char *rlocationpath = RLOCATIONPATH;
+const char *target = TARGET;
+const char *zig_target = ZIG_TARGET;
diff --git a/e2e/workspace/location-expansion/main.zig b/e2e/workspace/location-expansion/main.zig
new file mode 100644
index 00000000..56308f81
--- /dev/null
+++ b/e2e/workspace/location-expansion/main.zig
@@ -0,0 +1,29 @@
+const std = @import("std");
+const builtin = @import("builtin");
+
+extern const rlocationpath: [*:0]const u8;
+extern const target: [*:0]const u8;
+extern const zig_target: [*:0]const u8;
+
+test "RLOCATIONPATH is set" {
+ try std.testing.expectStringEndsWith(
+ std.mem.sliceTo(rlocationpath, 0),
+ "location-expansion/data.txt",
+ );
+}
+
+test "TARGET is set" {
+ try std.testing.expectEqualStrings(
+ "//location-expansion:test",
+ std.mem.sliceTo(target, 0),
+ );
+}
+
+test "ZIG_TARGET is set" {
+ const actual_target = try builtin.target.linuxTriple(std.testing.allocator);
+ defer std.testing.allocator.free(actual_target);
+ try std.testing.expectEqualStrings(
+ actual_target,
+ std.mem.sliceTo(zig_target, 0),
+ );
+}
diff --git a/internal_deps.bzl b/internal_deps.bzl
index 1fd39da0..0768cac4 100644
--- a/internal_deps.bzl
+++ b/internal_deps.bzl
@@ -81,13 +81,6 @@ def rules_zig_internal_deps():
urls = ["https://github.com/bazelbuild/stardoc/releases/download/0.6.2/stardoc-0.6.2.tar.gz"],
)
- http_archive(
- name = "aspect_bazel_lib",
- sha256 = "fc8bd670380eaba5314769abbe9fee21d641e3da06d9d26b8073a301f6d62332",
- strip_prefix = "bazel-lib-2.1.0",
- url = "https://github.com/aspect-build/bazel-lib/releases/download/v2.1.0/bazel-lib-v2.1.0.tar.gz",
- )
-
http_archive(
name = "rules_bazel_integration_test",
sha256 = "6e65d497c68f5794349bfa004369e144063686ce1ebd0227717cd23285be45ef",
diff --git a/zig/private/common/BUILD.bazel b/zig/private/common/BUILD.bazel
index 110bcca9..16af2d66 100644
--- a/zig/private/common/BUILD.bazel
+++ b/zig/private/common/BUILD.bazel
@@ -24,6 +24,7 @@ bzl_library(
":data",
":filetypes",
":linker_script",
+ ":location_expansion",
":zig_cache",
":zig_lib_dir",
":zig_target_triple",
@@ -40,6 +41,13 @@ bzl_library(
deps = ["@bazel_skylib//lib:paths"],
)
+bzl_library(
+ name = "location_expansion",
+ srcs = ["location_expansion.bzl"],
+ visibility = ["//zig:__subpackages__"],
+ deps = ["@aspect_bazel_lib//lib:expand_make_vars"],
+)
+
bzl_library(
name = "csrcs",
srcs = ["csrcs.bzl"],
@@ -80,6 +88,7 @@ filegroup(
":data.bzl",
":filetypes.bzl",
":linker_script.bzl",
+ ":location_expansion.bzl",
":zig_build.bzl",
":zig_cache.bzl",
":zig_lib_dir.bzl",
diff --git a/zig/private/common/location_expansion.bzl b/zig/private/common/location_expansion.bzl
new file mode 100644
index 00000000..0f0b2994
--- /dev/null
+++ b/zig/private/common/location_expansion.bzl
@@ -0,0 +1,35 @@
+"""Expand location placeholders and make variables on attributes.
+"""
+
+load("@aspect_bazel_lib//lib:expand_make_vars.bzl", "expand_locations", "expand_variables")
+
+def location_expansion(*, ctx, targets, outputs, attribute_name, strings):
+ """Perform location make variable expansion on the given strings.
+
+ Args:
+ ctx: Context object.
+ targets: List of Target, Expand locations of these targets.
+ outputs: List of File, Outputs of the rule to expand.
+ attribute_name: String, The name of the attribute we're expanding.
+ strings: List of String, The actual string values to expand.
+
+ Returns:
+ The given strings with location and make variable references substituted.
+ """
+ result = []
+
+ for s in strings:
+ result.append(
+ expand_variables(
+ ctx,
+ expand_locations(
+ ctx,
+ s,
+ targets,
+ ),
+ outputs,
+ attribute_name,
+ ),
+ )
+
+ return result
diff --git a/zig/private/common/zig_build.bzl b/zig/private/common/zig_build.bzl
index 58d1ffa6..fc134561 100644
--- a/zig/private/common/zig_build.bzl
+++ b/zig/private/common/zig_build.bzl
@@ -10,6 +10,7 @@ load(
"ZIG_SOURCE_EXTENSIONS",
)
load("//zig/private/common:linker_script.bzl", "zig_linker_script")
+load("//zig/private/common:location_expansion.bzl", "location_expansion")
load("//zig/private/common:zig_cache.bzl", "zig_cache_output")
load("//zig/private/common:zig_lib_dir.bzl", "zig_lib_dir")
load(
@@ -49,7 +50,7 @@ ATTRS = {
mandatory = False,
),
"copts": attr.string_list(
- doc = "C compiler flags required to build the C sources of the target.",
+ doc = "C compiler flags required to build the C sources of the target. Subject to location expansion.",
mandatory = False,
),
"deps": attr.label_list(
@@ -163,8 +164,18 @@ def zig_build_impl(ctx, *, kind):
direct_inputs.extend(ctx.files.extra_srcs)
args.add(ctx.file.main)
+ location_targets = ctx.attr.data
+
+ copts = location_expansion(
+ ctx = ctx,
+ targets = location_targets,
+ outputs = outputs,
+ attribute_name = "copts",
+ strings = ctx.attr.copts,
+ )
+
zig_csrcs(
- copts = ctx.attr.copts,
+ copts = copts,
csrcs = ctx.files.csrcs,
inputs = direct_inputs,
args = args,
diff --git a/zig/repositories.bzl b/zig/repositories.bzl
index be3ec58a..9155cf79 100644
--- a/zig/repositories.bzl
+++ b/zig/repositories.bzl
@@ -22,7 +22,6 @@ def _http_archive(name, **kwargs):
def rules_zig_dependencies():
"""Register dependencies required by rules_zig."""
- # The minimal version of bazel_skylib we require
_http_archive(
name = "bazel_skylib",
sha256 = "74d544d96f4a5bb630d465ca8bbcfe231e3594e5aae57e1edbf17a6eb3ca2506",
@@ -32,6 +31,13 @@ def rules_zig_dependencies():
],
)
+ _http_archive(
+ name = "aspect_bazel_lib",
+ sha256 = "fc8bd670380eaba5314769abbe9fee21d641e3da06d9d26b8073a301f6d62332",
+ strip_prefix = "bazel-lib-2.1.0",
+ url = "https://github.com/aspect-build/bazel-lib/releases/download/v2.1.0/bazel-lib-v2.1.0.tar.gz",
+ )
+
########
# Remaining content of the file is only used to support toolchains.
########