1// Copyright 2020 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 (
18	"testing"
19
20	"github.com/google/blueprint"
21)
22
23type testInstallDependencyTagModule struct {
24	ModuleBase
25	Properties struct {
26		Install_deps []string
27		Deps         []string
28	}
29}
30
31func (t *testInstallDependencyTagModule) GenerateAndroidBuildActions(ctx ModuleContext) {
32	outputFile := PathForModuleOut(ctx, "out")
33	ctx.Build(pctx, BuildParams{
34		Rule:   Touch,
35		Output: outputFile,
36	})
37	ctx.InstallFile(PathForModuleInstall(ctx), ctx.ModuleName(), outputFile)
38}
39
40var testInstallDependencyTagAlwaysDepTag = struct {
41	blueprint.DependencyTag
42	InstallAlwaysNeededDependencyTag
43}{}
44
45var testInstallDependencyTagNeverDepTag = struct {
46	blueprint.DependencyTag
47}{}
48
49func (t *testInstallDependencyTagModule) DepsMutator(ctx BottomUpMutatorContext) {
50	ctx.AddVariationDependencies(nil, testInstallDependencyTagAlwaysDepTag, t.Properties.Install_deps...)
51	ctx.AddVariationDependencies(nil, testInstallDependencyTagNeverDepTag, t.Properties.Deps...)
52}
53
54func testInstallDependencyTagModuleFactory() Module {
55	module := &testInstallDependencyTagModule{}
56	InitAndroidArchModule(module, HostAndDeviceDefault, MultilibCommon)
57	module.AddProperties(&module.Properties)
58	return module
59}
60
61func TestInstallDependencyTag(t *testing.T) {
62	bp := `
63		test_module {
64			name: "foo",
65			deps: ["dep"],
66			install_deps: ["install_dep"],
67		}
68
69		test_module {
70			name: "install_dep",
71			install_deps: ["transitive"],
72		}
73
74		test_module {
75			name: "transitive",
76		}
77
78		test_module {
79			name: "dep",
80		}
81	`
82
83	result := GroupFixturePreparers(
84		PrepareForTestWithArchMutator,
85		FixtureWithRootAndroidBp(bp),
86		FixtureRegisterWithContext(func(ctx RegistrationContext) {
87			ctx.RegisterModuleType("test_module", testInstallDependencyTagModuleFactory)
88		}),
89	).RunTest(t)
90
91	config := result.Config
92
93	hostFoo := result.ModuleForTests("foo", config.BuildOSCommonTarget.String()).Description("install")
94	hostInstallDep := result.ModuleForTests("install_dep", config.BuildOSCommonTarget.String()).Description("install")
95	hostTransitive := result.ModuleForTests("transitive", config.BuildOSCommonTarget.String()).Description("install")
96	hostDep := result.ModuleForTests("dep", config.BuildOSCommonTarget.String()).Description("install")
97
98	if g, w := hostFoo.Implicits.Strings(), hostInstallDep.Output.String(); !InList(w, g) {
99		t.Errorf("expected host dependency %q, got %q", w, g)
100	}
101
102	if g, w := hostFoo.Implicits.Strings(), hostTransitive.Output.String(); !InList(w, g) {
103		t.Errorf("expected host dependency %q, got %q", w, g)
104	}
105
106	if g, w := hostInstallDep.Implicits.Strings(), hostTransitive.Output.String(); !InList(w, g) {
107		t.Errorf("expected host dependency %q, got %q", w, g)
108	}
109
110	if g, w := hostFoo.Implicits.Strings(), hostDep.Output.String(); InList(w, g) {
111		t.Errorf("expected no host dependency %q, got %q", w, g)
112	}
113
114	deviceFoo := result.ModuleForTests("foo", "android_common").Description("install")
115	deviceInstallDep := result.ModuleForTests("install_dep", "android_common").Description("install")
116	deviceTransitive := result.ModuleForTests("transitive", "android_common").Description("install")
117	deviceDep := result.ModuleForTests("dep", "android_common").Description("install")
118
119	if g, w := deviceFoo.OrderOnly.Strings(), deviceInstallDep.Output.String(); !InList(w, g) {
120		t.Errorf("expected device dependency %q, got %q", w, g)
121	}
122
123	if g, w := deviceFoo.OrderOnly.Strings(), deviceTransitive.Output.String(); !InList(w, g) {
124		t.Errorf("expected device dependency %q, got %q", w, g)
125	}
126
127	if g, w := deviceInstallDep.OrderOnly.Strings(), deviceTransitive.Output.String(); !InList(w, g) {
128		t.Errorf("expected device dependency %q, got %q", w, g)
129	}
130
131	if g, w := deviceFoo.OrderOnly.Strings(), deviceDep.Output.String(); InList(w, g) {
132		t.Errorf("expected no device dependency %q, got %q", w, g)
133	}
134}
135