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 | 3243aa5 | 2021-02-25 14:44:14 +0100 | [diff] [blame] | 18 | "android/soong/shared" |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 19 | ) |
| 20 | |
| 21 | // This file supports dependencies on environment variables. During build manifest generation, |
| 22 | // any dependency on an environment variable is added to a list. During the singleton phase |
| 23 | // a JSON file is written containing the current value of all used environment variables. |
| 24 | // The next time the top-level build script is run, it uses the soong_env executable to |
| 25 | // compare the contents of the environment variables, rewriting the file if necessary to cause |
| 26 | // a manifest regeneration. |
| 27 | |
Dan Willemsen | 6606872 | 2017-05-08 21:15:59 +0000 | [diff] [blame] | 28 | var originalEnv map[string]string |
| 29 | |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 30 | func InitEnvironment(envFile string) { |
| 31 | var err error |
| 32 | originalEnv, err = shared.EnvFromFile(envFile) |
| 33 | if err != nil { |
| 34 | panic(err) |
Lukacs T. Berki | 848e00e | 2020-11-06 10:42:50 +0100 | [diff] [blame] | 35 | } |
Lukacs T. Berki | a5e0f71 | 2020-05-18 09:50:18 +0200 | [diff] [blame] | 36 | } |
| 37 | |
Colin Cross | 54855dd | 2017-11-28 23:55:23 -0800 | [diff] [blame] | 38 | func EnvSingleton() Singleton { |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 39 | return &envSingleton{} |
| 40 | } |
| 41 | |
| 42 | type envSingleton struct{} |
| 43 | |
Colin Cross | 54855dd | 2017-11-28 23:55:23 -0800 | [diff] [blame] | 44 | func (c *envSingleton) GenerateBuildActions(ctx SingletonContext) { |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 45 | envDeps := ctx.Config().EnvDeps() |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 46 | |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 47 | envFile := PathForOutput(ctx, "soong.environment.used") |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 48 | if ctx.Failed() { |
| 49 | return |
| 50 | } |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 51 | |
Lukacs T. Berki | 3243aa5 | 2021-02-25 14:44:14 +0100 | [diff] [blame] | 52 | data, err := shared.EnvFileContents(envDeps) |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 53 | if err != nil { |
| 54 | ctx.Errorf(err.Error()) |
| 55 | } |
| 56 | |
| 57 | err = WriteFileToOutputDir(envFile, data, 0666) |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 58 | if err != nil { |
| 59 | ctx.Errorf(err.Error()) |
| 60 | } |
| 61 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 62 | ctx.AddNinjaFileDeps(envFile.String()) |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 63 | } |