Add and update comments in env and android_env.go

Bug: 173450116
Test: m nothing
Change-Id: I582a483d11c2ca23fd6e9597e49cdfb7e69152de
This commit is contained in:
Jaewoong Jung 2020-11-16 16:01:27 -08:00
parent af335a4694
commit f2200adccb
2 changed files with 23 additions and 6 deletions

View File

@ -12,10 +12,11 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// soong_glob is the command line tool that checks if the list of files matching a glob has // soong_env determines if the given soong environment file (usually ".soong.environment") is stale
// changed, and only updates the output file list if it has changed. It is used to optimize // by comparing its contents to the current corresponding environment variable values.
// out build.ninja regenerations when non-matching files are added. See // It fails if the file cannot be opened or corrupted, or its contents differ from the current
// android/soong/android/glob.go for a longer description. // values.
package main package main
import ( import (
@ -34,6 +35,7 @@ func usage() {
os.Exit(2) os.Exit(2)
} }
// This is a simple executable packaging, and the real work happens in env.StaleEnvFile.
func main() { func main() {
flag.Parse() flag.Parse()

19
env/env.go vendored
View File

@ -27,6 +27,15 @@ import (
type envFileEntry struct{ Key, Value string } type envFileEntry struct{ Key, Value string }
type envFileData []envFileEntry type envFileData []envFileEntry
// Serializes the given environment variable name/value map into JSON formatted bytes by converting
// to envFileEntry values and marshaling them.
//
// e.g. OUT_DIR = "out"
// is converted to:
// {
// "Key": "OUT_DIR",
// "Value": "out",
// },
func EnvFileContents(envDeps map[string]string) ([]byte, error) { func EnvFileContents(envDeps map[string]string) ([]byte, error) {
contents := make(envFileData, 0, len(envDeps)) contents := make(envFileData, 0, len(envDeps))
for key, value := range envDeps { for key, value := range envDeps {
@ -45,8 +54,11 @@ func EnvFileContents(envDeps map[string]string) ([]byte, error) {
return data, nil return data, nil
} }
func StaleEnvFile(filename string) (bool, error) { // Reads and deserializes a Soong environment file located at the given file path to determine its
data, err := ioutil.ReadFile(filename) // 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) {
data, err := ioutil.ReadFile(filepath)
if err != nil { if err != nil {
return true, err return true, err
} }
@ -79,6 +91,7 @@ func StaleEnvFile(filename string) (bool, error) {
return false, nil return false, nil
} }
// Implements sort.Interface so that we can use sort.Sort on envFileData arrays.
func (e envFileData) Len() int { func (e envFileData) Len() int {
return len(e) return len(e)
} }
@ -90,3 +103,5 @@ func (e envFileData) Less(i, j int) bool {
func (e envFileData) Swap(i, j int) { func (e envFileData) Swap(i, j int) {
e[i], e[j] = e[j], e[i] e[i], e[j] = e[j], e[i]
} }
var _ sort.Interface = envFileData{}