blob: 229f0f9ce6f0e9264a15e3a3c974f855123c710a [file] [log] [blame]
Dan Willemsenf052f782017-05-18 15:29:04 -07001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package build
16
17import (
18 "fmt"
19 "io/ioutil"
20 "os"
21 "path/filepath"
22 "strings"
Nan Zhang17f27672018-12-12 16:01:49 -080023
24 "android/soong/ui/metrics"
Dan Willemsenf052f782017-05-18 15:29:04 -070025)
26
27func removeGlobs(ctx Context, globs ...string) {
28 for _, glob := range globs {
29 files, err := filepath.Glob(glob)
30 if err != nil {
31 // Only possible error is ErrBadPattern
32 panic(fmt.Errorf("%q: %s", glob, err))
33 }
34
35 for _, file := range files {
36 err = os.RemoveAll(file)
37 if err != nil {
38 ctx.Fatalf("Failed to remove file %q: %v", file, err)
39 }
40 }
41 }
42}
43
44// Remove everything under the out directory. Don't remove the out directory
45// itself in case it's a symlink.
46func clean(ctx Context, config Config, what int) {
47 removeGlobs(ctx, filepath.Join(config.OutDir(), "*"))
48 ctx.Println("Entire build directory removed.")
49}
50
51func dataClean(ctx Context, config Config, what int) {
52 removeGlobs(ctx, filepath.Join(config.ProductOut(), "data", "*"))
53}
54
55// installClean deletes all of the installed files -- the intent is to remove
56// files that may no longer be installed, either because the user previously
57// installed them, or they were previously installed by default but no longer
58// are.
59//
60// This is faster than a full clean, since we're not deleting the
61// intermediates. Instead of recompiling, we can just copy the results.
62func installClean(ctx Context, config Config, what int) {
63 dataClean(ctx, config, what)
64
65 if hostCrossOutPath := config.hostCrossOut(); hostCrossOutPath != "" {
66 hostCrossOut := func(path string) string {
67 return filepath.Join(hostCrossOutPath, path)
68 }
69 removeGlobs(ctx,
70 hostCrossOut("bin"),
71 hostCrossOut("coverage"),
72 hostCrossOut("lib*"),
73 hostCrossOut("nativetest*"))
74 }
75
76 hostOutPath := config.HostOut()
77 hostOut := func(path string) string {
78 return filepath.Join(hostOutPath, path)
79 }
80
81 productOutPath := config.ProductOut()
82 productOut := func(path string) string {
83 return filepath.Join(productOutPath, path)
84 }
85
86 // Host bin, frameworks, and lib* are intentionally omitted, since
87 // otherwise we'd have to rebuild any generated files created with
88 // those tools.
89 removeGlobs(ctx,
90 hostOut("obj/NOTICE_FILES"),
91 hostOut("obj/PACKAGING"),
92 hostOut("coverage"),
93 hostOut("cts"),
94 hostOut("nativetest*"),
95 hostOut("sdk"),
96 hostOut("sdk_addon"),
97 hostOut("testcases"),
98 hostOut("vts"),
99 productOut("*.img"),
Dan Willemsenf052f782017-05-18 15:29:04 -0700100 productOut("*.zip"),
Michael Bestas9aed8ed2017-12-21 04:03:01 +0200101 productOut("*.zip.md5sum"),
pimpmaneatonc6771b12019-10-14 09:13:42 -0600102 productOut("Changelog-*.txt"),
Dan Willemsena18660d2017-06-01 14:23:36 -0700103 productOut("android-info.txt"),
mydongistiny26a0a232019-10-14 09:20:01 -0600104 productOut("build_fingerprint.txt"),
105 productOut("build_thumbprint.txt"),
106 productOut("dexpreopt.config"),
107 productOut("installed-files.*"),
108 productOut("installed-files-*.*"),
109 productOut("recovery.id"),
Dan Willemsenf052f782017-05-18 15:29:04 -0700110 productOut("kernel"),
111 productOut("data"),
mydongistiny26a0a232019-10-14 09:20:01 -0600112 productOut("dtbo"),
Dan Willemsenf052f782017-05-18 15:29:04 -0700113 productOut("skin"),
mydongistiny26a0a232019-10-14 09:20:01 -0600114 productOut("obj/KERNEL_OBJ"),
Dan Willemsenf052f782017-05-18 15:29:04 -0700115 productOut("obj/NOTICE_FILES"),
116 productOut("obj/PACKAGING"),
Tom Cherry7803a012018-08-08 13:24:32 -0700117 productOut("ramdisk"),
Dan Willemsenf052f782017-05-18 15:29:04 -0700118 productOut("recovery"),
119 productOut("root"),
120 productOut("system"),
121 productOut("system_other"),
122 productOut("vendor"),
Jaekyun Seokf6307cc2018-05-16 12:25:41 +0900123 productOut("product"),
Dario Freni95cf7672018-08-17 00:57:57 +0100124 productOut("product_services"),
Dan Willemsenf052f782017-05-18 15:29:04 -0700125 productOut("oem"),
126 productOut("obj/FAKE"),
mydongistiny50d25c02018-08-18 01:14:25 -0700127 productOut("obj/KERNEL_OBJ"),
Dan Willemsenf052f782017-05-18 15:29:04 -0700128 productOut("breakpad"),
129 productOut("cache"),
130 productOut("coverage"),
131 productOut("installer"),
132 productOut("odm"),
133 productOut("sysloader"),
134 productOut("testcases"))
135}
136
137// Since products and build variants (unfortunately) shared the same
138// PRODUCT_OUT staging directory, things can get out of sync if different
139// build configurations are built in the same tree. This function will
140// notice when the configuration has changed and call installclean to
141// remove the files necessary to keep things consistent.
142func installCleanIfNecessary(ctx Context, config Config) {
143 configFile := config.DevicePreviousProductConfig()
144 prefix := "PREVIOUS_BUILD_CONFIG := "
145 suffix := "\n"
146 currentProduct := prefix + config.TargetProduct() + "-" + config.TargetBuildVariant() + suffix
147
Dan Willemsene0879fc2017-08-04 15:06:27 -0700148 ensureDirectoriesExist(ctx, filepath.Dir(configFile))
149
Dan Willemsenf052f782017-05-18 15:29:04 -0700150 writeConfig := func() {
151 err := ioutil.WriteFile(configFile, []byte(currentProduct), 0666)
152 if err != nil {
153 ctx.Fatalln("Failed to write product config:", err)
154 }
155 }
156
157 prev, err := ioutil.ReadFile(configFile)
158 if err != nil {
159 if os.IsNotExist(err) {
160 writeConfig()
161 return
162 } else {
163 ctx.Fatalln("Failed to read previous product config:", err)
164 }
165 } else if string(prev) == currentProduct {
166 return
167 }
168
169 if disable, _ := config.Environment().Get("DISABLE_AUTO_INSTALLCLEAN"); disable == "true" {
170 ctx.Println("DISABLE_AUTO_INSTALLCLEAN is set; skipping auto-clean. Your tree may be in an inconsistent state.")
171 return
172 }
173
Nan Zhang17f27672018-12-12 16:01:49 -0800174 ctx.BeginTrace(metrics.PrimaryNinja, "installclean")
Dan Willemsenf052f782017-05-18 15:29:04 -0700175 defer ctx.EndTrace()
176
177 prevConfig := strings.TrimPrefix(strings.TrimSuffix(string(prev), suffix), prefix)
178 currentConfig := strings.TrimPrefix(strings.TrimSuffix(currentProduct, suffix), prefix)
179
180 ctx.Printf("Build configuration changed: %q -> %q, forcing installclean\n", prevConfig, currentConfig)
181
182 installClean(ctx, config, 0)
183
184 writeConfig()
185}
Alberto97c3b18582018-05-05 22:01:02 +0200186
187// Remove everything relevant for a clean ota package
188func deviceClean(ctx Context, config Config, what int) {
Harsh Shandilya2206a8a2019-05-12 05:48:29 +0530189 removeGlobs(ctx, config.ProductOut())
Alberto97c3b18582018-05-05 22:01:02 +0200190}