@@ -25,11 +25,24 @@ def _cpp_component_impl(ctx):
2525 headers = ctx .files .hdrs
2626 wit_file = ctx .file .wit
2727
28- # Collect dependency headers
28+ # Collect dependency headers and libraries using CcInfo provider
2929 dep_headers = []
3030 dep_libraries = []
31+ dep_includes = []
32+
3133 for dep in ctx .attr .deps :
32- if DefaultInfo in dep :
34+ if CcInfo in dep :
35+ # Use proper CcInfo provider for header and library information
36+ cc_info = dep [CcInfo ]
37+ dep_headers .extend (cc_info .compilation_context .headers .to_list ())
38+ dep_includes .extend (cc_info .compilation_context .includes .to_list ())
39+ # Extract static libraries from linking context
40+ for linker_input in cc_info .linking_context .linker_inputs .to_list ():
41+ for library in linker_input .libraries :
42+ if library .static_library :
43+ dep_libraries .append (library .static_library )
44+ elif DefaultInfo in dep :
45+ # Fallback for non-CcInfo dependencies (e.g., legacy rules)
3346 for file in dep [DefaultInfo ].files .to_list ():
3447 if file .extension in ["h" , "hpp" , "hh" , "hxx" ]:
3548 dep_headers .append (file )
@@ -74,7 +87,17 @@ def _cpp_component_impl(ctx):
7487
7588 # Basic compiler flags for Preview2
7689 compile_args .add ("--target=wasm32-wasip2" )
77- compile_args .add ("--sysroot=" + sysroot )
90+ # Compute sysroot path from sysroot_files for external repository compatibility
91+ 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"
96+ else :
97+ # Fallback to configured path
98+ sysroot_path = sysroot
99+
100+ compile_args .add ("--sysroot=" + sysroot_path )
78101
79102 # Component model definitions
80103 compile_args .add ("-D_WASI_EMULATED_PROCESS_CLOCKS" )
@@ -112,16 +135,21 @@ def _cpp_component_impl(ctx):
112135
113136 # Add C++ standard library paths for wasm32-wasip2 target
114137 if ctx .attr .language == "cpp" :
115- compile_args .add ("-I" + sysroot + "/include/wasm32-wasip2/c++/v1" )
116- compile_args .add ("-I" + sysroot + "/include/c++/v1" )
138+ compile_args .add ("-I" + sysroot_path + "/include/wasm32-wasip2/c++/v1" )
139+ compile_args .add ("-I" + sysroot_path + "/include/c++/v1" )
117140
118141 for include in ctx .attr .includes :
119142 compile_args .add ("-I" + include )
120143
121- # Add dependency header directories
144+ # Add dependency include directories from CcInfo
145+ for include_dir in dep_includes :
146+ if include_dir not in [work_dir .path ] + ctx .attr .includes :
147+ compile_args .add ("-I" + include_dir )
148+
149+ # Add dependency header directories (fallback for non-CcInfo deps)
122150 for dep_hdr in dep_headers :
123151 include_dir = dep_hdr .dirname
124- if include_dir not in [work_dir .path ] + ctx .attr .includes :
152+ if include_dir not in [work_dir .path ] + ctx .attr .includes + dep_includes :
125153 compile_args .add ("-I" + include_dir )
126154
127155 # Defines
@@ -400,7 +428,9 @@ def _cc_component_library_impl(ctx):
400428 # Compile arguments
401429 compile_args = ctx .actions .args ()
402430 compile_args .add ("--target=wasm32-wasip2" )
403- compile_args .add ("--sysroot=" + sysroot )
431+ # Resolve sysroot path dynamically for external repository compatibility
432+ sysroot_dir = sysroot_files .files .to_list ()[0 ].dirname if sysroot_files .files else sysroot
433+ compile_args .add ("--sysroot=" + sysroot_dir )
404434 compile_args .add ("-c" ) # Compile only, don't link
405435
406436 # Component model definitions
@@ -480,8 +510,33 @@ def _cc_component_library_impl(ctx):
480510 progress_message = "Creating component library %s" % ctx .label ,
481511 )
482512
513+ # Collect transitive headers and libraries from dependencies
514+ transitive_headers = []
515+ transitive_libraries = []
516+ transitive_includes = []
517+
518+ for dep in ctx .attr .deps :
519+ if CcInfo in dep :
520+ cc_info = dep [CcInfo ]
521+ transitive_headers .append (cc_info .compilation_context .headers )
522+ transitive_includes .extend (cc_info .compilation_context .includes .to_list ())
523+ transitive_libraries .append (cc_info .linking_context .linker_inputs )
524+
525+ # Create compilation context with current headers and transitive headers
526+ compilation_context = cc_common .create_compilation_context (
527+ headers = depset (ctx .files .hdrs , transitive = transitive_headers ),
528+ includes = depset ([h .dirname for h in ctx .files .hdrs ] + ctx .attr .includes , transitive = [depset (transitive_includes )]),
529+ )
530+
531+ # Create CcInfo provider with compilation context only
532+ # Note: We don't create linking context since we're using custom WASM toolchain
533+ cc_info = CcInfo (
534+ compilation_context = compilation_context ,
535+ )
536+
483537 return [
484538 DefaultInfo (files = depset ([library ] + ctx .files .hdrs )),
539+ cc_info ,
485540 OutputGroupInfo (
486541 library = depset ([library ]),
487542 objects = depset (object_files ),
0 commit comments