Brian Silverman | e4c79ce | 2022-08-15 05:57:28 -0700 | [diff] [blame^] | 1 | use std::{ffi::CString, sync::Once}; |
| 2 | |
| 3 | autocxx::include_cpp! ( |
| 4 | #include "aos/init.h" |
| 5 | |
| 6 | safety!(unsafe) |
| 7 | |
| 8 | generate!("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. |
| 18 | pub fn test_init() { |
| 19 | static ONCE: Once = Once::new(); |
| 20 | ONCE.call_once(|| { |
| 21 | let argv0 = std::env::args().next().expect("must have argv[0]"); |
| 22 | let argv0 = CString::new(argv0).expect("argv[0] may not have NUL"); |
| 23 | // SAFETY: argv0 is a NUL-terminated string. |
| 24 | unsafe { ffi::aos::InitFromRust(argv0.as_ptr()) }; |
| 25 | }); |
| 26 | |
| 27 | // TODO(Brian): Do we want any of the other stuff that `:gtest_main` has? |
| 28 | // TODO(Brian): Call `aos::SetShmBase` like `:gtest_main` does. |
| 29 | } |