use of com.google.devtools.build.lib.packages.RuleClass in project bazel by bazelbuild.
the class BuildRuleWithDefaultsBuilder method populateLabelAttribute.
/**
* Populates the label type attribute with generated values. Populates with a file if possible, or
* generates an appropriate rule. Note, that the rules are always generated in the same package.
*/
public BuildRuleWithDefaultsBuilder populateLabelAttribute(String rulePkg, String filePkg, Attribute attribute) {
Type<?> attrType = attribute.getType();
String label = null;
if (attribute.getAllowedFileTypesPredicate() != FileTypeSet.NO_FILE) {
// Try to populate with files first
String extension = null;
if (attribute.getAllowedFileTypesPredicate() == FileTypeSet.ANY_FILE) {
extension = ".txt";
} else {
FileTypeSet fileTypes = attribute.getAllowedFileTypesPredicate();
// This argument should always hold, if not that means a Blaze design/implementation error
Preconditions.checkArgument(!fileTypes.getExtensions().isEmpty());
extension = fileTypes.getExtensions().get(0);
}
label = getDummyFileLabel(rulePkg, filePkg, extension, attrType);
} else {
Predicate<RuleClass> allowedRuleClasses = attribute.getAllowedRuleClassesPredicate();
if (allowedRuleClasses != Predicates.<RuleClass>alwaysFalse()) {
// See if there is an applicable rule among the already enqueued rules
BuildRuleBuilder referencedRuleBuilder = getFirstApplicableRule(allowedRuleClasses);
if (referencedRuleBuilder != null) {
label = ":" + referencedRuleBuilder.ruleName;
} else {
RuleClass referencedRuleClass = getFirstApplicableRuleClass(allowedRuleClasses);
if (referencedRuleClass != null) {
// Generate a rule with the appropriate ruleClass and a label for it in
// the original rule
label = ":" + getDummyRuleLabel(rulePkg, referencedRuleClass);
}
}
}
}
if (label != null) {
if (attrType instanceof ListType<?>) {
addMultiValueAttributes(attribute.getName(), label);
} else {
setSingleValueAttribute(attribute.getName(), label);
}
}
return this;
}
use of com.google.devtools.build.lib.packages.RuleClass in project bazel by bazelbuild.
the class BuildDocCollector method processAttributeDocs.
/**
* Go through all attributes of all documented rules and search the best attribute documentation
* if exists. The best documentation is the closest documentation in the ancestor graph. E.g. if
* java_library.deps documented in $rule and $java_rule then the one in $java_rule is going to
* apply since it's a closer ancestor of java_library.
*/
private void processAttributeDocs(Iterable<RuleDocumentation> ruleDocEntries, ListMultimap<String, RuleDocumentationAttribute> attributeDocEntries) throws BuildEncyclopediaDocException {
for (RuleDocumentation ruleDoc : ruleDocEntries) {
RuleClass ruleClass = ruleClassProvider.getRuleClassMap().get(ruleDoc.getRuleName());
if (ruleClass != null) {
if (ruleClass.isDocumented()) {
Class<? extends RuleDefinition> ruleDefinition = ruleClassProvider.getRuleClassDefinition(ruleDoc.getRuleName()).getClass();
for (Attribute attribute : ruleClass.getAttributes()) {
String attrName = attribute.getName();
List<RuleDocumentationAttribute> attributeDocList = attributeDocEntries.get(attrName);
if (attributeDocList != null) {
// There are attribute docs for this attribute.
// Search the closest one in the ancestor graph.
// Note that there can be only one 'closest' attribute since we forbid multiple
// inheritance of the same attribute in RuleClass.
int minLevel = Integer.MAX_VALUE;
RuleDocumentationAttribute bestAttributeDoc = null;
for (RuleDocumentationAttribute attributeDoc : attributeDocList) {
int level = attributeDoc.getDefinitionClassAncestryLevel(ruleDefinition, ruleClassProvider);
if (level >= 0 && level < minLevel) {
bestAttributeDoc = attributeDoc;
minLevel = level;
}
}
if (bestAttributeDoc != null) {
// Add reference to the Attribute that the attribute doc is associated with
// in order to generate documentation for the Attribute.
bestAttributeDoc.setAttribute(attribute);
ruleDoc.addAttribute(bestAttributeDoc);
// If there is no matching attribute doc try to add the common.
} else if (ruleDoc.getRuleType().equals(RuleType.BINARY) && PredefinedAttributes.BINARY_ATTRIBUTES.containsKey(attrName)) {
ruleDoc.addAttribute(PredefinedAttributes.BINARY_ATTRIBUTES.get(attrName));
} else if (ruleDoc.getRuleType().equals(RuleType.TEST) && PredefinedAttributes.TEST_ATTRIBUTES.containsKey(attrName)) {
ruleDoc.addAttribute(PredefinedAttributes.TEST_ATTRIBUTES.get(attrName));
} else if (PredefinedAttributes.COMMON_ATTRIBUTES.containsKey(attrName)) {
ruleDoc.addAttribute(PredefinedAttributes.COMMON_ATTRIBUTES.get(attrName));
}
}
}
}
} else {
throw ruleDoc.createException("Can't find RuleClass for " + ruleDoc.getRuleName());
}
}
}
use of com.google.devtools.build.lib.packages.RuleClass in project bazel by bazelbuild.
the class ListRuleMain method main.
public static void main(String[] args) throws Exception {
if (args.length == 0) {
System.err.println("Expected one input parameter, please provide the name of the rule class provider");
}
RuleClassProvider provider = createRuleClassProvider(args[0]);
Map<String, RuleClass> rcMap = provider.getRuleClassMap();
for (String name : rcMap.keySet()) {
System.out.println(name);
}
}
use of com.google.devtools.build.lib.packages.RuleClass in project bazel by bazelbuild.
the class CompileOneDependencyTransformer method transformCompileOneDependency.
private Target transformCompileOneDependency(ExtendedEventHandler eventHandler, Target target) throws TargetParsingException, InterruptedException {
if (!(target instanceof FileTarget)) {
throw new TargetParsingException("--compile_one_dependency target '" + target.getLabel() + "' must be a file");
}
Rule result = null;
Iterable<Rule> orderedRuleList = getOrderedRuleList(target.getPackage());
for (Rule rule : orderedRuleList) {
Set<Label> labels = getInputLabels(rule);
if (listContainsFile(eventHandler, labels, target.getLabel(), Sets.<Label>newHashSet())) {
if (rule.getRuleClassObject().isPreferredDependency(target.getName())) {
result = rule;
break;
}
if (result == null) {
result = rule;
}
}
}
if (result == null) {
throw new TargetParsingException("Couldn't find dependency on target '" + target.getLabel() + "'");
}
// If the rule has source targets, return it.
if (!RawAttributeMapper.of(result).getMergedValues("srcs", BuildType.LABEL_LIST).isEmpty()) {
return result;
}
// Try to find a rule in the same package that has 'result' as a dependency.
for (Rule rule : orderedRuleList) {
RawAttributeMapper attributes = RawAttributeMapper.of(rule);
// We don't know which path to follow for configurable attributes, so skip them.
if (attributes.isConfigurable("deps") || attributes.isConfigurable("srcs")) {
continue;
}
RuleClass ruleClass = rule.getRuleClassObject();
if (ruleClass.hasAttr("deps", BuildType.LABEL_LIST) && ruleClass.hasAttr("srcs", BuildType.LABEL_LIST)) {
for (Label dep : attributes.get("deps", BuildType.LABEL_LIST)) {
if (dep.equals(result.getLabel())) {
if (!attributes.get("srcs", BuildType.LABEL_LIST).isEmpty()) {
return rule;
}
}
}
}
}
return result;
}
use of com.google.devtools.build.lib.packages.RuleClass in project bazel by bazelbuild.
the class InfoItem method getBuildLanguageDefinition.
/**
* Returns a byte array containing a proto-buffer describing the build language.
*/
private static byte[] getBuildLanguageDefinition(RuleClassProvider provider) {
BuildLanguage.Builder resultPb = BuildLanguage.newBuilder();
Collection<RuleClass> ruleClasses = provider.getRuleClassMap().values();
for (RuleClass ruleClass : ruleClasses) {
if (!ruleClass.isDocumented()) {
continue;
}
RuleDefinition.Builder rulePb = RuleDefinition.newBuilder();
rulePb.setName(ruleClass.getName());
for (Attribute attr : ruleClass.getAttributes()) {
if (!attr.isDocumented()) {
continue;
}
AttributeDefinition.Builder attrPb = AttributeDefinition.newBuilder();
attrPb.setName(attr.getName());
// The protocol compiler, in its infinite wisdom, generates the field as one of the
// integer type and the getTypeEnum() method is missing. WTF?
attrPb.setType(ProtoUtils.getDiscriminatorFromType(attr.getType()));
attrPb.setMandatory(attr.isMandatory());
if (BuildType.isLabelType(attr.getType())) {
attrPb.setAllowedRuleClasses(getAllowedRuleClasses(ruleClasses, attr));
}
rulePb.addAttribute(attrPb);
}
resultPb.addRule(rulePb);
}
return resultPb.build().toByteArray();
}
Aggregations