blob: b568f781d0912ec07ae8a45d91a6ed1c278cbca8 [file] [log] [blame]
Colin Crossce75d2c2016-10-06 16:12:58 -07001// Copyright 2016 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 android
16
17import (
Colin Cross41955e82019-05-29 14:40:35 -070018 "fmt"
Colin Crossce75d2c2016-10-06 16:12:58 -070019 "testing"
20
21 "github.com/google/blueprint"
22)
23
24var prebuiltsTests = []struct {
25 name string
26 modules string
Martin Stjernholm009a9dc2020-03-05 17:34:13 +000027 prebuilt []OsClass
Colin Crossce75d2c2016-10-06 16:12:58 -070028}{
29 {
30 name: "no prebuilt",
31 modules: `
32 source {
33 name: "bar",
34 }`,
Martin Stjernholm009a9dc2020-03-05 17:34:13 +000035 prebuilt: nil,
Colin Crossce75d2c2016-10-06 16:12:58 -070036 },
37 {
38 name: "no source prebuilt not preferred",
39 modules: `
40 prebuilt {
41 name: "bar",
42 prefer: false,
Jiyong Parkd4671112019-04-19 17:23:04 +090043 srcs: ["prebuilt_file"],
Colin Crossce75d2c2016-10-06 16:12:58 -070044 }`,
Martin Stjernholm009a9dc2020-03-05 17:34:13 +000045 prebuilt: []OsClass{Device, Host},
Colin Crossce75d2c2016-10-06 16:12:58 -070046 },
47 {
48 name: "no source prebuilt preferred",
49 modules: `
50 prebuilt {
51 name: "bar",
52 prefer: true,
Jiyong Parkd4671112019-04-19 17:23:04 +090053 srcs: ["prebuilt_file"],
Colin Crossce75d2c2016-10-06 16:12:58 -070054 }`,
Martin Stjernholm009a9dc2020-03-05 17:34:13 +000055 prebuilt: []OsClass{Device, Host},
Colin Crossce75d2c2016-10-06 16:12:58 -070056 },
57 {
58 name: "prebuilt not preferred",
59 modules: `
60 source {
61 name: "bar",
62 }
Martin Stjernholm009a9dc2020-03-05 17:34:13 +000063
Colin Crossce75d2c2016-10-06 16:12:58 -070064 prebuilt {
65 name: "bar",
66 prefer: false,
Jiyong Parkd4671112019-04-19 17:23:04 +090067 srcs: ["prebuilt_file"],
Colin Crossce75d2c2016-10-06 16:12:58 -070068 }`,
Martin Stjernholm009a9dc2020-03-05 17:34:13 +000069 prebuilt: nil,
Colin Crossce75d2c2016-10-06 16:12:58 -070070 },
71 {
72 name: "prebuilt preferred",
73 modules: `
74 source {
75 name: "bar",
76 }
Martin Stjernholm009a9dc2020-03-05 17:34:13 +000077
Colin Crossce75d2c2016-10-06 16:12:58 -070078 prebuilt {
79 name: "bar",
80 prefer: true,
Jiyong Parkd4671112019-04-19 17:23:04 +090081 srcs: ["prebuilt_file"],
Colin Crossce75d2c2016-10-06 16:12:58 -070082 }`,
Martin Stjernholm009a9dc2020-03-05 17:34:13 +000083 prebuilt: []OsClass{Device, Host},
Colin Crossce75d2c2016-10-06 16:12:58 -070084 },
Colin Cross0d614dd2016-10-14 15:38:43 -070085 {
86 name: "prebuilt no file not preferred",
87 modules: `
88 source {
89 name: "bar",
90 }
Martin Stjernholm009a9dc2020-03-05 17:34:13 +000091
Colin Cross0d614dd2016-10-14 15:38:43 -070092 prebuilt {
93 name: "bar",
94 prefer: false,
95 }`,
Martin Stjernholm009a9dc2020-03-05 17:34:13 +000096 prebuilt: nil,
Colin Cross0d614dd2016-10-14 15:38:43 -070097 },
98 {
99 name: "prebuilt no file preferred",
100 modules: `
101 source {
102 name: "bar",
103 }
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000104
Colin Cross0d614dd2016-10-14 15:38:43 -0700105 prebuilt {
106 name: "bar",
107 prefer: true,
108 }`,
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000109 prebuilt: nil,
Colin Cross0d614dd2016-10-14 15:38:43 -0700110 },
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700111 {
112 name: "prebuilt file from filegroup preferred",
113 modules: `
114 filegroup {
115 name: "fg",
Jiyong Parkd4671112019-04-19 17:23:04 +0900116 srcs: ["prebuilt_file"],
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700117 }
118 prebuilt {
119 name: "bar",
120 prefer: true,
121 srcs: [":fg"],
122 }`,
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000123 prebuilt: []OsClass{Device, Host},
124 },
125 {
126 name: "prebuilt module for device only",
127 modules: `
128 source {
129 name: "bar",
130 }
131
132 prebuilt {
133 name: "bar",
134 host_supported: false,
135 prefer: true,
136 srcs: ["prebuilt_file"],
137 }`,
138 prebuilt: []OsClass{Device},
139 },
140 {
141 name: "prebuilt file for host only",
142 modules: `
143 source {
144 name: "bar",
145 }
146
147 prebuilt {
148 name: "bar",
149 prefer: true,
150 target: {
Paul Duffin69304cf2020-05-29 08:01:00 +0100151 host: {
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000152 srcs: ["prebuilt_file"],
153 },
154 },
155 }`,
156 prebuilt: []OsClass{Host},
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700157 },
Colin Crossce75d2c2016-10-06 16:12:58 -0700158}
159
160func TestPrebuilts(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800161 fs := map[string][]byte{
162 "prebuilt_file": nil,
163 "source_file": nil,
164 }
Colin Cross0d614dd2016-10-14 15:38:43 -0700165
Colin Crossce75d2c2016-10-06 16:12:58 -0700166 for _, test := range prebuiltsTests {
167 t.Run(test.name, func(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800168 bp := `
169 source {
170 name: "foo",
171 deps: [":bar"],
172 }
173 ` + test.modules
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000174 config := TestArchConfig(buildDir, nil, bp, fs)
Colin Cross98be1bb2019-12-13 20:41:13 -0800175
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000176 ctx := NewTestArchContext()
Paul Duffin78ac5b92020-01-14 12:42:08 +0000177 registerTestPrebuiltBuildComponents(ctx)
Colin Cross4b49b762019-11-22 15:25:03 -0800178 ctx.RegisterModuleType("filegroup", FileGroupFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800179 ctx.Register(config)
Colin Crossce75d2c2016-10-06 16:12:58 -0700180
Colin Cross98be1bb2019-12-13 20:41:13 -0800181 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800182 FailIfErrored(t, errs)
Colin Crossce75d2c2016-10-06 16:12:58 -0700183 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800184 FailIfErrored(t, errs)
Colin Crossce75d2c2016-10-06 16:12:58 -0700185
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000186 for _, variant := range ctx.ModuleVariantsForTests("foo") {
187 foo := ctx.ModuleForTests("foo", variant)
188 t.Run(foo.Module().Target().Os.Class.String(), func(t *testing.T) {
189 var dependsOnSourceModule, dependsOnPrebuiltModule bool
190 ctx.VisitDirectDeps(foo.Module(), func(m blueprint.Module) {
191 if _, ok := m.(*sourceModule); ok {
192 dependsOnSourceModule = true
193 }
194 if p, ok := m.(*prebuiltModule); ok {
195 dependsOnPrebuiltModule = true
196 if !p.Prebuilt().properties.UsePrebuilt {
197 t.Errorf("dependency on prebuilt module not marked used")
198 }
199 }
200 })
Colin Crossce75d2c2016-10-06 16:12:58 -0700201
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000202 deps := foo.Module().(*sourceModule).deps
203 if deps == nil || len(deps) != 1 {
204 t.Errorf("deps does not have single path, but is %v", deps)
Colin Crossc3e7fa62017-03-17 13:14:32 -0700205 }
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000206 var usingSourceFile, usingPrebuiltFile bool
207 if deps[0].String() == "source_file" {
208 usingSourceFile = true
209 }
210 if deps[0].String() == "prebuilt_file" {
211 usingPrebuiltFile = true
212 }
Colin Crossc3e7fa62017-03-17 13:14:32 -0700213
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000214 prebuilt := false
215 for _, os := range test.prebuilt {
216 if os == foo.Module().Target().Os.Class {
217 prebuilt = true
218 }
219 }
Jiyong Parkd4671112019-04-19 17:23:04 +0900220
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000221 if prebuilt {
222 if !dependsOnPrebuiltModule {
223 t.Errorf("doesn't depend on prebuilt module")
224 }
225 if !usingPrebuiltFile {
226 t.Errorf("doesn't use prebuilt_file")
227 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700228
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000229 if dependsOnSourceModule {
230 t.Errorf("depends on source module")
231 }
232 if usingSourceFile {
233 t.Errorf("using source_file")
234 }
235 } else {
236 if dependsOnPrebuiltModule {
237 t.Errorf("depends on prebuilt module")
238 }
239 if usingPrebuiltFile {
240 t.Errorf("using prebuilt_file")
241 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700242
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000243 if !dependsOnSourceModule {
244 t.Errorf("doesn't depend on source module")
245 }
246 if !usingSourceFile {
247 t.Errorf("doesn't use source_file")
248 }
249 }
250 })
Colin Crossce75d2c2016-10-06 16:12:58 -0700251 }
252 })
253 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700254}
255
Paul Duffin78ac5b92020-01-14 12:42:08 +0000256func registerTestPrebuiltBuildComponents(ctx RegistrationContext) {
257 ctx.RegisterModuleType("prebuilt", newPrebuiltModule)
258 ctx.RegisterModuleType("source", newSourceModule)
259
260 RegisterPrebuiltMutators(ctx)
261}
262
Colin Crossce75d2c2016-10-06 16:12:58 -0700263type prebuiltModule struct {
264 ModuleBase
Colin Cross74d73e22017-08-02 11:05:49 -0700265 prebuilt Prebuilt
266 properties struct {
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000267 Srcs []string `android:"path,arch_variant"`
Colin Cross74d73e22017-08-02 11:05:49 -0700268 }
Jiyong Parkd4671112019-04-19 17:23:04 +0900269 src Path
Colin Crossce75d2c2016-10-06 16:12:58 -0700270}
271
Colin Cross36242852017-06-23 15:06:31 -0700272func newPrebuiltModule() Module {
Colin Crossce75d2c2016-10-06 16:12:58 -0700273 m := &prebuiltModule{}
Colin Cross74d73e22017-08-02 11:05:49 -0700274 m.AddProperties(&m.properties)
275 InitPrebuiltModule(m, &m.properties.Srcs)
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000276 InitAndroidArchModule(m, HostAndDeviceDefault, MultilibCommon)
Colin Cross36242852017-06-23 15:06:31 -0700277 return m
Colin Crossce75d2c2016-10-06 16:12:58 -0700278}
279
280func (p *prebuiltModule) Name() string {
281 return p.prebuilt.Name(p.ModuleBase.Name())
282}
283
Jiyong Parkd4671112019-04-19 17:23:04 +0900284func (p *prebuiltModule) GenerateAndroidBuildActions(ctx ModuleContext) {
285 if len(p.properties.Srcs) >= 1 {
286 p.src = p.prebuilt.SingleSourcePath(ctx)
287 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700288}
289
290func (p *prebuiltModule) Prebuilt() *Prebuilt {
291 return &p.prebuilt
292}
293
Colin Cross41955e82019-05-29 14:40:35 -0700294func (p *prebuiltModule) OutputFiles(tag string) (Paths, error) {
295 switch tag {
296 case "":
297 return Paths{p.src}, nil
298 default:
299 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
300 }
Jiyong Parkd4671112019-04-19 17:23:04 +0900301}
302
Colin Crossce75d2c2016-10-06 16:12:58 -0700303type sourceModule struct {
304 ModuleBase
305 properties struct {
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000306 Deps []string `android:"path,arch_variant"`
Colin Crossce75d2c2016-10-06 16:12:58 -0700307 }
308 dependsOnSourceModule, dependsOnPrebuiltModule bool
Jiyong Parkd4671112019-04-19 17:23:04 +0900309 deps Paths
310 src Path
Colin Crossce75d2c2016-10-06 16:12:58 -0700311}
312
Colin Cross36242852017-06-23 15:06:31 -0700313func newSourceModule() Module {
Colin Crossce75d2c2016-10-06 16:12:58 -0700314 m := &sourceModule{}
Colin Cross36242852017-06-23 15:06:31 -0700315 m.AddProperties(&m.properties)
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000316 InitAndroidArchModule(m, HostAndDeviceDefault, MultilibCommon)
Colin Cross36242852017-06-23 15:06:31 -0700317 return m
Colin Crossce75d2c2016-10-06 16:12:58 -0700318}
319
320func (s *sourceModule) DepsMutator(ctx BottomUpMutatorContext) {
Jiyong Parkd4671112019-04-19 17:23:04 +0900321 // s.properties.Deps are annotated with android:path, so they are
322 // automatically added to the dependency by pathDeps mutator
Colin Crossce75d2c2016-10-06 16:12:58 -0700323}
324
325func (s *sourceModule) GenerateAndroidBuildActions(ctx ModuleContext) {
Jiyong Parkd4671112019-04-19 17:23:04 +0900326 s.deps = PathsForModuleSrc(ctx, s.properties.Deps)
327 s.src = PathForModuleSrc(ctx, "source_file")
328}
329
330func (s *sourceModule) Srcs() Paths {
331 return Paths{s.src}
Colin Crossce75d2c2016-10-06 16:12:58 -0700332}