blob: 289d803abd471b910b4c42220f87587b8de6284d [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 (
Lukacs T. Berki3243aa52021-02-25 14:44:14 +010018 "android/soong/shared"
Colin Cross68f55102015-03-25 14:43:57 -070019)
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 Willemsen66068722017-05-08 21:15:59 +000028var originalEnv map[string]string
29
Lukacs T. Berki7690c092021-02-26 14:27:36 +010030func InitEnvironment(envFile string) {
31 var err error
32 originalEnv, err = shared.EnvFromFile(envFile)
33 if err != nil {
34 panic(err)
Lukacs T. Berki848e00e2020-11-06 10:42:50 +010035 }
Lukacs T. Berkia5e0f712020-05-18 09:50:18 +020036}
37
Colin Cross54855dd2017-11-28 23:55:23 -080038func EnvSingleton() Singleton {
Colin Cross68f55102015-03-25 14:43:57 -070039 return &envSingleton{}
40}
41
42type envSingleton struct{}
43
Colin Cross54855dd2017-11-28 23:55:23 -080044func (c *envSingleton) GenerateBuildActions(ctx SingletonContext) {
Colin Crossaabf6792017-11-29 00:27:14 -080045 envDeps := ctx.Config().EnvDeps()
Colin Cross68f55102015-03-25 14:43:57 -070046
Lukacs T. Berki7690c092021-02-26 14:27:36 +010047 envFile := PathForOutput(ctx, "soong.environment.used")
Dan Willemsen34cc69e2015-09-23 15:26:20 -070048 if ctx.Failed() {
49 return
50 }
Colin Cross68f55102015-03-25 14:43:57 -070051
Lukacs T. Berki3243aa52021-02-25 14:44:14 +010052 data, err := shared.EnvFileContents(envDeps)
Colin Cross988414c2020-01-11 01:11:46 +000053 if err != nil {
54 ctx.Errorf(err.Error())
55 }
56
57 err = WriteFileToOutputDir(envFile, data, 0666)
Colin Cross68f55102015-03-25 14:43:57 -070058 if err != nil {
59 ctx.Errorf(err.Error())
60 }
61
Dan Willemsen34cc69e2015-09-23 15:26:20 -070062 ctx.AddNinjaFileDeps(envFile.String())
Colin Cross68f55102015-03-25 14:43:57 -070063}