Search in sources :

Example 21 with JavaLibrary

use of com.facebook.buck.jvm.java.JavaLibrary in project buck by facebook.

the class KotlinTestDescription 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(testTarget, ruleFinder, params, Preconditions.checkNotNull(testRule.getSourcePathToOutput()));
    }
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    JavacOptions javacOptions = JavacOptionsFactory.create(templateJavacOptions, params, resolver, ruleFinder, args);
    KotlincToJarStepFactory stepFactory = new KotlincToJarStepFactory(kotlinBuckConfig.getKotlinCompiler().get(), args.extraKotlincArguments);
    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)))).addAll(ruleFinder.filterBuildRuleInputs(javacOptions.getInputs(ruleFinder))).build()), params.getExtraDeps()).withAppendedFlavor(JavaTest.COMPILED_TESTS_LIBRARY_FLAVOR);
    JavaLibrary testsLibrary = resolver.addToIndex(new DefaultKotlinLibrary(testsLibraryParams, pathResolver, ruleFinder, args.srcs, ResourceValidator.validateResources(pathResolver, params.getProjectFilesystem(), args.resources), templateJavacOptions.getGeneratedSourceFolderName(), Optional.empty(), /* proguardConfig */
    ImmutableList.of(), /* postprocessClassesCommands */
    ImmutableSortedSet.of(), /* exportedDeps */
    ImmutableSortedSet.of(), /* providedDeps */
    JavaLibraryRules.getAbiInputs(resolver, testsLibraryParams.getDeps()), false, /* trackClassUsage */
    ImmutableSet.of(), /* additionalClasspathEntries */
    stepFactory, args.resourcesRoot, args.manifestFile, args.mavenCoords, ImmutableSortedSet.of(), /* tests */
    ImmutableSet.of()));
    return new KotlinTest(params.copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of(testsLibrary)), Suppliers.ofInstance(ImmutableSortedSet.of())), pathResolver, testsLibrary, ImmutableSet.<Either<SourcePath, Path>>of(kotlinBuckConfig.getPathToRuntimeJar()), args.labels, args.contacts, args.testType.orElse(TestType.JUNIT), javaOptions.getJavaRuntimeLauncher(), args.vmArgs, ImmutableMap.of(), /* nativeLibsEnvironment */
    args.testRuleTimeoutMs.map(Optional::of).orElse(defaultTestRuleTimeoutMs), args.testCaseTimeoutMs, args.env, args.getRunTestSeparately(), args.getForkMode(), args.stdOutLogLevel, args.stdErrLogLevel);
}
Also used : JavacOptions(com.facebook.buck.jvm.java.JavacOptions) SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) Optional(java.util.Optional) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePath(com.facebook.buck.rules.SourcePath) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) BuildTarget(com.facebook.buck.model.BuildTarget) BuildRule(com.facebook.buck.rules.BuildRule)

Example 22 with JavaLibrary

use of com.facebook.buck.jvm.java.JavaLibrary in project buck by facebook.

the class IjProject method write.

/**
   * Write the project to disk.
   *
   * @param runPostGenerationCleaner Whether or not the post-generation cleaner should be run.
   * @return set of {@link BuildTarget}s which should be built in order for the project to index
   *   correctly.
   * @throws IOException
   */
public ImmutableSet<BuildTarget> write(boolean runPostGenerationCleaner, boolean removeUnusedLibraries, boolean excludeArtifacts) throws IOException {
    final ImmutableSet.Builder<BuildTarget> requiredBuildTargets = ImmutableSet.builder();
    IjLibraryFactory libraryFactory = new DefaultIjLibraryFactory(new IjLibraryFactoryResolver() {

        @Override
        public Path getPath(SourcePath path) {
            Optional<BuildRule> rule = ruleFinder.getRule(path);
            if (rule.isPresent()) {
                requiredBuildTargets.add(rule.get().getBuildTarget());
            }
            return projectFilesystem.getRootPath().relativize(sourcePathResolver.getAbsolutePath(path));
        }

        @Override
        public Optional<SourcePath> getPathIfJavaLibrary(TargetNode<?, ?> targetNode) {
            BuildRule rule = buildRuleResolver.getRule(targetNode.getBuildTarget());
            if (!(rule instanceof JavaLibrary)) {
                return Optional.empty();
            }
            if (rule instanceof AndroidPrebuiltAar) {
                AndroidPrebuiltAar aarRule = (AndroidPrebuiltAar) rule;
                return Optional.ofNullable(aarRule.getBinaryJar());
            }
            requiredBuildTargets.add(rule.getBuildTarget());
            return Optional.ofNullable(rule.getSourcePathToOutput());
        }
    });
    IjModuleFactoryResolver moduleFactoryResolver = new IjModuleFactoryResolver() {

        @Override
        public Optional<Path> getDummyRDotJavaPath(TargetNode<?, ?> targetNode) {
            BuildTarget dummyRDotJavaTarget = AndroidLibraryGraphEnhancer.getDummyRDotJavaTarget(targetNode.getBuildTarget());
            Optional<BuildRule> dummyRDotJavaRule = buildRuleResolver.getRuleOptional(dummyRDotJavaTarget);
            if (dummyRDotJavaRule.isPresent()) {
                requiredBuildTargets.add(dummyRDotJavaTarget);
                return Optional.of(DummyRDotJava.getRDotJavaBinFolder(dummyRDotJavaTarget, projectFilesystem));
            }
            return Optional.empty();
        }

        @Override
        public Path getAndroidManifestPath(TargetNode<AndroidBinaryDescription.Arg, ?> targetNode) {
            return sourcePathResolver.getAbsolutePath(targetNode.getConstructorArg().manifest);
        }

        @Override
        public Optional<Path> getLibraryAndroidManifestPath(TargetNode<AndroidLibraryDescription.Arg, ?> targetNode) {
            Optional<SourcePath> manifestPath = targetNode.getConstructorArg().manifest;
            Optional<Path> defaultAndroidManifestPath = intellijConfig.getAndroidManifest().map(Path::toAbsolutePath);
            return manifestPath.map(sourcePathResolver::getAbsolutePath).map(Optional::of).orElse(defaultAndroidManifestPath);
        }

        @Override
        public Optional<Path> getProguardConfigPath(TargetNode<AndroidBinaryDescription.Arg, ?> targetNode) {
            return targetNode.getConstructorArg().proguardConfig.map(this::getRelativePathAndRecordRule);
        }

        @Override
        public Optional<Path> getAndroidResourcePath(TargetNode<AndroidResourceDescription.Arg, ?> targetNode) {
            return AndroidResourceDescription.getResDirectoryForProject(buildRuleResolver, targetNode).map(this::getRelativePathAndRecordRule);
        }

        @Override
        public Optional<Path> getAssetsPath(TargetNode<AndroidResourceDescription.Arg, ?> targetNode) {
            return AndroidResourceDescription.getAssetsDirectoryForProject(buildRuleResolver, targetNode).map(this::getRelativePathAndRecordRule);
        }

        @Override
        public Optional<Path> getAnnotationOutputPath(TargetNode<? extends JvmLibraryArg, ?> targetNode) {
            AnnotationProcessingParams annotationProcessingParams = targetNode.getConstructorArg().buildAnnotationProcessingParams(targetNode.getBuildTarget(), projectFilesystem, buildRuleResolver, ImmutableSet.of());
            if (annotationProcessingParams == null || annotationProcessingParams.isEmpty()) {
                return Optional.empty();
            }
            return Optional.ofNullable(annotationProcessingParams.getGeneratedSourceFolderName());
        }

        private Path getRelativePathAndRecordRule(SourcePath sourcePath) {
            requiredBuildTargets.addAll(OptionalCompat.asSet(ruleFinder.getRule(sourcePath).map(BuildRule::getBuildTarget)));
            return sourcePathResolver.getRelativePath(sourcePath);
        }
    };
    IjModuleGraph moduleGraph = IjModuleGraph.from(projectConfig, targetGraphAndTargets.getTargetGraph(), libraryFactory, new IjModuleFactory(projectFilesystem, moduleFactoryResolver, projectConfig, excludeArtifacts), aggregationMode);
    JavaPackageFinder parsingJavaPackageFinder = ParsingJavaPackageFinder.preparse(javaFileParser, projectFilesystem, IjProjectTemplateDataPreparer.createPackageLookupPathSet(moduleGraph), javaPackageFinder);
    IjProjectWriter writer = new IjProjectWriter(new IjProjectTemplateDataPreparer(parsingJavaPackageFinder, moduleGraph, projectFilesystem), projectConfig, projectFilesystem, moduleGraph);
    writer.write(runPostGenerationCleaner, removeUnusedLibraries);
    return requiredBuildTargets.build();
}
Also used : TargetNode(com.facebook.buck.rules.TargetNode) JvmLibraryArg(com.facebook.buck.jvm.java.JvmLibraryArg) SourcePath(com.facebook.buck.rules.SourcePath) JavaPackageFinder(com.facebook.buck.jvm.core.JavaPackageFinder) AndroidResourceDescription(com.facebook.buck.android.AndroidResourceDescription) ImmutableSet(com.google.common.collect.ImmutableSet) AndroidBinaryDescription(com.facebook.buck.android.AndroidBinaryDescription) BuildTarget(com.facebook.buck.model.BuildTarget) BuildRule(com.facebook.buck.rules.BuildRule) SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) Optional(java.util.Optional) AndroidLibraryDescription(com.facebook.buck.android.AndroidLibraryDescription) JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) AndroidPrebuiltAar(com.facebook.buck.android.AndroidPrebuiltAar) AnnotationProcessingParams(com.facebook.buck.jvm.java.AnnotationProcessingParams)

Example 23 with JavaLibrary

use of com.facebook.buck.jvm.java.JavaLibrary in project buck by facebook.

the class TestRunning method getReportCommand.

/**
   * Returns the ShellCommand object that is supposed to generate a code coverage report from data
   * obtained during the test run. This method will also generate a set of source paths to the class
   * files tested during the test run.
   */
private static Step getReportCommand(ImmutableSet<JavaLibrary> rulesUnderTest, DefaultJavaPackageFinder defaultJavaPackageFinder, JavaRuntimeLauncher javaRuntimeLauncher, ProjectFilesystem filesystem, SourcePathResolver sourcePathResolver, SourcePathRuleFinder ruleFinder, Path outputDirectory, CoverageReportFormat format, String title, boolean useIntermediateClassesDir, Optional<String> coverageIncludes, Optional<String> coverageExcludes) {
    ImmutableSet.Builder<String> srcDirectories = ImmutableSet.builder();
    ImmutableSet.Builder<Path> pathsToJars = ImmutableSet.builder();
    // Add all source directories of java libraries that we are testing to -sourcepath.
    for (JavaLibrary rule : rulesUnderTest) {
        ImmutableSet<String> sourceFolderPath = getPathToSourceFolders(rule, sourcePathResolver, ruleFinder, defaultJavaPackageFinder);
        if (!sourceFolderPath.isEmpty()) {
            srcDirectories.addAll(sourceFolderPath);
        }
        Path classesItem = null;
        if (useIntermediateClassesDir) {
            classesItem = DefaultJavaLibrary.getClassesDir(rule.getBuildTarget(), filesystem);
        } else {
            SourcePath path = rule.getSourcePathToOutput();
            if (path != null) {
                classesItem = sourcePathResolver.getRelativePath(path);
            }
        }
        if (classesItem == null) {
            continue;
        }
        pathsToJars.add(classesItem);
    }
    return new GenerateCodeCoverageReportStep(javaRuntimeLauncher, filesystem, srcDirectories.build(), pathsToJars.build(), outputDirectory, format, title, coverageIncludes, coverageExcludes);
}
Also used : Path(java.nio.file.Path) SourcePath(com.facebook.buck.rules.SourcePath) SourcePath(com.facebook.buck.rules.SourcePath) ImmutableSet(com.google.common.collect.ImmutableSet) JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) DefaultJavaLibrary(com.facebook.buck.jvm.java.DefaultJavaLibrary) GenerateCodeCoverageReportStep(com.facebook.buck.jvm.java.GenerateCodeCoverageReportStep)

Example 24 with JavaLibrary

use of com.facebook.buck.jvm.java.JavaLibrary in project buck by facebook.

the class GroovyTestDescription 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(defaultJavacOptions, params, resolver, ruleFinder, args).withAbiGenerationMode(JavacOptions.AbiGenerationMode.CLASS);
    GroovycToJarStepFactory stepFactory = new GroovycToJarStepFactory(groovyBuckConfig.getGroovyCompiler().get(), Optional.of(args.extraGroovycArguments), javacOptions);
    BuildRuleParams testsLibraryParams = params.copyAppendingExtraDeps(Iterables.concat(BuildRules.getExportedRules(Iterables.concat(params.getDeclaredDeps().get(), resolver.getAllRules(args.providedDeps))), ruleFinder.filterBuildRuleInputs(defaultJavacOptions.getInputs(ruleFinder)))).withAppendedFlavor(JavaTest.COMPILED_TESTS_LIBRARY_FLAVOR);
    JavaLibrary testsLibrary = resolver.addToIndex(new DefaultJavaLibrary(testsLibraryParams, pathResolver, ruleFinder, args.srcs, ResourceValidator.validateResources(pathResolver, params.getProjectFilesystem(), args.resources), defaultJavacOptions.getGeneratedSourceFolderName(), /* proguardConfig */
    Optional.empty(), /* postprocessClassesCommands */
    ImmutableList.of(), /* exportDeps */
    ImmutableSortedSet.of(), /* providedDeps */
    ImmutableSortedSet.of(), JavaLibraryRules.getAbiInputs(resolver, testsLibraryParams.getDeps()), /* trackClassUsage */
    false, /* additionalClasspathEntries */
    ImmutableSet.of(), stepFactory, args.resourcesRoot, args.manifestFile, args.mavenCoords, args.tests, args.removeClasses));
    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, /* nativeLibsEnvironment */
    ImmutableMap.of(), args.testRuleTimeoutMs.map(Optional::of).orElse(defaultTestRuleTimeoutMs), args.testCaseTimeoutMs, args.env, args.getRunTestSeparately(), args.getForkMode(), args.stdOutLogLevel, args.stdErrLogLevel);
}
Also used : JavacOptions(com.facebook.buck.jvm.java.JavacOptions) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) DefaultJavaLibrary(com.facebook.buck.jvm.java.DefaultJavaLibrary) Optional(java.util.Optional) BuildTarget(com.facebook.buck.model.BuildTarget) JavaTest(com.facebook.buck.jvm.java.JavaTest) BuildRule(com.facebook.buck.rules.BuildRule) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) DefaultJavaLibrary(com.facebook.buck.jvm.java.DefaultJavaLibrary)

Example 25 with JavaLibrary

use of com.facebook.buck.jvm.java.JavaLibrary in project buck by facebook.

the class GwtBinary method getClasspathEntries.

/**
   * The classpath entries needed to run {@code com.google.gwt.dev.Compiler} to build the module
   * specified by {@link #modules}.
   */
@VisibleForTesting
Iterable<Path> getClasspathEntries(SourcePathResolver pathResolver) {
    ImmutableSet.Builder<Path> classpathEntries = ImmutableSet.builder();
    classpathEntries.addAll(pathResolver.getAllAbsolutePaths(gwtModuleJars));
    for (BuildRule dep : getDeclaredDeps()) {
        if (!(dep instanceof JavaLibrary)) {
            continue;
        }
        JavaLibrary javaLibrary = (JavaLibrary) dep;
        for (SourcePath path : javaLibrary.getOutputClasspaths()) {
            classpathEntries.add(pathResolver.getAbsolutePath(path));
        }
    }
    return classpathEntries.build();
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) ImmutableSet(com.google.common.collect.ImmutableSet) JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) BuildRule(com.facebook.buck.rules.BuildRule) AbstractBuildRule(com.facebook.buck.rules.AbstractBuildRule) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

JavaLibrary (com.facebook.buck.jvm.java.JavaLibrary)33 BuildTarget (com.facebook.buck.model.BuildTarget)24 BuildRule (com.facebook.buck.rules.BuildRule)18 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)18 Path (java.nio.file.Path)17 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)15 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)14 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)13 Test (org.junit.Test)13 SourcePath (com.facebook.buck.rules.SourcePath)12 DefaultJavaLibrary (com.facebook.buck.jvm.java.DefaultJavaLibrary)11 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)11 TargetGraph (com.facebook.buck.rules.TargetGraph)9 ImmutableSet (com.google.common.collect.ImmutableSet)8 Optional (java.util.Optional)8 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)7 DefaultJavaPackageFinder (com.facebook.buck.jvm.java.DefaultJavaPackageFinder)6 PathSourcePath (com.facebook.buck.rules.PathSourcePath)6 JavaLibraryDescription (com.facebook.buck.jvm.java.JavaLibraryDescription)5 JavaTest (com.facebook.buck.jvm.java.JavaTest)5