Set up initial Bazel Rust build
Change-Id: I1f5e582248532818b7c4f827ed22509a1e3bb7f5
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/maize/+/253873
Reviewed-by: Ted Pudlik <tpudlik@google.com>
Lint: Lint 🤖 <android-build-ayeaye@system.gserviceaccount.com>
Commit-Queue: Erik Gilling <konkers@google.com>
diff --git a/.bazelrc b/.bazelrc
new file mode 100644
index 0000000..a03cf19
--- /dev/null
+++ b/.bazelrc
@@ -0,0 +1,49 @@
+# Copyright 2024 The Pigweed Authors
+#
+# 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.
+
+# Required for new toolchain resolution API.
+build --incompatible_enable_cc_toolchain_resolution
+
+# Enable clippy lints
+build --aspects=@rules_rust//rust:defs.bzl%rust_clippy_aspect
+build --output_groups=+clippy_checks
+
+# Enforce rustfmt formatting
+# TODO - konkers: Fix rustfmt
+# build --aspects=@rules_rust//rust:defs.bzl%rustfmt_aspect
+# build --output_groups=+rustfmt_checks
+
+# TODO - konkers: fix upstream stable toolchains
+build --@@rules_rust+//rust/toolchain/channel=nightly
+# Remote execution config definitions
+# ===================================
+# Use the remote cache. This will only work for users who have permission to access it.
+common:remote_cache --bes_backend="buildeventservice.googleapis.com"
+common:remote_cache --bes_instance_name=pigweed-rbe-open
+common:remote_cache --bes_results_url="https://source.cloud.google.com/results/invocations/"
+common:remote_cache --bes_timeout=600s
+common:remote_cache --experimental_remote_cache_eviction_retries=5
+common:remote_cache --google_default_credentials=true
+# Required to use buildeventservice on Bazel 8+.
+# TODO: b/345556899 -- Remove this flag when no longer required.
+common:remote_cache --legacy_important_outputs
+common:remote_cache --remote_cache=grpcs://remotebuildexecution.googleapis.com
+common:remote_cache --remote_instance_name=projects/pigweed-rbe-open/instances/default-instance
+common:remote_cache --remote_upload_local_results=false
+
+# cache-silo-key: this is essentially a "salt" added to the remote cache key.
+# Change it to a new value when we want to create a new cache from scratch
+# (e.g. to recover from cache poisoning). We've used the current date in
+# YYYYMMDD format in the past.
+common --remote_default_exec_properties=cache-silo-key=20240918
diff --git a/.bazelversion b/.bazelversion
new file mode 100644
index 0000000..41b382c
--- /dev/null
+++ b/.bazelversion
@@ -0,0 +1 @@
+1ff5af18d045e8f30a2a0367470db4e8225a785c
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..7226d31
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,45 @@
+# Copyright 2024 The Pigweed Authors
+#
+# 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.
+
+# Build
+/MODULE.bazel.lock
+rust-project.json
+user.bazelrc
+/bazel-*
+
+# Editors
+.vscode/*
+# Vim
+*.swp
+*.swo
+# Emacs
+*flycheck_*
+*_flymake.*
+.#*
+*~
+\#*#
+
+# Git
+*.orig
+*.BACKUP.*
+*.BASE.*
+*.LOCAL.*
+*.REMOTE.*
+*_BACKUP_*.txt
+*_BASE_*.txt
+*_LOCAL_*.txt
+*_REMOTE_*.txt
+
+# Mac
+.DS_Store
diff --git a/BUILD.bazel b/BUILD.bazel
new file mode 100644
index 0000000..b93ab02
--- /dev/null
+++ b/BUILD.bazel
@@ -0,0 +1,18 @@
+# Copyright 2024 The Pigweed Authors
+#
+# 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.
+
+alias(
+ name = "gen_rust_project",
+ actual = "@rules_rust//tools/rust_analyzer:gen_rust_project",
+)
diff --git a/MODULE.bazel b/MODULE.bazel
new file mode 100644
index 0000000..219f452
--- /dev/null
+++ b/MODULE.bazel
@@ -0,0 +1,64 @@
+# Copyright 2024 The Pigweed Authors
+#
+# 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 = "maize",
+)
+
+bazel_dep(name = "rules_rust", version = "0.52.2")
+bazel_dep(name = "pigweed")
+bazel_dep(name = "pw_toolchain")
+
+# Module overrides
+# ================
+# single_version_override(module_name = "rules_cc", version = "0.0.10")
+
+# TODO: https://pwbug.dev/354274498 - nanopb is not yet in the BCR.
+git_override(
+ module_name = "nanopb",
+ commit = "7c6c581bc6f7406a4f01c3b9853251ff0a68458b",
+ remote = "https://github.com/nanopb/nanopb.git",
+)
+
+git_override(
+ module_name = "pigweed",
+ commit = "23370ed5ed0e01273080c231ee3475916dc74fba",
+ remote = "https://pigweed.googlesource.com/pigweed/pigweed",
+)
+
+git_override(
+ module_name = "pw_toolchain",
+ commit = "23370ed5ed0e01273080c231ee3475916dc74fba",
+ remote = "https://pigweed.googlesource.com/pigweed/pigweed",
+ strip_prefix = "pw_toolchain_bazel",
+)
+
+# Toolchain registration
+# ======================
+# Register Pigweed's C++ toolchains.
+register_toolchains(
+ "@pigweed//pw_toolchain:cc_toolchain_cortex-m0",
+ "@pigweed//pw_toolchain:cc_toolchain_cortex-m33",
+ "@pigweed//pw_toolchain/host_clang:host_cc_toolchain_linux",
+ "@pigweed//pw_toolchain/host_clang:host_cc_toolchain_macos",
+ dev_dependency = True,
+)
+
+pw_rust = use_extension("@pigweed//pw_toolchain/rust:extensions.bzl", "pw_rust")
+pw_rust.toolchain(cipd_tag = "rust_revision:bf9c7a64ad222b85397573668b39e6d1ab9f4a72")
+use_repo(pw_rust, "pw_rust_toolchains")
+
+register_toolchains(
+ "@pw_rust_toolchains//:all",
+ dev_dependency = True,
+)
diff --git a/build/test/rust/BUILD.bazel b/build/test/rust/BUILD.bazel
new file mode 100644
index 0000000..d9ad974
--- /dev/null
+++ b/build/test/rust/BUILD.bazel
@@ -0,0 +1,27 @@
+# Copyright 2024 The Pigweed Authors
+#
+# 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_rust//rust:defs.bzl", "rust_library", "rust_test")
+
+rust_library(
+ name = "basic_test",
+ srcs = [
+ "basic_test.rs",
+ ],
+)
+
+rust_test(
+ name = "basic_test_test",
+ crate = ":basic_test",
+)
diff --git a/build/test/rust/basic_test.rs b/build/test/rust/basic_test.rs
new file mode 100644
index 0000000..f7ce691
--- /dev/null
+++ b/build/test/rust/basic_test.rs
@@ -0,0 +1,27 @@
+// Copyright 2024 The Pigweed Authors
+//
+// 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.
+
+pub fn add(a: u32, b: u32) -> u32 {
+ a + b
+}
+
+#[cfg(test)]
+mod test {
+ use super::*;
+
+ #[test]
+ fn adding_two_numbers_returns_correct_value() {
+ assert_eq!(add(1, 2), 1 + 2);
+ }
+}
diff --git a/pigweed.json b/pigweed.json
new file mode 100644
index 0000000..1fc805e
--- /dev/null
+++ b/pigweed.json
@@ -0,0 +1,20 @@
+{
+ "pw": {
+ "bazel_presubmit": {
+ "remote_cache": true,
+ "upload_local_results": true,
+ "programs": {
+ "default": [
+ [
+ "build",
+ "//..."
+ ],
+ [
+ "test",
+ "//..."
+ ]
+ ]
+ }
+ }
+ }
+}