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
This commit is contained in:
Lukacs T. Berki 2021-02-25 14:44:14 +01:00
parent f3e0d22234
commit 3243aa51eb
9 changed files with 16 additions and 130 deletions

View File

@ -12,7 +12,6 @@ bootstrap_go_package {
"soong",
"soong-android-soongconfig",
"soong-bazel",
"soong-env",
"soong-shared",
"soong-ui-metrics_proto",
],

View File

@ -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())
}

View File

@ -25,7 +25,6 @@ bootstrap_go_binary {
"soong",
"soong-android",
"soong-bp2build",
"soong-env",
"soong-ui-metrics_proto",
],
srcs: [

View File

@ -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,
}

View File

@ -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)
}

11
env/Android.bp vendored
View File

@ -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",
],
}

View File

@ -6,6 +6,7 @@ bootstrap_go_package {
name: "soong-shared",
pkgPath: "android/soong/shared",
srcs: [
"env.go",
"paths.go",
],
deps: [

View File

@ -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))
}

View File

@ -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)
}
}()