Home | History | Annotate | Download | only in android
      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 
     15 package android
     16 
     17 import (
     18 	"os"
     19 	"strings"
     20 
     21 	"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 
     33 var originalEnv map[string]string
     34 
     35 func 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 
     46 func EnvSingleton() blueprint.Singleton {
     47 	return &envSingleton{}
     48 }
     49 
     50 type envSingleton struct{}
     51 
     52 func (c *envSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
     53 	envDeps := ctx.Config().(Config).EnvDeps()
     54 
     55 	envFile := PathForOutput(ctx, ".soong.environment")
     56 	if ctx.Failed() {
     57 		return
     58 	}
     59 
     60 	err := env.WriteEnvFile(envFile.String(), envDeps)
     61 	if err != nil {
     62 		ctx.Errorf(err.Error())
     63 	}
     64 
     65 	ctx.AddNinjaFileDeps(envFile.String())
     66 }
     67