use of java.util.Optional in project buck by facebook.
the class MiniAapt method resourceUnion.
/**
* Collect resource information from R.txt for each dep and perform a resource union.
* @throws IOException
*/
public void resourceUnion() throws IOException {
for (Path depRTxt : pathsToSymbolsOfDeps) {
Iterable<String> lines = FluentIterable.from(filesystem.readLines(depRTxt)).filter(input -> !Strings.isNullOrEmpty(input)).toList();
for (String line : lines) {
Optional<RDotTxtEntry> entry = RDotTxtEntry.parse(line);
Preconditions.checkState(entry.isPresent());
resourceCollector.addResourceIfNotPresent(entry.get());
}
}
}
use of java.util.Optional in project buck by facebook.
the class MiniAapt method verifyReferences.
@VisibleForTesting
ImmutableSet<RDotTxtEntry> verifyReferences(ProjectFilesystem filesystem, ImmutableSet<RDotTxtEntry> references) throws IOException {
ImmutableSet.Builder<RDotTxtEntry> unresolved = ImmutableSet.builder();
ImmutableSet.Builder<RDotTxtEntry> definitionsBuilder = ImmutableSet.builder();
definitionsBuilder.addAll(resourceCollector.getResources());
for (Path depRTxt : pathsToSymbolsOfDeps) {
Iterable<String> lines = FluentIterable.from(filesystem.readLines(depRTxt)).filter(input -> !Strings.isNullOrEmpty(input)).toList();
for (String line : lines) {
Optional<RDotTxtEntry> entry = RDotTxtEntry.parse(line);
Preconditions.checkState(entry.isPresent());
definitionsBuilder.add(entry.get());
}
}
Set<RDotTxtEntry> definitions = definitionsBuilder.build();
for (RDotTxtEntry reference : references) {
if (!definitions.contains(reference)) {
unresolved.add(reference);
}
}
return unresolved.build();
}
use of java.util.Optional in project buck by facebook.
the class CxxLibraryDescription method createMetadata.
@Override
public <A extends Arg, U> Optional<U> createMetadata(BuildTarget buildTarget, BuildRuleResolver resolver, A args, Optional<ImmutableMap<BuildTarget, Version>> selectedVersions, final Class<U> metadataClass) throws NoSuchBuildTargetException {
Map.Entry<Flavor, MetadataType> type = METADATA_TYPE.getFlavorAndValue(buildTarget).orElseThrow(IllegalArgumentException::new);
BuildTarget baseTarget = buildTarget.withoutFlavors(type.getKey());
switch(type.getValue()) {
case CXX_HEADERS:
{
Optional<CxxHeaders> symlinkTree = Optional.empty();
if (!args.exportedHeaders.isEmpty()) {
CxxPreprocessables.HeaderMode mode = HEADER_MODE.getRequiredValue(buildTarget);
baseTarget = baseTarget.withoutFlavors(mode.getFlavor());
symlinkTree = Optional.of(CxxSymlinkTreeHeaders.from((HeaderSymlinkTree) resolver.requireRule(baseTarget.withAppendedFlavors(Type.EXPORTED_HEADERS.getFlavor(), mode.getFlavor())), CxxPreprocessables.IncludeType.LOCAL));
}
return symlinkTree.map(metadataClass::cast);
}
case CXX_PREPROCESSOR_INPUT:
{
Map.Entry<Flavor, CxxPlatform> platform = cxxPlatforms.getFlavorAndValue(buildTarget).orElseThrow(IllegalArgumentException::new);
Map.Entry<Flavor, HeaderVisibility> visibility = HEADER_VISIBILITY.getFlavorAndValue(buildTarget).orElseThrow(IllegalArgumentException::new);
baseTarget = baseTarget.withoutFlavors(platform.getKey(), visibility.getKey());
CxxPreprocessorInput.Builder cxxPreprocessorInputBuilder = CxxPreprocessorInput.builder();
// TODO(andrewjcg): We currently always add exported flags and frameworks to the
// preprocessor input to mimic existing behavior, but this should likely be fixed.
cxxPreprocessorInputBuilder.putAllPreprocessorFlags(CxxFlags.getLanguageFlags(args.exportedPreprocessorFlags, args.exportedPlatformPreprocessorFlags, args.exportedLangPreprocessorFlags, platform.getValue()));
cxxPreprocessorInputBuilder.addAllFrameworks(args.frameworks);
if (visibility.getValue() == HeaderVisibility.PRIVATE && !args.headers.isEmpty()) {
HeaderSymlinkTree symlinkTree = (HeaderSymlinkTree) resolver.requireRule(baseTarget.withAppendedFlavors(platform.getKey(), Type.HEADERS.getFlavor()));
cxxPreprocessorInputBuilder.addIncludes(CxxSymlinkTreeHeaders.from(symlinkTree, CxxPreprocessables.IncludeType.LOCAL));
}
if (visibility.getValue() == HeaderVisibility.PUBLIC) {
// Add platform-agnostic headers.
boolean shouldCreatePublicHeaderSymlinks = args.xcodePublicHeadersSymlinks.orElse(true);
CxxPreprocessables.HeaderMode mode = CxxDescriptionEnhancer.getHeaderModeForPlatform(resolver, platform.getValue(), shouldCreatePublicHeaderSymlinks);
Optional<CxxHeaders> exportedHeaders = resolver.requireMetadata(baseTarget.withAppendedFlavors(MetadataType.CXX_HEADERS.getFlavor(), mode.getFlavor()), CxxHeaders.class);
exportedHeaders.ifPresent(cxxPreprocessorInputBuilder::addIncludes);
// Add platform-specific headers.
if (!args.exportedPlatformHeaders.getMatchingValues(platform.getKey().toString()).isEmpty()) {
HeaderSymlinkTree symlinkTree = (HeaderSymlinkTree) resolver.requireRule(baseTarget.withAppendedFlavors(platform.getKey(), Type.EXPORTED_HEADERS.getFlavor()));
cxxPreprocessorInputBuilder.addIncludes(CxxSymlinkTreeHeaders.from(symlinkTree, CxxPreprocessables.IncludeType.LOCAL));
}
}
CxxPreprocessorInput cxxPreprocessorInput = cxxPreprocessorInputBuilder.build();
return Optional.of(cxxPreprocessorInput).map(metadataClass::cast);
}
case COMPILATION_DATABASE_DEPS:
{
return CxxDescriptionEnhancer.createCompilationDatabaseDependencies(buildTarget, cxxPlatforms, resolver, args).map(metadataClass::cast);
}
}
throw new IllegalStateException(String.format("unhandled metadata type: %s", type.getValue()));
}
use of java.util.Optional in project buck by facebook.
the class Omnibus method createRoot.
// Create a build rule which links the given root node against the merged omnibus library
// described by the given spec file.
protected static OmnibusRoot createRoot(BuildRuleParams params, BuildRuleResolver ruleResolver, SourcePathRuleFinder ruleFinder, CxxBuckConfig cxxBuckConfig, CxxPlatform cxxPlatform, ImmutableList<? extends Arg> extraLdflags, OmnibusSpec spec, SourcePath omnibus, NativeLinkTarget root, BuildTarget rootTargetBase, Optional<Path> output) throws NoSuchBuildTargetException {
ImmutableList.Builder<Arg> argsBuilder = ImmutableList.builder();
// Add any extra flags to the link.
argsBuilder.addAll(extraLdflags);
// Since the dummy omnibus library doesn't actually contain any symbols, make sure the linker
// won't drop its runtime reference to it.
argsBuilder.addAll(StringArg.from(cxxPlatform.getLd().resolve(ruleResolver).getNoAsNeededSharedLibsFlags()));
// Since we're linking against a dummy libomnibus, ignore undefined symbols.
argsBuilder.addAll(StringArg.from(cxxPlatform.getLd().resolve(ruleResolver).getIgnoreUndefinedSymbolsFlags()));
// Add the args for the root link target first.
NativeLinkableInput input = root.getNativeLinkTargetInput(cxxPlatform);
argsBuilder.addAll(input.getArgs());
// Grab a topologically sorted mapping of all the root's deps.
ImmutableMap<BuildTarget, NativeLinkable> deps = NativeLinkables.getNativeLinkables(cxxPlatform, root.getNativeLinkTargetDeps(cxxPlatform), Linker.LinkableDepType.SHARED);
// Now process the dependencies in topological order, to assemble the link line.
boolean alreadyAddedOmnibusToArgs = false;
for (Map.Entry<BuildTarget, NativeLinkable> entry : deps.entrySet()) {
BuildTarget target = entry.getKey();
NativeLinkable nativeLinkable = entry.getValue();
Linker.LinkableDepType linkStyle = NativeLinkables.getLinkStyle(nativeLinkable.getPreferredLinkage(cxxPlatform), Linker.LinkableDepType.SHARED);
// If this dep needs to be linked statically, then we always link it directly.
if (linkStyle != Linker.LinkableDepType.SHARED) {
Preconditions.checkState(linkStyle == Linker.LinkableDepType.STATIC_PIC);
argsBuilder.addAll(nativeLinkable.getNativeLinkableInput(cxxPlatform, linkStyle).getArgs());
continue;
}
// If this dep is another root node, substitute in the custom linked library we built for it.
if (spec.getRoots().containsKey(target)) {
argsBuilder.add(SourcePathArg.of(new DefaultBuildTargetSourcePath(getRootTarget(params.getBuildTarget(), target))));
continue;
}
// libomnibus instead.
if (spec.getBody().containsKey(target)) {
// && linkStyle == Linker.LinkableDepType.SHARED) {
if (!alreadyAddedOmnibusToArgs) {
argsBuilder.add(SourcePathArg.of(omnibus));
alreadyAddedOmnibusToArgs = true;
}
continue;
}
// Otherwise, this is either an explicitly statically linked or excluded node, so link it
// normally.
Preconditions.checkState(spec.getExcluded().containsKey(target));
argsBuilder.addAll(nativeLinkable.getNativeLinkableInput(cxxPlatform, linkStyle).getArgs());
}
// Create the root library rule using the arguments assembled above.
BuildTarget rootTarget = getRootTarget(params.getBuildTarget(), rootTargetBase);
NativeLinkTargetMode rootTargetMode = root.getNativeLinkTargetMode(cxxPlatform);
CxxLink rootLinkRule;
switch(rootTargetMode.getType()) {
// Link the root as a shared library.
case SHARED:
{
Optional<String> rootSoname = rootTargetMode.getLibraryName();
rootLinkRule = CxxLinkableEnhancer.createCxxLinkableSharedBuildRule(cxxBuckConfig, cxxPlatform, params, ruleResolver, ruleFinder, rootTarget, output.orElse(BuildTargets.getGenPath(params.getProjectFilesystem(), rootTarget, "%s").resolve(rootSoname.orElse(String.format("%s.%s", rootTarget.getShortName(), cxxPlatform.getSharedLibraryExtension())))), rootSoname, argsBuilder.build());
break;
}
// Link the root as an executable.
case EXECUTABLE:
{
rootLinkRule = CxxLinkableEnhancer.createCxxLinkableBuildRule(cxxBuckConfig, cxxPlatform, params, ruleResolver, ruleFinder, rootTarget, output.orElse(BuildTargets.getGenPath(params.getProjectFilesystem(), rootTarget, "%s").resolve(rootTarget.getShortName())), argsBuilder.build(), Linker.LinkableDepType.SHARED, Optional.empty());
break;
}
// $CASES-OMITTED$
default:
throw new IllegalStateException(String.format("%s: unexpected omnibus root type: %s %s", params.getBuildTarget(), root.getBuildTarget(), rootTargetMode.getType()));
}
CxxLink rootRule = ruleResolver.addToIndex(rootLinkRule);
return OmnibusRoot.of(rootRule.getSourcePathToOutput());
}
use of java.util.Optional in project buck by facebook.
the class DTestDescription method createBuildRule.
@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, BuildRuleParams params, BuildRuleResolver buildRuleResolver, A args) throws NoSuchBuildTargetException {
BuildTarget target = params.getBuildTarget();
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(buildRuleResolver);
SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
SymlinkTree sourceTree = buildRuleResolver.addToIndex(DDescriptionUtils.createSourceSymlinkTree(DDescriptionUtils.getSymlinkTreeTarget(params.getBuildTarget()), params, ruleFinder, pathResolver, args.srcs));
// Create a helper rule to build the test binary.
// The rule needs its own target so that we can depend on it without creating cycles.
BuildTarget binaryTarget = DDescriptionUtils.createBuildTargetForFile(target, "build-", target.getFullyQualifiedName(), cxxPlatform);
BuildRule binaryRule = DDescriptionUtils.createNativeLinkable(params.withBuildTarget(binaryTarget), buildRuleResolver, cxxPlatform, dBuckConfig, cxxBuckConfig, ImmutableList.of("-unittest"), args.srcs, args.linkerFlags, DIncludes.builder().setLinkTree(sourceTree.getSourcePathToOutput()).addAllSources(args.srcs.getPaths()).build());
buildRuleResolver.addToIndex(binaryRule);
return new DTest(params.copyAppendingExtraDeps(ImmutableList.of(binaryRule)), binaryRule, args.contacts, args.labels, args.testRuleTimeoutMs.map(Optional::of).orElse(defaultTestRuleTimeoutMs));
}
Aggregations