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

feat: support location and make variable expansion #184

Merged
merged 4 commits into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/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
Expand Down
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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)
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.

14 changes: 14 additions & 0 deletions e2e/workspace/location-expansion/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
)
Empty file.
3 changes: 3 additions & 0 deletions e2e/workspace/location-expansion/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const char *rlocationpath = RLOCATIONPATH;
const char *target = TARGET;
const char *zig_target = ZIG_TARGET;
29 changes: 29 additions & 0 deletions e2e/workspace/location-expansion/main.zig
Original file line number Diff line number Diff line change
@@ -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),
);
}
7 changes: 0 additions & 7 deletions internal_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 9 additions & 0 deletions zig/private/common/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ bzl_library(
":data",
":filetypes",
":linker_script",
":location_expansion",
":zig_cache",
":zig_lib_dir",
":zig_target_triple",
Expand All @@ -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"],
Expand Down Expand Up @@ -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",
Expand Down
35 changes: 35 additions & 0 deletions zig/private/common/location_expansion.bzl
Original file line number Diff line number Diff line change
@@ -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
15 changes: 13 additions & 2 deletions zig/private/common/zig_build.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion zig/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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.
########
Expand Down