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();
}
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();
}
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();
}
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());
}
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();
}
Aggregations