Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

118 changes: 104 additions & 14 deletions cpp/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,25 @@ def _cpp_component_impl(ctx):
headers = ctx.files.hdrs
wit_file = ctx.file.wit

# Collect dependency headers
# Collect dependency headers and libraries using CcInfo provider
dep_headers = []
dep_libraries = []
dep_includes = []

for dep in ctx.attr.deps:
if DefaultInfo in dep:
if CcInfo in dep:
# Use proper CcInfo provider for header and library information
cc_info = dep[CcInfo]
dep_headers.extend(cc_info.compilation_context.headers.to_list())
dep_includes.extend(cc_info.compilation_context.includes.to_list())

# Extract static libraries from linking context
for linker_input in cc_info.linking_context.linker_inputs.to_list():
for library in linker_input.libraries:
if library.static_library:
dep_libraries.append(library.static_library)
elif DefaultInfo in dep:
# Fallback for non-CcInfo dependencies (e.g., legacy rules)
for file in dep[DefaultInfo].files.to_list():
if file.extension in ["h", "hpp", "hh", "hxx"]:
dep_headers.append(file)
Expand Down Expand Up @@ -74,7 +88,23 @@ def _cpp_component_impl(ctx):

# Basic compiler flags for Preview2
compile_args.add("--target=wasm32-wasip2")
compile_args.add("--sysroot=" + sysroot)

# Build sysroot path from toolchain repository for external compatibility
if sysroot_files and sysroot_files.files:
# Use sysroot_files to determine the actual sysroot directory
toolchain_file = sysroot_files.files.to_list()[0]

# Extract the sysroot directory by removing the file component
if "/sysroot/" in toolchain_file.path:
# Get everything up to and including /sysroot/
sysroot_base = toolchain_file.path.split("/sysroot/")[0] + "/sysroot"
sysroot_path = sysroot_base
else:
sysroot_path = sysroot
else:
sysroot_path = sysroot

compile_args.add("--sysroot=" + sysroot_path)

# Component model definitions
compile_args.add("-D_WASI_EMULATED_PROCESS_CLOCKS")
Expand Down Expand Up @@ -109,19 +139,34 @@ def _cpp_component_impl(ctx):

# Include directories
compile_args.add("-I" + work_dir.path)

# Add C++ standard library paths for wasm32-wasip2 target
if ctx.attr.language == "cpp":
compile_args.add("-I" + sysroot + "/include/wasm32-wasip2/c++/v1")
compile_args.add("-I" + sysroot + "/include/c++/v1")

# WASI SDK stores C++ headers in share/wasi-sysroot, not just sysroot
if "/external/" in sysroot_path:
toolchain_repo = sysroot_path.split("/sysroot")[0]
wasi_sysroot = toolchain_repo + "/share/wasi-sysroot"
else:
wasi_sysroot = sysroot_path
compile_args.add("-I" + wasi_sysroot + "/include/wasm32-wasip2/c++/v1")
compile_args.add("-I" + wasi_sysroot + "/include/c++/v1")

# Also add clang's builtin headers
if "/external/" in sysroot_path:
compile_args.add("-I" + toolchain_repo + "/lib/clang/20/include")

for include in ctx.attr.includes:
compile_args.add("-I" + include)

# Add dependency header directories
# Add dependency include directories from CcInfo
for include_dir in dep_includes:
if include_dir not in [work_dir.path] + ctx.attr.includes:
compile_args.add("-I" + include_dir)

# Add dependency header directories (fallback for non-CcInfo deps)
for dep_hdr in dep_headers:
include_dir = dep_hdr.dirname
if include_dir not in [work_dir.path] + ctx.attr.includes:
if include_dir not in [work_dir.path] + ctx.attr.includes + dep_includes:
compile_args.add("-I" + include_dir)

# Defines
Expand Down Expand Up @@ -400,7 +445,17 @@ def _cc_component_library_impl(ctx):
# Compile arguments
compile_args = ctx.actions.args()
compile_args.add("--target=wasm32-wasip2")
compile_args.add("--sysroot=" + sysroot)

# Resolve sysroot path dynamically for external repository compatibility
if sysroot_files and sysroot_files.files:
toolchain_file = sysroot_files.files.to_list()[0]
if "/sysroot/" in toolchain_file.path:
sysroot_dir = toolchain_file.path.split("/sysroot/")[0] + "/sysroot"
else:
sysroot_dir = sysroot
else:
sysroot_dir = sysroot
compile_args.add("--sysroot=" + sysroot_dir)
compile_args.add("-c") # Compile only, don't link

# Component model definitions
Expand Down Expand Up @@ -431,12 +486,22 @@ def _cc_component_library_impl(ctx):
# Include directories
for hdr in ctx.files.hdrs:
compile_args.add("-I" + hdr.dirname)

# Add C++ standard library paths for wasm32-wasip2 target
if ctx.attr.language == "cpp":
compile_args.add("-I" + sysroot + "/include/wasm32-wasip2/c++/v1")
compile_args.add("-I" + sysroot + "/include/c++/v1")

# WASI SDK stores C++ headers in share/wasi-sysroot, not just sysroot
if "/external/" in sysroot_dir:
toolchain_repo = sysroot_dir.split("/sysroot")[0]
wasi_sysroot = toolchain_repo + "/share/wasi-sysroot"
else:
wasi_sysroot = sysroot_dir
compile_args.add("-I" + wasi_sysroot + "/include/wasm32-wasip2/c++/v1")
compile_args.add("-I" + wasi_sysroot + "/include/c++/v1")

# Also add clang's builtin headers
if "/external/" in sysroot_dir:
compile_args.add("-I" + toolchain_repo + "/lib/clang/20/include")

for include in ctx.attr.includes:
compile_args.add("-I" + include)

Expand Down Expand Up @@ -480,8 +545,33 @@ def _cc_component_library_impl(ctx):
progress_message = "Creating component library %s" % ctx.label,
)

# Collect transitive headers and libraries from dependencies
transitive_headers = []
transitive_libraries = []
transitive_includes = []

for dep in ctx.attr.deps:
if CcInfo in dep:
cc_info = dep[CcInfo]
transitive_headers.append(cc_info.compilation_context.headers)
transitive_includes.extend(cc_info.compilation_context.includes.to_list())
transitive_libraries.append(cc_info.linking_context.linker_inputs)

# Create compilation context with current headers and transitive headers
compilation_context = cc_common.create_compilation_context(
headers = depset(ctx.files.hdrs, transitive = transitive_headers),
includes = depset([h.dirname for h in ctx.files.hdrs] + ctx.attr.includes, transitive = [depset(transitive_includes)]),
)

# Create CcInfo provider with compilation context only
# Note: We don't create linking context since we're using custom WASM toolchain
cc_info = CcInfo(
compilation_context = compilation_context,
)

return [
DefaultInfo(files = depset([library] + ctx.files.hdrs)),
cc_info,
OutputGroupInfo(
library = depset([library]),
objects = depset(object_files),
Expand Down
16 changes: 15 additions & 1 deletion docs-site/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,16 @@ export default defineConfig({
expressiveCode: {
themes: ['github-dark', 'github-light'],
// Map languages for better syntax highlighting
langs: ['python', 'rust', 'go', 'javascript', 'typescript', 'bash', 'yaml', 'json', 'dockerfile', 'starlark']
langs: ['python', 'rust', 'go', 'javascript', 'typescript', 'bash', 'yaml', 'json', 'dockerfile'],
// Use Python grammar for Starlark since Starlark syntax is a subset of Python
shiki: {
langAlias: {
'starlark': 'python',
'star': 'python',
'bzl': 'python',
'bazel': 'python'
}
}
},
social: [
{
Expand Down Expand Up @@ -79,6 +88,9 @@ export default defineConfig({
label: 'Examples',
items: [
{ label: 'Basic Component', slug: 'examples/basic' },
{ label: 'Calculator (C++)', slug: 'examples/calculator' },
{ label: 'HTTP Service (Go)', slug: 'examples/http-service' },
{ label: 'Multi-Language System', slug: 'examples/multi-language' },
],
},
{
Expand All @@ -98,6 +110,8 @@ export default defineConfig({
label: 'Production',
items: [
{ label: 'Deployment Guide', slug: 'production/deployment-guide' },
{ label: 'OCI Publishing', slug: 'production/publishing' },
{ label: 'Performance Optimization', slug: 'production/performance' },
],
},
{
Expand Down
Loading
Loading