Support C++ and Rust interop with autocxx
Change-Id: Id2c852bf48ed58d6284e7ab8a388ac38419fc474
Signed-off-by: Brian Silverman <bsilver16384@gmail.com>
diff --git a/build_tests/BUILD b/build_tests/BUILD
index dc76f45..e7f236a 100644
--- a/build_tests/BUILD
+++ b/build_tests/BUILD
@@ -5,6 +5,7 @@
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_test")
load("@npm//@bazel/typescript:index.bzl", "ts_library")
load("@npm//@bazel/concatjs:index.bzl", "karma_web_test_suite")
+load("//tools/build_rules:autocxx.bzl", "autocxx_library")
cc_test(
name = "gflags_build_test",
@@ -119,7 +120,11 @@
go_library(
name = "build_tests_lib",
- srcs = ["hello.go"],
+ srcs = [
+ "hello.go",
+ # Not sure why gazelle wants this here?
+ "hello_autocxx.h",
+ ],
importpath = "github.com/frc971/971-Robot-Code/build_tests",
target_compatible_with = ["@platforms//cpu:x86_64"],
visibility = ["//visibility:private"],
@@ -191,3 +196,27 @@
target_compatible_with = ["@platforms//os:linux"],
deps = [":rust_in_cc_rs"],
)
+
+cc_library(
+ name = "hello_autocxx_cc",
+ hdrs = [
+ "hello_autocxx.h",
+ ],
+)
+
+autocxx_library(
+ name = "hello_autocxx",
+ srcs = ["hello_autocxx.rs"],
+ libs = [":hello_autocxx_cc"],
+ override_cc_toolchain = "@llvm_toolchain//:cc-clang-x86_64-linux",
+ target_compatible_with = ["//tools/platforms/rust:has_support"],
+)
+
+rust_test(
+ name = "hello_autocxx_test",
+ crate = ":hello_autocxx",
+ # TODO: Make Rust play happy with pic vs nopic. Details at:
+ # https://github.com/bazelbuild/rules_rust/issues/118
+ rustc_flags = ["-Crelocation-model=static"],
+ target_compatible_with = ["//tools/platforms/rust:has_support"],
+)
diff --git a/build_tests/hello_autocxx.h b/build_tests/hello_autocxx.h
new file mode 100644
index 0000000..8b6e285
--- /dev/null
+++ b/build_tests/hello_autocxx.h
@@ -0,0 +1,8 @@
+#ifndef BUILD_TESTS_HELLO_AUTOCXX_H_
+#define BUILD_TESTS_HELLO_AUTOCXX_H_
+
+#include <stdlib.h>
+
+int plain_function() { return 971; }
+
+#endif // BUILD_TESTS_HELLO_AUTOCXX_H_
diff --git a/build_tests/hello_autocxx.rs b/build_tests/hello_autocxx.rs
new file mode 100644
index 0000000..483f8be
--- /dev/null
+++ b/build_tests/hello_autocxx.rs
@@ -0,0 +1,16 @@
+use autocxx::include_cpp;
+
+include_cpp! (
+#include "build_tests/hello_autocxx.h"
+generate!("plain_function")
+);
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_plain_function() {
+ assert_eq!(autocxx::c_int::from(971), unsafe { ffi::plain_function() });
+ }
+}