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. 14package bp2build 15 16import ( 17 "testing" 18 19 "android/soong/android" 20 "android/soong/cc" 21) 22 23func runYasmTestCase(t *testing.T, tc Bp2buildTestCase) { 24 t.Helper() 25 RunBp2BuildTestCase(t, registerYasmModuleTypes, tc) 26} 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) { 37 runYasmTestCase(t, Bp2buildTestCase{ 38 Description: "Simple yasm test", 39 ModuleTypeUnderTest: "cc_library", 40 ModuleTypeUnderTestFactory: cc.LibraryFactory, 41 Filesystem: map[string]string{ 42 "main.cpp": "", 43 "myfile.asm": "", 44 }, 45 Blueprint: ` 46cc_library { 47 name: "foo", 48 srcs: ["main.cpp", "myfile.asm"], 49}`, 50 ExpectedBazelTargets: append([]string{ 51 MakeBazelTarget("yasm", "foo_yasm", map[string]string{ 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 65func TestYasmWithIncludeDirs(t *testing.T) { 66 runYasmTestCase(t, Bp2buildTestCase{ 67 Description: "Simple yasm test", 68 ModuleTypeUnderTest: "cc_library", 69 ModuleTypeUnderTestFactory: cc.LibraryFactory, 70 Filesystem: map[string]string{ 71 "main.cpp": "", 72 "myfile.asm": "", 73 "include1/foo/myinclude.inc": "", 74 "include2/foo/myinclude2.inc": "", 75 }, 76 Blueprint: ` 77cc_library { 78 name: "foo", 79 local_include_dirs: ["include1/foo"], 80 export_include_dirs: ["include2/foo"], 81 srcs: ["main.cpp", "myfile.asm"], 82}`, 83 ExpectedBazelTargets: append([]string{ 84 MakeBazelTarget("yasm", "foo_yasm", map[string]string{ 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 106func TestYasmConditionalBasedOnArch(t *testing.T) { 107 runYasmTestCase(t, Bp2buildTestCase{ 108 Description: "Simple yasm test", 109 ModuleTypeUnderTest: "cc_library", 110 ModuleTypeUnderTestFactory: cc.LibraryFactory, 111 Filesystem: map[string]string{ 112 "main.cpp": "", 113 "myfile.asm": "", 114 }, 115 Blueprint: ` 116cc_library { 117 name: "foo", 118 srcs: ["main.cpp"], 119 arch: { 120 x86: { 121 srcs: ["myfile.asm"], 122 }, 123 }, 124}`, 125 ExpectedBazelTargets: append([]string{ 126 MakeBazelTarget("yasm", "foo_yasm", map[string]string{ 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 143func TestYasmPartiallyConditional(t *testing.T) { 144 runYasmTestCase(t, Bp2buildTestCase{ 145 Description: "Simple yasm test", 146 ModuleTypeUnderTest: "cc_library", 147 ModuleTypeUnderTestFactory: cc.LibraryFactory, 148 Filesystem: map[string]string{ 149 "main.cpp": "", 150 "myfile.asm": "", 151 "mysecondfile.asm": "", 152 }, 153 Blueprint: ` 154cc_library { 155 name: "foo", 156 srcs: ["main.cpp", "myfile.asm"], 157 arch: { 158 x86: { 159 srcs: ["mysecondfile.asm"], 160 }, 161 }, 162}`, 163 ExpectedBazelTargets: append([]string{ 164 MakeBazelTarget("yasm", "foo_yasm", map[string]string{ 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} 180