Dennis Shen | c5e39f5 | 2023-09-14 18:52:49 +0000 | [diff] [blame] | 1 | // Copyright 2023 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 15 | package codegen |
Dennis Shen | c5e39f5 | 2023-09-14 18:52:49 +0000 | [diff] [blame] | 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "testing" |
| 20 | |
| 21 | "android/soong/android" |
| 22 | "android/soong/cc" |
Dennis Shen | c6dc551 | 2024-01-10 14:07:35 +0000 | [diff] [blame] | 23 | |
| 24 | "github.com/google/blueprint" |
Dennis Shen | c5e39f5 | 2023-09-14 18:52:49 +0000 | [diff] [blame] | 25 | ) |
| 26 | |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 27 | var ccCodegenModeTestData = []struct { |
Dennis Shen | c5e39f5 | 2023-09-14 18:52:49 +0000 | [diff] [blame] | 28 | setting, expected string |
| 29 | }{ |
| 30 | {"", "production"}, |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 31 | {"mode: `production`,", "production"}, |
| 32 | {"mode: `test`,", "test"}, |
| 33 | {"mode: `exported`,", "exported"}, |
Zhi Dou | 70e2124 | 2023-12-20 23:14:34 +0000 | [diff] [blame] | 34 | {"mode: `force-read-only`,", "force-read-only"}, |
Dennis Shen | c5e39f5 | 2023-09-14 18:52:49 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | func TestCCCodegenMode(t *testing.T) { |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 38 | for _, testData := range ccCodegenModeTestData { |
Dennis Shen | c5e39f5 | 2023-09-14 18:52:49 +0000 | [diff] [blame] | 39 | testCCCodegenModeHelper(t, testData.setting, testData.expected) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func testCCCodegenModeHelper(t *testing.T, bpMode string, ruleMode string) { |
| 44 | t.Helper() |
| 45 | result := android.GroupFixturePreparers( |
| 46 | PrepareForTestWithAconfigBuildComponents, |
| 47 | cc.PrepareForTestWithCcDefaultModules). |
| 48 | ExtendWithErrorHandler(android.FixtureExpectsNoErrors). |
| 49 | RunTestWithBp(t, fmt.Sprintf(` |
| 50 | aconfig_declarations { |
| 51 | name: "my_aconfig_declarations", |
| 52 | package: "com.example.package", |
Yu Liu | 315a53c | 2024-04-24 16:41:57 +0000 | [diff] [blame] | 53 | container: "com.android.foo", |
Dennis Shen | c5e39f5 | 2023-09-14 18:52:49 +0000 | [diff] [blame] | 54 | srcs: ["foo.aconfig"], |
| 55 | } |
| 56 | |
| 57 | cc_library { |
| 58 | name: "server_configurable_flags", |
| 59 | srcs: ["server_configurable_flags.cc"], |
| 60 | } |
| 61 | |
Ted Bauer | f0f1859 | 2024-04-23 18:25:26 +0000 | [diff] [blame] | 62 | cc_library { |
| 63 | name: "libbase", |
| 64 | srcs: ["libbase.cc"], |
| 65 | } |
| 66 | |
| 67 | cc_library { |
| 68 | name: "liblog", |
| 69 | srcs: ["liblog.cc"], |
| 70 | } |
| 71 | |
| 72 | cc_library { |
| 73 | name: "libaconfig_storage_read_api_cc", |
| 74 | srcs: ["libaconfig_storage_read_api_cc.cc"], |
| 75 | } |
| 76 | |
| 77 | cc_library { |
| 78 | name: "libaconfig_storage_protos_cc", |
| 79 | srcs: ["libaconfig_storage_protos_cc.cc"], |
| 80 | } |
| 81 | |
Dennis Shen | c5e39f5 | 2023-09-14 18:52:49 +0000 | [diff] [blame] | 82 | cc_aconfig_library { |
| 83 | name: "my_cc_aconfig_library", |
| 84 | aconfig_declarations: "my_aconfig_declarations", |
| 85 | %s |
| 86 | } |
| 87 | `, bpMode)) |
| 88 | |
| 89 | module := result.ModuleForTests("my_cc_aconfig_library", "android_arm64_armv8-a_shared") |
| 90 | rule := module.Rule("cc_aconfig_library") |
| 91 | android.AssertStringEquals(t, "rule must contain test mode", rule.Args["mode"], ruleMode) |
| 92 | } |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 93 | |
| 94 | var incorrectCCCodegenModeTestData = []struct { |
| 95 | setting, expectedErr string |
| 96 | }{ |
| 97 | {"mode: `unsupported`,", "mode: \"unsupported\" is not a supported mode"}, |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | func TestIncorrectCCCodegenMode(t *testing.T) { |
| 101 | for _, testData := range incorrectCCCodegenModeTestData { |
| 102 | testIncorrectCCCodegenModeHelper(t, testData.setting, testData.expectedErr) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | func testIncorrectCCCodegenModeHelper(t *testing.T, bpMode string, err string) { |
| 107 | t.Helper() |
| 108 | android.GroupFixturePreparers( |
| 109 | PrepareForTestWithAconfigBuildComponents, |
| 110 | cc.PrepareForTestWithCcDefaultModules). |
| 111 | ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(err)). |
| 112 | RunTestWithBp(t, fmt.Sprintf(` |
| 113 | aconfig_declarations { |
| 114 | name: "my_aconfig_declarations", |
| 115 | package: "com.example.package", |
Yu Liu | 315a53c | 2024-04-24 16:41:57 +0000 | [diff] [blame] | 116 | container: "com.android.foo", |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 117 | srcs: ["foo.aconfig"], |
| 118 | } |
| 119 | |
| 120 | cc_library { |
| 121 | name: "server_configurable_flags", |
| 122 | srcs: ["server_configurable_flags.cc"], |
| 123 | } |
| 124 | |
Ted Bauer | f0f1859 | 2024-04-23 18:25:26 +0000 | [diff] [blame] | 125 | cc_library { |
| 126 | name: "libbase", |
| 127 | srcs: ["libbase.cc"], |
| 128 | } |
| 129 | |
| 130 | cc_library { |
| 131 | name: "liblog", |
| 132 | srcs: ["liblog.cc"], |
| 133 | } |
| 134 | |
| 135 | cc_library { |
| 136 | name: "libaconfig_storage_read_api_cc", |
| 137 | srcs: ["libaconfig_storage_read_api_cc.cc"], |
| 138 | } |
| 139 | |
| 140 | cc_library { |
| 141 | name: "libaconfig_storage_protos_cc", |
| 142 | srcs: ["libaconfig_storage_protos_cc.cc"], |
| 143 | } |
| 144 | |
| 145 | |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 146 | cc_aconfig_library { |
| 147 | name: "my_cc_aconfig_library", |
| 148 | aconfig_declarations: "my_aconfig_declarations", |
| 149 | %s |
| 150 | } |
| 151 | `, bpMode)) |
| 152 | } |
Yu Liu | 6dc93f9 | 2023-12-14 01:19:35 +0000 | [diff] [blame] | 153 | |
| 154 | func TestAndroidMkCcLibrary(t *testing.T) { |
| 155 | bp := ` |
| 156 | aconfig_declarations { |
| 157 | name: "my_aconfig_declarations_foo", |
| 158 | package: "com.example.package", |
| 159 | srcs: ["foo.aconfig"], |
| 160 | container: "vendor", |
| 161 | } |
| 162 | |
| 163 | cc_aconfig_library { |
| 164 | name: "my_cc_aconfig_library_foo", |
| 165 | aconfig_declarations: "my_aconfig_declarations_foo", |
| 166 | vendor_available: true, |
| 167 | } |
| 168 | |
| 169 | aconfig_declarations { |
| 170 | name: "my_aconfig_declarations_bar", |
| 171 | package: "com.example.package", |
Yu Liu | 315a53c | 2024-04-24 16:41:57 +0000 | [diff] [blame] | 172 | container: "com.android.foo", |
Yu Liu | 6dc93f9 | 2023-12-14 01:19:35 +0000 | [diff] [blame] | 173 | srcs: ["bar.aconfig"], |
| 174 | } |
| 175 | |
| 176 | cc_aconfig_library { |
| 177 | name: "my_cc_aconfig_library_bar", |
| 178 | aconfig_declarations: "my_aconfig_declarations_bar", |
| 179 | vendor_available: true, |
| 180 | } |
| 181 | |
| 182 | cc_library { |
| 183 | name: "my_cc_library", |
| 184 | srcs: [ |
| 185 | "src/foo.cc", |
| 186 | ], |
| 187 | static_libs: [ |
| 188 | "my_cc_aconfig_library_foo", |
| 189 | "my_cc_aconfig_library_bar", |
| 190 | ], |
| 191 | vendor: true, |
| 192 | } |
| 193 | |
| 194 | cc_library { |
| 195 | name: "server_configurable_flags", |
| 196 | srcs: ["server_configurable_flags.cc"], |
Kiyoung Kim | b5fdb2e | 2024-01-03 14:24:34 +0900 | [diff] [blame] | 197 | vendor_available: true, |
Yu Liu | 6dc93f9 | 2023-12-14 01:19:35 +0000 | [diff] [blame] | 198 | } |
Ted Bauer | f0f1859 | 2024-04-23 18:25:26 +0000 | [diff] [blame] | 199 | |
| 200 | cc_library { |
| 201 | name: "libbase", |
| 202 | srcs: ["libbase.cc"], |
| 203 | vendor_available: true, |
| 204 | } |
| 205 | |
| 206 | cc_library { |
| 207 | name: "liblog", |
| 208 | srcs: ["liblog.cc"], |
| 209 | vendor_available: true, |
| 210 | } |
| 211 | |
| 212 | cc_library { |
| 213 | name: "libaconfig_storage_read_api_cc", |
| 214 | srcs: ["libaconfig_storage_read_api_cc.cc"], |
| 215 | vendor_available: true, |
| 216 | } |
| 217 | |
| 218 | cc_library { |
| 219 | name: "libaconfig_storage_protos_cc", |
| 220 | srcs: ["libaconfig_storage_protos_cc.cc"], |
| 221 | vendor_available: true, |
| 222 | } |
Yu Liu | 6dc93f9 | 2023-12-14 01:19:35 +0000 | [diff] [blame] | 223 | ` |
| 224 | result := android.GroupFixturePreparers( |
| 225 | PrepareForTestWithAconfigBuildComponents, |
| 226 | cc.PrepareForTestWithCcDefaultModules). |
| 227 | ExtendWithErrorHandler(android.FixtureExpectsNoErrors).RunTestWithBp(t, bp) |
| 228 | |
Kiyoung Kim | b5fdb2e | 2024-01-03 14:24:34 +0900 | [diff] [blame] | 229 | module := result.ModuleForTests("my_cc_library", "android_vendor_arm64_armv8-a_shared").Module() |
Yu Liu | 6dc93f9 | 2023-12-14 01:19:35 +0000 | [diff] [blame] | 230 | |
| 231 | entry := android.AndroidMkEntriesForTest(t, result.TestContext, module)[0] |
| 232 | |
| 233 | makeVar := entry.EntryMap["LOCAL_ACONFIG_FILES"] |
Yu Liu | 6dc93f9 | 2023-12-14 01:19:35 +0000 | [diff] [blame] | 234 | android.EnsureListContainsSuffix(t, makeVar, "my_aconfig_declarations_foo/intermediate.pb") |
| 235 | } |
Dennis Shen | c6dc551 | 2024-01-10 14:07:35 +0000 | [diff] [blame] | 236 | |
| 237 | func TestForceReadOnly(t *testing.T) { |
| 238 | t.Helper() |
| 239 | result := android.GroupFixturePreparers( |
| 240 | PrepareForTestWithAconfigBuildComponents, |
| 241 | cc.PrepareForTestWithCcDefaultModules). |
| 242 | ExtendWithErrorHandler(android.FixtureExpectsNoErrors). |
| 243 | RunTestWithBp(t, fmt.Sprintf(` |
| 244 | aconfig_declarations { |
| 245 | name: "my_aconfig_declarations", |
| 246 | package: "com.example.package", |
Yu Liu | 315a53c | 2024-04-24 16:41:57 +0000 | [diff] [blame] | 247 | container: "com.android.foo", |
Dennis Shen | c6dc551 | 2024-01-10 14:07:35 +0000 | [diff] [blame] | 248 | srcs: ["foo.aconfig"], |
| 249 | } |
| 250 | |
| 251 | cc_aconfig_library { |
| 252 | name: "my_cc_aconfig_library", |
| 253 | aconfig_declarations: "my_aconfig_declarations", |
| 254 | mode: "force-read-only", |
| 255 | } |
Ted Bauer | c3031c5 | 2024-05-01 00:30:17 +0000 | [diff] [blame] | 256 | |
| 257 | |
| 258 | cc_library { |
| 259 | name: "libbase", |
| 260 | srcs: ["libbase.cc"], |
| 261 | } |
| 262 | |
| 263 | cc_library { |
| 264 | name: "liblog", |
| 265 | srcs: ["liblog.cc"], |
| 266 | } |
| 267 | |
| 268 | cc_library { |
| 269 | name: "libaconfig_storage_read_api_cc", |
| 270 | srcs: ["libaconfig_storage_read_api_cc.cc"], |
| 271 | } |
Dennis Shen | c6dc551 | 2024-01-10 14:07:35 +0000 | [diff] [blame] | 272 | `)) |
| 273 | |
| 274 | module := result.ModuleForTests("my_cc_aconfig_library", "android_arm64_armv8-a_shared").Module() |
| 275 | dependOnBaseLib := false |
| 276 | result.VisitDirectDeps(module, func(dep blueprint.Module) { |
| 277 | if dep.Name() == baseLibDep { |
| 278 | dependOnBaseLib = true |
| 279 | } |
| 280 | }) |
| 281 | android.AssertBoolEquals(t, "should not have dependency on server_configuriable_flags", |
| 282 | dependOnBaseLib, false) |
| 283 | } |