blob: 5629c83a60d7a185c84f293fe5d3b770800cde62 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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
15package com.google.devtools.build.lib.analysis;
16
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010017import com.google.devtools.build.lib.analysis.MakeVariableExpander.ExpansionException;
18import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
19import com.google.devtools.build.lib.packages.Package;
Francois-Rene Rideauc0a8c582016-01-28 18:36:22 +000020import com.google.devtools.build.lib.syntax.SkylarkDict;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010021
Laurent Le Brune8ce8472015-03-12 14:20:01 +000022import java.util.LinkedHashMap;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010023import java.util.Map;
24
25/**
26 * Implements make variable expansion for make variables that depend on the
27 * configuration and the target (not on behavior of the
28 * {@link ConfiguredTarget} implementation)
29 */
30public class ConfigurationMakeVariableContext implements MakeVariableExpander.Context {
31 private final Package pkg;
32 private final Map<String, String> commandLineEnv;
33 private final Map<String, String> globalEnv;
34 private final String platform;
35
36 public ConfigurationMakeVariableContext(Package pkg, BuildConfiguration configuration) {
37 this.pkg = pkg;
38 commandLineEnv = configuration.getCommandLineDefines();
39 globalEnv = configuration.getGlobalMakeEnvironment();
40 platform = configuration.getPlatformName();
41 }
42
43 @Override
44 public String lookupMakeVariable(String var) throws ExpansionException {
45 String value = commandLineEnv.get(var);
46 if (value == null) {
47 value = pkg.lookupMakeVariable(var, platform);
48 }
49 if (value == null) {
50 value = globalEnv.get(var);
51 }
52 if (value == null) {
53 throw new MakeVariableExpander.ExpansionException("$(" + var + ") not defined");
54 }
55
56 return value;
57 }
Laurent Le Brune8ce8472015-03-12 14:20:01 +000058
Francois-Rene Rideauc0a8c582016-01-28 18:36:22 +000059 public SkylarkDict<String, String> collectMakeVariables() {
Laurent Le Brune8ce8472015-03-12 14:20:01 +000060 Map<String, String> map = new LinkedHashMap<>();
61 // Collect variables in the reverse order as in lookupMakeVariable
62 // because each update is overwriting.
63 map.putAll(pkg.getAllMakeVariables(platform));
64 map.putAll(globalEnv);
65 map.putAll(commandLineEnv);
Francois-Rene Rideauc0a8c582016-01-28 18:36:22 +000066 return SkylarkDict.<String, String>copyOf(null, map);
Laurent Le Brune8ce8472015-03-12 14:20:01 +000067 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010068}