2017-05-19 00:29:04 +02:00
|
|
|
// Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package build
|
|
|
|
|
|
|
|
import (
|
2020-01-03 22:40:45 +01:00
|
|
|
"bytes"
|
2017-05-19 00:29:04 +02:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-01-03 22:40:45 +01:00
|
|
|
"sort"
|
2017-05-19 00:29:04 +02:00
|
|
|
"strings"
|
2018-12-13 01:01:49 +01:00
|
|
|
|
|
|
|
"android/soong/ui/metrics"
|
2017-05-19 00:29:04 +02:00
|
|
|
)
|
|
|
|
|
2020-11-24 15:13:41 +01:00
|
|
|
// Given a series of glob patterns, remove matching files and directories from the filesystem.
|
|
|
|
// For example, "malware*" would remove all files and directories in the current directory that begin with "malware".
|
2017-05-19 00:29:04 +02:00
|
|
|
func removeGlobs(ctx Context, globs ...string) {
|
|
|
|
for _, glob := range globs {
|
2020-11-24 15:13:41 +01:00
|
|
|
// Find files and directories that match this glob pattern.
|
2017-05-19 00:29:04 +02:00
|
|
|
files, err := filepath.Glob(glob)
|
|
|
|
if err != nil {
|
|
|
|
// Only possible error is ErrBadPattern
|
|
|
|
panic(fmt.Errorf("%q: %s", glob, err))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
err = os.RemoveAll(file)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Fatalf("Failed to remove file %q: %v", file, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove everything under the out directory. Don't remove the out directory
|
|
|
|
// itself in case it's a symlink.
|
2020-11-24 15:13:41 +01:00
|
|
|
func clean(ctx Context, config Config) {
|
2017-05-19 00:29:04 +02:00
|
|
|
removeGlobs(ctx, filepath.Join(config.OutDir(), "*"))
|
|
|
|
ctx.Println("Entire build directory removed.")
|
|
|
|
}
|
|
|
|
|
2020-11-24 15:13:41 +01:00
|
|
|
// Remove everything in the data directory.
|
|
|
|
func dataClean(ctx Context, config Config) {
|
2017-05-19 00:29:04 +02:00
|
|
|
removeGlobs(ctx, filepath.Join(config.ProductOut(), "data", "*"))
|
2020-11-24 15:13:41 +01:00
|
|
|
ctx.Println("Entire data directory removed.")
|
2017-05-19 00:29:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// installClean deletes all of the installed files -- the intent is to remove
|
|
|
|
// files that may no longer be installed, either because the user previously
|
|
|
|
// installed them, or they were previously installed by default but no longer
|
|
|
|
// are.
|
|
|
|
//
|
|
|
|
// This is faster than a full clean, since we're not deleting the
|
|
|
|
// intermediates. Instead of recompiling, we can just copy the results.
|
2020-11-24 15:13:41 +01:00
|
|
|
func installClean(ctx Context, config Config) {
|
|
|
|
dataClean(ctx, config)
|
2017-05-19 00:29:04 +02:00
|
|
|
|
|
|
|
if hostCrossOutPath := config.hostCrossOut(); hostCrossOutPath != "" {
|
|
|
|
hostCrossOut := func(path string) string {
|
|
|
|
return filepath.Join(hostCrossOutPath, path)
|
|
|
|
}
|
|
|
|
removeGlobs(ctx,
|
|
|
|
hostCrossOut("bin"),
|
|
|
|
hostCrossOut("coverage"),
|
|
|
|
hostCrossOut("lib*"),
|
|
|
|
hostCrossOut("nativetest*"))
|
|
|
|
}
|
|
|
|
|
|
|
|
hostOutPath := config.HostOut()
|
|
|
|
hostOut := func(path string) string {
|
|
|
|
return filepath.Join(hostOutPath, path)
|
|
|
|
}
|
|
|
|
|
2020-10-10 04:11:22 +02:00
|
|
|
hostCommonOut := func(path string) string {
|
|
|
|
return filepath.Join(config.hostOutRoot(), "common", path)
|
|
|
|
}
|
|
|
|
|
2017-05-19 00:29:04 +02:00
|
|
|
productOutPath := config.ProductOut()
|
|
|
|
productOut := func(path string) string {
|
|
|
|
return filepath.Join(productOutPath, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Host bin, frameworks, and lib* are intentionally omitted, since
|
|
|
|
// otherwise we'd have to rebuild any generated files created with
|
|
|
|
// those tools.
|
|
|
|
removeGlobs(ctx,
|
2019-09-11 15:50:08 +02:00
|
|
|
hostOut("apex"),
|
2017-05-19 00:29:04 +02:00
|
|
|
hostOut("obj/NOTICE_FILES"),
|
|
|
|
hostOut("obj/PACKAGING"),
|
|
|
|
hostOut("coverage"),
|
|
|
|
hostOut("cts"),
|
|
|
|
hostOut("nativetest*"),
|
|
|
|
hostOut("sdk"),
|
|
|
|
hostOut("sdk_addon"),
|
|
|
|
hostOut("testcases"),
|
|
|
|
hostOut("vts"),
|
2020-03-19 06:42:00 +01:00
|
|
|
hostOut("vts10"),
|
2019-11-26 19:02:53 +01:00
|
|
|
hostOut("vts-core"),
|
2020-10-10 04:11:22 +02:00
|
|
|
hostCommonOut("obj/PACKAGING"),
|
2017-05-19 00:29:04 +02:00
|
|
|
productOut("*.img"),
|
|
|
|
productOut("*.zip"),
|
2017-06-01 23:23:36 +02:00
|
|
|
productOut("android-info.txt"),
|
2020-05-08 01:39:36 +02:00
|
|
|
productOut("misc_info.txt"),
|
2019-08-26 20:31:33 +02:00
|
|
|
productOut("apex"),
|
2017-05-19 00:29:04 +02:00
|
|
|
productOut("kernel"),
|
2021-01-21 17:16:47 +01:00
|
|
|
productOut("kernel-*"),
|
2017-05-19 00:29:04 +02:00
|
|
|
productOut("data"),
|
|
|
|
productOut("skin"),
|
|
|
|
productOut("obj/NOTICE_FILES"),
|
|
|
|
productOut("obj/PACKAGING"),
|
2018-08-08 22:24:32 +02:00
|
|
|
productOut("ramdisk"),
|
2019-10-08 12:12:37 +02:00
|
|
|
productOut("debug_ramdisk"),
|
2021-03-03 08:44:02 +01:00
|
|
|
productOut("vendor_ramdisk"),
|
2020-03-19 19:57:11 +01:00
|
|
|
productOut("vendor_debug_ramdisk"),
|
2019-10-08 12:12:37 +02:00
|
|
|
productOut("test_harness_ramdisk"),
|
2017-05-19 00:29:04 +02:00
|
|
|
productOut("recovery"),
|
|
|
|
productOut("root"),
|
|
|
|
productOut("system"),
|
|
|
|
productOut("system_other"),
|
|
|
|
productOut("vendor"),
|
2020-10-14 20:29:16 +02:00
|
|
|
productOut("vendor_dlkm"),
|
2018-05-16 05:25:41 +02:00
|
|
|
productOut("product"),
|
2019-06-25 09:47:17 +02:00
|
|
|
productOut("system_ext"),
|
2017-05-19 00:29:04 +02:00
|
|
|
productOut("oem"),
|
|
|
|
productOut("obj/FAKE"),
|
|
|
|
productOut("breakpad"),
|
|
|
|
productOut("cache"),
|
|
|
|
productOut("coverage"),
|
|
|
|
productOut("installer"),
|
|
|
|
productOut("odm"),
|
2020-10-14 20:29:16 +02:00
|
|
|
productOut("odm_dlkm"),
|
2017-05-19 00:29:04 +02:00
|
|
|
productOut("sysloader"),
|
2021-04-28 04:45:25 +02:00
|
|
|
productOut("testcases"),
|
|
|
|
productOut("symbols"))
|
2017-05-19 00:29:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Since products and build variants (unfortunately) shared the same
|
|
|
|
// PRODUCT_OUT staging directory, things can get out of sync if different
|
|
|
|
// build configurations are built in the same tree. This function will
|
2020-11-24 15:13:41 +01:00
|
|
|
// notice when the configuration has changed and call installClean to
|
2017-05-19 00:29:04 +02:00
|
|
|
// remove the files necessary to keep things consistent.
|
|
|
|
func installCleanIfNecessary(ctx Context, config Config) {
|
|
|
|
configFile := config.DevicePreviousProductConfig()
|
|
|
|
prefix := "PREVIOUS_BUILD_CONFIG := "
|
|
|
|
suffix := "\n"
|
2020-11-24 15:13:41 +01:00
|
|
|
currentConfig := prefix + config.TargetProduct() + "-" + config.TargetBuildVariant() + suffix
|
2017-05-19 00:29:04 +02:00
|
|
|
|
2017-08-05 00:06:27 +02:00
|
|
|
ensureDirectoriesExist(ctx, filepath.Dir(configFile))
|
|
|
|
|
2017-05-19 00:29:04 +02:00
|
|
|
writeConfig := func() {
|
2020-11-24 15:13:41 +01:00
|
|
|
err := ioutil.WriteFile(configFile, []byte(currentConfig), 0666) // a+rw
|
2017-05-19 00:29:04 +02:00
|
|
|
if err != nil {
|
|
|
|
ctx.Fatalln("Failed to write product config:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-24 15:13:41 +01:00
|
|
|
previousConfigBytes, err := ioutil.ReadFile(configFile)
|
2017-05-19 00:29:04 +02:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2020-11-24 15:13:41 +01:00
|
|
|
// Just write the new config file, no old config file to worry about.
|
2017-05-19 00:29:04 +02:00
|
|
|
writeConfig()
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
ctx.Fatalln("Failed to read previous product config:", err)
|
|
|
|
}
|
2020-11-24 15:13:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
previousConfig := string(previousConfigBytes)
|
|
|
|
if previousConfig == currentConfig {
|
|
|
|
// Same config as before - nothing to clean.
|
2017-05-19 00:29:04 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-24 15:13:41 +01:00
|
|
|
if config.Environment().IsEnvTrue("DISABLE_AUTO_INSTALLCLEAN") {
|
|
|
|
ctx.Println("DISABLE_AUTO_INSTALLCLEAN is set and true; skipping auto-clean. Your tree may be in an inconsistent state.")
|
2017-05-19 00:29:04 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-13 01:01:49 +01:00
|
|
|
ctx.BeginTrace(metrics.PrimaryNinja, "installclean")
|
2017-05-19 00:29:04 +02:00
|
|
|
defer ctx.EndTrace()
|
|
|
|
|
2020-11-24 15:13:41 +01:00
|
|
|
previousProductAndVariant := strings.TrimPrefix(strings.TrimSuffix(previousConfig, suffix), prefix)
|
|
|
|
currentProductAndVariant := strings.TrimPrefix(strings.TrimSuffix(currentConfig, suffix), prefix)
|
2017-05-19 00:29:04 +02:00
|
|
|
|
2020-11-24 15:13:41 +01:00
|
|
|
ctx.Printf("Build configuration changed: %q -> %q, forcing installclean\n", previousProductAndVariant, currentProductAndVariant)
|
2017-05-19 00:29:04 +02:00
|
|
|
|
2020-11-24 15:13:41 +01:00
|
|
|
installClean(ctx, config)
|
2017-05-19 00:29:04 +02:00
|
|
|
|
|
|
|
writeConfig()
|
|
|
|
}
|
2020-01-03 22:40:45 +01:00
|
|
|
|
|
|
|
// cleanOldFiles takes an input file (with all paths relative to basePath), and removes files from
|
|
|
|
// the filesystem if they were removed from the input file since the last execution.
|
2020-11-24 15:13:41 +01:00
|
|
|
func cleanOldFiles(ctx Context, basePath, newFile string) {
|
|
|
|
newFile = filepath.Join(basePath, newFile)
|
|
|
|
oldFile := newFile + ".previous"
|
2020-01-03 22:40:45 +01:00
|
|
|
|
2020-11-24 15:13:41 +01:00
|
|
|
if _, err := os.Stat(newFile); err != nil {
|
|
|
|
ctx.Fatalf("Expected %q to be readable", newFile)
|
2020-01-03 22:40:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(oldFile); os.IsNotExist(err) {
|
2020-11-24 15:13:41 +01:00
|
|
|
if err := os.Rename(newFile, oldFile); err != nil {
|
|
|
|
ctx.Fatalf("Failed to rename file list (%q->%q): %v", newFile, oldFile, err)
|
2020-01-03 22:40:45 +01:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-24 15:13:41 +01:00
|
|
|
var newData, oldData []byte
|
|
|
|
if data, err := ioutil.ReadFile(newFile); err == nil {
|
|
|
|
newData = data
|
|
|
|
} else {
|
|
|
|
ctx.Fatalf("Failed to read list of installable files (%q): %v", newFile, err)
|
|
|
|
}
|
|
|
|
if data, err := ioutil.ReadFile(oldFile); err == nil {
|
|
|
|
oldData = data
|
2020-01-03 22:40:45 +01:00
|
|
|
} else {
|
2020-11-24 15:13:41 +01:00
|
|
|
ctx.Fatalf("Failed to read list of installable files (%q): %v", oldFile, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Common case: nothing has changed
|
|
|
|
if bytes.Equal(newData, oldData) {
|
|
|
|
return
|
2020-01-03 22:40:45 +01:00
|
|
|
}
|
|
|
|
|
2020-11-24 15:13:41 +01:00
|
|
|
var newPaths, oldPaths []string
|
|
|
|
newPaths = strings.Fields(string(newData))
|
|
|
|
oldPaths = strings.Fields(string(oldData))
|
|
|
|
|
2020-01-03 22:40:45 +01:00
|
|
|
// These should be mostly sorted by make already, but better make sure Go concurs
|
|
|
|
sort.Strings(newPaths)
|
|
|
|
sort.Strings(oldPaths)
|
|
|
|
|
|
|
|
for len(oldPaths) > 0 {
|
|
|
|
if len(newPaths) > 0 {
|
|
|
|
if oldPaths[0] == newPaths[0] {
|
|
|
|
// Same file; continue
|
|
|
|
newPaths = newPaths[1:]
|
|
|
|
oldPaths = oldPaths[1:]
|
|
|
|
continue
|
|
|
|
} else if oldPaths[0] > newPaths[0] {
|
|
|
|
// New file; ignore
|
|
|
|
newPaths = newPaths[1:]
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2020-11-24 15:13:41 +01:00
|
|
|
|
2020-01-03 22:40:45 +01:00
|
|
|
// File only exists in the old list; remove if it exists
|
2020-11-24 15:13:41 +01:00
|
|
|
oldPath := filepath.Join(basePath, oldPaths[0])
|
2020-01-03 22:40:45 +01:00
|
|
|
oldPaths = oldPaths[1:]
|
2020-11-24 15:13:41 +01:00
|
|
|
|
|
|
|
if oldFile, err := os.Stat(oldPath); err == nil {
|
|
|
|
if oldFile.IsDir() {
|
|
|
|
if err := os.Remove(oldPath); err == nil {
|
|
|
|
ctx.Println("Removed directory that is no longer installed: ", oldPath)
|
|
|
|
cleanEmptyDirs(ctx, filepath.Dir(oldPath))
|
2020-01-03 22:40:45 +01:00
|
|
|
} else {
|
2020-11-24 15:13:41 +01:00
|
|
|
ctx.Println("Failed to remove directory that is no longer installed (%q): %v", oldPath, err)
|
2020-01-03 22:40:45 +01:00
|
|
|
ctx.Println("It's recommended to run `m installclean`")
|
|
|
|
}
|
|
|
|
} else {
|
2020-11-24 15:13:41 +01:00
|
|
|
// Removing a file, not a directory.
|
|
|
|
if err := os.Remove(oldPath); err == nil {
|
|
|
|
ctx.Println("Removed file that is no longer installed: ", oldPath)
|
|
|
|
cleanEmptyDirs(ctx, filepath.Dir(oldPath))
|
2020-01-03 22:40:45 +01:00
|
|
|
} else if !os.IsNotExist(err) {
|
2020-11-24 15:13:41 +01:00
|
|
|
ctx.Fatalf("Failed to remove file that is no longer installed (%q): %v", oldPath, err)
|
2020-01-03 22:40:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the new list as the base for the next build
|
2020-11-24 15:13:41 +01:00
|
|
|
os.Rename(newFile, oldFile)
|
2020-01-03 22:40:45 +01:00
|
|
|
}
|
2020-02-13 23:37:15 +01:00
|
|
|
|
2020-11-24 15:13:41 +01:00
|
|
|
// cleanEmptyDirs will delete a directory if it contains no files.
|
|
|
|
// If a deletion occurs, then it also recurses upwards to try and delete empty parent directories.
|
2020-02-13 23:37:15 +01:00
|
|
|
func cleanEmptyDirs(ctx Context, dir string) {
|
|
|
|
files, err := ioutil.ReadDir(dir)
|
2020-11-24 15:13:41 +01:00
|
|
|
if err != nil {
|
|
|
|
ctx.Println("Could not read directory while trying to clean empty dirs: ", dir)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(files) > 0 {
|
|
|
|
// Directory is not empty.
|
2020-02-13 23:37:15 +01:00
|
|
|
return
|
|
|
|
}
|
2020-11-24 15:13:41 +01:00
|
|
|
|
2020-02-13 23:37:15 +01:00
|
|
|
if err := os.Remove(dir); err == nil {
|
2020-11-24 15:13:41 +01:00
|
|
|
ctx.Println("Removed empty directory (may no longer be installed?): ", dir)
|
2020-02-13 23:37:15 +01:00
|
|
|
} else {
|
2020-11-24 15:13:41 +01:00
|
|
|
ctx.Fatalf("Failed to remove empty directory (which may no longer be installed?) %q: (%v)", dir, err)
|
2020-02-13 23:37:15 +01:00
|
|
|
}
|
2020-11-24 15:13:41 +01:00
|
|
|
|
|
|
|
// Try and delete empty parent directories too.
|
2020-02-13 23:37:15 +01:00
|
|
|
cleanEmptyDirs(ctx, filepath.Dir(dir))
|
|
|
|
}
|