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
34 changes: 15 additions & 19 deletions zig/private/common/zig_build.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ load("//zig/private/common:zig_lib_dir.bzl", "zig_lib_dir")
load(
"//zig/private/providers:zig_module_info.bzl",
"ZigModuleInfo",
"zig_module_dependencies",
"zig_module_info",
"zig_module_specifications",
)
load(
Expand Down Expand Up @@ -360,17 +360,18 @@ def zig_build_impl(ctx, *, kind):
args = args,
)

direct_inputs.append(ctx.file.main)
direct_inputs.extend(ctx.files.srcs)
direct_inputs.extend(ctx.files.extra_srcs)

bazel_builtin = bazel_builtin_module(ctx)

zig_module_dependencies(
deps = ctx.attr.deps,
extra_deps = [bazel_builtin],
args = args,
zig_version = zigtoolchaininfo.zig_version,
zdeps = []
for dep in ctx.attr.deps:
if ZigModuleInfo in dep:
zdeps.append(dep[ZigModuleInfo])

root_module = zig_module_info(
name = ctx.attr.name,
canonical_name = ctx.label.name,
main = ctx.file.main,
srcs = ctx.files.srcs,
extra_srcs = ctx.files.extra_srcs,
deps = zdeps + [bazel_builtin_module(ctx)],
)

zig_settings(
Expand All @@ -383,19 +384,14 @@ def zig_build_impl(ctx, *, kind):
args = args,
)

args.add(ctx.file.main, format = "-M{}=%s".format(ctx.label.name))

zig_module_specifications(
deps = ctx.attr.deps,
extra_deps = [bazel_builtin],
inputs = transitive_inputs,
root_module = root_module,
args = args,
zig_version = zigtoolchaininfo.zig_version,
)

inputs = depset(
direct = direct_inputs,
transitive = transitive_inputs,
transitive = transitive_inputs + [root_module.transitive_inputs],
order = "preorder",
)

Expand Down
34 changes: 16 additions & 18 deletions zig/private/common/zig_docs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ load("//zig/private/common:zig_cache.bzl", "zig_cache_output")
load("//zig/private/common:zig_lib_dir.bzl", "zig_lib_dir")
load(
"//zig/private/providers:zig_module_info.bzl",
"zig_module_dependencies",
"ZigModuleInfo",
"zig_module_info",
"zig_module_specifications",
)
load(
Expand Down Expand Up @@ -101,28 +102,25 @@ def zig_docs_impl(ctx, *, kind):
data = direct_data,
)

direct_inputs.append(ctx.file.main)
direct_inputs.extend(ctx.files.srcs)
direct_inputs.extend(ctx.files.extra_srcs)
direct_inputs.extend(ctx.files.extra_docs)

bazel_builtin = bazel_builtin_module(ctx)

zig_module_dependencies(
deps = ctx.attr.deps,
extra_deps = [bazel_builtin],
args = args,
zig_version = zigtoolchaininfo.zig_version,
zdeps = []
for dep in ctx.attr.deps:
if ZigModuleInfo in dep:
zdeps.append(dep[ZigModuleInfo])

root_module = zig_module_info(
name = ctx.attr.name,
canonical_name = ctx.label.name,
main = ctx.file.main,
srcs = ctx.files.srcs,
extra_srcs = ctx.files.extra_srcs,
deps = zdeps + [bazel_builtin_module(ctx)],
)

args.add(ctx.file.main, format = "-M{}=%s".format(ctx.label.name))

zig_module_specifications(
deps = ctx.attr.deps,
extra_deps = [bazel_builtin],
inputs = transitive_inputs,
root_module = root_module,
args = args,
zig_version = zigtoolchaininfo.zig_version,
)

zig_settings(
Expand All @@ -137,7 +135,7 @@ def zig_docs_impl(ctx, *, kind):

inputs = depset(
direct = direct_inputs,
transitive = transitive_inputs,
transitive = transitive_inputs + [root_module.transitive_inputs],
order = "preorder",
)

Expand Down
121 changes: 27 additions & 94 deletions zig/private/providers/zig_module_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,24 @@ instead the Zig compiler performs whole program compilation.
FIELDS = {
"name": "string, The import name of the module.",
"canonical_name": "string, The canonical name may differ from the import name via remapping.",
"transitive_args": "depset of struct, All module CLI specifications required when depending on the module, including transitive dependencies, to be rendered.",
"transitive_inputs": "depset of File, All build inputs files required when depending on the module, including transitive dependencies.",
"module_context": "struct, per module compilation context required when depending on the module.",
"transitive_module_contexts": "depset of struct, All compilation context required by direct and transitive dependencies.",
"transitive_inputs": "depset of File, All dependencies required when depending on the module, including transitive dependencies.",
}

ZigModuleInfo = provider(
fields = FIELDS,
doc = DOC,
)

def zig_module_info(*, name, canonical_name, main, srcs, extra_srcs, deps):
def _zig_module_context(canonical_name, main, deps):
return struct(
canonical_name = canonical_name,
main = main.path,
dependency_mappings = tuple([struct(name = dep.name, canonical_name = dep.canonical_name) for dep in deps]),
)

def zig_module_info(*, name, canonical_name, main, srcs = [], extra_srcs = [], deps = []):
"""Create `ZigModuleInfo` for a new Zig module.

Args:
Expand All @@ -36,111 +44,36 @@ def zig_module_info(*, name, canonical_name, main, srcs, extra_srcs, deps):
Returns:
`ZigModuleInfo`
"""
args_transitive = []
srcs_transitive = []

for dep in deps:
args_transitive.append(dep.transitive_args)
srcs_transitive.append(dep.transitive_inputs)

arg_direct = _module_args(
canonical_name = canonical_name,
main = main,
deps = deps,
)
srcs_direct = [main] + srcs + extra_srcs
module_context = _zig_module_context(canonical_name, main, deps)

transitive_args = depset(direct = [arg_direct], transitive = args_transitive)
transitive_inputs = depset(direct = srcs_direct, transitive = srcs_transitive)
module = ZigModuleInfo(
name = name,
canonical_name = canonical_name,
transitive_args = transitive_args,
transitive_inputs = transitive_inputs,
module_context = module_context,
transitive_module_contexts = depset(direct = [dep.module_context for dep in deps], transitive = [dep.transitive_module_contexts for dep in deps], order = "postorder"),
transitive_inputs = depset(direct = [main] + srcs + extra_srcs, transitive = [dep.transitive_inputs for dep in deps], order = "preorder"),
)

return module

def _dep_arg(dep):
if dep.canonical_name != dep.name:
return struct(name = dep.name, canonical_name = dep.canonical_name)
else:
return struct(name = dep.name)

def _module_args(*, canonical_name, main, deps):
return struct(
name = canonical_name,
main = main.path,
deps = tuple([_dep_arg(dep) for dep in deps]),
)

def _render_dep(dep):
dep_spec = dep.name

if hasattr(dep, "canonical_name") and dep.canonical_name != dep.name:
dep_spec += "=" + dep.canonical_name
def _render_per_module_args(module):
args = []
for mapping in module.dependency_mappings:
args.extend(["--dep", "{}={}".format(mapping.name, mapping.canonical_name)])

return dep_spec
args.append("-M{name}={src}".format(name = module.canonical_name, src = module.main))

def _render_args(args):
rendered = []
return args

for dep in args.deps:
rendered.extend(["--dep", _render_dep(dep)])

rendered.extend(["-M{name}={main}".format(
name = args.name,
main = args.main,
)])

return rendered

def zig_module_dependencies(*, zig_version, deps, extra_deps = [], args):
"""Collect flags for the Zig main module to depend on other modules.

Args:
zig_version: string, The version of the Zig SDK.
deps: List of Target, Considers the targets that have a ZigModuleInfo provider.
extra_deps: List of ZigModuleInfo.
args: Args; mutable, Append the needed Zig compiler flags to this object.
"""
_ = zig_version # @unused
deps_args = []

modules = [
dep[ZigModuleInfo]
for dep in deps
if ZigModuleInfo in dep
] + extra_deps

for module in modules:
deps_args.append(_render_dep(module))

args.add_all(deps_args, before_each = "--dep")

def zig_module_specifications(*, zig_version, deps, extra_deps = [], inputs, args):
def zig_module_specifications(*, root_module, args):
"""Collect inputs and flags to build Zig modules.

Args:
zig_version: string, The version of the Zig SDK.
deps: List of Target, Considers the targets that have a ZigModuleInfo provider.
extra_deps: List of ZigModuleInfo.
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.
root_module: ZigModuleInfo; The root module for which to render args.
args: Args; mutable, Append the needed Zig compiler flags to this object.
"""
_ = zig_version # @unused
transitive_args = []

modules = [
dep[ZigModuleInfo]
for dep in deps
if ZigModuleInfo in dep
] + extra_deps

for module in modules:
transitive_args.append(module.transitive_args)
inputs.append(module.transitive_inputs)

render_args = _render_args

args.add_all(depset(transitive = transitive_args), map_each = render_args)
# The first module is the main module.
args.add_all(_render_per_module_args(root_module.module_context))
args.add_all(root_module.transitive_module_contexts, map_each = _render_per_module_args)
Loading
Loading