Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Mårten Kongstad | fbd71e2 | 2023-05-31 13:29:35 +0200 | [diff] [blame] | 17 | use anyhow::{ensure, Result}; |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 18 | use serde::Serialize; |
| 19 | use tinytemplate::TinyTemplate; |
| 20 | |
| 21 | use crate::aconfig::{FlagState, Permission}; |
| 22 | use crate::cache::{Cache, Item}; |
Mårten Kongstad | fbd71e2 | 2023-05-31 13:29:35 +0200 | [diff] [blame] | 23 | use crate::codegen; |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 24 | use crate::commands::OutputFile; |
| 25 | |
| 26 | pub fn generate_cpp_code(cache: &Cache) -> Result<OutputFile> { |
Mårten Kongstad | 066575b | 2023-06-07 16:29:25 +0200 | [diff] [blame^] | 27 | let package = cache.package(); |
| 28 | let class_elements: Vec<ClassElement> = |
| 29 | cache.iter().map(|item| create_class_element(package, item)).collect(); |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 30 | let readwrite = class_elements.iter().any(|item| item.readwrite); |
Mårten Kongstad | fbd71e2 | 2023-05-31 13:29:35 +0200 | [diff] [blame] | 31 | let header = package.replace('.', "_"); |
| 32 | let cpp_namespace = package.replace('.', "::"); |
| 33 | ensure!(codegen::is_valid_name_ident(&header)); |
Mårten Kongstad | 066575b | 2023-06-07 16:29:25 +0200 | [diff] [blame^] | 34 | let context = Context { |
| 35 | header: header.clone(), |
| 36 | cpp_namespace, |
| 37 | package: package.to_string(), |
| 38 | readwrite, |
| 39 | class_elements, |
| 40 | }; |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 41 | let mut template = TinyTemplate::new(); |
| 42 | template.add_template("cpp_code_gen", include_str!("../templates/cpp.template"))?; |
| 43 | let contents = template.render("cpp_code_gen", &context)?; |
Mårten Kongstad | fbd71e2 | 2023-05-31 13:29:35 +0200 | [diff] [blame] | 44 | let path = ["aconfig", &(header + ".h")].iter().collect(); |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 45 | Ok(OutputFile { contents: contents.into(), path }) |
| 46 | } |
| 47 | |
| 48 | #[derive(Serialize)] |
| 49 | struct Context { |
Mårten Kongstad | fbd71e2 | 2023-05-31 13:29:35 +0200 | [diff] [blame] | 50 | pub header: String, |
| 51 | pub cpp_namespace: String, |
Mårten Kongstad | 9fb5896 | 2023-05-31 13:02:13 +0200 | [diff] [blame] | 52 | pub package: String, |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 53 | pub readwrite: bool, |
| 54 | pub class_elements: Vec<ClassElement>, |
| 55 | } |
| 56 | |
| 57 | #[derive(Serialize)] |
| 58 | struct ClassElement { |
| 59 | pub readwrite: bool, |
| 60 | pub default_value: String, |
| 61 | pub flag_name: String, |
Mårten Kongstad | 066575b | 2023-06-07 16:29:25 +0200 | [diff] [blame^] | 62 | pub device_config_namespace: String, |
| 63 | pub device_config_flag: String, |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Mårten Kongstad | 066575b | 2023-06-07 16:29:25 +0200 | [diff] [blame^] | 66 | fn create_class_element(package: &str, item: &Item) -> ClassElement { |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 67 | ClassElement { |
| 68 | readwrite: item.permission == Permission::ReadWrite, |
| 69 | default_value: if item.state == FlagState::Enabled { |
| 70 | "true".to_string() |
| 71 | } else { |
| 72 | "false".to_string() |
| 73 | }, |
| 74 | flag_name: item.name.clone(), |
Mårten Kongstad | 066575b | 2023-06-07 16:29:25 +0200 | [diff] [blame^] | 75 | device_config_namespace: item.namespace.to_string(), |
| 76 | device_config_flag: codegen::create_device_config_ident(package, &item.name) |
| 77 | .expect("values checked at cache creation time"), |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | #[cfg(test)] |
| 82 | mod tests { |
| 83 | use super::*; |
| 84 | use crate::aconfig::{FlagDeclaration, FlagState, FlagValue, Permission}; |
Mårten Kongstad | 2f95444 | 2023-05-17 16:51:16 +0200 | [diff] [blame] | 85 | use crate::cache::CacheBuilder; |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 86 | use crate::commands::Source; |
| 87 | |
| 88 | #[test] |
| 89 | fn test_cpp_codegen_build_time_flag_only() { |
Mårten Kongstad | fbd71e2 | 2023-05-31 13:29:35 +0200 | [diff] [blame] | 90 | let package = "com.example"; |
Mårten Kongstad | 9fb5896 | 2023-05-31 13:02:13 +0200 | [diff] [blame] | 91 | let mut builder = CacheBuilder::new(package.to_string()).unwrap(); |
Mårten Kongstad | 2f95444 | 2023-05-17 16:51:16 +0200 | [diff] [blame] | 92 | builder |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 93 | .add_flag_declaration( |
| 94 | Source::File("aconfig_one.txt".to_string()), |
| 95 | FlagDeclaration { |
| 96 | name: "my_flag_one".to_string(), |
Mårten Kongstad | 066575b | 2023-06-07 16:29:25 +0200 | [diff] [blame^] | 97 | namespace: "ns".to_string(), |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 98 | description: "buildtime disable".to_string(), |
| 99 | }, |
| 100 | ) |
Mårten Kongstad | 2f95444 | 2023-05-17 16:51:16 +0200 | [diff] [blame] | 101 | .unwrap() |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 102 | .add_flag_value( |
| 103 | Source::Memory, |
| 104 | FlagValue { |
Mårten Kongstad | 9fb5896 | 2023-05-31 13:02:13 +0200 | [diff] [blame] | 105 | package: package.to_string(), |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 106 | name: "my_flag_one".to_string(), |
| 107 | state: FlagState::Disabled, |
| 108 | permission: Permission::ReadOnly, |
| 109 | }, |
| 110 | ) |
Mårten Kongstad | 2f95444 | 2023-05-17 16:51:16 +0200 | [diff] [blame] | 111 | .unwrap() |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 112 | .add_flag_declaration( |
| 113 | Source::File("aconfig_two.txt".to_string()), |
| 114 | FlagDeclaration { |
| 115 | name: "my_flag_two".to_string(), |
Mårten Kongstad | 066575b | 2023-06-07 16:29:25 +0200 | [diff] [blame^] | 116 | namespace: "ns".to_string(), |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 117 | description: "buildtime enable".to_string(), |
| 118 | }, |
| 119 | ) |
Mårten Kongstad | 2f95444 | 2023-05-17 16:51:16 +0200 | [diff] [blame] | 120 | .unwrap() |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 121 | .add_flag_value( |
| 122 | Source::Memory, |
| 123 | FlagValue { |
Mårten Kongstad | 9fb5896 | 2023-05-31 13:02:13 +0200 | [diff] [blame] | 124 | package: package.to_string(), |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 125 | name: "my_flag_two".to_string(), |
| 126 | state: FlagState::Enabled, |
| 127 | permission: Permission::ReadOnly, |
| 128 | }, |
| 129 | ) |
| 130 | .unwrap(); |
Mårten Kongstad | 2f95444 | 2023-05-17 16:51:16 +0200 | [diff] [blame] | 131 | let cache = builder.build(); |
Mårten Kongstad | fbd71e2 | 2023-05-31 13:29:35 +0200 | [diff] [blame] | 132 | let expect_content = r#"#ifndef com_example_HEADER_H |
| 133 | #define com_example_HEADER_H |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 134 | |
Mårten Kongstad | fbd71e2 | 2023-05-31 13:29:35 +0200 | [diff] [blame] | 135 | namespace com::example { |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 136 | |
| 137 | class my_flag_one { |
| 138 | public: |
| 139 | virtual const bool value() { |
| 140 | return false; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | class my_flag_two { |
| 145 | public: |
| 146 | virtual const bool value() { |
| 147 | return true; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | } |
| 152 | #endif |
| 153 | "#; |
| 154 | let file = generate_cpp_code(&cache).unwrap(); |
Mårten Kongstad | fbd71e2 | 2023-05-31 13:29:35 +0200 | [diff] [blame] | 155 | assert_eq!("aconfig/com_example.h", file.path.to_str().unwrap()); |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 156 | assert_eq!( |
| 157 | expect_content.replace(' ', ""), |
| 158 | String::from_utf8(file.contents).unwrap().replace(' ', "") |
| 159 | ); |
| 160 | } |
| 161 | |
| 162 | #[test] |
| 163 | fn test_cpp_codegen_runtime_flag() { |
Mårten Kongstad | fbd71e2 | 2023-05-31 13:29:35 +0200 | [diff] [blame] | 164 | let package = "com.example"; |
Mårten Kongstad | 9fb5896 | 2023-05-31 13:02:13 +0200 | [diff] [blame] | 165 | let mut builder = CacheBuilder::new(package.to_string()).unwrap(); |
Mårten Kongstad | 2f95444 | 2023-05-17 16:51:16 +0200 | [diff] [blame] | 166 | builder |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 167 | .add_flag_declaration( |
| 168 | Source::File("aconfig_one.txt".to_string()), |
| 169 | FlagDeclaration { |
| 170 | name: "my_flag_one".to_string(), |
Mårten Kongstad | 066575b | 2023-06-07 16:29:25 +0200 | [diff] [blame^] | 171 | namespace: "ns".to_string(), |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 172 | description: "buildtime disable".to_string(), |
| 173 | }, |
| 174 | ) |
Mårten Kongstad | 2f95444 | 2023-05-17 16:51:16 +0200 | [diff] [blame] | 175 | .unwrap() |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 176 | .add_flag_declaration( |
| 177 | Source::File("aconfig_two.txt".to_string()), |
| 178 | FlagDeclaration { |
| 179 | name: "my_flag_two".to_string(), |
Mårten Kongstad | 066575b | 2023-06-07 16:29:25 +0200 | [diff] [blame^] | 180 | namespace: "ns".to_string(), |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 181 | description: "runtime enable".to_string(), |
| 182 | }, |
| 183 | ) |
Mårten Kongstad | 2f95444 | 2023-05-17 16:51:16 +0200 | [diff] [blame] | 184 | .unwrap() |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 185 | .add_flag_value( |
| 186 | Source::Memory, |
| 187 | FlagValue { |
Mårten Kongstad | 9fb5896 | 2023-05-31 13:02:13 +0200 | [diff] [blame] | 188 | package: package.to_string(), |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 189 | name: "my_flag_two".to_string(), |
| 190 | state: FlagState::Enabled, |
| 191 | permission: Permission::ReadWrite, |
| 192 | }, |
| 193 | ) |
| 194 | .unwrap(); |
Mårten Kongstad | 2f95444 | 2023-05-17 16:51:16 +0200 | [diff] [blame] | 195 | let cache = builder.build(); |
Mårten Kongstad | fbd71e2 | 2023-05-31 13:29:35 +0200 | [diff] [blame] | 196 | let expect_content = r#"#ifndef com_example_HEADER_H |
| 197 | #define com_example_HEADER_H |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 198 | |
| 199 | #include <server_configurable_flags/get_flags.h> |
| 200 | using namespace server_configurable_flags; |
| 201 | |
Mårten Kongstad | fbd71e2 | 2023-05-31 13:29:35 +0200 | [diff] [blame] | 202 | namespace com::example { |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 203 | |
| 204 | class my_flag_one { |
| 205 | public: |
| 206 | virtual const bool value() { |
| 207 | return GetServerConfigurableFlag( |
Mårten Kongstad | 066575b | 2023-06-07 16:29:25 +0200 | [diff] [blame^] | 208 | "ns", |
| 209 | "com.example.my_flag_one", |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 210 | "false") == "true"; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | class my_flag_two { |
| 215 | public: |
| 216 | virtual const bool value() { |
| 217 | return GetServerConfigurableFlag( |
Mårten Kongstad | 066575b | 2023-06-07 16:29:25 +0200 | [diff] [blame^] | 218 | "ns", |
| 219 | "com.example.my_flag_two", |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 220 | "true") == "true"; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | } |
| 225 | #endif |
| 226 | "#; |
| 227 | let file = generate_cpp_code(&cache).unwrap(); |
Mårten Kongstad | fbd71e2 | 2023-05-31 13:29:35 +0200 | [diff] [blame] | 228 | assert_eq!("aconfig/com_example.h", file.path.to_str().unwrap()); |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 229 | assert_eq!( |
| 230 | expect_content.replace(' ', ""), |
| 231 | String::from_utf8(file.contents).unwrap().replace(' ', "") |
| 232 | ); |
| 233 | } |
| 234 | } |