blob: ec5794e369460dfb4abbeb2c60ead542be5f9d27 [file] [log] [blame]
Colin Cross68f55102015-03-25 14:43:57 -07001// 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 Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross68f55102015-03-25 14:43:57 -070016
17import (
Dan Willemsen66068722017-05-08 21:15:59 +000018 "os"
19 "strings"
20
Colin Cross68f55102015-03-25 14:43:57 -070021 "android/soong/env"
22
23 "github.com/google/blueprint"
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 Willemsen66068722017-05-08 21:15:59 +000033var originalEnv map[string]string
34
35func init() {
36 originalEnv = make(map[string]string)
37 for _, env := range os.Environ() {
38 idx := strings.IndexRune(env, '=')
39 if idx != -1 {
40 originalEnv[env[:idx]] = env[idx+1:]
41 }
42 }
43 os.Clearenv()
44}
45
Colin Cross68f55102015-03-25 14:43:57 -070046func EnvSingleton() blueprint.Singleton {
47 return &envSingleton{}
48}
49
50type envSingleton struct{}
51
52func (c *envSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
53 envDeps := ctx.Config().(Config).EnvDeps()
54
Dan Willemsen34cc69e2015-09-23 15:26:20 -070055 envFile := PathForOutput(ctx, ".soong.environment")
56 if ctx.Failed() {
57 return
58 }
Colin Cross68f55102015-03-25 14:43:57 -070059
Dan Willemsen34cc69e2015-09-23 15:26:20 -070060 err := env.WriteEnvFile(envFile.String(), envDeps)
Colin Cross68f55102015-03-25 14:43:57 -070061 if err != nil {
62 ctx.Errorf(err.Error())
63 }
64
Dan Willemsen34cc69e2015-09-23 15:26:20 -070065 ctx.AddNinjaFileDeps(envFile.String())
Colin Cross68f55102015-03-25 14:43:57 -070066}