@@ -54,7 +54,46 @@ load("//rust:transitions.bzl", "wasm_transition")
5454load ("//tools/bazel_helpers:file_ops_actions.bzl" , "setup_cpp_workspace_action" )
5555
5656def _cpp_component_impl (ctx ):
57- """Implementation of cpp_component rule"""
57+ """Implementation of cpp_component rule for C/C++ WebAssembly components.
58+
59+ Compiles C/C++ source code into a WebAssembly component using WASI SDK with
60+ native Preview2 support, WIT binding generation, and component model integration.
61+
62+ Args:
63+ ctx: The rule context containing:
64+ - ctx.files.srcs: C/C++ source files to compile
65+ - ctx.files.hdrs: Header files for the component
66+ - ctx.attr.deps: Dependencies (cc_component_library and external libraries)
67+ - ctx.file.wit: WIT interface definition file
68+ - ctx.attr.world: WIT world to target
69+ - ctx.attr.language: Either "c" or "cpp"
70+ - ctx.attr.cxx_std: C++ standard (c++17/20/23)
71+ - ctx.attr.enable_exceptions: Enable C++ exception handling
72+ - ctx.attr.optimize: Enable optimizations (-O3, -flto)
73+
74+ Returns:
75+ List of providers:
76+ - WasmComponentInfo: Component metadata with language and toolchain info
77+ - DefaultInfo: Component .wasm file and validation logs
78+ - OutputGroupInfo: Organized outputs (bindings, wasm_module, validation)
79+
80+ The implementation follows these steps:
81+ 1. Generate C/C++ bindings from WIT using wit-bindgen
82+ 2. Set up compilation workspace with proper header staging
83+ 3. Compile WIT bindings separately (C compilation to avoid C++ flags)
84+ 4. Compile application sources with dependency headers and includes
85+ 5. Link everything together with C++ standard library (if needed)
86+ 6. Embed WIT metadata and create component using wasm-tools
87+ 7. Optionally validate the component against WIT specification
88+
89+ Key features:
90+ - Cross-package header dependency resolution using CcInfo
91+ - Proper staging of local vs external headers
92+ - Support for modern C++ standards (C++17/20/23)
93+ - Exception handling support (increases binary size)
94+ - LTO optimization for size reduction
95+ - WASI SDK Preview2 native compilation
96+ """
5897
5998 # Get C/C++ toolchain
6099 cpp_toolchain = ctx .toolchains ["@rules_wasm_component//toolchains:cpp_component_toolchain_type" ]
@@ -543,7 +582,36 @@ cpp_component = rule(
543582)
544583
545584def _cpp_wit_bindgen_impl (ctx ):
546- """Implementation of cpp_wit_bindgen rule"""
585+ """Implementation of cpp_wit_bindgen rule for standalone WIT binding generation.
586+
587+ Generates C/C++ bindings from WIT interface definitions without building
588+ a complete component. Useful for creating reusable binding libraries.
589+
590+ Args:
591+ ctx: The rule context containing:
592+ - ctx.file.wit: WIT interface definition file
593+ - ctx.attr.world: WIT world to generate bindings for
594+ - ctx.attr.stubs_only: Generate only stub functions
595+ - ctx.attr.string_encoding: String encoding (utf8/utf16/compact-utf16)
596+
597+ Returns:
598+ List of providers:
599+ - DefaultInfo: Generated bindings directory
600+ - OutputGroupInfo: Organized output (bindings group)
601+
602+ Generated files include:
603+ - <world>.h: Header file with interface declarations
604+ - <world>.c: Implementation file with binding code
605+ - <world>_component_type.o: Pre-compiled component type object
606+
607+ Example:
608+ cpp_wit_bindgen(
609+ name = "http_bindings",
610+ wit = "http.wit",
611+ world = "http-handler",
612+ string_encoding = "utf8",
613+ )
614+ """
547615
548616 # Get C/C++ toolchain
549617 cpp_toolchain = ctx .toolchains ["@rules_wasm_component//toolchains:cpp_component_toolchain_type" ]
@@ -629,7 +697,49 @@ cpp_wit_bindgen = rule(
629697)
630698
631699def _cc_component_library_impl (ctx ):
632- """Implementation of cc_component_library rule"""
700+ """Implementation of cc_component_library rule for reusable C/C++ component libraries.
701+
702+ Compiles C/C++ source files into a static library (.a) that can be linked
703+ into WebAssembly components. Provides proper header staging and dependency
704+ propagation through CcInfo provider.
705+
706+ Args:
707+ ctx: The rule context containing:
708+ - ctx.files.srcs: C/C++ source files to compile
709+ - ctx.files.hdrs: Public header files
710+ - ctx.attr.deps: Dependencies (other cc_component_library targets)
711+ - ctx.attr.language: Either "c" or "cpp"
712+ - ctx.attr.cxx_std: C++ standard for compilation
713+ - ctx.attr.optimize: Enable optimizations
714+ - ctx.attr.includes: Additional include directories
715+
716+ Returns:
717+ List of providers:
718+ - DefaultInfo: Static library file and public headers
719+ - CcInfo: Compilation and linking contexts for transitive dependencies
720+ - OutputGroupInfo: Organized outputs (library, objects, headers)
721+
722+ The implementation:
723+ 1. Sets up workspace with proper cross-package header staging
724+ 2. Compiles each source file to object file (.o)
725+ 3. Creates static library from object files using llvm-ar
726+ 4. Builds CcInfo with:
727+ - Compilation context (headers + include paths)
728+ - Linking context (static library for linking)
729+ 5. Propagates transitive dependencies correctly
730+
731+ CcInfo Provider Details:
732+ compilation_context:
733+ - headers: All headers (direct + transitive)
734+ - includes: Include paths for compiler
735+ linking_context:
736+ - linker_inputs: Static library for final linking
737+
738+ Critical for cross-package dependencies:
739+ - Stages local headers for relative includes
740+ - Uses original paths for external library headers
741+ - Propagates include paths transitively
742+ """
633743
634744 # Get C/C++ toolchain
635745 cpp_toolchain = ctx .toolchains ["@rules_wasm_component//toolchains:cpp_component_toolchain_type" ]
0 commit comments