blob: 0ed94afdb3ca047d635110806dc4db12078796b8 [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 (
satayev53bbc082020-07-23 15:15:54 +000020 "android/soong/android"
Artur Satayev4e1f2bd2020-05-14 15:15:01 +010021
22 "github.com/google/blueprint"
Artur Satayev849f8442020-04-28 14:57:42 +010023)
24
25func init() {
26 android.RegisterSingletonType("apex_depsinfo_singleton", apexDepsInfoSingletonFactory)
27}
28
29type apexDepsInfoSingleton struct {
Artur Satayev4e1f2bd2020-05-14 15:15:01 +010030 allowedApexDepsInfoCheckResult android.OutputPath
Artur Satayev849f8442020-04-28 14:57:42 +010031}
32
33func apexDepsInfoSingletonFactory() android.Singleton {
34 return &apexDepsInfoSingleton{}
35}
36
Artur Satayev4e1f2bd2020-05-14 15:15:01 +010037var (
38 // Generate new apex allowed_deps.txt by merging all internal dependencies.
39 generateApexDepsInfoFilesRule = pctx.AndroidStaticRule("generateApexDepsInfoFilesRule", blueprint.RuleParams{
40 Command: "cat $out.rsp | xargs cat" +
41 // Only track non-external dependencies, i.e. those that end up in the binary
42 " | grep -v '(external)'" +
43 // Ignore comments in any of the files
44 " | grep -v '^#'" +
45 " | sort -u -f >$out",
Artur Satayev849f8442020-04-28 14:57:42 +010046 Rspfile: "$out.rsp",
47 RspfileContent: "$in",
Artur Satayev4e1f2bd2020-05-14 15:15:01 +010048 })
49
50 // Diff two given lists while ignoring comments in the allowed deps file.
51 diffAllowedApexDepsInfoRule = pctx.AndroidStaticRule("diffAllowedApexDepsInfoRule", blueprint.RuleParams{
52 Description: "Diff ${allowed_deps} and ${new_allowed_deps}",
53 Command: `
54 if grep -v '^#' ${allowed_deps} | diff -B - ${new_allowed_deps}; then
55 touch ${out};
56 else
57 echo -e "\n******************************";
58 echo "ERROR: go/apex-allowed-deps-error";
59 echo "******************************";
60 echo "Detected changes to allowed dependencies in updatable modules.";
Artur Satayevfdb61ed2021-03-11 16:45:04 +000061 echo "To fix and update packages/modules/common/build/allowed_deps.txt, please run:";
62 echo "$$ (croot && packages/modules/common/build/update-apex-allowed-deps.sh)";
Artur Satayev4e1f2bd2020-05-14 15:15:01 +010063 echo "Members of mainline-modularization@google.com will review the changes.";
64 echo -e "******************************\n";
65 exit 1;
66 fi;
67 `,
68 }, "allowed_deps", "new_allowed_deps")
Artur Satayev849f8442020-04-28 14:57:42 +010069)
70
71func (s *apexDepsInfoSingleton) GenerateBuildActions(ctx android.SingletonContext) {
72 updatableFlatLists := android.Paths{}
73 ctx.VisitAllModules(func(module android.Module) {
74 if binaryInfo, ok := module.(android.ApexBundleDepsInfoIntf); ok {
Colin Cross56a83212020-09-15 18:30:11 -070075 apexInfo := ctx.ModuleProvider(module, android.ApexInfoProvider).(android.ApexInfo)
76 if path := binaryInfo.FlatListPath(); path != nil {
77 if binaryInfo.Updatable() || apexInfo.Updatable {
78 updatableFlatLists = append(updatableFlatLists, path)
79 }
Artur Satayev849f8442020-04-28 14:57:42 +010080 }
81 }
82 })
83
Artur Satayevb77b0c52021-03-18 11:04:09 +000084 allowedDepsSource := android.ExistentPathForSource(ctx, "packages/modules/common/build/allowed_deps.txt")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +010085 newAllowedDeps := android.PathForOutput(ctx, "apex", "depsinfo", "new-allowed-deps.txt")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +010086 s.allowedApexDepsInfoCheckResult = android.PathForOutput(ctx, newAllowedDeps.Rel()+".check")
Artur Satayevb77b0c52021-03-18 11:04:09 +000087
88 if !allowedDepsSource.Valid() {
89 // Unbundled projects may not have packages/modules/common/ checked out; ignore those.
90 ctx.Build(pctx, android.BuildParams{
91 Rule: android.Touch,
92 Output: s.allowedApexDepsInfoCheckResult,
93 })
94 } else {
95 allowedDeps := allowedDepsSource.Path()
96
97 ctx.Build(pctx, android.BuildParams{
98 Rule: generateApexDepsInfoFilesRule,
99 Inputs: append(updatableFlatLists, allowedDeps),
100 Output: newAllowedDeps,
101 })
102
103 ctx.Build(pctx, android.BuildParams{
104 Rule: diffAllowedApexDepsInfoRule,
105 Input: newAllowedDeps,
106 Output: s.allowedApexDepsInfoCheckResult,
107 Args: map[string]string{
108 "allowed_deps": allowedDeps.String(),
109 "new_allowed_deps": newAllowedDeps.String(),
110 },
111 })
112 }
Artur Satayeved667a82020-07-30 18:26:28 +0100113
114 ctx.Phony("apex-allowed-deps-check", s.allowedApexDepsInfoCheckResult)
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100115}
116
117func (s *apexDepsInfoSingleton) MakeVars(ctx android.MakeVarsContext) {
118 // Export check result to Make. The path is added to droidcore.
119 ctx.Strict("APEX_ALLOWED_DEPS_CHECK", s.allowedApexDepsInfoCheckResult.String())
Artur Satayev849f8442020-04-28 14:57:42 +0100120}