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" |
| 20 | "strings" |
Lukacs T. Berki | a5e0f71 | 2020-05-18 09:50:18 +0200 | [diff] [blame] | 21 | "syscall" |
Dan Willemsen | 6606872 | 2017-05-08 21:15:59 +0000 | [diff] [blame] | 22 | |
Lukacs T. Berki | 3243aa5 | 2021-02-25 14:44:14 +0100 | [diff] [blame] | 23 | "android/soong/shared" |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | // This file supports dependencies on environment variables. During build manifest generation, |
| 27 | // any dependency on an environment variable is added to a list. During the singleton phase |
| 28 | // a JSON file is written containing the current value of all used environment variables. |
| 29 | // The next time the top-level build script is run, it uses the soong_env executable to |
| 30 | // compare the contents of the environment variables, rewriting the file if necessary to cause |
| 31 | // a manifest regeneration. |
| 32 | |
Dan Willemsen | 6606872 | 2017-05-08 21:15:59 +0000 | [diff] [blame] | 33 | var originalEnv map[string]string |
Lukacs T. Berki | a5e0f71 | 2020-05-18 09:50:18 +0200 | [diff] [blame] | 34 | var soongDelveListen string |
| 35 | var soongDelvePath string |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame^] | 36 | var isDebugging bool |
Dan Willemsen | 6606872 | 2017-05-08 21:15:59 +0000 | [diff] [blame] | 37 | |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame^] | 38 | func InitEnvironment(envFile string) { |
| 39 | var err error |
| 40 | originalEnv, err = shared.EnvFromFile(envFile) |
| 41 | if err != nil { |
| 42 | panic(err) |
Lukacs T. Berki | 848e00e | 2020-11-06 10:42:50 +0100 | [diff] [blame] | 43 | } |
Colin Cross | aa812d1 | 2019-06-19 13:33:24 -0700 | [diff] [blame] | 44 | |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame^] | 45 | soongDelveListen = originalEnv["SOONG_DELVE"] |
| 46 | soongDelvePath = originalEnv["SOONG_DELVE_PATH"] |
Dan Willemsen | 6606872 | 2017-05-08 21:15:59 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame^] | 49 | // Returns whether the current process is running under Delve due to |
| 50 | // ReexecWithDelveMaybe(). |
| 51 | func IsDebugging() bool { |
| 52 | return isDebugging |
| 53 | } |
Lukacs T. Berki | a5e0f71 | 2020-05-18 09:50:18 +0200 | [diff] [blame] | 54 | func ReexecWithDelveMaybe() { |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame^] | 55 | isDebugging = os.Getenv("SOONG_DELVE_REEXECUTED") == "true" |
| 56 | if isDebugging || soongDelveListen == "" { |
Lukacs T. Berki | a5e0f71 | 2020-05-18 09:50:18 +0200 | [diff] [blame] | 57 | return |
| 58 | } |
| 59 | |
| 60 | if soongDelvePath == "" { |
| 61 | fmt.Fprintln(os.Stderr, "SOONG_DELVE is set but failed to find dlv") |
| 62 | os.Exit(1) |
| 63 | } |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame^] | 64 | |
| 65 | soongDelveEnv := []string{} |
| 66 | for _, env := range os.Environ() { |
| 67 | idx := strings.IndexRune(env, '=') |
| 68 | if idx != -1 { |
| 69 | soongDelveEnv = append(soongDelveEnv, env) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | soongDelveEnv = append(soongDelveEnv, "SOONG_DELVE_REEXECUTED=true") |
| 74 | |
Lukacs T. Berki | a5e0f71 | 2020-05-18 09:50:18 +0200 | [diff] [blame] | 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 | 54855dd | 2017-11-28 23:55:23 -0800 | [diff] [blame] | 91 | func EnvSingleton() Singleton { |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 92 | return &envSingleton{} |
| 93 | } |
| 94 | |
| 95 | type envSingleton struct{} |
| 96 | |
Colin Cross | 54855dd | 2017-11-28 23:55:23 -0800 | [diff] [blame] | 97 | func (c *envSingleton) GenerateBuildActions(ctx SingletonContext) { |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 98 | envDeps := ctx.Config().EnvDeps() |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 99 | |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame^] | 100 | envFile := PathForOutput(ctx, "soong.environment.used") |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 101 | if ctx.Failed() { |
| 102 | return |
| 103 | } |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 104 | |
Lukacs T. Berki | 3243aa5 | 2021-02-25 14:44:14 +0100 | [diff] [blame] | 105 | data, err := shared.EnvFileContents(envDeps) |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 106 | if err != nil { |
| 107 | ctx.Errorf(err.Error()) |
| 108 | } |
| 109 | |
| 110 | err = WriteFileToOutputDir(envFile, data, 0666) |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 111 | if err != nil { |
| 112 | ctx.Errorf(err.Error()) |
| 113 | } |
| 114 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 115 | ctx.AddNinjaFileDeps(envFile.String()) |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 116 | } |