libgbl: create build targets

Creates some initial no-op code to get the build targets set up for
future development, as well as test configuration.

Bug: 293348611
Test: atest
Change-Id: Ie30ab07523ae659d0f24d88b920ba42a17820756
diff --git a/libgbl/Android.bp b/libgbl/Android.bp
new file mode 100644
index 0000000..c3e766c
--- /dev/null
+++ b/libgbl/Android.bp
@@ -0,0 +1,63 @@
+// Copyright 2023, The Android Open Source Project
+//
+// 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
+//
+//     http://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.
+
+package {
+    default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+rust_defaults {
+    name: "libgbl_defaults",
+    srcs: ["src/lib.rs"],
+}
+
+// GBL Rust library.
+//
+// We can only build rlib here because we want to build with [no_std], but
+// don't want to define panic handlers (the library user should do that).
+// Only rlibs can compile without these definitions and allow other code
+// to provide them instead (https://stackoverflow.com/q/43097676).
+//
+// We also only need a host lib because this library is not currently used
+// in-tree except for testing; bootloaders will need to copy this code out to
+// their external codebase, so they will not be consuming these build targets
+// directly.
+rust_library_host_rlib {
+    name: "libgbl",
+    crate_name: "gbl",
+    defaults: [ "libgbl_defaults" ],
+}
+
+// libgbl unit tests.
+//
+// Rust unit tests always use std, so this module performs our main logic
+// unit testing but cannot test a full [no_std] compilation.
+rust_test_host {
+    name: "libgbl_test",
+    defaults: [ "libgbl_defaults" ],
+    // Also build the module which can test [no_std] compilation.
+    required: [ "libgbl_test_nostd" ],
+}
+
+// Test building with libgbl in a [no_std] environment.
+//
+// This is a compile-only test to ensure [no_std] works and demonstrate
+// which Rust hooks are required to implement. This code will not be exeuted,
+// so actual logic unittests go in libgbl_test instead.
+rust_binary_host {
+    name: "libgbl_test_nostd",
+    srcs: [ "tests/nostd.rs" ],
+    rlibs: [ "libgbl" ],
+    // panic=abort to satisfy the eh_personality compile requirement.
+    flags: [ "-C", "panic=abort" ],
+}
diff --git a/libgbl/cargo.toml b/libgbl/cargo.toml
new file mode 100644
index 0000000..2f7003e
--- /dev/null
+++ b/libgbl/cargo.toml
@@ -0,0 +1,21 @@
+# Copyright 2023, The Android Open Source Project
+#
+# 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
+#
+#     http:#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.
+
+# For now this file is only provided so that `rustfmt` can run, but it may
+# be worthwhile to set it up properly as a reference implementation for building
+# with libgbl.
+
+[package]
+name = "gbl"
+version = "0.1.0"
diff --git a/libgbl/src/lib.rs b/libgbl/src/lib.rs
new file mode 100644
index 0000000..c33e772
--- /dev/null
+++ b/libgbl/src/lib.rs
@@ -0,0 +1,36 @@
+// Copyright 2023, The Android Open Source Project
+//
+// 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
+//
+//     http://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.
+
+//! # Generic Boot Loader (gbl) Library
+//!
+//! TODO: documentation.
+
+// This code is intended for use in bootloaders that typically will not support
+// the Rust standard library
+#![no_std]
+
+/// Placeholder code to get a build rule and tests in place.
+pub fn foo() -> u32 {
+    42
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_foo() {
+        assert_eq!(foo(), 42);
+    }
+}
diff --git a/libgbl/tests/nostd.rs b/libgbl/tests/nostd.rs
new file mode 100644
index 0000000..26e3dbb
--- /dev/null
+++ b/libgbl/tests/nostd.rs
@@ -0,0 +1,37 @@
+// Copyright 2023, The Android Open Source Project
+//
+// 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
+//
+//     http://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.
+
+//! This is a compilation-only test to ensure that libgbl can build against
+//! [no_std] code.
+//!
+//! This also provides a reference for the Rust hooks that a [no_std] user must
+//! provide in order to build against libgbl.
+
+#![no_main]
+#![no_std]
+
+use core::panic::PanicInfo;
+
+use gbl as _;
+
+#[panic_handler]
+fn panic(_: &PanicInfo) -> ! {
+    loop {}
+}
+
+/// main() entry point replacement required by [no_std].
+#[no_mangle]
+pub fn main() -> ! {
+    panic!()
+}