blob: 0b44b4d62d14f0b00d69afba9bdd48c7ac7df6cc [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,
Roland Levillaine5f9ee52019-09-11 14:50:08 +010090 hostOut("apex"),
Dan Willemsenf052f782017-05-18 15:29:04 -070091 hostOut("obj/NOTICE_FILES"),
92 hostOut("obj/PACKAGING"),
93 hostOut("coverage"),
94 hostOut("cts"),
95 hostOut("nativetest*"),
96 hostOut("sdk"),
97 hostOut("sdk_addon"),
98 hostOut("testcases"),
99 hostOut("vts"),
Dan Shi53f1a192019-11-26 10:02:53 -0800100 hostOut("vts-core"),
Dan Willemsenf052f782017-05-18 15:29:04 -0700101 productOut("*.img"),
Dan Willemsenf052f782017-05-18 15:29:04 -0700102 productOut("*.zip"),
Dan Willemsena18660d2017-06-01 14:23:36 -0700103 productOut("android-info.txt"),
Steven Moreland0aabb112019-08-26 11:31:33 -0700104 productOut("apex"),
Dan Willemsenf052f782017-05-18 15:29:04 -0700105 productOut("kernel"),
106 productOut("data"),
107 productOut("skin"),
108 productOut("obj/NOTICE_FILES"),
109 productOut("obj/PACKAGING"),
Tom Cherry7803a012018-08-08 13:24:32 -0700110 productOut("ramdisk"),
Bowgo Tsai5145c2c2019-10-08 18:12:37 +0800111 productOut("debug_ramdisk"),
112 productOut("test_harness_ramdisk"),
Dan Willemsenf052f782017-05-18 15:29:04 -0700113 productOut("recovery"),
114 productOut("root"),
115 productOut("system"),
116 productOut("system_other"),
117 productOut("vendor"),
Jaekyun Seokf6307cc2018-05-16 12:25:41 +0900118 productOut("product"),
Justin Yund5f6c822019-06-25 16:47:17 +0900119 productOut("system_ext"),
Dan Willemsenf052f782017-05-18 15:29:04 -0700120 productOut("oem"),
121 productOut("obj/FAKE"),
122 productOut("breakpad"),
123 productOut("cache"),
124 productOut("coverage"),
125 productOut("installer"),
126 productOut("odm"),
127 productOut("sysloader"),
128 productOut("testcases"))
129}
130
131// Since products and build variants (unfortunately) shared the same
132// PRODUCT_OUT staging directory, things can get out of sync if different
133// build configurations are built in the same tree. This function will
134// notice when the configuration has changed and call installclean to
135// remove the files necessary to keep things consistent.
136func installCleanIfNecessary(ctx Context, config Config) {
137 configFile := config.DevicePreviousProductConfig()
138 prefix := "PREVIOUS_BUILD_CONFIG := "
139 suffix := "\n"
140 currentProduct := prefix + config.TargetProduct() + "-" + config.TargetBuildVariant() + suffix
141
Dan Willemsene0879fc2017-08-04 15:06:27 -0700142 ensureDirectoriesExist(ctx, filepath.Dir(configFile))
143
Dan Willemsenf052f782017-05-18 15:29:04 -0700144 writeConfig := func() {
145 err := ioutil.WriteFile(configFile, []byte(currentProduct), 0666)
146 if err != nil {
147 ctx.Fatalln("Failed to write product config:", err)
148 }
149 }
150
151 prev, err := ioutil.ReadFile(configFile)
152 if err != nil {
153 if os.IsNotExist(err) {
154 writeConfig()
155 return
156 } else {
157 ctx.Fatalln("Failed to read previous product config:", err)
158 }
159 } else if string(prev) == currentProduct {
160 return
161 }
162
163 if disable, _ := config.Environment().Get("DISABLE_AUTO_INSTALLCLEAN"); disable == "true" {
164 ctx.Println("DISABLE_AUTO_INSTALLCLEAN is set; skipping auto-clean. Your tree may be in an inconsistent state.")
165 return
166 }
167
Nan Zhang17f27672018-12-12 16:01:49 -0800168 ctx.BeginTrace(metrics.PrimaryNinja, "installclean")
Dan Willemsenf052f782017-05-18 15:29:04 -0700169 defer ctx.EndTrace()
170
171 prevConfig := strings.TrimPrefix(strings.TrimSuffix(string(prev), suffix), prefix)
172 currentConfig := strings.TrimPrefix(strings.TrimSuffix(currentProduct, suffix), prefix)
173
174 ctx.Printf("Build configuration changed: %q -> %q, forcing installclean\n", prevConfig, currentConfig)
175
176 installClean(ctx, config, 0)
177
178 writeConfig()
179}