• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package apex
18
19import (
20	"encoding/json"
21	"strings"
22
23	"github.com/google/blueprint"
24
25	"android/soong/android"
26)
27
28func init() {
29	registerApexDepsInfoComponents(android.InitRegistrationContext)
30}
31
32func registerApexDepsInfoComponents(ctx android.RegistrationContext) {
33	ctx.RegisterParallelSingletonType("apex_depsinfo_singleton", apexDepsInfoSingletonFactory)
34}
35
36type apexDepsInfoSingleton struct {
37	allowedApexDepsInfoCheckResult android.OutputPath
38}
39
40func apexDepsInfoSingletonFactory() android.Singleton {
41	return &apexDepsInfoSingleton{}
42}
43
44var (
45	// Generate new apex allowed_deps.txt by merging all internal dependencies.
46	generateApexDepsInfoFilesRule = pctx.AndroidStaticRule("generateApexDepsInfoFilesRule", blueprint.RuleParams{
47		Command: "cat $out.rsp | xargs cat" +
48			// Only track non-external dependencies, i.e. those that end up in the binary
49			" | grep -v '(external)'" +
50			// Allowlist androidx deps
51			" | grep -v '^androidx\\.'" +
52			" | grep -v '^prebuilt_androidx\\.'" +
53			// Ignore comments in any of the files
54			" | grep -v '^#'" +
55			" | sort -u -f >$out",
56		Rspfile:        "$out.rsp",
57		RspfileContent: "$in",
58	})
59
60	// Diff two given lists while ignoring comments in the allowed deps file.
61	diffAllowedApexDepsInfoRule = pctx.AndroidStaticRule("diffAllowedApexDepsInfoRule", blueprint.RuleParams{
62		Description: "Diff ${allowed_deps} and ${new_allowed_deps}",
63		Command: `
64			if grep -v '^#' ${allowed_deps} | diff -B - ${new_allowed_deps}; then
65			   touch ${out};
66			else
67				echo;
68				echo "******************************";
69				echo "ERROR: go/apex-allowed-deps-error contains more information";
70				echo "******************************";
71				echo "Detected changes to allowed dependencies in updatable modules.";
72				echo "To fix and update packages/modules/common/build/allowed_deps.txt, please run:";
73				echo "$$ (croot && packages/modules/common/build/update-apex-allowed-deps.sh)";
74				echo;
75				echo "When submitting the generated CL, you must include the following information";
76				echo "in the commit message if you are adding a new dependency:";
77				echo "Apex-Size-Increase: Expected binary size increase for affected APEXes (or the size of the .jar / .so file of the new library)";
78				echo "Previous-Platform-Support: Are the maintainers of the new dependency committed to supporting previous platform releases?";
79				echo "Aosp-First: Is the new dependency being developed AOSP-first or internal?";
80				echo "Test-Info: What’s the testing strategy for the new dependency? Does it have its own tests, and are you adding integration tests? How/when are the tests run?";
81				echo "You do not need OWNERS approval to submit the change, but mainline-modularization@";
82				echo "will periodically review additions and may require changes.";
83				echo "******************************";
84				echo;
85				exit 1;
86			fi;
87		`,
88	}, "allowed_deps", "new_allowed_deps")
89)
90
91func (s *apexDepsInfoSingleton) GenerateBuildActions(ctx android.SingletonContext) {
92	updatableFlatLists := android.Paths{}
93	ctx.VisitAllModuleProxies(func(module android.ModuleProxy) {
94		if binaryInfo, ok := android.OtherModuleProvider(ctx, module, android.ApexBundleDepsDataProvider); ok {
95			apexInfo, _ := android.OtherModuleProvider(ctx, module, android.ApexInfoProvider)
96			if path := binaryInfo.FlatListPath; path != nil {
97				if binaryInfo.Updatable || apexInfo.Updatable {
98					if strings.HasPrefix(module.String(), "com.android.") {
99						updatableFlatLists = append(updatableFlatLists, path)
100					}
101				}
102			}
103		}
104	})
105
106	allowedDepsSource := android.ExistentPathForSource(ctx, "packages/modules/common/build/allowed_deps.txt")
107	newAllowedDeps := android.PathForOutput(ctx, "apex", "depsinfo", "new-allowed-deps.txt")
108	s.allowedApexDepsInfoCheckResult = android.PathForOutput(ctx, newAllowedDeps.Rel()+".check")
109
110	if !allowedDepsSource.Valid() {
111		// Unbundled projects may not have packages/modules/common/ checked out; ignore those.
112		ctx.Build(pctx, android.BuildParams{
113			Rule:   android.Touch,
114			Output: s.allowedApexDepsInfoCheckResult,
115		})
116	} else {
117		allowedDeps := allowedDepsSource.Path()
118
119		ctx.Build(pctx, android.BuildParams{
120			Rule:   generateApexDepsInfoFilesRule,
121			Inputs: append(updatableFlatLists, allowedDeps),
122			Output: newAllowedDeps,
123		})
124
125		ctx.Build(pctx, android.BuildParams{
126			Rule:   diffAllowedApexDepsInfoRule,
127			Input:  newAllowedDeps,
128			Output: s.allowedApexDepsInfoCheckResult,
129			Args: map[string]string{
130				"allowed_deps":     allowedDeps.String(),
131				"new_allowed_deps": newAllowedDeps.String(),
132			},
133		})
134	}
135
136	ctx.Phony("apex-allowed-deps-check", s.allowedApexDepsInfoCheckResult)
137}
138
139func (s *apexDepsInfoSingleton) MakeVars(ctx android.MakeVarsContext) {
140	// Export check result to Make. The path is added to droidcore.
141	ctx.Strict("APEX_ALLOWED_DEPS_CHECK", s.allowedApexDepsInfoCheckResult.String())
142}
143
144func init() {
145	registerApexPrebuiltInfoComponents(android.InitRegistrationContext)
146}
147
148func registerApexPrebuiltInfoComponents(ctx android.RegistrationContext) {
149	ctx.RegisterParallelSingletonType("apex_prebuiltinfo_singleton", apexPrebuiltInfoFactory)
150}
151
152func apexPrebuiltInfoFactory() android.Singleton {
153	return &apexPrebuiltInfo{}
154}
155
156type apexPrebuiltInfo struct {
157	out android.WritablePath
158}
159
160func (a *apexPrebuiltInfo) GenerateBuildActions(ctx android.SingletonContext) {
161	prebuiltInfos := []android.PrebuiltInfo{}
162
163	ctx.VisitAllModuleProxies(func(m android.ModuleProxy) {
164		prebuiltInfo, exists := android.OtherModuleProvider(ctx, m, android.PrebuiltInfoProvider)
165		// Use prebuiltInfoProvider to filter out non apex soong modules.
166		// Use HideFromMake to filter out the unselected variants of a specific apex.
167		if exists && !android.OtherModulePointerProviderOrDefault(ctx, m, android.CommonModuleInfoProvider).HideFromMake {
168			prebuiltInfos = append(prebuiltInfos, prebuiltInfo)
169		}
170	})
171
172	j, err := json.Marshal(prebuiltInfos)
173	if err != nil {
174		ctx.Errorf("Could not convert prebuilt info of apexes to json due to error: %v", err)
175	}
176	a.out = android.PathForOutput(ctx, "prebuilt_info.json")
177	android.WriteFileRule(ctx, a.out, string(j))
178	ctx.DistForGoal("droidcore", a.out)
179}
180