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/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/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/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

# 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
60 changes: 60 additions & 0 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-mode/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_debug",
actual = ":library",
mode = "debug",
)

zig_configure(
name = "library_release_safe",
actual = ":library",
mode = "release_safe",
)

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

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

build_test(
name = "library_test",
targets = [
":library_debug_symbol",
":library_release_safe_symbol",
],
)

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

zig_configure_binary(
name = "binary_debug",
actual = ":binary",
mode = "debug",
)

zig_configure_binary(
name = "binary_release_safe",
actual = ":binary",
mode = "release_safe",
)

# 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_debug_output",
out = "binary_debug_output.actual",
tool = ":binary_debug",
)

run(
name = "binary_release_safe_output",
out = "binary_release_safe_output.actual",
tool = ":binary_release_safe",
)

diff_test(
name = "binary_debug_output_test",
file1 = ":binary_debug_output.expected",
file2 = ":binary_debug_output.actual",
)

diff_test(
name = "binary_release_safe_output_test",
file1 = ":binary_release_safe_output.expected",
file2 = ":binary_release_safe_output.actual",
)

zig_test(
name = "_test_debug",
main = "test_debug.zig",
tags = ["manual"],
)

zig_configure_test(
name = "test_debug",
actual = "_test_debug",
mode = "debug",
)

zig_test(
name = "_test_release_safe",
main = "test_release_safe.zig",
tags = ["manual"],
)

zig_configure_test(
name = "test_release_safe",
actual = "_test_release_safe",
mode = "release_safe",
)
9 changes: 9 additions & 0 deletions e2e/workspace/configure-mode/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(
"{s}\n",
.{std.meta.tagName(builtin.mode)},
) catch unreachable;
}
1 change: 1 addition & 0 deletions e2e/workspace/configure-mode/binary_debug_output.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Debug
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ReleaseSafe
19 changes: 19 additions & 0 deletions e2e/workspace/configure-mode/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-mode/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 = @tagName(builtin.mode), .linkage = .Strong });
}

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

test "mode is Debug" {
try std.testing.expectEqual(std.builtin.Mode.Debug, builtin.mode);
}
6 changes: 6 additions & 0 deletions e2e/workspace/configure-mode/test_release_safe.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const std = @import("std");
const builtin = @import("builtin");

test "mode is ReleaseSafe" {
try std.testing.expectEqual(std.builtin.Mode.ReleaseSafe, builtin.mode);
}
1 change: 1 addition & 0 deletions zig/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ bzl_library(
visibility = ["//visibility:public"],
deps = [
"//zig/private:zig_binary",
"//zig/private:zig_configure",
"//zig/private:zig_library",
"//zig/private:zig_package",
"//zig/private:zig_test",
Expand Down
9 changes: 9 additions & 0 deletions zig/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@ load("//zig/private:zig_binary.bzl", _zig_binary = "zig_binary")
load("//zig/private:zig_library.bzl", _zig_library = "zig_library")
load("//zig/private:zig_package.bzl", _zig_package = "zig_package")
load("//zig/private:zig_test.bzl", _zig_test = "zig_test")
load(
"//zig/private:zig_configure.bzl",
_zig_configure = "zig_configure",
_zig_configure_binary = "zig_configure_binary",
_zig_configure_test = "zig_configure_test",
)

zig_binary = _zig_binary
zig_library = _zig_library
zig_package = _zig_package
zig_test = _zig_test
zig_configure = _zig_configure
zig_configure_binary = _zig_configure_binary
zig_configure_test = _zig_configure_test
11 changes: 11 additions & 0 deletions zig/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ bzl_library(
],
)

bzl_library(
name = "zig_configure",
srcs = ["zig_configure.bzl"],
visibility = ["//zig:__subpackages__"],
deps = [
":settings",
"@bazel_skylib//lib:paths",
],
)

bzl_library(
name = "resolved_toolchain",
srcs = ["resolved_toolchain.bzl"],
Expand Down Expand Up @@ -76,6 +86,7 @@ filegroup(
":toolchains_repo.bzl",
":versions.bzl",
":zig_binary.bzl",
":zig_configure.bzl",
":zig_library.bzl",
":zig_package.bzl",
":zig_test.bzl",
Expand Down
2 changes: 2 additions & 0 deletions zig/private/settings.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ MODE_ARGS = {
"release_fast": ["-O", "ReleaseFast"],
}

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

def _settings_impl(ctx):
args = []

Expand Down
Loading