2016-10-07 07:12:58 +08:00
|
|
|
// Copyright 2016 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package android
|
|
|
|
|
|
|
|
import (
|
2019-05-30 05:40:35 +08:00
|
|
|
"fmt"
|
2016-10-07 07:12:58 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/blueprint"
|
|
|
|
)
|
|
|
|
|
|
|
|
var prebuiltsTests = []struct {
|
|
|
|
name string
|
|
|
|
modules string
|
|
|
|
prebuilt bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no prebuilt",
|
|
|
|
modules: `
|
|
|
|
source {
|
|
|
|
name: "bar",
|
|
|
|
}`,
|
|
|
|
prebuilt: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no source prebuilt not preferred",
|
|
|
|
modules: `
|
|
|
|
prebuilt {
|
|
|
|
name: "bar",
|
|
|
|
prefer: false,
|
2019-04-19 16:23:04 +08:00
|
|
|
srcs: ["prebuilt_file"],
|
2016-10-07 07:12:58 +08:00
|
|
|
}`,
|
|
|
|
prebuilt: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no source prebuilt preferred",
|
|
|
|
modules: `
|
|
|
|
prebuilt {
|
|
|
|
name: "bar",
|
|
|
|
prefer: true,
|
2019-04-19 16:23:04 +08:00
|
|
|
srcs: ["prebuilt_file"],
|
2016-10-07 07:12:58 +08:00
|
|
|
}`,
|
|
|
|
prebuilt: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prebuilt not preferred",
|
|
|
|
modules: `
|
|
|
|
source {
|
|
|
|
name: "bar",
|
|
|
|
}
|
|
|
|
|
|
|
|
prebuilt {
|
|
|
|
name: "bar",
|
|
|
|
prefer: false,
|
2019-04-19 16:23:04 +08:00
|
|
|
srcs: ["prebuilt_file"],
|
2016-10-07 07:12:58 +08:00
|
|
|
}`,
|
|
|
|
prebuilt: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prebuilt preferred",
|
|
|
|
modules: `
|
|
|
|
source {
|
|
|
|
name: "bar",
|
|
|
|
}
|
|
|
|
|
|
|
|
prebuilt {
|
|
|
|
name: "bar",
|
|
|
|
prefer: true,
|
2019-04-19 16:23:04 +08:00
|
|
|
srcs: ["prebuilt_file"],
|
2016-10-07 07:12:58 +08:00
|
|
|
}`,
|
|
|
|
prebuilt: true,
|
|
|
|
},
|
2016-10-15 06:38:43 +08:00
|
|
|
{
|
|
|
|
name: "prebuilt no file not preferred",
|
|
|
|
modules: `
|
|
|
|
source {
|
|
|
|
name: "bar",
|
|
|
|
}
|
|
|
|
|
|
|
|
prebuilt {
|
|
|
|
name: "bar",
|
|
|
|
prefer: false,
|
|
|
|
}`,
|
|
|
|
prebuilt: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prebuilt no file preferred",
|
|
|
|
modules: `
|
|
|
|
source {
|
|
|
|
name: "bar",
|
|
|
|
}
|
|
|
|
|
|
|
|
prebuilt {
|
|
|
|
name: "bar",
|
|
|
|
prefer: true,
|
|
|
|
}`,
|
|
|
|
prebuilt: false,
|
|
|
|
},
|
2018-04-18 05:58:42 +08:00
|
|
|
{
|
|
|
|
name: "prebuilt file from filegroup preferred",
|
|
|
|
modules: `
|
|
|
|
filegroup {
|
|
|
|
name: "fg",
|
2019-04-19 16:23:04 +08:00
|
|
|
srcs: ["prebuilt_file"],
|
2018-04-18 05:58:42 +08:00
|
|
|
}
|
|
|
|
prebuilt {
|
|
|
|
name: "bar",
|
|
|
|
prefer: true,
|
|
|
|
srcs: [":fg"],
|
|
|
|
}`,
|
|
|
|
prebuilt: true,
|
|
|
|
},
|
2016-10-07 07:12:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrebuilts(t *testing.T) {
|
2017-10-11 14:07:38 +08:00
|
|
|
config := TestConfig(buildDir, nil)
|
2016-10-15 06:38:43 +08:00
|
|
|
|
2016-10-07 07:12:58 +08:00
|
|
|
for _, test := range prebuiltsTests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2017-07-14 05:43:27 +08:00
|
|
|
ctx := NewTestContext()
|
2017-07-28 06:41:32 +08:00
|
|
|
ctx.PreArchMutators(RegisterPrebuiltsPreArchMutators)
|
|
|
|
ctx.PostDepsMutators(RegisterPrebuiltsPostDepsMutators)
|
2018-04-18 05:58:42 +08:00
|
|
|
ctx.RegisterModuleType("filegroup", ModuleFactoryAdaptor(FileGroupFactory))
|
2017-06-24 06:06:31 +08:00
|
|
|
ctx.RegisterModuleType("prebuilt", ModuleFactoryAdaptor(newPrebuiltModule))
|
|
|
|
ctx.RegisterModuleType("source", ModuleFactoryAdaptor(newSourceModule))
|
2017-07-14 05:43:27 +08:00
|
|
|
ctx.Register()
|
2016-10-07 07:12:58 +08:00
|
|
|
ctx.MockFileSystem(map[string][]byte{
|
2019-04-19 16:23:04 +08:00
|
|
|
"prebuilt_file": nil,
|
|
|
|
"source_file": nil,
|
2016-10-07 07:12:58 +08:00
|
|
|
"Blueprints": []byte(`
|
|
|
|
source {
|
|
|
|
name: "foo",
|
2019-04-19 16:23:04 +08:00
|
|
|
deps: [":bar"],
|
2016-10-07 07:12:58 +08:00
|
|
|
}
|
|
|
|
` + test.modules),
|
|
|
|
})
|
|
|
|
|
|
|
|
_, errs := ctx.ParseBlueprintsFiles("Blueprints")
|
2018-03-12 16:29:17 +08:00
|
|
|
FailIfErrored(t, errs)
|
2016-10-07 07:12:58 +08:00
|
|
|
_, errs = ctx.PrepareBuildActions(config)
|
2018-03-12 16:29:17 +08:00
|
|
|
FailIfErrored(t, errs)
|
2016-10-07 07:12:58 +08:00
|
|
|
|
2017-07-14 05:43:27 +08:00
|
|
|
foo := ctx.ModuleForTests("foo", "")
|
2016-10-07 07:12:58 +08:00
|
|
|
|
2017-03-18 04:14:32 +08:00
|
|
|
var dependsOnSourceModule, dependsOnPrebuiltModule bool
|
2017-07-14 05:43:27 +08:00
|
|
|
ctx.VisitDirectDeps(foo.Module(), func(m blueprint.Module) {
|
2017-03-18 04:14:32 +08:00
|
|
|
if _, ok := m.(*sourceModule); ok {
|
|
|
|
dependsOnSourceModule = true
|
|
|
|
}
|
|
|
|
if p, ok := m.(*prebuiltModule); ok {
|
|
|
|
dependsOnPrebuiltModule = true
|
2017-08-03 02:05:49 +08:00
|
|
|
if !p.Prebuilt().properties.UsePrebuilt {
|
2017-03-18 04:14:32 +08:00
|
|
|
t.Errorf("dependency on prebuilt module not marked used")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-04-19 16:23:04 +08:00
|
|
|
deps := foo.Module().(*sourceModule).deps
|
|
|
|
if deps == nil || len(deps) != 1 {
|
|
|
|
t.Errorf("deps does not have single path, but is %v", deps)
|
|
|
|
}
|
|
|
|
var usingSourceFile, usingPrebuiltFile bool
|
|
|
|
if deps[0].String() == "source_file" {
|
|
|
|
usingSourceFile = true
|
|
|
|
}
|
|
|
|
if deps[0].String() == "prebuilt_file" {
|
|
|
|
usingPrebuiltFile = true
|
|
|
|
}
|
|
|
|
|
2016-10-07 07:12:58 +08:00
|
|
|
if test.prebuilt {
|
2017-03-18 04:14:32 +08:00
|
|
|
if !dependsOnPrebuiltModule {
|
2016-10-07 07:12:58 +08:00
|
|
|
t.Errorf("doesn't depend on prebuilt module")
|
|
|
|
}
|
2019-04-19 16:23:04 +08:00
|
|
|
if !usingPrebuiltFile {
|
|
|
|
t.Errorf("doesn't use prebuilt_file")
|
|
|
|
}
|
2016-10-07 07:12:58 +08:00
|
|
|
|
2017-03-18 04:14:32 +08:00
|
|
|
if dependsOnSourceModule {
|
2016-10-07 07:12:58 +08:00
|
|
|
t.Errorf("depends on source module")
|
|
|
|
}
|
2019-04-19 16:23:04 +08:00
|
|
|
if usingSourceFile {
|
|
|
|
t.Errorf("using source_file")
|
|
|
|
}
|
2016-10-07 07:12:58 +08:00
|
|
|
} else {
|
2017-03-18 04:14:32 +08:00
|
|
|
if dependsOnPrebuiltModule {
|
2016-10-07 07:12:58 +08:00
|
|
|
t.Errorf("depends on prebuilt module")
|
|
|
|
}
|
2019-04-19 16:23:04 +08:00
|
|
|
if usingPrebuiltFile {
|
|
|
|
t.Errorf("using prebuilt_file")
|
|
|
|
}
|
2016-10-07 07:12:58 +08:00
|
|
|
|
2017-03-18 04:14:32 +08:00
|
|
|
if !dependsOnSourceModule {
|
2019-04-19 16:23:04 +08:00
|
|
|
t.Errorf("doesn't depend on source module")
|
|
|
|
}
|
|
|
|
if !usingSourceFile {
|
|
|
|
t.Errorf("doesn't use source_file")
|
2016-10-07 07:12:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type prebuiltModule struct {
|
|
|
|
ModuleBase
|
2017-08-03 02:05:49 +08:00
|
|
|
prebuilt Prebuilt
|
|
|
|
properties struct {
|
2019-03-05 14:35:41 +08:00
|
|
|
Srcs []string `android:"path"`
|
2017-08-03 02:05:49 +08:00
|
|
|
}
|
2019-04-19 16:23:04 +08:00
|
|
|
src Path
|
2016-10-07 07:12:58 +08:00
|
|
|
}
|
|
|
|
|
2017-06-24 06:06:31 +08:00
|
|
|
func newPrebuiltModule() Module {
|
2016-10-07 07:12:58 +08:00
|
|
|
m := &prebuiltModule{}
|
2017-08-03 02:05:49 +08:00
|
|
|
m.AddProperties(&m.properties)
|
|
|
|
InitPrebuiltModule(m, &m.properties.Srcs)
|
2017-06-24 06:06:31 +08:00
|
|
|
InitAndroidModule(m)
|
|
|
|
return m
|
2016-10-07 07:12:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *prebuiltModule) Name() string {
|
|
|
|
return p.prebuilt.Name(p.ModuleBase.Name())
|
|
|
|
}
|
|
|
|
|
2019-04-19 16:23:04 +08:00
|
|
|
func (p *prebuiltModule) GenerateAndroidBuildActions(ctx ModuleContext) {
|
|
|
|
if len(p.properties.Srcs) >= 1 {
|
|
|
|
p.src = p.prebuilt.SingleSourcePath(ctx)
|
|
|
|
}
|
2016-10-07 07:12:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *prebuiltModule) Prebuilt() *Prebuilt {
|
|
|
|
return &p.prebuilt
|
|
|
|
}
|
|
|
|
|
2019-05-30 05:40:35 +08:00
|
|
|
func (p *prebuiltModule) OutputFiles(tag string) (Paths, error) {
|
|
|
|
switch tag {
|
|
|
|
case "":
|
|
|
|
return Paths{p.src}, nil
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported module reference tag %q", tag)
|
|
|
|
}
|
2019-04-19 16:23:04 +08:00
|
|
|
}
|
|
|
|
|
2016-10-07 07:12:58 +08:00
|
|
|
type sourceModule struct {
|
|
|
|
ModuleBase
|
|
|
|
properties struct {
|
2019-04-19 16:23:04 +08:00
|
|
|
Deps []string `android:"path"`
|
2016-10-07 07:12:58 +08:00
|
|
|
}
|
|
|
|
dependsOnSourceModule, dependsOnPrebuiltModule bool
|
2019-04-19 16:23:04 +08:00
|
|
|
deps Paths
|
|
|
|
src Path
|
2016-10-07 07:12:58 +08:00
|
|
|
}
|
|
|
|
|
2017-06-24 06:06:31 +08:00
|
|
|
func newSourceModule() Module {
|
2016-10-07 07:12:58 +08:00
|
|
|
m := &sourceModule{}
|
2017-06-24 06:06:31 +08:00
|
|
|
m.AddProperties(&m.properties)
|
|
|
|
InitAndroidModule(m)
|
|
|
|
return m
|
2016-10-07 07:12:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sourceModule) DepsMutator(ctx BottomUpMutatorContext) {
|
2019-04-19 16:23:04 +08:00
|
|
|
// s.properties.Deps are annotated with android:path, so they are
|
|
|
|
// automatically added to the dependency by pathDeps mutator
|
2016-10-07 07:12:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sourceModule) GenerateAndroidBuildActions(ctx ModuleContext) {
|
2019-04-19 16:23:04 +08:00
|
|
|
s.deps = PathsForModuleSrc(ctx, s.properties.Deps)
|
|
|
|
s.src = PathForModuleSrc(ctx, "source_file")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sourceModule) Srcs() Paths {
|
|
|
|
return Paths{s.src}
|
2016-10-07 07:12:58 +08:00
|
|
|
}
|