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/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
build --deleted_packages=e2e/workspace,e2e/workspace/configure-mode,e2e/workspace/configure-threaded,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/configure-mode,e2e/workspace/configure-threaded,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
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.

41 changes: 41 additions & 0 deletions e2e/workspace/linker-script/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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_library", "zig_test")

zig_binary(
name = "binary",
linker_script = "custom.ld",
main = "bin.zig",
)

genrule(
name = "bin_output",
outs = ["bin_output.actual"],
cmd = "$(execpath :binary) > $(OUTS)",
tools = [":binary"],
)

diff_test(
name = "bin_output_test",
file1 = ":bin_output.expected",
file2 = ":bin_output.actual",
)

zig_library(
name = "library",
linker_script = "custom.ld",
main = "lib.zig",
)

build_test(
name = "library_build_test",
targets = [
":library",
],
)

zig_test(
name = "test",
linker_script = "custom.ld",
main = "test.zig",
)
7 changes: 7 additions & 0 deletions e2e/workspace/linker-script/bin.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const std = @import("std");

extern const custom_global_symbol: u8;

pub fn main() !void {
try std.io.getStdOut().writer().print("{d}\n", .{custom_global_symbol});
}
1 change: 1 addition & 0 deletions e2e/workspace/linker-script/bin_output.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
42
21 changes: 21 additions & 0 deletions e2e/workspace/linker-script/custom.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
SECTIONS
{
. = 0x200000 + SIZEOF_HEADERS;
.rodata : { *(.rodata*) }
. += 0x10000;
.text : { *(.text*) }
. += 0x10000;
.got : { *(.got*) }
. += 0x10000;
.bss : { *(.bss*) }
. += 0x10000 - SIZEOF(.bss) % 0x10000;
.tbss : { *(.tbss*) }
. += 0x10000 - SIZEOF(.tbss) % 0x10000;
.data : { *(.data*) }
. += 0x10000;
.custom ALIGN(0x8) :
{
PROVIDE(custom_global_symbol = .);
BYTE(42)
}
}
7 changes: 7 additions & 0 deletions e2e/workspace/linker-script/lib.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const std = @import("std");

extern const custom_global_symbol: u8;

export fn getCustomGlobalSymbol() u8 {
return custom_global_symbol;
}
7 changes: 7 additions & 0 deletions e2e/workspace/linker-script/test.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const std = @import("std");

extern const custom_global_symbol: u8;

test "custom_global_symbol is 42" {
try std.testing.expectEqual(@as(u8, 42), custom_global_symbol);
}
22 changes: 12 additions & 10 deletions zig/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,33 @@ bzl_library(
visibility = ["//zig:__subpackages__"],
deps = [
"//zig/private/common:filetypes",
"//zig/private/common:linker_script",
"//zig/private/common:zig_cache",
"//zig/private/providers:zig_package_info",
"//zig/private/providers:zig_settings_info",
],
)

bzl_library(
name = "zig_library",
srcs = ["zig_library.bzl"],
name = "zig_configure",
srcs = ["zig_configure.bzl"],
visibility = ["//zig:__subpackages__"],
deps = [
"//zig/private/common:filetypes",
"//zig/private/common:zig_cache",
"//zig/private/providers:zig_package_info",
"//zig/private/providers:zig_settings_info",
":settings",
"@bazel_skylib//lib:paths",
],
)

bzl_library(
name = "zig_configure",
srcs = ["zig_configure.bzl"],
name = "zig_library",
srcs = ["zig_library.bzl"],
visibility = ["//zig:__subpackages__"],
deps = [
":settings",
"@bazel_skylib//lib:paths",
"//zig/private/common:filetypes",
"//zig/private/common:linker_script",
"//zig/private/common:zig_cache",
"//zig/private/providers:zig_package_info",
"//zig/private/providers:zig_settings_info",
],
)

Expand Down
7 changes: 7 additions & 0 deletions zig/private/common/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@ bzl_library(
visibility = ["//zig:__subpackages__"],
)

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

# Execute `bazel run //util:update_filegroups` to update this target.
filegroup(
name = "all_files",
srcs = [
":BUILD.bazel",
":filetypes.bzl",
":linker_script.bzl",
":zig_cache.bzl",
],
visibility = ["//zig/private:__pkg__"],
Expand Down
19 changes: 19 additions & 0 deletions zig/private/common/linker_script.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""Handle custom linker scripts."""

def zig_linker_script(*, linker_script, inputs, args):
"""Handle custom linker scripts.

Sets the appropriate command-line flags for the Zig compiler to configure a
custom linker script, if provided.

Args:
linker_script: optional; Label, The linker script attribute.
inputs: List; mutable, Append the linker script inputs to this collection.
args: Args; mutable, Append the Zig command-line flags to this object.
"""

if linker_script == None:
return

inputs.append(linker_script)
args.add_all(["-T", linker_script])
12 changes: 12 additions & 0 deletions zig/private/zig_binary.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Implementation of the zig_binary rule."""

load("//zig/private/common:filetypes.bzl", "ZIG_SOURCE_EXTENSIONS")
load("//zig/private/common:linker_script.bzl", "zig_linker_script")
load("//zig/private/common:zig_cache.bzl", "zig_cache_output")
load(
"//zig/private/providers:zig_package_info.bzl",
Expand Down Expand Up @@ -32,6 +33,11 @@ ATTRS = {
mandatory = False,
providers = [ZigPackageInfo],
),
"linker_script": attr.label(
doc = "Custom linker script for the target.",
allow_single_file = True,
mandatory = False,
),
"_settings": attr.label(
default = "//zig/settings",
doc = "Zig build settings.",
Expand Down Expand Up @@ -65,6 +71,12 @@ def _zig_binary_impl(ctx):
args = args,
)

zig_linker_script(
linker_script = ctx.file.linker_script,
inputs = direct_inputs,
args = args,
)

zig_cache_output(
actions = ctx.actions,
name = ctx.label.name,
Expand Down
12 changes: 12 additions & 0 deletions zig/private/zig_library.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Implementation of the zig_library rule."""

load("//zig/private/common:filetypes.bzl", "ZIG_SOURCE_EXTENSIONS")
load("//zig/private/common:linker_script.bzl", "zig_linker_script")
load("//zig/private/common:zig_cache.bzl", "zig_cache_output")
load(
"//zig/private/providers:zig_package_info.bzl",
Expand Down Expand Up @@ -32,6 +33,11 @@ ATTRS = {
mandatory = False,
providers = [ZigPackageInfo],
),
"linker_script": attr.label(
doc = "Custom linker script for the target.",
allow_single_file = True,
mandatory = False,
),
"_settings": attr.label(
default = "//zig/settings",
doc = "Zig build settings.",
Expand Down Expand Up @@ -66,6 +72,12 @@ def _zig_library_impl(ctx):
args = args,
)

zig_linker_script(
linker_script = ctx.file.linker_script,
inputs = direct_inputs,
args = args,
)

zig_cache_output(
actions = ctx.actions,
name = ctx.label.name,
Expand Down
12 changes: 12 additions & 0 deletions zig/private/zig_test.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Implementation of the zig_test rule."""

load("//zig/private/common:filetypes.bzl", "ZIG_SOURCE_EXTENSIONS")
load("//zig/private/common:linker_script.bzl", "zig_linker_script")
load("//zig/private/common:zig_cache.bzl", "zig_cache_output")
load(
"//zig/private/providers:zig_package_info.bzl",
Expand Down Expand Up @@ -32,6 +33,11 @@ ATTRS = {
mandatory = False,
providers = [ZigPackageInfo],
),
"linker_script": attr.label(
doc = "Custom linker script for the target.",
allow_single_file = True,
mandatory = False,
),
"_settings": attr.label(
default = "//zig/settings",
doc = "Zig build settings.",
Expand Down Expand Up @@ -65,6 +71,12 @@ def _zig_test_impl(ctx):
args = args,
)

zig_linker_script(
linker_script = ctx.file.linker_script,
inputs = direct_inputs,
args = args,
)

zig_cache_output(
actions = ctx.actions,
name = ctx.label.name,
Expand Down