@@ -29,13 +29,14 @@ def _cpp_component_impl(ctx):
2929 dep_headers = []
3030 dep_libraries = []
3131 dep_includes = []
32-
32+
3333 for dep in ctx .attr .deps :
3434 if CcInfo in dep :
3535 # Use proper CcInfo provider for header and library information
3636 cc_info = dep [CcInfo ]
3737 dep_headers .extend (cc_info .compilation_context .headers .to_list ())
3838 dep_includes .extend (cc_info .compilation_context .includes .to_list ())
39+
3940 # Extract static libraries from linking context
4041 for linker_input in cc_info .linking_context .linker_inputs .to_list ():
4142 for library in linker_input .libraries :
@@ -87,16 +88,22 @@ def _cpp_component_impl(ctx):
8788
8889 # Basic compiler flags for Preview2
8990 compile_args .add ("--target=wasm32-wasip2" )
90- # Compute sysroot path from sysroot_files for external repository compatibility
91+
92+ # Build sysroot path from toolchain repository for external compatibility
9193 if sysroot_files and sysroot_files .files :
92- # Use the directory containing the sysroot files as the sysroot path
93- sysroot_file = sysroot_files .files .to_list ()[0 ]
94- # Extract the repository-relative sysroot path
95- sysroot_path = sysroot_file .path .split ("/sysroot/" )[0 ] + "/sysroot"
94+ # Use sysroot_files to determine the actual sysroot directory
95+ toolchain_file = sysroot_files .files .to_list ()[0 ]
96+
97+ # Extract the sysroot directory by removing the file component
98+ if "/sysroot/" in toolchain_file .path :
99+ # Get everything up to and including /sysroot/
100+ sysroot_base = toolchain_file .path .split ("/sysroot/" )[0 ] + "/sysroot"
101+ sysroot_path = sysroot_base
102+ else :
103+ sysroot_path = sysroot
96104 else :
97- # Fallback to configured path
98105 sysroot_path = sysroot
99-
106+
100107 compile_args .add ("--sysroot=" + sysroot_path )
101108
102109 # Component model definitions
@@ -132,20 +139,30 @@ def _cpp_component_impl(ctx):
132139
133140 # Include directories
134141 compile_args .add ("-I" + work_dir .path )
135-
142+
136143 # Add C++ standard library paths for wasm32-wasip2 target
137144 if ctx .attr .language == "cpp" :
138- compile_args .add ("-I" + sysroot_path + "/include/wasm32-wasip2/c++/v1" )
139- compile_args .add ("-I" + sysroot_path + "/include/c++/v1" )
140-
145+ # WASI SDK stores C++ headers in share/wasi-sysroot, not just sysroot
146+ if "/external/" in sysroot_path :
147+ toolchain_repo = sysroot_path .split ("/sysroot" )[0 ]
148+ wasi_sysroot = toolchain_repo + "/share/wasi-sysroot"
149+ else :
150+ wasi_sysroot = sysroot_path
151+ compile_args .add ("-I" + wasi_sysroot + "/include/wasm32-wasip2/c++/v1" )
152+ compile_args .add ("-I" + wasi_sysroot + "/include/c++/v1" )
153+
154+ # Also add clang's builtin headers
155+ if "/external/" in sysroot_path :
156+ compile_args .add ("-I" + toolchain_repo + "/lib/clang/20/include" )
157+
141158 for include in ctx .attr .includes :
142159 compile_args .add ("-I" + include )
143160
144161 # Add dependency include directories from CcInfo
145162 for include_dir in dep_includes :
146163 if include_dir not in [work_dir .path ] + ctx .attr .includes :
147164 compile_args .add ("-I" + include_dir )
148-
165+
149166 # Add dependency header directories (fallback for non-CcInfo deps)
150167 for dep_hdr in dep_headers :
151168 include_dir = dep_hdr .dirname
@@ -428,8 +445,16 @@ def _cc_component_library_impl(ctx):
428445 # Compile arguments
429446 compile_args = ctx .actions .args ()
430447 compile_args .add ("--target=wasm32-wasip2" )
448+
431449 # Resolve sysroot path dynamically for external repository compatibility
432- sysroot_dir = sysroot_files .files .to_list ()[0 ].dirname if sysroot_files .files else sysroot
450+ if sysroot_files and sysroot_files .files :
451+ toolchain_file = sysroot_files .files .to_list ()[0 ]
452+ if "/sysroot/" in toolchain_file .path :
453+ sysroot_dir = toolchain_file .path .split ("/sysroot/" )[0 ] + "/sysroot"
454+ else :
455+ sysroot_dir = sysroot
456+ else :
457+ sysroot_dir = sysroot
433458 compile_args .add ("--sysroot=" + sysroot_dir )
434459 compile_args .add ("-c" ) # Compile only, don't link
435460
@@ -461,12 +486,22 @@ def _cc_component_library_impl(ctx):
461486 # Include directories
462487 for hdr in ctx .files .hdrs :
463488 compile_args .add ("-I" + hdr .dirname )
464-
489+
465490 # Add C++ standard library paths for wasm32-wasip2 target
466491 if ctx .attr .language == "cpp" :
467- compile_args .add ("-I" + sysroot + "/include/wasm32-wasip2/c++/v1" )
468- compile_args .add ("-I" + sysroot + "/include/c++/v1" )
469-
492+ # WASI SDK stores C++ headers in share/wasi-sysroot, not just sysroot
493+ if "/external/" in sysroot_dir :
494+ toolchain_repo = sysroot_dir .split ("/sysroot" )[0 ]
495+ wasi_sysroot = toolchain_repo + "/share/wasi-sysroot"
496+ else :
497+ wasi_sysroot = sysroot_dir
498+ compile_args .add ("-I" + wasi_sysroot + "/include/wasm32-wasip2/c++/v1" )
499+ compile_args .add ("-I" + wasi_sysroot + "/include/c++/v1" )
500+
501+ # Also add clang's builtin headers
502+ if "/external/" in sysroot_dir :
503+ compile_args .add ("-I" + toolchain_repo + "/lib/clang/20/include" )
504+
470505 for include in ctx .attr .includes :
471506 compile_args .add ("-I" + include )
472507
@@ -514,7 +549,7 @@ def _cc_component_library_impl(ctx):
514549 transitive_headers = []
515550 transitive_libraries = []
516551 transitive_includes = []
517-
552+
518553 for dep in ctx .attr .deps :
519554 if CcInfo in dep :
520555 cc_info = dep [CcInfo ]
@@ -527,7 +562,7 @@ def _cc_component_library_impl(ctx):
527562 headers = depset (ctx .files .hdrs , transitive = transitive_headers ),
528563 includes = depset ([h .dirname for h in ctx .files .hdrs ] + ctx .attr .includes , transitive = [depset (transitive_includes )]),
529564 )
530-
565+
531566 # Create CcInfo provider with compilation context only
532567 # Note: We don't create linking context since we're using custom WASM toolchain
533568 cc_info = CcInfo (
0 commit comments