| 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) { | 
| 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 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{ | 
| 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", 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_alwayslink", AttrNameToString{ | 
|  | 43 | "static_library": `"libf.so"`, | 
|  | 44 | "alwayslink":     "True", | 
|  | 45 | }), | 
| Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 46 | }, | 
|  | 47 | }) | 
|  | 48 | } | 
|  | 49 |  | 
|  | 50 | func TestStaticPrebuiltLibraryWithArchVariance(t *testing.T) { | 
| Trevor Radcliffe | 1b4b2d9 | 2022-09-01 18:57:01 +0000 | [diff] [blame] | 51 | RunBp2BuildTestCaseSimple(t, | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 52 | Bp2buildTestCase{ | 
|  | 53 | Description:                "prebuilt library static with arch variance", | 
|  | 54 | ModuleTypeUnderTest:        "cc_prebuilt_library_static", | 
|  | 55 | ModuleTypeUnderTestFactory: cc.PrebuiltStaticLibraryFactory, | 
|  | 56 | Filesystem: map[string]string{ | 
| Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 57 | "libf.so": "", | 
|  | 58 | "libg.so": "", | 
|  | 59 | }, | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 60 | Blueprint: ` | 
| Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 61 | cc_prebuilt_library_static { | 
|  | 62 | name: "libtest", | 
|  | 63 | arch: { | 
|  | 64 | arm64: { srcs: ["libf.so"], }, | 
|  | 65 | arm: { srcs: ["libg.so"], }, | 
|  | 66 | }, | 
|  | 67 | bazel_module: { bp2build_available: true }, | 
|  | 68 | }`, | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 69 | ExpectedBazelTargets: []string{ | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c5184ec | 2022-10-17 14:48:57 +0000 | [diff] [blame] | 70 | MakeBazelTarget("cc_prebuilt_library_static", "libtest", AttrNameToString{ | 
| Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 71 | "static_library": `select({ | 
| Jingwen Chen | 9c2e3ee | 2023-10-11 10:51:28 +0000 | [diff] [blame] | 72 | "//build/bazel_common_rules/platforms/arch:arm": "libg.so", | 
|  | 73 | "//build/bazel_common_rules/platforms/arch:arm64": "libf.so", | 
| Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 74 | "//conditions:default": None, | 
| Alex Márquez Pérez Muñíz Díaz Puras Thaureaux | c353abd | 2023-03-10 20:57:38 +0000 | [diff] [blame] | 75 | })`}), | 
|  | 76 | MakeBazelTarget("cc_prebuilt_library_static", "libtest_alwayslink", AttrNameToString{ | 
|  | 77 | "alwayslink": "True", | 
|  | 78 | "static_library": `select({ | 
| Jingwen Chen | 9c2e3ee | 2023-10-11 10:51:28 +0000 | [diff] [blame] | 79 | "//build/bazel_common_rules/platforms/arch:arm": "libg.so", | 
|  | 80 | "//build/bazel_common_rules/platforms/arch:arm64": "libf.so", | 
| Alex Márquez Pérez Muñíz Díaz Puras Thaureaux | c353abd | 2023-03-10 20:57:38 +0000 | [diff] [blame] | 81 | "//conditions:default": None, | 
|  | 82 | })`}), | 
| Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 83 | }, | 
|  | 84 | }) | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | func TestStaticPrebuiltLibraryStaticStanzaFails(t *testing.T) { | 
| Trevor Radcliffe | 1b4b2d9 | 2022-09-01 18:57:01 +0000 | [diff] [blame] | 88 | RunBp2BuildTestCaseSimple(t, | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 89 | Bp2buildTestCase{ | 
|  | 90 | Description:                "prebuilt library with static stanza fails because multiple sources", | 
|  | 91 | ModuleTypeUnderTest:        "cc_prebuilt_library_static", | 
|  | 92 | ModuleTypeUnderTestFactory: cc.PrebuiltStaticLibraryFactory, | 
|  | 93 | Filesystem: map[string]string{ | 
| Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 94 | "libf.so": "", | 
|  | 95 | "libg.so": "", | 
|  | 96 | }, | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 97 | Blueprint: ` | 
| Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 98 | cc_prebuilt_library_static { | 
|  | 99 | name: "libtest", | 
|  | 100 | srcs: ["libf.so"], | 
|  | 101 | static: { | 
|  | 102 | srcs: ["libg.so"], | 
|  | 103 | }, | 
|  | 104 | bazel_module: { bp2build_available: true }, | 
|  | 105 | }`, | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 106 | ExpectedErr: fmt.Errorf("Expected at most one source file"), | 
| Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 107 | }) | 
|  | 108 | } | 
| Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame] | 109 |  | 
|  | 110 | func TestCcLibraryStaticConvertLex(t *testing.T) { | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 111 | runCcLibrarySharedTestCase(t, Bp2buildTestCase{ | 
|  | 112 | Description:                "cc_library_static with lex files", | 
|  | 113 | ModuleTypeUnderTest:        "cc_library_static", | 
|  | 114 | ModuleTypeUnderTestFactory: cc.LibraryStaticFactory, | 
|  | 115 | Filesystem: map[string]string{ | 
| Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame] | 116 | "foo.c":   "", | 
|  | 117 | "bar.cc":  "", | 
|  | 118 | "foo1.l":  "", | 
|  | 119 | "bar1.ll": "", | 
|  | 120 | "foo2.l":  "", | 
|  | 121 | "bar2.ll": "", | 
|  | 122 | }, | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 123 | Blueprint: `cc_library_static { | 
| Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame] | 124 | name: "foo_lib", | 
|  | 125 | srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"], | 
|  | 126 | lex: { flags: ["--foo_flags"] }, | 
|  | 127 | include_build_directory: false, | 
|  | 128 | bazel_module: { bp2build_available: true }, | 
|  | 129 | }`, | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 130 | ExpectedBazelTargets: []string{ | 
| Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 131 | MakeBazelTarget("genlex", "foo_lib_genlex_l", AttrNameToString{ | 
| Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame] | 132 | "srcs": `[ | 
|  | 133 | "foo1.l", | 
|  | 134 | "foo2.l", | 
|  | 135 | ]`, | 
|  | 136 | "lexopts": `["--foo_flags"]`, | 
|  | 137 | }), | 
| Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 138 | MakeBazelTarget("genlex", "foo_lib_genlex_ll", AttrNameToString{ | 
| Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame] | 139 | "srcs": `[ | 
|  | 140 | "bar1.ll", | 
|  | 141 | "bar2.ll", | 
|  | 142 | ]`, | 
|  | 143 | "lexopts": `["--foo_flags"]`, | 
|  | 144 | }), | 
| Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 145 | MakeBazelTarget("cc_library_static", "foo_lib", AttrNameToString{ | 
| Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame] | 146 | "srcs": `[ | 
|  | 147 | "bar.cc", | 
|  | 148 | ":foo_lib_genlex_ll", | 
|  | 149 | ]`, | 
|  | 150 | "srcs_c": `[ | 
|  | 151 | "foo.c", | 
|  | 152 | ":foo_lib_genlex_l", | 
|  | 153 | ]`, | 
|  | 154 | }), | 
|  | 155 | }, | 
|  | 156 | }) | 
|  | 157 | } |