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
5 changes: 3 additions & 2 deletions zig/private/common/translate_c.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
load("//zig/private/common:escape_label.bzl", "escape_label", "escape_label_str")
load("//zig/private/providers:zig_module_info.bzl", "zig_module_info")

def zig_translate_c(*, ctx, name, zigtoolchaininfo, global_args, cc_infos, output_prefix = ""):
def zig_translate_c(*, ctx, name, canonical_name = None, zigtoolchaininfo, global_args, cc_infos, output_prefix = ""):
"""Handle translate-c build action.

Sets the appropriate command-line flags for the Zig compiler to expose
Expand All @@ -15,6 +15,7 @@ def zig_translate_c(*, ctx, name, zigtoolchaininfo, global_args, cc_infos, outpu
Args:
ctx: Context object.
name: String, the name of the resulting Zig module.
canonical_name: String or None, optional canonical name override for the resulting Zig module.
zigtoolchaininfo: ZigToolchainInfo.
global_args: Args; mutable, Append the global Zig command-line flags to this object.
cc_infos: List of CcInfo, The CcInfo providers for the C dependencies.
Expand Down Expand Up @@ -93,7 +94,7 @@ def zig_translate_c(*, ctx, name, zigtoolchaininfo, global_args, cc_infos, outpu
# To avoid collisions, we need to escape both label and name,
# joined using a separator that cannot appear in escapted text.
# (here "__" is used as separator, since "_" is escaped as "_U").
canonical_name = "{}__{}".format(escape_label(label = ctx.label), escape_label_str(name)),
canonical_name = canonical_name if canonical_name else "{}__{}".format(escape_label(label = ctx.label), escape_label_str(name)),
main = zig_out,
cdeps = [cc_info],
)
1 change: 1 addition & 0 deletions zig/private/common/zig_build.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ def zig_build_impl(ctx, *, kind):
c_module = zig_translate_c(
ctx = ctx,
name = "c",
canonical_name = "c",
zigtoolchaininfo = zigtoolchaininfo,
global_args = global_args,
cc_infos = [root_module.cc_info],
Expand Down
1 change: 1 addition & 0 deletions zig/private/common/zig_docs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ def zig_docs_impl(ctx, *, kind):
c_module = zig_translate_c(
ctx = ctx,
name = "c",
canonical_name = "c",
zigtoolchaininfo = zigtoolchaininfo,
global_args = global_args,
cc_infos = [root_module.cc_info],
Expand Down
4 changes: 2 additions & 2 deletions zig/private/providers/zig_module_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,5 @@ def zig_module_specifications(*, root_module, args, c_module = None):
args.add_all(root_module.transitive_module_contexts, map_each = _render_per_module_args)

if c_module:
# Global C module has a predefined canonical name.
args.add(c_module.module_context.main, format = "-Mc=%s")
args.add_all(_render_per_module_args(c_module.module_context))
args.add_all(c_module.transitive_module_contexts, map_each = _render_per_module_args)
5 changes: 4 additions & 1 deletion zig/tests/module_info_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,10 @@ def _write_simple_module_with_global_c_expected_specs_args_impl(ctx):
src = mods["data_global_c"].module_context.main,
)])
expected.extend(bazel_builtins["data_global_c"].mod_flags)
expected.extend(["'-Mc={}'".format(mods["data_c_zig"].module_context.main)])
expected.extend(["'-M{name}={src}'".format(
name = mods["data_c_zig"].canonical_name,
src = mods["data_c_zig"].module_context.main,
)])

ctx.actions.write(
output = ctx.outputs.out,
Expand Down
Loading