/** * Adds multiple compilation prerequisites. * * <p>There are two kinds of "compilation prerequisites": declared header files and pregrepped * headers. */ public Builder addCompilationPrerequisites(Iterable<Artifact> prerequisites) { // LIPO collector must not add compilation prerequisites in order to avoid // the creation of a middleman action. for (Artifact prerequisite : prerequisites) { String basename = prerequisite.getFilename(); Preconditions.checkArgument(!Link.OBJECT_FILETYPES.matches(basename)); Preconditions.checkArgument(!Link.ARCHIVE_LIBRARY_FILETYPES.matches(basename)); Preconditions.checkArgument(!Link.SHARED_LIBRARY_FILETYPES.matches(basename)); } Iterables.addAll(compilationPrerequisites, prerequisites); return this; }
@Override public List<Target> getLabelListAttr( QueryExpression caller, Target target, String attrName, String errorMsgPrefix) throws QueryException { Preconditions.checkArgument(target instanceof Rule); List<Target> result = new ArrayList<>(); Rule rule = (Rule) target; AggregatingAttributeMapper attrMap = AggregatingAttributeMapper.of(rule); Type<?> attrType = attrMap.getAttributeType(attrName); if (attrType == null) { // Return an empty list if the attribute isn't defined for this rule. return ImmutableList.of(); } for (Label label : attrMap.getReachableLabels(attrName, false)) { try { result.add(queryEnvironment.getTarget(label)); } catch (TargetNotFoundException e) { queryEnvironment.reportBuildFileError(caller, errorMsgPrefix + e.getMessage()); } } return result; }
/** * Constructs an Environment. This is the main, most basic constructor. * * @param globalFrame a frame for the global Environment * @param dynamicFrame a frame for the dynamic Environment * @param eventHandler an EventHandler for warnings, errors, etc * @param importedExtensions Extension-s from which to import bindings with load() * @param isSkylark true if in Skylark context * @param fileContentHashCode a hash for the source file being evaluated, if any * @param isLoadingPhase true if in loading phase */ private Environment( Frame globalFrame, Frame dynamicFrame, EventHandler eventHandler, Map<String, Extension> importedExtensions, boolean isSkylark, @Nullable String fileContentHashCode, boolean isLoadingPhase) { this.globalFrame = Preconditions.checkNotNull(globalFrame); this.dynamicFrame = Preconditions.checkNotNull(dynamicFrame); Preconditions.checkArgument(globalFrame.mutability().isMutable()); Preconditions.checkArgument(dynamicFrame.mutability().isMutable()); this.eventHandler = eventHandler; this.importedExtensions = importedExtensions; this.isSkylark = isSkylark; this.fileContentHashCode = fileContentHashCode; this.isLoadingPhase = isLoadingPhase; }
/** * Adds the given targets as dependencies - this can include explicit dependencies on other rules * (like from a "deps" attribute) and also implicit dependencies on runtime libraries. */ public CcLibraryHelper addDeps(Iterable<? extends TransitiveInfoCollection> deps) { for (TransitiveInfoCollection dep : deps) { Preconditions.checkArgument( dep.getConfiguration() == null || configuration.equalsOrIsSupersetOf(dep.getConfiguration()), "dep " + dep.getLabel() + " has a different config than " + ruleContext.getLabel()); this.deps.add(dep); } return this; }
/** Builds the Environment. */ public Environment build() { Preconditions.checkArgument(mutability.isMutable()); if (parent != null) { Preconditions.checkArgument(!parent.mutability().isMutable()); } Frame globalFrame = new Frame(mutability, parent); Frame dynamicFrame = new Frame(mutability, null); if (importedExtensions == null) { importedExtensions = ImmutableMap.of(); } return new Environment( globalFrame, dynamicFrame, eventHandler, importedExtensions, isSkylark, fileContentHashCode, isLoadingPhase); }
// The precondition ensures generic type safety @SuppressWarnings("unchecked") public <T> NestedSet<T> getSet(Class<T> type) { // Empty sets don't need have to have a type since they don't have items if (set.isEmpty()) { return (NestedSet<T>) set; } Preconditions.checkArgument( contentType.canBeCastTo(type), String.format( "Expected a set of '%s' but got a set of '%s'", EvalUtils.getDataTypeNameFromClass(type), contentType)); return (NestedSet<T>) set; }
/** * Prepends the given path to each path in {@code paths}. Empty paths are transformed to the value * of {@code variable} rather than {@code variable + "/."}. Absolute paths are returned without * modifications. */ @VisibleForTesting static Iterable<String> rootEach(final String prefix, Iterable<PathFragment> paths) { Preconditions.checkArgument( prefix.startsWith("$"), "prefix should start with a build setting variable like '$(NAME)': %s", prefix); Preconditions.checkArgument( !prefix.endsWith("/"), "prefix should not end with '/': %s", prefix); return Iterables.transform( paths, new Function<PathFragment, String>() { @Override public String apply(PathFragment input) { if (input.getSafePathString().equals(".")) { return prefix; } else if (input.isAbsolute()) { return input.getSafePathString(); } else { return prefix + "/" + input.getSafePathString(); } } }); }
/** * Creates SymlinkTreeAction instance. * * @param owner action owner * @param inputManifest the input runfiles manifest * @param artifactMiddleman the middleman artifact representing all the files the symlinks point * to (on Windows we need to know if the target of a "symlink" is a directory or a file so we * need to build it before) * @param outputManifest the generated symlink tree manifest (must have "MANIFEST" base name). * Symlink tree root will be set to the artifact's parent directory. * @param filesetTree true if this is fileset symlink tree, false if this is a runfiles symlink * tree. */ public SymlinkTreeAction( ActionOwner owner, Artifact inputManifest, @Nullable Artifact artifactMiddleman, Artifact outputManifest, boolean filesetTree, PathFragment shExecutable) { super(owner, computeInputs(inputManifest, artifactMiddleman), ImmutableList.of(outputManifest)); Preconditions.checkArgument(outputManifest.getPath().getBaseName().equals("MANIFEST")); this.inputManifest = inputManifest; this.outputManifest = outputManifest; this.filesetTree = filesetTree; this.shExecutable = shExecutable; }
/** * Transitively visit a nested set. * * @param nestedSet the nested set to visit transitively. */ @SuppressWarnings("unchecked") public void visit(NestedSet<E> nestedSet) { // This method suppresses the unchecked warning so that it can access the internal NestedSet // raw structure. Preconditions.checkArgument(nestedSet.getOrder() == Order.STABLE_ORDER); if (!visited.add(nestedSet)) { return; } for (NestedSet<E> subset : nestedSet.transitiveSets()) { visit(subset); } for (Object member : nestedSet.directMembers()) { if (visited.add((E) member)) { callback.accept((E) member); } } }
@Override public Iterable<String> getAttrAsString(Target target, String attrName) { Preconditions.checkArgument(target instanceof Rule); List<String> values = new ArrayList<>(); // May hold null values. Attribute attribute = ((Rule) target).getAttributeDefinition(attrName); if (attribute != null) { Type<?> attributeType = attribute.getType(); for (Object attrValue : AggregatingAttributeMapper.of((Rule) target) .visitAttribute(attribute.getName(), attributeType)) { // Ugly hack to maintain backward 'attr' query compatibility for BOOLEAN and TRISTATE // attributes. These are internally stored as actual Boolean or TriState objects but were // historically queried as integers. To maintain compatibility, we inspect their actual // value and return the integer equivalent represented as a String. This code is the // opposite of the code in BooleanType and TriStateType respectively. if (attributeType == BOOLEAN) { values.add(Type.BOOLEAN.cast(attrValue) ? "1" : "0"); } else if (attributeType == TRISTATE) { switch (BuildType.TRISTATE.cast(attrValue)) { case AUTO: values.add("-1"); break; case NO: values.add("0"); break; case YES: values.add("1"); break; default: throw new AssertionError("This can't happen!"); } } else { values.add(attrValue == null ? null : attrValue.toString()); } } } return values; }
@Override public void handle(Event event) { Preconditions.checkArgument( !EventKind.ERRORS_AND_WARNINGS.contains(event.getKind()), event); }
EnvironmentGroupConfiguredTarget(TargetContext targetContext, EnvironmentGroup envGroup) { super(targetContext); Preconditions.checkArgument(targetContext.getConfiguration() == null); }
/** * Sets the type of the link. It is an error to try to set this to {@link * LinkTargetType#INTERFACE_DYNAMIC_LIBRARY}. Note that all the static target types (see {@link * LinkTargetType#isStaticLibraryLink}) are equivalent, and there is no check that the output * artifact matches the target type extension. */ public Builder setLinkTargetType(LinkTargetType linkTargetType) { Preconditions.checkArgument(linkTargetType != LinkTargetType.INTERFACE_DYNAMIC_LIBRARY); this.linkTargetType = linkTargetType; return this; }
public LinkCommandLine build() { if (linkTargetType.isStaticLibraryLink()) { Preconditions.checkArgument( linkstamps.isEmpty(), "linkstamps may only be present on dynamic library or executable links"); Preconditions.checkArgument( buildInfoHeaderArtifacts.isEmpty(), "build info headers may only be present on dynamic library or executable links"); } ImmutableList<String> actualLinkstampCompileOptions; if (linkstampCompileOptions.isEmpty()) { actualLinkstampCompileOptions = DEFAULT_LINKSTAMP_OPTIONS; } else { actualLinkstampCompileOptions = ImmutableList.copyOf( Iterables.concat(DEFAULT_LINKSTAMP_OPTIONS, linkstampCompileOptions)); } // The ruleContext can be null for some tests. if (ruleContext != null) { if (featureConfiguration == null) { if (toolchain != null) { featureConfiguration = CcCommon.configureFeatures( ruleContext, toolchain, CcLibraryHelper.SourceCategory.CC); } else { featureConfiguration = CcCommon.configureFeatures(ruleContext); } } } if (variables == null) { variables = Variables.EMPTY; } String actionName = linkTargetType.getActionName(); return new LinkCommandLine( actionName, configuration, owner, output, interfaceOutput, buildInfoHeaderArtifacts, linkerInputs, runtimeInputs, linkTargetType, linkStaticness, linkopts, features, linkstamps, actualLinkstampCompileOptions, CppHelper.getFdoBuildStamp(ruleContext), runtimeSolibDir, nativeDeps, useTestOnlyFlags, needWholeArchive, paramFile, interfaceSoBuilder, noWholeArchiveFlags, variables, featureConfiguration); }
public SkylarkImportLookupKey(Label importLabel, boolean inWorkspace) { Preconditions.checkNotNull(importLabel); Preconditions.checkArgument(!importLabel.getPackageIdentifier().getRepository().isDefault()); this.importLabel = importLabel; this.inWorkspace = inWorkspace; }
@Override public List<String> getStringListAttr(Target target, String attrName) { Preconditions.checkArgument(target instanceof Rule); return NonconfigurableAttributeMapper.of((Rule) target).get(attrName, Type.STRING_LIST); }