Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [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 | "testing" |
| 18 | |
| 19 | "android/soong/android" |
| 20 | "android/soong/cc" |
| 21 | ) |
| 22 | |
Sam Delmerico | c9a49df | 2022-08-08 10:52:27 -0400 | [diff] [blame] | 23 | func runYasmTestCase(t *testing.T, tc Bp2buildTestCase) { |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 24 | t.Helper() |
Sam Delmerico | c9a49df | 2022-08-08 10:52:27 -0400 | [diff] [blame] | 25 | RunBp2BuildTestCase(t, registerYasmModuleTypes, tc) |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | func registerYasmModuleTypes(ctx android.RegistrationContext) { |
| 29 | cc.RegisterCCBuildComponents(ctx) |
| 30 | ctx.RegisterModuleType("filegroup", android.FileGroupFactory) |
| 31 | ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory) |
| 32 | ctx.RegisterModuleType("cc_prebuilt_library_static", cc.PrebuiltStaticLibraryFactory) |
| 33 | ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory) |
| 34 | } |
| 35 | |
| 36 | func TestYasmSimple(t *testing.T) { |
Sam Delmerico | c9a49df | 2022-08-08 10:52:27 -0400 | [diff] [blame] | 37 | runYasmTestCase(t, Bp2buildTestCase{ |
| 38 | Description: "Simple yasm test", |
| 39 | ModuleTypeUnderTest: "cc_library", |
| 40 | ModuleTypeUnderTestFactory: cc.LibraryFactory, |
| 41 | Filesystem: map[string]string{ |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 42 | "main.cpp": "", |
| 43 | "myfile.asm": "", |
| 44 | }, |
Sam Delmerico | c9a49df | 2022-08-08 10:52:27 -0400 | [diff] [blame] | 45 | Blueprint: ` |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 46 | cc_library { |
| 47 | name: "foo", |
| 48 | srcs: ["main.cpp", "myfile.asm"], |
| 49 | }`, |
Sam Delmerico | c9a49df | 2022-08-08 10:52:27 -0400 | [diff] [blame] | 50 | ExpectedBazelTargets: append([]string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 51 | MakeBazelTarget("yasm", "foo_yasm", map[string]string{ |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 52 | "include_dirs": `["."]`, |
| 53 | "srcs": `["myfile.asm"]`, |
| 54 | }), |
| 55 | }, makeCcLibraryTargets("foo", map[string]string{ |
| 56 | "local_includes": `["."]`, |
| 57 | "srcs": `[ |
| 58 | "main.cpp", |
| 59 | ":foo_yasm", |
| 60 | ]`, |
| 61 | })...), |
| 62 | }) |
| 63 | } |
| 64 | |
| 65 | func TestYasmWithIncludeDirs(t *testing.T) { |
Sam Delmerico | c9a49df | 2022-08-08 10:52:27 -0400 | [diff] [blame] | 66 | runYasmTestCase(t, Bp2buildTestCase{ |
| 67 | Description: "Simple yasm test", |
| 68 | ModuleTypeUnderTest: "cc_library", |
| 69 | ModuleTypeUnderTestFactory: cc.LibraryFactory, |
| 70 | Filesystem: map[string]string{ |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 71 | "main.cpp": "", |
| 72 | "myfile.asm": "", |
| 73 | "include1/foo/myinclude.inc": "", |
| 74 | "include2/foo/myinclude2.inc": "", |
| 75 | }, |
Sam Delmerico | c9a49df | 2022-08-08 10:52:27 -0400 | [diff] [blame] | 76 | Blueprint: ` |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 77 | cc_library { |
| 78 | name: "foo", |
| 79 | local_include_dirs: ["include1/foo"], |
| 80 | export_include_dirs: ["include2/foo"], |
| 81 | srcs: ["main.cpp", "myfile.asm"], |
| 82 | }`, |
Sam Delmerico | c9a49df | 2022-08-08 10:52:27 -0400 | [diff] [blame] | 83 | ExpectedBazelTargets: append([]string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 84 | MakeBazelTarget("yasm", "foo_yasm", map[string]string{ |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 85 | "include_dirs": `[ |
| 86 | "include1/foo", |
| 87 | ".", |
| 88 | "include2/foo", |
| 89 | ]`, |
| 90 | "srcs": `["myfile.asm"]`, |
| 91 | }), |
| 92 | }, makeCcLibraryTargets("foo", map[string]string{ |
| 93 | "local_includes": `[ |
| 94 | "include1/foo", |
| 95 | ".", |
| 96 | ]`, |
| 97 | "export_includes": `["include2/foo"]`, |
| 98 | "srcs": `[ |
| 99 | "main.cpp", |
| 100 | ":foo_yasm", |
| 101 | ]`, |
| 102 | })...), |
| 103 | }) |
| 104 | } |
| 105 | |
| 106 | func TestYasmConditionalBasedOnArch(t *testing.T) { |
Sam Delmerico | c9a49df | 2022-08-08 10:52:27 -0400 | [diff] [blame] | 107 | runYasmTestCase(t, Bp2buildTestCase{ |
| 108 | Description: "Simple yasm test", |
| 109 | ModuleTypeUnderTest: "cc_library", |
| 110 | ModuleTypeUnderTestFactory: cc.LibraryFactory, |
| 111 | Filesystem: map[string]string{ |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 112 | "main.cpp": "", |
| 113 | "myfile.asm": "", |
| 114 | }, |
Sam Delmerico | c9a49df | 2022-08-08 10:52:27 -0400 | [diff] [blame] | 115 | Blueprint: ` |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 116 | cc_library { |
| 117 | name: "foo", |
| 118 | srcs: ["main.cpp"], |
| 119 | arch: { |
| 120 | x86: { |
| 121 | srcs: ["myfile.asm"], |
| 122 | }, |
| 123 | }, |
| 124 | }`, |
Sam Delmerico | c9a49df | 2022-08-08 10:52:27 -0400 | [diff] [blame] | 125 | ExpectedBazelTargets: append([]string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 126 | MakeBazelTarget("yasm", "foo_yasm", map[string]string{ |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 127 | "include_dirs": `["."]`, |
| 128 | "srcs": `select({ |
| 129 | "//build/bazel/platforms/arch:x86": ["myfile.asm"], |
| 130 | "//conditions:default": [], |
| 131 | })`, |
| 132 | }), |
| 133 | }, makeCcLibraryTargets("foo", map[string]string{ |
| 134 | "local_includes": `["."]`, |
| 135 | "srcs": `["main.cpp"] + select({ |
| 136 | "//build/bazel/platforms/arch:x86": [":foo_yasm"], |
| 137 | "//conditions:default": [], |
| 138 | })`, |
| 139 | })...), |
| 140 | }) |
| 141 | } |
| 142 | |
| 143 | func TestYasmPartiallyConditional(t *testing.T) { |
Sam Delmerico | c9a49df | 2022-08-08 10:52:27 -0400 | [diff] [blame] | 144 | runYasmTestCase(t, Bp2buildTestCase{ |
| 145 | Description: "Simple yasm test", |
| 146 | ModuleTypeUnderTest: "cc_library", |
| 147 | ModuleTypeUnderTestFactory: cc.LibraryFactory, |
| 148 | Filesystem: map[string]string{ |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 149 | "main.cpp": "", |
| 150 | "myfile.asm": "", |
| 151 | "mysecondfile.asm": "", |
| 152 | }, |
Sam Delmerico | c9a49df | 2022-08-08 10:52:27 -0400 | [diff] [blame] | 153 | Blueprint: ` |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 154 | cc_library { |
| 155 | name: "foo", |
| 156 | srcs: ["main.cpp", "myfile.asm"], |
| 157 | arch: { |
| 158 | x86: { |
| 159 | srcs: ["mysecondfile.asm"], |
| 160 | }, |
| 161 | }, |
| 162 | }`, |
Sam Delmerico | c9a49df | 2022-08-08 10:52:27 -0400 | [diff] [blame] | 163 | ExpectedBazelTargets: append([]string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 164 | MakeBazelTarget("yasm", "foo_yasm", map[string]string{ |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 165 | "include_dirs": `["."]`, |
| 166 | "srcs": `["myfile.asm"] + select({ |
| 167 | "//build/bazel/platforms/arch:x86": ["mysecondfile.asm"], |
| 168 | "//conditions:default": [], |
| 169 | })`, |
| 170 | }), |
| 171 | }, makeCcLibraryTargets("foo", map[string]string{ |
| 172 | "local_includes": `["."]`, |
| 173 | "srcs": `[ |
| 174 | "main.cpp", |
| 175 | ":foo_yasm", |
| 176 | ]`, |
| 177 | })...), |
| 178 | }) |
| 179 | } |