Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 1 | // Copyright 2021 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 | |
| 15 | package bp2build |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
| 19 | "android/soong/cc" |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 20 | "android/soong/genrule" |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 21 | "fmt" |
| 22 | "strings" |
| 23 | "testing" |
| 24 | ) |
| 25 | |
| 26 | const ( |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 27 | ccBinaryTypePlaceHolder = "{rule_name}" |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 28 | ) |
| 29 | |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 30 | type testBazelTarget struct { |
| 31 | typ string |
| 32 | name string |
| 33 | attrs attrNameToString |
| 34 | } |
| 35 | |
| 36 | func generateBazelTargetsForTest(targets []testBazelTarget) []string { |
| 37 | ret := make([]string, 0, len(targets)) |
| 38 | for _, t := range targets { |
| 39 | ret = append(ret, makeBazelTarget(t.typ, t.name, t.attrs)) |
| 40 | } |
| 41 | return ret |
| 42 | } |
| 43 | |
| 44 | type ccBinaryBp2buildTestCase struct { |
| 45 | description string |
| 46 | blueprint string |
| 47 | targets []testBazelTarget |
| 48 | } |
| 49 | |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 50 | func registerCcBinaryModuleTypes(ctx android.RegistrationContext) { |
| 51 | cc.RegisterCCBuildComponents(ctx) |
| 52 | ctx.RegisterModuleType("filegroup", android.FileGroupFactory) |
| 53 | ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory) |
| 54 | ctx.RegisterModuleType("cc_library", cc.LibraryFactory) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 55 | ctx.RegisterModuleType("genrule", genrule.GenRuleFactory) |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 56 | } |
| 57 | |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 58 | var binaryReplacer = strings.NewReplacer(ccBinaryTypePlaceHolder, "cc_binary") |
| 59 | var hostBinaryReplacer = strings.NewReplacer(ccBinaryTypePlaceHolder, "cc_binary_host") |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 60 | |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 61 | func runCcBinaryTests(t *testing.T, tc ccBinaryBp2buildTestCase) { |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 62 | t.Helper() |
| 63 | runCcBinaryTestCase(t, tc) |
| 64 | runCcHostBinaryTestCase(t, tc) |
| 65 | } |
| 66 | |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 67 | func runCcBinaryTestCase(t *testing.T, tc ccBinaryBp2buildTestCase) { |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 68 | t.Helper() |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 69 | moduleTypeUnderTest := "cc_binary" |
| 70 | testCase := bp2buildTestCase{ |
| 71 | expectedBazelTargets: generateBazelTargetsForTest(tc.targets), |
| 72 | moduleTypeUnderTest: moduleTypeUnderTest, |
| 73 | moduleTypeUnderTestFactory: cc.BinaryFactory, |
| 74 | moduleTypeUnderTestBp2BuildMutator: cc.BinaryBp2build, |
| 75 | description: fmt.Sprintf("%s %s", moduleTypeUnderTest, tc.description), |
| 76 | blueprint: binaryReplacer.Replace(tc.blueprint), |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 77 | } |
| 78 | t.Run(testCase.description, func(t *testing.T) { |
| 79 | runBp2BuildTestCase(t, registerCcBinaryModuleTypes, testCase) |
| 80 | }) |
| 81 | } |
| 82 | |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 83 | func runCcHostBinaryTestCase(t *testing.T, tc ccBinaryBp2buildTestCase) { |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 84 | t.Helper() |
| 85 | testCase := tc |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 86 | for i, tar := range testCase.targets { |
| 87 | if tar.typ != "cc_binary" { |
| 88 | continue |
| 89 | } |
| 90 | tar.attrs["target_compatible_with"] = `select({ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 91 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 92 | "//conditions:default": [], |
| 93 | })` |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 94 | testCase.targets[i] = tar |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 95 | } |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 96 | moduleTypeUnderTest := "cc_binary_host" |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 97 | t.Run(testCase.description, func(t *testing.T) { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 98 | runBp2BuildTestCase(t, registerCcBinaryModuleTypes, bp2buildTestCase{ |
| 99 | expectedBazelTargets: generateBazelTargetsForTest(testCase.targets), |
| 100 | moduleTypeUnderTest: moduleTypeUnderTest, |
| 101 | moduleTypeUnderTestFactory: cc.BinaryHostFactory, |
| 102 | moduleTypeUnderTestBp2BuildMutator: cc.BinaryHostBp2build, |
| 103 | description: fmt.Sprintf("%s %s", moduleTypeUnderTest, tc.description), |
| 104 | blueprint: hostBinaryReplacer.Replace(testCase.blueprint), |
| 105 | }) |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 106 | }) |
| 107 | } |
| 108 | |
| 109 | func TestBasicCcBinary(t *testing.T) { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 110 | runCcBinaryTests(t, ccBinaryBp2buildTestCase{ |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 111 | description: "basic -- properties -> attrs with little/no transformation", |
| 112 | blueprint: ` |
| 113 | {rule_name} { |
| 114 | name: "foo", |
| 115 | srcs: ["a.cc"], |
| 116 | local_include_dirs: ["dir"], |
| 117 | include_dirs: ["absolute_dir"], |
| 118 | cflags: ["-Dcopt"], |
| 119 | cppflags: ["-Dcppflag"], |
| 120 | conlyflags: ["-Dconlyflag"], |
| 121 | asflags: ["-Dasflag"], |
| 122 | ldflags: ["ld-flag"], |
| 123 | rtti: true, |
| 124 | strip: { |
| 125 | all: true, |
| 126 | keep_symbols: true, |
| 127 | keep_symbols_and_debug_frame: true, |
| 128 | keep_symbols_list: ["symbol"], |
| 129 | none: true, |
| 130 | }, |
| 131 | } |
| 132 | `, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 133 | targets: []testBazelTarget{ |
| 134 | {"cc_binary", "foo", attrNameToString{ |
| 135 | "absolute_includes": `["absolute_dir"]`, |
| 136 | "asflags": `["-Dasflag"]`, |
| 137 | "conlyflags": `["-Dconlyflag"]`, |
| 138 | "copts": `["-Dcopt"]`, |
| 139 | "cppflags": `["-Dcppflag"]`, |
| 140 | "linkopts": `["ld-flag"]`, |
| 141 | "local_includes": `[ |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 142 | "dir", |
| 143 | ".", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 144 | ]`, |
| 145 | "rtti": `True`, |
| 146 | "srcs": `["a.cc"]`, |
| 147 | "strip": `{ |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 148 | "all": True, |
| 149 | "keep_symbols": True, |
| 150 | "keep_symbols_and_debug_frame": True, |
| 151 | "keep_symbols_list": ["symbol"], |
| 152 | "none": True, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 153 | }`, |
| 154 | }, |
| 155 | }, |
| 156 | }, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 157 | }) |
| 158 | } |
| 159 | |
| 160 | func TestCcBinaryWithSharedLdflagDisableFeature(t *testing.T) { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 161 | runCcBinaryTests(t, ccBinaryBp2buildTestCase{ |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 162 | description: `ldflag "-shared" disables static_flag feature`, |
| 163 | blueprint: ` |
| 164 | {rule_name} { |
| 165 | name: "foo", |
| 166 | ldflags: ["-shared"], |
| 167 | include_build_directory: false, |
| 168 | } |
| 169 | `, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 170 | targets: []testBazelTarget{ |
| 171 | {"cc_binary", "foo", attrNameToString{ |
| 172 | "features": `["-static_flag"]`, |
| 173 | "linkopts": `["-shared"]`, |
| 174 | }, |
| 175 | }, |
| 176 | }, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 177 | }) |
| 178 | } |
| 179 | |
| 180 | func TestCcBinaryWithLinkStatic(t *testing.T) { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 181 | runCcBinaryTests(t, ccBinaryBp2buildTestCase{ |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 182 | description: "link static", |
| 183 | blueprint: ` |
| 184 | {rule_name} { |
| 185 | name: "foo", |
| 186 | static_executable: true, |
| 187 | include_build_directory: false, |
| 188 | } |
| 189 | `, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 190 | targets: []testBazelTarget{ |
| 191 | {"cc_binary", "foo", attrNameToString{ |
| 192 | "linkshared": `False`, |
| 193 | }, |
| 194 | }, |
| 195 | }, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 196 | }) |
| 197 | } |
| 198 | |
| 199 | func TestCcBinaryVersionScript(t *testing.T) { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 200 | runCcBinaryTests(t, ccBinaryBp2buildTestCase{ |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 201 | description: `version script`, |
| 202 | blueprint: ` |
| 203 | {rule_name} { |
| 204 | name: "foo", |
| 205 | include_build_directory: false, |
| 206 | version_script: "vs", |
| 207 | } |
| 208 | `, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 209 | targets: []testBazelTarget{ |
| 210 | {"cc_binary", "foo", attrNameToString{ |
| 211 | "additional_linker_inputs": `["vs"]`, |
| 212 | "linkopts": `["-Wl,--version-script,$(location vs)"]`, |
| 213 | }, |
| 214 | }, |
| 215 | }, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 216 | }) |
| 217 | } |
| 218 | |
| 219 | func TestCcBinarySplitSrcsByLang(t *testing.T) { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 220 | runCcHostBinaryTestCase(t, ccBinaryBp2buildTestCase{ |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 221 | description: "split srcs by lang", |
| 222 | blueprint: ` |
| 223 | {rule_name} { |
| 224 | name: "foo", |
| 225 | srcs: [ |
| 226 | "asonly.S", |
| 227 | "conly.c", |
| 228 | "cpponly.cpp", |
| 229 | ":fg_foo", |
| 230 | ], |
| 231 | include_build_directory: false, |
| 232 | } |
| 233 | ` + simpleModuleDoNotConvertBp2build("filegroup", "fg_foo"), |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 234 | targets: []testBazelTarget{ |
| 235 | {"cc_binary", "foo", attrNameToString{ |
| 236 | "srcs": `[ |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 237 | "cpponly.cpp", |
| 238 | ":fg_foo_cpp_srcs", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 239 | ]`, |
| 240 | "srcs_as": `[ |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 241 | "asonly.S", |
| 242 | ":fg_foo_as_srcs", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 243 | ]`, |
| 244 | "srcs_c": `[ |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 245 | "conly.c", |
| 246 | ":fg_foo_c_srcs", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 247 | ]`, |
| 248 | }, |
| 249 | }, |
| 250 | }, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 251 | }) |
| 252 | } |
| 253 | |
| 254 | func TestCcBinaryDoNotDistinguishBetweenDepsAndImplementationDeps(t *testing.T) { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 255 | runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{ |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 256 | description: "no implementation deps", |
| 257 | blueprint: ` |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 258 | genrule { |
| 259 | name: "generated_hdr", |
| 260 | cmd: "nothing to see here", |
| 261 | } |
| 262 | |
| 263 | genrule { |
| 264 | name: "export_generated_hdr", |
| 265 | cmd: "nothing to see here", |
| 266 | } |
| 267 | |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 268 | {rule_name} { |
| 269 | name: "foo", |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 270 | srcs: ["foo.cpp"], |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 271 | shared_libs: ["implementation_shared_dep", "shared_dep"], |
| 272 | export_shared_lib_headers: ["shared_dep"], |
| 273 | static_libs: ["implementation_static_dep", "static_dep"], |
| 274 | export_static_lib_headers: ["static_dep", "whole_static_dep"], |
| 275 | whole_static_libs: ["not_explicitly_exported_whole_static_dep", "whole_static_dep"], |
| 276 | include_build_directory: false, |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 277 | generated_headers: ["generated_hdr", "export_generated_hdr"], |
| 278 | export_generated_headers: ["export_generated_hdr"], |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 279 | } |
| 280 | ` + |
| 281 | simpleModuleDoNotConvertBp2build("cc_library_static", "static_dep") + |
| 282 | simpleModuleDoNotConvertBp2build("cc_library_static", "implementation_static_dep") + |
| 283 | simpleModuleDoNotConvertBp2build("cc_library_static", "whole_static_dep") + |
| 284 | simpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep") + |
| 285 | simpleModuleDoNotConvertBp2build("cc_library", "shared_dep") + |
| 286 | simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep"), |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 287 | targets: []testBazelTarget{ |
| 288 | {"cc_binary", "foo", attrNameToString{ |
| 289 | "deps": `[ |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 290 | ":implementation_static_dep", |
| 291 | ":static_dep", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 292 | ]`, |
| 293 | "dynamic_deps": `[ |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 294 | ":implementation_shared_dep", |
| 295 | ":shared_dep", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 296 | ]`, |
| 297 | "srcs": `[ |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 298 | "foo.cpp", |
| 299 | ":generated_hdr", |
| 300 | ":export_generated_hdr", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 301 | ]`, |
| 302 | "whole_archive_deps": `[ |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 303 | ":not_explicitly_exported_whole_static_dep", |
| 304 | ":whole_static_dep", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 305 | ]`, |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 306 | "local_includes": `["."]`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 307 | }, |
| 308 | }, |
| 309 | }, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 310 | }) |
| 311 | } |
| 312 | |
| 313 | func TestCcBinaryNocrtTests(t *testing.T) { |
| 314 | baseTestCases := []struct { |
| 315 | description string |
| 316 | soongProperty string |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 317 | bazelAttr attrNameToString |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 318 | }{ |
| 319 | { |
| 320 | description: "nocrt: true", |
| 321 | soongProperty: `nocrt: true,`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 322 | bazelAttr: attrNameToString{"link_crt": `False`}, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 323 | }, |
| 324 | { |
| 325 | description: "nocrt: false", |
| 326 | soongProperty: `nocrt: false,`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 327 | bazelAttr: attrNameToString{}, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 328 | }, |
| 329 | { |
| 330 | description: "nocrt: not set", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 331 | bazelAttr: attrNameToString{}, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 332 | }, |
| 333 | } |
| 334 | |
| 335 | baseBlueprint := `{rule_name} { |
| 336 | name: "foo",%s |
| 337 | include_build_directory: false, |
| 338 | } |
| 339 | ` |
| 340 | |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 341 | for _, btc := range baseTestCases { |
| 342 | prop := btc.soongProperty |
| 343 | if len(prop) > 0 { |
| 344 | prop = "\n" + prop |
| 345 | } |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 346 | runCcBinaryTests(t, ccBinaryBp2buildTestCase{ |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 347 | description: btc.description, |
| 348 | blueprint: fmt.Sprintf(baseBlueprint, prop), |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 349 | targets: []testBazelTarget{ |
| 350 | {"cc_binary", "foo", btc.bazelAttr}, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 351 | }, |
| 352 | }) |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | func TestCcBinaryNo_libcrtTests(t *testing.T) { |
| 357 | baseTestCases := []struct { |
| 358 | description string |
| 359 | soongProperty string |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 360 | bazelAttr attrNameToString |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 361 | }{ |
| 362 | { |
| 363 | description: "no_libcrt: true", |
| 364 | soongProperty: `no_libcrt: true,`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 365 | bazelAttr: attrNameToString{"use_libcrt": `False`}, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 366 | }, |
| 367 | { |
| 368 | description: "no_libcrt: false", |
| 369 | soongProperty: `no_libcrt: false,`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 370 | bazelAttr: attrNameToString{"use_libcrt": `True`}, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 371 | }, |
| 372 | { |
| 373 | description: "no_libcrt: not set", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 374 | bazelAttr: attrNameToString{}, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 375 | }, |
| 376 | } |
| 377 | |
| 378 | baseBlueprint := `{rule_name} { |
| 379 | name: "foo",%s |
| 380 | include_build_directory: false, |
| 381 | } |
| 382 | ` |
| 383 | |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 384 | for _, btc := range baseTestCases { |
| 385 | prop := btc.soongProperty |
| 386 | if len(prop) > 0 { |
| 387 | prop = "\n" + prop |
| 388 | } |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 389 | runCcBinaryTests(t, ccBinaryBp2buildTestCase{ |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 390 | description: btc.description, |
| 391 | blueprint: fmt.Sprintf(baseBlueprint, prop), |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 392 | targets: []testBazelTarget{ |
| 393 | {"cc_binary", "foo", btc.bazelAttr}, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 394 | }, |
| 395 | }) |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | func TestCcBinaryPropertiesToFeatures(t *testing.T) { |
| 400 | baseTestCases := []struct { |
| 401 | description string |
| 402 | soongProperty string |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 403 | bazelAttr attrNameToString |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 404 | }{ |
| 405 | { |
| 406 | description: "pack_relocation: true", |
| 407 | soongProperty: `pack_relocations: true,`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 408 | bazelAttr: attrNameToString{}, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 409 | }, |
| 410 | { |
| 411 | description: "pack_relocations: false", |
| 412 | soongProperty: `pack_relocations: false,`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 413 | bazelAttr: attrNameToString{"features": `["disable_pack_relocations"]`}, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 414 | }, |
| 415 | { |
| 416 | description: "pack_relocations: not set", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 417 | bazelAttr: attrNameToString{}, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 418 | }, |
| 419 | { |
| 420 | description: "pack_relocation: true", |
| 421 | soongProperty: `allow_undefined_symbols: true,`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 422 | bazelAttr: attrNameToString{"features": `["-no_undefined_symbols"]`}, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 423 | }, |
| 424 | { |
| 425 | description: "allow_undefined_symbols: false", |
| 426 | soongProperty: `allow_undefined_symbols: false,`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 427 | bazelAttr: attrNameToString{}, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 428 | }, |
| 429 | { |
| 430 | description: "allow_undefined_symbols: not set", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 431 | bazelAttr: attrNameToString{}, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 432 | }, |
| 433 | } |
| 434 | |
| 435 | baseBlueprint := `{rule_name} { |
| 436 | name: "foo",%s |
| 437 | include_build_directory: false, |
| 438 | } |
| 439 | ` |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 440 | for _, btc := range baseTestCases { |
| 441 | prop := btc.soongProperty |
| 442 | if len(prop) > 0 { |
| 443 | prop = "\n" + prop |
| 444 | } |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 445 | runCcBinaryTests(t, ccBinaryBp2buildTestCase{ |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 446 | description: btc.description, |
| 447 | blueprint: fmt.Sprintf(baseBlueprint, prop), |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 448 | targets: []testBazelTarget{ |
| 449 | {"cc_binary", "foo", btc.bazelAttr}, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 450 | }, |
| 451 | }) |
| 452 | } |
| 453 | } |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 454 | |
| 455 | func TestCcBinarySharedProto(t *testing.T) { |
| 456 | runCcBinaryTests(t, ccBinaryBp2buildTestCase{ |
| 457 | blueprint: soongCcProtoLibraries + `{rule_name} { |
| 458 | name: "foo", |
| 459 | srcs: ["foo.proto"], |
| 460 | proto: { |
| 461 | canonical_path_from_root: false, |
| 462 | }, |
| 463 | include_build_directory: false, |
| 464 | }`, |
| 465 | targets: []testBazelTarget{ |
| 466 | {"proto_library", "foo_proto", attrNameToString{ |
| 467 | "srcs": `["foo.proto"]`, |
| 468 | }}, {"cc_lite_proto_library", "foo_cc_proto_lite", attrNameToString{ |
| 469 | "deps": `[":foo_proto"]`, |
| 470 | }}, {"cc_binary", "foo", attrNameToString{ |
| 471 | "dynamic_deps": `[":libprotobuf-cpp-lite"]`, |
| 472 | "whole_archive_deps": `[":foo_cc_proto_lite"]`, |
| 473 | }}, |
| 474 | }, |
| 475 | }) |
| 476 | } |
| 477 | |
| 478 | func TestCcBinaryStaticProto(t *testing.T) { |
| 479 | runCcBinaryTests(t, ccBinaryBp2buildTestCase{ |
| 480 | blueprint: soongCcProtoLibraries + `{rule_name} { |
| 481 | name: "foo", |
| 482 | srcs: ["foo.proto"], |
| 483 | static_executable: true, |
| 484 | proto: { |
| 485 | canonical_path_from_root: false, |
| 486 | }, |
| 487 | include_build_directory: false, |
| 488 | }`, |
| 489 | targets: []testBazelTarget{ |
| 490 | {"proto_library", "foo_proto", attrNameToString{ |
| 491 | "srcs": `["foo.proto"]`, |
| 492 | }}, {"cc_lite_proto_library", "foo_cc_proto_lite", attrNameToString{ |
| 493 | "deps": `[":foo_proto"]`, |
| 494 | }}, {"cc_binary", "foo", attrNameToString{ |
| 495 | "deps": `[":libprotobuf-cpp-lite"]`, |
| 496 | "whole_archive_deps": `[":foo_cc_proto_lite"]`, |
| 497 | "linkshared": `False`, |
| 498 | }}, |
| 499 | }, |
| 500 | }) |
| 501 | } |