blob: 7b3acb50937dbf8603838f05412d7888079661c5 [file] [log] [blame]
Artur Satayev849f8442020-04-28 14:57:42 +01001/*
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 (
Spandan Dasa747d2e2024-03-11 21:37:25 +000020 "encoding/json"
21
Artur Satayeva4405fa2020-05-14 15:15:01 +010022 "github.com/google/blueprint"
Artur Satayev9af168d2020-06-24 10:17:19 +000023
24 "android/soong/android"
Artur Satayev849f8442020-04-28 14:57:42 +010025)
26
27func init() {
Jooyung Han4495f842023-04-25 16:39:59 +090028 registerApexDepsInfoComponents(android.InitRegistrationContext)
29}
30
31func registerApexDepsInfoComponents(ctx android.RegistrationContext) {
LaMont Jones0c10e4d2023-05-16 00:58:37 +000032 ctx.RegisterParallelSingletonType("apex_depsinfo_singleton", apexDepsInfoSingletonFactory)
Artur Satayev849f8442020-04-28 14:57:42 +010033}
34
35type apexDepsInfoSingleton struct {
Artur Satayev019560f2020-07-02 11:51:47 +000036 allowedApexDepsInfoCheckResult android.OutputPath
Artur Satayev849f8442020-04-28 14:57:42 +010037}
38
39func apexDepsInfoSingletonFactory() android.Singleton {
40 return &apexDepsInfoSingleton{}
41}
42
Artur Satayev019560f2020-07-02 11:51:47 +000043var (
44 // Generate new apex allowed_deps.txt by merging all internal dependencies.
45 generateApexDepsInfoFilesRule = pctx.AndroidStaticRule("generateApexDepsInfoFilesRule", blueprint.RuleParams{
46 Command: "cat $out.rsp | xargs cat" +
47 // Only track non-external dependencies, i.e. those that end up in the binary
48 " | grep -v '(external)'" +
Alan Viverette1e352322024-06-17 14:21:49 -040049 // Allowlist androidx deps
50 " | grep -v '^androidx\\.'" +
51 " | grep -v '^prebuilt_androidx\\.'" +
Artur Satayev019560f2020-07-02 11:51:47 +000052 // Ignore comments in any of the files
53 " | grep -v '^#'" +
54 " | sort -u -f >$out",
Artur Satayev849f8442020-04-28 14:57:42 +010055 Rspfile: "$out.rsp",
56 RspfileContent: "$in",
Artur Satayev019560f2020-07-02 11:51:47 +000057 })
58
59 // Diff two given lists while ignoring comments in the allowed deps file.
60 diffAllowedApexDepsInfoRule = pctx.AndroidStaticRule("diffAllowedApexDepsInfoRule", blueprint.RuleParams{
Chan Wang82563602025-01-14 15:53:05 +000061 Description: "Diff ${allowed_deps} and ${new_allowed_deps}",
Artur Satayev019560f2020-07-02 11:51:47 +000062 Command: `
Chan Wang82563602025-01-14 15:53:05 +000063 if grep -v '^#' ${allowed_deps} | diff -B - ${new_allowed_deps}; then
Artur Satayev019560f2020-07-02 11:51:47 +000064 touch ${out};
65 else
Colin Cross7cd1d422024-11-13 13:05:49 -080066 echo;
67 echo "******************************";
Pedro Loureiro87c88172023-02-07 17:08:35 +000068 echo "ERROR: go/apex-allowed-deps-error contains more information";
Artur Satayev019560f2020-07-02 11:51:47 +000069 echo "******************************";
70 echo "Detected changes to allowed dependencies in updatable modules.";
Artur Satayev8dfcbe42021-03-11 16:45:04 +000071 echo "To fix and update packages/modules/common/build/allowed_deps.txt, please run:";
Colin Cross7cd1d422024-11-13 13:05:49 -080072 echo "$$ (croot && packages/modules/common/build/update-apex-allowed-deps.sh)";
73 echo;
Mathew Inwood768a0112021-08-02 16:43:05 +010074 echo "When submitting the generated CL, you must include the following information";
75 echo "in the commit message if you are adding a new dependency:";
Pedro Loureiro87c88172023-02-07 17:08:35 +000076 echo "Apex-Size-Increase: Expected binary size increase for affected APEXes (or the size of the .jar / .so file of the new library)";
77 echo "Previous-Platform-Support: Are the maintainers of the new dependency committed to supporting previous platform releases?";
78 echo "Aosp-First: Is the new dependency being developed AOSP-first or internal?";
79 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?";
Mathew Inwood768a0112021-08-02 16:43:05 +010080 echo "You do not need OWNERS approval to submit the change, but mainline-modularization@";
81 echo "will periodically review additions and may require changes.";
Colin Cross7cd1d422024-11-13 13:05:49 -080082 echo "******************************";
83 echo;
Artur Satayev019560f2020-07-02 11:51:47 +000084 exit 1;
85 fi;
86 `,
Chan Wang82563602025-01-14 15:53:05 +000087 }, "allowed_deps", "new_allowed_deps")
Artur Satayev849f8442020-04-28 14:57:42 +010088)
89
90func (s *apexDepsInfoSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Artur Satayev9af168d2020-06-24 10:17:19 +000091 updatableFlatLists := android.Paths{}
Artur Satayev849f8442020-04-28 14:57:42 +010092 ctx.VisitAllModules(func(module android.Module) {
93 if binaryInfo, ok := module.(android.ApexBundleDepsInfoIntf); ok {
Yu Liu663e4502024-08-12 18:23:59 +000094 apexInfo, _ := android.OtherModuleProvider(ctx, module, android.ApexInfoProvider)
Colin Cross56a83212020-09-15 18:30:11 -070095 if path := binaryInfo.FlatListPath(); path != nil {
96 if binaryInfo.Updatable() || apexInfo.Updatable {
Pechetty Sravani (xWF)0d02c062025-01-13 00:39:47 -080097 updatableFlatLists = append(updatableFlatLists, path)
Colin Cross56a83212020-09-15 18:30:11 -070098 }
Artur Satayev849f8442020-04-28 14:57:42 +010099 }
100 }
101 })
Chan Wang82563602025-01-14 15:53:05 +0000102
103 allowedDepsSource := android.ExistentPathForSource(ctx, "packages/modules/common/build/allowed_deps.txt")
Artur Satayev019560f2020-07-02 11:51:47 +0000104 newAllowedDeps := android.PathForOutput(ctx, "apex", "depsinfo", "new-allowed-deps.txt")
Artur Satayev019560f2020-07-02 11:51:47 +0000105 s.allowedApexDepsInfoCheckResult = android.PathForOutput(ctx, newAllowedDeps.Rel()+".check")
Chan Wang82563602025-01-14 15:53:05 +0000106
107 if !allowedDepsSource.Valid() {
Artur Satayevb77b0c52021-03-18 11:04:09 +0000108 // Unbundled projects may not have packages/modules/common/ checked out; ignore those.
109 ctx.Build(pctx, android.BuildParams{
110 Rule: android.Touch,
111 Output: s.allowedApexDepsInfoCheckResult,
112 })
113 } else {
Chan Wang82563602025-01-14 15:53:05 +0000114 allowedDeps := allowedDepsSource.Path()
115
Artur Satayevb77b0c52021-03-18 11:04:09 +0000116 ctx.Build(pctx, android.BuildParams{
117 Rule: generateApexDepsInfoFilesRule,
Chan Wang82563602025-01-14 15:53:05 +0000118 Inputs: append(updatableFlatLists, allowedDeps),
Artur Satayevb77b0c52021-03-18 11:04:09 +0000119 Output: newAllowedDeps,
120 })
Chan Wang82563602025-01-14 15:53:05 +0000121
Artur Satayevb77b0c52021-03-18 11:04:09 +0000122 ctx.Build(pctx, android.BuildParams{
123 Rule: diffAllowedApexDepsInfoRule,
124 Input: newAllowedDeps,
125 Output: s.allowedApexDepsInfoCheckResult,
126 Args: map[string]string{
Chan Wang82563602025-01-14 15:53:05 +0000127 "allowed_deps": allowedDeps.String(),
128 "new_allowed_deps": newAllowedDeps.String(),
Artur Satayevb77b0c52021-03-18 11:04:09 +0000129 },
130 })
131 }
Chan Wang82563602025-01-14 15:53:05 +0000132
Artur Satayev01aa11e2020-07-30 18:26:28 +0100133 ctx.Phony("apex-allowed-deps-check", s.allowedApexDepsInfoCheckResult)
Artur Satayev019560f2020-07-02 11:51:47 +0000134}
135
136func (s *apexDepsInfoSingleton) MakeVars(ctx android.MakeVarsContext) {
137 // Export check result to Make. The path is added to droidcore.
138 ctx.Strict("APEX_ALLOWED_DEPS_CHECK", s.allowedApexDepsInfoCheckResult.String())
Artur Satayev849f8442020-04-28 14:57:42 +0100139}
Spandan Dasa747d2e2024-03-11 21:37:25 +0000140
141func init() {
142 registerApexPrebuiltInfoComponents(android.InitRegistrationContext)
143}
144
145func registerApexPrebuiltInfoComponents(ctx android.RegistrationContext) {
146 ctx.RegisterParallelSingletonType("apex_prebuiltinfo_singleton", apexPrebuiltInfoFactory)
147}
148
149func apexPrebuiltInfoFactory() android.Singleton {
150 return &apexPrebuiltInfo{}
151}
152
153type apexPrebuiltInfo struct {
154 out android.WritablePath
155}
156
157func (a *apexPrebuiltInfo) GenerateBuildActions(ctx android.SingletonContext) {
Spandan Das3490dfd2024-03-11 21:37:25 +0000158 prebuiltInfos := []android.PrebuiltInfo{}
Spandan Dasa747d2e2024-03-11 21:37:25 +0000159
160 ctx.VisitAllModules(func(m android.Module) {
Yu Liu663e4502024-08-12 18:23:59 +0000161 prebuiltInfo, exists := android.OtherModuleProvider(ctx, m, android.PrebuiltInfoProvider)
Spandan Dasa747d2e2024-03-11 21:37:25 +0000162 // Use prebuiltInfoProvider to filter out non apex soong modules.
163 // Use HideFromMake to filter out the unselected variants of a specific apex.
164 if exists && !m.IsHideFromMake() {
165 prebuiltInfos = append(prebuiltInfos, prebuiltInfo)
166 }
167 })
168
169 j, err := json.Marshal(prebuiltInfos)
170 if err != nil {
171 ctx.Errorf("Could not convert prebuilt info of apexes to json due to error: %v", err)
172 }
173 a.out = android.PathForOutput(ctx, "prebuilt_info.json")
174 android.WriteFileRule(ctx, a.out, string(j))
175}
176
177func (a *apexPrebuiltInfo) MakeVars(ctx android.MakeVarsContext) {
178 ctx.DistForGoal("droidcore", a.out)
179}