Skip to content
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 @@ -7,8 +7,8 @@ import %workspace%/.bazelrc.flags
# Deleted packages for integration tests.
# To update these lines, execute
# `bazel run @rules_bazel_integration_test//tools:update_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/embed-file,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-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/embed-file,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-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/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-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/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-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
12 changes: 8 additions & 4 deletions docs/rules.md

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

31 changes: 31 additions & 0 deletions e2e/workspace/data-dependencies/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
load("@rules_zig//zig:defs.bzl", "zig_package", "zig_test")

zig_test(
name = "direct-data",
data = ["data.txt"],
main = "direct-data.zig",
)

zig_package(
name = "direct-package",
data = ["data.txt"],
main = "direct-package.zig",
)

zig_test(
name = "direct-package-data",
main = "direct-package-data.zig",
deps = [":direct-package"],
)

zig_package(
name = "indirect-package",
main = "indirect-package.zig",
deps = [":direct-package"],
)

zig_test(
name = "indirect-package-data",
main = "indirect-package-data.zig",
deps = [":indirect-package"],
)
1 change: 1 addition & 0 deletions e2e/workspace/data-dependencies/data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!
11 changes: 11 additions & 0 deletions e2e/workspace/data-dependencies/direct-data.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const std = @import("std");

test "read data file" {
var file = try std.fs.cwd().openFile("data-dependencies/data.txt", .{});
defer file.close();

const content = try file.readToEndAlloc(std.testing.allocator, 4096);
defer std.testing.allocator.free(content);

try std.testing.expectEqualStrings("Hello World!\n", content);
}
9 changes: 9 additions & 0 deletions e2e/workspace/data-dependencies/direct-package-data.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const std = @import("std");
const direct_package = @import("direct-package");

test "read data file" {
const content = try direct_package.readData(std.testing.allocator);
defer std.testing.allocator.free(content);

try std.testing.expectEqualStrings("Hello World!\n", content);
}
10 changes: 10 additions & 0 deletions e2e/workspace/data-dependencies/direct-package.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const std = @import("std");

pub fn readData(allocator: std.mem.Allocator) ![]u8 {
var file = try std.fs.cwd().openFile("data-dependencies/data.txt", .{});
defer file.close();

const content = try file.readToEndAlloc(allocator, 4096);

return content;
}
9 changes: 9 additions & 0 deletions e2e/workspace/data-dependencies/indirect-package-data.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const std = @import("std");
const indirect_package = @import("indirect-package");

test "read data file" {
const content = try indirect_package.readData(std.testing.allocator);
defer std.testing.allocator.free(content);

try std.testing.expectEqualStrings("Hello World!\n", content);
}
3 changes: 3 additions & 0 deletions e2e/workspace/data-dependencies/indirect-package.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const direct_package = @import("direct-package");

pub const readData = direct_package.readData;
1 change: 1 addition & 0 deletions zig/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ bzl_library(
srcs = ["zig_package.bzl"],
visibility = ["//zig:__subpackages__"],
deps = [
"//zig/private/common:data",
"//zig/private/common:filetypes",
"//zig/private/providers:zig_package_info",
],
Expand Down
8 changes: 8 additions & 0 deletions zig/private/common/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ bzl_library(
visibility = ["//zig:__subpackages__"],
deps = [
":csrcs",
":data",
":filetypes",
":linker_script",
":zig_cache",
Expand All @@ -36,6 +37,12 @@ bzl_library(
visibility = ["//zig:__subpackages__"],
)

bzl_library(
name = "data",
srcs = ["data.bzl"],
visibility = ["//zig:__subpackages__"],
)

bzl_library(
name = "filetypes",
srcs = ["filetypes.bzl"],
Expand All @@ -54,6 +61,7 @@ filegroup(
srcs = [
":BUILD.bazel",
":csrcs.bzl",
":data.bzl",
":filetypes.bzl",
":linker_script.bzl",
":zig_build.bzl",
Expand Down
39 changes: 39 additions & 0 deletions zig/private/common/data.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Handle runtime data dependencies."""

def zig_collect_data(*, data, deps, transitive_data, transitive_runfiles):
"""Handle data dependencies.

Collects runtime data from the given dependencies.
Data dependency attributes will contribute both,
their output files and their own runtime data dependencies.
Other dependency attributes will only contribute
their own runtime data dependencies.

Args:
data: List of Target, Data dependency attributes.
deps: List of Target, Other dependency attributes.
transitive_data: List of depset of File; mutable, Append data file dependencies.
transitive_runfiles: List of runfiles; mutable, Append runfile dependencies.
"""
for data in data:
transitive_data.append(data[DefaultInfo].files)
transitive_runfiles.append(data[DefaultInfo].default_runfiles)

for dep in deps:
transitive_runfiles.append(dep[DefaultInfo].default_runfiles)

def zig_create_runfiles(*, ctx_runfiles, direct_data, transitive_data, transitive_runfiles):
"""Create a new runfiles object.

The newly created runfiles will bundle all provided data files and runfiles.

Args:
ctx_runfiles: Runfiles constructor function.
direct_data: List of File, Data files.
transitive_data: List of depset of File, Data files.
transitive_runfiles: List of depset of File, Runfiles.
"""
return ctx_runfiles(
files = direct_data,
transitive_files = depset(transitive = transitive_data),
).merge_all(transitive_runfiles)
40 changes: 30 additions & 10 deletions zig/private/common/zig_build.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ load(
"ZIG_SOURCE_EXTENSIONS",
)
load("//zig/private/common:csrcs.bzl", "zig_csrcs")
load("//zig/private/common:data.bzl", "zig_collect_data", "zig_create_runfiles")
load("//zig/private/common:linker_script.bzl", "zig_linker_script")
load("//zig/private/common:zig_cache.bzl", "zig_cache_output")
load("//zig/private/common:zig_lib_dir.bzl", "zig_lib_dir")
Expand Down Expand Up @@ -59,6 +60,11 @@ ATTRS = {
allow_single_file = True,
mandatory = False,
),
"data": attr.label_list(
allow_files = True,
doc = "Files required by the target during runtime.",
mandatory = False,
),
"_settings": attr.label(
default = "//zig/settings",
doc = "Zig build settings.",
Expand All @@ -84,15 +90,24 @@ def zig_build_impl(ctx, *, kind):
zigtoolchaininfo = ctx.toolchains["//zig:toolchain_type"].zigtoolchaininfo
zigtargetinfo = ctx.toolchains["//zig/target:toolchain_type"].zigtargetinfo

default_executable = None
default_files = None
default_runfiles = None
executable = None
files = None
direct_data = []
transitive_data = []
transitive_runfiles = []

outputs = []

direct_inputs = []
transitive_inputs = []

zig_collect_data(
data = ctx.attr.data,
deps = ctx.attr.deps,
transitive_data = transitive_data,
transitive_runfiles = transitive_runfiles,
)

args = ctx.actions.args()
args.use_param_file("@%s")

Expand All @@ -102,17 +117,17 @@ def zig_build_impl(ctx, *, kind):
outputs.append(output)
args.add(output, format = "-femit-bin=%s")

default_executable = output
default_files = depset([output])
default_runfiles = ctx.runfiles(files = [output])
executable = output
files = depset([output])
direct_data.append(output)
elif kind == "zig_library":
# TODO[AH] Set `.lib` extension on Windows.
static = ctx.actions.declare_file(ctx.label.name + ".a")
outputs.append(static)
args.add(static, format = "-femit-bin=%s")
# TODO[AH] Support dynamic library output.

default_files = depset([static])
files = depset([static])
else:
fail("Unknown rule kind '{}'.".format(kind))

Expand Down Expand Up @@ -195,9 +210,14 @@ def zig_build_impl(ctx, *, kind):
)

default = DefaultInfo(
executable = default_executable,
files = default_files,
runfiles = default_runfiles,
executable = executable,
files = files,
runfiles = zig_create_runfiles(
ctx_runfiles = ctx.runfiles,
direct_data = [],
transitive_data = transitive_data,
transitive_runfiles = transitive_runfiles,
),
)

return [default]
Loading