| Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 1 | // 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 Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 15 | package android | 
| Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 16 |  | 
|  | 17 | import ( | 
| Dan Willemsen | 6606872 | 2017-05-08 21:15:59 +0000 | [diff] [blame] | 18 | "os" | 
| Colin Cross | aa812d1 | 2019-06-19 13:33:24 -0700 | [diff] [blame] | 19 | "os/exec" | 
| Dan Willemsen | 6606872 | 2017-05-08 21:15:59 +0000 | [diff] [blame] | 20 | "strings" | 
|  | 21 |  | 
| Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 22 | "android/soong/env" | 
| Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 23 | ) | 
|  | 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 Willemsen | 6606872 | 2017-05-08 21:15:59 +0000 | [diff] [blame] | 32 | var originalEnv map[string]string | 
| Colin Cross | aa812d1 | 2019-06-19 13:33:24 -0700 | [diff] [blame] | 33 | var SoongDelveListen string | 
|  | 34 | var SoongDelvePath string | 
| Dan Willemsen | 6606872 | 2017-05-08 21:15:59 +0000 | [diff] [blame] | 35 |  | 
|  | 36 | func init() { | 
| Colin Cross | aa812d1 | 2019-06-19 13:33:24 -0700 | [diff] [blame] | 37 | // 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 Willemsen | 6606872 | 2017-05-08 21:15:59 +0000 | [diff] [blame] | 43 | 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 Cross | aa812d1 | 2019-06-19 13:33:24 -0700 | [diff] [blame] | 50 | // 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 Willemsen | 6606872 | 2017-05-08 21:15:59 +0000 | [diff] [blame] | 52 | os.Clearenv() | 
|  | 53 | } | 
|  | 54 |  | 
| Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 55 | // getenv checks either os.Getenv or originalEnv so that it works before or after the init() | 
|  | 56 | // function above.  It doesn't add any dependencies on the environment variable, so it should | 
|  | 57 | // only be used for values that won't change.  For values that might change use ctx.Config().Getenv. | 
|  | 58 | func getenv(key string) string { | 
|  | 59 | if originalEnv == nil { | 
|  | 60 | return os.Getenv(key) | 
|  | 61 | } else { | 
|  | 62 | return originalEnv[key] | 
|  | 63 | } | 
|  | 64 | } | 
|  | 65 |  | 
| Colin Cross | 54855dd | 2017-11-28 23:55:23 -0800 | [diff] [blame] | 66 | func EnvSingleton() Singleton { | 
| Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 67 | return &envSingleton{} | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | type envSingleton struct{} | 
|  | 71 |  | 
| Colin Cross | 54855dd | 2017-11-28 23:55:23 -0800 | [diff] [blame] | 72 | func (c *envSingleton) GenerateBuildActions(ctx SingletonContext) { | 
| Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 73 | envDeps := ctx.Config().EnvDeps() | 
| Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 74 |  | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 75 | envFile := PathForOutput(ctx, ".soong.environment") | 
|  | 76 | if ctx.Failed() { | 
|  | 77 | return | 
|  | 78 | } | 
| Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 79 |  | 
| Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 80 | data, err := env.EnvFileContents(envDeps) | 
|  | 81 | if err != nil { | 
|  | 82 | ctx.Errorf(err.Error()) | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | err = WriteFileToOutputDir(envFile, data, 0666) | 
| Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 86 | if err != nil { | 
|  | 87 | ctx.Errorf(err.Error()) | 
|  | 88 | } | 
|  | 89 |  | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 90 | ctx.AddNinjaFileDeps(envFile.String()) | 
| Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 91 | } |