Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 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 com.google.devtools.build.lib.analysis; |
| 16 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 17 | import com.google.devtools.build.lib.analysis.MakeVariableExpander.ExpansionException; |
| 18 | import com.google.devtools.build.lib.analysis.config.BuildConfiguration; |
| 19 | import com.google.devtools.build.lib.packages.Package; |
Francois-Rene Rideau | c0a8c58 | 2016-01-28 18:36:22 +0000 | [diff] [blame^] | 20 | import com.google.devtools.build.lib.syntax.SkylarkDict; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 21 | |
Laurent Le Brun | e8ce847 | 2015-03-12 14:20:01 +0000 | [diff] [blame] | 22 | import java.util.LinkedHashMap; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 23 | import 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 | */ |
| 30 | public 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 Brun | e8ce847 | 2015-03-12 14:20:01 +0000 | [diff] [blame] | 58 | |
Francois-Rene Rideau | c0a8c58 | 2016-01-28 18:36:22 +0000 | [diff] [blame^] | 59 | public SkylarkDict<String, String> collectMakeVariables() { |
Laurent Le Brun | e8ce847 | 2015-03-12 14:20:01 +0000 | [diff] [blame] | 60 | 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 Rideau | c0a8c58 | 2016-01-28 18:36:22 +0000 | [diff] [blame^] | 66 | return SkylarkDict.<String, String>copyOf(null, map); |
Laurent Le Brun | e8ce847 | 2015-03-12 14:20:01 +0000 | [diff] [blame] | 67 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 68 | } |