Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame^] | 18 | "fmt" |
Colin Cross | 0d614dd | 2016-10-14 15:38:43 -0700 | [diff] [blame] | 19 | "io/ioutil" |
| 20 | "os" |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 21 | "testing" |
| 22 | |
| 23 | "github.com/google/blueprint" |
| 24 | ) |
| 25 | |
| 26 | var prebuiltsTests = []struct { |
| 27 | name string |
| 28 | modules string |
| 29 | prebuilt bool |
| 30 | }{ |
| 31 | { |
| 32 | name: "no prebuilt", |
| 33 | modules: ` |
| 34 | source { |
| 35 | name: "bar", |
| 36 | }`, |
| 37 | prebuilt: false, |
| 38 | }, |
| 39 | { |
| 40 | name: "no source prebuilt not preferred", |
| 41 | modules: ` |
| 42 | prebuilt { |
| 43 | name: "bar", |
| 44 | prefer: false, |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 45 | srcs: ["prebuilt_file"], |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 46 | }`, |
| 47 | prebuilt: true, |
| 48 | }, |
| 49 | { |
| 50 | name: "no source prebuilt preferred", |
| 51 | modules: ` |
| 52 | prebuilt { |
| 53 | name: "bar", |
| 54 | prefer: true, |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 55 | srcs: ["prebuilt_file"], |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 56 | }`, |
| 57 | prebuilt: true, |
| 58 | }, |
| 59 | { |
| 60 | name: "prebuilt not preferred", |
| 61 | modules: ` |
| 62 | source { |
| 63 | name: "bar", |
| 64 | } |
| 65 | |
| 66 | prebuilt { |
| 67 | name: "bar", |
| 68 | prefer: false, |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 69 | srcs: ["prebuilt_file"], |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 70 | }`, |
| 71 | prebuilt: false, |
| 72 | }, |
| 73 | { |
| 74 | name: "prebuilt preferred", |
| 75 | modules: ` |
| 76 | source { |
| 77 | name: "bar", |
| 78 | } |
| 79 | |
| 80 | prebuilt { |
| 81 | name: "bar", |
| 82 | prefer: true, |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 83 | srcs: ["prebuilt_file"], |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 84 | }`, |
| 85 | prebuilt: true, |
| 86 | }, |
Colin Cross | 0d614dd | 2016-10-14 15:38:43 -0700 | [diff] [blame] | 87 | { |
| 88 | name: "prebuilt no file not preferred", |
| 89 | modules: ` |
| 90 | source { |
| 91 | name: "bar", |
| 92 | } |
| 93 | |
| 94 | prebuilt { |
| 95 | name: "bar", |
| 96 | prefer: false, |
| 97 | }`, |
| 98 | prebuilt: false, |
| 99 | }, |
| 100 | { |
| 101 | name: "prebuilt no file preferred", |
| 102 | modules: ` |
| 103 | source { |
| 104 | name: "bar", |
| 105 | } |
| 106 | |
| 107 | prebuilt { |
| 108 | name: "bar", |
| 109 | prefer: true, |
| 110 | }`, |
| 111 | prebuilt: false, |
| 112 | }, |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 113 | { |
| 114 | name: "prebuilt file from filegroup preferred", |
| 115 | modules: ` |
| 116 | filegroup { |
| 117 | name: "fg", |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 118 | srcs: ["prebuilt_file"], |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 119 | } |
| 120 | prebuilt { |
| 121 | name: "bar", |
| 122 | prefer: true, |
| 123 | srcs: [":fg"], |
| 124 | }`, |
| 125 | prebuilt: true, |
| 126 | }, |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | func TestPrebuilts(t *testing.T) { |
Colin Cross | 0d614dd | 2016-10-14 15:38:43 -0700 | [diff] [blame] | 130 | buildDir, err := ioutil.TempDir("", "soong_prebuilt_test") |
| 131 | if err != nil { |
| 132 | t.Fatal(err) |
| 133 | } |
| 134 | defer os.RemoveAll(buildDir) |
| 135 | |
Colin Cross | 6ccbc91 | 2017-10-10 23:07:38 -0700 | [diff] [blame] | 136 | config := TestConfig(buildDir, nil) |
Colin Cross | 0d614dd | 2016-10-14 15:38:43 -0700 | [diff] [blame] | 137 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 138 | for _, test := range prebuiltsTests { |
| 139 | t.Run(test.name, func(t *testing.T) { |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 140 | ctx := NewTestContext() |
Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 141 | ctx.PreArchMutators(RegisterPrebuiltsPreArchMutators) |
| 142 | ctx.PostDepsMutators(RegisterPrebuiltsPostDepsMutators) |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 143 | ctx.RegisterModuleType("filegroup", ModuleFactoryAdaptor(FileGroupFactory)) |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 144 | ctx.RegisterModuleType("prebuilt", ModuleFactoryAdaptor(newPrebuiltModule)) |
| 145 | ctx.RegisterModuleType("source", ModuleFactoryAdaptor(newSourceModule)) |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 146 | ctx.Register() |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 147 | ctx.MockFileSystem(map[string][]byte{ |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 148 | "prebuilt_file": nil, |
| 149 | "source_file": nil, |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 150 | "Blueprints": []byte(` |
| 151 | source { |
| 152 | name: "foo", |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 153 | deps: [":bar"], |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 154 | } |
| 155 | ` + test.modules), |
| 156 | }) |
| 157 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 158 | _, errs := ctx.ParseBlueprintsFiles("Blueprints") |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 159 | FailIfErrored(t, errs) |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 160 | _, errs = ctx.PrepareBuildActions(config) |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 161 | FailIfErrored(t, errs) |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 162 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 163 | foo := ctx.ModuleForTests("foo", "") |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 164 | |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 165 | var dependsOnSourceModule, dependsOnPrebuiltModule bool |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 166 | ctx.VisitDirectDeps(foo.Module(), func(m blueprint.Module) { |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 167 | if _, ok := m.(*sourceModule); ok { |
| 168 | dependsOnSourceModule = true |
| 169 | } |
| 170 | if p, ok := m.(*prebuiltModule); ok { |
| 171 | dependsOnPrebuiltModule = true |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 172 | if !p.Prebuilt().properties.UsePrebuilt { |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 173 | t.Errorf("dependency on prebuilt module not marked used") |
| 174 | } |
| 175 | } |
| 176 | }) |
| 177 | |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 178 | deps := foo.Module().(*sourceModule).deps |
| 179 | if deps == nil || len(deps) != 1 { |
| 180 | t.Errorf("deps does not have single path, but is %v", deps) |
| 181 | } |
| 182 | var usingSourceFile, usingPrebuiltFile bool |
| 183 | if deps[0].String() == "source_file" { |
| 184 | usingSourceFile = true |
| 185 | } |
| 186 | if deps[0].String() == "prebuilt_file" { |
| 187 | usingPrebuiltFile = true |
| 188 | } |
| 189 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 190 | if test.prebuilt { |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 191 | if !dependsOnPrebuiltModule { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 192 | t.Errorf("doesn't depend on prebuilt module") |
| 193 | } |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 194 | if !usingPrebuiltFile { |
| 195 | t.Errorf("doesn't use prebuilt_file") |
| 196 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 197 | |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 198 | if dependsOnSourceModule { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 199 | t.Errorf("depends on source module") |
| 200 | } |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 201 | if usingSourceFile { |
| 202 | t.Errorf("using source_file") |
| 203 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 204 | } else { |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 205 | if dependsOnPrebuiltModule { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 206 | t.Errorf("depends on prebuilt module") |
| 207 | } |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 208 | if usingPrebuiltFile { |
| 209 | t.Errorf("using prebuilt_file") |
| 210 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 211 | |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 212 | if !dependsOnSourceModule { |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 213 | t.Errorf("doesn't depend on source module") |
| 214 | } |
| 215 | if !usingSourceFile { |
| 216 | t.Errorf("doesn't use source_file") |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | }) |
| 220 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | type prebuiltModule struct { |
| 224 | ModuleBase |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 225 | prebuilt Prebuilt |
| 226 | properties struct { |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 227 | Srcs []string `android:"path"` |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 228 | } |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 229 | src Path |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 232 | func newPrebuiltModule() Module { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 233 | m := &prebuiltModule{} |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 234 | m.AddProperties(&m.properties) |
| 235 | InitPrebuiltModule(m, &m.properties.Srcs) |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 236 | InitAndroidModule(m) |
| 237 | return m |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | func (p *prebuiltModule) Name() string { |
| 241 | return p.prebuilt.Name(p.ModuleBase.Name()) |
| 242 | } |
| 243 | |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 244 | func (p *prebuiltModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
| 245 | if len(p.properties.Srcs) >= 1 { |
| 246 | p.src = p.prebuilt.SingleSourcePath(ctx) |
| 247 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | func (p *prebuiltModule) Prebuilt() *Prebuilt { |
| 251 | return &p.prebuilt |
| 252 | } |
| 253 | |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame^] | 254 | func (p *prebuiltModule) OutputFiles(tag string) (Paths, error) { |
| 255 | switch tag { |
| 256 | case "": |
| 257 | return Paths{p.src}, nil |
| 258 | default: |
| 259 | return nil, fmt.Errorf("unsupported module reference tag %q", tag) |
| 260 | } |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 261 | } |
| 262 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 263 | type sourceModule struct { |
| 264 | ModuleBase |
| 265 | properties struct { |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 266 | Deps []string `android:"path"` |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 267 | } |
| 268 | dependsOnSourceModule, dependsOnPrebuiltModule bool |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 269 | deps Paths |
| 270 | src Path |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 273 | func newSourceModule() Module { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 274 | m := &sourceModule{} |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 275 | m.AddProperties(&m.properties) |
| 276 | InitAndroidModule(m) |
| 277 | return m |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | func (s *sourceModule) DepsMutator(ctx BottomUpMutatorContext) { |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 281 | // s.properties.Deps are annotated with android:path, so they are |
| 282 | // automatically added to the dependency by pathDeps mutator |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | func (s *sourceModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 286 | s.deps = PathsForModuleSrc(ctx, s.properties.Deps) |
| 287 | s.src = PathForModuleSrc(ctx, "source_file") |
| 288 | } |
| 289 | |
| 290 | func (s *sourceModule) Srcs() Paths { |
| 291 | return Paths{s.src} |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 292 | } |