use of com.facebook.buck.rules.BuildRule in project buck by facebook.
the class DDescriptionUtils method sourcePathsForCompiledSources.
/**
* Generates BuildTargets and BuildRules to compile D sources to object files, and
* returns a list of SourcePaths referring to the generated object files.
* @param sources source files to compile
* @param compilerFlags flags to pass to the compiler
* @param baseParams build parameters for the compilation
* @param buildRuleResolver resolver for build rules
* @param sourcePathResolver resolver for source paths
* @param cxxPlatform the C++ platform to compile for
* @param dBuckConfig the Buck configuration for D
* @return SourcePaths of the generated object files
*/
public static ImmutableList<SourcePath> sourcePathsForCompiledSources(BuildRuleParams baseParams, BuildRuleResolver buildRuleResolver, SourcePathResolver sourcePathResolver, SourcePathRuleFinder ruleFinder, CxxPlatform cxxPlatform, DBuckConfig dBuckConfig, ImmutableList<String> compilerFlags, SourceList sources, DIncludes includes) throws NoSuchBuildTargetException {
ImmutableList.Builder<SourcePath> sourcePaths = ImmutableList.builder();
for (Map.Entry<String, SourcePath> source : sources.toNameMap(baseParams.getBuildTarget(), sourcePathResolver, "srcs").entrySet()) {
BuildTarget compileTarget = createDCompileBuildTarget(baseParams.getBuildTarget(), source.getKey(), cxxPlatform);
BuildRule rule = requireBuildRule(compileTarget, baseParams, buildRuleResolver, ruleFinder, dBuckConfig, compilerFlags, source.getKey(), source.getValue(), includes);
sourcePaths.add(rule.getSourcePathToOutput());
}
return sourcePaths.build();
}
use of com.facebook.buck.rules.BuildRule in project buck by facebook.
the class DDescriptionUtils method requireBuildRule.
/**
* Ensures that a DCompileBuildRule exists for the given target, creating a DCompileBuildRule
* if neccesary.
* @param baseParams build parameters for the rule
* @param buildRuleResolver BuildRuleResolver the rule should be in
* @param src the source file to be compiled
* @param compilerFlags flags to pass to the compiler
* @param compileTarget the target the rule should be for
* @param dBuckConfig the Buck configuration for D
* @return the build rule
*/
public static DCompileBuildRule requireBuildRule(BuildTarget compileTarget, BuildRuleParams baseParams, BuildRuleResolver buildRuleResolver, SourcePathRuleFinder ruleFinder, DBuckConfig dBuckConfig, ImmutableList<String> compilerFlags, String name, SourcePath src, DIncludes includes) throws NoSuchBuildTargetException {
Optional<BuildRule> existingRule = buildRuleResolver.getRuleOptional(compileTarget);
if (existingRule.isPresent()) {
return (DCompileBuildRule) existingRule.get();
} else {
Tool compiler = dBuckConfig.getDCompiler();
Map<BuildTarget, DIncludes> transitiveIncludes = new TreeMap<>();
transitiveIncludes.put(baseParams.getBuildTarget(), includes);
for (Map.Entry<BuildTarget, DLibrary> library : getTransitiveDLibraryRules(baseParams.getDeps()).entrySet()) {
transitiveIncludes.put(library.getKey(), library.getValue().getIncludes());
}
ImmutableSortedSet.Builder<BuildRule> depsBuilder = ImmutableSortedSet.naturalOrder();
depsBuilder.addAll(compiler.getDeps(ruleFinder));
depsBuilder.addAll(ruleFinder.filterBuildRuleInputs(src));
for (DIncludes dIncludes : transitiveIncludes.values()) {
depsBuilder.addAll(dIncludes.getDeps(ruleFinder));
}
ImmutableSortedSet<BuildRule> deps = depsBuilder.build();
return buildRuleResolver.addToIndex(new DCompileBuildRule(baseParams.withBuildTarget(compileTarget).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(deps), Suppliers.ofInstance(ImmutableSortedSet.of())), compiler, ImmutableList.<String>builder().addAll(dBuckConfig.getBaseCompilerFlags()).addAll(compilerFlags).build(), name, ImmutableSortedSet.of(src), ImmutableList.copyOf(transitiveIncludes.values())));
}
}
use of com.facebook.buck.rules.BuildRule 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));
}
use of com.facebook.buck.rules.BuildRule in project buck by facebook.
the class CxxPreprocessables method addHeaderSymlinkTree.
/**
* @return adds a the header {@link com.facebook.buck.rules.SymlinkTree} for the given rule to
* the {@link CxxPreprocessorInput}.
*/
public static CxxPreprocessorInput.Builder addHeaderSymlinkTree(CxxPreprocessorInput.Builder builder, BuildTarget target, BuildRuleResolver ruleResolver, CxxPlatform platform, HeaderVisibility headerVisibility, IncludeType includeType) throws NoSuchBuildTargetException {
BuildRule rule = ruleResolver.requireRule(BuildTarget.builder(target).addFlavors(platform.getFlavor(), CxxDescriptionEnhancer.getHeaderSymlinkTreeFlavor(headerVisibility)).build());
Preconditions.checkState(rule instanceof HeaderSymlinkTree, "Attempt to add %s of type %s and class %s to %s", rule.getFullyQualifiedName(), rule.getType(), rule.getClass().getName(), target);
HeaderSymlinkTree symlinkTree = (HeaderSymlinkTree) rule;
builder.addIncludes(CxxSymlinkTreeHeaders.from(symlinkTree, includeType));
return builder;
}
use of com.facebook.buck.rules.BuildRule in project buck by facebook.
the class PrebuiltCxxLibraryDescription method getApplicableSourcePath.
private static SourcePath getApplicableSourcePath(final BuildTarget target, final CellPathResolver cellRoots, final ProjectFilesystem filesystem, final BuildRuleResolver ruleResolver, final CxxPlatform cxxPlatform, Optional<String> versionSubDir, final String basePathString, final Optional<String> addedPathString) {
ImmutableList<BuildRule> deps;
MacroHandler handler = getMacroHandler(Optional.of(cxxPlatform));
try {
deps = handler.extractBuildTimeDeps(target, cellRoots, ruleResolver, basePathString);
} catch (MacroException e) {
deps = ImmutableList.of();
}
Path libDirPath = filesystem.getPath(expandMacros(handler, target, cellRoots, ruleResolver, basePathString));
if (versionSubDir.isPresent()) {
libDirPath = filesystem.getPath(versionSubDir.get()).resolve(libDirPath);
}
// So just expand the macros and return a PathSourcePath
if (deps.isEmpty()) {
Path resultPath = libDirPath;
if (addedPathString.isPresent()) {
resultPath = libDirPath.resolve(expandMacros(handler, target, cellRoots, ruleResolver, addedPathString.get()));
}
resultPath = target.getBasePath().resolve(resultPath);
return new PathSourcePath(filesystem, resultPath);
}
// If we get here then this is referencing the output from a build rule.
// This always return a ExplicitBuildTargetSourcePath
Path p = filesystem.resolve(libDirPath);
if (addedPathString.isPresent()) {
p = p.resolve(addedPathString.get());
}
p = filesystem.relativize(p);
return new ExplicitBuildTargetSourcePath(deps.iterator().next().getBuildTarget(), p);
}
Aggregations