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 | 0d614dd | 2016-10-14 15:38:43 -0700 | [diff] [blame] | 18 | "io/ioutil" |
| 19 | "os" |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 20 | "testing" |
| 21 | |
| 22 | "github.com/google/blueprint" |
| 23 | ) |
| 24 | |
| 25 | var 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 Cross | 0d614dd | 2016-10-14 15:38:43 -0700 | [diff] [blame] | 86 | { |
| 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 Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 112 | { |
| 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 Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | func TestPrebuilts(t *testing.T) { |
Colin Cross | 0d614dd | 2016-10-14 15:38:43 -0700 | [diff] [blame] | 128 | buildDir, err := ioutil.TempDir("", "soong_prebuilt_test") |
| 129 | if err != nil { |
| 130 | t.Fatal(err) |
| 131 | } |
| 132 | defer os.RemoveAll(buildDir) |
| 133 | |
Colin Cross | 6ccbc91 | 2017-10-10 23:07:38 -0700 | [diff] [blame] | 134 | config := TestConfig(buildDir, nil) |
Colin Cross | 0d614dd | 2016-10-14 15:38:43 -0700 | [diff] [blame] | 135 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 136 | for _, test := range prebuiltsTests { |
| 137 | t.Run(test.name, func(t *testing.T) { |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 138 | ctx := NewTestContext() |
Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 139 | ctx.PreArchMutators(RegisterPrebuiltsPreArchMutators) |
| 140 | ctx.PostDepsMutators(RegisterPrebuiltsPostDepsMutators) |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 141 | ctx.RegisterModuleType("filegroup", ModuleFactoryAdaptor(FileGroupFactory)) |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 142 | ctx.RegisterModuleType("prebuilt", ModuleFactoryAdaptor(newPrebuiltModule)) |
| 143 | ctx.RegisterModuleType("source", ModuleFactoryAdaptor(newSourceModule)) |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 144 | ctx.Register() |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 145 | ctx.MockFileSystem(map[string][]byte{ |
| 146 | "Blueprints": []byte(` |
| 147 | source { |
| 148 | name: "foo", |
| 149 | deps: ["bar"], |
| 150 | } |
| 151 | ` + test.modules), |
| 152 | }) |
| 153 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 154 | _, errs := ctx.ParseBlueprintsFiles("Blueprints") |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 155 | FailIfErrored(t, errs) |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 156 | _, errs = ctx.PrepareBuildActions(config) |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 157 | FailIfErrored(t, errs) |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 158 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 159 | foo := ctx.ModuleForTests("foo", "") |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 160 | |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 161 | var dependsOnSourceModule, dependsOnPrebuiltModule bool |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 162 | ctx.VisitDirectDeps(foo.Module(), func(m blueprint.Module) { |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 163 | if _, ok := m.(*sourceModule); ok { |
| 164 | dependsOnSourceModule = true |
| 165 | } |
| 166 | if p, ok := m.(*prebuiltModule); ok { |
| 167 | dependsOnPrebuiltModule = true |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 168 | if !p.Prebuilt().properties.UsePrebuilt { |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 169 | t.Errorf("dependency on prebuilt module not marked used") |
| 170 | } |
| 171 | } |
| 172 | }) |
| 173 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 174 | if test.prebuilt { |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 175 | if !dependsOnPrebuiltModule { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 176 | t.Errorf("doesn't depend on prebuilt module") |
| 177 | } |
| 178 | |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 179 | if dependsOnSourceModule { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 180 | t.Errorf("depends on source module") |
| 181 | } |
| 182 | } else { |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 183 | if dependsOnPrebuiltModule { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 184 | t.Errorf("depends on prebuilt module") |
| 185 | } |
| 186 | |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 187 | if !dependsOnSourceModule { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 188 | t.Errorf("doens't depend on source module") |
| 189 | } |
| 190 | } |
| 191 | }) |
| 192 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | type prebuiltModule struct { |
| 196 | ModuleBase |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 197 | prebuilt Prebuilt |
| 198 | properties struct { |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame^] | 199 | Srcs []string `android:"path"` |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 200 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 203 | func newPrebuiltModule() Module { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 204 | m := &prebuiltModule{} |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 205 | m.AddProperties(&m.properties) |
| 206 | InitPrebuiltModule(m, &m.properties.Srcs) |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 207 | InitAndroidModule(m) |
| 208 | return m |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | func (p *prebuiltModule) Name() string { |
| 212 | return p.prebuilt.Name(p.ModuleBase.Name()) |
| 213 | } |
| 214 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 215 | func (p *prebuiltModule) GenerateAndroidBuildActions(ModuleContext) { |
| 216 | } |
| 217 | |
| 218 | func (p *prebuiltModule) Prebuilt() *Prebuilt { |
| 219 | return &p.prebuilt |
| 220 | } |
| 221 | |
| 222 | type sourceModule struct { |
| 223 | ModuleBase |
| 224 | properties struct { |
| 225 | Deps []string |
| 226 | } |
| 227 | dependsOnSourceModule, dependsOnPrebuiltModule bool |
| 228 | } |
| 229 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 230 | func newSourceModule() Module { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 231 | m := &sourceModule{} |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 232 | m.AddProperties(&m.properties) |
| 233 | InitAndroidModule(m) |
| 234 | return m |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | func (s *sourceModule) DepsMutator(ctx BottomUpMutatorContext) { |
| 238 | for _, d := range s.properties.Deps { |
| 239 | ctx.AddDependency(ctx.Module(), nil, d) |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | func (s *sourceModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 244 | } |