| 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" | 
 | 19 | 	"strings" | 
 | 20 |  | 
| Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 21 | 	"android/soong/env" | 
| Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 22 | ) | 
 | 23 |  | 
 | 24 | // This file supports dependencies on environment variables.  During build manifest generation, | 
 | 25 | // any dependency on an environment variable is added to a list.  During the singleton phase | 
 | 26 | // a JSON file is written containing the current value of all used environment variables. | 
 | 27 | // The next time the top-level build script is run, it uses the soong_env executable to | 
 | 28 | // compare the contents of the environment variables, rewriting the file if necessary to cause | 
 | 29 | // a manifest regeneration. | 
 | 30 |  | 
| Dan Willemsen | 6606872 | 2017-05-08 21:15:59 +0000 | [diff] [blame] | 31 | var originalEnv map[string]string | 
 | 32 |  | 
 | 33 | func init() { | 
 | 34 | 	originalEnv = make(map[string]string) | 
 | 35 | 	for _, env := range os.Environ() { | 
 | 36 | 		idx := strings.IndexRune(env, '=') | 
 | 37 | 		if idx != -1 { | 
 | 38 | 			originalEnv[env[:idx]] = env[idx+1:] | 
 | 39 | 		} | 
 | 40 | 	} | 
 | 41 | 	os.Clearenv() | 
 | 42 | } | 
 | 43 |  | 
| Colin Cross | 54855dd | 2017-11-28 23:55:23 -0800 | [diff] [blame] | 44 | func EnvSingleton() Singleton { | 
| Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 45 | 	return &envSingleton{} | 
 | 46 | } | 
 | 47 |  | 
 | 48 | type envSingleton struct{} | 
 | 49 |  | 
| Colin Cross | 54855dd | 2017-11-28 23:55:23 -0800 | [diff] [blame] | 50 | func (c *envSingleton) GenerateBuildActions(ctx SingletonContext) { | 
| Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 51 | 	envDeps := ctx.Config().EnvDeps() | 
| Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 52 |  | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 53 | 	envFile := PathForOutput(ctx, ".soong.environment") | 
 | 54 | 	if ctx.Failed() { | 
 | 55 | 		return | 
 | 56 | 	} | 
| Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 57 |  | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 58 | 	err := env.WriteEnvFile(envFile.String(), envDeps) | 
| Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 59 | 	if err != nil { | 
 | 60 | 		ctx.Errorf(err.Error()) | 
 | 61 | 	} | 
 | 62 |  | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 63 | 	ctx.AddNinjaFileDeps(envFile.String()) | 
| Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 64 | } |