platform_build_soong/ui/build/cleanbuild.go
Dan Willemsen e0879fc3ae Add --skip-make to replace Soong's blueprint wrapper with soong_ui
This way we only have one way to start a build, which always has logging
/ tracing / etc, even if we don't need Kati.

There's two ways to use this:

As a direct replacement for mkdir out; cd out; ../bootstrap.bash;
./soong -- as long as --skip-make is always passed, we'll never run
Kati, and Soong will run outside of it's "make" mode. This preserves
most of the speed, and allows full user control over the Soong
configuration.

A (experimental, dangerous) way to temporarily bypass the product
variable and kati steps of a build. As long as a user is sure that
nothing has changed from the last build, and they know exactly which
Ninja targets they want to build (which may not be the same as the
arguments normally passed to 'm'), this can lead to shorter build
startup times.

Test: rm -rf out; m --skip-make libc
Test: rm -rf out; m libc; m --skip-make libc
Test: rm -rf out; mkdir out; cd out; ../bootstrap.bash; ./soong libc
Test: build/soong/scripts/build-ndk-prebuilts.sh
Change-Id: Ic0f91167b5779dba3f248a379fbaac67a75a946e
2017-08-08 22:49:28 -07:00

169 lines
5 KiB
Go

// 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 (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
func removeGlobs(ctx Context, globs ...string) {
for _, glob := range globs {
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.
func clean(ctx Context, config Config, what int) {
removeGlobs(ctx, filepath.Join(config.OutDir(), "*"))
ctx.Println("Entire build directory removed.")
}
func dataClean(ctx Context, config Config, what int) {
removeGlobs(ctx, filepath.Join(config.ProductOut(), "data", "*"))
}
// 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.
func installClean(ctx Context, config Config, what int) {
dataClean(ctx, config, what)
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)
}
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,
hostOut("obj/NOTICE_FILES"),
hostOut("obj/PACKAGING"),
hostOut("coverage"),
hostOut("cts"),
hostOut("nativetest*"),
hostOut("sdk"),
hostOut("sdk_addon"),
hostOut("testcases"),
hostOut("vts"),
productOut("*.img"),
productOut("*.zip"),
productOut("android-info.txt"),
productOut("kernel"),
productOut("data"),
productOut("skin"),
productOut("obj/NOTICE_FILES"),
productOut("obj/PACKAGING"),
productOut("recovery"),
productOut("root"),
productOut("system"),
productOut("system_other"),
productOut("vendor"),
productOut("oem"),
productOut("obj/FAKE"),
productOut("breakpad"),
productOut("cache"),
productOut("coverage"),
productOut("installer"),
productOut("odm"),
productOut("sysloader"),
productOut("testcases"))
}
// 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
// notice when the configuration has changed and call installclean to
// remove the files necessary to keep things consistent.
func installCleanIfNecessary(ctx Context, config Config) {
configFile := config.DevicePreviousProductConfig()
prefix := "PREVIOUS_BUILD_CONFIG := "
suffix := "\n"
currentProduct := prefix + config.TargetProduct() + "-" + config.TargetBuildVariant() + suffix
ensureDirectoriesExist(ctx, filepath.Dir(configFile))
writeConfig := func() {
err := ioutil.WriteFile(configFile, []byte(currentProduct), 0666)
if err != nil {
ctx.Fatalln("Failed to write product config:", err)
}
}
prev, err := ioutil.ReadFile(configFile)
if err != nil {
if os.IsNotExist(err) {
writeConfig()
return
} else {
ctx.Fatalln("Failed to read previous product config:", err)
}
} else if string(prev) == currentProduct {
return
}
if disable, _ := config.Environment().Get("DISABLE_AUTO_INSTALLCLEAN"); disable == "true" {
ctx.Println("DISABLE_AUTO_INSTALLCLEAN is set; skipping auto-clean. Your tree may be in an inconsistent state.")
return
}
ctx.BeginTrace("installclean")
defer ctx.EndTrace()
prevConfig := strings.TrimPrefix(strings.TrimSuffix(string(prev), suffix), prefix)
currentConfig := strings.TrimPrefix(strings.TrimSuffix(currentProduct, suffix), prefix)
ctx.Printf("Build configuration changed: %q -> %q, forcing installclean\n", prevConfig, currentConfig)
installClean(ctx, config, 0)
writeConfig()
}