blob: f96491f88a68c73fe20809e508148067c503901e [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 (
Artur Satayev849f8442020-04-28 14:57:42 +010020 "android/soong/android"
Artur Satayeva4405fa2020-05-14 15:15:01 +010021 "github.com/google/blueprint"
Artur Satayev849f8442020-04-28 14:57:42 +010022)
23
24func init() {
25 android.RegisterSingletonType("apex_depsinfo_singleton", apexDepsInfoSingletonFactory)
26}
27
28type apexDepsInfoSingleton struct {
Artur Satayeva4405fa2020-05-14 15:15:01 +010029 allowedApexDepsInfoCheckResult android.OutputPath
Artur Satayev849f8442020-04-28 14:57:42 +010030}
31
32func apexDepsInfoSingletonFactory() android.Singleton {
33 return &apexDepsInfoSingleton{}
34}
35
Artur Satayeva4405fa2020-05-14 15:15:01 +010036var (
37 mergeApexDepsInfoFilesRule = pctx.AndroidStaticRule("mergeApexDepsInfoFilesRule", blueprint.RuleParams{
Artur Satayev849f8442020-04-28 14:57:42 +010038 Command: "cat $out.rsp | xargs cat > $out",
39 Rspfile: "$out.rsp",
40 RspfileContent: "$in",
Artur Satayeva4405fa2020-05-14 15:15:01 +010041 })
42
43 // Filter out apex dependencies that are external or safe to ignore for build determinism.
44 filterApexDepsRule = pctx.AndroidStaticRule("filterApexDepsRule", blueprint.RuleParams{
45 Command: "cat ${in}" +
46 // Only track non-external dependencies, i.e. those that end up in the binary...
47 " | grep -v '(external)'" +
48 // ...and those that are safe in any apex but can be different per product.
49 " | grep -v 'libgcc_stripped'" +
50 " | grep -v 'libunwind_llvm'" +
51 " | grep -v 'ndk_crtbegin_so.19'" +
52 " | grep -v 'ndk_crtbegin_so.21'" +
53 " | grep -v 'ndk_crtbegin_so.27'" +
54 " | grep -v 'ndk_crtend_so.19'" +
55 " | grep -v 'ndk_crtend_so.21'" +
56 " | grep -v 'ndk_crtend_so.27'" +
57 " | grep -v 'ndk_libunwind'" +
58 " | grep -v 'prebuilt_libclang_rt.builtins-aarch64-android'" +
59 " | grep -v 'prebuilt_libclang_rt.builtins-arm-android'" +
60 " | grep -v 'prebuilt_libclang_rt.builtins-i686-android'" +
61 " | grep -v 'prebuilt_libclang_rt.builtins-x86_64-android'" +
62 " | grep -v 'libclang_rt.hwasan-aarch64-android.llndk'" +
63 " > ${out}",
64 })
65
66 diffAllowedApexDepsInfoRule = pctx.AndroidStaticRule("diffAllowedApexDepsInfoRule", blueprint.RuleParams{
67 // Diff two given lists while ignoring comments in the allowed deps file
68 Description: "Diff ${allowed_flatlists} and ${merged_flatlists}",
69 Command: `
70 if grep -v '^#' ${allowed_flatlists} | diff -B ${merged_flatlists} -; then
71 touch ${out};
72 else
73 echo -e "\n******************************";
74 echo "ERROR: go/apex-allowed-deps-error";
75 echo "******************************";
76 echo "Detected changes to allowed dependencies in updatable modules.";
77 echo "To fix and update build/soong/apex/allowed_deps.txt, please run:";
78 echo "$$ (croot && build/soong/scripts/update-apex-allowed-deps.sh)";
79 echo "Members of mainline-modularization@google.com will review the changes.";
80 echo -e "******************************\n";
81 exit 1;
82 fi;`,
83 }, "allowed_flatlists", "merged_flatlists")
Artur Satayev849f8442020-04-28 14:57:42 +010084)
85
86func (s *apexDepsInfoSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Artur Satayeva4405fa2020-05-14 15:15:01 +010087 modulePaths := map[string]android.Path{}
Artur Satayev849f8442020-04-28 14:57:42 +010088 ctx.VisitAllModules(func(module android.Module) {
89 if binaryInfo, ok := module.(android.ApexBundleDepsInfoIntf); ok {
Artur Satayeva4405fa2020-05-14 15:15:01 +010090 if !binaryInfo.Updatable() {
91 return
92 }
93 if path := binaryInfo.FlatListPath(); path.String() != "" {
94 // TODO(b/159734404): don't use module.String() to sort modules/variants.
95 // This is needed though, as an order of module variants may be
96 // different between products.
97 ms := module.String()
98 if _, ok := modulePaths[ms]; !ok {
99 modulePaths[ms] = path
100 } else if modulePaths[ms] != path {
101 ctx.Errorf("Mismatching output paths for the same module %v:\n%v\n%v", ms, modulePaths[ms], path)
Artur Satayev849f8442020-04-28 14:57:42 +0100102 }
103 }
104 }
105 })
106
Artur Satayeva4405fa2020-05-14 15:15:01 +0100107 updatableFlatLists := android.Paths{}
108 // Avoid non-determinism by sorting module and variation names.
109 for _, key := range android.FirstUniqueStrings(android.SortedStringKeys(modulePaths)) {
110 updatableFlatLists = append(updatableFlatLists, modulePaths[key])
111 }
112
113 // Merge all individual flatlists of updatable modules into a single output file
114 updatableFlatListsPath := android.PathForOutput(ctx, "apex", "depsinfo", "updatable-flatlists.txt")
Artur Satayev849f8442020-04-28 14:57:42 +0100115 ctx.Build(pctx, android.BuildParams{
Artur Satayeva4405fa2020-05-14 15:15:01 +0100116 Rule: mergeApexDepsInfoFilesRule,
117 Inputs: updatableFlatLists,
118 Output: updatableFlatListsPath,
Artur Satayev849f8442020-04-28 14:57:42 +0100119 })
Artur Satayeva4405fa2020-05-14 15:15:01 +0100120
121 // Build a filtered version of updatable flatlists without external dependencies
122 filteredFlatLists := android.PathForOutput(ctx, "apex", "depsinfo", "filtered-updatable-flatlists.txt")
123 ctx.Build(pctx, android.BuildParams{
124 Rule: filterApexDepsRule,
125 Input: updatableFlatListsPath,
126 Output: filteredFlatLists,
127 })
128
129 // Check filtered version against allowed deps
130 allowedDeps := android.ExistentPathForSource(ctx, "build/soong/apex/allowed_deps.txt").Path()
131 s.allowedApexDepsInfoCheckResult = android.PathForOutput(ctx, filteredFlatLists.Rel()+".check")
132 ctx.Build(pctx, android.BuildParams{
133 Rule: diffAllowedApexDepsInfoRule,
134 Input: filteredFlatLists,
135 Output: s.allowedApexDepsInfoCheckResult,
136 Args: map[string]string{
137 "allowed_flatlists": allowedDeps.String(),
138 "merged_flatlists": filteredFlatLists.String(),
139 },
140 })
141}
142
143func (s *apexDepsInfoSingleton) MakeVars(ctx android.MakeVarsContext) {
144 // Export check result to Make. The path is added to droidcore.
145 ctx.Strict("APEX_ALLOWED_DEPS_CHECK", s.allowedApexDepsInfoCheckResult.String())
Artur Satayev849f8442020-04-28 14:57:42 +0100146}