Merge "aconfig: add c codegen" into main am: afd8b2d75b am: 24ecb48d89 am: f91df0cd73 am: 008d729b86 am: 9c1a675518

Original change: https://android-review.googlesource.com/c/platform/build/+/2653700

Change-Id: Ie8b5f3c6f999892703530dd09e45e253c595c441
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/tools/aconfig/src/codegen_cpp.rs b/tools/aconfig/src/codegen_cpp.rs
index 8acac8e..979b8ad 100644
--- a/tools/aconfig/src/codegen_cpp.rs
+++ b/tools/aconfig/src/codegen_cpp.rs
@@ -67,6 +67,16 @@
             },
             dir: "",
         },
+        FileSpec {
+            name: &format!("{}_c.h", header),
+            template: include_str!("../templates/c_exported_header.template"),
+            dir: "include",
+        },
+        FileSpec {
+            name: &format!("{}_c.cc", header),
+            template: include_str!("../templates/c_source_file.template"),
+            dir: "",
+        },
     ];
     files.iter().map(|file| generate_file(file, &context)).collect()
 }
@@ -387,6 +397,71 @@
 }
 "#;
 
+    const C_EXPORTED_HEADER_EXPECTED: &str = r#"
+#ifndef com_android_aconfig_test_c_HEADER_H
+#define com_android_aconfig_test_c_HEADER_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern const char* com_android_aconfig_test_DISABLED_RO;
+extern const char* com_android_aconfig_test_DISABLED_RW;
+extern const char* com_android_aconfig_test_ENABLED_RO;
+extern const char* com_android_aconfig_test_ENABLED_RW;
+
+bool com_android_aconfig_test_disabled_ro();
+
+bool com_android_aconfig_test_disabled_rw();
+
+bool com_android_aconfig_test_enabled_ro();
+
+bool com_android_aconfig_test_enabled_rw();
+
+void com_android_aconfig_test_override_flag(const char* name, bool val);
+
+void com_android_aconfig_test_reset_overrides();
+
+#ifdef __cplusplus
+}
+#endif
+#endif
+"#;
+
+    const C_SOURCE_FILE_EXPECTED: &str = r#"
+#include "com_android_aconfig_test_c.h"
+#include "com_android_aconfig_test.h"
+#include <string>
+
+const char* com_android_aconfig_test_DISABLED_RO = "com.android.aconfig.test.disabled_ro";
+const char* com_android_aconfig_test_DISABLED_RW = "com.android.aconfig.test.disabled_rw";
+const char* com_android_aconfig_test_ENABLED_RO = "com.android.aconfig.test.enabled_ro";
+const char* com_android_aconfig_test_ENABLED_RW = "com.android.aconfig.test.enabled_rw";
+
+bool com_android_aconfig_test_disabled_ro() {
+    return com::android::aconfig::test::disabled_ro();
+}
+
+bool com_android_aconfig_test_disabled_rw() {
+    return com::android::aconfig::test::disabled_rw();
+}
+
+bool com_android_aconfig_test_enabled_ro() {
+    return com::android::aconfig::test::enabled_ro();
+}
+
+bool com_android_aconfig_test_enabled_rw() {
+    return com::android::aconfig::test::enabled_rw();
+}
+
+void com_android_aconfig_test_override_flag(const char* name, bool val) {
+    com::android::aconfig::test::override_flag(std::string(name), val);
+}
+
+void com_android_aconfig_test_reset_overrides() {
+    com::android::aconfig::test::reset_overrides();
+}
+"#;
     fn test_generate_cpp_code(mode: CodegenMode) {
         let parsed_flags = crate::test::parse_test_flags();
         let generated =
@@ -435,6 +510,26 @@
                 generated_files_map.get(&target_file_path).unwrap()
             )
         );
+
+        target_file_path = String::from("include/com_android_aconfig_test_c.h");
+        assert!(generated_files_map.contains_key(&target_file_path));
+        assert_eq!(
+            None,
+            crate::test::first_significant_code_diff(
+                C_EXPORTED_HEADER_EXPECTED,
+                generated_files_map.get(&target_file_path).unwrap()
+            )
+        );
+
+        target_file_path = String::from("com_android_aconfig_test_c.cc");
+        assert!(generated_files_map.contains_key(&target_file_path));
+        assert_eq!(
+            None,
+            crate::test::first_significant_code_diff(
+                C_SOURCE_FILE_EXPECTED,
+                generated_files_map.get(&target_file_path).unwrap()
+            )
+        );
     }
 
     #[test]
diff --git a/tools/aconfig/templates/c_exported_header.template b/tools/aconfig/templates/c_exported_header.template
new file mode 100644
index 0000000..08af860
--- /dev/null
+++ b/tools/aconfig/templates/c_exported_header.template
@@ -0,0 +1,22 @@
+#ifndef {header}_c_HEADER_H
+#define {header}_c_HEADER_H
+
+#ifdef __cplusplus
+extern "C" \{
+#endif
+
+{{ for item in class_elements}}
+extern const char* {header}_{item.uppercase_flag_name};
+{{ endfor -}}
+
+{{ for item in class_elements}}
+bool {header}_{item.flag_name}();{{ endfor }}
+
+void {header}_override_flag(const char* name, bool val);
+
+void {header}_reset_overrides();
+
+#ifdef __cplusplus
+}
+#endif
+#endif
diff --git a/tools/aconfig/templates/c_source_file.template b/tools/aconfig/templates/c_source_file.template
new file mode 100644
index 0000000..18da299
--- /dev/null
+++ b/tools/aconfig/templates/c_source_file.template
@@ -0,0 +1,21 @@
+#include "{header}_c.h"
+#include "{header}.h"
+#include <string>
+
+{{ for item in class_elements}}
+const char* {header}_{item.uppercase_flag_name} = "{item.device_config_flag}";
+{{ endfor - }}
+
+{{ for item in class_elements}}
+bool {header}_{item.flag_name}() \{
+    return {cpp_namespace}::{item.flag_name}();
+}
+{{ endfor }}
+
+void {header}_override_flag(const char* name, bool val) \{
+     {cpp_namespace}::override_flag(std::string(name), val);
+}
+
+void {header}_reset_overrides() \{
+     {cpp_namespace}::reset_overrides();
+}