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 TestStaticPrebuiltLibrary(t *testing.T) { |
| 24 | runBp2BuildTestCaseSimple(t, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 25 | Bp2buildTestCase{ |
| 26 | Description: "prebuilt library static simple", |
| 27 | ModuleTypeUnderTest: "cc_prebuilt_library_static", |
| 28 | ModuleTypeUnderTestFactory: cc.PrebuiltStaticLibraryFactory, |
| 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_static { |
| 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{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame^] | 39 | MakeBazelTarget("prebuilt_library_static", "libtest", AttrNameToString{ |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 40 | "static_library": `"libf.so"`, |
| 41 | }), |
| 42 | }, |
| 43 | }) |
| 44 | } |
| 45 | |
| 46 | func TestStaticPrebuiltLibraryWithArchVariance(t *testing.T) { |
| 47 | runBp2BuildTestCaseSimple(t, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 48 | Bp2buildTestCase{ |
| 49 | Description: "prebuilt library static with arch variance", |
| 50 | ModuleTypeUnderTest: "cc_prebuilt_library_static", |
| 51 | ModuleTypeUnderTestFactory: cc.PrebuiltStaticLibraryFactory, |
| 52 | Filesystem: map[string]string{ |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 53 | "libf.so": "", |
| 54 | "libg.so": "", |
| 55 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 56 | Blueprint: ` |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 57 | cc_prebuilt_library_static { |
| 58 | name: "libtest", |
| 59 | arch: { |
| 60 | arm64: { srcs: ["libf.so"], }, |
| 61 | arm: { srcs: ["libg.so"], }, |
| 62 | }, |
| 63 | bazel_module: { bp2build_available: true }, |
| 64 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 65 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame^] | 66 | MakeBazelTarget("prebuilt_library_static", "libtest", AttrNameToString{ |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 67 | "static_library": `select({ |
| 68 | "//build/bazel/platforms/arch:arm": "libg.so", |
| 69 | "//build/bazel/platforms/arch:arm64": "libf.so", |
| 70 | "//conditions:default": None, |
| 71 | })`, |
| 72 | }), |
| 73 | }, |
| 74 | }) |
| 75 | } |
| 76 | |
| 77 | func TestStaticPrebuiltLibraryStaticStanzaFails(t *testing.T) { |
| 78 | runBp2BuildTestCaseSimple(t, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 79 | Bp2buildTestCase{ |
| 80 | Description: "prebuilt library with static stanza fails because multiple sources", |
| 81 | ModuleTypeUnderTest: "cc_prebuilt_library_static", |
| 82 | ModuleTypeUnderTestFactory: cc.PrebuiltStaticLibraryFactory, |
| 83 | Filesystem: map[string]string{ |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 84 | "libf.so": "", |
| 85 | "libg.so": "", |
| 86 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 87 | Blueprint: ` |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 88 | cc_prebuilt_library_static { |
| 89 | name: "libtest", |
| 90 | srcs: ["libf.so"], |
| 91 | static: { |
| 92 | srcs: ["libg.so"], |
| 93 | }, |
| 94 | bazel_module: { bp2build_available: true }, |
| 95 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 96 | ExpectedErr: fmt.Errorf("Expected at most one source file"), |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 97 | }) |
| 98 | } |
Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame] | 99 | |
| 100 | func TestCcLibraryStaticConvertLex(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 101 | runCcLibrarySharedTestCase(t, Bp2buildTestCase{ |
| 102 | Description: "cc_library_static with lex files", |
| 103 | ModuleTypeUnderTest: "cc_library_static", |
| 104 | ModuleTypeUnderTestFactory: cc.LibraryStaticFactory, |
| 105 | Filesystem: map[string]string{ |
Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame] | 106 | "foo.c": "", |
| 107 | "bar.cc": "", |
| 108 | "foo1.l": "", |
| 109 | "bar1.ll": "", |
| 110 | "foo2.l": "", |
| 111 | "bar2.ll": "", |
| 112 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 113 | Blueprint: `cc_library_static { |
Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame] | 114 | name: "foo_lib", |
| 115 | srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"], |
| 116 | lex: { flags: ["--foo_flags"] }, |
| 117 | include_build_directory: false, |
| 118 | bazel_module: { bp2build_available: true }, |
| 119 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 120 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame^] | 121 | MakeBazelTarget("genlex", "foo_lib_genlex_l", AttrNameToString{ |
Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame] | 122 | "srcs": `[ |
| 123 | "foo1.l", |
| 124 | "foo2.l", |
| 125 | ]`, |
| 126 | "lexopts": `["--foo_flags"]`, |
| 127 | }), |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame^] | 128 | MakeBazelTarget("genlex", "foo_lib_genlex_ll", AttrNameToString{ |
Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame] | 129 | "srcs": `[ |
| 130 | "bar1.ll", |
| 131 | "bar2.ll", |
| 132 | ]`, |
| 133 | "lexopts": `["--foo_flags"]`, |
| 134 | }), |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame^] | 135 | MakeBazelTarget("cc_library_static", "foo_lib", AttrNameToString{ |
Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame] | 136 | "srcs": `[ |
| 137 | "bar.cc", |
| 138 | ":foo_lib_genlex_ll", |
| 139 | ]`, |
| 140 | "srcs_c": `[ |
| 141 | "foo.c", |
| 142 | ":foo_lib_genlex_l", |
| 143 | ]`, |
| 144 | }), |
| 145 | }, |
| 146 | }) |
| 147 | } |