Skip to content

Commit 74c83a8

Browse files
committed
refactor: enhance C++ component architecture and fix SIMD integration
Modernize C++ WebAssembly component implementation with improved architecture, proper exception handling, and cross-platform SIMD support. C++ Architecture Improvements: - Make exception handling configurable instead of hardcoded -fno-exceptions - Restructure calculator example with proper header separation - Add comprehensive math utilities with cross-platform compatibility - Implement proper SIMD abstraction for WebAssembly and native targets SIMD Integration Fixes: - Replace incorrect WebAssembly SIMD intrinsics with proper wasm_simd128.h APIs - Add platform detection for optimal SIMD instruction selection - Implement fallback mechanisms for unsupported SIMD operations - Fix image processing algorithms with correct WebAssembly SIMD syntax Build System Enhancements: - Update toolchain to support configurable compiler flags - Remove hardcoded assumptions about exception handling - Add proper cross-platform compilation support - Maintain compatibility with existing build configurations These changes establish a robust foundation for C++ WebAssembly components while maintaining performance through proper SIMD utilization and flexible build configuration options.
1 parent 638fd53 commit 74c83a8

File tree

8 files changed

+217
-51
lines changed

8 files changed

+217
-51
lines changed

cpp/defs.bzl

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,12 @@ echo "Prepared C/C++ component sources in $WORK_DIR"
140140

141141
# C++ specific flags
142142
if ctx.attr.language == "cpp":
143-
compile_args.add("-fno-exceptions")
143+
if ctx.attr.enable_exceptions:
144+
# Enable exceptions if specifically requested
145+
pass
146+
else:
147+
compile_args.add("-fno-exceptions")
148+
144149
if ctx.attr.enable_rtti:
145150
# Only enable RTTI if specifically requested
146151
pass
@@ -154,6 +159,12 @@ echo "Prepared C/C++ component sources in $WORK_DIR"
154159
compile_args.add("-I" + work_dir.path)
155160
for include in ctx.attr.includes:
156161
compile_args.add("-I" + include)
162+
163+
# Add dependency header directories
164+
for dep_hdr in dep_headers:
165+
include_dir = dep_hdr.dirname
166+
if include_dir not in [work_dir.path] + ctx.attr.includes:
167+
compile_args.add("-I" + include_dir)
157168

158169
# Defines
159170
for define in ctx.attr.defines:
@@ -269,6 +280,10 @@ cpp_component = rule(
269280
default = False,
270281
doc = "Enable C++ RTTI (not recommended for components)",
271282
),
283+
"enable_exceptions": attr.bool(
284+
default = False,
285+
doc = "Enable C++ exceptions (increases binary size)",
286+
),
272287
},
273288
toolchains = ["@rules_wasm_component//toolchains:cpp_component_toolchain_type"],
274289
doc = """
@@ -422,7 +437,12 @@ def _cc_component_library_impl(ctx):
422437

423438
# C++ specific flags
424439
if ctx.attr.language == "cpp":
425-
compile_args.add("-fno-exceptions")
440+
if ctx.attr.enable_exceptions:
441+
# Enable exceptions if specifically requested
442+
pass
443+
else:
444+
compile_args.add("-fno-exceptions")
445+
426446
compile_args.add("-fno-rtti")
427447
if ctx.attr.cxx_std:
428448
compile_args.add("-std=" + ctx.attr.cxx_std)
@@ -474,10 +494,11 @@ def _cc_component_library_impl(ctx):
474494
)
475495

476496
return [
477-
DefaultInfo(files = depset([library])),
497+
DefaultInfo(files = depset([library] + ctx.files.hdrs)),
478498
OutputGroupInfo(
479499
library = depset([library]),
480500
objects = depset(object_files),
501+
headers = depset(ctx.files.hdrs),
481502
),
482503
]
483504

@@ -518,6 +539,10 @@ cc_component_library = rule(
518539
"cxx_std": attr.string(
519540
doc = "C++ standard (e.g., c++17, c++20, c++23)",
520541
),
542+
"enable_exceptions": attr.bool(
543+
default = False,
544+
doc = "Enable C++ exceptions (increases binary size)",
545+
),
521546
},
522547
toolchains = ["@rules_wasm_component//toolchains:cpp_component_toolchain_type"],
523548
doc = """

examples/cpp_component/calculator/BUILD.bazel

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ cc_component_library(
1818
srcs = ["src/math_utils.cpp"],
1919
hdrs = ["src/math_utils.h"],
2020
cxx_std = "c++20",
21+
enable_exceptions = True,
2122
language = "cpp",
2223
optimize = True,
2324
)
@@ -30,9 +31,10 @@ cpp_component(
3031
"src/calculator.cpp",
3132
],
3233
hdrs = [
33-
"src/calculator.h",
34+
"src/calculator_impl.h",
3435
],
3536
cxx_std = "c++20",
37+
enable_exceptions = True,
3638
language = "cpp",
3739
optimize = True,
3840
wit = "wit/calculator.wit",

examples/cpp_component/calculator/src/calculator.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#include "calculator.h"
2-
#include "math_utils.h"
3-
#include <sstream>
1+
#include "calculator_impl.h"
42
#include <algorithm>
3+
#include <limits>
4+
#include <sstream>
55

66
namespace calculator {
77

examples/cpp_component/calculator/src/calculator.h renamed to examples/cpp_component/calculator/src/calculator_impl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#pragma once
22

33
#include "math_utils.h"
4-
#include <vector>
4+
#include <optional>
55
#include <string>
6+
#include <vector>
67

78
// Generated WIT bindings will be included here
89
// #include "calculator_bindings.h"

examples/cpp_component/calculator/src/math_utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#pragma once
22

33
#include <cmath>
4+
#include <functional>
45
#include <optional>
6+
#include <stdexcept>
57
#include <string>
68
#include <vector>
79

0 commit comments

Comments
 (0)