Adam Snaider | 48a62f3 | 2023-10-02 15:49:23 -0700 | [diff] [blame] | 1 | #include "aos/init_for_rust.h" |
| 2 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 3 | #include "absl/flags/flag.h" |
| 4 | #include "absl/flags/reflection.h" |
| 5 | #include "absl/log/check.h" |
| 6 | #include "absl/log/initialize.h" |
| 7 | #include "absl/log/log.h" |
Adam Snaider | 48a62f3 | 2023-10-02 15:49:23 -0700 | [diff] [blame] | 8 | |
| 9 | #include "aos/init.h" |
| 10 | |
| 11 | namespace aos { |
| 12 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 13 | void InitFromRust(const char * /*argv0*/) { |
| 14 | CHECK(!aos::IsInitialized()) << "Only initialize once."; |
Adam Snaider | 48a62f3 | 2023-10-02 15:49:23 -0700 | [diff] [blame] | 15 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 16 | absl::InitializeLog(); |
Adam Snaider | 48a62f3 | 2023-10-02 15:49:23 -0700 | [diff] [blame] | 17 | |
| 18 | // TODO(Brian): Where should Rust binaries be configured to write coredumps? |
| 19 | |
| 20 | // TODO(Brian): Figure out what to do with allocator hooks for C++ and Rust. |
| 21 | |
| 22 | MarkInitialized(); |
| 23 | } |
| 24 | |
| 25 | std::vector<FlagInfo> GetCppFlags() { |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 26 | absl::flat_hash_map<absl::string_view, absl::CommandLineFlag *> info = |
| 27 | absl::GetAllFlags(); |
Adam Snaider | 48a62f3 | 2023-10-02 15:49:23 -0700 | [diff] [blame] | 28 | std::vector<FlagInfo> out; |
| 29 | for (const auto &flag : info) { |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 30 | std::string type; |
| 31 | if (flag.second->IsOfType<float>()) { |
| 32 | type = "float"; |
| 33 | } else if (flag.second->IsOfType<double>()) { |
| 34 | type = "double"; |
| 35 | } else if (flag.second->IsOfType<bool>()) { |
| 36 | type = "bool"; |
| 37 | } else if (flag.second->IsOfType<uint8_t>()) { |
| 38 | type = "uint8_t"; |
| 39 | } else if (flag.second->IsOfType<int8_t>()) { |
| 40 | type = "int8_t"; |
| 41 | } else if (flag.second->IsOfType<uint16_t>()) { |
| 42 | type = "uint16_t"; |
| 43 | } else if (flag.second->IsOfType<int16_t>()) { |
| 44 | type = "int16_t"; |
| 45 | } else if (flag.second->IsOfType<uint32_t>()) { |
| 46 | type = "uint32_t"; |
| 47 | } else if (flag.second->IsOfType<int32_t>()) { |
| 48 | type = "int32_t"; |
| 49 | } else if (flag.second->IsOfType<uint64_t>()) { |
| 50 | type = "uint64_t"; |
| 51 | } else if (flag.second->IsOfType<int64_t>()) { |
| 52 | type = "int64_t"; |
| 53 | } else if (flag.second->IsOfType<std::string>()) { |
| 54 | type = "string"; |
| 55 | } else if (flag.second->IsOfType<std::vector<std::string>>()) { |
| 56 | type = "vector<string>"; |
| 57 | } else { |
| 58 | LOG(FATAL) << "Unknown type for flag " << flag.second->Name() |
| 59 | << " in file " << flag.second->Filename(); |
| 60 | } |
| 61 | |
| 62 | LOG(INFO) << "Reporting flag " << flag.second->Name() << " " << type << " " |
| 63 | << flag.second->DefaultValue(); |
Adam Snaider | 48a62f3 | 2023-10-02 15:49:23 -0700 | [diff] [blame] | 64 | FlagInfo out_flag = { |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 65 | .name_ = std::string(flag.second->Name()), |
| 66 | .type_ = type, |
| 67 | .description_ = flag.second->Help(), |
| 68 | .default_value_ = flag.second->DefaultValue(), |
| 69 | .filename_ = flag.second->Filename(), |
Adam Snaider | 48a62f3 | 2023-10-02 15:49:23 -0700 | [diff] [blame] | 70 | }; |
| 71 | out.push_back(out_flag); |
| 72 | } |
| 73 | return out; |
| 74 | } |
| 75 | |
| 76 | bool SetCommandLineOption(const char *name, const char *value) { |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 77 | absl::CommandLineFlag *flag = absl::FindCommandLineFlag(name); |
| 78 | if (flag == nullptr) { |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | std::string error; |
| 83 | bool result = flag->ParseFrom(value, &error); |
| 84 | return result; |
Adam Snaider | 48a62f3 | 2023-10-02 15:49:23 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | std::string GetCommandLineOption(const char *name) { |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 88 | absl::CommandLineFlag *flag = absl::FindCommandLineFlag(name); |
| 89 | CHECK(flag != nullptr) << "Unknown flag " << name; |
| 90 | |
| 91 | return flag->CurrentValue(); |
Adam Snaider | 48a62f3 | 2023-10-02 15:49:23 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | } // namespace aos |