// Copyright 2014 Google Inc. 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.rules;
import static com.google.devtools.build.lib.syntax.SkylarkType.castList;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.packages.Attribute;
import com.google.devtools.build.lib.packages.Attribute.AllowedValueSet;
import com.google.devtools.build.lib.packages.Attribute.ConfigurationTransition;
import com.google.devtools.build.lib.packages.Attribute.SkylarkLateBound;
import com.google.devtools.build.lib.packages.Type;
import com.google.devtools.build.lib.packages.Type.ConversionException;
import com.google.devtools.build.lib.syntax.BuiltinFunction;
import com.google.devtools.build.lib.syntax.Environment;
import com.google.devtools.build.lib.syntax.EvalException;
import com.google.devtools.build.lib.syntax.EvalUtils;
import com.google.devtools.build.lib.syntax.FuncallExpression;
import com.google.devtools.build.lib.syntax.Label;
import com.google.devtools.build.lib.syntax.SkylarkCallbackFunction;
import com.google.devtools.build.lib.syntax.SkylarkEnvironment;
import com.google.devtools.build.lib.syntax.SkylarkList;
import com.google.devtools.build.lib.syntax.SkylarkModule;
import com.google.devtools.build.lib.syntax.SkylarkSignature;
import com.google.devtools.build.lib.syntax.SkylarkSignature.Param;
import com.google.devtools.build.lib.syntax.SkylarkSignatureProcessor;
import com.google.devtools.build.lib.syntax.UserDefinedFunction;
import com.google.devtools.build.lib.util.FileTypeSet;
import java.util.Map;
/**
* A helper class to provide Attr module in Skylark.
*/
@SkylarkModule(
name = "attr",
namespace = true,
onlyLoadingPhase = true,
doc =
"Module for creating new attributes. "
+ "They are only for use with the rule function.")
public final class SkylarkAttr {
private static final String MANDATORY_DOC =
"set to True if users have to explicitely specify the value";
private static final String NON_EMPTY_DOC = "set to True if the attribute must not be empty";
private static final String ALLOW_FILES_DOC =
"whether File targets are allowed. Can be True, False (default), or " + "a FileType filter.";
private static final String ALLOW_RULES_DOC =
"which rule targets (name of the classes) are allowed. This is deprecated (kept only for "
+ "compatiblity), use providers instead.";
private static final String FLAGS_DOC = "deprecated, will be removed";
private static final String DEFAULT_DOC = "sets the default value of the attribute.";
private static final String CONFIGURATION_DOC =
"configuration of the attribute. " + "For example, use DATA_CFG or HOST_CFG.";
private static final String EXECUTABLE_DOC =
"set to True if the labels have to be executable. This means the label must refer to an "
+ "executable file, or to a rule that outputs an executable file. Access the labels "
+ "with ctx.executable.<attribute_name>.";
private static final String VALUES_DOC =
"specify the list of allowed values for the attribute. An error is raised if any other "
+ "value is given.";
private static boolean containsNonNoneKey(Map arguments, String key) {
return arguments.containsKey(key) && arguments.get(key) != Environment.NONE;
}
private static Attribute.Builder> createAttribute(Type> type, Map arguments,
FuncallExpression ast, SkylarkEnvironment env) throws EvalException, ConversionException {
// We use an empty name now so that we can set it later.
// This trick makes sense only in the context of Skylark (builtin rules should not use it).
Attribute.Builder> builder = Attribute.attr("", type);
Object defaultValue = arguments.get("default");
if (!EvalUtils.isNullOrNone(defaultValue)) {
if (defaultValue instanceof UserDefinedFunction) {
// Late bound attribute. Non label type attributes already caused a type check error.
builder.value(new SkylarkLateBound(
new SkylarkCallbackFunction((UserDefinedFunction) defaultValue, ast, env)));
} else {
builder.defaultValue(defaultValue);
}
}
for (String flag : castList(arguments.get("flags"), String.class)) {
builder.setPropertyFlag(flag);
}
if (containsNonNoneKey(arguments, "mandatory") && (Boolean) arguments.get("mandatory")) {
builder.setPropertyFlag("MANDATORY");
}
if (containsNonNoneKey(arguments, "non_empty") && (Boolean) arguments.get("non_empty")) {
builder.setPropertyFlag("NON_EMPTY");
}
if (containsNonNoneKey(arguments, "executable") && (Boolean) arguments.get("executable")) {
builder.setPropertyFlag("EXECUTABLE");
}
if (containsNonNoneKey(arguments, "single_file") && (Boolean) arguments.get("single_file")) {
builder.setPropertyFlag("SINGLE_ARTIFACT");
}
if (containsNonNoneKey(arguments, "allow_files")) {
Object fileTypesObj = arguments.get("allow_files");
if (fileTypesObj == Boolean.TRUE) {
builder.allowedFileTypes(FileTypeSet.ANY_FILE);
} else if (fileTypesObj == Boolean.FALSE) {
builder.allowedFileTypes(FileTypeSet.NO_FILE);
} else if (fileTypesObj instanceof SkylarkFileType) {
builder.allowedFileTypes(((SkylarkFileType) fileTypesObj).getFileTypeSet());
} else {
throw new EvalException(ast.getLocation(),
"allow_files should be a boolean or a filetype object.");
}
} else if (type.equals(Type.LABEL) || type.equals(Type.LABEL_LIST)) {
builder.allowedFileTypes(FileTypeSet.NO_FILE);
}
Object ruleClassesObj = arguments.get("allow_rules");
if (ruleClassesObj != null && ruleClassesObj != Environment.NONE) {
builder.allowedRuleClasses(castList(ruleClassesObj, String.class,
"allowed rule classes for attribute definition"));
}
Iterable