Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 1 | // Copyright 2022 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 | // |
Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 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 | package bp2build |
| 15 | |
| 16 | import ( |
| 17 | "fmt" |
| 18 | "testing" |
| 19 | |
| 20 | "android/soong/cc" |
| 21 | ) |
| 22 | |
| 23 | func TestPrebuiltLibraryStaticAndSharedSimple(t *testing.T) { |
Trevor Radcliffe | 1b4b2d9 | 2022-09-01 18:57:01 +0000 | [diff] [blame] | 24 | RunBp2BuildTestCaseSimple(t, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 25 | Bp2buildTestCase{ |
| 26 | Description: "prebuilt library static and shared simple", |
| 27 | ModuleTypeUnderTest: "cc_prebuilt_library", |
| 28 | ModuleTypeUnderTestFactory: cc.PrebuiltLibraryFactory, |
| 29 | Filesystem: map[string]string{ |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 30 | "libf.so": "", |
| 31 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 32 | Blueprint: ` |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 33 | cc_prebuilt_library { |
| 34 | name: "libtest", |
| 35 | srcs: ["libf.so"], |
| 36 | bazel_module: { bp2build_available: true }, |
| 37 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 38 | ExpectedBazelTargets: []string{ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c5184ec | 2022-10-17 14:48:57 +0000 | [diff] [blame] | 39 | MakeBazelTarget("cc_prebuilt_library_static", "libtest_bp2build_cc_library_static", AttrNameToString{ |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 40 | "static_library": `"libf.so"`, |
| 41 | }), |
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux | c353abd | 2023-03-10 20:57:38 +0000 | [diff] [blame^] | 42 | MakeBazelTarget("cc_prebuilt_library_static", "libtest_bp2build_cc_library_static_alwayslink", AttrNameToString{ |
| 43 | "static_library": `"libf.so"`, |
| 44 | "alwayslink": "True", |
| 45 | }), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c5184ec | 2022-10-17 14:48:57 +0000 | [diff] [blame] | 46 | MakeBazelTarget("cc_prebuilt_library_shared", "libtest", AttrNameToString{ |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 47 | "shared_library": `"libf.so"`, |
| 48 | }), |
| 49 | }, |
| 50 | }) |
| 51 | } |
| 52 | |
| 53 | func TestPrebuiltLibraryWithArchVariance(t *testing.T) { |
Trevor Radcliffe | 1b4b2d9 | 2022-09-01 18:57:01 +0000 | [diff] [blame] | 54 | RunBp2BuildTestCaseSimple(t, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 55 | Bp2buildTestCase{ |
| 56 | Description: "prebuilt library with arch variance", |
| 57 | ModuleTypeUnderTest: "cc_prebuilt_library", |
| 58 | ModuleTypeUnderTestFactory: cc.PrebuiltLibraryFactory, |
| 59 | Filesystem: map[string]string{ |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 60 | "libf.so": "", |
| 61 | "libg.so": "", |
| 62 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 63 | Blueprint: ` |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 64 | cc_prebuilt_library { |
| 65 | name: "libtest", |
| 66 | arch: { |
| 67 | arm64: { srcs: ["libf.so"], }, |
| 68 | arm: { srcs: ["libg.so"], }, |
| 69 | }, |
| 70 | bazel_module: { bp2build_available: true }, |
| 71 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 72 | ExpectedBazelTargets: []string{ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c5184ec | 2022-10-17 14:48:57 +0000 | [diff] [blame] | 73 | MakeBazelTarget("cc_prebuilt_library_static", "libtest_bp2build_cc_library_static", AttrNameToString{ |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 74 | "static_library": `select({ |
| 75 | "//build/bazel/platforms/arch:arm": "libg.so", |
| 76 | "//build/bazel/platforms/arch:arm64": "libf.so", |
| 77 | "//conditions:default": None, |
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux | c353abd | 2023-03-10 20:57:38 +0000 | [diff] [blame^] | 78 | })`}), |
| 79 | MakeBazelTarget("cc_prebuilt_library_static", "libtest_bp2build_cc_library_static_alwayslink", AttrNameToString{ |
| 80 | "alwayslink": "True", |
| 81 | "static_library": `select({ |
| 82 | "//build/bazel/platforms/arch:arm": "libg.so", |
| 83 | "//build/bazel/platforms/arch:arm64": "libf.so", |
| 84 | "//conditions:default": None, |
| 85 | })`}), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c5184ec | 2022-10-17 14:48:57 +0000 | [diff] [blame] | 86 | MakeBazelTarget("cc_prebuilt_library_shared", "libtest", AttrNameToString{ |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 87 | "shared_library": `select({ |
| 88 | "//build/bazel/platforms/arch:arm": "libg.so", |
| 89 | "//build/bazel/platforms/arch:arm64": "libf.so", |
| 90 | "//conditions:default": None, |
| 91 | })`, |
| 92 | }), |
| 93 | }, |
| 94 | }) |
| 95 | } |
| 96 | |
| 97 | func TestPrebuiltLibraryAdditionalAttrs(t *testing.T) { |
Trevor Radcliffe | 1b4b2d9 | 2022-09-01 18:57:01 +0000 | [diff] [blame] | 98 | RunBp2BuildTestCaseSimple(t, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 99 | Bp2buildTestCase{ |
| 100 | Description: "prebuilt library additional attributes", |
| 101 | ModuleTypeUnderTest: "cc_prebuilt_library", |
| 102 | ModuleTypeUnderTestFactory: cc.PrebuiltLibraryFactory, |
| 103 | Filesystem: map[string]string{ |
Paul Duffin | 4c0765a | 2022-10-29 17:48:00 +0100 | [diff] [blame] | 104 | "libf.so": "", |
| 105 | "testdir/1/include.h": "", |
| 106 | "testdir/2/other.h": "", |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 107 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 108 | Blueprint: ` |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 109 | cc_prebuilt_library { |
| 110 | name: "libtest", |
| 111 | srcs: ["libf.so"], |
| 112 | export_include_dirs: ["testdir/1/"], |
| 113 | export_system_include_dirs: ["testdir/2/"], |
| 114 | bazel_module: { bp2build_available: true }, |
| 115 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 116 | ExpectedBazelTargets: []string{ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c5184ec | 2022-10-17 14:48:57 +0000 | [diff] [blame] | 117 | MakeBazelTarget("cc_prebuilt_library_static", "libtest_bp2build_cc_library_static", AttrNameToString{ |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 118 | "static_library": `"libf.so"`, |
| 119 | "export_includes": `["testdir/1/"]`, |
| 120 | "export_system_includes": `["testdir/2/"]`, |
| 121 | }), |
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux | c353abd | 2023-03-10 20:57:38 +0000 | [diff] [blame^] | 122 | MakeBazelTarget("cc_prebuilt_library_static", "libtest_bp2build_cc_library_static_alwayslink", AttrNameToString{ |
| 123 | "static_library": `"libf.so"`, |
| 124 | "export_includes": `["testdir/1/"]`, |
| 125 | "export_system_includes": `["testdir/2/"]`, |
| 126 | "alwayslink": "True", |
| 127 | }), |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 128 | // TODO(b/229374533): When fixed, update this test |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c5184ec | 2022-10-17 14:48:57 +0000 | [diff] [blame] | 129 | MakeBazelTarget("cc_prebuilt_library_shared", "libtest", AttrNameToString{ |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 130 | "shared_library": `"libf.so"`, |
| 131 | }), |
| 132 | }, |
| 133 | }) |
| 134 | } |
| 135 | |
| 136 | func TestPrebuiltLibrarySharedStanzaFails(t *testing.T) { |
Trevor Radcliffe | 1b4b2d9 | 2022-09-01 18:57:01 +0000 | [diff] [blame] | 137 | RunBp2BuildTestCaseSimple(t, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 138 | Bp2buildTestCase{ |
| 139 | Description: "prebuilt library with shared stanza fails because multiple sources", |
| 140 | ModuleTypeUnderTest: "cc_prebuilt_library", |
| 141 | ModuleTypeUnderTestFactory: cc.PrebuiltLibraryFactory, |
| 142 | Filesystem: map[string]string{ |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 143 | "libf.so": "", |
| 144 | "libg.so": "", |
| 145 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 146 | Blueprint: ` |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 147 | cc_prebuilt_library { |
| 148 | name: "libtest", |
| 149 | srcs: ["libf.so"], |
| 150 | shared: { |
| 151 | srcs: ["libg.so"], |
| 152 | }, |
| 153 | bazel_module: { bp2build_available: true }, |
| 154 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 155 | ExpectedErr: fmt.Errorf("Expected at most one source file"), |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 156 | }) |
| 157 | } |
| 158 | |
| 159 | func TestPrebuiltLibraryStaticStanzaFails(t *testing.T) { |
Trevor Radcliffe | 1b4b2d9 | 2022-09-01 18:57:01 +0000 | [diff] [blame] | 160 | RunBp2BuildTestCaseSimple(t, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 161 | Bp2buildTestCase{ |
| 162 | Description: "prebuilt library with static stanza fails because multiple sources", |
| 163 | ModuleTypeUnderTest: "cc_prebuilt_library", |
| 164 | ModuleTypeUnderTestFactory: cc.PrebuiltLibraryFactory, |
| 165 | Filesystem: map[string]string{ |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 166 | "libf.so": "", |
| 167 | "libg.so": "", |
| 168 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 169 | Blueprint: ` |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 170 | cc_prebuilt_library { |
| 171 | name: "libtest", |
| 172 | srcs: ["libf.so"], |
| 173 | static: { |
| 174 | srcs: ["libg.so"], |
| 175 | }, |
| 176 | bazel_module: { bp2build_available: true }, |
| 177 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 178 | ExpectedErr: fmt.Errorf("Expected at most one source file"), |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 179 | }) |
| 180 | } |
| 181 | |
| 182 | func TestPrebuiltLibrarySharedAndStaticStanzas(t *testing.T) { |
Trevor Radcliffe | 1b4b2d9 | 2022-09-01 18:57:01 +0000 | [diff] [blame] | 183 | RunBp2BuildTestCaseSimple(t, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 184 | Bp2buildTestCase{ |
| 185 | Description: "prebuilt library with both shared and static stanzas", |
| 186 | ModuleTypeUnderTest: "cc_prebuilt_library", |
| 187 | ModuleTypeUnderTestFactory: cc.PrebuiltLibraryFactory, |
| 188 | Filesystem: map[string]string{ |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 189 | "libf.so": "", |
| 190 | "libg.so": "", |
| 191 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 192 | Blueprint: ` |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 193 | cc_prebuilt_library { |
| 194 | name: "libtest", |
| 195 | static: { |
| 196 | srcs: ["libf.so"], |
| 197 | }, |
| 198 | shared: { |
| 199 | srcs: ["libg.so"], |
| 200 | }, |
| 201 | bazel_module: { bp2build_available: true }, |
| 202 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 203 | ExpectedBazelTargets: []string{ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c5184ec | 2022-10-17 14:48:57 +0000 | [diff] [blame] | 204 | MakeBazelTarget("cc_prebuilt_library_static", "libtest_bp2build_cc_library_static", AttrNameToString{ |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 205 | "static_library": `"libf.so"`, |
| 206 | }), |
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux | c353abd | 2023-03-10 20:57:38 +0000 | [diff] [blame^] | 207 | MakeBazelTarget("cc_prebuilt_library_static", "libtest_bp2build_cc_library_static_alwayslink", AttrNameToString{ |
| 208 | "static_library": `"libf.so"`, |
| 209 | "alwayslink": "True", |
| 210 | }), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c5184ec | 2022-10-17 14:48:57 +0000 | [diff] [blame] | 211 | MakeBazelTarget("cc_prebuilt_library_shared", "libtest", AttrNameToString{ |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 212 | "shared_library": `"libg.so"`, |
| 213 | }), |
| 214 | }, |
| 215 | }) |
| 216 | } |
| 217 | |
| 218 | // TODO(b/228623543): When this bug is fixed, enable this test |
| 219 | //func TestPrebuiltLibraryOnlyShared(t *testing.T) { |
Trevor Radcliffe | 1b4b2d9 | 2022-09-01 18:57:01 +0000 | [diff] [blame] | 220 | // RunBp2BuildTestCaseSimple(t, |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 221 | // bp2buildTestCase{ |
| 222 | // description: "prebuilt library shared only", |
| 223 | // moduleTypeUnderTest: "cc_prebuilt_library", |
| 224 | // moduleTypeUnderTestFactory: cc.PrebuiltLibraryFactory, |
| 225 | // filesystem: map[string]string{ |
| 226 | // "libf.so": "", |
| 227 | // }, |
| 228 | // blueprint: ` |
| 229 | //cc_prebuilt_library { |
| 230 | // name: "libtest", |
| 231 | // srcs: ["libf.so"], |
| 232 | // static: { |
| 233 | // enabled: false, |
| 234 | // }, |
| 235 | // bazel_module: { bp2build_available: true }, |
| 236 | //}`, |
| 237 | // expectedBazelTargets: []string{ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c5184ec | 2022-10-17 14:48:57 +0000 | [diff] [blame] | 238 | // makeBazelTarget("cc_prebuilt_library_shared", "libtest", attrNameToString{ |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 239 | // "shared_library": `"libf.so"`, |
| 240 | // }), |
| 241 | // }, |
| 242 | // }) |
| 243 | //} |
| 244 | |
| 245 | // TODO(b/228623543): When this bug is fixed, enable this test |
| 246 | //func TestPrebuiltLibraryOnlyStatic(t *testing.T) { |
Trevor Radcliffe | 1b4b2d9 | 2022-09-01 18:57:01 +0000 | [diff] [blame] | 247 | // RunBp2BuildTestCaseSimple(t, |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 248 | // bp2buildTestCase{ |
| 249 | // description: "prebuilt library static only", |
| 250 | // moduleTypeUnderTest: "cc_prebuilt_library", |
| 251 | // moduleTypeUnderTestFactory: cc.PrebuiltLibraryFactory, |
| 252 | // filesystem: map[string]string{ |
| 253 | // "libf.so": "", |
| 254 | // }, |
| 255 | // blueprint: ` |
| 256 | //cc_prebuilt_library { |
| 257 | // name: "libtest", |
| 258 | // srcs: ["libf.so"], |
| 259 | // shared: { |
| 260 | // enabled: false, |
| 261 | // }, |
| 262 | // bazel_module: { bp2build_available: true }, |
| 263 | //}`, |
| 264 | // expectedBazelTargets: []string{ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c5184ec | 2022-10-17 14:48:57 +0000 | [diff] [blame] | 265 | // makeBazelTarget("cc_prebuilt_library_static", "libtest_bp2build_cc_library_static", attrNameToString{ |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 266 | // "static_library": `"libf.so"`, |
| 267 | // }), |
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux | c353abd | 2023-03-10 20:57:38 +0000 | [diff] [blame^] | 268 | // makeBazelTarget("cc_prebuilt_library_static", "libtest_bp2build_cc_library_static_always", attrNameToString{ |
| 269 | // "static_library": `"libf.so"`, |
| 270 | // "alwayslink": "True", |
| 271 | // }), |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 272 | // }, |
| 273 | // }) |
| 274 | //} |