| 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 |  | 
| Dennis Shen | c5e39f5 | 2023-09-14 18:52:49 +0000 | [diff] [blame] | 77 | 			cc_aconfig_library { | 
 | 78 | 				name: "my_cc_aconfig_library", | 
 | 79 | 				aconfig_declarations: "my_aconfig_declarations", | 
 | 80 | 				%s | 
 | 81 | 			} | 
 | 82 | 		`, bpMode)) | 
 | 83 |  | 
 | 84 | 	module := result.ModuleForTests("my_cc_aconfig_library", "android_arm64_armv8-a_shared") | 
 | 85 | 	rule := module.Rule("cc_aconfig_library") | 
 | 86 | 	android.AssertStringEquals(t, "rule must contain test mode", rule.Args["mode"], ruleMode) | 
 | 87 | } | 
| Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 88 |  | 
 | 89 | var incorrectCCCodegenModeTestData = []struct { | 
 | 90 | 	setting, expectedErr string | 
 | 91 | }{ | 
 | 92 | 	{"mode: `unsupported`,", "mode: \"unsupported\" is not a supported mode"}, | 
| Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 93 | } | 
 | 94 |  | 
 | 95 | func TestIncorrectCCCodegenMode(t *testing.T) { | 
 | 96 | 	for _, testData := range incorrectCCCodegenModeTestData { | 
 | 97 | 		testIncorrectCCCodegenModeHelper(t, testData.setting, testData.expectedErr) | 
 | 98 | 	} | 
 | 99 | } | 
 | 100 |  | 
 | 101 | func testIncorrectCCCodegenModeHelper(t *testing.T, bpMode string, err string) { | 
 | 102 | 	t.Helper() | 
 | 103 | 	android.GroupFixturePreparers( | 
 | 104 | 		PrepareForTestWithAconfigBuildComponents, | 
 | 105 | 		cc.PrepareForTestWithCcDefaultModules). | 
 | 106 | 		ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(err)). | 
 | 107 | 		RunTestWithBp(t, fmt.Sprintf(` | 
 | 108 | 			aconfig_declarations { | 
 | 109 | 				name: "my_aconfig_declarations", | 
 | 110 | 				package: "com.example.package", | 
| Yu Liu | 315a53c | 2024-04-24 16:41:57 +0000 | [diff] [blame] | 111 | 				container: "com.android.foo", | 
| Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 112 | 				srcs: ["foo.aconfig"], | 
 | 113 | 			} | 
 | 114 |  | 
 | 115 | 			cc_library { | 
 | 116 |     		name: "server_configurable_flags", | 
 | 117 |     		srcs: ["server_configurable_flags.cc"], | 
 | 118 | 			} | 
 | 119 |  | 
| Ted Bauer | f0f1859 | 2024-04-23 18:25:26 +0000 | [diff] [blame] | 120 | 			cc_library { | 
 | 121 |     		name: "libbase", | 
 | 122 |     		srcs: ["libbase.cc"], | 
 | 123 | 			} | 
 | 124 |  | 
 | 125 | 			cc_library { | 
 | 126 |     		name: "liblog", | 
 | 127 |     		srcs: ["liblog.cc"], | 
 | 128 | 			} | 
 | 129 |  | 
 | 130 | 			cc_library { | 
 | 131 |     		name: "libaconfig_storage_read_api_cc", | 
 | 132 |     		srcs: ["libaconfig_storage_read_api_cc.cc"], | 
 | 133 | 			} | 
 | 134 |  | 
| Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 135 | 			cc_aconfig_library { | 
 | 136 | 				name: "my_cc_aconfig_library", | 
 | 137 | 				aconfig_declarations: "my_aconfig_declarations", | 
 | 138 | 				%s | 
 | 139 | 			} | 
 | 140 | 		`, bpMode)) | 
 | 141 | } | 
| Yu Liu | 6dc93f9 | 2023-12-14 01:19:35 +0000 | [diff] [blame] | 142 |  | 
 | 143 | func TestAndroidMkCcLibrary(t *testing.T) { | 
 | 144 | 	bp := ` | 
 | 145 | 		aconfig_declarations { | 
 | 146 | 			name: "my_aconfig_declarations_foo", | 
 | 147 | 			package: "com.example.package", | 
 | 148 | 			srcs: ["foo.aconfig"], | 
 | 149 | 			container: "vendor", | 
 | 150 | 		} | 
 | 151 |  | 
 | 152 | 		cc_aconfig_library { | 
 | 153 | 			name: "my_cc_aconfig_library_foo", | 
 | 154 | 			aconfig_declarations: "my_aconfig_declarations_foo", | 
 | 155 | 			vendor_available: true, | 
 | 156 | 		} | 
 | 157 |  | 
 | 158 | 		aconfig_declarations { | 
 | 159 | 			name: "my_aconfig_declarations_bar", | 
 | 160 | 			package: "com.example.package", | 
| Yu Liu | 315a53c | 2024-04-24 16:41:57 +0000 | [diff] [blame] | 161 | 			container: "com.android.foo", | 
| Yu Liu | 6dc93f9 | 2023-12-14 01:19:35 +0000 | [diff] [blame] | 162 | 			srcs: ["bar.aconfig"], | 
 | 163 | 		} | 
 | 164 |  | 
 | 165 | 		cc_aconfig_library { | 
 | 166 | 			name: "my_cc_aconfig_library_bar", | 
 | 167 | 			aconfig_declarations: "my_aconfig_declarations_bar", | 
 | 168 | 			vendor_available: true, | 
 | 169 | 		} | 
 | 170 |  | 
 | 171 | 		cc_library { | 
 | 172 | 			name: "my_cc_library", | 
 | 173 | 			srcs: [ | 
 | 174 | 				"src/foo.cc", | 
 | 175 | 			], | 
 | 176 | 			static_libs: [ | 
 | 177 | 				"my_cc_aconfig_library_foo", | 
 | 178 | 				"my_cc_aconfig_library_bar", | 
 | 179 | 			], | 
 | 180 | 			vendor: true, | 
 | 181 | 		} | 
 | 182 |  | 
 | 183 | 		cc_library { | 
 | 184 | 			name: "server_configurable_flags", | 
 | 185 | 			srcs: ["server_configurable_flags.cc"], | 
| Kiyoung Kim | b5fdb2e | 2024-01-03 14:24:34 +0900 | [diff] [blame] | 186 | 			vendor_available: true, | 
| Yu Liu | 6dc93f9 | 2023-12-14 01:19:35 +0000 | [diff] [blame] | 187 | 		} | 
| Ted Bauer | f0f1859 | 2024-04-23 18:25:26 +0000 | [diff] [blame] | 188 |  | 
 | 189 | 		cc_library { | 
 | 190 | 			name: "libbase", | 
 | 191 | 			srcs: ["libbase.cc"], | 
 | 192 | 			vendor_available: true, | 
 | 193 | 		} | 
 | 194 |  | 
 | 195 | 		cc_library { | 
 | 196 | 			name: "liblog", | 
 | 197 | 			srcs: ["liblog.cc"], | 
 | 198 | 			vendor_available: true, | 
 | 199 | 		} | 
 | 200 |  | 
 | 201 | 		cc_library { | 
 | 202 | 			name: "libaconfig_storage_read_api_cc", | 
 | 203 | 			srcs: ["libaconfig_storage_read_api_cc.cc"], | 
 | 204 | 			vendor_available: true, | 
 | 205 | 		} | 
| Yu Liu | 6dc93f9 | 2023-12-14 01:19:35 +0000 | [diff] [blame] | 206 | 	` | 
 | 207 | 	result := android.GroupFixturePreparers( | 
 | 208 | 		PrepareForTestWithAconfigBuildComponents, | 
 | 209 | 		cc.PrepareForTestWithCcDefaultModules). | 
 | 210 | 		ExtendWithErrorHandler(android.FixtureExpectsNoErrors).RunTestWithBp(t, bp) | 
 | 211 |  | 
| Kiyoung Kim | b5fdb2e | 2024-01-03 14:24:34 +0900 | [diff] [blame] | 212 | 	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] | 213 |  | 
 | 214 | 	entry := android.AndroidMkEntriesForTest(t, result.TestContext, module)[0] | 
 | 215 |  | 
 | 216 | 	makeVar := entry.EntryMap["LOCAL_ACONFIG_FILES"] | 
| Yu Liu | 6dc93f9 | 2023-12-14 01:19:35 +0000 | [diff] [blame] | 217 | 	android.EnsureListContainsSuffix(t, makeVar, "my_aconfig_declarations_foo/intermediate.pb") | 
 | 218 | } | 
| Dennis Shen | c6dc551 | 2024-01-10 14:07:35 +0000 | [diff] [blame] | 219 |  | 
 | 220 | func TestForceReadOnly(t *testing.T) { | 
 | 221 | 	t.Helper() | 
 | 222 | 	result := android.GroupFixturePreparers( | 
 | 223 | 		PrepareForTestWithAconfigBuildComponents, | 
 | 224 | 		cc.PrepareForTestWithCcDefaultModules). | 
 | 225 | 		ExtendWithErrorHandler(android.FixtureExpectsNoErrors). | 
 | 226 | 		RunTestWithBp(t, fmt.Sprintf(` | 
 | 227 | 			aconfig_declarations { | 
 | 228 | 				name: "my_aconfig_declarations", | 
 | 229 | 				package: "com.example.package", | 
| Yu Liu | 315a53c | 2024-04-24 16:41:57 +0000 | [diff] [blame] | 230 | 				container: "com.android.foo", | 
| Dennis Shen | c6dc551 | 2024-01-10 14:07:35 +0000 | [diff] [blame] | 231 | 				srcs: ["foo.aconfig"], | 
 | 232 | 			} | 
 | 233 |  | 
 | 234 | 			cc_aconfig_library { | 
 | 235 | 				name: "my_cc_aconfig_library", | 
 | 236 | 				aconfig_declarations: "my_aconfig_declarations", | 
 | 237 | 				mode: "force-read-only", | 
 | 238 | 			} | 
| Ted Bauer | c3031c5 | 2024-05-01 00:30:17 +0000 | [diff] [blame] | 239 |  | 
 | 240 |  | 
 | 241 | 			cc_library { | 
 | 242 |     		                name: "libbase", | 
 | 243 |     		                srcs: ["libbase.cc"], | 
 | 244 | 			} | 
 | 245 |  | 
 | 246 | 			cc_library { | 
 | 247 |     		                name: "liblog", | 
 | 248 |     		                srcs: ["liblog.cc"], | 
 | 249 | 			} | 
 | 250 |  | 
 | 251 | 			cc_library { | 
 | 252 |     		                name: "libaconfig_storage_read_api_cc", | 
 | 253 |     		                srcs: ["libaconfig_storage_read_api_cc.cc"], | 
 | 254 | 			} | 
| Dennis Shen | c6dc551 | 2024-01-10 14:07:35 +0000 | [diff] [blame] | 255 | 		`)) | 
 | 256 |  | 
 | 257 | 	module := result.ModuleForTests("my_cc_aconfig_library", "android_arm64_armv8-a_shared").Module() | 
 | 258 | 	dependOnBaseLib := false | 
 | 259 | 	result.VisitDirectDeps(module, func(dep blueprint.Module) { | 
 | 260 | 		if dep.Name() == baseLibDep { | 
 | 261 | 			dependOnBaseLib = true | 
 | 262 | 		} | 
 | 263 | 	}) | 
 | 264 | 	android.AssertBoolEquals(t, "should not have dependency on server_configuriable_flags", | 
 | 265 | 		dependOnBaseLib, false) | 
 | 266 | } |