Skip to content

Commit c12075c

Browse files
committed
feat: implement AOT embedding and parallel processing, improve codebase quality
- Implement full AOT embedding with custom WASM sections and LEB128 encoding - Add true parallel processing using futures::stream for 10x concurrency - Fix architecture violation: move mock services from test/ to examples/ - Remove hardcoded /tmp paths for cross-platform compatibility - Add comprehensive docstrings to 29 core rule functions
1 parent 6f8e288 commit c12075c

File tree

33 files changed

+3405
-116
lines changed

33 files changed

+3405
-116
lines changed

MODULE.bazel.lock

Lines changed: 520 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cpp/defs.bzl

Lines changed: 113 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,46 @@ load("//rust:transitions.bzl", "wasm_transition")
5454
load("//tools/bazel_helpers:file_ops_actions.bzl", "setup_cpp_workspace_action")
5555

5656
def _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

545584
def _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

631699
def _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"]

docs-site/src/content/docs/composition/wac-oci-integration.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ wac_compose_with_oci(
217217
```python title="BUILD.bazel"
218218
wkg_registry_config(
219219
name = "production_registries",
220-
cache_dir = "/tmp/wkg_cache",
220+
# cache_dir = "/path/to/cache", # Optional: use platform-specific path if needed
221221
default_registry = "github",
222222
enable_mirror_fallback = True,
223223
registries = [

docs-site/src/content/docs/production/publishing.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ wkg_registry_config(
3939
wkg_registry_config(
4040
name = "production_registries",
4141
default_registry = "github",
42-
cache_dir = "/tmp/wkg_cache",
42+
# cache_dir = "/path/to/cache", # Optional: use platform-specific path if needed
4343
enable_mirror_fallback = True,
4444
timeout_seconds = 120,
4545
registries = [

examples/oci_publishing/BUILD.bazel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ wkg_registry_config(
4747
# Enhanced registry configuration with advanced features
4848
wkg_registry_config(
4949
name = "production_registries",
50-
cache_dir = "/tmp/wkg_cache",
50+
# Note: cache_dir is optional. If needed, use a platform-agnostic path:
51+
# - Linux/macOS: Use environment variable or absolute path
52+
# - Windows: Use %TEMP% or absolute path (e.g., "C:/temp/wkg_cache")
53+
# cache_dir = "/tmp/wkg_cache", # Example - remove hardcoded path for cross-platform compatibility
5154
credential_files = [
5255
"docker:docker_config",
5356
"kubernetes:kubernetes",

examples/wac_oci_composition/BUILD.bazel

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ wkg_registry_config(
6060
# Advanced registry configuration with authentication
6161
wkg_registry_config(
6262
name = "production_registries",
63-
cache_dir = "/tmp/wkg_cache",
63+
# Note: cache_dir is optional. If needed, use a platform-agnostic path:
64+
# - Linux/macOS: Use environment variable or absolute path
65+
# - Windows: Use %TEMP% or absolute path (e.g., "C:/temp/wkg_cache")
66+
# cache_dir = "/tmp/wkg_cache", # Example - remove hardcoded path for cross-platform compatibility
6467
default_registry = "github",
6568
enable_mirror_fallback = True,
6669
registries = [
@@ -285,8 +288,8 @@ wac_compose_with_oci(
285288
""",
286289
local_components = {
287290
"frontend": ":frontend_component",
288-
"mock_auth": "//test/integration:service_a_component",
289-
"mock_data": "//test/integration:service_b_component",
291+
"mock_auth": "//examples/wac_oci_composition/mock_services:service_a_component",
292+
"mock_data": "//examples/wac_oci_composition/mock_services:service_b_component",
290293
},
291294
oci_components = {}, # No external dependencies for development
292295
)

examples/wac_oci_composition/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ wkg_registry_config(
165165
```bazel
166166
wkg_registry_config(
167167
name = "production_registries",
168-
cache_dir = "/tmp/wkg_cache",
168+
# cache_dir = "/path/to/cache", # Optional: use platform-specific path if needed
169169
default_registry = "github",
170170
enable_mirror_fallback = True,
171171
registries = [
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""Mock services for WAC composition examples
2+
3+
These mock services provide simple implementations for development and testing
4+
of WAC composition features without requiring external OCI registries.
5+
"""
6+
7+
load("//rust:defs.bzl", "rust_wasm_component_bindgen")
8+
load("//wit:defs.bzl", "wit_library")
9+
10+
package(default_visibility = ["//examples/wac_oci_composition:__pkg__"])
11+
12+
# Service A: Simple storage service
13+
wit_library(
14+
name = "service_a_interface",
15+
package_name = "test:[email protected]",
16+
srcs = ["service_a.wit"],
17+
)
18+
19+
rust_wasm_component_bindgen(
20+
name = "service_a_component",
21+
srcs = ["src/service_a.rs"],
22+
wit = ":service_a_interface",
23+
)
24+
25+
# Service B: User management service that depends on Service A
26+
wit_library(
27+
name = "service_b_interface",
28+
package_name = "test:[email protected]",
29+
srcs = ["service_b.wit"],
30+
deps = [":service_a_interface"],
31+
)
32+
33+
rust_wasm_component_bindgen(
34+
name = "service_b_component",
35+
srcs = ["src/service_b.rs"],
36+
wit = ":service_b_interface",
37+
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package test:service-a@1.0.0;
2+
3+
/// Service A interface for composition testing
4+
interface storage {
5+
store: func(key: string, value: string);
6+
retrieve: func(key: string) -> option<string>;
7+
}
8+
9+
world service-a {
10+
export storage;
11+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package test:service-b@1.0.0;
2+
3+
use test:service-a/storage@1.0.0;
4+
5+
/// Service B interface that depends on Service A
6+
interface api {
7+
create-user: func(name: string, email: string) -> u32;
8+
get-user: func(id: u32) -> option<string>;
9+
}
10+
11+
world service-b {
12+
import storage;
13+
export api;
14+
}

0 commit comments

Comments
 (0)