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 ( |
Lukacs T. Berki | a5e0f71 | 2020-05-18 09:50:18 +0200 | [diff] [blame] | 18 | "fmt" |
Dan Willemsen | 6606872 | 2017-05-08 21:15:59 +0000 | [diff] [blame] | 19 | "os" |
Colin Cross | aa812d1 | 2019-06-19 13:33:24 -0700 | [diff] [blame] | 20 | "os/exec" |
Dan Willemsen | 6606872 | 2017-05-08 21:15:59 +0000 | [diff] [blame] | 21 | "strings" |
Lukacs T. Berki | a5e0f71 | 2020-05-18 09:50:18 +0200 | [diff] [blame] | 22 | "syscall" |
Dan Willemsen | 6606872 | 2017-05-08 21:15:59 +0000 | [diff] [blame] | 23 | |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 24 | "android/soong/env" |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | // This file supports dependencies on environment variables. During build manifest generation, |
| 28 | // any dependency on an environment variable is added to a list. During the singleton phase |
| 29 | // a JSON file is written containing the current value of all used environment variables. |
| 30 | // The next time the top-level build script is run, it uses the soong_env executable to |
| 31 | // compare the contents of the environment variables, rewriting the file if necessary to cause |
| 32 | // a manifest regeneration. |
| 33 | |
Dan Willemsen | 6606872 | 2017-05-08 21:15:59 +0000 | [diff] [blame] | 34 | var originalEnv map[string]string |
Lukacs T. Berki | a5e0f71 | 2020-05-18 09:50:18 +0200 | [diff] [blame] | 35 | var soongDelveListen string |
| 36 | var soongDelvePath string |
| 37 | var soongDelveEnv []string |
Dan Willemsen | 6606872 | 2017-05-08 21:15:59 +0000 | [diff] [blame] | 38 | |
| 39 | func init() { |
Colin Cross | aa812d1 | 2019-06-19 13:33:24 -0700 | [diff] [blame] | 40 | // Delve support needs to read this environment variable very early, before NewConfig has created a way to |
| 41 | // access originalEnv with dependencies. Store the value where soong_build can find it, it will manually |
| 42 | // ensure the dependencies are created. |
Lukacs T. Berki | a5e0f71 | 2020-05-18 09:50:18 +0200 | [diff] [blame] | 43 | soongDelveListen = os.Getenv("SOONG_DELVE") |
Lukacs T. Berki | 848e00e | 2020-11-06 10:42:50 +0100 | [diff] [blame] | 44 | soongDelvePath = os.Getenv("SOONG_DELVE_PATH") |
| 45 | if soongDelvePath == "" { |
| 46 | soongDelvePath, _ = exec.LookPath("dlv") |
| 47 | } |
Colin Cross | aa812d1 | 2019-06-19 13:33:24 -0700 | [diff] [blame] | 48 | |
Dan Willemsen | 6606872 | 2017-05-08 21:15:59 +0000 | [diff] [blame] | 49 | originalEnv = make(map[string]string) |
Lukacs T. Berki | a5e0f71 | 2020-05-18 09:50:18 +0200 | [diff] [blame] | 50 | soongDelveEnv = []string{} |
Dan Willemsen | 6606872 | 2017-05-08 21:15:59 +0000 | [diff] [blame] | 51 | for _, env := range os.Environ() { |
| 52 | idx := strings.IndexRune(env, '=') |
| 53 | if idx != -1 { |
| 54 | originalEnv[env[:idx]] = env[idx+1:] |
Lukacs T. Berki | 848e00e | 2020-11-06 10:42:50 +0100 | [diff] [blame] | 55 | if env[:idx] != "SOONG_DELVE" && env[:idx] != "SOONG_DELVE_PATH" { |
Lukacs T. Berki | a5e0f71 | 2020-05-18 09:50:18 +0200 | [diff] [blame] | 56 | soongDelveEnv = append(soongDelveEnv, env) |
| 57 | } |
Dan Willemsen | 6606872 | 2017-05-08 21:15:59 +0000 | [diff] [blame] | 58 | } |
| 59 | } |
Lukacs T. Berki | a5e0f71 | 2020-05-18 09:50:18 +0200 | [diff] [blame] | 60 | |
Colin Cross | aa812d1 | 2019-06-19 13:33:24 -0700 | [diff] [blame] | 61 | // Clear the environment to prevent use of os.Getenv(), which would not provide dependencies on environment |
| 62 | // 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] | 63 | os.Clearenv() |
| 64 | } |
| 65 | |
Lukacs T. Berki | a5e0f71 | 2020-05-18 09:50:18 +0200 | [diff] [blame] | 66 | func ReexecWithDelveMaybe() { |
| 67 | if soongDelveListen == "" { |
| 68 | return |
| 69 | } |
| 70 | |
| 71 | if soongDelvePath == "" { |
| 72 | fmt.Fprintln(os.Stderr, "SOONG_DELVE is set but failed to find dlv") |
| 73 | os.Exit(1) |
| 74 | } |
| 75 | dlvArgv := []string{ |
| 76 | soongDelvePath, |
| 77 | "--listen=:" + soongDelveListen, |
| 78 | "--headless=true", |
| 79 | "--api-version=2", |
| 80 | "exec", |
| 81 | os.Args[0], |
| 82 | "--", |
| 83 | } |
| 84 | dlvArgv = append(dlvArgv, os.Args[1:]...) |
| 85 | os.Chdir(absSrcDir) |
| 86 | syscall.Exec(soongDelvePath, dlvArgv, soongDelveEnv) |
| 87 | fmt.Fprintln(os.Stderr, "exec() failed while trying to reexec with Delve") |
| 88 | os.Exit(1) |
| 89 | } |
| 90 | |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 91 | // getenv checks either os.Getenv or originalEnv so that it works before or after the init() |
| 92 | // function above. It doesn't add any dependencies on the environment variable, so it should |
| 93 | // only be used for values that won't change. For values that might change use ctx.Config().Getenv. |
| 94 | func getenv(key string) string { |
| 95 | if originalEnv == nil { |
| 96 | return os.Getenv(key) |
| 97 | } else { |
| 98 | return originalEnv[key] |
| 99 | } |
| 100 | } |
| 101 | |
Colin Cross | 54855dd | 2017-11-28 23:55:23 -0800 | [diff] [blame] | 102 | func EnvSingleton() Singleton { |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 103 | return &envSingleton{} |
| 104 | } |
| 105 | |
| 106 | type envSingleton struct{} |
| 107 | |
Colin Cross | 54855dd | 2017-11-28 23:55:23 -0800 | [diff] [blame] | 108 | func (c *envSingleton) GenerateBuildActions(ctx SingletonContext) { |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 109 | envDeps := ctx.Config().EnvDeps() |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 110 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 111 | envFile := PathForOutput(ctx, ".soong.environment") |
| 112 | if ctx.Failed() { |
| 113 | return |
| 114 | } |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 115 | |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 116 | data, err := env.EnvFileContents(envDeps) |
| 117 | if err != nil { |
| 118 | ctx.Errorf(err.Error()) |
| 119 | } |
| 120 | |
| 121 | err = WriteFileToOutputDir(envFile, data, 0666) |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 122 | if err != nil { |
| 123 | ctx.Errorf(err.Error()) |
| 124 | } |
| 125 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 126 | ctx.AddNinjaFileDeps(envFile.String()) |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 127 | } |