blob: 6c3cd9eefc018ddcb2d4148503fd6c00634d6bf5 [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 {
Martin Stjernholmf4677322020-07-07 02:20:40 +010025 name string
26 replaceBp bool // modules is added to default bp boilerplate if false.
27 modules string
28 prebuilt []OsType
Colin Crossce75d2c2016-10-06 16:12:58 -070029}{
30 {
31 name: "no prebuilt",
32 modules: `
33 source {
34 name: "bar",
35 }`,
Martin Stjernholm009a9dc2020-03-05 17:34:13 +000036 prebuilt: nil,
Colin Crossce75d2c2016-10-06 16:12:58 -070037 },
38 {
39 name: "no source prebuilt not preferred",
40 modules: `
41 prebuilt {
42 name: "bar",
43 prefer: false,
Jiyong Parkd4671112019-04-19 17:23:04 +090044 srcs: ["prebuilt_file"],
Colin Crossce75d2c2016-10-06 16:12:58 -070045 }`,
Martin Stjernholmf4677322020-07-07 02:20:40 +010046 prebuilt: []OsType{Android, BuildOs},
Colin Crossce75d2c2016-10-06 16:12:58 -070047 },
48 {
49 name: "no source prebuilt preferred",
50 modules: `
51 prebuilt {
52 name: "bar",
53 prefer: true,
Jiyong Parkd4671112019-04-19 17:23:04 +090054 srcs: ["prebuilt_file"],
Colin Crossce75d2c2016-10-06 16:12:58 -070055 }`,
Martin Stjernholmf4677322020-07-07 02:20:40 +010056 prebuilt: []OsType{Android, BuildOs},
Colin Crossce75d2c2016-10-06 16:12:58 -070057 },
58 {
59 name: "prebuilt not preferred",
60 modules: `
61 source {
62 name: "bar",
63 }
Martin Stjernholm009a9dc2020-03-05 17:34:13 +000064
Colin Crossce75d2c2016-10-06 16:12:58 -070065 prebuilt {
66 name: "bar",
67 prefer: false,
Jiyong Parkd4671112019-04-19 17:23:04 +090068 srcs: ["prebuilt_file"],
Colin Crossce75d2c2016-10-06 16:12:58 -070069 }`,
Martin Stjernholm009a9dc2020-03-05 17:34:13 +000070 prebuilt: nil,
Colin Crossce75d2c2016-10-06 16:12:58 -070071 },
72 {
73 name: "prebuilt preferred",
74 modules: `
75 source {
76 name: "bar",
77 }
Martin Stjernholm009a9dc2020-03-05 17:34:13 +000078
Colin Crossce75d2c2016-10-06 16:12:58 -070079 prebuilt {
80 name: "bar",
81 prefer: true,
Jiyong Parkd4671112019-04-19 17:23:04 +090082 srcs: ["prebuilt_file"],
Colin Crossce75d2c2016-10-06 16:12:58 -070083 }`,
Martin Stjernholmf4677322020-07-07 02:20:40 +010084 prebuilt: []OsType{Android, BuildOs},
Colin Crossce75d2c2016-10-06 16:12:58 -070085 },
Colin Cross0d614dd2016-10-14 15:38:43 -070086 {
87 name: "prebuilt no file not preferred",
88 modules: `
89 source {
90 name: "bar",
91 }
Martin Stjernholm009a9dc2020-03-05 17:34:13 +000092
Colin Cross0d614dd2016-10-14 15:38:43 -070093 prebuilt {
94 name: "bar",
95 prefer: false,
96 }`,
Martin Stjernholm009a9dc2020-03-05 17:34:13 +000097 prebuilt: nil,
Colin Cross0d614dd2016-10-14 15:38:43 -070098 },
99 {
100 name: "prebuilt no file preferred",
101 modules: `
102 source {
103 name: "bar",
104 }
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000105
Colin Cross0d614dd2016-10-14 15:38:43 -0700106 prebuilt {
107 name: "bar",
108 prefer: true,
109 }`,
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000110 prebuilt: nil,
Colin Cross0d614dd2016-10-14 15:38:43 -0700111 },
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700112 {
113 name: "prebuilt file from filegroup preferred",
114 modules: `
115 filegroup {
116 name: "fg",
Jiyong Parkd4671112019-04-19 17:23:04 +0900117 srcs: ["prebuilt_file"],
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700118 }
119 prebuilt {
120 name: "bar",
121 prefer: true,
122 srcs: [":fg"],
123 }`,
Martin Stjernholmf4677322020-07-07 02:20:40 +0100124 prebuilt: []OsType{Android, BuildOs},
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000125 },
126 {
127 name: "prebuilt module for device only",
128 modules: `
129 source {
130 name: "bar",
131 }
132
133 prebuilt {
134 name: "bar",
135 host_supported: false,
136 prefer: true,
137 srcs: ["prebuilt_file"],
138 }`,
Martin Stjernholmf4677322020-07-07 02:20:40 +0100139 prebuilt: []OsType{Android},
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000140 },
141 {
142 name: "prebuilt file for host only",
143 modules: `
144 source {
145 name: "bar",
146 }
147
148 prebuilt {
149 name: "bar",
150 prefer: true,
151 target: {
Paul Duffin69304cf2020-05-29 08:01:00 +0100152 host: {
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000153 srcs: ["prebuilt_file"],
154 },
155 },
156 }`,
Martin Stjernholmf4677322020-07-07 02:20:40 +0100157 prebuilt: []OsType{BuildOs},
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700158 },
Jaewoong Jungfb25a642020-06-11 10:37:51 -0700159 {
160 name: "prebuilt override not preferred",
161 modules: `
162 source {
163 name: "baz",
164 }
165
166 override_source {
167 name: "bar",
168 base: "baz",
169 }
170
171 prebuilt {
172 name: "bar",
173 prefer: false,
174 srcs: ["prebuilt_file"],
175 }`,
176 prebuilt: nil,
177 },
178 {
179 name: "prebuilt override preferred",
180 modules: `
181 source {
182 name: "baz",
183 }
184
185 override_source {
186 name: "bar",
187 base: "baz",
188 }
189
190 prebuilt {
191 name: "bar",
192 prefer: true,
193 srcs: ["prebuilt_file"],
194 }`,
Martin Stjernholmf4677322020-07-07 02:20:40 +0100195 prebuilt: []OsType{Android, BuildOs},
196 },
197 {
198 name: "prebuilt including default-disabled OS",
199 replaceBp: true,
200 modules: `
201 source {
202 name: "foo",
203 deps: [":bar"],
204 target: {
205 windows: {
206 enabled: true,
207 },
208 },
209 }
210
211 source {
212 name: "bar",
213 target: {
214 windows: {
215 enabled: true,
216 },
217 },
218 }
219
220 prebuilt {
221 name: "bar",
222 prefer: true,
223 srcs: ["prebuilt_file"],
224 target: {
225 windows: {
226 enabled: true,
227 },
228 },
229 }`,
230 prebuilt: []OsType{Android, BuildOs, Windows},
231 },
232 {
233 name: "fall back to source for default-disabled OS",
234 replaceBp: true,
235 modules: `
236 source {
237 name: "foo",
238 deps: [":bar"],
239 target: {
240 windows: {
241 enabled: true,
242 },
243 },
244 }
245
246 source {
247 name: "bar",
248 target: {
249 windows: {
250 enabled: true,
251 },
252 },
253 }
254
255 prebuilt {
256 name: "bar",
257 prefer: true,
258 srcs: ["prebuilt_file"],
259 }`,
260 prebuilt: []OsType{Android, BuildOs},
Jaewoong Jungfb25a642020-06-11 10:37:51 -0700261 },
Colin Crossce75d2c2016-10-06 16:12:58 -0700262}
263
264func TestPrebuilts(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800265 fs := map[string][]byte{
266 "prebuilt_file": nil,
267 "source_file": nil,
268 }
Colin Cross0d614dd2016-10-14 15:38:43 -0700269
Colin Crossce75d2c2016-10-06 16:12:58 -0700270 for _, test := range prebuiltsTests {
271 t.Run(test.name, func(t *testing.T) {
Martin Stjernholmf4677322020-07-07 02:20:40 +0100272 bp := test.modules
273 if !test.replaceBp {
274 bp = bp + `
275 source {
276 name: "foo",
277 deps: [":bar"],
278 }`
279 }
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000280 config := TestArchConfig(buildDir, nil, bp, fs)
Colin Cross98be1bb2019-12-13 20:41:13 -0800281
Martin Stjernholmf4677322020-07-07 02:20:40 +0100282 // Add windows to the target list to test the logic when a variant is
283 // disabled by default.
284 if !Windows.DefaultDisabled {
285 t.Errorf("windows is assumed to be disabled by default")
286 }
287 config.config.Targets[Windows] = []Target{
288 {Windows, Arch{ArchType: X86_64}, NativeBridgeDisabled, "", ""},
289 }
290
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000291 ctx := NewTestArchContext()
Paul Duffin78ac5b92020-01-14 12:42:08 +0000292 registerTestPrebuiltBuildComponents(ctx)
Colin Cross4b49b762019-11-22 15:25:03 -0800293 ctx.RegisterModuleType("filegroup", FileGroupFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800294 ctx.Register(config)
Colin Crossce75d2c2016-10-06 16:12:58 -0700295
Colin Cross98be1bb2019-12-13 20:41:13 -0800296 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800297 FailIfErrored(t, errs)
Colin Crossce75d2c2016-10-06 16:12:58 -0700298 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800299 FailIfErrored(t, errs)
Colin Crossce75d2c2016-10-06 16:12:58 -0700300
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000301 for _, variant := range ctx.ModuleVariantsForTests("foo") {
302 foo := ctx.ModuleForTests("foo", variant)
Martin Stjernholmf4677322020-07-07 02:20:40 +0100303 t.Run(foo.Module().Target().Os.String(), func(t *testing.T) {
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000304 var dependsOnSourceModule, dependsOnPrebuiltModule bool
305 ctx.VisitDirectDeps(foo.Module(), func(m blueprint.Module) {
306 if _, ok := m.(*sourceModule); ok {
307 dependsOnSourceModule = true
308 }
309 if p, ok := m.(*prebuiltModule); ok {
310 dependsOnPrebuiltModule = true
311 if !p.Prebuilt().properties.UsePrebuilt {
312 t.Errorf("dependency on prebuilt module not marked used")
313 }
314 }
315 })
Colin Crossce75d2c2016-10-06 16:12:58 -0700316
Martin Stjernholmf4677322020-07-07 02:20:40 +0100317 moduleIsDisabled := !foo.Module().Enabled()
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000318 deps := foo.Module().(*sourceModule).deps
Martin Stjernholmf4677322020-07-07 02:20:40 +0100319 if moduleIsDisabled {
320 if len(deps) > 0 {
321 t.Errorf("disabled module got deps: %v", deps)
322 }
323 } else {
324 if len(deps) != 1 {
325 t.Errorf("deps does not have single path, but is %v", deps)
326 }
Colin Crossc3e7fa62017-03-17 13:14:32 -0700327 }
Martin Stjernholmf4677322020-07-07 02:20:40 +0100328
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000329 var usingSourceFile, usingPrebuiltFile bool
Martin Stjernholmf4677322020-07-07 02:20:40 +0100330 if len(deps) > 0 && deps[0].String() == "source_file" {
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000331 usingSourceFile = true
332 }
Martin Stjernholmf4677322020-07-07 02:20:40 +0100333 if len(deps) > 0 && deps[0].String() == "prebuilt_file" {
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000334 usingPrebuiltFile = true
335 }
Colin Crossc3e7fa62017-03-17 13:14:32 -0700336
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000337 prebuilt := false
338 for _, os := range test.prebuilt {
Martin Stjernholmf4677322020-07-07 02:20:40 +0100339 if os == foo.Module().Target().Os {
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000340 prebuilt = true
341 }
342 }
Jiyong Parkd4671112019-04-19 17:23:04 +0900343
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000344 if prebuilt {
Martin Stjernholmf4677322020-07-07 02:20:40 +0100345 if moduleIsDisabled {
346 t.Errorf("dependent module for prebuilt is disabled")
347 }
348
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000349 if !dependsOnPrebuiltModule {
350 t.Errorf("doesn't depend on prebuilt module")
351 }
352 if !usingPrebuiltFile {
353 t.Errorf("doesn't use prebuilt_file")
354 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700355
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000356 if dependsOnSourceModule {
357 t.Errorf("depends on source module")
358 }
359 if usingSourceFile {
360 t.Errorf("using source_file")
361 }
Martin Stjernholmf4677322020-07-07 02:20:40 +0100362 } else if !moduleIsDisabled {
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000363 if dependsOnPrebuiltModule {
364 t.Errorf("depends on prebuilt module")
365 }
366 if usingPrebuiltFile {
367 t.Errorf("using prebuilt_file")
368 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700369
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000370 if !dependsOnSourceModule {
371 t.Errorf("doesn't depend on source module")
372 }
373 if !usingSourceFile {
374 t.Errorf("doesn't use source_file")
375 }
376 }
377 })
Colin Crossce75d2c2016-10-06 16:12:58 -0700378 }
379 })
380 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700381}
382
Paul Duffin78ac5b92020-01-14 12:42:08 +0000383func registerTestPrebuiltBuildComponents(ctx RegistrationContext) {
384 ctx.RegisterModuleType("prebuilt", newPrebuiltModule)
385 ctx.RegisterModuleType("source", newSourceModule)
Jaewoong Jungfb25a642020-06-11 10:37:51 -0700386 ctx.RegisterModuleType("override_source", newOverrideSourceModule)
Paul Duffin78ac5b92020-01-14 12:42:08 +0000387
388 RegisterPrebuiltMutators(ctx)
Jaewoong Jungfb25a642020-06-11 10:37:51 -0700389 ctx.PostDepsMutators(RegisterOverridePostDepsMutators)
Paul Duffin78ac5b92020-01-14 12:42:08 +0000390}
391
Colin Crossce75d2c2016-10-06 16:12:58 -0700392type prebuiltModule struct {
393 ModuleBase
Colin Cross74d73e22017-08-02 11:05:49 -0700394 prebuilt Prebuilt
395 properties struct {
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000396 Srcs []string `android:"path,arch_variant"`
Colin Cross74d73e22017-08-02 11:05:49 -0700397 }
Jiyong Parkd4671112019-04-19 17:23:04 +0900398 src Path
Colin Crossce75d2c2016-10-06 16:12:58 -0700399}
400
Colin Cross36242852017-06-23 15:06:31 -0700401func newPrebuiltModule() Module {
Colin Crossce75d2c2016-10-06 16:12:58 -0700402 m := &prebuiltModule{}
Colin Cross74d73e22017-08-02 11:05:49 -0700403 m.AddProperties(&m.properties)
404 InitPrebuiltModule(m, &m.properties.Srcs)
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000405 InitAndroidArchModule(m, HostAndDeviceDefault, MultilibCommon)
Colin Cross36242852017-06-23 15:06:31 -0700406 return m
Colin Crossce75d2c2016-10-06 16:12:58 -0700407}
408
409func (p *prebuiltModule) Name() string {
410 return p.prebuilt.Name(p.ModuleBase.Name())
411}
412
Jiyong Parkd4671112019-04-19 17:23:04 +0900413func (p *prebuiltModule) GenerateAndroidBuildActions(ctx ModuleContext) {
414 if len(p.properties.Srcs) >= 1 {
415 p.src = p.prebuilt.SingleSourcePath(ctx)
416 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700417}
418
419func (p *prebuiltModule) Prebuilt() *Prebuilt {
420 return &p.prebuilt
421}
422
Colin Cross41955e82019-05-29 14:40:35 -0700423func (p *prebuiltModule) OutputFiles(tag string) (Paths, error) {
424 switch tag {
425 case "":
426 return Paths{p.src}, nil
427 default:
428 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
429 }
Jiyong Parkd4671112019-04-19 17:23:04 +0900430}
431
Jaewoong Jungfb25a642020-06-11 10:37:51 -0700432type sourceModuleProperties struct {
433 Deps []string `android:"path,arch_variant"`
434}
435
Colin Crossce75d2c2016-10-06 16:12:58 -0700436type sourceModule struct {
437 ModuleBase
Jaewoong Jungfb25a642020-06-11 10:37:51 -0700438 OverridableModuleBase
439
440 properties sourceModuleProperties
Colin Crossce75d2c2016-10-06 16:12:58 -0700441 dependsOnSourceModule, dependsOnPrebuiltModule bool
Jiyong Parkd4671112019-04-19 17:23:04 +0900442 deps Paths
443 src Path
Colin Crossce75d2c2016-10-06 16:12:58 -0700444}
445
Colin Cross36242852017-06-23 15:06:31 -0700446func newSourceModule() Module {
Colin Crossce75d2c2016-10-06 16:12:58 -0700447 m := &sourceModule{}
Colin Cross36242852017-06-23 15:06:31 -0700448 m.AddProperties(&m.properties)
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000449 InitAndroidArchModule(m, HostAndDeviceDefault, MultilibCommon)
Jaewoong Jungfb25a642020-06-11 10:37:51 -0700450 InitOverridableModule(m, nil)
Colin Cross36242852017-06-23 15:06:31 -0700451 return m
Colin Crossce75d2c2016-10-06 16:12:58 -0700452}
453
Jaewoong Jungfb25a642020-06-11 10:37:51 -0700454func (s *sourceModule) OverridablePropertiesDepsMutator(ctx BottomUpMutatorContext) {
Jiyong Parkd4671112019-04-19 17:23:04 +0900455 // s.properties.Deps are annotated with android:path, so they are
456 // automatically added to the dependency by pathDeps mutator
Colin Crossce75d2c2016-10-06 16:12:58 -0700457}
458
459func (s *sourceModule) GenerateAndroidBuildActions(ctx ModuleContext) {
Jiyong Parkd4671112019-04-19 17:23:04 +0900460 s.deps = PathsForModuleSrc(ctx, s.properties.Deps)
461 s.src = PathForModuleSrc(ctx, "source_file")
462}
463
464func (s *sourceModule) Srcs() Paths {
465 return Paths{s.src}
Colin Crossce75d2c2016-10-06 16:12:58 -0700466}
Jaewoong Jungfb25a642020-06-11 10:37:51 -0700467
468type overrideSourceModule struct {
469 ModuleBase
470 OverrideModuleBase
471}
472
473func (o *overrideSourceModule) GenerateAndroidBuildActions(_ ModuleContext) {
474}
475
476func newOverrideSourceModule() Module {
477 m := &overrideSourceModule{}
478 m.AddProperties(&sourceModuleProperties{})
479
480 InitAndroidArchModule(m, HostAndDeviceDefault, MultilibCommon)
481 InitOverrideModule(m)
482 return m
483}