Search in sources :

Example 1 with PrebuiltJar

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

the class Project method getIntellijNameForRule.

/**
   * @param rule whose corresponding IntelliJ module name will be returned
   * @param basePathToAliasMap may be null if rule is a {@link PrebuiltJar}
   */
private String getIntellijNameForRule(BuildRule rule, @Nullable Map<Path, String> basePathToAliasMap) {
    // Get basis for the library/module name.
    String name;
    if (rule instanceof PrebuiltJar) {
        PrebuiltJar prebuiltJar = (PrebuiltJar) rule;
        Path absolutePath = resolver.getAbsolutePath(prebuiltJar.getSourcePathToOutput());
        Path relativePath = projectFilesystem.getRootPath().relativize(absolutePath);
        String binaryJarUnixPath = MorePaths.pathWithUnixSeparators(relativePath);
        return getIntellijNameForBinaryJar(binaryJarUnixPath);
    } else {
        Path basePath = rule.getBuildTarget().getBasePath();
        if (basePathToAliasMap != null && basePathToAliasMap.containsKey(basePath)) {
            name = Preconditions.checkNotNull(basePathToAliasMap.get(basePath));
        } else {
            name = MorePaths.pathWithUnixSeparators(rule.getBuildTarget().getBasePath());
            name = name.replace('/', '_');
            // Must add a prefix to ensure that name is non-empty.
            name = "module_" + name;
        }
        // Normalize name.
        return normalizeIntelliJName(name);
    }
}
Also used : Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) PrebuiltJar(com.facebook.buck.jvm.java.PrebuiltJar)

Example 2 with PrebuiltJar

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

the class Project method writeJsonConfig.

private void writeJsonConfig(File jsonTempFile, List<SerializableModule> modules) throws IOException {
    List<SerializablePrebuiltJarRule> libraries = Lists.newArrayListWithCapacity(libraryJars.size());
    for (BuildRule libraryJar : libraryJars) {
        Preconditions.checkState(libraryJar instanceof PrebuiltJar);
        String name = getIntellijNameForRule(libraryJar, null);
        PrebuiltJar prebuiltJar = (PrebuiltJar) libraryJar;
        Path binaryJarAbsolutePath = resolver.getAbsolutePath(prebuiltJar.getSourcePathToOutput());
        String binaryJar = MorePaths.pathWithUnixSeparators(projectFilesystem.relativize(binaryJarAbsolutePath));
        String sourceJar = prebuiltJar.getSourceJar().map(resolver::getAbsolutePath).map(projectFilesystem::relativize).map(MorePaths::pathWithUnixSeparators).orElse(null);
        String javadocUrl = prebuiltJar.getJavadocUrl().orElse(null);
        libraries.add(new SerializablePrebuiltJarRule(name, binaryJar, sourceJar, javadocUrl));
    }
    List<SerializableAndroidAar> aars = Lists.newArrayListWithCapacity(androidAars.size());
    for (BuildRule aar : androidAars) {
        Preconditions.checkState(aar instanceof AndroidPrebuiltAar);
        AndroidPrebuiltAar preBuiltAar = (AndroidPrebuiltAar) aar;
        String name = getIntellijNameForAar(preBuiltAar);
        aars.add(createSerializableAndroidAar(name, preBuiltAar));
    }
    writeJsonConfig(jsonTempFile, modules, libraries, aars);
}
Also used : Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) SerializableAndroidAar.createSerializableAndroidAar(com.facebook.buck.jvm.java.intellij.SerializableAndroidAar.createSerializableAndroidAar) AndroidPrebuiltAar(com.facebook.buck.android.AndroidPrebuiltAar) BuildRule(com.facebook.buck.rules.BuildRule) PrebuiltJar(com.facebook.buck.jvm.java.PrebuiltJar)

Example 3 with PrebuiltJar

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

the class Project method walkRuleAndAdd.

/**
   * Walks the dependencies of a build rule and adds the appropriate DependentModules to the
   * specified dependencies collection. All library dependencies will be added before any module
   * dependencies. See {@code ProjectTest#testThatJarsAreListedBeforeModules()} for details on why
   * this behavior is important.
   */
@SuppressWarnings("PMD.LooseCoupling")
private void walkRuleAndAdd(final BuildRule rule, final boolean isForTests, final LinkedHashSet<SerializableDependentModule> dependencies, @Nullable final BuildRule srcTarget) {
    final Path basePathForRule = rule.getBuildTarget().getBasePath();
    Set<BuildRule> targetsToWalk;
    if (rule instanceof JavaTest) {
        targetsToWalk = ((JavaTest) rule).getCompiledTestsLibrary().getDeps();
    } else {
        targetsToWalk = rule.getDeps();
    }
    new AbstractBreadthFirstTraversal<BuildRule>(targetsToWalk) {

        private final LinkedHashSet<SerializableDependentModule> librariesToAdd = Sets.newLinkedHashSet();

        private final LinkedHashSet<SerializableDependentModule> modulesToAdd = Sets.newLinkedHashSet();

        @Override
        public ImmutableSet<BuildRule> visit(BuildRule dep) {
            // Hack: we don't want uber R.java to show up in the deps that IntelliJ sees.
            if (dep.getBuildTarget().toString().endsWith("_uber_r_dot_java")) {
                return ImmutableSet.of();
            }
            ImmutableSet<BuildRule> depsToVisit;
            if (rule.getProperties().is(PACKAGING) || dep instanceof AndroidResource || dep == rule) {
                depsToVisit = dep.getDeps();
            } else if (dep.getProperties().is(LIBRARY) && dep instanceof ExportDependencies) {
                depsToVisit = ((ExportDependencies) dep).getExportedDeps();
            } else {
                depsToVisit = ImmutableSet.of();
            }
            // should contain the union of :lib and :test's deps as dependent modules.
            if (isForTests && depsToVisit.isEmpty() && dep.getBuildTarget().getBasePath().equals(basePathForRule) && !dep.equals(srcTarget)) {
                depsToVisit = dep.getDeps();
            }
            SerializableDependentModule dependentModule;
            if (androidAars.contains(dep)) {
                AndroidPrebuiltAar aar = androidAars.getParentAar(dep);
                dependentModule = SerializableDependentModule.newLibrary(aar.getBuildTarget(), getIntellijNameForAar(aar));
            } else if (dep instanceof PrebuiltJar) {
                libraryJars.add(dep);
                String libraryName = getIntellijNameForRule(dep);
                dependentModule = SerializableDependentModule.newLibrary(dep.getBuildTarget(), libraryName);
            } else if (dep instanceof AndroidPrebuiltAar) {
                androidAars.add((AndroidPrebuiltAar) dep);
                String libraryName = getIntellijNameForAar(dep);
                dependentModule = SerializableDependentModule.newLibrary(dep.getBuildTarget(), libraryName);
            } else if ((dep instanceof CxxLibrary) || (dep instanceof NdkLibrary) || (dep instanceof JavaLibrary) || (dep instanceof AndroidResource)) {
                String moduleName = getIntellijNameForRule(dep);
                dependentModule = SerializableDependentModule.newModule(dep.getBuildTarget(), moduleName);
            } else {
                return depsToVisit;
            }
            if (librariesToAdd.contains(dependentModule) || modulesToAdd.contains(dependentModule)) {
                return depsToVisit;
            }
            if (isForTests) {
                dependentModule.scope = "TEST";
            } else {
                // If the dependentModule has already been added in the "TEST" scope, then it should be
                // removed and then re-added using the current (compile) scope.
                String currentScope = dependentModule.scope;
                dependentModule.scope = "TEST";
                if (dependencies.contains(dependentModule)) {
                    dependencies.remove(dependentModule);
                }
                dependentModule.scope = currentScope;
            }
            // dependencies collection once the traversal is complete in the onComplete() method.
            if (dependentModule.isLibrary()) {
                librariesToAdd.add(dependentModule);
            } else {
                modulesToAdd.add(dependentModule);
            }
            return depsToVisit;
        }

        @Override
        protected void onComplete() {
            dependencies.addAll(librariesToAdd);
            dependencies.addAll(modulesToAdd);
        }
    }.start();
}
Also used : Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) CxxLibrary(com.facebook.buck.cxx.CxxLibrary) ExportDependencies(com.facebook.buck.rules.ExportDependencies) AndroidResource(com.facebook.buck.android.AndroidResource) PrebuiltJar(com.facebook.buck.jvm.java.PrebuiltJar) ImmutableSet(com.google.common.collect.ImmutableSet) JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) JavaTest(com.facebook.buck.jvm.java.JavaTest) AndroidPrebuiltAar(com.facebook.buck.android.AndroidPrebuiltAar) BuildRule(com.facebook.buck.rules.BuildRule) NdkLibrary(com.facebook.buck.android.NdkLibrary)

Example 4 with PrebuiltJar

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

the class AndroidPrebuiltAarDescription method createBuildRule.

@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, BuildRuleParams params, BuildRuleResolver buildRuleResolver, A args) throws NoSuchBuildTargetException {
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(buildRuleResolver);
    ImmutableSet<Flavor> flavors = params.getBuildTarget().getFlavors();
    if (flavors.contains(AAR_UNZIP_FLAVOR)) {
        Preconditions.checkState(flavors.size() == 1);
        BuildRuleParams unzipAarParams = params.copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of()), Suppliers.ofInstance(ImmutableSortedSet.copyOf(ruleFinder.filterBuildRuleInputs(args.aar))));
        return new UnzipAar(unzipAarParams, args.aar);
    }
    BuildRule unzipAarRule = buildRuleResolver.requireRule(params.getBuildTarget().withFlavors(AAR_UNZIP_FLAVOR));
    Preconditions.checkState(unzipAarRule instanceof UnzipAar, "aar_unzip flavor created rule of unexpected type %s for target %s", unzipAarRule.getClass(), params.getBuildTarget());
    UnzipAar unzipAar = (UnzipAar) unzipAarRule;
    if (CalculateAbi.isAbiTarget(params.getBuildTarget())) {
        return CalculateAbi.of(params.getBuildTarget(), ruleFinder, params, new ExplicitBuildTargetSourcePath(unzipAar.getBuildTarget(), unzipAar.getPathToClassesJar()));
    }
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    Iterable<PrebuiltJar> javaDeps = Iterables.concat(Iterables.filter(buildRuleResolver.getAllRules(args.deps), PrebuiltJar.class), Iterables.transform(Iterables.filter(buildRuleResolver.getAllRules(args.deps), AndroidPrebuiltAar.class), AndroidPrebuiltAar::getPrebuiltJar));
    if (flavors.contains(AAR_PREBUILT_JAR_FLAVOR)) {
        Preconditions.checkState(flavors.size() == 1, "Expected only flavor to be %s but also found %s", AAR_PREBUILT_JAR_FLAVOR, flavors);
        BuildRuleParams buildRuleParams = params.copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.copyOf(javaDeps)), Suppliers.ofInstance(ImmutableSortedSet.of(unzipAar)));
        return new PrebuiltJar(/* params */
        buildRuleParams, /* resolver */
        pathResolver, /* binaryJar */
        new ExplicitBuildTargetSourcePath(unzipAar.getBuildTarget(), unzipAar.getPathToClassesJar()), /* sourceJar */
        Optional.empty(), /* gwtJar */
        Optional.empty(), /* javadocUrl */
        Optional.empty(), /* mavenCoords */
        Optional.empty(), /* provided */
        false);
    }
    BuildRule prebuiltJarRule = buildRuleResolver.requireRule(BuildTargets.createFlavoredBuildTarget(params.getBuildTarget().checkUnflavored(), AAR_PREBUILT_JAR_FLAVOR));
    Preconditions.checkState(prebuiltJarRule instanceof PrebuiltJar, "%s flavor created rule of unexpected type %s for target %s", AAR_PREBUILT_JAR_FLAVOR, unzipAarRule.getType(), params.getBuildTarget());
    PrebuiltJar prebuiltJar = (PrebuiltJar) prebuiltJarRule;
    Preconditions.checkArgument(flavors.isEmpty(), "Unexpected flavors for android_prebuilt_aar: %s", flavors);
    BuildRuleParams androidLibraryParams = params.copyReplacingDeclaredAndExtraDeps(/* declaredDeps */
    Suppliers.ofInstance(ImmutableSortedSet.of(prebuiltJar)), /* extraDeps */
    Suppliers.ofInstance(ImmutableSortedSet.of(unzipAar)));
    return new AndroidPrebuiltAar(androidLibraryParams, /* resolver */
    pathResolver, ruleFinder, /* proguardConfig */
    new ExplicitBuildTargetSourcePath(unzipAar.getBuildTarget(), unzipAar.getProguardConfig()), /* nativeLibsDirectory */
    new ExplicitBuildTargetSourcePath(unzipAar.getBuildTarget(), unzipAar.getNativeLibsDirectory()), /* prebuiltJar */
    prebuiltJar, /* unzipRule */
    unzipAar, /* javacOptions */
    javacOptions, new JavacToJarStepFactory(javacOptions, new BootClasspathAppender()), /* exportedDeps */
    javaDeps, JavaLibraryRules.getAbiInputs(buildRuleResolver, androidLibraryParams.getDeps()));
}
Also used : JavacToJarStepFactory(com.facebook.buck.jvm.java.JavacToJarStepFactory) PrebuiltJar(com.facebook.buck.jvm.java.PrebuiltJar) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) InternalFlavor(com.facebook.buck.model.InternalFlavor) Flavor(com.facebook.buck.model.Flavor) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) BuildRule(com.facebook.buck.rules.BuildRule) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath)

Aggregations

PrebuiltJar (com.facebook.buck.jvm.java.PrebuiltJar)4 BuildRule (com.facebook.buck.rules.BuildRule)3 PathSourcePath (com.facebook.buck.rules.PathSourcePath)3 SourcePath (com.facebook.buck.rules.SourcePath)3 Path (java.nio.file.Path)3 AndroidPrebuiltAar (com.facebook.buck.android.AndroidPrebuiltAar)2 AndroidResource (com.facebook.buck.android.AndroidResource)1 NdkLibrary (com.facebook.buck.android.NdkLibrary)1 CxxLibrary (com.facebook.buck.cxx.CxxLibrary)1 JavaLibrary (com.facebook.buck.jvm.java.JavaLibrary)1 JavaTest (com.facebook.buck.jvm.java.JavaTest)1 JavacToJarStepFactory (com.facebook.buck.jvm.java.JavacToJarStepFactory)1 SerializableAndroidAar.createSerializableAndroidAar (com.facebook.buck.jvm.java.intellij.SerializableAndroidAar.createSerializableAndroidAar)1 Flavor (com.facebook.buck.model.Flavor)1 InternalFlavor (com.facebook.buck.model.InternalFlavor)1 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)1 ExplicitBuildTargetSourcePath (com.facebook.buck.rules.ExplicitBuildTargetSourcePath)1 ExportDependencies (com.facebook.buck.rules.ExportDependencies)1 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)1 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)1