use of com.facebook.buck.rules.BuildRuleParams in project buck by facebook.
the class PrecompiledHeaderFeatureTest method preconfiguredSourceRuleFactoryBuilder.
/**
* Configures a CxxSourceRuleFactory.Builder with some sane defaults for PCH tests.
* Note: doesn't call "setPrefixHeader", which actually sets the PCH parameters; caller
* needs to do that in their various tests.
*/
private static CxxSourceRuleFactory.Builder preconfiguredSourceRuleFactoryBuilder(String targetPath, BuildRuleResolver ruleResolver) {
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(ruleResolver);
SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
BuildTarget target = BuildTargetFactory.newInstance(targetPath);
BuildRuleParams params = new FakeBuildRuleParamsBuilder(target).build();
return CxxSourceRuleFactory.builder().setParams(params).setResolver(ruleResolver).setPathResolver(pathResolver).setRuleFinder(ruleFinder).setPicType(AbstractCxxSourceRuleFactory.PicType.PDC);
}
use of com.facebook.buck.rules.BuildRuleParams in project buck by facebook.
the class CxxTestDescription method createBuildRule.
@SuppressWarnings("PMD.PrematureDeclaration")
@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, BuildRuleParams inputParams, final BuildRuleResolver resolver, final A args) throws NoSuchBuildTargetException {
Optional<StripStyle> flavoredStripStyle = StripStyle.FLAVOR_DOMAIN.getValue(inputParams.getBuildTarget());
Optional<LinkerMapMode> flavoredLinkerMapMode = LinkerMapMode.FLAVOR_DOMAIN.getValue(inputParams.getBuildTarget());
inputParams = CxxStrip.removeStripStyleFlavorInParams(inputParams, flavoredStripStyle);
inputParams = LinkerMapMode.removeLinkerMapModeFlavorInParams(inputParams, flavoredLinkerMapMode);
final BuildRuleParams params = inputParams;
Optional<CxxPlatform> platform = cxxPlatforms.getValue(params.getBuildTarget());
CxxPlatform cxxPlatform = platform.orElse(defaultCxxPlatform);
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
if (params.getBuildTarget().getFlavors().contains(CxxCompilationDatabase.COMPILATION_DATABASE)) {
BuildRuleParams paramsWithoutFlavor = params.withoutFlavor(CxxCompilationDatabase.COMPILATION_DATABASE);
CxxLinkAndCompileRules cxxLinkAndCompileRules = CxxDescriptionEnhancer.createBuildRulesForCxxBinaryDescriptionArg(targetGraph, paramsWithoutFlavor, resolver, cxxBuckConfig, cxxPlatform, args, flavoredStripStyle, flavoredLinkerMapMode);
return CxxCompilationDatabase.createCompilationDatabase(params, cxxLinkAndCompileRules.compileRules);
}
if (params.getBuildTarget().getFlavors().contains(CxxCompilationDatabase.UBER_COMPILATION_DATABASE)) {
return CxxDescriptionEnhancer.createUberCompilationDatabase(platform.isPresent() ? params : params.withAppendedFlavor(cxxPlatform.getFlavor()), resolver);
}
if (params.getBuildTarget().getFlavors().contains(CxxDescriptionEnhancer.SANDBOX_TREE_FLAVOR)) {
return CxxDescriptionEnhancer.createSandboxTreeBuildRule(resolver, args, cxxPlatform, params);
}
// Generate the link rule that builds the test binary.
final CxxLinkAndCompileRules cxxLinkAndCompileRules = CxxDescriptionEnhancer.createBuildRulesForCxxBinaryDescriptionArg(targetGraph, params, resolver, cxxBuckConfig, cxxPlatform, args, flavoredStripStyle, flavoredLinkerMapMode);
// Construct the actual build params we'll use, notably with an added dependency on the
// CxxLink rule above which builds the test binary.
BuildRuleParams testParams = params.copyReplacingDeclaredAndExtraDeps(() -> cxxLinkAndCompileRules.deps, params.getExtraDeps()).copyAppendingExtraDeps(cxxLinkAndCompileRules.executable.getDeps(ruleFinder));
testParams = CxxStrip.restoreStripStyleFlavorInParams(testParams, flavoredStripStyle);
testParams = LinkerMapMode.restoreLinkerMapModeFlavorInParams(testParams, flavoredLinkerMapMode);
// Supplier which expands macros in the passed in test environment.
ImmutableMap<String, String> testEnv = ImmutableMap.copyOf(Maps.transformValues(args.env, CxxDescriptionEnhancer.MACRO_HANDLER.getExpander(params.getBuildTarget(), params.getCellRoots(), resolver)));
// Supplier which expands macros in the passed in test arguments.
Supplier<ImmutableList<String>> testArgs = () -> args.args.stream().map(CxxDescriptionEnhancer.MACRO_HANDLER.getExpander(params.getBuildTarget(), params.getCellRoots(), resolver)::apply).collect(MoreCollectors.toImmutableList());
Supplier<ImmutableSortedSet<BuildRule>> additionalDeps = () -> {
ImmutableSortedSet.Builder<BuildRule> deps = ImmutableSortedSet.naturalOrder();
// It's not uncommon for users to add dependencies onto other binaries that they run
// during the test, so make sure to add them as runtime deps.
deps.addAll(Sets.difference(params.getDeps(), cxxLinkAndCompileRules.getBinaryRule().getDeps()));
// Add any build-time from any macros embedded in the `env` or `args` parameter.
for (String part : Iterables.concat(args.args, args.env.values())) {
try {
deps.addAll(CxxDescriptionEnhancer.MACRO_HANDLER.extractBuildTimeDeps(params.getBuildTarget(), params.getCellRoots(), resolver, part));
} catch (MacroException e) {
throw new HumanReadableException(e, "%s: %s", params.getBuildTarget(), e.getMessage());
}
}
return deps.build();
};
CxxTest test;
CxxTestType type = args.framework.orElse(getDefaultTestType());
switch(type) {
case GTEST:
{
test = new CxxGtestTest(testParams, ruleFinder, cxxLinkAndCompileRules.getBinaryRule(), cxxLinkAndCompileRules.executable, testEnv, testArgs, FluentIterable.from(args.resources).transform(p -> new PathSourcePath(params.getProjectFilesystem(), p)).toSortedSet(Ordering.natural()), additionalDeps, args.labels, args.contacts, args.runTestSeparately.orElse(false), args.testRuleTimeoutMs.map(Optional::of).orElse(defaultTestRuleTimeoutMs), cxxBuckConfig.getMaximumTestOutputSize());
break;
}
case BOOST:
{
test = new CxxBoostTest(testParams, ruleFinder, cxxLinkAndCompileRules.getBinaryRule(), cxxLinkAndCompileRules.executable, testEnv, testArgs, FluentIterable.from(args.resources).transform(p -> new PathSourcePath(params.getProjectFilesystem(), p)).toSortedSet(Ordering.natural()), additionalDeps, args.labels, args.contacts, args.runTestSeparately.orElse(false), args.testRuleTimeoutMs.map(Optional::of).orElse(defaultTestRuleTimeoutMs));
break;
}
default:
{
Preconditions.checkState(false, "Unhandled C++ test type: %s", type);
throw new RuntimeException();
}
}
return test;
}
use of com.facebook.buck.rules.BuildRuleParams in project buck by facebook.
the class AbstractCxxSourceRuleFactory method buildPrecompiledHeader.
/**
* Look up or build a precompiled header build rule which this build rule is requesting.
*
* <p>
* This method will first try to determine whether a matching PCH was already created;
* if so, it will be reused. This is done by searching the cache in the {@link BuildRuleResolver}
* owned by this class. If this ends up building a new instance of {@link CxxPrecompiledHeader},
* it will be added to the resolver cache.
* </p>
*/
private CxxPrecompiledHeader buildPrecompiledHeader(PreprocessorDelegate preprocessorDelegate, CxxSource.Type sourceType, CxxToolFlags compilerFlags, SourcePath headerPath, DepsBuilder depsBuilder, UnflavoredBuildTarget templateTarget, ImmutableSortedSet<Flavor> flavors) {
BuildTarget target = BuildTarget.builder(templateTarget).addAllFlavors(flavors).build();
Optional<CxxPrecompiledHeader> existingRule = getResolver().getRuleOptionalWithType(target, CxxPrecompiledHeader.class);
if (existingRule.isPresent()) {
return existingRule.get();
}
// Give the PCH a filename that looks like a header file with .gch appended to it, GCC-style.
// GCC accepts an "-include" flag with the .h file as its arg, and auto-appends ".gch" to
// automagically use the precompiled header in place of the original header. Of course in
// our case we'll only have the ".gch" file, which is alright; the ".h" isn't truly needed.
Path output = BuildTargets.getGenPath(getParams().getProjectFilesystem(), target, "%s.h.gch");
CompilerDelegate compilerDelegate = new CompilerDelegate(getPathResolver(), getCxxPlatform().getCompilerDebugPathSanitizer(), CxxSourceTypes.getCompiler(getCxxPlatform(), CxxSourceTypes.getPreprocessorOutputType(sourceType)).resolve(getResolver()), compilerFlags);
depsBuilder.add(compilerDelegate);
depsBuilder.add(headerPath);
BuildRuleParams params = getParams().withBuildTarget(target).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(depsBuilder.build()), Suppliers.ofInstance(ImmutableSortedSet.of()));
CxxPrecompiledHeader rule = new CxxPrecompiledHeader(params, output, preprocessorDelegate, compilerDelegate, compilerFlags, headerPath, sourceType, getCxxPlatform().getCompilerDebugPathSanitizer(), getCxxPlatform().getAssemblerDebugPathSanitizer());
getResolver().addToIndex(rule);
return rule;
}
use of com.facebook.buck.rules.BuildRuleParams in project buck by facebook.
the class JavaBinaryDescription method createBuildRule.
@Override
public <A extends Args> BuildRule createBuildRule(TargetGraph targetGraph, BuildRuleParams params, BuildRuleResolver resolver, A args) throws NoSuchBuildTargetException {
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
ImmutableMap<String, SourcePath> nativeLibraries = JavaLibraryRules.getNativeLibraries(params.getDeps(), cxxPlatform);
BuildRuleParams binaryParams = params;
// package it into the final fat JAR, so adjust it's params to use a flavored target.
if (!nativeLibraries.isEmpty()) {
binaryParams = params.withAppendedFlavor(FAT_JAR_INNER_JAR_FLAVOR);
}
// Construct the build rule to build the binary JAR.
ImmutableSet<JavaLibrary> transitiveClasspathDeps = JavaLibraryClasspathProvider.getClasspathDeps(binaryParams.getDeps());
ImmutableSet<SourcePath> transitiveClasspaths = JavaLibraryClasspathProvider.getClasspathsFromLibraries(transitiveClasspathDeps);
BuildRule rule = new JavaBinary(binaryParams.copyAppendingExtraDeps(transitiveClasspathDeps), javaOptions.getJavaRuntimeLauncher(), args.mainClass.orElse(null), args.manifestFile.orElse(null), args.mergeManifests.orElse(true), args.metaInfDirectory.orElse(null), args.blacklist, transitiveClasspathDeps, transitiveClasspaths, javaBuckConfig.shouldCacheBinaries());
// up the original binary JAR and any required native libraries.
if (!nativeLibraries.isEmpty()) {
BuildRule innerJarRule = rule;
resolver.addToIndex(innerJarRule);
SourcePath innerJar = innerJarRule.getSourcePathToOutput();
rule = new JarFattener(params.copyAppendingExtraDeps(Suppliers.<Iterable<BuildRule>>ofInstance(ruleFinder.filterBuildRuleInputs(ImmutableList.<SourcePath>builder().add(innerJar).addAll(nativeLibraries.values()).build()))), ruleFinder, javacOptions, innerJar, nativeLibraries, javaOptions.getJavaRuntimeLauncher());
}
return rule;
}
use of com.facebook.buck.rules.BuildRuleParams in project buck by facebook.
the class JavaTestDescription method createBuildRule.
@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, BuildRuleParams params, BuildRuleResolver resolver, A args) throws NoSuchBuildTargetException {
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
if (CalculateAbi.isAbiTarget(params.getBuildTarget())) {
BuildTarget testTarget = CalculateAbi.getLibraryTarget(params.getBuildTarget());
BuildRule testRule = resolver.requireRule(testTarget);
return CalculateAbi.of(params.getBuildTarget(), ruleFinder, params, Preconditions.checkNotNull(testRule.getSourcePathToOutput()));
}
SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
JavacOptions javacOptions = JavacOptionsFactory.create(templateJavacOptions, params, resolver, ruleFinder, args);
CxxLibraryEnhancement cxxLibraryEnhancement = new CxxLibraryEnhancement(params, args.useCxxLibraries, args.cxxLibraryWhitelist, resolver, ruleFinder, cxxPlatform);
params = cxxLibraryEnhancement.updatedParams;
BuildRuleParams testsLibraryParams = params.copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.<BuildRule>naturalOrder().addAll(params.getDeclaredDeps().get()).addAll(BuildRules.getExportedRules(Iterables.concat(params.getDeclaredDeps().get(), resolver.getAllRules(args.providedDeps)))).build()), Suppliers.ofInstance(ImmutableSortedSet.<BuildRule>naturalOrder().addAll(params.getExtraDeps().get()).addAll(ruleFinder.filterBuildRuleInputs(javacOptions.getInputs(ruleFinder))).build())).withAppendedFlavor(JavaTest.COMPILED_TESTS_LIBRARY_FLAVOR);
JavaLibrary testsLibrary = resolver.addToIndex(new DefaultJavaLibrary(testsLibraryParams, pathResolver, ruleFinder, args.srcs, ResourceValidator.validateResources(pathResolver, params.getProjectFilesystem(), args.resources), javacOptions.getGeneratedSourceFolderName(), args.proguardConfig, /* postprocessClassesCommands */
ImmutableList.of(), /* exportDeps */
ImmutableSortedSet.of(), resolver.getAllRules(args.providedDeps), JavaLibraryRules.getAbiInputs(resolver, testsLibraryParams.getDeps()), javacOptions.trackClassUsage(), /* additionalClasspathEntries */
ImmutableSet.of(), new JavacToJarStepFactory(javacOptions, JavacOptionsAmender.IDENTITY), args.resourcesRoot, args.manifestFile, args.mavenCoords, /* tests */
ImmutableSortedSet.of(), /* classesToRemoveFromJar */
ImmutableSet.of()));
return new JavaTest(params.copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of(testsLibrary)), Suppliers.ofInstance(ImmutableSortedSet.of())), pathResolver, testsLibrary, /* additionalClasspathEntries */
ImmutableSet.of(), args.labels, args.contacts, args.testType.orElse(TestType.JUNIT), javaOptions.getJavaRuntimeLauncher(), args.vmArgs, cxxLibraryEnhancement.nativeLibsEnvironment, args.testRuleTimeoutMs.map(Optional::of).orElse(defaultTestRuleTimeoutMs), args.testCaseTimeoutMs, args.env, args.getRunTestSeparately(), args.getForkMode(), args.stdOutLogLevel, args.stdErrLogLevel);
}
Aggregations