blob: 2944e8a2aa0ee5639b3b688b8feb6cf1eb8db313 [file] [log] [blame]
Dennis Shen1dc9ad42023-05-12 00:21:55 +00001/*
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 Kongstadfbd71e22023-05-31 13:29:35 +020017use anyhow::{ensure, Result};
Dennis Shen1dc9ad42023-05-12 00:21:55 +000018use serde::Serialize;
19use tinytemplate::TinyTemplate;
20
Mårten Kongstadfbd71e22023-05-31 13:29:35 +020021use crate::codegen;
Dennis Shen1dc9ad42023-05-12 00:21:55 +000022use crate::commands::OutputFile;
Mårten Kongstad403658f2023-06-14 09:51:56 +020023use crate::protos::{ProtoFlagPermission, ProtoFlagState, ProtoParsedFlag};
Dennis Shen1dc9ad42023-05-12 00:21:55 +000024
Mårten Kongstad403658f2023-06-14 09:51:56 +020025pub fn generate_cpp_code<'a, I>(package: &str, parsed_flags_iter: I) -> Result<OutputFile>
26where
27 I: Iterator<Item = &'a ProtoParsedFlag>,
28{
Mårten Kongstad066575b2023-06-07 16:29:25 +020029 let class_elements: Vec<ClassElement> =
Mårten Kongstad403658f2023-06-14 09:51:56 +020030 parsed_flags_iter.map(|pf| create_class_element(package, pf)).collect();
Dennis Shen1dc9ad42023-05-12 00:21:55 +000031 let readwrite = class_elements.iter().any(|item| item.readwrite);
Mårten Kongstadfbd71e22023-05-31 13:29:35 +020032 let header = package.replace('.', "_");
33 let cpp_namespace = package.replace('.', "::");
34 ensure!(codegen::is_valid_name_ident(&header));
Mårten Kongstad066575b2023-06-07 16:29:25 +020035 let context = Context {
36 header: header.clone(),
37 cpp_namespace,
38 package: package.to_string(),
39 readwrite,
40 class_elements,
41 };
Dennis Shen1dc9ad42023-05-12 00:21:55 +000042 let mut template = TinyTemplate::new();
43 template.add_template("cpp_code_gen", include_str!("../templates/cpp.template"))?;
44 let contents = template.render("cpp_code_gen", &context)?;
Mårten Kongstadfbd71e22023-05-31 13:29:35 +020045 let path = ["aconfig", &(header + ".h")].iter().collect();
Dennis Shen1dc9ad42023-05-12 00:21:55 +000046 Ok(OutputFile { contents: contents.into(), path })
47}
48
49#[derive(Serialize)]
50struct Context {
Mårten Kongstadfbd71e22023-05-31 13:29:35 +020051 pub header: String,
52 pub cpp_namespace: String,
Mårten Kongstad9fb58962023-05-31 13:02:13 +020053 pub package: String,
Dennis Shen1dc9ad42023-05-12 00:21:55 +000054 pub readwrite: bool,
55 pub class_elements: Vec<ClassElement>,
56}
57
58#[derive(Serialize)]
59struct ClassElement {
60 pub readwrite: bool,
61 pub default_value: String,
62 pub flag_name: String,
Mårten Kongstad066575b2023-06-07 16:29:25 +020063 pub device_config_namespace: String,
64 pub device_config_flag: String,
Dennis Shen1dc9ad42023-05-12 00:21:55 +000065}
66
Mårten Kongstad403658f2023-06-14 09:51:56 +020067fn create_class_element(package: &str, pf: &ProtoParsedFlag) -> ClassElement {
Dennis Shen1dc9ad42023-05-12 00:21:55 +000068 ClassElement {
Mårten Kongstad403658f2023-06-14 09:51:56 +020069 readwrite: pf.permission() == ProtoFlagPermission::READ_WRITE,
70 default_value: if pf.state() == ProtoFlagState::ENABLED {
Dennis Shen1dc9ad42023-05-12 00:21:55 +000071 "true".to_string()
72 } else {
73 "false".to_string()
74 },
Mårten Kongstad403658f2023-06-14 09:51:56 +020075 flag_name: pf.name().to_string(),
76 device_config_namespace: pf.namespace().to_string(),
77 device_config_flag: codegen::create_device_config_ident(package, pf.name())
78 .expect("values checked at flag parse time"),
Dennis Shen1dc9ad42023-05-12 00:21:55 +000079 }
80}
81
82#[cfg(test)]
83mod tests {
84 use super::*;
Dennis Shen1dc9ad42023-05-12 00:21:55 +000085
86 #[test]
Mårten Kongstad403658f2023-06-14 09:51:56 +020087 fn test_generate_cpp_code() {
88 let parsed_flags = crate::test::parse_test_flags();
89 let generated =
90 generate_cpp_code(crate::test::TEST_PACKAGE, parsed_flags.parsed_flag.iter()).unwrap();
91 assert_eq!("aconfig/com_android_aconfig_test.h", format!("{}", generated.path.display()));
92 let expected = r#"
93#ifndef com_android_aconfig_test_HEADER_H
94#define com_android_aconfig_test_HEADER_H
95#include <server_configurable_flags/get_flags.h>
Dennis Shen1dc9ad42023-05-12 00:21:55 +000096
Mårten Kongstad403658f2023-06-14 09:51:56 +020097using namespace server_configurable_flags;
Dennis Shen1dc9ad42023-05-12 00:21:55 +000098
Mårten Kongstad403658f2023-06-14 09:51:56 +020099namespace com::android::aconfig::test {
100 static const bool disabled_ro() {
101 return false;
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000102 }
103
Mårten Kongstad403658f2023-06-14 09:51:56 +0200104 static const bool disabled_rw() {
105 return GetServerConfigurableFlag(
106 "aconfig_test",
107 "com.android.aconfig.test.disabled_rw",
108 "false") == "true";
109 }
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000110
Mårten Kongstad403658f2023-06-14 09:51:56 +0200111 static const bool enabled_ro() {
112 return true;
113 }
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000114
Mårten Kongstad403658f2023-06-14 09:51:56 +0200115 static const bool enabled_rw() {
116 return GetServerConfigurableFlag(
117 "aconfig_test",
118 "com.android.aconfig.test.enabled_rw",
119 "true") == "true";
120 }
121}
122#endif
123"#;
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000124 assert_eq!(
Mårten Kongstadb0255072023-06-08 10:15:43 +0200125 None,
126 crate::test::first_significant_code_diff(
Mårten Kongstad403658f2023-06-14 09:51:56 +0200127 expected,
128 &String::from_utf8(generated.contents).unwrap()
Mårten Kongstadb0255072023-06-08 10:15:43 +0200129 )
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000130 );
131 }
132}