You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix: implement libs attribute for C++ nostdlib custom library linking
- Added libs attribute to cpp_component and cc_component_library rules
- Fixed nostdlib limitation where no custom libraries could be specified
- Implemented smart linking logic: nostdlib=true uses only libs, nostdlib=false adds libs to standard libraries
- Added comprehensive minimal_nostdlib example with three component variants
- Created tests for nostdlib functionality and WASM validation
- Supports both library names ('m') and direct linker flags ('-lm')
- Enables minimal WebAssembly components with precise library control
Technical changes:
- cpp/defs.bzl: Enhanced library linking logic (lines 325-347)
- Added libs string_list attribute to both component rules
- examples/cpp_component/minimal_nostdlib/: Complete working examples
- test/cpp/BUILD.bazel: Added nostdlib validation tests
Root cause resolved: Previously nostdlib=true provided no way to link specific libraries, making the feature unusable for minimal components requiring math or system libraries.
# When nostdlib is enabled, only link explicitly specified libraries
328
+
forlibinctx.attr.libs:
329
+
iflib.startswith("-"):
330
+
compile_args.add(lib) # Direct linker flag (e.g., "-lm", "-ldl")
331
+
else:
332
+
compile_args.add("-l"+lib) # Library name (e.g., "m" -> "-lm")
333
+
else:
334
+
# Standard library linking for C++ language
335
+
ifctx.attr.language=="cpp":
336
+
compile_args.add("-lc++")
337
+
compile_args.add("-lc++abi")
329
338
330
-
# Add exception handling support if enabled
331
-
# Note: Exception handling symbols are typically in libc++abi which we already link
339
+
# Add exception handling support if enabled
340
+
# Note: Exception handling symbols are typically in libc++abi which we already link
341
+
342
+
# Add any additional libraries specified by user
343
+
forlibinctx.attr.libs:
344
+
iflib.startswith("-"):
345
+
compile_args.add(lib) # Direct linker flag
346
+
else:
347
+
compile_args.add("-l"+lib) # Library name
332
348
333
349
# Add dependency libraries for linking
334
350
forlibindep_libraries:
@@ -493,6 +509,10 @@ cpp_component = rule(
493
509
default=False,
494
510
doc="Disable standard library linking to create minimal components that match WIT specifications exactly",
495
511
),
512
+
"libs": attr.string_list(
513
+
default= [],
514
+
doc="Libraries to link. When nostdlib=True, only these libraries are linked. When nostdlib=False, these are added to standard libraries. Examples: ['m', 'dl'] or ['-lm', '-ldl']",
515
+
),
496
516
"validate_wit": attr.bool(
497
517
default=False,
498
518
doc="Validate that the component exports match the WIT specification",
@@ -888,6 +908,10 @@ cc_component_library = rule(
888
908
default=False,
889
909
doc="Disable standard library linking to create minimal components that match WIT specifications exactly",
890
910
),
911
+
"libs": attr.string_list(
912
+
default= [],
913
+
doc="Libraries to link. When nostdlib=True, only these libraries are linked. When nostdlib=False, these are added to standard libraries. Examples: ['m', 'dl'] or ['-lm', '-ldl']",
This example demonstrates how to use the `nostdlib` feature in C++ WebAssembly components with custom library linking.
4
+
5
+
## Problem Solved
6
+
7
+
Previously, when `nostdlib = True` was used in `cpp_component`, you couldn't specify which libraries to link, making the feature unusable for minimal components that need specific system libraries.
8
+
9
+
## New `libs` Attribute
10
+
11
+
The `libs` attribute allows you to specify exactly which libraries to link:
12
+
13
+
```bazel
14
+
cpp_component(
15
+
name="minimal_component",
16
+
srcs= ["minimal.cpp"],
17
+
wit="minimal.wit",
18
+
nostdlib=True, # Disable standard library
19
+
libs= ["m", "c"], # Link only math and basic C libraries
0 commit comments