// Copyright 2014 The Bazel Authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package com.google.devtools.build.lib.analysis; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.devtools.build.lib.analysis.MakeVariableExpander.ExpansionException; import com.google.devtools.build.lib.analysis.MakeVariableSupplier.MapBackedMakeVariableSupplier; import com.google.devtools.build.lib.analysis.MakeVariableSupplier.PackageBackedMakeVariableSupplier; import com.google.devtools.build.lib.analysis.config.BuildConfiguration; import com.google.devtools.build.lib.packages.Package; import com.google.devtools.build.lib.syntax.SkylarkDict; import com.google.devtools.build.lib.util.Preconditions; import java.util.LinkedHashMap; import java.util.Map; /** * Implements make variable expansion for make variables that depend on the configuration and the * target (not on behavior of the {@link ConfiguredTarget} implementation). Retrieved Make variable * value can be modified using {@link MakeVariableSupplier} */ public class ConfigurationMakeVariableContext implements MakeVariableExpander.Context { private final ImmutableList allMakeVariableSuppliers; // TODO(b/37567440): Remove when Skylark callers can be updated to get this from // CcToolchainProvider. We should use CcCommon.CC_TOOLCHAIN_ATTRIBUTE_NAME, but we didn't want to // pollute core with C++ specific constant. private static final ImmutableList defaultMakeVariableAttributes = ImmutableList.of(":cc_toolchain"); public ConfigurationMakeVariableContext( RuleContext ruleContext, Package pkg, BuildConfiguration configuration) { this( ruleContext.getMakeVariables(defaultMakeVariableAttributes), pkg, configuration, ImmutableList.of()); } public ConfigurationMakeVariableContext( ImmutableMap ruleMakeVariables, Package pkg, BuildConfiguration configuration) { this(ruleMakeVariables, pkg, configuration, ImmutableList.of()); } public ConfigurationMakeVariableContext( RuleContext ruleContext, Package pkg, BuildConfiguration configuration, Iterable makeVariableSuppliers) { this( ruleContext.getMakeVariables(defaultMakeVariableAttributes), pkg, configuration, makeVariableSuppliers); } public ConfigurationMakeVariableContext( ImmutableMap ruleMakeVariables, Package pkg, BuildConfiguration configuration, Iterable extraMakeVariableSuppliers) { this.allMakeVariableSuppliers = ImmutableList.builder() .addAll(Preconditions.checkNotNull(extraMakeVariableSuppliers)) .add(new MapBackedMakeVariableSupplier(ruleMakeVariables)) .add(new MapBackedMakeVariableSupplier(configuration.getCommandLineBuildVariables())) .add(new PackageBackedMakeVariableSupplier(pkg, configuration.getPlatformName())) .add(new MapBackedMakeVariableSupplier(configuration.getGlobalMakeEnvironment())) .build(); } @Override public String lookupMakeVariable(String variableName) throws ExpansionException { for (MakeVariableSupplier supplier : allMakeVariableSuppliers) { String variableValue = supplier.getMakeVariable(variableName); if (variableValue != null) { return variableValue; } } throw new MakeVariableExpander.ExpansionException("$(" + variableName + ") not defined"); } public SkylarkDict collectMakeVariables() { Map map = new LinkedHashMap<>(); // Collect variables in the reverse order as in lookupMakeVariable // because each update is overwriting. for (MakeVariableSupplier supplier : allMakeVariableSuppliers.reverse()) { map.putAll(supplier.getAllMakeVariables()); } return SkylarkDict.copyOf(null, map); } }