xref: /aosp_15_r20/build/soong/apex/apex_singleton.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
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_list} and ${new_allowed_deps}",
63		Command: `
64			if grep -v -h '^#' ${allowed_deps_list} | sort -u -f| diff -B -u - ${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_list", "new_allowed_deps")
89)
90
91func (s *apexDepsInfoSingleton) GenerateBuildActions(ctx android.SingletonContext) {
92	allowedDepsSources := []android.OptionalPath{android.ExistentPathForSource(ctx, "packages/modules/common/build/allowed_deps.txt")}
93	extraAllowedDepsPath := ctx.Config().ExtraAllowedDepsTxt()
94	if extraAllowedDepsPath != "" {
95		allowedDepsSources = append(allowedDepsSources, android.ExistentPathForSource(ctx, extraAllowedDepsPath))
96	}
97	updatableFlatLists := android.Paths{}
98	ctx.VisitAllModules(func(module android.Module) {
99		if binaryInfo, ok := module.(android.ApexBundleDepsInfoIntf); ok {
100			apexInfo, _ := android.OtherModuleProvider(ctx, module, android.ApexInfoProvider)
101			if path := binaryInfo.FlatListPath(); path != nil {
102				if binaryInfo.Updatable() || apexInfo.Updatable {
103					updatableFlatLists = append(updatableFlatLists, path)
104				}
105			}
106		}
107	})
108	newAllowedDeps := android.PathForOutput(ctx, "apex", "depsinfo", "new-allowed-deps.txt")
109	s.allowedApexDepsInfoCheckResult = android.PathForOutput(ctx, newAllowedDeps.Rel()+".check")
110	hasOneValidDepsPath := false
111	for _, allowedDepsSource := range allowedDepsSources {
112		if allowedDepsSource.Valid() {
113			hasOneValidDepsPath = true
114			updatableFlatLists = append(updatableFlatLists, allowedDepsSource.Path())
115		}
116	}
117	allowedDepsStrList := make([]string, len(allowedDepsSources))
118	for _, value := range allowedDepsSources {
119		allowedDepsStrList = append(allowedDepsStrList, value.String())
120	}
121	allowedDepsListString := strings.Join(allowedDepsStrList, " ")
122	if !hasOneValidDepsPath {
123		// Unbundled projects may not have packages/modules/common/ checked out; ignore those.
124		ctx.Build(pctx, android.BuildParams{
125			Rule:   android.Touch,
126			Output: s.allowedApexDepsInfoCheckResult,
127		})
128	} else {
129		ctx.Build(pctx, android.BuildParams{
130			Rule:   generateApexDepsInfoFilesRule,
131			Inputs: updatableFlatLists,
132			Output: newAllowedDeps,
133		})
134		ctx.Build(pctx, android.BuildParams{
135			Rule:   diffAllowedApexDepsInfoRule,
136			Input:  newAllowedDeps,
137			Output: s.allowedApexDepsInfoCheckResult,
138			Args: map[string]string{
139				"allowed_deps_list": allowedDepsListString,
140				"new_allowed_deps":  newAllowedDeps.String(),
141			},
142		})
143	}
144	ctx.Phony("apex-allowed-deps-check", s.allowedApexDepsInfoCheckResult)
145}
146
147func (s *apexDepsInfoSingleton) MakeVars(ctx android.MakeVarsContext) {
148	// Export check result to Make. The path is added to droidcore.
149	ctx.Strict("APEX_ALLOWED_DEPS_CHECK", s.allowedApexDepsInfoCheckResult.String())
150}
151
152func init() {
153	registerApexPrebuiltInfoComponents(android.InitRegistrationContext)
154}
155
156func registerApexPrebuiltInfoComponents(ctx android.RegistrationContext) {
157	ctx.RegisterParallelSingletonType("apex_prebuiltinfo_singleton", apexPrebuiltInfoFactory)
158}
159
160func apexPrebuiltInfoFactory() android.Singleton {
161	return &apexPrebuiltInfo{}
162}
163
164type apexPrebuiltInfo struct {
165	out android.WritablePath
166}
167
168func (a *apexPrebuiltInfo) GenerateBuildActions(ctx android.SingletonContext) {
169	prebuiltInfos := []android.PrebuiltInfo{}
170
171	ctx.VisitAllModules(func(m android.Module) {
172		prebuiltInfo, exists := android.OtherModuleProvider(ctx, m, android.PrebuiltInfoProvider)
173		// Use prebuiltInfoProvider to filter out non apex soong modules.
174		// Use HideFromMake to filter out the unselected variants of a specific apex.
175		if exists && !m.IsHideFromMake() {
176			prebuiltInfos = append(prebuiltInfos, prebuiltInfo)
177		}
178	})
179
180	j, err := json.Marshal(prebuiltInfos)
181	if err != nil {
182		ctx.Errorf("Could not convert prebuilt info of apexes to json due to error: %v", err)
183	}
184	a.out = android.PathForOutput(ctx, "prebuilt_info.json")
185	android.WriteFileRule(ctx, a.out, string(j))
186}
187
188func (a *apexPrebuiltInfo) MakeVars(ctx android.MakeVarsContext) {
189	ctx.DistForGoal("droidcore", a.out)
190}
191