build: Add Bazel based build (pdf schematic) and test (ERC)

Change-Id: Ica99064ef00186e3386100932dd014e045ad9e3a
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/hardware/testing/+/244552
Reviewed-by: Armando Montanez <amontanez@google.com>
Lint: Lint 🤖 <android-build-ayeaye@system.gserviceaccount.com>
Pigweed-Auto-Submit: Erik Gilling <konkers@google.com>
Commit-Queue: Erik Gilling <konkers@google.com>
diff --git a/.bazelrc b/.bazelrc
new file mode 100644
index 0000000..d6b8b6a
--- /dev/null
+++ b/.bazelrc
@@ -0,0 +1 @@
+build --action_env=PATH --action_env=HOME
diff --git a/.gitignore b/.gitignore
index 59fde34..95d643c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -29,3 +29,9 @@
 # Exported BOM files
 *.xml
 *.csv
+
+# Bazel symlinks
+/bazel-*
+
+# Bazel module lockfile
+MODULE.bazel.lock
diff --git a/MODULE.bazel b/MODULE.bazel
new file mode 100644
index 0000000..fe54e16
--- /dev/null
+++ b/MODULE.bazel
@@ -0,0 +1,20 @@
+# 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 = "pigweed-hardware-testing",
+    version = "0.0.1",
+)
+
+bazel_dep(name = "bazel_skylib", version = "1.7.1")
diff --git a/README.md b/README.md
index c5ae3d3..0262e2f 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,10 @@
-Pigweed Open Source Template Repository
-=======================================
+# Pigweed Hardware Testing.... Hardware
 
-This repository is a template that we will use when creating new open source
-repositories for Pigweed.
+## Bazel build instructions
+
+* Install [bazelisk](https://github.com/bazelbuild/bazelisk).
+* Install [KiCad](https://www.kicad.org/download/) and ensure `kicad-cli`
+  is added to your `$PATH`.
+* Follow the instruction at [third_party/rp2350/README.md](third_party/rp2350/README.md)
+* Run `bazelisk build //...` to build artifacts (PDF Schematic, etc.).
+* Run `bazelisk test //..` to run ERC/DRC.
diff --git a/build/BUILD.bazel b/build/BUILD.bazel
new file mode 100644
index 0000000..038a123
--- /dev/null
+++ b/build/BUILD.bazel
@@ -0,0 +1,25 @@
+# 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.
+
+sh_binary(
+    name = "kicad_cli",
+    srcs = ["kicad_cli.sh"],
+    visibility = ["//visibility:public"],
+)
+
+sh_binary(
+    name = "kicad_cli_test",
+    srcs = ["kicad_cli_test.sh"],
+    visibility = ["//visibility:public"],
+)
diff --git a/build/kicad.bzl b/build/kicad.bzl
new file mode 100644
index 0000000..c21bb6d
--- /dev/null
+++ b/build/kicad.bzl
@@ -0,0 +1,69 @@
+# 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.
+
+"""KiCad Bazel Rules."""
+
+load("@bazel_skylib//rules:native_binary.bzl", "native_test")
+
+def kicad_project(name, project, sym_lib_table, fp_lib_table, schematic_root, schematic_srcs, data):
+    kicad_schematic_pdf(name = name + "_sch", root = schematic_root, srcs = schematic_srcs, data = data + [project, sym_lib_table])
+    kicad_schematic_test(name = name + "_test", root = schematic_root, srcs = schematic_srcs, data = data + [project, sym_lib_table])
+
+def kicad_schematic_test(name, root, srcs, data):
+    args = ["sch", "erc", "--exit-code-violations", "$(rootpath " + root + " )"]
+
+    native_test(
+        name = name,
+        src = str(Label("//build:kicad_cli_test")),
+        args = args,
+        data = srcs + data + [root],
+        # Note: out is mandatory in older bazel-skylib versions.
+        out = name + ".exe",
+    )
+
+def _kicad_schematic_pdf_impl(ctx):
+    pdf_file = ctx.actions.declare_file(ctx.label.name + ".pdf")
+
+    args = ["sch", "export", "pdf", "--output", pdf_file.path, ctx.file.root.path]
+
+    ctx.actions.run(
+        inputs = ctx.files.srcs + [ctx.file.root],
+        outputs = [pdf_file],
+        arguments = args,
+        use_default_shell_env = True,
+        progress_message = "running ERC on %s" % ctx.attr.name,
+        executable = ctx.executable._kicad_cli,
+    )
+
+    return [
+        DefaultInfo(files = depset([pdf_file])),
+    ]
+
+kicad_schematic_pdf = rule(
+    implementation = _kicad_schematic_pdf_impl,
+    attrs = {
+        "srcs": attr.label_list(allow_files = [".kicad_sch"]),
+        "root": attr.label(
+            allow_single_file = [".kicad_sch"],
+            mandatory = True,
+        ),
+        "data": attr.label_list(allow_files = True),
+        "_kicad_cli": attr.label(
+            executable = True,
+            cfg = "exec",
+            allow_files = True,
+            default = Label("//build:kicad_cli"),
+        ),
+    },
+)
diff --git a/build/kicad_cli.sh b/build/kicad_cli.sh
new file mode 100755
index 0000000..73c16cf
--- /dev/null
+++ b/build/kicad_cli.sh
@@ -0,0 +1,18 @@
+#!/bin/sh
+# 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.
+
+echo "running" kicad-cli "$@"
+which kicad-cli
+kicad-cli "$@"
diff --git a/build/kicad_cli_test.sh b/build/kicad_cli_test.sh
new file mode 100755
index 0000000..4c6aa24
--- /dev/null
+++ b/build/kicad_cli_test.sh
@@ -0,0 +1,24 @@
+#!/bin/sh
+# 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.
+
+echo "PWD: " $(pwd)
+REPORT_NAME=$(mktemp)
+echo running kicad-cli "$@" --output $REPORT_NAME
+
+kicad-cli "$@" --output $REPORT_NAME
+
+EXIT_CODE=$?
+cat $REPORT_NAME
+exit $EXIT_CODE
diff --git a/pcb/rp2350_target_board/BUILD.bazel b/pcb/rp2350_target_board/BUILD.bazel
new file mode 100644
index 0000000..3301225
--- /dev/null
+++ b/pcb/rp2350_target_board/BUILD.bazel
@@ -0,0 +1,32 @@
+# 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("//build:kicad.bzl", "kicad_project")
+
+kicad_project(
+    name = "rp2350_target_board",
+    data = ["//third_party/rp2350"],
+    fp_lib_table = "fp-lib-table",
+    project = "rp2350_target_board.kicad_pro",
+    schematic_root = "rp2350_target_board.kicad_sch",
+    schematic_srcs = [
+        "buddy.kicad_sch",
+        "debug_probe.kicad_sch",
+        "dut.kicad_sch",
+        "indicators.kicad_sch",
+        "power.kicad_sch",
+        "rp2350.kicad_sch",
+    ],
+    sym_lib_table = "sym-lib-table",
+)
diff --git a/third_party/rp2350/BUILD.bazel b/third_party/rp2350/BUILD.bazel
new file mode 100644
index 0000000..1cf9161
--- /dev/null
+++ b/third_party/rp2350/BUILD.bazel
@@ -0,0 +1,22 @@
+# 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.
+
+filegroup(
+    name = "rp2350",
+    srcs = [
+        "MCU_RaspberryPi_RP2350.kicad_sym",
+        "RP2350_QFN60.step",
+    ],
+    visibility = ["//visibility:public"],
+)