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 @contrib_rules_bazel_integration_test//tools:update_deleted_packages`
build --deleted_packages=e2e/workspace,e2e/workspace/configure-mode,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/configure-mode,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/configure-mode,e2e/workspace/configure-threaded,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/configure-mode,e2e/workspace/configure-threaded,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
1 change: 1 addition & 0 deletions .bazelrc.flags
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
# the same aliases.

build --flag_alias=zig_mode=@rules_zig//zig/settings:mode
build --flag_alias=zig_threaded=@rules_zig//zig/settings:threaded
9 changes: 6 additions & 3 deletions docs/rules.md

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

124 changes: 124 additions & 0 deletions e2e/workspace/configure-threaded/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
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_configure",
"zig_configure_binary",
"zig_configure_test",
"zig_library",
"zig_test",
)
load(":defs.bzl", "run")

zig_library(
name = "library",
main = "library.zig",
tags = ["manual"],
)

zig_configure(
name = "library_single",
actual = ":library",
threaded = "single",
)

zig_configure(
name = "library_multi",
actual = ":library",
threaded = "multi",
)

genrule(
name = "library_single_symbol",
srcs = [":library_single"],
outs = ["library_single_symbol.txt"],
cmd = "$(NM) -j --defined-only $(SRCS) | grep single_threaded > $(OUTS)",
toolchains = ["@bazel_tools//tools/cpp:current_cc_toolchain"],
)

genrule(
name = "library_multi_symbol",
srcs = [":library_multi"],
outs = ["library_multi_symbol.txt"],
cmd = "$(NM) -j --defined-only $(SRCS) | grep multi_threaded > $(OUTS)",
toolchains = ["@bazel_tools//tools/cpp:current_cc_toolchain"],
)

build_test(
name = "library_test",
targets = [
":library_single_symbol",
":library_multi_symbol",
],
)

zig_binary(
name = "binary",
main = "binary.zig",
tags = ["manual"],
)

zig_configure_binary(
name = "binary_single",
actual = ":binary",
threaded = "single",
)

zig_configure_binary(
name = "binary_multi",
actual = ":binary",
threaded = "multi",
)

# NOTE using genrule instead of run fails with Bazel version 5.3.2.
# It seems that the configuration transition is not handled correctly by
# genrule in Bazel version 5.3.2.

run(
name = "binary_single_output",
out = "binary_single_output.actual",
tool = ":binary_single",
)

run(
name = "binary_multi_output",
out = "binary_multi_output.actual",
tool = ":binary_multi",
)

diff_test(
name = "binary_single_output_test",
file1 = ":binary_single_output.expected",
file2 = ":binary_single_output.actual",
)

diff_test(
name = "binary_multi_output_test",
file1 = ":binary_multi_output.expected",
file2 = ":binary_multi_output.actual",
)

zig_test(
name = "_test_single",
main = "test_single.zig",
tags = ["manual"],
)

zig_configure_test(
name = "test_single",
actual = "_test_single",
threaded = "single",
)

zig_test(
name = "_test_multi",
main = "test_multi.zig",
tags = ["manual"],
)

zig_configure_test(
name = "test_multi",
actual = "_test_multi",
threaded = "multi",
)
9 changes: 9 additions & 0 deletions e2e/workspace/configure-threaded/binary.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const std = @import("std");
const builtin = @import("builtin");

pub fn main() void {
std.io.getStdOut().writer().print(
"{}\n",
.{builtin.single_threaded},
) catch unreachable;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
false
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
19 changes: 19 additions & 0 deletions e2e/workspace/configure-threaded/defs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""Implements a rule to run a binary and write its output to a file."""

def _run_impl(ctx):
ctx.actions.run_shell(
outputs = [ctx.outputs.out],
tools = [ctx.executable.tool],
mnemonic = "RunBinary",
command = "{} > {}".format(ctx.executable.tool.path, ctx.outputs.out.path),
progress_message = "Running binary to create %{output}",
)
return [DefaultInfo(files = depset([ctx.outputs.out]))]

run = rule(
_run_impl,
attrs = {
"out": attr.output(),
"tool": attr.label(executable = True, cfg = "exec"),
},
)
8 changes: 8 additions & 0 deletions e2e/workspace/configure-threaded/library.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const std = @import("std");
const builtin = @import("builtin");

comptime {
@export(internalName, .{ .name = if (builtin.single_threaded) "single_threaded" else "multi_threaded", .linkage = .Strong });
}

fn internalName() callconv(.C) void {}
6 changes: 6 additions & 0 deletions e2e/workspace/configure-threaded/test_multi.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const std = @import("std");
const builtin = @import("builtin");

test "single_threaded is false" {
try std.testing.expect(!builtin.single_threaded);
}
6 changes: 6 additions & 0 deletions e2e/workspace/configure-threaded/test_single.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const std = @import("std");
const builtin = @import("builtin");

test "single_threaded is true" {
try std.testing.expect(builtin.single_threaded);
}
1 change: 1 addition & 0 deletions zig/config/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ filegroup(
srcs = [
":BUILD.bazel",
"//zig/config/mode:all_files",
"//zig/config/threaded:all_files",
],
visibility = ["//zig:__pkg__"],
)
20 changes: 20 additions & 0 deletions zig/config/threaded/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
config_setting(
name = "multi",
flag_values = {
"//zig/settings:threaded": "multi",
},
)

config_setting(
name = "single",
flag_values = {
"//zig/settings:threaded": "single",
},
)

# Execute `bazel run //util:update_filegroups` to update this target.
filegroup(
name = "all_files",
srcs = [":BUILD.bazel"],
visibility = ["//zig/config:__pkg__"],
)
1 change: 1 addition & 0 deletions zig/private/providers/zig_settings_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ZigSettingsInfo = provider(
doc = "Collection of all active Zig build settings.",
fields = {
"mode": "The Zig build mode.",
"threaded": "The Zig multi- or single-threaded setting.",
"args": "The collected compiler arguments for all active settings.",
},
)
Expand Down
15 changes: 15 additions & 0 deletions zig/private/settings.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ ATTRS = {
doc = "The build mode setting.",
mandatory = True,
),
"threaded": attr.label(
doc = "The Zig multi- or single-threaded setting.",
mandatory = True,
),
}

MODE_ARGS = {
Expand All @@ -22,14 +26,25 @@ MODE_ARGS = {

MODE_VALUES = ["debug", "release_safe", "release_small", "release_fast"]

THREADED_ARGS = {
"multi": ["-fno-single-threaded"],
"single": ["-fsingle-threaded"],
}

THREADED_VALUES = ["multi", "single"]

def _settings_impl(ctx):
args = []

mode = ctx.attr.mode[BuildSettingInfo].value
args.extend(MODE_ARGS[mode])

threaded = ctx.attr.threaded[BuildSettingInfo].value
args.extend(THREADED_ARGS[threaded])

settings_info = ZigSettingsInfo(
mode = mode,
threaded = threaded,
args = args,
)

Expand Down
21 changes: 17 additions & 4 deletions zig/private/zig_configure.bzl
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
"""Implementation of the zig_configure rule."""

load("@bazel_skylib//lib:paths.bzl", "paths")
load("//zig/private:settings.bzl", "MODE_VALUES")
load("//zig/private:settings.bzl", "MODE_VALUES", "THREADED_VALUES")

DOC = """\
"""

def _zig_transition_impl(settings, attr):
result = {}
result = dict(settings)
if attr.mode:
result["//zig/settings:mode"] = attr.mode
if attr.threaded:
result["//zig/settings:threaded"] = attr.threaded
return result

_zig_transition = transition(
implementation = _zig_transition_impl,
inputs = [],
outputs = ["//zig/settings:mode"],
inputs = [
"//zig/settings:mode",
"//zig/settings:threaded",
],
outputs = [
"//zig/settings:mode",
"//zig/settings:threaded",
],
)

def _make_attrs(*, executable, test):
Expand All @@ -31,6 +39,11 @@ def _make_attrs(*, executable, test):
mandatory = False,
values = MODE_VALUES,
),
"threaded": attr.string(
doc = "The threaded setting",
mandatory = False,
values = THREADED_VALUES,
),
"_whitelist_function_transition": attr.label(
default = "@bazel_tools//tools/whitelists/function_transition_whitelist",
),
Expand Down
Loading