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
24 changes: 24 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

module(
name = "emboss",
version = "0.0.0",
Expand All @@ -18,6 +32,16 @@ bazel_dep(name = "rules_python", version = "1.0.0")
bazel_dep(name = "rules_cc", version = "0.1.0")
bazel_dep(name = "rules_shell", version = "0.3.0")
bazel_dep(name = "rules_license", version = "0.0.8")
bazel_dep(name = "rules_rust", version = "0.71.3")

rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
rust.toolchain(
edition = "2021",
versions = ["1.79.0"],
)
use_repo(rust, "rust_toolchains")

register_toolchains("@rust_toolchains//:all")

pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
pip.parse(
Expand Down
103 changes: 103 additions & 0 deletions build_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ There is also a convenience macro, `emboss_cc_library()`, which creates an
load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cc_toolchain", "use_cc_toolchain")
load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
load("@rules_rust//rust:defs.bzl", "rust_library")

def emboss_cc_library(name, srcs, deps = [], import_dirs = [], enable_enum_traits = True, **kwargs):
"""Constructs a C++ library from an .emb file.
Expand Down Expand Up @@ -273,3 +274,105 @@ cc_emboss_library = rule(
},
provides = [CcInfo, EmbossInfo],
)

def _rust_emboss_codegen_impl(ctx):
if len(ctx.attr.deps) != 1:
fail("`deps` attribute must contain exactly one label.", attr = "deps")
dep = ctx.attr.deps[0]
emboss_info = dep[EmbossInfo]

src = emboss_info.direct_source
out_file = ctx.actions.declare_file(src.basename + ".rs")

args = ctx.actions.args()
args.add("--input-file")
args.add_all(emboss_info.direct_ir)
args.add("--output-file")
args.add(out_file)

ctx.actions.run(
inputs = emboss_info.direct_ir,
outputs = [out_file],
executable = ctx.executable._emboss_rust_compiler,
arguments = [args],
mnemonic = "EmbossRust",
)

return [DefaultInfo(files = depset([out_file]))]

_rust_emboss_codegen = rule(
implementation = _rust_emboss_codegen_impl,
attrs = {
"deps": attr.label_list(
providers = [EmbossInfo],
allow_rules = ["emboss_library"],
allow_files = False,
),
"_emboss_rust_compiler": attr.label(
executable = True,
cfg = "exec",
default = Label("//compiler/back_end/experimental/rust:emboss_codegen_rust"),
),
},
)

def _derive_workspace_path(src, package):
if src.startswith("//"):
if ":" in src:
pkg, name = src[2:].split(":", 1)
else:
parts = src[2:].split("/")
pkg = "/".join(parts[:-1])
name = parts[-1]
return pkg + "/" + name if pkg else name
elif src.startswith("@"):
return src.replace(":", "/").replace("@", "")
else:
return package + "/" + src if package else src

def _derive_crate_name(src, package):
path = _derive_workspace_path(src, package)
return path.replace("/", "_").replace(".", "_").replace("-", "_")

def emboss_rust_library(name, srcs, deps = [], rust_deps = [], import_dirs = [], **kwargs):
"""Constructs a Rust library from an .emb file.

Args:
name: The name of the resulting rust_library.
srcs: A list containing exactly one .emb source file.
deps: A list of emboss_rust_library dependencies.
rust_deps: Dependencies to forward to the rust_library.
import_dirs: A list of directories to add to the import path.
**kwargs: Additional arguments to pass to rust_library.
"""
if len(srcs) != 1:
fail(
"Must specify exactly one Emboss source file for emboss_rust_library.",
"srcs",
)

emboss_library_name = name + "_ir"

emboss_library(
name = emboss_library_name,
srcs = srcs,
deps = [dep + "_ir" for dep in deps],
import_dirs = import_dirs,
)

codegen_name = name + "_srcs"

_rust_emboss_codegen(
name = codegen_name,
deps = [":" + emboss_library_name],
)

crate_name = kwargs.pop("crate_name", _derive_crate_name(srcs[0], native.package_name()))

rust_library(
name = name,
srcs = [":" + codegen_name],
crate_name = crate_name,
deps = rust_deps + deps + ["//runtime/experimental/rust:emboss_runtime"],
**kwargs
)
13 changes: 13 additions & 0 deletions compiler/back_end/experimental/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
37 changes: 37 additions & 0 deletions compiler/back_end/experimental/rust/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@rules_python//python:py_binary.bzl", "py_binary")

package(
default_applicable_licenses = ["//:license"],
default_visibility = [
"//visibility:private",
],
)

py_binary(
name = "emboss_codegen_rust",
srcs = ["emboss_codegen_rust.py"],
data = ["generated_code_templates"],
visibility = ["//visibility:public"],
deps = [
"//compiler/back_end/util:code_template",
"//compiler/front_end:module_ir",
"//compiler/util:error",
"//compiler/util:ir_data",
"//compiler/util:ir_util",
"//compiler/util:resources",
],
)
15 changes: 15 additions & 0 deletions compiler/back_end/experimental/rust/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Init file for rust backend.
Loading
Loading