blob: 319c15dfa8579d64dc1d637d83e639e4641684bc [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 Cross0d614dd2016-10-14 15:38:43 -070018 "io/ioutil"
19 "os"
Colin Crossce75d2c2016-10-06 16:12:58 -070020 "testing"
21
22 "github.com/google/blueprint"
23)
24
25var prebuiltsTests = []struct {
26 name string
27 modules string
28 prebuilt bool
29}{
30 {
31 name: "no prebuilt",
32 modules: `
33 source {
34 name: "bar",
35 }`,
36 prebuilt: false,
37 },
38 {
39 name: "no source prebuilt not preferred",
40 modules: `
41 prebuilt {
42 name: "bar",
43 prefer: false,
44 srcs: ["prebuilt"],
45 }`,
46 prebuilt: true,
47 },
48 {
49 name: "no source prebuilt preferred",
50 modules: `
51 prebuilt {
52 name: "bar",
53 prefer: true,
54 srcs: ["prebuilt"],
55 }`,
56 prebuilt: true,
57 },
58 {
59 name: "prebuilt not preferred",
60 modules: `
61 source {
62 name: "bar",
63 }
64
65 prebuilt {
66 name: "bar",
67 prefer: false,
68 srcs: ["prebuilt"],
69 }`,
70 prebuilt: false,
71 },
72 {
73 name: "prebuilt preferred",
74 modules: `
75 source {
76 name: "bar",
77 }
78
79 prebuilt {
80 name: "bar",
81 prefer: true,
82 srcs: ["prebuilt"],
83 }`,
84 prebuilt: true,
85 },
Colin Cross0d614dd2016-10-14 15:38:43 -070086 {
87 name: "prebuilt no file not preferred",
88 modules: `
89 source {
90 name: "bar",
91 }
92
93 prebuilt {
94 name: "bar",
95 prefer: false,
96 }`,
97 prebuilt: false,
98 },
99 {
100 name: "prebuilt no file preferred",
101 modules: `
102 source {
103 name: "bar",
104 }
105
106 prebuilt {
107 name: "bar",
108 prefer: true,
109 }`,
110 prebuilt: false,
111 },
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700112 {
113 name: "prebuilt file from filegroup preferred",
114 modules: `
115 filegroup {
116 name: "fg",
117 }
118 prebuilt {
119 name: "bar",
120 prefer: true,
121 srcs: [":fg"],
122 }`,
123 prebuilt: true,
124 },
Colin Crossce75d2c2016-10-06 16:12:58 -0700125}
126
127func TestPrebuilts(t *testing.T) {
Colin Cross0d614dd2016-10-14 15:38:43 -0700128 buildDir, err := ioutil.TempDir("", "soong_prebuilt_test")
129 if err != nil {
130 t.Fatal(err)
131 }
132 defer os.RemoveAll(buildDir)
133
Colin Cross6ccbc912017-10-10 23:07:38 -0700134 config := TestConfig(buildDir, nil)
Colin Cross0d614dd2016-10-14 15:38:43 -0700135
Colin Crossce75d2c2016-10-06 16:12:58 -0700136 for _, test := range prebuiltsTests {
137 t.Run(test.name, func(t *testing.T) {
Colin Crosscec81712017-07-13 14:43:27 -0700138 ctx := NewTestContext()
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700139 ctx.PreArchMutators(RegisterPrebuiltsPreArchMutators)
140 ctx.PostDepsMutators(RegisterPrebuiltsPostDepsMutators)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700141 ctx.RegisterModuleType("filegroup", ModuleFactoryAdaptor(FileGroupFactory))
Colin Cross36242852017-06-23 15:06:31 -0700142 ctx.RegisterModuleType("prebuilt", ModuleFactoryAdaptor(newPrebuiltModule))
143 ctx.RegisterModuleType("source", ModuleFactoryAdaptor(newSourceModule))
Colin Crosscec81712017-07-13 14:43:27 -0700144 ctx.Register()
Colin Crossce75d2c2016-10-06 16:12:58 -0700145 ctx.MockFileSystem(map[string][]byte{
146 "Blueprints": []byte(`
147 source {
148 name: "foo",
149 deps: ["bar"],
150 }
151 ` + test.modules),
152 })
153
Colin Crossce75d2c2016-10-06 16:12:58 -0700154 _, errs := ctx.ParseBlueprintsFiles("Blueprints")
Logan Chien42039712018-03-12 16:29:17 +0800155 FailIfErrored(t, errs)
Colin Crossce75d2c2016-10-06 16:12:58 -0700156 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800157 FailIfErrored(t, errs)
Colin Crossce75d2c2016-10-06 16:12:58 -0700158
Colin Crosscec81712017-07-13 14:43:27 -0700159 foo := ctx.ModuleForTests("foo", "")
Colin Crossce75d2c2016-10-06 16:12:58 -0700160
Colin Crossc3e7fa62017-03-17 13:14:32 -0700161 var dependsOnSourceModule, dependsOnPrebuiltModule bool
Colin Crosscec81712017-07-13 14:43:27 -0700162 ctx.VisitDirectDeps(foo.Module(), func(m blueprint.Module) {
Colin Crossc3e7fa62017-03-17 13:14:32 -0700163 if _, ok := m.(*sourceModule); ok {
164 dependsOnSourceModule = true
165 }
166 if p, ok := m.(*prebuiltModule); ok {
167 dependsOnPrebuiltModule = true
Colin Cross74d73e22017-08-02 11:05:49 -0700168 if !p.Prebuilt().properties.UsePrebuilt {
Colin Crossc3e7fa62017-03-17 13:14:32 -0700169 t.Errorf("dependency on prebuilt module not marked used")
170 }
171 }
172 })
173
Colin Crossce75d2c2016-10-06 16:12:58 -0700174 if test.prebuilt {
Colin Crossc3e7fa62017-03-17 13:14:32 -0700175 if !dependsOnPrebuiltModule {
Colin Crossce75d2c2016-10-06 16:12:58 -0700176 t.Errorf("doesn't depend on prebuilt module")
177 }
178
Colin Crossc3e7fa62017-03-17 13:14:32 -0700179 if dependsOnSourceModule {
Colin Crossce75d2c2016-10-06 16:12:58 -0700180 t.Errorf("depends on source module")
181 }
182 } else {
Colin Crossc3e7fa62017-03-17 13:14:32 -0700183 if dependsOnPrebuiltModule {
Colin Crossce75d2c2016-10-06 16:12:58 -0700184 t.Errorf("depends on prebuilt module")
185 }
186
Colin Crossc3e7fa62017-03-17 13:14:32 -0700187 if !dependsOnSourceModule {
Colin Crossce75d2c2016-10-06 16:12:58 -0700188 t.Errorf("doens't depend on source module")
189 }
190 }
191 })
192 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700193}
194
195type prebuiltModule struct {
196 ModuleBase
Colin Cross74d73e22017-08-02 11:05:49 -0700197 prebuilt Prebuilt
198 properties struct {
Colin Cross27b922f2019-03-04 22:35:41 -0800199 Srcs []string `android:"path"`
Colin Cross74d73e22017-08-02 11:05:49 -0700200 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700201}
202
Colin Cross36242852017-06-23 15:06:31 -0700203func newPrebuiltModule() Module {
Colin Crossce75d2c2016-10-06 16:12:58 -0700204 m := &prebuiltModule{}
Colin Cross74d73e22017-08-02 11:05:49 -0700205 m.AddProperties(&m.properties)
206 InitPrebuiltModule(m, &m.properties.Srcs)
Colin Cross36242852017-06-23 15:06:31 -0700207 InitAndroidModule(m)
208 return m
Colin Crossce75d2c2016-10-06 16:12:58 -0700209}
210
211func (p *prebuiltModule) Name() string {
212 return p.prebuilt.Name(p.ModuleBase.Name())
213}
214
Colin Crossce75d2c2016-10-06 16:12:58 -0700215func (p *prebuiltModule) GenerateAndroidBuildActions(ModuleContext) {
216}
217
218func (p *prebuiltModule) Prebuilt() *Prebuilt {
219 return &p.prebuilt
220}
221
222type sourceModule struct {
223 ModuleBase
224 properties struct {
225 Deps []string
226 }
227 dependsOnSourceModule, dependsOnPrebuiltModule bool
228}
229
Colin Cross36242852017-06-23 15:06:31 -0700230func newSourceModule() Module {
Colin Crossce75d2c2016-10-06 16:12:58 -0700231 m := &sourceModule{}
Colin Cross36242852017-06-23 15:06:31 -0700232 m.AddProperties(&m.properties)
233 InitAndroidModule(m)
234 return m
Colin Crossce75d2c2016-10-06 16:12:58 -0700235}
236
237func (s *sourceModule) DepsMutator(ctx BottomUpMutatorContext) {
238 for _, d := range s.properties.Deps {
239 ctx.AddDependency(ctx.Module(), nil, d)
240 }
241}
242
243func (s *sourceModule) GenerateAndroidBuildActions(ctx ModuleContext) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700244}