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 | ]`, |
| 306 | }, |
| 307 | }, |
| 308 | }, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 309 | }) |
| 310 | } |
| 311 | |
| 312 | func TestCcBinaryNocrtTests(t *testing.T) { |
| 313 | baseTestCases := []struct { |
| 314 | description string |
| 315 | soongProperty string |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 316 | bazelAttr attrNameToString |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 317 | }{ |
| 318 | { |
| 319 | description: "nocrt: true", |
| 320 | soongProperty: `nocrt: true,`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 321 | bazelAttr: attrNameToString{"link_crt": `False`}, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 322 | }, |
| 323 | { |
| 324 | description: "nocrt: false", |
| 325 | soongProperty: `nocrt: false,`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 326 | bazelAttr: attrNameToString{}, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 327 | }, |
| 328 | { |
| 329 | description: "nocrt: not set", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 330 | bazelAttr: attrNameToString{}, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 331 | }, |
| 332 | } |
| 333 | |
| 334 | baseBlueprint := `{rule_name} { |
| 335 | name: "foo",%s |
| 336 | include_build_directory: false, |
| 337 | } |
| 338 | ` |
| 339 | |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 340 | for _, btc := range baseTestCases { |
| 341 | prop := btc.soongProperty |
| 342 | if len(prop) > 0 { |
| 343 | prop = "\n" + prop |
| 344 | } |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 345 | runCcBinaryTests(t, ccBinaryBp2buildTestCase{ |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 346 | description: btc.description, |
| 347 | blueprint: fmt.Sprintf(baseBlueprint, prop), |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 348 | targets: []testBazelTarget{ |
| 349 | {"cc_binary", "foo", btc.bazelAttr}, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 350 | }, |
| 351 | }) |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | func TestCcBinaryNo_libcrtTests(t *testing.T) { |
| 356 | baseTestCases := []struct { |
| 357 | description string |
| 358 | soongProperty string |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 359 | bazelAttr attrNameToString |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 360 | }{ |
| 361 | { |
| 362 | description: "no_libcrt: true", |
| 363 | soongProperty: `no_libcrt: true,`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 364 | bazelAttr: attrNameToString{"use_libcrt": `False`}, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 365 | }, |
| 366 | { |
| 367 | description: "no_libcrt: false", |
| 368 | soongProperty: `no_libcrt: false,`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 369 | bazelAttr: attrNameToString{"use_libcrt": `True`}, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 370 | }, |
| 371 | { |
| 372 | description: "no_libcrt: not set", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 373 | bazelAttr: attrNameToString{}, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 374 | }, |
| 375 | } |
| 376 | |
| 377 | baseBlueprint := `{rule_name} { |
| 378 | name: "foo",%s |
| 379 | include_build_directory: false, |
| 380 | } |
| 381 | ` |
| 382 | |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 383 | for _, btc := range baseTestCases { |
| 384 | prop := btc.soongProperty |
| 385 | if len(prop) > 0 { |
| 386 | prop = "\n" + prop |
| 387 | } |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 388 | runCcBinaryTests(t, ccBinaryBp2buildTestCase{ |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 389 | description: btc.description, |
| 390 | blueprint: fmt.Sprintf(baseBlueprint, prop), |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 391 | targets: []testBazelTarget{ |
| 392 | {"cc_binary", "foo", btc.bazelAttr}, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 393 | }, |
| 394 | }) |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | func TestCcBinaryPropertiesToFeatures(t *testing.T) { |
| 399 | baseTestCases := []struct { |
| 400 | description string |
| 401 | soongProperty string |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 402 | bazelAttr attrNameToString |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 403 | }{ |
| 404 | { |
| 405 | description: "pack_relocation: true", |
| 406 | soongProperty: `pack_relocations: true,`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 407 | bazelAttr: attrNameToString{}, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 408 | }, |
| 409 | { |
| 410 | description: "pack_relocations: false", |
| 411 | soongProperty: `pack_relocations: false,`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 412 | bazelAttr: attrNameToString{"features": `["disable_pack_relocations"]`}, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 413 | }, |
| 414 | { |
| 415 | description: "pack_relocations: not set", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 416 | bazelAttr: attrNameToString{}, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 417 | }, |
| 418 | { |
| 419 | description: "pack_relocation: true", |
| 420 | soongProperty: `allow_undefined_symbols: true,`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 421 | bazelAttr: attrNameToString{"features": `["-no_undefined_symbols"]`}, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 422 | }, |
| 423 | { |
| 424 | description: "allow_undefined_symbols: false", |
| 425 | soongProperty: `allow_undefined_symbols: false,`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 426 | bazelAttr: attrNameToString{}, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 427 | }, |
| 428 | { |
| 429 | description: "allow_undefined_symbols: not set", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 430 | bazelAttr: attrNameToString{}, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 431 | }, |
| 432 | } |
| 433 | |
| 434 | baseBlueprint := `{rule_name} { |
| 435 | name: "foo",%s |
| 436 | include_build_directory: false, |
| 437 | } |
| 438 | ` |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 439 | for _, btc := range baseTestCases { |
| 440 | prop := btc.soongProperty |
| 441 | if len(prop) > 0 { |
| 442 | prop = "\n" + prop |
| 443 | } |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 444 | runCcBinaryTests(t, ccBinaryBp2buildTestCase{ |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 445 | description: btc.description, |
| 446 | blueprint: fmt.Sprintf(baseBlueprint, prop), |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 447 | targets: []testBazelTarget{ |
| 448 | {"cc_binary", "foo", btc.bazelAttr}, |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 449 | }, |
| 450 | }) |
| 451 | } |
| 452 | } |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 453 | |
| 454 | func TestCcBinarySharedProto(t *testing.T) { |
| 455 | runCcBinaryTests(t, ccBinaryBp2buildTestCase{ |
| 456 | blueprint: soongCcProtoLibraries + `{rule_name} { |
| 457 | name: "foo", |
| 458 | srcs: ["foo.proto"], |
| 459 | proto: { |
| 460 | canonical_path_from_root: false, |
| 461 | }, |
| 462 | include_build_directory: false, |
| 463 | }`, |
| 464 | targets: []testBazelTarget{ |
| 465 | {"proto_library", "foo_proto", attrNameToString{ |
| 466 | "srcs": `["foo.proto"]`, |
| 467 | }}, {"cc_lite_proto_library", "foo_cc_proto_lite", attrNameToString{ |
| 468 | "deps": `[":foo_proto"]`, |
| 469 | }}, {"cc_binary", "foo", attrNameToString{ |
| 470 | "dynamic_deps": `[":libprotobuf-cpp-lite"]`, |
| 471 | "whole_archive_deps": `[":foo_cc_proto_lite"]`, |
| 472 | }}, |
| 473 | }, |
| 474 | }) |
| 475 | } |
| 476 | |
| 477 | func TestCcBinaryStaticProto(t *testing.T) { |
| 478 | runCcBinaryTests(t, ccBinaryBp2buildTestCase{ |
| 479 | blueprint: soongCcProtoLibraries + `{rule_name} { |
| 480 | name: "foo", |
| 481 | srcs: ["foo.proto"], |
| 482 | static_executable: true, |
| 483 | proto: { |
| 484 | canonical_path_from_root: false, |
| 485 | }, |
| 486 | include_build_directory: false, |
| 487 | }`, |
| 488 | targets: []testBazelTarget{ |
| 489 | {"proto_library", "foo_proto", attrNameToString{ |
| 490 | "srcs": `["foo.proto"]`, |
| 491 | }}, {"cc_lite_proto_library", "foo_cc_proto_lite", attrNameToString{ |
| 492 | "deps": `[":foo_proto"]`, |
| 493 | }}, {"cc_binary", "foo", attrNameToString{ |
| 494 | "deps": `[":libprotobuf-cpp-lite"]`, |
| 495 | "whole_archive_deps": `[":foo_cc_proto_lite"]`, |
| 496 | "linkshared": `False`, |
| 497 | }}, |
| 498 | }, |
| 499 | }) |
| 500 | } |