Search in sources :

Example 36 with SourcePath

use of com.facebook.buck.rules.SourcePath in project buck by facebook.

the class CopyResourcesStep method buildSteps.

@VisibleForTesting
ImmutableList<Step> buildSteps() {
    ImmutableList.Builder<Step> allSteps = ImmutableList.builder();
    if (resources.isEmpty()) {
        return allSteps.build();
    }
    String targetPackageDir = javaPackageFinder.findJavaPackage(target);
    for (SourcePath rawResource : resources) {
        // If the path to the file defining this rule were:
        // "first-party/orca/lib-http/tests/com/facebook/orca/BUCK"
        //
        // And the value of resource were:
        // "first-party/orca/lib-http/tests/com/facebook/orca/protocol/base/batch_exception1.txt"
        //
        // Assuming that `src_roots = tests` were in the [java] section of the .buckconfig file,
        // then javaPackageAsPath would be:
        // "com/facebook/orca/protocol/base/"
        //
        // And the path that we would want to copy to the classes directory would be:
        // "com/facebook/orca/protocol/base/batch_exception1.txt"
        //
        // Therefore, some path-wrangling is required to produce the correct string.
        Optional<BuildRule> underlyingRule = ruleFinder.getRule(rawResource);
        Path relativePathToResource = resolver.getRelativePath(rawResource);
        String resource;
        if (underlyingRule.isPresent()) {
            BuildTarget underlyingTarget = underlyingRule.get().getBuildTarget();
            if (underlyingRule.get() instanceof HasOutputName) {
                resource = MorePaths.pathWithUnixSeparators(underlyingTarget.getBasePath().resolve(((HasOutputName) underlyingRule.get()).getOutputName()));
            } else {
                Path genOutputParent = BuildTargets.getGenPath(filesystem, underlyingTarget, "%s").getParent();
                Path scratchOutputParent = BuildTargets.getScratchPath(filesystem, underlyingTarget, "%s").getParent();
                Optional<Path> outputPath = MorePaths.stripPrefix(relativePathToResource, genOutputParent).map(Optional::of).orElse(MorePaths.stripPrefix(relativePathToResource, scratchOutputParent));
                Preconditions.checkState(outputPath.isPresent(), "%s is used as a resource but does not output to a default output directory", underlyingTarget.getFullyQualifiedName());
                resource = MorePaths.pathWithUnixSeparators(underlyingTarget.getBasePath().resolve(outputPath.get()));
            }
        } else {
            resource = MorePaths.pathWithUnixSeparators(relativePathToResource);
        }
        Path javaPackageAsPath = javaPackageFinder.findJavaPackageFolder(outputDirectory.getFileSystem().getPath(resource));
        Path relativeSymlinkPath;
        if ("".equals(javaPackageAsPath.toString())) {
            // In this case, the project root is acting as the default package, so the resource path
            // works fine.
            relativeSymlinkPath = relativePathToResource.getFileName();
        } else {
            int lastIndex = resource.lastIndexOf(MorePaths.pathWithUnixSeparatorsAndTrailingSlash(javaPackageAsPath));
            if (lastIndex < 0) {
                Preconditions.checkState(rawResource instanceof BuildTargetSourcePath, "If resource path %s does not contain %s, then it must be a BuildTargetSourcePath.", relativePathToResource, javaPackageAsPath);
                // Handle the case where we depend on the output of another BuildRule. In that case, just
                // grab the output and put in the same package as this target would be in.
                relativeSymlinkPath = outputDirectory.getFileSystem().getPath(String.format("%s%s%s", targetPackageDir, targetPackageDir.isEmpty() ? "" : "/", resolver.getRelativePath(rawResource).getFileName()));
            } else {
                relativeSymlinkPath = outputDirectory.getFileSystem().getPath(resource.substring(lastIndex));
            }
        }
        Path target = outputDirectory.resolve(relativeSymlinkPath);
        MkdirAndSymlinkFileStep link = new MkdirAndSymlinkFileStep(filesystem, resolver.getAbsolutePath(rawResource), target);
        allSteps.add(link);
    }
    return allSteps.build();
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) Path(java.nio.file.Path) HasOutputName(com.facebook.buck.model.HasOutputName) ImmutableList(com.google.common.collect.ImmutableList) Step(com.facebook.buck.step.Step) MkdirAndSymlinkFileStep(com.facebook.buck.step.fs.MkdirAndSymlinkFileStep) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) BuildTarget(com.facebook.buck.model.BuildTarget) BuildRule(com.facebook.buck.rules.BuildRule) MkdirAndSymlinkFileStep(com.facebook.buck.step.fs.MkdirAndSymlinkFileStep) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 37 with SourcePath

use of com.facebook.buck.rules.SourcePath in project buck by facebook.

the class ReactNativeBundle method getInputsAfterBuildingLocally.

@Override
public ImmutableList<SourcePath> getInputsAfterBuildingLocally(BuildContext context) throws IOException {
    ImmutableList.Builder<SourcePath> inputs = ImmutableList.builder();
    // Use the generated depfile to determinate which sources ended up being used.
    ImmutableMap<Path, SourcePath> pathToSourceMap = Maps.uniqueIndex(srcs, context.getSourcePathResolver()::getAbsolutePath);
    Path depFile = getPathToDepFile(getBuildTarget(), getProjectFilesystem());
    for (String line : getProjectFilesystem().readLines(depFile)) {
        Path path = getProjectFilesystem().getPath(line);
        SourcePath sourcePath = pathToSourceMap.get(path);
        if (sourcePath == null) {
            throw new IOException(String.format("%s: entry path '%s' transitively uses source file not preset in `srcs`: %s", getBuildTarget(), entryPath, path));
        }
        inputs.add(sourcePath);
    }
    return inputs.build();
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) ImmutableList(com.google.common.collect.ImmutableList) IOException(java.io.IOException)

Example 38 with SourcePath

use of com.facebook.buck.rules.SourcePath in project buck by facebook.

the class AbstractJavacOptions method getInputs.

public ImmutableSortedSet<SourcePath> getInputs(SourcePathRuleFinder ruleFinder) {
    ImmutableSortedSet.Builder<SourcePath> builder = ImmutableSortedSet.<SourcePath>naturalOrder().addAll(getAnnotationProcessingParams().getInputs());
    Optional<SourcePath> javacJarPath = getJavacJarPath();
    if (javacJarPath.isPresent()) {
        SourcePath sourcePath = javacJarPath.get();
        // Add the original rule regardless of what happens next.
        builder.add(sourcePath);
        Optional<BuildRule> possibleRule = ruleFinder.getRule(sourcePath);
        if (possibleRule.isPresent()) {
            BuildRule rule = possibleRule.get();
            // And now include any transitive deps that contribute to the classpath.
            if (rule instanceof JavaLibrary) {
                builder.addAll(((JavaLibrary) rule).getDepsForTransitiveClasspathEntries().stream().map(BuildRule::getSourcePathToOutput).collect(MoreCollectors.toImmutableList()));
            } else {
                builder.add(sourcePath);
            }
        }
    }
    return builder.build();
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) BuildRule(com.facebook.buck.rules.BuildRule)

Example 39 with SourcePath

use of com.facebook.buck.rules.SourcePath in project buck by facebook.

the class AbstractJavacOptions method appendOptionsTo.

public void appendOptionsTo(OptionsConsumer optionsConsumer, SourcePathResolver pathResolver, ProjectFilesystem filesystem) {
    // Add some standard options.
    optionsConsumer.addOptionValue("source", getSourceLevel());
    optionsConsumer.addOptionValue("target", getTargetLevel());
    // Set the sourcepath to stop us reading source files out of jars by mistake.
    optionsConsumer.addOptionValue("sourcepath", "");
    if (isDebug()) {
        optionsConsumer.addFlag("g");
    }
    if (isVerbose()) {
        optionsConsumer.addFlag("verbose");
    }
    // Override the bootclasspath if Buck is building Java code for Android.
    if (getBootclasspath().isPresent()) {
        optionsConsumer.addOptionValue("bootclasspath", getBootclasspath().get());
    } else {
        String bcp = getSourceToBootclasspath().get(getSourceLevel());
        if (bcp != null) {
            optionsConsumer.addOptionValue("bootclasspath", bcp);
        }
    }
    // Add annotation processors.
    AnnotationProcessingParams annotationProcessingParams = getAnnotationProcessingParams();
    if (!annotationProcessingParams.isEmpty()) {
        // Specify where to generate sources so IntelliJ can pick them up.
        Path generateTo = annotationProcessingParams.getGeneratedSourceFolderName();
        if (generateTo != null) {
            //noinspection ConstantConditions
            optionsConsumer.addOptionValue("s", filesystem.resolve(generateTo).toString());
        }
        ImmutableList<ResolvedJavacPluginProperties> annotationProcessors = annotationProcessingParams.getAnnotationProcessors(filesystem, pathResolver);
        // Specify processorpath to search for processors.
        optionsConsumer.addOptionValue("processorpath", annotationProcessors.stream().map(ResolvedJavacPluginProperties::getClasspath).flatMap(Arrays::stream).distinct().map(URL::toString).collect(Collectors.joining(File.pathSeparator)));
        // Specify names of processors.
        optionsConsumer.addOptionValue("processor", annotationProcessors.stream().map(ResolvedJavacPluginProperties::getProcessorNames).flatMap(Collection::stream).collect(Collectors.joining(",")));
        // Add processor parameters.
        for (String parameter : annotationProcessingParams.getParameters()) {
            optionsConsumer.addFlag("A" + parameter);
        }
        if (annotationProcessingParams.getProcessOnly()) {
            optionsConsumer.addFlag("proc:only");
        }
    }
    // Add extra arguments.
    optionsConsumer.addExtras(getExtraArguments());
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) Collection(java.util.Collection) Arrays(java.util.Arrays) URL(java.net.URL)

Example 40 with SourcePath

use of com.facebook.buck.rules.SourcePath in project buck by facebook.

the class CxxLuaExtensionDescription method getExtensionArgs.

private ImmutableList<com.facebook.buck.rules.args.Arg> getExtensionArgs(BuildRuleParams params, BuildRuleResolver ruleResolver, SourcePathResolver pathResolver, SourcePathRuleFinder ruleFinder, CxxPlatform cxxPlatform, Arg args) throws NoSuchBuildTargetException {
    // Extract all C/C++ sources from the constructor arg.
    ImmutableMap<String, CxxSource> srcs = CxxDescriptionEnhancer.parseCxxSources(params.getBuildTarget(), ruleResolver, ruleFinder, pathResolver, cxxPlatform, args);
    ImmutableMap<Path, SourcePath> headers = CxxDescriptionEnhancer.parseHeaders(params.getBuildTarget(), ruleResolver, ruleFinder, pathResolver, Optional.of(cxxPlatform), args);
    // Setup the header symlink tree and combine all the preprocessor input from this rule
    // and all dependencies.
    HeaderSymlinkTree headerSymlinkTree = CxxDescriptionEnhancer.requireHeaderSymlinkTree(params, ruleResolver, cxxPlatform, headers, HeaderVisibility.PRIVATE, true);
    Optional<SymlinkTree> sandboxTree = Optional.empty();
    if (cxxBuckConfig.sandboxSources()) {
        sandboxTree = CxxDescriptionEnhancer.createSandboxTree(params, ruleResolver, cxxPlatform);
    }
    ImmutableList<CxxPreprocessorInput> cxxPreprocessorInput = CxxDescriptionEnhancer.collectCxxPreprocessorInput(params, cxxPlatform, CxxFlags.getLanguageFlags(args.preprocessorFlags, args.platformPreprocessorFlags, args.langPreprocessorFlags, cxxPlatform), ImmutableList.of(headerSymlinkTree), ImmutableSet.of(), CxxPreprocessables.getTransitiveCxxPreprocessorInput(cxxPlatform, params.getDeps()), args.includeDirs, sandboxTree);
    // Generate rule to build the object files.
    ImmutableMap<CxxPreprocessAndCompile, SourcePath> picObjects = CxxSourceRuleFactory.requirePreprocessAndCompileRules(params, ruleResolver, pathResolver, ruleFinder, cxxBuckConfig, cxxPlatform, cxxPreprocessorInput, CxxFlags.getLanguageFlags(args.compilerFlags, args.platformCompilerFlags, args.langCompilerFlags, cxxPlatform), args.prefixHeader, args.precompiledHeader, srcs, CxxSourceRuleFactory.PicType.PIC, sandboxTree);
    ImmutableList.Builder<com.facebook.buck.rules.args.Arg> argsBuilder = ImmutableList.builder();
    argsBuilder.addAll(CxxDescriptionEnhancer.toStringWithMacrosArgs(params.getBuildTarget(), params.getCellRoots(), ruleResolver, cxxPlatform, CxxFlags.getFlagsWithMacrosWithPlatformMacroExpansion(args.linkerFlags, args.platformLinkerFlags, cxxPlatform)));
    // Add object files into the args.
    argsBuilder.addAll(SourcePathArg.from(picObjects.values()));
    return argsBuilder.build();
}
Also used : Path(java.nio.file.Path) SourcePath(com.facebook.buck.rules.SourcePath) HeaderSymlinkTree(com.facebook.buck.cxx.HeaderSymlinkTree) CxxPreprocessAndCompile(com.facebook.buck.cxx.CxxPreprocessAndCompile) ImmutableList(com.google.common.collect.ImmutableList) CxxSource(com.facebook.buck.cxx.CxxSource) SourcePath(com.facebook.buck.rules.SourcePath) SymlinkTree(com.facebook.buck.rules.SymlinkTree) HeaderSymlinkTree(com.facebook.buck.cxx.HeaderSymlinkTree) SourcePathArg(com.facebook.buck.rules.args.SourcePathArg) CxxConstructorArg(com.facebook.buck.cxx.CxxConstructorArg) CxxPreprocessorInput(com.facebook.buck.cxx.CxxPreprocessorInput)

Aggregations

SourcePath (com.facebook.buck.rules.SourcePath)285 Path (java.nio.file.Path)151 BuildTarget (com.facebook.buck.model.BuildTarget)111 Test (org.junit.Test)105 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)102 PathSourcePath (com.facebook.buck.rules.PathSourcePath)100 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)96 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)90 BuildRule (com.facebook.buck.rules.BuildRule)79 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)69 FakeSourcePath (com.facebook.buck.rules.FakeSourcePath)69 ExplicitBuildTargetSourcePath (com.facebook.buck.rules.ExplicitBuildTargetSourcePath)64 ImmutableList (com.google.common.collect.ImmutableList)64 DefaultBuildTargetSourcePath (com.facebook.buck.rules.DefaultBuildTargetSourcePath)53 ImmutableMap (com.google.common.collect.ImmutableMap)48 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)43 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)42 HumanReadableException (com.facebook.buck.util.HumanReadableException)39 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)37 ImmutableSet (com.google.common.collect.ImmutableSet)34