blob: d9f2db2f31d9f0df7a643e0d776ad1608c65be59 [file] [log] [blame]
Colin Cross68f55102015-03-25 14:43:57 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Colin Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross68f55102015-03-25 14:43:57 -070016
17import (
Dan Willemsen66068722017-05-08 21:15:59 +000018 "os"
Colin Crossaa812d12019-06-19 13:33:24 -070019 "os/exec"
Dan Willemsen66068722017-05-08 21:15:59 +000020 "strings"
21
Colin Cross68f55102015-03-25 14:43:57 -070022 "android/soong/env"
Colin Cross68f55102015-03-25 14:43:57 -070023)
24
25// This file supports dependencies on environment variables. During build manifest generation,
26// any dependency on an environment variable is added to a list. During the singleton phase
27// a JSON file is written containing the current value of all used environment variables.
28// The next time the top-level build script is run, it uses the soong_env executable to
29// compare the contents of the environment variables, rewriting the file if necessary to cause
30// a manifest regeneration.
31
Dan Willemsen66068722017-05-08 21:15:59 +000032var originalEnv map[string]string
Colin Crossaa812d12019-06-19 13:33:24 -070033var SoongDelveListen string
34var SoongDelvePath string
Dan Willemsen66068722017-05-08 21:15:59 +000035
36func init() {
Colin Crossaa812d12019-06-19 13:33:24 -070037 // Delve support needs to read this environment variable very early, before NewConfig has created a way to
38 // access originalEnv with dependencies. Store the value where soong_build can find it, it will manually
39 // ensure the dependencies are created.
40 SoongDelveListen = os.Getenv("SOONG_DELVE")
41 SoongDelvePath, _ = exec.LookPath("dlv")
42
Dan Willemsen66068722017-05-08 21:15:59 +000043 originalEnv = make(map[string]string)
44 for _, env := range os.Environ() {
45 idx := strings.IndexRune(env, '=')
46 if idx != -1 {
47 originalEnv[env[:idx]] = env[idx+1:]
48 }
49 }
Colin Crossaa812d12019-06-19 13:33:24 -070050 // Clear the environment to prevent use of os.Getenv(), which would not provide dependencies on environment
51 // variable values. The environment is available through ctx.Config().Getenv, ctx.Config().IsEnvTrue, etc.
Dan Willemsen66068722017-05-08 21:15:59 +000052 os.Clearenv()
53}
54
Colin Cross54855dd2017-11-28 23:55:23 -080055func EnvSingleton() Singleton {
Colin Cross68f55102015-03-25 14:43:57 -070056 return &envSingleton{}
57}
58
59type envSingleton struct{}
60
Colin Cross54855dd2017-11-28 23:55:23 -080061func (c *envSingleton) GenerateBuildActions(ctx SingletonContext) {
Colin Crossaabf6792017-11-29 00:27:14 -080062 envDeps := ctx.Config().EnvDeps()
Colin Cross68f55102015-03-25 14:43:57 -070063
Dan Willemsen34cc69e2015-09-23 15:26:20 -070064 envFile := PathForOutput(ctx, ".soong.environment")
65 if ctx.Failed() {
66 return
67 }
Colin Cross68f55102015-03-25 14:43:57 -070068
Dan Willemsen34cc69e2015-09-23 15:26:20 -070069 err := env.WriteEnvFile(envFile.String(), envDeps)
Colin Cross68f55102015-03-25 14:43:57 -070070 if err != nil {
71 ctx.Errorf(err.Error())
72 }
73
Dan Willemsen34cc69e2015-09-23 15:26:20 -070074 ctx.AddNinjaFileDeps(envFile.String())
Colin Cross68f55102015-03-25 14:43:57 -070075}