• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// 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"
23
24	"android/soong/ui/metrics"
25)
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"),
100		productOut("*.zip"),
101		productOut("android-info.txt"),
102		productOut("kernel"),
103		productOut("data"),
104		productOut("skin"),
105		productOut("obj/NOTICE_FILES"),
106		productOut("obj/PACKAGING"),
107		productOut("ramdisk"),
108		productOut("recovery"),
109		productOut("root"),
110		productOut("system"),
111		productOut("system_other"),
112		productOut("vendor"),
113		productOut("product"),
114		productOut("product_services"),
115		productOut("oem"),
116		productOut("obj/FAKE"),
117		productOut("breakpad"),
118		productOut("cache"),
119		productOut("coverage"),
120		productOut("installer"),
121		productOut("odm"),
122		productOut("sysloader"),
123		productOut("testcases"))
124}
125
126// Since products and build variants (unfortunately) shared the same
127// PRODUCT_OUT staging directory, things can get out of sync if different
128// build configurations are built in the same tree. This function will
129// notice when the configuration has changed and call installclean to
130// remove the files necessary to keep things consistent.
131func installCleanIfNecessary(ctx Context, config Config) {
132	configFile := config.DevicePreviousProductConfig()
133	prefix := "PREVIOUS_BUILD_CONFIG := "
134	suffix := "\n"
135	currentProduct := prefix + config.TargetProduct() + "-" + config.TargetBuildVariant() + suffix
136
137	ensureDirectoriesExist(ctx, filepath.Dir(configFile))
138
139	writeConfig := func() {
140		err := ioutil.WriteFile(configFile, []byte(currentProduct), 0666)
141		if err != nil {
142			ctx.Fatalln("Failed to write product config:", err)
143		}
144	}
145
146	prev, err := ioutil.ReadFile(configFile)
147	if err != nil {
148		if os.IsNotExist(err) {
149			writeConfig()
150			return
151		} else {
152			ctx.Fatalln("Failed to read previous product config:", err)
153		}
154	} else if string(prev) == currentProduct {
155		return
156	}
157
158	if disable, _ := config.Environment().Get("DISABLE_AUTO_INSTALLCLEAN"); disable == "true" {
159		ctx.Println("DISABLE_AUTO_INSTALLCLEAN is set; skipping auto-clean. Your tree may be in an inconsistent state.")
160		return
161	}
162
163	ctx.BeginTrace(metrics.PrimaryNinja, "installclean")
164	defer ctx.EndTrace()
165
166	prevConfig := strings.TrimPrefix(strings.TrimSuffix(string(prev), suffix), prefix)
167	currentConfig := strings.TrimPrefix(strings.TrimSuffix(currentProduct, suffix), prefix)
168
169	ctx.Printf("Build configuration changed: %q -> %q, forcing installclean\n", prevConfig, currentConfig)
170
171	installClean(ctx, config, 0)
172
173	writeConfig()
174}
175