blob: 9ac62f15a892e67cf962f104a591f6694e9f6920 [file] [log] [blame]
Brian Silvermane4c79ce2022-08-15 05:57:28 -07001use std::{ffi::CString, sync::Once};
2
3autocxx::include_cpp! (
4#include "aos/init.h"
5
6safety!(unsafe)
7
8generate!("aos::InitFromRust")
9);
10
11/// Initializes things for a test.
12///
13/// TODO(Brian): Should we provide a proc macro attribute that handles calling this?
14///
15/// # Panics
16///
17/// Panics if non-test initialization has already been performed.
18pub fn test_init() {
Adam Snaider43516782023-06-26 15:14:18 -070019 init();
20 // TODO(Brian): Do we want any of the other stuff that `:gtest_main` has?
21 // TODO(Brian): Call `aos::SetShmBase` like `:gtest_main` does.
22}
23
24pub fn init() {
Brian Silvermane4c79ce2022-08-15 05:57:28 -070025 static ONCE: Once = Once::new();
26 ONCE.call_once(|| {
27 let argv0 = std::env::args().next().expect("must have argv[0]");
28 let argv0 = CString::new(argv0).expect("argv[0] may not have NUL");
29 // SAFETY: argv0 is a NUL-terminated string.
30 unsafe { ffi::aos::InitFromRust(argv0.as_ptr()) };
31 });
Brian Silvermane4c79ce2022-08-15 05:57:28 -070032}