Search in sources :

Example 26 with BuildRule

use of com.facebook.buck.rules.BuildRule in project buck by facebook.

the class HaskellLibraryDescription method createBuildRule.

@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, final BuildRuleParams params, final BuildRuleResolver resolver, final A args) throws NoSuchBuildTargetException {
    final BuildTarget buildTarget = params.getBuildTarget();
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    final SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    // See if we're building a particular "type" and "platform" of this library, and if so, extract
    // them from the flavors attached to the build target.
    Optional<Map.Entry<Flavor, Type>> type = LIBRARY_TYPE.getFlavorAndValue(buildTarget);
    Optional<CxxPlatform> cxxPlatform = cxxPlatforms.getValue(buildTarget);
    if (type.isPresent()) {
        Preconditions.checkState(cxxPlatform.isPresent());
        // Get the base build, without any flavors referring to the library type or platform.
        BuildTarget baseTarget = params.getBuildTarget().withoutFlavors(Sets.union(Type.FLAVOR_VALUES, cxxPlatforms.getFlavors()));
        switch(type.get().getValue()) {
            case PACKAGE_SHARED:
            case PACKAGE_STATIC:
            case PACKAGE_STATIC_PIC:
                Linker.LinkableDepType depType;
                if (type.get().getValue().equals(Type.PACKAGE_SHARED)) {
                    depType = Linker.LinkableDepType.SHARED;
                } else if (type.get().getValue().equals(Type.PACKAGE_STATIC)) {
                    depType = Linker.LinkableDepType.STATIC;
                } else {
                    depType = Linker.LinkableDepType.STATIC_PIC;
                }
                return requirePackage(baseTarget, params, resolver, pathResolver, ruleFinder, cxxPlatform.get(), args, depType);
            case SHARED:
                return requireSharedLibrary(baseTarget, params, resolver, pathResolver, ruleFinder, cxxPlatform.get(), args);
            case STATIC_PIC:
            case STATIC:
                return requireStaticLibrary(baseTarget, params, resolver, pathResolver, ruleFinder, cxxPlatform.get(), args, type.get().getValue() == Type.STATIC ? Linker.LinkableDepType.STATIC : Linker.LinkableDepType.STATIC_PIC);
        }
        throw new IllegalStateException(String.format("%s: unexpected type `%s`", params.getBuildTarget(), type.get().getValue()));
    }
    return new HaskellLibrary(params) {

        @Override
        public HaskellCompileInput getCompileInput(CxxPlatform cxxPlatform, Linker.LinkableDepType depType) throws NoSuchBuildTargetException {
            HaskellPackageRule rule = requirePackage(getBaseBuildTarget(getBuildTarget()), params, resolver, pathResolver, ruleFinder, cxxPlatform, args, depType);
            return HaskellCompileInput.builder().addPackages(rule.getPackage()).build();
        }

        @Override
        public Iterable<? extends NativeLinkable> getNativeLinkableDeps() {
            return ImmutableList.of();
        }

        @Override
        public Iterable<? extends NativeLinkable> getNativeLinkableExportedDeps() {
            return FluentIterable.from(getDeps()).filter(NativeLinkable.class);
        }

        @Override
        public NativeLinkableInput getNativeLinkableInput(CxxPlatform cxxPlatform, Linker.LinkableDepType type) throws NoSuchBuildTargetException {
            Iterable<com.facebook.buck.rules.args.Arg> linkArgs;
            switch(type) {
                case STATIC:
                case STATIC_PIC:
                    Archive archive = requireStaticLibrary(getBaseBuildTarget(getBuildTarget()), params, resolver, pathResolver, ruleFinder, cxxPlatform, args, type);
                    linkArgs = args.linkWhole.orElse(false) ? cxxPlatform.getLd().resolve(resolver).linkWhole(archive.toArg()) : ImmutableList.of(archive.toArg());
                    break;
                case SHARED:
                    BuildRule rule = requireSharedLibrary(getBaseBuildTarget(getBuildTarget()), params, resolver, pathResolver, ruleFinder, cxxPlatform, args);
                    linkArgs = ImmutableList.of(SourcePathArg.of(rule.getSourcePathToOutput()));
                    break;
                default:
                    throw new IllegalStateException();
            }
            return NativeLinkableInput.builder().addAllArgs(linkArgs).build();
        }

        @Override
        public Linkage getPreferredLinkage(CxxPlatform cxxPlatform) {
            return args.preferredLinkage.orElse(Linkage.ANY);
        }

        @Override
        public ImmutableMap<String, SourcePath> getSharedLibraries(CxxPlatform cxxPlatform) throws NoSuchBuildTargetException {
            ImmutableMap.Builder<String, SourcePath> libs = ImmutableMap.builder();
            String sharedLibrarySoname = CxxDescriptionEnhancer.getSharedLibrarySoname(Optional.empty(), getBuildTarget(), cxxPlatform);
            BuildRule sharedLibraryBuildRule = requireSharedLibrary(getBaseBuildTarget(getBuildTarget()), params, resolver, pathResolver, ruleFinder, cxxPlatform, args);
            libs.put(sharedLibrarySoname, sharedLibraryBuildRule.getSourcePathToOutput());
            return libs.build();
        }
    };
}
Also used : Archive(com.facebook.buck.cxx.Archive) CxxPlatform(com.facebook.buck.cxx.CxxPlatform) Linker(com.facebook.buck.cxx.Linker) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) ImmutableMap(com.google.common.collect.ImmutableMap) SourcePath(com.facebook.buck.rules.SourcePath) BuildTarget(com.facebook.buck.model.BuildTarget) SourcePathArg(com.facebook.buck.rules.args.SourcePathArg) BuildRule(com.facebook.buck.rules.BuildRule)

Example 27 with BuildRule

use of com.facebook.buck.rules.BuildRule in project buck by facebook.

the class GoDescriptors method getTestMainGenerator.

static Tool getTestMainGenerator(GoBuckConfig goBuckConfig, BuildRuleParams sourceParams, BuildRuleResolver resolver) throws NoSuchBuildTargetException {
    Optional<Tool> configTool = goBuckConfig.getGoTestMainGenerator(resolver);
    if (configTool.isPresent()) {
        return configTool.get();
    }
    // TODO(mikekap): Make a single test main gen, rather than one per test. The generator itself
    // doesn't vary per test.
    BuildTarget generatorTarget = sourceParams.getBuildTarget().withFlavors(InternalFlavor.of("make-test-main-gen"));
    Optional<BuildRule> generator = resolver.getRuleOptional(generatorTarget);
    if (generator.isPresent()) {
        return ((BinaryBuildRule) generator.get()).getExecutableCommand();
    }
    BuildTarget generatorSourceTarget = sourceParams.getBuildTarget().withAppendedFlavors(InternalFlavor.of("test-main-gen-source"));
    WriteFile writeFile = resolver.addToIndex(new WriteFile(sourceParams.withBuildTarget(generatorSourceTarget).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of()), Suppliers.ofInstance(ImmutableSortedSet.of())), extractTestMainGenerator(), BuildTargets.getGenPath(sourceParams.getProjectFilesystem(), generatorSourceTarget, "%s/main.go"), /* executable */
    false));
    GoBinary binary = resolver.addToIndex(createGoBinaryRule(sourceParams.withBuildTarget(generatorTarget).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of()), Suppliers.ofInstance(ImmutableSortedSet.of(writeFile))), resolver, goBuckConfig, ImmutableSet.of(writeFile.getSourcePathToOutput()), ImmutableList.of(), ImmutableList.of(), ImmutableList.of(), goBuckConfig.getDefaultPlatform()));
    return binary.getExecutableCommand();
}
Also used : WriteFile(com.facebook.buck.file.WriteFile) BuildTarget(com.facebook.buck.model.BuildTarget) BinaryBuildRule(com.facebook.buck.rules.BinaryBuildRule) BuildRule(com.facebook.buck.rules.BuildRule) BinaryBuildRule(com.facebook.buck.rules.BinaryBuildRule) Tool(com.facebook.buck.rules.Tool)

Example 28 with BuildRule

use of com.facebook.buck.rules.BuildRule in project buck by facebook.

the class GoTestDescription method createTestMainRule.

private GoBinary createTestMainRule(BuildRuleParams params, final BuildRuleResolver resolver, Arg args, GoPlatform platform) throws NoSuchBuildTargetException {
    Path packageName = getGoPackageName(resolver, params.getBuildTarget(), args);
    BuildRuleParams testTargetParams = params.withAppendedFlavor(TEST_LIBRARY_FLAVOR);
    BuildRule testLibrary = new NoopBuildRule(testTargetParams);
    resolver.addToIndex(testLibrary);
    BuildRule generatedTestMain = requireTestMainGenRule(params, resolver, args.srcs, packageName);
    GoBinary testMain = GoDescriptors.createGoBinaryRule(params.withAppendedFlavor(InternalFlavor.of("test-main")).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of(testLibrary)), Suppliers.ofInstance(ImmutableSortedSet.of(generatedTestMain))), resolver, goBuckConfig, ImmutableSet.of(generatedTestMain.getSourcePathToOutput()), args.compilerFlags, args.assemblerFlags, args.linkerFlags, platform);
    resolver.addToIndex(testMain);
    return testMain;
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) NoopBuildRule(com.facebook.buck.rules.NoopBuildRule) BuildRule(com.facebook.buck.rules.BuildRule) NoopBuildRule(com.facebook.buck.rules.NoopBuildRule)

Example 29 with BuildRule

use of com.facebook.buck.rules.BuildRule in project buck by facebook.

the class HalideLibraryDescription method createHalideStaticLibrary.

private BuildRule createHalideStaticLibrary(BuildRuleParams params, BuildRuleResolver ruleResolver, SourcePathRuleFinder ruleFinder, CxxPlatform platform, Arg args) throws NoSuchBuildTargetException {
    if (!isPlatformSupported(args, platform)) {
        return new NoopBuildRule(params);
    }
    BuildRule halideCompile = ruleResolver.requireRule(params.getBuildTarget().withFlavors(HALIDE_COMPILE_FLAVOR, platform.getFlavor()));
    BuildTarget buildTarget = halideCompile.getBuildTarget();
    return Archive.from(params.getBuildTarget(), params, ruleFinder, platform, cxxBuckConfig.getArchiveContents(), CxxDescriptionEnhancer.getStaticLibraryPath(params.getProjectFilesystem(), params.getBuildTarget(), platform.getFlavor(), CxxSourceRuleFactory.PicType.PIC, platform.getStaticLibraryExtension()), ImmutableList.of(new ExplicitBuildTargetSourcePath(buildTarget, HalideCompile.objectOutputPath(buildTarget, params.getProjectFilesystem(), args.functionName))));
}
Also used : NoopBuildRule(com.facebook.buck.rules.NoopBuildRule) BuildTarget(com.facebook.buck.model.BuildTarget) BuildRule(com.facebook.buck.rules.BuildRule) NoopBuildRule(com.facebook.buck.rules.NoopBuildRule) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath)

Example 30 with BuildRule

use of com.facebook.buck.rules.BuildRule in project buck by facebook.

the class CsharpLibrary method resolveReferences.

private ImmutableList<Either<Path, String>> resolveReferences(SourcePathResolver pathResolver, ImmutableList<Either<BuildRule, String>> refs) {
    ImmutableList.Builder<Either<Path, String>> resolved = ImmutableList.builder();
    for (Either<BuildRule, String> ref : refs) {
        if (ref.isLeft()) {
            // TODO(shs96c): Do this in the constructor? Or the Description?
            BuildRule rule = ref.getLeft();
            Preconditions.checkArgument(rule instanceof CsharpLibrary || rule instanceof PrebuiltDotnetLibrary);
            SourcePath outputPath = Preconditions.checkNotNull(rule.getSourcePathToOutput());
            resolved.add(Either.ofLeft(pathResolver.getAbsolutePath(outputPath)));
        } else {
            resolved.add(Either.ofRight(ref.getRight()));
        }
    }
    return resolved.build();
}
Also used : ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) ImmutableList(com.google.common.collect.ImmutableList) Either(com.facebook.buck.model.Either) AbstractBuildRule(com.facebook.buck.rules.AbstractBuildRule) BuildRule(com.facebook.buck.rules.BuildRule)

Aggregations

BuildRule (com.facebook.buck.rules.BuildRule)361 BuildTarget (com.facebook.buck.model.BuildTarget)199 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)190 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)189 Test (org.junit.Test)175 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)165 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)162 SourcePath (com.facebook.buck.rules.SourcePath)118 Path (java.nio.file.Path)95 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)70 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)70 FakeBuildRule (com.facebook.buck.rules.FakeBuildRule)66 TargetGraph (com.facebook.buck.rules.TargetGraph)63 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)61 FakeSourcePath (com.facebook.buck.rules.FakeSourcePath)61 ImmutableList (com.google.common.collect.ImmutableList)58 PathSourcePath (com.facebook.buck.rules.PathSourcePath)51 ImmutableSet (com.google.common.collect.ImmutableSet)45 ImmutableMap (com.google.common.collect.ImmutableMap)44 RuleKey (com.facebook.buck.rules.RuleKey)43