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
21 changes: 21 additions & 0 deletions docs/rules.md

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

23 changes: 23 additions & 0 deletions e2e/workspace/simple-library/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@rules_zig//zig:defs.bzl", "zig_library")

zig_library(
name = "library",
main = "main.zig",
)

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

build_test(
name = "library_build_test",
targets = [
":library",
":library-symbol",
],
)
7 changes: 7 additions & 0 deletions e2e/workspace/simple-library/main.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const std = @import("std");

export fn sayHello() void {
std.io.getStdOut().writeAll(
"Hello World!\n",
) catch unreachable;
}
1 change: 1 addition & 0 deletions zig/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ bzl_library(
visibility = ["//visibility:public"],
deps = [
"//zig/private:zig_binary",
"//zig/private:zig_library",
"//zig/private:zig_package",
],
)
Expand Down
2 changes: 2 additions & 0 deletions zig/defs.bzl
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"Public API re-exports"

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")

zig_binary = _zig_binary
zig_library = _zig_library
zig_package = _zig_package
38 changes: 22 additions & 16 deletions zig/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,39 +1,45 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

bzl_library(
name = "toolchains_repo",
srcs = ["toolchains_repo.bzl"],
name = "zig_binary",
srcs = ["zig_binary.bzl"],
visibility = ["//zig:__subpackages__"],
deps = [
"//zig/private/common:filetypes",
"//zig/private/common:zig_cache",
"//zig/private/providers:zig_package_info",
],
)

bzl_library(
name = "versions",
srcs = ["versions.bzl"],
name = "zig_package",
srcs = ["zig_package.bzl"],
visibility = ["//zig:__subpackages__"],
deps = [
"//zig/private/common:filetypes",
"//zig/private/providers:zig_package_info",
],
)

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

bzl_library(
name = "zig_package",
srcs = ["zig_package.bzl"],
name = "toolchains_repo",
srcs = ["toolchains_repo.bzl"],
visibility = ["//zig:__subpackages__"],
deps = [
":filetypes",
"//zig/private/providers:zig_package_info",
],
)

bzl_library(
name = "filetypes",
srcs = ["filetypes.bzl"],
name = "versions",
srcs = ["versions.bzl"],
visibility = ["//zig:__subpackages__"],
)
14 changes: 14 additions & 0 deletions zig/private/common/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

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

bzl_library(
name = "filetypes",
srcs = ["filetypes.bzl"],
visibility = ["//zig:__subpackages__"],
)
File renamed without changes.
26 changes: 26 additions & 0 deletions zig/private/common/zig_cache.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Defines utilities to handle the Zig compiler cache."""

load("@bazel_skylib//lib:paths.bzl", "paths")

def zig_cache_output(*, actions, name, outputs, args):
"""Handle the Zig compiler cache.

Declares directory outputs for the local and global Zig compiler cache.
Appends both to the given outputs list, and arguments object.

Args:
actions: `ctx.actions`.
name: String, A unique name to distinguish this cache from others.
outputs: List; mutable, Append the declared outputs to this list.
args: Args; mutable, Append the Zig cache flags to this object.
"""

# TODO[AH] Persist or share at least the global cache somehow.
local_cache = actions.declare_directory(paths.join(".zig-cache", "local", name))
global_cache = actions.declare_directory(paths.join(".zig-cache", "global", name))

outputs.append(local_cache)
outputs.append(global_cache)

args.add_all(["--cache-dir", local_cache.path])
args.add_all(["--global-cache-dir", global_cache.path])
23 changes: 11 additions & 12 deletions zig/private/providers/zig_package_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,17 @@ ZigPackageInfo = provider(
doc = DOC,
)

def add_package_flags(args, package):
"""Generate the Zig compiler flags to depend on the given package.

Generates `--pkg-begin` and `--pkg-end` flags required to build a target
that depends on this package.
def zig_package_dependencies(*, deps, inputs, args):
"""Collect inputs and flags for Zig package dependencies.

Args:
args: The Args object to extend with the required flags.
package: The package to generate flags for.
deps: List of Target, Considers the targets that have a ZigPackageInfo provider.
inputs: List of depset of File; mutable, Append the needed inputs to this list.
args: Args; mutable, Append the needed Zig compiler flags to this object.
"""
args.add_all(package.flags)

def get_package_files(package):
"""Generate a `depset` of the files required to depend on the package."""
return package.all_srcs
for dep in deps:
if not ZigPackageInfo in dep:
continue
package = dep[ZigPackageInfo]
inputs.append(package.all_srcs)
args.add_all(package.flags)
42 changes: 23 additions & 19 deletions zig/private/zig_binary.bzl
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
"""Implementation of the zig_binary rule."""

load("@bazel_skylib//lib:paths.bzl", "paths")
load("//zig/private:filetypes.bzl", "ZIG_SOURCE_EXTENSIONS")
load("//zig/private/common:filetypes.bzl", "ZIG_SOURCE_EXTENSIONS")
load("//zig/private/common:zig_cache.bzl", "zig_cache_output")
load(
"//zig/private/providers:zig_package_info.bzl",
"ZigPackageInfo",
"add_package_flags",
"get_package_files",
"zig_package_dependencies",
)

DOC = """\
Expand All @@ -33,33 +32,38 @@ ATTRS = {
def _zig_binary_impl(ctx):
ziginfo = ctx.toolchains["//zig:toolchain_type"].ziginfo

# TODO[AH] Append `.exe` extension on Windows.
output = ctx.actions.declare_file(ctx.label.name)
outputs = []

local_cache = ctx.actions.declare_directory(paths.join(".zig-cache", "local", ctx.label.name))
global_cache = ctx.actions.declare_directory(paths.join(".zig-cache", "global", ctx.label.name))

direct_inputs = [ctx.file.main] + ctx.files.srcs
direct_inputs = []
transitive_inputs = []

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

# TODO[AH] Append `.exe` extension on Windows.
output = ctx.actions.declare_file(ctx.label.name)
outputs.append(output)
args.add(output, format = "-femit-bin=%s")

direct_inputs.append(ctx.file.main)
direct_inputs.extend(ctx.files.srcs)
args.add(ctx.file.main)

for dep in ctx.attr.deps:
if ZigPackageInfo in dep:
package = dep[ZigPackageInfo]
transitive_inputs.append(get_package_files(package))
add_package_flags(args, package)
zig_package_dependencies(
deps = ctx.attr.deps,
inputs = transitive_inputs,
args = args,
)

# TODO[AH] Persist or share at least the global cache somehow.
args.add_all(["--cache-dir", local_cache.path])
args.add_all(["--global-cache-dir", global_cache.path])
zig_cache_output(
actions = ctx.actions,
name = ctx.label.name,
outputs = outputs,
args = args,
)

ctx.actions.run(
outputs = [output, local_cache, global_cache],
outputs = outputs,
inputs = depset(direct = direct_inputs, transitive = transitive_inputs),
executable = ziginfo.target_tool_path,
tools = ziginfo.tool_files,
Expand Down
88 changes: 88 additions & 0 deletions zig/private/zig_library.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
"""Implementation of the zig_library rule."""

load("//zig/private/common:filetypes.bzl", "ZIG_SOURCE_EXTENSIONS")
load("//zig/private/common:zig_cache.bzl", "zig_cache_output")
load(
"//zig/private/providers:zig_package_info.bzl",
"ZigPackageInfo",
"zig_package_dependencies",
)

DOC = """\
"""

ATTRS = {
"main": attr.label(
allow_single_file = ZIG_SOURCE_EXTENSIONS,
doc = "The main source file.",
mandatory = True,
),
"srcs": attr.label_list(
allow_files = ZIG_SOURCE_EXTENSIONS,
doc = "Other source files required to build the target.",
mandatory = False,
),
"deps": attr.label_list(
doc = "Packages or libraries required to build the target.",
mandatory = False,
providers = [ZigPackageInfo],
),
}

def _zig_library_impl(ctx):
ziginfo = ctx.toolchains["//zig:toolchain_type"].ziginfo

outputs = []

direct_inputs = []
transitive_inputs = []

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

# 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.

direct_inputs.append(ctx.file.main)
direct_inputs.extend(ctx.files.srcs)
args.add(ctx.file.main)

zig_package_dependencies(
deps = ctx.attr.deps,
inputs = transitive_inputs,
args = args,
)

zig_cache_output(
actions = ctx.actions,
name = ctx.label.name,
outputs = outputs,
args = args,
)

ctx.actions.run(
outputs = outputs,
inputs = depset(direct = direct_inputs, transitive = transitive_inputs),
executable = ziginfo.target_tool_path,
tools = ziginfo.tool_files,
arguments = ["build-lib", args],
mnemonic = "ZigBuildLib",
progress_message = "Building %{input} as Zig library %{output}",
execution_requirements = {tag: "" for tag in ctx.attr.tags},
)

default = DefaultInfo(
files = depset([static]),
)

return [default]

zig_library = rule(
_zig_library_impl,
attrs = ATTRS,
doc = DOC,
toolchains = ["//zig:toolchain_type"],
)
2 changes: 1 addition & 1 deletion zig/private/zig_package.bzl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Implementation of the zig_package rule."""

load("//zig/private/common:filetypes.bzl", "ZIG_SOURCE_EXTENSIONS")
load("//zig/private/providers:zig_package_info.bzl", "ZigPackageInfo")
load("//zig/private:filetypes.bzl", "ZIG_SOURCE_EXTENSIONS")

DOC = """\
"""
Expand Down
Loading