use of com.facebook.buck.rules.BuildTargetSourcePath in project buck by facebook.
the class ClasspathMacroExpanderTest method extractRuleKeyAppendables.
@Test
public void extractRuleKeyAppendables() throws Exception {
TargetNode<?, ?> depNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//exciting:dep")).addSrc(Paths.get("Dep.java")).build();
TargetNode<?, ?> ruleNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//exciting:target")).addSrc(Paths.get("Other.java")).addDep(depNode.getBuildTarget()).build();
TargetGraph targetGraph = TargetGraphFactory.newInstance(depNode, ruleNode);
BuildRuleResolver ruleResolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
BuildRule rule = ruleResolver.requireRule(ruleNode.getBuildTarget());
BuildRule dep = ruleResolver.requireRule(depNode.getBuildTarget());
BuildTarget forTarget = BuildTargetFactory.newInstance("//:rule");
Object ruleKeyAppendables = expander.extractRuleKeyAppendables(forTarget, createCellRoots(filesystem), ruleResolver, ImmutableList.of(rule.getBuildTarget().toString()));
assertThat(ruleKeyAppendables, Matchers.instanceOf(ImmutableSortedSet.class));
Set<BuildTarget> seenBuildTargets = new LinkedHashSet<>();
for (Object appendable : ((ImmutableSortedSet<?>) ruleKeyAppendables)) {
assertThat(appendable, Matchers.instanceOf(BuildTargetSourcePath.class));
seenBuildTargets.add(((BuildTargetSourcePath<?>) appendable).getTarget());
}
assertThat(seenBuildTargets, Matchers.equalTo(ImmutableSortedSet.of(rule.getBuildTarget(), dep.getBuildTarget())));
}
use of com.facebook.buck.rules.BuildTargetSourcePath in project buck by facebook.
the class AndroidNativeLibsPackageableGraphEnhancer method generateStripRules.
// Note: this method produces rules that will be shared between multiple apps,
// so be careful not to let information about this particular app slip into the definitions.
private static ImmutableMap<StripLinkable, StrippedObjectDescription> generateStripRules(BuildRuleParams buildRuleParams, SourcePathRuleFinder ruleFinder, BuildRuleResolver ruleResolver, BuildTarget appRuleTarget, ImmutableMap<NdkCxxPlatforms.TargetCpuType, NdkCxxPlatform> nativePlatforms, ImmutableMap<Pair<NdkCxxPlatforms.TargetCpuType, String>, SourcePath> libs) {
ImmutableMap.Builder<StripLinkable, StrippedObjectDescription> result = ImmutableMap.builder();
for (Map.Entry<Pair<NdkCxxPlatforms.TargetCpuType, String>, SourcePath> entry : libs.entrySet()) {
SourcePath sourcePath = entry.getValue();
NdkCxxPlatforms.TargetCpuType targetCpuType = entry.getKey().getFirst();
NdkCxxPlatform platform = Preconditions.checkNotNull(nativePlatforms.get(targetCpuType));
// To be safe, default to using the app rule target as the base for the strip rule.
// This will be used for stripping the C++ runtime. We could use something more easily
// shareable (like just using the app's containing directory, or even the repo root),
// but stripping the C++ runtime is pretty fast, so just keep the safe old behavior for now.
BuildTarget baseBuildTarget = appRuleTarget;
// to allow sharing the rule between all apps that depend on it.
if (sourcePath instanceof BuildTargetSourcePath) {
baseBuildTarget = ((BuildTargetSourcePath<?>) sourcePath).getTarget();
}
String sharedLibrarySoName = entry.getKey().getSecond();
BuildTarget targetForStripRule = BuildTarget.builder(baseBuildTarget).addFlavors(InternalFlavor.of("android-strip")).addFlavors(InternalFlavor.of(Flavor.replaceInvalidCharacters(sharedLibrarySoName))).addFlavors(InternalFlavor.of(Flavor.replaceInvalidCharacters(targetCpuType.name()))).build();
Optional<BuildRule> previouslyCreated = ruleResolver.getRuleOptional(targetForStripRule);
StripLinkable stripLinkable;
if (previouslyCreated.isPresent()) {
stripLinkable = (StripLinkable) previouslyCreated.get();
} else {
BuildRuleParams paramsForStripLinkable = buildRuleParams.withBuildTarget(targetForStripRule).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.<BuildRule>naturalOrder().addAll(ruleFinder.filterBuildRuleInputs(ImmutableList.of(sourcePath))).build()), Suppliers.ofInstance(ImmutableSortedSet.of()));
stripLinkable = new StripLinkable(paramsForStripLinkable, platform.getCxxPlatform().getStrip(), sourcePath, sharedLibrarySoName);
ruleResolver.addToIndex(stripLinkable);
}
result.put(stripLinkable, StrippedObjectDescription.builder().setSourcePath(stripLinkable.getSourcePathToOutput()).setStrippedObjectName(sharedLibrarySoName).setTargetCpuType(targetCpuType).build());
}
return result.build();
}
use of com.facebook.buck.rules.BuildTargetSourcePath in project buck by facebook.
the class AbstractCxxSourceRuleFactory method buildPrecompiledHeaderFromTemplateRule.
/**
* Build a PCH rule, given a {@code cxx_precompiled_header} rule.
*
* <p>
* We'll "instantiate" this PCH from this template, using the parameters (src, dependencies)
* from the template itself, plus the build flags that are used in the current build rule
* (so that this instantiated version uses compatible build flags and thus the PCH is guaranteed
* usable with this rule).
* </p>
*/
private CxxPrecompiledHeader buildPrecompiledHeaderFromTemplateRule(PreprocessorDelegateCacheValue preprocessorDelegateCacheValue, CxxSource.Type sourceType, ImmutableList<String> sourceFlags, SourcePath headerTargetPath) {
DepsBuilder depsBuilder = new DepsBuilder(getRuleFinder());
PreprocessorDelegate preprocessorDelegateForCxxRule = preprocessorDelegateCacheValue.getPreprocessorDelegate();
Preprocessor preprocessor = preprocessorDelegateForCxxRule.getPreprocessor();
BuildTarget pchTemplateTarget = ((BuildTargetSourcePath<?>) headerTargetPath).getTarget();
Optional<CxxPrecompiledHeaderTemplate> pchTemplateRuleOpt = getResolver().getRuleOptionalWithType(pchTemplateTarget, CxxPrecompiledHeaderTemplate.class);
Preconditions.checkState(pchTemplateRuleOpt.isPresent());
CxxPrecompiledHeaderTemplate pchTemplate = pchTemplateRuleOpt.get();
// Build compiler flags, taking from the source rule, but leaving out its deps.
// We just need the flags pertaining to PCH compatibility: language, PIC, macros, etc.
// and nothing related to the deps of this particular rule (hence 'getNonIncludePathFlags').
CxxToolFlags compilerFlags = CxxToolFlags.concat(preprocessorDelegateForCxxRule.getNonIncludePathFlags(/* no pch */
Optional.empty()), computeCompilerFlags(sourceType, sourceFlags));
// Now build a new pp-delegate specially for this PCH rule.
PreprocessorDelegate preprocessorDelegate = pchTemplate.buildPreprocessorDelegate(getCxxPlatform(), preprocessor, compilerFlags);
// Language needs to be part of the key, PCHs built under a different language are incompatible.
// (Replace `c++` with `cxx`; avoid default scrubbing which would make it the cryptic `c__`.)
final String langCode = sourceType.getLanguage().replaceAll("c\\+\\+", "cxx");
final String pchBaseID = "pch-" + langCode + "-" + preprocessorDelegateCacheValue.getBaseHash(compilerFlags);
for (BuildRule rule : pchTemplate.getDeps()) {
depsBuilder.add(rule);
}
depsBuilder.add(pchTemplate.requireAggregatedDepsRule(getCxxPlatform()));
depsBuilder.add(preprocessorDelegate);
return buildPrecompiledHeader(preprocessorDelegate, sourceType, compilerFlags, pchTemplate.sourcePath, depsBuilder, pchTemplateTarget.getUnflavoredBuildTarget(), ImmutableSortedSet.of(getCxxPlatform().getFlavor(), InternalFlavor.of(Flavor.replaceInvalidCharacters(pchBaseID))));
}
use of com.facebook.buck.rules.BuildTargetSourcePath in project buck by facebook.
the class PythonBinaryDescriptionTest method linkerFlagsUsingMergedNativeLinkStrategy.
@Test
public void linkerFlagsUsingMergedNativeLinkStrategy() throws Exception {
CxxLibraryBuilder cxxDepBuilder = new CxxLibraryBuilder(BuildTargetFactory.newInstance("//:dep")).setSrcs(ImmutableSortedSet.of(SourceWithFlags.of(new FakeSourcePath("dep.c"))));
CxxLibraryBuilder cxxBuilder = new CxxLibraryBuilder(BuildTargetFactory.newInstance("//:cxx")).setSrcs(ImmutableSortedSet.of(SourceWithFlags.of(new FakeSourcePath("cxx.c")))).setDeps(ImmutableSortedSet.of(cxxDepBuilder.getTarget()));
PythonBuckConfig config = new PythonBuckConfig(FakeBuckConfig.builder().build(), new AlwaysFoundExecutableFinder()) {
@Override
public NativeLinkStrategy getNativeLinkStrategy() {
return NativeLinkStrategy.MERGED;
}
};
PythonBinaryBuilder binaryBuilder = new PythonBinaryBuilder(BuildTargetFactory.newInstance("//:bin"), config, PythonTestUtils.PYTHON_PLATFORMS, CxxPlatformUtils.DEFAULT_PLATFORM, CxxPlatformUtils.DEFAULT_PLATFORMS);
binaryBuilder.setLinkerFlags(ImmutableList.of("-flag"));
binaryBuilder.setMainModule("main");
binaryBuilder.setDeps(ImmutableSortedSet.of(cxxBuilder.getTarget()));
BuildRuleResolver resolver = new BuildRuleResolver(TargetGraphFactory.newInstance(cxxDepBuilder.build(), cxxBuilder.build(), binaryBuilder.build()), new DefaultTargetNodeToBuildRuleTransformer());
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(resolver));
cxxDepBuilder.build(resolver);
cxxBuilder.build(resolver);
PythonBinary binary = binaryBuilder.build(resolver);
for (SourcePath path : binary.getComponents().getNativeLibraries().values()) {
CxxLink link = resolver.getRuleOptionalWithType(((BuildTargetSourcePath<?>) path).getTarget(), CxxLink.class).get();
assertThat(Arg.stringify(link.getArgs(), pathResolver), Matchers.hasItem("-flag"));
}
}
use of com.facebook.buck.rules.BuildTargetSourcePath in project buck by facebook.
the class DefaultRuleKeyFactory method newBuilder.
private RuleKeyBuilder<RuleKeyResult<RuleKey>> newBuilder() {
return new RuleKeyBuilder<RuleKeyResult<RuleKey>>(ruleFinder, pathResolver, hashLoader) {
private final ImmutableList.Builder<Object> deps = ImmutableList.builder();
private final ImmutableList.Builder<RuleKeyInput> inputs = ImmutableList.builder();
@Override
protected RuleKeyBuilder<RuleKeyResult<RuleKey>> setBuildRule(BuildRule rule) {
// Record the `BuildRule` as an immediate dep.
deps.add(rule);
return setBuildRuleKey(DefaultRuleKeyFactory.this.build(rule));
}
private RuleKeyResult<RuleKey> calculateRuleKeyAppendableKey(RuleKeyAppendable appendable) {
RuleKeyBuilder<RuleKeyResult<RuleKey>> subKeyBuilder = newBuilder();
appendable.appendToRuleKey(subKeyBuilder);
return subKeyBuilder.build();
}
@Override
protected RuleKeyBuilder<RuleKeyResult<RuleKey>> setAppendableRuleKey(RuleKeyAppendable appendable) {
// Record the `RuleKeyAppendable` as an immediate dep.
deps.add(appendable);
// Calculate the rule key for the rule key appendable.
RuleKey ruleKey = ruleKeyCache.get(appendable, this::calculateRuleKeyAppendableKey);
return setAppendableRuleKey(ruleKey);
}
@Override
protected RuleKeyBuilder<RuleKeyResult<RuleKey>> setSourcePath(SourcePath sourcePath) throws IOException {
if (sourcePath instanceof BuildTargetSourcePath) {
return setSourcePathAsRule((BuildTargetSourcePath<?>) sourcePath);
} else {
// Add `PathSourcePath`s to our tracked inputs.
pathResolver.getPathSourcePath(sourcePath).ifPresent(path -> inputs.add(RuleKeyInput.of(path.getFilesystem(), path.getRelativePath())));
return setSourcePathDirectly(sourcePath);
}
}
@Override
protected RuleKeyBuilder<RuleKeyResult<RuleKey>> setNonHashingSourcePath(SourcePath sourcePath) {
try {
// changes to dependent rulekeys.
return setSourcePath(sourcePath);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public RuleKeyResult<RuleKey> build() {
return new RuleKeyResult<>(buildRuleKey(), deps.build(), inputs.build());
}
};
}
Aggregations