Search in sources :

Example 1 with JavaLibrary

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

the class AndroidBinaryGraphEnhancer method createPreDexRulesForLibraries.

@VisibleForTesting
ImmutableMultimap<APKModule, DexProducedFromJavaLibrary> createPreDexRulesForLibraries(Iterable<BuildRule> additionalJavaLibrariesToDex, AndroidPackageableCollection packageableCollection) {
    Iterable<BuildTarget> additionalJavaLibraryTargets = FluentIterable.from(additionalJavaLibrariesToDex).transform(BuildRule::getBuildTarget);
    ImmutableMultimap.Builder<APKModule, DexProducedFromJavaLibrary> preDexDeps = ImmutableMultimap.builder();
    for (BuildTarget buildTarget : Iterables.concat(packageableCollection.getJavaLibrariesToDex(), additionalJavaLibraryTargets)) {
        Preconditions.checkState(!buildTargetsToExcludeFromDex.contains(buildTarget), "JavaLibrary should have been excluded from target to dex: %s", buildTarget);
        BuildRule libraryRule = ruleResolver.getRule(buildTarget);
        Preconditions.checkState(libraryRule instanceof JavaLibrary);
        JavaLibrary javaLibrary = (JavaLibrary) libraryRule;
        // resources, but export_deps is true), then there will not be anything to dx.
        if (javaLibrary.getSourcePathToOutput() == null) {
            continue;
        }
        // See whether the corresponding IntermediateDexRule has already been added to the
        // ruleResolver.
        BuildTarget originalTarget = javaLibrary.getBuildTarget();
        BuildTarget preDexTarget = BuildTarget.builder(originalTarget).addFlavors(DEX_FLAVOR).build();
        Optional<BuildRule> preDexRule = ruleResolver.getRuleOptional(preDexTarget);
        if (preDexRule.isPresent()) {
            preDexDeps.put(apkModuleGraph.findModuleForTarget(buildTarget), (DexProducedFromJavaLibrary) preDexRule.get());
            continue;
        }
        // Create the IntermediateDexRule and add it to both the ruleResolver and preDexDeps.
        BuildRuleParams paramsForPreDex = buildRuleParams.withBuildTarget(preDexTarget).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of(ruleResolver.getRule(javaLibrary.getBuildTarget()))), Suppliers.ofInstance(ImmutableSortedSet.of()));
        DexProducedFromJavaLibrary preDex = new DexProducedFromJavaLibrary(paramsForPreDex, javaLibrary);
        ruleResolver.addToIndex(preDex);
        preDexDeps.put(apkModuleGraph.findModuleForTarget(buildTarget), preDex);
    }
    return preDexDeps.build();
}
Also used : JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) DefaultJavaLibrary(com.facebook.buck.jvm.java.DefaultJavaLibrary) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) BuildTarget(com.facebook.buck.model.BuildTarget) BuildRule(com.facebook.buck.rules.BuildRule) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with JavaLibrary

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

the class AndroidBinaryGraphEnhancer method addBuildConfigDeps.

/**
   * If the user specified any android_build_config() rules, then we must add some build rules to
   * generate the production {@code BuildConfig.class} files and ensure that they are included in
   * the list of {@link AndroidPackageableCollection#getClasspathEntriesToDex}.
   */
public static ImmutableSortedSet<JavaLibrary> addBuildConfigDeps(BuildRuleParams originalParams, PackageType packageType, EnumSet<ExopackageMode> exopackageModes, BuildConfigFields buildConfigValues, Optional<SourcePath> buildConfigValuesFile, BuildRuleResolver ruleResolver, JavacOptions javacOptions, AndroidPackageableCollection packageableCollection) throws NoSuchBuildTargetException {
    ImmutableSortedSet.Builder<JavaLibrary> result = ImmutableSortedSet.naturalOrder();
    BuildConfigFields buildConfigConstants = BuildConfigFields.fromFields(ImmutableList.of(BuildConfigFields.Field.of("boolean", BuildConfigs.DEBUG_CONSTANT, String.valueOf(packageType != AndroidBinary.PackageType.RELEASE)), BuildConfigFields.Field.of("boolean", BuildConfigs.IS_EXO_CONSTANT, String.valueOf(!exopackageModes.isEmpty())), BuildConfigFields.Field.of("int", BuildConfigs.EXOPACKAGE_FLAGS, String.valueOf(ExopackageMode.toBitmask(exopackageModes)))));
    for (Map.Entry<String, BuildConfigFields> entry : packageableCollection.getBuildConfigs().entrySet()) {
        // Merge the user-defined constants with the APK-specific overrides.
        BuildConfigFields totalBuildConfigValues = BuildConfigFields.empty().putAll(entry.getValue()).putAll(buildConfigValues).putAll(buildConfigConstants);
        // Each enhanced dep needs a unique build target, so we parameterize the build target by the
        // Java package.
        String javaPackage = entry.getKey();
        Flavor flavor = InternalFlavor.of("buildconfig_" + javaPackage.replace('.', '_'));
        BuildTarget buildTargetWithFlavors = BuildTarget.builder(originalParams.getBuildTarget()).addFlavors(flavor).build();
        BuildRuleParams buildConfigParams = new BuildRuleParams(buildTargetWithFlavors, /* declaredDeps */
        Suppliers.ofInstance(ImmutableSortedSet.of()), /* extraDeps */
        Suppliers.ofInstance(ImmutableSortedSet.of()), originalParams.getProjectFilesystem(), originalParams.getCellRoots());
        JavaLibrary buildConfigJavaLibrary = AndroidBuildConfigDescription.createBuildRule(buildConfigParams, javaPackage, totalBuildConfigValues, buildConfigValuesFile, /* useConstantExpressions */
        true, javacOptions, ruleResolver);
        ruleResolver.addToIndex(buildConfigJavaLibrary);
        Preconditions.checkNotNull(buildConfigJavaLibrary.getSourcePathToOutput(), "%s must have an output file.", buildConfigJavaLibrary);
        result.add(buildConfigJavaLibrary);
    }
    return result.build();
}
Also used : JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) DefaultJavaLibrary(com.facebook.buck.jvm.java.DefaultJavaLibrary) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) BuildTarget(com.facebook.buck.model.BuildTarget) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Map(java.util.Map) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) ImmutableMap(com.google.common.collect.ImmutableMap) InternalFlavor(com.facebook.buck.model.InternalFlavor) Flavor(com.facebook.buck.model.Flavor) BuildConfigFields(com.facebook.buck.rules.coercer.BuildConfigFields)

Example 3 with JavaLibrary

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

the class AndroidBinaryGraphEnhancer method createAdditionalBuildables.

AndroidGraphEnhancementResult createAdditionalBuildables() throws NoSuchBuildTargetException {
    ImmutableSortedSet.Builder<BuildRule> enhancedDeps = ImmutableSortedSet.naturalOrder();
    enhancedDeps.addAll(originalDeps);
    ImmutableList.Builder<BuildRule> additionalJavaLibrariesBuilder = ImmutableList.builder();
    AndroidPackageableCollector collector = new AndroidPackageableCollector(originalBuildTarget, buildTargetsToExcludeFromDex, resourcesToExclude, apkModuleGraph);
    collector.addPackageables(AndroidPackageableCollector.getPackageableRules(originalDeps));
    AndroidPackageableCollection packageableCollection = collector.build();
    AndroidPackageableCollection.ResourceDetails resourceDetails = packageableCollection.getResourceDetails();
    AndroidNativeLibsGraphEnhancementResult nativeLibsEnhancementResult = nativeLibsEnhancer.enhance(packageableCollection);
    Optional<ImmutableMap<APKModule, CopyNativeLibraries>> copyNativeLibraries = nativeLibsEnhancementResult.getCopyNativeLibraries();
    if (copyNativeLibraries.isPresent()) {
        ruleResolver.addAllToIndex(copyNativeLibraries.get().values());
        enhancedDeps.addAll(copyNativeLibraries.get().values());
    }
    Optional<ImmutableSortedMap<String, String>> sonameMergeMap = nativeLibsEnhancementResult.getSonameMergeMap();
    if (sonameMergeMap.isPresent() && nativeLibraryMergeCodeGenerator.isPresent()) {
        BuildRule generatorRule = ruleResolver.getRule(nativeLibraryMergeCodeGenerator.get());
        GenerateCodeForMergedLibraryMap generateCodeForMergedLibraryMap = new GenerateCodeForMergedLibraryMap(buildRuleParams.withAppendedFlavor(GENERATE_NATIVE_LIB_MERGE_MAP_GENERATED_CODE_FLAVOR).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of(generatorRule)), Suppliers.ofInstance(ImmutableSortedSet.of())), sonameMergeMap.get(), generatorRule);
        ruleResolver.addToIndex(generateCodeForMergedLibraryMap);
        BuildRuleParams paramsForCompileGenCode = buildRuleParams.withAppendedFlavor(COMPILE_NATIVE_LIB_MERGE_MAP_GENERATED_CODE_FLAVOR).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of(generateCodeForMergedLibraryMap)), Suppliers.ofInstance(ImmutableSortedSet.of()));
        DefaultJavaLibrary compileMergedNativeLibMapGenCode = new DefaultJavaLibrary(paramsForCompileGenCode, pathResolver, ruleFinder, ImmutableSet.of(generateCodeForMergedLibraryMap.getSourcePathToOutput()), /* resources */
        ImmutableSet.of(), javacOptions.getGeneratedSourceFolderName(), /* proguardConfig */
        Optional.empty(), /* postprocessClassesCommands */
        ImmutableList.of(), /* exportedDeps */
        ImmutableSortedSet.of(), /* providedDeps */
        ImmutableSortedSet.of(), JavaLibraryRules.getAbiInputs(ruleResolver, paramsForCompileGenCode.getDeps()), /* trackClassUsage */
        false, /* additionalClasspathEntries */
        ImmutableSet.of(), new JavacToJarStepFactory(// to 6 in their .buckconfig.
        javacOptions.withSourceLevel("7").withTargetLevel("7"), JavacOptionsAmender.IDENTITY), /* resourcesRoot */
        Optional.empty(), /* manifest file */
        Optional.empty(), /* mavenCoords */
        Optional.empty(), ImmutableSortedSet.of(), /* classesToRemoveFromJar */
        ImmutableSet.of());
        ruleResolver.addToIndex(compileMergedNativeLibMapGenCode);
        additionalJavaLibrariesBuilder.add(compileMergedNativeLibMapGenCode);
        enhancedDeps.add(compileMergedNativeLibMapGenCode);
    }
    ImmutableSortedSet<BuildRule> resourceRules = getTargetsAsRules(resourceDetails.getResourcesWithNonEmptyResDir());
    ImmutableCollection<BuildRule> rulesWithResourceDirectories = ruleFinder.filterBuildRuleInputs(resourceDetails.getResourceDirectories());
    FilteredResourcesProvider filteredResourcesProvider;
    boolean needsResourceFiltering = resourceFilter.isEnabled() || resourceCompressionMode.isStoreStringsAsAssets() || !locales.isEmpty();
    if (needsResourceFiltering) {
        BuildRuleParams paramsForResourcesFilter = buildRuleParams.withAppendedFlavor(RESOURCES_FILTER_FLAVOR).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.<BuildRule>naturalOrder().addAll(resourceRules).addAll(rulesWithResourceDirectories).build()), Suppliers.ofInstance(ImmutableSortedSet.of()));
        ResourcesFilter resourcesFilter = new ResourcesFilter(paramsForResourcesFilter, resourceDetails.getResourceDirectories(), ImmutableSet.copyOf(resourceDetails.getWhitelistedStringDirectories()), locales, resourceCompressionMode, resourceFilter);
        ruleResolver.addToIndex(resourcesFilter);
        filteredResourcesProvider = resourcesFilter;
        enhancedDeps.add(resourcesFilter);
        resourceRules = ImmutableSortedSet.of(resourcesFilter);
    } else {
        filteredResourcesProvider = new IdentityResourcesProvider(resourceDetails.getResourceDirectories().stream().map(pathResolver::getRelativePath).collect(MoreCollectors.toImmutableList()));
    }
    // Create the AaptPackageResourcesBuildable.
    BuildRuleParams paramsForAaptPackageResources = buildRuleParams.withAppendedFlavor(AAPT_PACKAGE_FLAVOR).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of()), Suppliers.ofInstance(ImmutableSortedSet.of()));
    AaptPackageResources aaptPackageResources = new AaptPackageResources(paramsForAaptPackageResources, ruleFinder, ruleResolver, manifest, filteredResourcesProvider, getTargetsAsResourceDeps(resourceDetails.getResourcesWithNonEmptyResDir()), getTargetsAsRules(resourceDetails.getResourcesWithEmptyResButNonEmptyAssetsDir()), packageableCollection.getAssetsDirectories(), resourceUnionPackage, shouldBuildStringSourceMap, skipCrunchPngs, includesVectorDrawables, bannedDuplicateResourceTypes, manifestEntries);
    ruleResolver.addToIndex(aaptPackageResources);
    enhancedDeps.add(aaptPackageResources);
    Optional<PackageStringAssets> packageStringAssets = Optional.empty();
    if (resourceCompressionMode.isStoreStringsAsAssets()) {
        BuildRuleParams paramsForPackageStringAssets = buildRuleParams.withAppendedFlavor(PACKAGE_STRING_ASSETS_FLAVOR).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.<BuildRule>naturalOrder().add(aaptPackageResources).addAll(resourceRules).addAll(rulesWithResourceDirectories).addAll(Iterables.filter(ImmutableList.of(filteredResourcesProvider), BuildRule.class)).build()), Suppliers.ofInstance(ImmutableSortedSet.of()));
        packageStringAssets = Optional.of(new PackageStringAssets(paramsForPackageStringAssets, locales, filteredResourcesProvider, aaptPackageResources));
        ruleResolver.addToIndex(packageStringAssets.get());
        enhancedDeps.add(packageStringAssets.get());
    }
    // already been added to the APK under test.
    if (packageType != PackageType.INSTRUMENTED) {
        ImmutableSortedSet<JavaLibrary> buildConfigDepsRules = addBuildConfigDeps(buildRuleParams, packageType, exopackageModes, buildConfigValues, buildConfigValuesFile, ruleResolver, javacOptions, packageableCollection);
        enhancedDeps.addAll(buildConfigDepsRules);
        additionalJavaLibrariesBuilder.addAll(buildConfigDepsRules);
    }
    ImmutableList<BuildRule> additionalJavaLibraries = additionalJavaLibrariesBuilder.build();
    ImmutableMultimap<APKModule, DexProducedFromJavaLibrary> preDexedLibraries = ImmutableMultimap.of();
    if (shouldPreDex) {
        preDexedLibraries = createPreDexRulesForLibraries(// TODO(dreiss): Put R.java here.
        additionalJavaLibraries, packageableCollection);
    }
    // Create rule to trim uber R.java sources.
    Collection<DexProducedFromJavaLibrary> preDexedLibrariesForResourceIdFiltering = trimResourceIds ? preDexedLibraries.values() : ImmutableList.of();
    BuildRuleParams paramsForTrimUberRDotJava = buildRuleParams.withAppendedFlavor(TRIM_UBER_R_DOT_JAVA_FLAVOR).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.<BuildRule>naturalOrder().add(aaptPackageResources).addAll(preDexedLibrariesForResourceIdFiltering).build()), Suppliers.ofInstance(ImmutableSortedSet.of()));
    TrimUberRDotJava trimUberRDotJava = new TrimUberRDotJava(paramsForTrimUberRDotJava, aaptPackageResources, preDexedLibrariesForResourceIdFiltering, keepResourcePattern);
    ruleResolver.addToIndex(trimUberRDotJava);
    // Create rule to compile uber R.java sources.
    BuildRuleParams paramsForCompileUberRDotJava = buildRuleParams.withAppendedFlavor(COMPILE_UBER_R_DOT_JAVA_FLAVOR).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of(trimUberRDotJava)), Suppliers.ofInstance(ImmutableSortedSet.of()));
    JavaLibrary compileUberRDotJava = new DefaultJavaLibrary(paramsForCompileUberRDotJava, pathResolver, ruleFinder, ImmutableSet.of(trimUberRDotJava.getSourcePathToOutput()), /* resources */
    ImmutableSet.of(), javacOptions.getGeneratedSourceFolderName(), /* proguardConfig */
    Optional.empty(), /* postprocessClassesCommands */
    ImmutableList.of(), /* exportedDeps */
    ImmutableSortedSet.of(), /* providedDeps */
    ImmutableSortedSet.of(), // we can just use its output as the ABI.
    JavaLibraryRules.getAbiInputs(ruleResolver, paramsForCompileUberRDotJava.getDeps()), /* trackClassUsage */
    false, /* additionalClasspathEntries */
    ImmutableSet.of(), new JavacToJarStepFactory(javacOptions.withSourceLevel("7").withTargetLevel("7"), JavacOptionsAmender.IDENTITY), /* resourcesRoot */
    Optional.empty(), /* manifest file */
    Optional.empty(), /* mavenCoords */
    Optional.empty(), ImmutableSortedSet.of(), /* classesToRemoveFromJar */
    ImmutableSet.of());
    ruleResolver.addToIndex(compileUberRDotJava);
    // Create rule to dex uber R.java sources.
    BuildRuleParams paramsForDexUberRDotJava = buildRuleParams.withAppendedFlavor(DEX_UBER_R_DOT_JAVA_FLAVOR).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of(compileUberRDotJava)), Suppliers.ofInstance(ImmutableSortedSet.of()));
    DexProducedFromJavaLibrary dexUberRDotJava = new DexProducedFromJavaLibrary(paramsForDexUberRDotJava, compileUberRDotJava);
    ruleResolver.addToIndex(dexUberRDotJava);
    Optional<PreDexMerge> preDexMerge = Optional.empty();
    if (shouldPreDex) {
        preDexMerge = Optional.of(createPreDexMergeRule(preDexedLibraries, dexUberRDotJava));
        enhancedDeps.add(preDexMerge.get());
    } else {
        enhancedDeps.addAll(getTargetsAsRules(packageableCollection.getJavaLibrariesToDex()));
        // If not pre-dexing, AndroidBinary needs to ProGuard and/or dex the compiled R.java.
        enhancedDeps.add(compileUberRDotJava);
    }
    // Add dependencies on all the build rules generating third-party JARs.  This is mainly to
    // correctly capture deps when a prebuilt_jar forwards the output from another build rule.
    enhancedDeps.addAll(ruleFinder.filterBuildRuleInputs(packageableCollection.getPathsToThirdPartyJars()));
    Optional<ComputeExopackageDepsAbi> computeExopackageDepsAbi = Optional.empty();
    if (!exopackageModes.isEmpty()) {
        BuildRuleParams paramsForComputeExopackageAbi = buildRuleParams.withAppendedFlavor(CALCULATE_ABI_FLAVOR).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(enhancedDeps.build()), Suppliers.ofInstance(ImmutableSortedSet.of()));
        computeExopackageDepsAbi = Optional.of(new ComputeExopackageDepsAbi(paramsForComputeExopackageAbi, exopackageModes, packageableCollection, copyNativeLibraries, preDexMerge));
        ruleResolver.addToIndex(computeExopackageDepsAbi.get());
        enhancedDeps.add(computeExopackageDepsAbi.get());
    }
    return AndroidGraphEnhancementResult.builder().setPackageableCollection(packageableCollection).setAaptPackageResources(aaptPackageResources).setCompiledUberRDotJava(compileUberRDotJava).setCopyNativeLibraries(copyNativeLibraries).setPackageStringAssets(packageStringAssets).setPreDexMerge(preDexMerge).setComputeExopackageDepsAbi(computeExopackageDepsAbi).setClasspathEntriesToDex(ImmutableSet.<SourcePath>builder().addAll(packageableCollection.getClasspathEntriesToDex()).addAll(additionalJavaLibraries.stream().map(BuildRule::getSourcePathToOutput).collect(MoreCollectors.toImmutableList())).build()).setFinalDeps(enhancedDeps.build()).setAPKModuleGraph(apkModuleGraph).build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) DefaultJavaLibrary(com.facebook.buck.jvm.java.DefaultJavaLibrary) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) BuildRule(com.facebook.buck.rules.BuildRule) JavacToJarStepFactory(com.facebook.buck.jvm.java.JavacToJarStepFactory) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) ImmutableMap(com.google.common.collect.ImmutableMap) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) DefaultJavaLibrary(com.facebook.buck.jvm.java.DefaultJavaLibrary)

Example 4 with JavaLibrary

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

the class TestRunning method runTests.

@SuppressWarnings("PMD.EmptyCatchBlock")
public static int runTests(final CommandRunnerParams params, Iterable<TestRule> tests, ExecutionContext executionContext, final TestRunningOptions options, ListeningExecutorService service, BuildEngine buildEngine, final StepRunner stepRunner, SourcePathResolver sourcePathResolver, SourcePathRuleFinder ruleFinder) throws IOException, ExecutionException, InterruptedException {
    ImmutableSet<JavaLibrary> rulesUnderTestForCoverage;
    // If needed, we first run instrumentation on the class files.
    if (options.isCodeCoverageEnabled()) {
        rulesUnderTestForCoverage = getRulesUnderTest(tests);
        if (!rulesUnderTestForCoverage.isEmpty()) {
            try {
                // We'll use the filesystem of the first rule under test. This will fail if there are any
                // tests from a different repo, but it'll help us bootstrap ourselves to being able to
                // support multiple repos
                // TODO(t8220837): Support tests in multiple repos
                JavaLibrary library = rulesUnderTestForCoverage.iterator().next();
                stepRunner.runStepForBuildTarget(executionContext, new MakeCleanDirectoryStep(library.getProjectFilesystem(), JacocoConstants.getJacocoOutputDir(library.getProjectFilesystem())), Optional.empty());
            } catch (StepFailedException e) {
                params.getBuckEventBus().post(ConsoleEvent.severe(Throwables.getRootCause(e).getLocalizedMessage()));
                return 1;
            }
        }
    } else {
        rulesUnderTestForCoverage = ImmutableSet.of();
    }
    final ImmutableSet<String> testTargets = FluentIterable.from(tests).transform(BuildRule::getBuildTarget).transform(Object::toString).toSet();
    final int totalNumberOfTests = Iterables.size(tests);
    params.getBuckEventBus().post(TestRunEvent.started(options.isRunAllTests(), options.getTestSelectorList(), options.shouldExplainTestSelectorList(), testTargets));
    // Start running all of the tests. The result of each java_test() rule is represented as a
    // ListenableFuture.
    List<ListenableFuture<TestResults>> results = Lists.newArrayList();
    TestRuleKeyFileHelper testRuleKeyFileHelper = new TestRuleKeyFileHelper(buildEngine);
    final AtomicInteger lastReportedTestSequenceNumber = new AtomicInteger();
    final List<TestRun> separateTestRuns = Lists.newArrayList();
    List<TestRun> parallelTestRuns = Lists.newArrayList();
    for (final TestRule test : tests) {
        // Determine whether the test needs to be executed.
        final Callable<TestResults> resultsInterpreter = getCachingCallable(test.interpretTestResults(executionContext, /*isUsingTestSelectors*/
        !options.getTestSelectorList().isEmpty()));
        boolean isTestRunRequired;
        isTestRunRequired = isTestRunRequiredForTest(test, buildEngine, executionContext, testRuleKeyFileHelper, options.getTestResultCacheMode(), resultsInterpreter, !options.getTestSelectorList().isEmpty(), !options.getEnvironmentOverrides().isEmpty());
        final Map<String, UUID> testUUIDMap = new HashMap<>();
        final AtomicReference<TestStatusMessageEvent.Started> currentTestStatusMessageEvent = new AtomicReference<>();
        TestRule.TestReportingCallback testReportingCallback = new TestRule.TestReportingCallback() {

            @Override
            public void testsDidBegin() {
                LOG.debug("Tests for rule %s began", test.getBuildTarget());
            }

            @Override
            public void statusDidBegin(TestStatusMessage didBeginMessage) {
                LOG.debug("Test status did begin: %s", didBeginMessage);
                TestStatusMessageEvent.Started startedEvent = TestStatusMessageEvent.started(didBeginMessage);
                TestStatusMessageEvent.Started previousEvent = currentTestStatusMessageEvent.getAndSet(startedEvent);
                Preconditions.checkState(previousEvent == null, "Received begin status before end status (%s)", previousEvent);
                params.getBuckEventBus().post(startedEvent);
                String message = didBeginMessage.getMessage();
                if (message.toLowerCase().contains("debugger")) {
                    executionContext.getStdErr().println(executionContext.getAnsi().asWarningText(message));
                }
            }

            @Override
            public void statusDidEnd(TestStatusMessage didEndMessage) {
                LOG.debug("Test status did end: %s", didEndMessage);
                TestStatusMessageEvent.Started previousEvent = currentTestStatusMessageEvent.getAndSet(null);
                Preconditions.checkState(previousEvent != null, "Received end status before begin status (%s)", previousEvent);
                params.getBuckEventBus().post(TestStatusMessageEvent.finished(previousEvent, didEndMessage));
            }

            @Override
            public void testDidBegin(String testCaseName, String testName) {
                LOG.debug("Test rule %s test case %s test name %s began", test.getBuildTarget(), testCaseName, testName);
                UUID testUUID = UUID.randomUUID();
                // UUID is immutable and thread-safe as of Java 7, so it's
                // safe to stash in a map and use later:
                //
                // http://bugs.java.com/view_bug.do?bug_id=6611830
                testUUIDMap.put(testCaseName + ":" + testName, testUUID);
                params.getBuckEventBus().post(TestSummaryEvent.started(testUUID, testCaseName, testName));
            }

            @Override
            public void testDidEnd(TestResultSummary testResultSummary) {
                LOG.debug("Test rule %s test did end: %s", test.getBuildTarget(), testResultSummary);
                UUID testUUID = testUUIDMap.get(testResultSummary.getTestCaseName() + ":" + testResultSummary.getTestName());
                Preconditions.checkNotNull(testUUID);
                params.getBuckEventBus().post(TestSummaryEvent.finished(testUUID, testResultSummary));
            }

            @Override
            public void testsDidEnd(List<TestCaseSummary> testCaseSummaries) {
                LOG.debug("Test rule %s tests did end: %s", test.getBuildTarget(), testCaseSummaries);
            }
        };
        List<Step> steps;
        if (isTestRunRequired) {
            params.getBuckEventBus().post(IndividualTestEvent.started(testTargets));
            ImmutableList.Builder<Step> stepsBuilder = ImmutableList.builder();
            Preconditions.checkState(buildEngine.isRuleBuilt(test.getBuildTarget()));
            List<Step> testSteps = test.runTests(executionContext, options, sourcePathResolver, testReportingCallback);
            if (!testSteps.isEmpty()) {
                stepsBuilder.addAll(testSteps);
                stepsBuilder.add(testRuleKeyFileHelper.createRuleKeyInDirStep(test));
            }
            steps = stepsBuilder.build();
        } else {
            steps = ImmutableList.of();
        }
        TestRun testRun = TestRun.of(test, steps, getStatusTransformingCallable(isTestRunRequired, resultsInterpreter), testReportingCallback);
        // commands because the rule is cached, but its results must still be processed.
        if (test.runTestSeparately()) {
            LOG.debug("Running test %s in serial", test);
            separateTestRuns.add(testRun);
        } else {
            LOG.debug("Running test %s in parallel", test);
            parallelTestRuns.add(testRun);
        }
    }
    for (TestRun testRun : parallelTestRuns) {
        ListenableFuture<TestResults> testResults = runStepsAndYieldResult(stepRunner, executionContext, testRun.getSteps(), testRun.getTestResultsCallable(), testRun.getTest().getBuildTarget(), params.getBuckEventBus(), service);
        results.add(transformTestResults(params, testResults, testRun.getTest(), testRun.getTestReportingCallback(), testTargets, lastReportedTestSequenceNumber, totalNumberOfTests));
    }
    ListenableFuture<List<TestResults>> parallelTestStepsFuture = Futures.allAsList(results);
    final List<TestResults> completedResults = Lists.newArrayList();
    final ListeningExecutorService directExecutorService = MoreExecutors.newDirectExecutorService();
    ListenableFuture<Void> uberFuture = MoreFutures.addListenableCallback(parallelTestStepsFuture, new FutureCallback<List<TestResults>>() {

        @Override
        public void onSuccess(List<TestResults> parallelTestResults) {
            LOG.debug("Parallel tests completed, running separate tests...");
            completedResults.addAll(parallelTestResults);
            List<ListenableFuture<TestResults>> separateResultsList = Lists.newArrayList();
            for (TestRun testRun : separateTestRuns) {
                separateResultsList.add(transformTestResults(params, runStepsAndYieldResult(stepRunner, executionContext, testRun.getSteps(), testRun.getTestResultsCallable(), testRun.getTest().getBuildTarget(), params.getBuckEventBus(), directExecutorService), testRun.getTest(), testRun.getTestReportingCallback(), testTargets, lastReportedTestSequenceNumber, totalNumberOfTests));
            }
            ListenableFuture<List<TestResults>> serialResults = Futures.allAsList(separateResultsList);
            try {
                completedResults.addAll(serialResults.get());
            } catch (ExecutionException e) {
                LOG.error(e, "Error fetching serial test results");
                throw new HumanReadableException(e, "Error fetching serial test results");
            } catch (InterruptedException e) {
                LOG.error(e, "Interrupted fetching serial test results");
                try {
                    serialResults.cancel(true);
                } catch (CancellationException ignored) {
                // Rethrow original InterruptedException instead.
                }
                Thread.currentThread().interrupt();
                throw new HumanReadableException(e, "Test cancelled");
            }
            LOG.debug("Done running serial tests.");
        }

        @Override
        public void onFailure(Throwable e) {
            LOG.error(e, "Parallel tests failed, not running serial tests");
            throw new HumanReadableException(e, "Parallel tests failed");
        }
    }, directExecutorService);
    try {
        // Block until all the tests have finished running.
        uberFuture.get();
    } catch (ExecutionException e) {
        e.printStackTrace(params.getConsole().getStdErr());
        return 1;
    } catch (InterruptedException e) {
        try {
            uberFuture.cancel(true);
        } catch (CancellationException ignored) {
        // Rethrow original InterruptedException instead.
        }
        Thread.currentThread().interrupt();
        throw e;
    }
    params.getBuckEventBus().post(TestRunEvent.finished(testTargets, completedResults));
    // Write out the results as XML, if requested.
    Optional<String> path = options.getPathToXmlTestOutput();
    if (path.isPresent()) {
        try (Writer writer = Files.newWriter(new File(path.get()), Charsets.UTF_8)) {
            writeXmlOutput(completedResults, writer);
        }
    }
    // Generate the code coverage report.
    if (options.isCodeCoverageEnabled() && !rulesUnderTestForCoverage.isEmpty()) {
        try {
            JavaBuckConfig javaBuckConfig = params.getBuckConfig().getView(JavaBuckConfig.class);
            DefaultJavaPackageFinder defaultJavaPackageFinder = javaBuckConfig.createDefaultJavaPackageFinder();
            stepRunner.runStepForBuildTarget(executionContext, getReportCommand(rulesUnderTestForCoverage, defaultJavaPackageFinder, javaBuckConfig.getDefaultJavaOptions().getJavaRuntimeLauncher(), params.getCell().getFilesystem(), sourcePathResolver, ruleFinder, JacocoConstants.getJacocoOutputDir(params.getCell().getFilesystem()), options.getCoverageReportFormat(), options.getCoverageReportTitle(), javaBuckConfig.getDefaultJavacOptions().getSpoolMode() == JavacOptions.SpoolMode.INTERMEDIATE_TO_DISK, options.getCoverageIncludes(), options.getCoverageExcludes()), Optional.empty());
        } catch (StepFailedException e) {
            params.getBuckEventBus().post(ConsoleEvent.severe(Throwables.getRootCause(e).getLocalizedMessage()));
            return 1;
        }
    }
    boolean failures = Iterables.any(completedResults, results1 -> {
        LOG.debug("Checking result %s for failure", results1);
        return !results1.isSuccess();
    });
    return failures ? TEST_FAILURES_EXIT_CODE : 0;
}
Also used : HashMap(java.util.HashMap) TestResults(com.facebook.buck.test.TestResults) JavaBuckConfig(com.facebook.buck.jvm.java.JavaBuckConfig) DefaultJavaPackageFinder(com.facebook.buck.jvm.java.DefaultJavaPackageFinder) StepFailedException(com.facebook.buck.step.StepFailedException) BuildRule(com.facebook.buck.rules.BuildRule) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) UUID(java.util.UUID) TestResultSummary(com.facebook.buck.test.TestResultSummary) TestRule(com.facebook.buck.rules.TestRule) TestStatusMessageEvent(com.facebook.buck.rules.TestStatusMessageEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CancellationException(java.util.concurrent.CancellationException) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) File(java.io.File) ImmutableList(com.google.common.collect.ImmutableList) Step(com.facebook.buck.step.Step) GenerateCodeCoverageReportStep(com.facebook.buck.jvm.java.GenerateCodeCoverageReportStep) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) ExecutionException(java.util.concurrent.ExecutionException) AtomicReference(java.util.concurrent.atomic.AtomicReference) TestStatusMessage(com.facebook.buck.test.TestStatusMessage) JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) DefaultJavaLibrary(com.facebook.buck.jvm.java.DefaultJavaLibrary) HumanReadableException(com.facebook.buck.util.HumanReadableException) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) StringWriter(java.io.StringWriter)

Example 5 with JavaLibrary

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

the class TestRunning method getRulesUnderTest.

/**
   * Generates the set of Java library rules under test.
   */
private static ImmutableSet<JavaLibrary> getRulesUnderTest(Iterable<TestRule> tests) {
    ImmutableSet.Builder<JavaLibrary> rulesUnderTest = ImmutableSet.builder();
    // Gathering all rules whose source will be under test.
    for (TestRule test : tests) {
        if (test instanceof JavaTest) {
            // Look at the transitive dependencies for `tests` attribute that refers to this test.
            JavaTest javaTest = (JavaTest) test;
            ImmutableSet<JavaLibrary> transitiveDeps = javaTest.getCompiledTestsLibrary().getTransitiveClasspathDeps();
            for (JavaLibrary dep : transitiveDeps) {
                if (dep instanceof JavaLibraryWithTests) {
                    ImmutableSortedSet<BuildTarget> depTests = ((JavaLibraryWithTests) dep).getTests();
                    if (depTests.contains(test.getBuildTarget())) {
                        rulesUnderTest.add(dep);
                    }
                }
            }
        }
    }
    return rulesUnderTest.build();
}
Also used : TestRule(com.facebook.buck.rules.TestRule) JavaLibraryWithTests(com.facebook.buck.jvm.java.JavaLibraryWithTests) ImmutableSet(com.google.common.collect.ImmutableSet) JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) DefaultJavaLibrary(com.facebook.buck.jvm.java.DefaultJavaLibrary) BuildTarget(com.facebook.buck.model.BuildTarget) JavaTest(com.facebook.buck.jvm.java.JavaTest)

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