blob: 55d4febf9476ff5bcf56eb8f362bf6a936e5d4ff [file] [log] [blame]
Cole Faust7071a052022-07-29 15:58:33 -07001// 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.
14package bp2build
15
16import (
17 "testing"
18
19 "android/soong/android"
20 "android/soong/cc"
21)
22
Sam Delmericoc9a49df2022-08-08 10:52:27 -040023func runYasmTestCase(t *testing.T, tc Bp2buildTestCase) {
Cole Faust7071a052022-07-29 15:58:33 -070024 t.Helper()
Sam Delmericoc9a49df2022-08-08 10:52:27 -040025 RunBp2BuildTestCase(t, registerYasmModuleTypes, tc)
Cole Faust7071a052022-07-29 15:58:33 -070026}
27
28func 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
36func TestYasmSimple(t *testing.T) {
Sam Delmericoc9a49df2022-08-08 10:52:27 -040037 runYasmTestCase(t, Bp2buildTestCase{
38 Description: "Simple yasm test",
39 ModuleTypeUnderTest: "cc_library",
40 ModuleTypeUnderTestFactory: cc.LibraryFactory,
41 Filesystem: map[string]string{
Cole Faust7071a052022-07-29 15:58:33 -070042 "main.cpp": "",
43 "myfile.asm": "",
44 },
Sam Delmericoc9a49df2022-08-08 10:52:27 -040045 Blueprint: `
Cole Faust7071a052022-07-29 15:58:33 -070046cc_library {
47 name: "foo",
48 srcs: ["main.cpp", "myfile.asm"],
49}`,
Sam Delmericoc9a49df2022-08-08 10:52:27 -040050 ExpectedBazelTargets: append([]string{
Alixe06d75b2022-08-31 18:28:19 +000051 MakeBazelTarget("yasm", "foo_yasm", map[string]string{
Cole Faust7071a052022-07-29 15:58:33 -070052 "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
65func TestYasmWithIncludeDirs(t *testing.T) {
Sam Delmericoc9a49df2022-08-08 10:52:27 -040066 runYasmTestCase(t, Bp2buildTestCase{
67 Description: "Simple yasm test",
68 ModuleTypeUnderTest: "cc_library",
69 ModuleTypeUnderTestFactory: cc.LibraryFactory,
70 Filesystem: map[string]string{
Cole Faust7071a052022-07-29 15:58:33 -070071 "main.cpp": "",
72 "myfile.asm": "",
73 "include1/foo/myinclude.inc": "",
74 "include2/foo/myinclude2.inc": "",
75 },
Sam Delmericoc9a49df2022-08-08 10:52:27 -040076 Blueprint: `
Cole Faust7071a052022-07-29 15:58:33 -070077cc_library {
78 name: "foo",
79 local_include_dirs: ["include1/foo"],
80 export_include_dirs: ["include2/foo"],
81 srcs: ["main.cpp", "myfile.asm"],
82}`,
Sam Delmericoc9a49df2022-08-08 10:52:27 -040083 ExpectedBazelTargets: append([]string{
Alixe06d75b2022-08-31 18:28:19 +000084 MakeBazelTarget("yasm", "foo_yasm", map[string]string{
Cole Faust7071a052022-07-29 15:58:33 -070085 "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
106func TestYasmConditionalBasedOnArch(t *testing.T) {
Sam Delmericoc9a49df2022-08-08 10:52:27 -0400107 runYasmTestCase(t, Bp2buildTestCase{
108 Description: "Simple yasm test",
109 ModuleTypeUnderTest: "cc_library",
110 ModuleTypeUnderTestFactory: cc.LibraryFactory,
111 Filesystem: map[string]string{
Cole Faust7071a052022-07-29 15:58:33 -0700112 "main.cpp": "",
113 "myfile.asm": "",
114 },
Sam Delmericoc9a49df2022-08-08 10:52:27 -0400115 Blueprint: `
Cole Faust7071a052022-07-29 15:58:33 -0700116cc_library {
117 name: "foo",
118 srcs: ["main.cpp"],
119 arch: {
120 x86: {
121 srcs: ["myfile.asm"],
122 },
123 },
124}`,
Sam Delmericoc9a49df2022-08-08 10:52:27 -0400125 ExpectedBazelTargets: append([]string{
Alixe06d75b2022-08-31 18:28:19 +0000126 MakeBazelTarget("yasm", "foo_yasm", map[string]string{
Cole Faust7071a052022-07-29 15:58:33 -0700127 "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
143func TestYasmPartiallyConditional(t *testing.T) {
Sam Delmericoc9a49df2022-08-08 10:52:27 -0400144 runYasmTestCase(t, Bp2buildTestCase{
145 Description: "Simple yasm test",
146 ModuleTypeUnderTest: "cc_library",
147 ModuleTypeUnderTestFactory: cc.LibraryFactory,
148 Filesystem: map[string]string{
Cole Faust7071a052022-07-29 15:58:33 -0700149 "main.cpp": "",
150 "myfile.asm": "",
151 "mysecondfile.asm": "",
152 },
Sam Delmericoc9a49df2022-08-08 10:52:27 -0400153 Blueprint: `
Cole Faust7071a052022-07-29 15:58:33 -0700154cc_library {
155 name: "foo",
156 srcs: ["main.cpp", "myfile.asm"],
157 arch: {
158 x86: {
159 srcs: ["mysecondfile.asm"],
160 },
161 },
162}`,
Sam Delmericoc9a49df2022-08-08 10:52:27 -0400163 ExpectedBazelTargets: append([]string{
Alixe06d75b2022-08-31 18:28:19 +0000164 MakeBazelTarget("yasm", "foo_yasm", map[string]string{
Cole Faust7071a052022-07-29 15:58:33 -0700165 "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}