Search in sources :

Example 6 with BuildTargetSourcePath

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())));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) BuildTarget(com.facebook.buck.model.BuildTarget) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) BuildRule(com.facebook.buck.rules.BuildRule) TargetGraph(com.facebook.buck.rules.TargetGraph) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) Test(org.junit.Test)

Example 7 with BuildTargetSourcePath

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();
}
Also used : ImmutableMap(com.google.common.collect.ImmutableMap) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) BuildTarget(com.facebook.buck.model.BuildTarget) BuildRule(com.facebook.buck.rules.BuildRule) Map(java.util.Map) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) ImmutableMap(com.google.common.collect.ImmutableMap) Pair(com.facebook.buck.model.Pair)

Example 8 with BuildTargetSourcePath

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))));
}
Also used : UnflavoredBuildTarget(com.facebook.buck.model.UnflavoredBuildTarget) BuildTarget(com.facebook.buck.model.BuildTarget) BuildRule(com.facebook.buck.rules.BuildRule) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath)

Example 9 with BuildTargetSourcePath

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"));
    }
}
Also used : FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) AlwaysFoundExecutableFinder(com.facebook.buck.io.AlwaysFoundExecutableFinder) SourcePath(com.facebook.buck.rules.SourcePath) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) PrebuiltCxxLibraryBuilder(com.facebook.buck.cxx.PrebuiltCxxLibraryBuilder) CxxLibraryBuilder(com.facebook.buck.cxx.CxxLibraryBuilder) CxxLink(com.facebook.buck.cxx.CxxLink) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Example 10 with BuildTargetSourcePath

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());
        }
    };
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) RuleKey(com.facebook.buck.rules.RuleKey) AbstractBuildRule(com.facebook.buck.rules.AbstractBuildRule) BuildRule(com.facebook.buck.rules.BuildRule) IOException(java.io.IOException) RuleKeyAppendable(com.facebook.buck.rules.RuleKeyAppendable) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath)

Aggregations

BuildTargetSourcePath (com.facebook.buck.rules.BuildTargetSourcePath)10 SourcePath (com.facebook.buck.rules.SourcePath)8 BuildTarget (com.facebook.buck.model.BuildTarget)7 BuildRule (com.facebook.buck.rules.BuildRule)7 PathSourcePath (com.facebook.buck.rules.PathSourcePath)5 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)4 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)4 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)4 UnflavoredBuildTarget (com.facebook.buck.model.UnflavoredBuildTarget)2 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)2 DefaultBuildTargetSourcePath (com.facebook.buck.rules.DefaultBuildTargetSourcePath)2 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)2 ExplicitBuildTargetSourcePath (com.facebook.buck.rules.ExplicitBuildTargetSourcePath)2 RuleKey (com.facebook.buck.rules.RuleKey)2 RuleKeyAppendable (com.facebook.buck.rules.RuleKeyAppendable)2 Path (java.nio.file.Path)2 Test (org.junit.Test)2 SourceTreePath (com.facebook.buck.apple.xcode.xcodeproj.SourceTreePath)1 CxxLibraryBuilder (com.facebook.buck.cxx.CxxLibraryBuilder)1 CxxLink (com.facebook.buck.cxx.CxxLink)1