From 3243aa51eb348fdc0729c00ac3b26485cbd26196 Mon Sep 17 00:00:00 2001 From: "Lukacs T. Berki" Date: Thu, 25 Feb 2021 14:44:14 +0100 Subject: [PATCH] Move environment staleness check to soong_ui. Also delete the now-unnecessary soong_env binary. Test: Manually. Also checked what happens when a used environment variable changes. Change-Id: Ib393e7f444e94819198c6cce4bcd8719d9fd9441 --- android/Android.bp | 1 - android/env.go | 4 +-- cmd/soong_build/Android.bp | 1 - cmd/soong_env/Android.bp | 28 ------------------- cmd/soong_env/soong_env.go | 57 -------------------------------------- env/Android.bp | 11 -------- shared/Android.bp | 1 + {env => shared}/env.go | 12 ++++---- ui/build/soong.go | 31 +++++---------------- 9 files changed, 16 insertions(+), 130 deletions(-) delete mode 100644 cmd/soong_env/Android.bp delete mode 100644 cmd/soong_env/soong_env.go delete mode 100644 env/Android.bp rename {env => shared}/env.go (89%) diff --git a/android/Android.bp b/android/Android.bp index a8fa53a41..35437b8bb 100644 --- a/android/Android.bp +++ b/android/Android.bp @@ -12,7 +12,6 @@ bootstrap_go_package { "soong", "soong-android-soongconfig", "soong-bazel", - "soong-env", "soong-shared", "soong-ui-metrics_proto", ], diff --git a/android/env.go b/android/env.go index c2a09aab8..a8c7777ad 100644 --- a/android/env.go +++ b/android/env.go @@ -21,7 +21,7 @@ import ( "strings" "syscall" - "android/soong/env" + "android/soong/shared" ) // This file supports dependencies on environment variables. During build manifest generation, @@ -113,7 +113,7 @@ func (c *envSingleton) GenerateBuildActions(ctx SingletonContext) { return } - data, err := env.EnvFileContents(envDeps) + data, err := shared.EnvFileContents(envDeps) if err != nil { ctx.Errorf(err.Error()) } diff --git a/cmd/soong_build/Android.bp b/cmd/soong_build/Android.bp index 6a0a87bc6..9f0922449 100644 --- a/cmd/soong_build/Android.bp +++ b/cmd/soong_build/Android.bp @@ -25,7 +25,6 @@ bootstrap_go_binary { "soong", "soong-android", "soong-bp2build", - "soong-env", "soong-ui-metrics_proto", ], srcs: [ diff --git a/cmd/soong_env/Android.bp b/cmd/soong_env/Android.bp deleted file mode 100644 index ad717d0b1..000000000 --- a/cmd/soong_env/Android.bp +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright 2015 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 { - default_applicable_licenses: ["Android-Apache-2.0"], -} - -bootstrap_go_binary { - name: "soong_env", - deps: [ - "soong-env", - ], - srcs: [ - "soong_env.go", - ], - default: true, -} diff --git a/cmd/soong_env/soong_env.go b/cmd/soong_env/soong_env.go deleted file mode 100644 index 8020b17a0..000000000 --- a/cmd/soong_env/soong_env.go +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright 2015 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. - -// soong_env determines if the given soong environment file (usually ".soong.environment") is stale -// by comparing its contents to the current corresponding environment variable values. -// It fails if the file cannot be opened or corrupted, or its contents differ from the current -// values. - -package main - -import ( - "flag" - "fmt" - "os" - - "android/soong/env" -) - -func usage() { - fmt.Fprintf(os.Stderr, "usage: soong_env env_file\n") - fmt.Fprintf(os.Stderr, "exits with success if the environment varibles in env_file match\n") - fmt.Fprintf(os.Stderr, "the current environment\n") - flag.PrintDefaults() - os.Exit(2) -} - -// This is a simple executable packaging, and the real work happens in env.StaleEnvFile. -func main() { - flag.Parse() - - if flag.NArg() != 1 { - usage() - } - - stale, err := env.StaleEnvFile(flag.Arg(0)) - if err != nil { - fmt.Fprintf(os.Stderr, "error: %s\n", err.Error()) - os.Exit(1) - } - - if stale { - os.Exit(1) - } - - os.Exit(0) -} diff --git a/env/Android.bp b/env/Android.bp deleted file mode 100644 index c6a97b1e6..000000000 --- a/env/Android.bp +++ /dev/null @@ -1,11 +0,0 @@ -package { - default_applicable_licenses: ["Android-Apache-2.0"], -} - -bootstrap_go_package { - name: "soong-env", - pkgPath: "android/soong/env", - srcs: [ - "env.go", - ], -} diff --git a/shared/Android.bp b/shared/Android.bp index 5aa9d54f7..c79bc2b39 100644 --- a/shared/Android.bp +++ b/shared/Android.bp @@ -6,6 +6,7 @@ bootstrap_go_package { name: "soong-shared", pkgPath: "android/soong/shared", srcs: [ + "env.go", "paths.go", ], deps: [ diff --git a/env/env.go b/shared/env.go similarity index 89% rename from env/env.go rename to shared/env.go index 735a38aa4..7900daa88 100644 --- a/env/env.go +++ b/shared/env.go @@ -12,15 +12,15 @@ // See the License for the specific language governing permissions and // limitations under the License. -// env implements the environment JSON file handling for the soong_env command line tool run before -// the builder and for the env writer in the builder. -package env +// Implements the environment JSON file handling for serializing the +// environment variables that were used in soong_build so that soong_ui can +// check whether they have changed +package shared import ( "encoding/json" "fmt" "io/ioutil" - "os" "sort" ) @@ -57,7 +57,7 @@ func EnvFileContents(envDeps map[string]string) ([]byte, error) { // Reads and deserializes a Soong environment file located at the given file path to determine its // staleness. If any environment variable values have changed, it prints them out and returns true. // Failing to read or parse the file also causes it to return true. -func StaleEnvFile(filepath string) (bool, error) { +func StaleEnvFile(filepath string, getenv func(string) string) (bool, error) { data, err := ioutil.ReadFile(filepath) if err != nil { return true, err @@ -74,7 +74,7 @@ func StaleEnvFile(filepath string) (bool, error) { for _, entry := range contents { key := entry.Key old := entry.Value - cur := os.Getenv(key) + cur := getenv(key) if old != cur { changed = append(changed, fmt.Sprintf("%s (%q -> %q)", key, old, cur)) } diff --git a/ui/build/soong.go b/ui/build/soong.go index 899ab5da5..125dbcc11 100644 --- a/ui/build/soong.go +++ b/ui/build/soong.go @@ -19,8 +19,8 @@ import ( "os" "path/filepath" "strconv" - "strings" + "android/soong/shared" soong_metrics_proto "android/soong/ui/metrics/metrics_proto" "github.com/golang/protobuf/proto" @@ -79,29 +79,12 @@ func runSoong(ctx Context, config Config) { defer ctx.EndTrace() envFile := filepath.Join(config.SoongOutDir(), ".soong.environment") - envTool := filepath.Join(config.SoongOutDir(), ".bootstrap/bin/soong_env") - if _, err := os.Stat(envFile); err == nil { - if _, err := os.Stat(envTool); err == nil { - cmd := Command(ctx, config, "soong_env", envTool, envFile) - cmd.Sandbox = soongSandbox - - var buf strings.Builder - cmd.Stdout = &buf - cmd.Stderr = &buf - if err := cmd.Run(); err != nil { - ctx.Verboseln("soong_env failed, forcing manifest regeneration") - os.Remove(envFile) - } - - if buf.Len() > 0 { - ctx.Verboseln(buf.String()) - } - } else { - ctx.Verboseln("Missing soong_env tool, forcing manifest regeneration") - os.Remove(envFile) - } - } else if !os.IsNotExist(err) { - ctx.Fatalf("Failed to stat %f: %v", envFile, err) + getenv := func(k string) string { + v, _ := config.Environment().Get(k) + return v + } + if stale, _ := shared.StaleEnvFile(envFile, getenv); stale { + os.Remove(envFile) } }()