Skip to content

Commit 87f660c

Browse files
committed
fix: resolve C++ component compilation and file operations issues
- Fix file_ops_actions.bzl to properly copy source files to workspace directory - Resolve C++ compilation issue by using clang instead of clang++ for WebAssembly - Add dependency library linking support for C++ components - Both C and C++ components now build successfully with proper file operations Fixes #21: C++ component file operations and compilation failures
1 parent 1bf3871 commit 87f660c

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ jobs:
100100
bazel build -- //... \
101101
-//examples/world_export/... \
102102
-//examples/multi_profile/... \
103-
-//examples/cpp_component/... \
104103
-//examples/js_component/... \
105104
-//examples/wac_remote_compose/... \
106105
-//examples/wac_oci_composition/... \
@@ -199,7 +198,6 @@ jobs:
199198
bazel build -- //... \
200199
-//examples/world_export/... \
201200
-//examples/multi_profile/... \
202-
-//examples/cpp_component/... \
203201
-//examples/js_component/... \
204202
-//examples/wac_remote_compose/... \
205203
-//examples/wac_oci_composition/... \

cpp/defs.bzl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ def _cpp_component_impl(ctx):
99

1010
# Get C/C++ toolchain
1111
cpp_toolchain = ctx.toolchains["@rules_wasm_component//toolchains:cpp_component_toolchain_type"]
12-
clang = cpp_toolchain.clang if ctx.attr.language == "c" else cpp_toolchain.clang_cpp
12+
# Use clang for both C and C++ compilation to avoid clang++ preprocessor issues
13+
clang = cpp_toolchain.clang # if ctx.attr.language == "c" else cpp_toolchain.clang_cpp
1314
wit_bindgen = cpp_toolchain.wit_bindgen
1415
wasm_tools = cpp_toolchain.wasm_tools
1516
sysroot = cpp_toolchain.sysroot
@@ -130,10 +131,14 @@ def _cpp_component_impl(ctx):
130131
for src in sources:
131132
compile_args.add(work_dir.path + "/" + src.basename)
132133

134+
# Add dependency libraries for linking
135+
for lib in dep_libraries:
136+
compile_args.add(lib.path)
137+
133138
ctx.actions.run(
134139
executable = clang,
135140
arguments = [compile_args],
136-
inputs = [work_dir, sysroot],
141+
inputs = [work_dir, sysroot] + dep_libraries,
137142
outputs = [wasm_binary],
138143
mnemonic = "CompileCppWasm",
139144
progress_message = "Compiling C/C++ to WASM for %s" % ctx.label,

tools/bazel_helpers/file_ops_actions.bzl

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,36 @@ def prepare_workspace_action(ctx, config):
182182
# For now, use a simple approach: just create the directory and copy files directly
183183
# This avoids the complex JSON parsing issues while still providing the functionality
184184

185-
# Create the workspace directory
186-
ctx.actions.run(
187-
executable = file_ops_component,
188-
arguments = ["create_directory", "--path", workspace_dir.path],
189-
inputs = [],
185+
# Create workspace directory and copy all files in a single action
186+
# Collect all input files
187+
all_inputs = []
188+
commands = ["mkdir -p {}".format(workspace_dir.path)]
189+
190+
# Add sources
191+
for source_info in config.get("sources", []):
192+
src_file = source_info["source"]
193+
dest_name = source_info.get("destination") or src_file.basename
194+
all_inputs.append(src_file)
195+
commands.append("cp {} {}/{}".format(src_file.path, workspace_dir.path, dest_name))
196+
197+
# Add headers
198+
for header_info in config.get("headers", []):
199+
hdr_file = header_info["source"]
200+
dest_name = header_info.get("destination") or hdr_file.basename
201+
all_inputs.append(hdr_file)
202+
commands.append("cp {} {}/{}".format(hdr_file.path, workspace_dir.path, dest_name))
203+
204+
# Add dependencies
205+
for dep_info in config.get("dependencies", []):
206+
dep_file = dep_info["source"]
207+
dest_name = dep_info.get("destination") or dep_file.basename
208+
all_inputs.append(dep_file)
209+
commands.append("cp {} {}/{}".format(dep_file.path, workspace_dir.path, dest_name))
210+
211+
# Execute workspace preparation in single action
212+
ctx.actions.run_shell(
213+
command = " && ".join(commands),
214+
inputs = all_inputs,
190215
outputs = [workspace_dir],
191216
mnemonic = "PrepareWorkspace",
192217
progress_message = "Preparing {} workspace for {}".format(

0 commit comments

Comments
 (0)