1// Copyright 2018 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 15package cc 16 17import ( 18 "reflect" 19 "testing" 20 21 "android/soong/android" 22) 23 24func testGenruleContext(config android.Config) *android.TestContext { 25 ctx := android.NewTestArchContext(config) 26 ctx.RegisterModuleType("cc_genrule", GenRuleFactory) 27 ctx.Register() 28 29 return ctx 30} 31 32func TestArchGenruleCmd(t *testing.T) { 33 fs := map[string][]byte{ 34 "tool": nil, 35 "foo": nil, 36 "bar": nil, 37 } 38 bp := ` 39 cc_genrule { 40 name: "gen", 41 tool_files: ["tool"], 42 cmd: "$(location tool) $(in) $(out)", 43 out: ["out_arm"], 44 arch: { 45 arm: { 46 srcs: ["foo"], 47 }, 48 arm64: { 49 srcs: ["bar"], 50 }, 51 }, 52 } 53 ` 54 config := android.TestArchConfig(t.TempDir(), nil, bp, fs) 55 56 ctx := testGenruleContext(config) 57 58 _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) 59 if errs == nil { 60 _, errs = ctx.PrepareBuildActions(config) 61 } 62 if errs != nil { 63 t.Fatal(errs) 64 } 65 66 gen := ctx.ModuleForTests("gen", "android_arm_armv7-a-neon").Output("out_arm") 67 expected := []string{"foo"} 68 if !reflect.DeepEqual(expected, gen.Implicits.Strings()[:len(expected)]) { 69 t.Errorf(`want arm inputs %v, got %v`, expected, gen.Implicits.Strings()) 70 } 71 72 gen = ctx.ModuleForTests("gen", "android_arm64_armv8-a").Output("out_arm") 73 expected = []string{"bar"} 74 if !reflect.DeepEqual(expected, gen.Implicits.Strings()[:len(expected)]) { 75 t.Errorf(`want arm64 inputs %v, got %v`, expected, gen.Implicits.Strings()) 76 } 77} 78 79func TestLibraryGenruleCmd(t *testing.T) { 80 bp := ` 81 cc_library { 82 name: "libboth", 83 } 84 85 cc_library_shared { 86 name: "libshared", 87 } 88 89 cc_library_static { 90 name: "libstatic", 91 } 92 93 cc_genrule { 94 name: "gen", 95 tool_files: ["tool"], 96 srcs: [ 97 ":libboth", 98 ":libshared", 99 ":libstatic", 100 ], 101 cmd: "$(location tool) $(in) $(out)", 102 out: ["out"], 103 } 104 ` 105 ctx := testCc(t, bp) 106 107 gen := ctx.ModuleForTests("gen", "android_arm_armv7-a-neon").Output("out") 108 expected := []string{"libboth.so", "libshared.so", "libstatic.a"} 109 var got []string 110 for _, input := range gen.Implicits { 111 got = append(got, input.Base()) 112 } 113 if !reflect.DeepEqual(expected, got[:len(expected)]) { 114 t.Errorf(`want inputs %v, got %v`, expected, got) 115 } 116} 117 118func TestCmdPrefix(t *testing.T) { 119 bp := ` 120 cc_genrule { 121 name: "gen", 122 cmd: "echo foo", 123 out: ["out"], 124 native_bridge_supported: true, 125 } 126 ` 127 128 testCases := []struct { 129 name string 130 variant string 131 preparer android.FixturePreparer 132 133 arch string 134 nativeBridge string 135 multilib string 136 }{ 137 { 138 name: "arm", 139 variant: "android_arm_armv7-a-neon", 140 arch: "arm", 141 multilib: "lib32", 142 }, 143 { 144 name: "arm64", 145 variant: "android_arm64_armv8-a", 146 arch: "arm64", 147 multilib: "lib64", 148 }, 149 { 150 name: "nativebridge", 151 variant: "android_native_bridge_arm_armv7-a-neon", 152 preparer: android.FixtureModifyConfig(func(config android.Config) { 153 config.Targets[android.Android] = []android.Target{ 154 { 155 Os: android.Android, 156 Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, 157 NativeBridge: android.NativeBridgeDisabled, 158 }, 159 { 160 Os: android.Android, 161 Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, 162 NativeBridge: android.NativeBridgeEnabled, 163 NativeBridgeHostArchName: "x86", 164 NativeBridgeRelativePath: "arm", 165 }, 166 } 167 }), 168 arch: "arm", 169 multilib: "lib32", 170 nativeBridge: "arm", 171 }, 172 } 173 174 for _, tt := range testCases { 175 t.Run(tt.name, func(t *testing.T) { 176 result := android.GroupFixturePreparers( 177 PrepareForIntegrationTestWithCc, 178 android.OptionalFixturePreparer(tt.preparer), 179 ).RunTestWithBp(t, bp) 180 gen := result.ModuleForTests("gen", tt.variant) 181 sboxProto := android.RuleBuilderSboxProtoForTests(t, gen.Output("genrule.sbox.textproto")) 182 cmd := *sboxProto.Commands[0].Command 183 android.AssertStringDoesContain(t, "incorrect CC_ARCH", cmd, "CC_ARCH="+tt.arch+" ") 184 android.AssertStringDoesContain(t, "incorrect CC_NATIVE_BRIDGE", cmd, "CC_NATIVE_BRIDGE="+tt.nativeBridge+" ") 185 android.AssertStringDoesContain(t, "incorrect CC_MULTILIB", cmd, "CC_MULTILIB="+tt.multilib+" ") 186 }) 187 } 188} 189