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 | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 19 | "testing" |
| 20 | |
| 21 | "github.com/google/blueprint" |
| 22 | ) |
| 23 | |
| 24 | var prebuiltsTests = []struct { |
| 25 | name string |
| 26 | modules string |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 27 | prebuilt []OsClass |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 28 | }{ |
| 29 | { |
| 30 | name: "no prebuilt", |
| 31 | modules: ` |
| 32 | source { |
| 33 | name: "bar", |
| 34 | }`, |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 35 | prebuilt: nil, |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 36 | }, |
| 37 | { |
| 38 | name: "no source prebuilt not preferred", |
| 39 | modules: ` |
| 40 | prebuilt { |
| 41 | name: "bar", |
| 42 | prefer: false, |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 43 | srcs: ["prebuilt_file"], |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 44 | }`, |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 45 | prebuilt: []OsClass{Device, Host}, |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 46 | }, |
| 47 | { |
| 48 | name: "no source prebuilt preferred", |
| 49 | modules: ` |
| 50 | prebuilt { |
| 51 | name: "bar", |
| 52 | prefer: true, |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 53 | srcs: ["prebuilt_file"], |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 54 | }`, |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 55 | prebuilt: []OsClass{Device, Host}, |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 56 | }, |
| 57 | { |
| 58 | name: "prebuilt not preferred", |
| 59 | modules: ` |
| 60 | source { |
| 61 | name: "bar", |
| 62 | } |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 63 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 64 | prebuilt { |
| 65 | name: "bar", |
| 66 | prefer: false, |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 67 | srcs: ["prebuilt_file"], |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 68 | }`, |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 69 | prebuilt: nil, |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 70 | }, |
| 71 | { |
| 72 | name: "prebuilt preferred", |
| 73 | modules: ` |
| 74 | source { |
| 75 | name: "bar", |
| 76 | } |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 77 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 78 | prebuilt { |
| 79 | name: "bar", |
| 80 | prefer: true, |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 81 | srcs: ["prebuilt_file"], |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 82 | }`, |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 83 | prebuilt: []OsClass{Device, Host}, |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 84 | }, |
Colin Cross | 0d614dd | 2016-10-14 15:38:43 -0700 | [diff] [blame] | 85 | { |
| 86 | name: "prebuilt no file not preferred", |
| 87 | modules: ` |
| 88 | source { |
| 89 | name: "bar", |
| 90 | } |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 91 | |
Colin Cross | 0d614dd | 2016-10-14 15:38:43 -0700 | [diff] [blame] | 92 | prebuilt { |
| 93 | name: "bar", |
| 94 | prefer: false, |
| 95 | }`, |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 96 | prebuilt: nil, |
Colin Cross | 0d614dd | 2016-10-14 15:38:43 -0700 | [diff] [blame] | 97 | }, |
| 98 | { |
| 99 | name: "prebuilt no file preferred", |
| 100 | modules: ` |
| 101 | source { |
| 102 | name: "bar", |
| 103 | } |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 104 | |
Colin Cross | 0d614dd | 2016-10-14 15:38:43 -0700 | [diff] [blame] | 105 | prebuilt { |
| 106 | name: "bar", |
| 107 | prefer: true, |
| 108 | }`, |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 109 | prebuilt: nil, |
Colin Cross | 0d614dd | 2016-10-14 15:38:43 -0700 | [diff] [blame] | 110 | }, |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 111 | { |
| 112 | name: "prebuilt file from filegroup preferred", |
| 113 | modules: ` |
| 114 | filegroup { |
| 115 | name: "fg", |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 116 | srcs: ["prebuilt_file"], |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 117 | } |
| 118 | prebuilt { |
| 119 | name: "bar", |
| 120 | prefer: true, |
| 121 | srcs: [":fg"], |
| 122 | }`, |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 123 | 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 Duffin | 69304cf | 2020-05-29 08:01:00 +0100 | [diff] [blame] | 151 | host: { |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 152 | srcs: ["prebuilt_file"], |
| 153 | }, |
| 154 | }, |
| 155 | }`, |
| 156 | prebuilt: []OsClass{Host}, |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 157 | }, |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | func TestPrebuilts(t *testing.T) { |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 161 | fs := map[string][]byte{ |
| 162 | "prebuilt_file": nil, |
| 163 | "source_file": nil, |
| 164 | } |
Colin Cross | 0d614dd | 2016-10-14 15:38:43 -0700 | [diff] [blame] | 165 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 166 | for _, test := range prebuiltsTests { |
| 167 | t.Run(test.name, func(t *testing.T) { |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 168 | bp := ` |
| 169 | source { |
| 170 | name: "foo", |
| 171 | deps: [":bar"], |
| 172 | } |
| 173 | ` + test.modules |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 174 | config := TestArchConfig(buildDir, nil, bp, fs) |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 175 | |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 176 | ctx := NewTestArchContext() |
Paul Duffin | 78ac5b9 | 2020-01-14 12:42:08 +0000 | [diff] [blame] | 177 | registerTestPrebuiltBuildComponents(ctx) |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 178 | ctx.RegisterModuleType("filegroup", FileGroupFactory) |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 179 | ctx.Register(config) |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 180 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 181 | _, errs := ctx.ParseBlueprintsFiles("Android.bp") |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 182 | FailIfErrored(t, errs) |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 183 | _, errs = ctx.PrepareBuildActions(config) |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 184 | FailIfErrored(t, errs) |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 185 | |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 186 | 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 Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 201 | |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 202 | 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 Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 205 | } |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 206 | 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 Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 213 | |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 214 | prebuilt := false |
| 215 | for _, os := range test.prebuilt { |
| 216 | if os == foo.Module().Target().Os.Class { |
| 217 | prebuilt = true |
| 218 | } |
| 219 | } |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 220 | |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 221 | 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 Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 228 | |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 229 | 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 Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 242 | |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 243 | 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 Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 251 | } |
| 252 | }) |
| 253 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Paul Duffin | 78ac5b9 | 2020-01-14 12:42:08 +0000 | [diff] [blame] | 256 | func registerTestPrebuiltBuildComponents(ctx RegistrationContext) { |
| 257 | ctx.RegisterModuleType("prebuilt", newPrebuiltModule) |
| 258 | ctx.RegisterModuleType("source", newSourceModule) |
| 259 | |
| 260 | RegisterPrebuiltMutators(ctx) |
| 261 | } |
| 262 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 263 | type prebuiltModule struct { |
| 264 | ModuleBase |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 265 | prebuilt Prebuilt |
| 266 | properties struct { |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 267 | Srcs []string `android:"path,arch_variant"` |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 268 | } |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 269 | src Path |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 272 | func newPrebuiltModule() Module { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 273 | m := &prebuiltModule{} |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 274 | m.AddProperties(&m.properties) |
| 275 | InitPrebuiltModule(m, &m.properties.Srcs) |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 276 | InitAndroidArchModule(m, HostAndDeviceDefault, MultilibCommon) |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 277 | return m |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | func (p *prebuiltModule) Name() string { |
| 281 | return p.prebuilt.Name(p.ModuleBase.Name()) |
| 282 | } |
| 283 | |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 284 | func (p *prebuiltModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
| 285 | if len(p.properties.Srcs) >= 1 { |
| 286 | p.src = p.prebuilt.SingleSourcePath(ctx) |
| 287 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | func (p *prebuiltModule) Prebuilt() *Prebuilt { |
| 291 | return &p.prebuilt |
| 292 | } |
| 293 | |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 294 | func (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 Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 301 | } |
| 302 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 303 | type sourceModule struct { |
| 304 | ModuleBase |
| 305 | properties struct { |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 306 | Deps []string `android:"path,arch_variant"` |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 307 | } |
| 308 | dependsOnSourceModule, dependsOnPrebuiltModule bool |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 309 | deps Paths |
| 310 | src Path |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 313 | func newSourceModule() Module { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 314 | m := &sourceModule{} |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 315 | m.AddProperties(&m.properties) |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 316 | InitAndroidArchModule(m, HostAndDeviceDefault, MultilibCommon) |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 317 | return m |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | func (s *sourceModule) DepsMutator(ctx BottomUpMutatorContext) { |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 321 | // s.properties.Deps are annotated with android:path, so they are |
| 322 | // automatically added to the dependency by pathDeps mutator |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | func (s *sourceModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 326 | s.deps = PathsForModuleSrc(ctx, s.properties.Deps) |
| 327 | s.src = PathForModuleSrc(ctx, "source_file") |
| 328 | } |
| 329 | |
| 330 | func (s *sourceModule) Srcs() Paths { |
| 331 | return Paths{s.src} |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 332 | } |