write soong.environment.used.* file only if changed

Bug: b/255309129
Test: `lunch aosp_x86-userdebug` and `export NINJA_ARGS="-d explain"`
      Then do the following:
        1) rm -rf out && m bp2build
        2) touch bionic/libc && m bp2build
        3) ALLOW_MISSING_DEPENDENCIES=true m bp2build
      After each step above observe the following two
        a)`stat out/soong/soong.environment.used.bp2build | grep Modify`
        b)`stat out/soong/bp2build_workspace_marker | grep Modify`
      Verified:
       step 2 changes only (b) - prior to this CL, it'd change (a) as well
       step 3 still changes both (as expected)
Change-Id: I98a94878a14e19043f448b0904fc67d5d1dc9733
This commit is contained in:
Usta Shrestha
2022-10-24 11:33:09 -04:00
committed by Usta (Tsering) Shrestha
parent 706d35fe7c
commit 2ba28a34bd

View File

@@ -15,6 +15,7 @@
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
@@ -478,12 +479,19 @@ func writeUsedEnvironmentFile(configuration android.Config, finalOutputFile stri
os.Exit(1)
}
err = ioutil.WriteFile(path, data, 0666)
if err != nil {
if preexistingData, err := os.ReadFile(path); err != nil {
if !os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "error reading used environment file '%s': %s\n", usedEnvFile, err)
os.Exit(1)
}
} else if bytes.Equal(preexistingData, data) {
// used environment file is unchanged
return
}
if err = os.WriteFile(path, data, 0666); err != nil {
fmt.Fprintf(os.Stderr, "error writing used environment file '%s': %s\n", usedEnvFile, err)
os.Exit(1)
}
// Touch the output file so that it's not older than the file we just
// wrote. We can't write the environment file earlier because one an access
// new environment variables while writing it.