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 | // |
| 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 | package bp2build |
| 15 | |
| 16 | import ( |
| 17 | "fmt" |
| 18 | "testing" |
| 19 | |
| 20 | "android/soong/cc" |
| 21 | ) |
| 22 | |
| 23 | func TestPrebuiltLibraryStaticAndSharedSimple(t *testing.T) { |
| 24 | runBp2BuildTestCaseSimple(t, |
| 25 | bp2buildTestCase{ |
| 26 | description: "prebuilt library static and shared simple", |
| 27 | moduleTypeUnderTest: "cc_prebuilt_library", |
| 28 | moduleTypeUnderTestFactory: cc.PrebuiltLibraryFactory, |
| 29 | filesystem: map[string]string{ |
| 30 | "libf.so": "", |
| 31 | }, |
| 32 | blueprint: ` |
| 33 | cc_prebuilt_library { |
| 34 | name: "libtest", |
| 35 | srcs: ["libf.so"], |
| 36 | bazel_module: { bp2build_available: true }, |
| 37 | }`, |
| 38 | expectedBazelTargets: []string{ |
| 39 | makeBazelTarget("prebuilt_library_static", "libtest_bp2build_cc_library_static", attrNameToString{ |
| 40 | "static_library": `"libf.so"`, |
| 41 | }), |
| 42 | makeBazelTarget("prebuilt_library_shared", "libtest", attrNameToString{ |
| 43 | "shared_library": `"libf.so"`, |
| 44 | }), |
| 45 | }, |
| 46 | }) |
| 47 | } |
| 48 | |
| 49 | func TestPrebuiltLibraryWithArchVariance(t *testing.T) { |
| 50 | runBp2BuildTestCaseSimple(t, |
| 51 | bp2buildTestCase{ |
| 52 | description: "prebuilt library with arch variance", |
| 53 | moduleTypeUnderTest: "cc_prebuilt_library", |
| 54 | moduleTypeUnderTestFactory: cc.PrebuiltLibraryFactory, |
| 55 | filesystem: map[string]string{ |
| 56 | "libf.so": "", |
| 57 | "libg.so": "", |
| 58 | }, |
| 59 | blueprint: ` |
| 60 | cc_prebuilt_library { |
| 61 | name: "libtest", |
| 62 | arch: { |
| 63 | arm64: { srcs: ["libf.so"], }, |
| 64 | arm: { srcs: ["libg.so"], }, |
| 65 | }, |
| 66 | bazel_module: { bp2build_available: true }, |
| 67 | }`, |
| 68 | expectedBazelTargets: []string{ |
| 69 | makeBazelTarget("prebuilt_library_static", "libtest_bp2build_cc_library_static", attrNameToString{ |
| 70 | "static_library": `select({ |
| 71 | "//build/bazel/platforms/arch:arm": "libg.so", |
| 72 | "//build/bazel/platforms/arch:arm64": "libf.so", |
| 73 | "//conditions:default": None, |
| 74 | })`, |
| 75 | }), |
| 76 | makeBazelTarget("prebuilt_library_shared", "libtest", attrNameToString{ |
| 77 | "shared_library": `select({ |
| 78 | "//build/bazel/platforms/arch:arm": "libg.so", |
| 79 | "//build/bazel/platforms/arch:arm64": "libf.so", |
| 80 | "//conditions:default": None, |
| 81 | })`, |
| 82 | }), |
| 83 | }, |
| 84 | }) |
| 85 | } |
| 86 | |
| 87 | func TestPrebuiltLibraryAdditionalAttrs(t *testing.T) { |
| 88 | runBp2BuildTestCaseSimple(t, |
| 89 | bp2buildTestCase{ |
| 90 | description: "prebuilt library additional attributes", |
| 91 | moduleTypeUnderTest: "cc_prebuilt_library", |
| 92 | moduleTypeUnderTestFactory: cc.PrebuiltLibraryFactory, |
| 93 | filesystem: map[string]string{ |
| 94 | "libf.so": "", |
| 95 | "testdir/1/": "", |
| 96 | "testdir/2/": "", |
| 97 | }, |
| 98 | blueprint: ` |
| 99 | cc_prebuilt_library { |
| 100 | name: "libtest", |
| 101 | srcs: ["libf.so"], |
| 102 | export_include_dirs: ["testdir/1/"], |
| 103 | export_system_include_dirs: ["testdir/2/"], |
| 104 | bazel_module: { bp2build_available: true }, |
| 105 | }`, |
| 106 | expectedBazelTargets: []string{ |
| 107 | makeBazelTarget("prebuilt_library_static", "libtest_bp2build_cc_library_static", attrNameToString{ |
| 108 | "static_library": `"libf.so"`, |
| 109 | "export_includes": `["testdir/1/"]`, |
| 110 | "export_system_includes": `["testdir/2/"]`, |
| 111 | }), |
| 112 | // TODO(b/229374533): When fixed, update this test |
| 113 | makeBazelTarget("prebuilt_library_shared", "libtest", attrNameToString{ |
| 114 | "shared_library": `"libf.so"`, |
| 115 | }), |
| 116 | }, |
| 117 | }) |
| 118 | } |
| 119 | |
| 120 | func TestPrebuiltLibrarySharedStanzaFails(t *testing.T) { |
| 121 | runBp2BuildTestCaseSimple(t, |
| 122 | bp2buildTestCase{ |
| 123 | description: "prebuilt library with shared stanza fails because multiple sources", |
| 124 | moduleTypeUnderTest: "cc_prebuilt_library", |
| 125 | moduleTypeUnderTestFactory: cc.PrebuiltLibraryFactory, |
| 126 | filesystem: map[string]string{ |
| 127 | "libf.so": "", |
| 128 | "libg.so": "", |
| 129 | }, |
| 130 | blueprint: ` |
| 131 | cc_prebuilt_library { |
| 132 | name: "libtest", |
| 133 | srcs: ["libf.so"], |
| 134 | shared: { |
| 135 | srcs: ["libg.so"], |
| 136 | }, |
| 137 | bazel_module: { bp2build_available: true }, |
| 138 | }`, |
| 139 | expectedErr: fmt.Errorf("Expected at most once source file"), |
| 140 | }) |
| 141 | } |
| 142 | |
| 143 | func TestPrebuiltLibraryStaticStanzaFails(t *testing.T) { |
| 144 | runBp2BuildTestCaseSimple(t, |
| 145 | bp2buildTestCase{ |
| 146 | description: "prebuilt library with static stanza fails because multiple sources", |
| 147 | moduleTypeUnderTest: "cc_prebuilt_library", |
| 148 | moduleTypeUnderTestFactory: cc.PrebuiltLibraryFactory, |
| 149 | filesystem: map[string]string{ |
| 150 | "libf.so": "", |
| 151 | "libg.so": "", |
| 152 | }, |
| 153 | blueprint: ` |
| 154 | cc_prebuilt_library { |
| 155 | name: "libtest", |
| 156 | srcs: ["libf.so"], |
| 157 | static: { |
| 158 | srcs: ["libg.so"], |
| 159 | }, |
| 160 | bazel_module: { bp2build_available: true }, |
| 161 | }`, |
| 162 | expectedErr: fmt.Errorf("Expected at most once source file"), |
| 163 | }) |
| 164 | } |
| 165 | |
| 166 | func TestPrebuiltLibrarySharedAndStaticStanzas(t *testing.T) { |
| 167 | runBp2BuildTestCaseSimple(t, |
| 168 | bp2buildTestCase{ |
| 169 | description: "prebuilt library with both shared and static stanzas", |
| 170 | moduleTypeUnderTest: "cc_prebuilt_library", |
| 171 | moduleTypeUnderTestFactory: cc.PrebuiltLibraryFactory, |
| 172 | filesystem: map[string]string{ |
| 173 | "libf.so": "", |
| 174 | "libg.so": "", |
| 175 | }, |
| 176 | blueprint: ` |
| 177 | cc_prebuilt_library { |
| 178 | name: "libtest", |
| 179 | static: { |
| 180 | srcs: ["libf.so"], |
| 181 | }, |
| 182 | shared: { |
| 183 | srcs: ["libg.so"], |
| 184 | }, |
| 185 | bazel_module: { bp2build_available: true }, |
| 186 | }`, |
| 187 | expectedBazelTargets: []string{ |
| 188 | makeBazelTarget("prebuilt_library_static", "libtest_bp2build_cc_library_static", attrNameToString{ |
| 189 | "static_library": `"libf.so"`, |
| 190 | }), |
| 191 | makeBazelTarget("prebuilt_library_shared", "libtest", attrNameToString{ |
| 192 | "shared_library": `"libg.so"`, |
| 193 | }), |
| 194 | }, |
| 195 | }) |
| 196 | } |
| 197 | |
| 198 | // TODO(b/228623543): When this bug is fixed, enable this test |
| 199 | //func TestPrebuiltLibraryOnlyShared(t *testing.T) { |
| 200 | // runBp2BuildTestCaseSimple(t, |
| 201 | // bp2buildTestCase{ |
| 202 | // description: "prebuilt library shared only", |
| 203 | // moduleTypeUnderTest: "cc_prebuilt_library", |
| 204 | // moduleTypeUnderTestFactory: cc.PrebuiltLibraryFactory, |
| 205 | // filesystem: map[string]string{ |
| 206 | // "libf.so": "", |
| 207 | // }, |
| 208 | // blueprint: ` |
| 209 | //cc_prebuilt_library { |
| 210 | // name: "libtest", |
| 211 | // srcs: ["libf.so"], |
| 212 | // static: { |
| 213 | // enabled: false, |
| 214 | // }, |
| 215 | // bazel_module: { bp2build_available: true }, |
| 216 | //}`, |
| 217 | // expectedBazelTargets: []string{ |
| 218 | // makeBazelTarget("prebuilt_library_shared", "libtest", attrNameToString{ |
| 219 | // "shared_library": `"libf.so"`, |
| 220 | // }), |
| 221 | // }, |
| 222 | // }) |
| 223 | //} |
| 224 | |
| 225 | // TODO(b/228623543): When this bug is fixed, enable this test |
| 226 | //func TestPrebuiltLibraryOnlyStatic(t *testing.T) { |
| 227 | // runBp2BuildTestCaseSimple(t, |
| 228 | // bp2buildTestCase{ |
| 229 | // description: "prebuilt library static only", |
| 230 | // moduleTypeUnderTest: "cc_prebuilt_library", |
| 231 | // moduleTypeUnderTestFactory: cc.PrebuiltLibraryFactory, |
| 232 | // filesystem: map[string]string{ |
| 233 | // "libf.so": "", |
| 234 | // }, |
| 235 | // blueprint: ` |
| 236 | //cc_prebuilt_library { |
| 237 | // name: "libtest", |
| 238 | // srcs: ["libf.so"], |
| 239 | // shared: { |
| 240 | // enabled: false, |
| 241 | // }, |
| 242 | // bazel_module: { bp2build_available: true }, |
| 243 | //}`, |
| 244 | // expectedBazelTargets: []string{ |
| 245 | // makeBazelTarget("prebuilt_library_static", "libtest_bp2build_cc_library_static", attrNameToString{ |
| 246 | // "static_library": `"libf.so"`, |
| 247 | // }), |
| 248 | // }, |
| 249 | // }) |
| 250 | //} |