Skip to content

feat: add support for translate-c#517

Merged
aherrmann merged 41 commits intoaherrmann:mainfrom
cerisier:cerisier/translate_c
Sep 16, 2025
Merged

feat: add support for translate-c#517
aherrmann merged 41 commits intoaherrmann:mainfrom
cerisier:cerisier/translate_c

Conversation

@cerisier
Copy link
Contributor

@cerisier cerisier commented Sep 11, 2025

This PR addresses #515

It supports:

  • Local translate-c modules.
  • 1 Global translate-c module from all direct cdeps excluding those who were translate-c in a local module.
  • cImport excluding those who were translate-c in a local module.

The important files to review are:

  • zig_module_info.bzl
  • translate_c.bzl
  • zig_build.bzl

Some explanation of changes:

  • Since translate-c is an additional action, I had to split args between global_args and command specific args.
  • CcInfo are merged, losing compilation_context for CcInfo that are going to be local modules (so that they are not in the global C module, and so that they are not @cImportable)
  • Local C modules are regular ZigModuleInfo and added as such (args are computed just like any other regular zig dep)
  • Global C module dependency are added to the module if that module has a direct c dependency.
  • Global C module has a fixed name (--dep c) because it is referenced as a dependency before it can be defined.
  • A transitive dirty flag (has_transitive_cdeps) is used in order to know if the global C module should be defined (otherwise zig complains that it is defined but not used)

@cerisier
Copy link
Contributor Author

Once approved, I would recommend a squash and merge.

@cerisier cerisier force-pushed the cerisier/translate_c branch from d1d1998 to 8820c70 Compare September 12, 2025 12:16
Copy link
Owner

@aherrmann aherrmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! That looks like it was a lot of work!
This largely looks good to me. I've left a few comments and questions on some of the details.

direct_inputs = []
transitive_inputs = []

solib_parents = []
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tested that zig docs targets still work for Zig modules with C deps after these changes?
(Note CI currently doesn't test this as of #448, see also #273).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do bazel test //... in both root and e2e/workspace and it works. I've had to fix a few things already so should be good. Unless it is tested otherwise ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That tests Zig docs generation for targets without C deps.
But, here's a failure to generate Zig docs I found with this changeset:

$ (cd e2e/workspace; bazel build //translate-c/transitive-cc-library-zig-binary:binary --output_groups=zig_docs)

To turn that into a proper test you can use filegroup(..., output_group = "zig_docs") and tag it as "zig-docs".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, let me address that right now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. There was an issue of duplicate output from zig_translate_c since we copy the rule impl almost line by line for zig_docs. I added a prefix argument for generated file paths in zig_translate_c.

However, I was thinking that maybe we should have this baked in the zig_build_impl along with a BuildSetting to conditionally -femit-docs. WDYT ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I saw that. I think my choice to have separate zig_build_impl and zig_docs_impl functions as we have them today and effectively duplicate part of the implementation was not a good choice. It always seemed a little off to me, but wasn't too much of a problem, but now with the duplicate action there's clearly an issue.

I've contemplated the -femit-docs path. A downside is that the effort to compile and link would be duplicated when switching between docs enabled and disabled. Another option is to merge zig_docs_impl into zig_build_impl, re-use common actions (like the c module) and common flags, and only distinguish what's not commonly shared. This needs a bit of care as well though, as it's easy to accidentally conflate what belongs into which action.

@aherrmann aherrmann changed the title Add support for translate-c feat: add support for translate-c Sep 15, 2025
Comment on lines +78 to +82
# Only forward the linking context since compilation_context is now handled
# by Zig through the generated _c.zig.
cc_info = CcInfo(
linking_context = cc_common.merge_linking_contexts(linking_contexts = [cc_info.linking_context for cc_info in cc_infos]),
)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I mean is that above in that function we already have a call to

cc_info = cc_common.merge_cc_infos(direct_cc_infos = cc_infos)

So, instead merging the linking contexts once again here, we can just pluck the merged one out of that object.

@cerisier
Copy link
Contributor Author

@aherrmann all fixed. Thanks for pointing them out !

@aherrmann
Copy link
Owner

@cerisier Lightning fast! Thanks. Note there's also a comment about the docs #517 (comment)

@cerisier
Copy link
Contributor Author

@cerisier Lightning fast! Thanks. Note there's also a comment about the docs #517 (comment)

Addressed

Copy link
Owner

@aherrmann aherrmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, that looks good! And thank you for addressing all the comments!

@aherrmann aherrmann merged commit 4eac60c into aherrmann:main Sep 16, 2025
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants