// 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.config; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.cmdline.LabelSyntaxException; import com.google.devtools.build.lib.util.Preconditions; import java.util.Locale; import java.util.Map; import java.util.Set; /** * A helper class to compute and inject a defaults package into the package cache. * *
The //tools/defaults
package provides a mechanism let tool locations be
* specified over the commandline, without requiring any special support in the rule code.
* As such, it can be used in genrule $(location)
substitutions.
*
*
It works as follows: *
env.getHostPrerequisiteArtifact("$somelanguage_compiler")
.
*
* $somelanguage_compiler
is defined in the
* {@link RuleDefinition} subclass for that language.
*
* //tools/defaults:somelanguage_compiler
.
*
* //third_party/somelanguage:compiler
), and
* return the name in its implementation of {@link FragmentOptions#getDefaultsLabels}.
*
* //tools/defaults:somelanguage_compiler
.
*
* //tools/defaults
package is synthesized, using
* the values as specified on the command-line. The contents of
* tools/defaults/BUILD
is ignored.
*
* tools/defaults/BUILD
must exist, so we create a package in that
* location.
*
* For built-in rules (as opposed to genrules), late-bound labels provide an alternative
* method of depending on command-line values. These work by declaring attribute default values
* to be {@link LateBoundLabel} instances, whose getDefault(Rule rule, T
* configuration)
method will have access to {@link BuildConfiguration}, which in turn
* may depend on command line flag values.
*/
public final class DefaultsPackage {
// The template contents are broken into lines such that the resulting file has no more than 80
// characters per line.
private static final String HEADER = ""
+ "# DO NOT EDIT THIS FILE!\n"
+ "#\n"
+ "# Bazel does not read this file. Instead, it internally replaces the targets in\n"
+ "# this package with the correct packages as given on the command line.\n"
+ "#\n"
+ "# If these options are not given on the command line, Bazel will use the exact\n"
+ "# same targets as given here."
+ "\n"
+ "package(default_visibility = ['//visibility:public'])\n";
/**
* The map from entries to their values.
*/
private ImmutableMap