blob: 0d16e626190da7a8353fb12773c0ca63f6bc40c5 [file] [log] [blame]
Colin Crossef354482018-10-23 11:27:50 -07001// 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
Colin Cross98be1bb2019-12-13 20:41:13 -080024func testGenruleContext(config android.Config) *android.TestContext {
Colin Crossae8600b2020-10-29 17:09:13 -070025 ctx := android.NewTestArchContext(config)
Wei Libcd39942021-09-16 23:57:28 +000026 ctx.RegisterModuleType("cc_genrule", GenRuleFactory)
Colin Crossae8600b2020-10-29 17:09:13 -070027 ctx.Register()
Colin Crossef354482018-10-23 11:27:50 -070028
29 return ctx
30}
31
32func TestArchGenruleCmd(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -080033 fs := map[string][]byte{
34 "tool": nil,
35 "foo": nil,
36 "bar": nil,
37 }
Colin Crossef354482018-10-23 11:27:50 -070038 bp := `
39 cc_genrule {
40 name: "gen",
41 tool_files: ["tool"],
42 cmd: "$(location tool) $(in) $(out)",
Yu Liud6201012022-10-17 12:29:15 -070043 out: ["out_arm"],
Colin Crossef354482018-10-23 11:27:50 -070044 arch: {
45 arm: {
46 srcs: ["foo"],
Colin Crossef354482018-10-23 11:27:50 -070047 },
48 arm64: {
49 srcs: ["bar"],
Colin Crossef354482018-10-23 11:27:50 -070050 },
51 },
52 }
53 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +000054 config := android.TestArchConfig(t.TempDir(), nil, bp, fs)
Colin Crossef354482018-10-23 11:27:50 -070055
Colin Cross98be1bb2019-12-13 20:41:13 -080056 ctx := testGenruleContext(config)
Colin Crossef354482018-10-23 11:27:50 -070057
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
Colin Cross7113d202019-11-20 16:39:12 -080066 gen := ctx.ModuleForTests("gen", "android_arm_armv7-a-neon").Output("out_arm")
Colin Crossef354482018-10-23 11:27:50 -070067 expected := []string{"foo"}
Colin Cross3d680512020-11-13 16:23:53 -080068 if !reflect.DeepEqual(expected, gen.Implicits.Strings()[:len(expected)]) {
69 t.Errorf(`want arm inputs %v, got %v`, expected, gen.Implicits.Strings())
Colin Crossef354482018-10-23 11:27:50 -070070 }
71
Yu Liud6201012022-10-17 12:29:15 -070072 gen = ctx.ModuleForTests("gen", "android_arm64_armv8-a").Output("out_arm")
Colin Crossef354482018-10-23 11:27:50 -070073 expected = []string{"bar"}
Colin Cross3d680512020-11-13 16:23:53 -080074 if !reflect.DeepEqual(expected, gen.Implicits.Strings()[:len(expected)]) {
75 t.Errorf(`want arm64 inputs %v, got %v`, expected, gen.Implicits.Strings())
Colin Crossef354482018-10-23 11:27:50 -070076 }
77}
Colin Cross81ca6cd2020-08-06 17:46:48 -070078
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
Colin Cross3d680512020-11-13 16:23:53 -0800110 for _, input := range gen.Implicits {
Colin Cross81ca6cd2020-08-06 17:46:48 -0700111 got = append(got, input.Base())
112 }
Colin Cross3d680512020-11-13 16:23:53 -0800113 if !reflect.DeepEqual(expected, got[:len(expected)]) {
Colin Cross81ca6cd2020-08-06 17:46:48 -0700114 t.Errorf(`want inputs %v, got %v`, expected, got)
115 }
116}
Colin Crossf3bfd022021-09-27 15:15:06 -0700117
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}