use of com.facebook.buck.jvm.java.JavaLibrary in project buck by facebook.
the class AndroidAarDescription method createBuildRule.
@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, BuildRuleParams originalBuildRuleParams, BuildRuleResolver resolver, A args) throws NoSuchBuildTargetException {
UnflavoredBuildTarget originalBuildTarget = originalBuildRuleParams.getBuildTarget().checkUnflavored();
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
ImmutableList.Builder<BuildRule> aarExtraDepsBuilder = ImmutableList.<BuildRule>builder().addAll(originalBuildRuleParams.getExtraDeps().get());
/* android_manifest */
AndroidManifestDescription.Arg androidManifestArgs = androidManifestDescription.createUnpopulatedConstructorArg();
androidManifestArgs.skeleton = args.manifestSkeleton;
androidManifestArgs.deps = args.deps;
BuildRuleParams androidManifestParams = originalBuildRuleParams.withBuildTarget(BuildTargets.createFlavoredBuildTarget(originalBuildTarget, AAR_ANDROID_MANIFEST_FLAVOR));
AndroidManifest manifest = androidManifestDescription.createBuildRule(targetGraph, androidManifestParams, resolver, androidManifestArgs);
aarExtraDepsBuilder.add(resolver.addToIndex(manifest));
final APKModuleGraph apkModuleGraph = new APKModuleGraph(targetGraph, originalBuildRuleParams.getBuildTarget(), Optional.empty());
/* assemble dirs */
AndroidPackageableCollector collector = new AndroidPackageableCollector(originalBuildRuleParams.getBuildTarget(), /* buildTargetsToExcludeFromDex */
ImmutableSet.of(), /* resourcesToExclude */
ImmutableSet.of(), apkModuleGraph);
collector.addPackageables(AndroidPackageableCollector.getPackageableRules(originalBuildRuleParams.getDeps()));
AndroidPackageableCollection packageableCollection = collector.build();
ImmutableSortedSet<BuildRule> androidResourceDeclaredDeps = AndroidResourceHelper.androidResOnly(originalBuildRuleParams.getDeclaredDeps().get());
ImmutableSortedSet<BuildRule> androidResourceExtraDeps = AndroidResourceHelper.androidResOnly(originalBuildRuleParams.getExtraDeps().get());
BuildRuleParams assembleAssetsParams = originalBuildRuleParams.withBuildTarget(BuildTargets.createFlavoredBuildTarget(originalBuildTarget, AAR_ASSEMBLE_ASSETS_FLAVOR)).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(androidResourceDeclaredDeps), Suppliers.ofInstance(androidResourceExtraDeps));
ImmutableCollection<SourcePath> assetsDirectories = packageableCollection.getAssetsDirectories();
AssembleDirectories assembleAssetsDirectories = new AssembleDirectories(assembleAssetsParams, assetsDirectories);
aarExtraDepsBuilder.add(resolver.addToIndex(assembleAssetsDirectories));
BuildRuleParams assembleResourceParams = originalBuildRuleParams.withBuildTarget(BuildTargets.createFlavoredBuildTarget(originalBuildTarget, AAR_ASSEMBLE_RESOURCE_FLAVOR)).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(androidResourceDeclaredDeps), Suppliers.ofInstance(androidResourceExtraDeps));
ImmutableCollection<SourcePath> resDirectories = packageableCollection.getResourceDetails().getResourceDirectories();
MergeAndroidResourceSources assembleResourceDirectories = new MergeAndroidResourceSources(assembleResourceParams, resDirectories);
aarExtraDepsBuilder.add(resolver.addToIndex(assembleResourceDirectories));
/* android_resource */
BuildRuleParams androidResourceParams = originalBuildRuleParams.withBuildTarget(BuildTargets.createFlavoredBuildTarget(originalBuildTarget, AAR_ANDROID_RESOURCE_FLAVOR)).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of(manifest, assembleAssetsDirectories, assembleResourceDirectories)), Suppliers.ofInstance(ImmutableSortedSet.of()));
AndroidResource androidResource = new AndroidResource(androidResourceParams, ruleFinder, /* deps */
ImmutableSortedSet.<BuildRule>naturalOrder().add(assembleAssetsDirectories).add(assembleResourceDirectories).addAll(originalBuildRuleParams.getDeclaredDeps().get()).build(), assembleResourceDirectories.getSourcePathToOutput(), /* resSrcs */
ImmutableSortedMap.of(), /* rDotJavaPackage */
null, assembleAssetsDirectories.getSourcePathToOutput(), /* assetsSrcs */
ImmutableSortedMap.of(), manifest.getSourcePathToOutput(), /* hasWhitelistedStrings */
false);
aarExtraDepsBuilder.add(resolver.addToIndex(androidResource));
ImmutableSortedSet.Builder<SourcePath> classpathToIncludeInAar = ImmutableSortedSet.naturalOrder();
classpathToIncludeInAar.addAll(packageableCollection.getClasspathEntriesToDex());
aarExtraDepsBuilder.addAll(BuildRules.toBuildRulesFor(originalBuildRuleParams.getBuildTarget(), resolver, packageableCollection.getJavaLibrariesToDex()));
if (!args.buildConfigValues.getNameToField().isEmpty() && !args.includeBuildConfigClass) {
throw new HumanReadableException("Rule %s has build_config_values set but does not set " + "include_build_config_class to True. Either indicate you want to include the " + "BuildConfig class in the final .aar or do not specify build config values.", originalBuildRuleParams.getBuildTarget());
}
if (args.includeBuildConfigClass) {
ImmutableSortedSet<JavaLibrary> buildConfigRules = AndroidBinaryGraphEnhancer.addBuildConfigDeps(originalBuildRuleParams, AndroidBinary.PackageType.RELEASE, EnumSet.noneOf(AndroidBinary.ExopackageMode.class), args.buildConfigValues, Optional.empty(), resolver, javacOptions, packageableCollection);
resolver.addAllToIndex(buildConfigRules);
aarExtraDepsBuilder.addAll(buildConfigRules);
classpathToIncludeInAar.addAll(buildConfigRules.stream().map(BuildRule::getSourcePathToOutput).collect(Collectors.toList()));
}
/* native_libraries */
AndroidNativeLibsPackageableGraphEnhancer packageableGraphEnhancer = new AndroidNativeLibsPackageableGraphEnhancer(resolver, originalBuildRuleParams, nativePlatforms, ImmutableSet.of(), cxxBuckConfig, /* nativeLibraryMergeMap */
Optional.empty(), /* nativeLibraryMergeGlue */
Optional.empty(), AndroidBinary.RelinkerMode.DISABLED, apkModuleGraph);
Optional<ImmutableMap<APKModule, CopyNativeLibraries>> nativeLibrariesOptional = packageableGraphEnhancer.enhance(packageableCollection).getCopyNativeLibraries();
if (nativeLibrariesOptional.isPresent() && nativeLibrariesOptional.get().containsKey(apkModuleGraph.getRootAPKModule())) {
aarExtraDepsBuilder.add(resolver.addToIndex(nativeLibrariesOptional.get().get(apkModuleGraph.getRootAPKModule())));
}
Optional<Path> assembledNativeLibsDir = nativeLibrariesOptional.map(input -> {
CopyNativeLibraries copyNativeLibraries = input.get(apkModuleGraph.getRootAPKModule());
if (copyNativeLibraries == null) {
throw new HumanReadableException("Native libraries are present but not in the root application module.");
}
return copyNativeLibraries.getPathToNativeLibsDir();
});
BuildRuleParams androidAarParams = originalBuildRuleParams.copyReplacingExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.copyOf(aarExtraDepsBuilder.build())));
return new AndroidAar(androidAarParams, manifest, androidResource, assembleResourceDirectories.getSourcePathToOutput(), assembleAssetsDirectories.getSourcePathToOutput(), assembledNativeLibsDir, ImmutableSet.copyOf(packageableCollection.getNativeLibAssetsDirectories().values()), classpathToIncludeInAar.build());
}
use of com.facebook.buck.jvm.java.JavaLibrary in project buck by facebook.
the class AndroidBinary method addProguardCommands.
/**
* @return the resulting set of ProGuarded classpath entries to dex.
*/
@VisibleForTesting
ImmutableSet<Path> addProguardCommands(Set<Path> classpathEntriesToDex, Set<Path> depsProguardConfigs, boolean skipProguard, ImmutableList.Builder<Step> steps, BuildableContext buildableContext, SourcePathResolver resolver) {
ImmutableSet.Builder<Path> additionalLibraryJarsForProguardBuilder = ImmutableSet.builder();
for (JavaLibrary buildRule : rulesToExcludeFromDex) {
additionalLibraryJarsForProguardBuilder.addAll(buildRule.getImmediateClasspaths().stream().map(resolver::getAbsolutePath).collect(MoreCollectors.toImmutableSet()));
}
// Create list of proguard Configs for the app project and its dependencies
ImmutableSet.Builder<Path> proguardConfigsBuilder = ImmutableSet.builder();
proguardConfigsBuilder.addAll(depsProguardConfigs);
if (proguardConfig.isPresent()) {
proguardConfigsBuilder.add(resolver.getAbsolutePath(proguardConfig.get()));
}
Path proguardConfigDir = getProguardTextFilesPath();
// Transform our input classpath to a set of output locations for each input classpath.
// TODO(jasta): the output path we choose is the result of a slicing function against
// input classpath. This is fragile and should be replaced with knowledge of the BuildTarget.
final ImmutableMap<Path, Path> inputOutputEntries = classpathEntriesToDex.stream().collect(MoreCollectors.toImmutableMap(java.util.function.Function.identity(), (path) -> getProguardOutputFromInputClasspath(proguardConfigDir, path)));
// Run ProGuard on the classpath entries.
ProGuardObfuscateStep.create(javaRuntimeLauncher, getProjectFilesystem(), proguardJarOverride.isPresent() ? Optional.of(resolver.getAbsolutePath(proguardJarOverride.get())) : Optional.empty(), proguardMaxHeapSize, proguardAgentPath, resolver.getRelativePath(aaptGeneratedProguardConfigFile), proguardConfigsBuilder.build(), sdkProguardConfig, optimizationPasses, proguardJvmArgs, inputOutputEntries, additionalLibraryJarsForProguardBuilder.build(), proguardConfigDir, buildableContext, skipProguard, steps);
// ProGuard then return the input classes to dex.
if (skipProguard) {
return ImmutableSet.copyOf(inputOutputEntries.keySet());
} else {
return ImmutableSet.copyOf(inputOutputEntries.values());
}
}
use of com.facebook.buck.jvm.java.JavaLibrary in project buck by facebook.
the class AndroidBinaryDescription method createBuildRule.
@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, BuildRuleParams params, BuildRuleResolver resolver, A args) throws NoSuchBuildTargetException {
try (SimplePerfEvent.Scope ignored = SimplePerfEvent.scope(Optional.ofNullable(resolver.getEventBus()), PerfEventId.of("AndroidBinaryDescription"), "target", params.getBuildTarget().toString())) {
ResourceCompressionMode compressionMode = getCompressionMode(args);
BuildTarget target = params.getBuildTarget();
boolean isFlavored = target.isFlavored();
if (isFlavored) {
if (target.getFlavors().contains(PACKAGE_STRING_ASSETS_FLAVOR) && !compressionMode.isStoreStringsAsAssets()) {
throw new HumanReadableException("'package_string_assets' flavor does not exist for %s.", target.getUnflavoredBuildTarget());
}
params = params.withBuildTarget(BuildTarget.of(target.getUnflavoredBuildTarget()));
}
BuildRule keystore = resolver.getRule(args.keystore);
if (!(keystore instanceof Keystore)) {
throw new HumanReadableException("In %s, keystore='%s' must be a keystore() but was %s().", params.getBuildTarget(), keystore.getFullyQualifiedName(), keystore.getType());
}
APKModuleGraph apkModuleGraph = new APKModuleGraph(targetGraph, target, Optional.of(args.applicationModuleTargets));
ProGuardObfuscateStep.SdkProguardType androidSdkProguardConfig = args.androidSdkProguardConfig.orElse(ProGuardObfuscateStep.SdkProguardType.DEFAULT);
// was not specified, and allow the old form to override the default.
if (args.useAndroidProguardConfigWithOptimizations.isPresent()) {
Preconditions.checkArgument(!args.androidSdkProguardConfig.isPresent(), "The deprecated use_android_proguard_config_with_optimizations parameter" + " cannot be used with android_sdk_proguard_config.");
LOG.error("Target %s specified use_android_proguard_config_with_optimizations, " + "which is deprecated. Use android_sdk_proguard_config.", params.getBuildTarget());
androidSdkProguardConfig = args.useAndroidProguardConfigWithOptimizations.orElse(false) ? ProGuardObfuscateStep.SdkProguardType.OPTIMIZED : ProGuardObfuscateStep.SdkProguardType.DEFAULT;
}
EnumSet<ExopackageMode> exopackageModes = EnumSet.noneOf(ExopackageMode.class);
if (!args.exopackageModes.isEmpty()) {
exopackageModes = EnumSet.copyOf(args.exopackageModes);
} else if (args.exopackage.orElse(false)) {
LOG.error("Target %s specified exopackage=True, which is deprecated. Use exopackage_modes.", params.getBuildTarget());
exopackageModes = EnumSet.of(ExopackageMode.SECONDARY_DEX);
}
DexSplitMode dexSplitMode = createDexSplitMode(args, exopackageModes);
PackageType packageType = getPackageType(args);
boolean shouldPreDex = !args.disablePreDex && PackageType.DEBUG.equals(packageType) && !args.preprocessJavaClassesBash.isPresent();
ResourceFilter resourceFilter = new ResourceFilter(args.resourceFilter);
AndroidBinaryGraphEnhancer graphEnhancer = new AndroidBinaryGraphEnhancer(params, resolver, compressionMode, resourceFilter, args.getBannedDuplicateResourceTypes(), args.resourceUnionPackage, addFallbackLocales(args.locales), args.manifest, packageType, ImmutableSet.copyOf(args.cpuFilters), args.buildStringSourceMap, shouldPreDex, AndroidBinary.getPrimaryDexPath(params.getBuildTarget(), params.getProjectFilesystem()), dexSplitMode, ImmutableSet.copyOf(args.noDx.orElse(ImmutableSet.of())), /* resourcesToExclude */
ImmutableSet.of(), args.skipCrunchPngs, args.includesVectorDrawables, javacOptions, exopackageModes, args.buildConfigValues, args.buildConfigValuesFile, Optional.empty(), args.trimResourceIds, args.keepResourcePattern, nativePlatforms, Optional.of(args.nativeLibraryMergeMap), args.nativeLibraryMergeGlue, args.nativeLibraryMergeCodeGenerator, args.enableRelinker ? RelinkerMode.ENABLED : RelinkerMode.DISABLED, dxExecutorService, args.manifestEntries, cxxBuckConfig, apkModuleGraph, dxConfig);
AndroidGraphEnhancementResult result = graphEnhancer.createAdditionalBuildables();
if (target.getFlavors().contains(PACKAGE_STRING_ASSETS_FLAVOR)) {
Optional<PackageStringAssets> packageStringAssets = result.getPackageStringAssets();
Preconditions.checkState(packageStringAssets.isPresent());
return packageStringAssets.get();
}
// Build rules added to "no_dx" are only hints, not hard dependencies. Therefore, although a
// target may be mentioned in that parameter, it may not be present as a build rule.
ImmutableSortedSet.Builder<BuildRule> builder = ImmutableSortedSet.naturalOrder();
for (BuildTarget noDxTarget : args.noDx.orElse(ImmutableSet.of())) {
Optional<BuildRule> ruleOptional = resolver.getRuleOptional(noDxTarget);
if (ruleOptional.isPresent()) {
builder.add(ruleOptional.get());
} else {
LOG.info("%s: no_dx target not a dependency: %s", target, noDxTarget);
}
}
ImmutableSortedSet<BuildRule> buildRulesToExcludeFromDex = builder.build();
ImmutableSortedSet<JavaLibrary> rulesToExcludeFromDex = RichStream.from(buildRulesToExcludeFromDex).filter(JavaLibrary.class).collect(MoreCollectors.toImmutableSortedSet(Ordering.natural()));
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
Optional<RedexOptions> redexOptions = getRedexOptions(params, resolver, args);
ImmutableSortedSet<BuildRule> redexExtraDeps = redexOptions.map(a -> a.getRedexExtraArgs().stream().flatMap(arg -> arg.getDeps(ruleFinder).stream()).collect(MoreCollectors.toImmutableSortedSet(Ordering.natural()))).orElse(ImmutableSortedSet.of());
return new AndroidBinary(params.copyReplacingExtraDeps(Suppliers.ofInstance(result.getFinalDeps())).copyAppendingExtraDeps(ruleFinder.filterBuildRuleInputs(result.getPackageableCollection().getProguardConfigs())).copyAppendingExtraDeps(rulesToExcludeFromDex).copyAppendingExtraDeps(redexExtraDeps), ruleFinder, proGuardConfig.getProguardJarOverride(), proGuardConfig.getProguardMaxHeapSize(), Optional.of(args.proguardJvmArgs), proGuardConfig.getProguardAgentPath(), (Keystore) keystore, packageType, dexSplitMode, args.noDx.orElse(ImmutableSet.of()), androidSdkProguardConfig, args.optimizationPasses, args.proguardConfig, args.skipProguard, redexOptions, compressionMode, args.cpuFilters, resourceFilter, exopackageModes, MACRO_HANDLER.getExpander(params.getBuildTarget(), params.getCellRoots(), resolver), args.preprocessJavaClassesBash, rulesToExcludeFromDex, result, args.reorderClassesIntraDex, args.dexReorderToolFile, args.dexReorderDataDumpFile, args.xzCompressionLevel, dxExecutorService, args.packageAssetLibraries, args.compressAssetLibraries, args.manifestEntries, javaOptions.getJavaRuntimeLauncher(), dxConfig.getDxMaxHeapSize());
}
}
use of com.facebook.buck.jvm.java.JavaLibrary in project buck by facebook.
the class AndroidInstrumentationApkDescription method createBuildRule.
@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, BuildRuleParams params, BuildRuleResolver resolver, A args) throws NoSuchBuildTargetException {
BuildRule installableApk = resolver.getRule(args.apk);
if (!(installableApk instanceof HasInstallableApk)) {
throw new HumanReadableException("In %s, apk='%s' must be an android_binary() or apk_genrule() but was %s().", params.getBuildTarget(), installableApk.getFullyQualifiedName(), installableApk.getType());
}
AndroidBinary apkUnderTest = getUnderlyingApk((HasInstallableApk) installableApk);
ImmutableSortedSet<JavaLibrary> rulesToExcludeFromDex = new ImmutableSortedSet.Builder<>(Ordering.<JavaLibrary>natural()).addAll(apkUnderTest.getRulesToExcludeFromDex()).addAll(getClasspathDeps(apkUnderTest.getClasspathDeps())).build();
// TODO(natthu): Instrumentation APKs should also exclude native libraries and assets from the
// apk under test.
AndroidPackageableCollection.ResourceDetails resourceDetails = apkUnderTest.getAndroidPackageableCollection().getResourceDetails();
ImmutableSet<BuildTarget> resourcesToExclude = ImmutableSet.copyOf(Iterables.concat(resourceDetails.getResourcesWithNonEmptyResDir(), resourceDetails.getResourcesWithEmptyResButNonEmptyAssetsDir()));
Path primaryDexPath = AndroidBinary.getPrimaryDexPath(params.getBuildTarget(), params.getProjectFilesystem());
AndroidBinaryGraphEnhancer graphEnhancer = new AndroidBinaryGraphEnhancer(params, resolver, ResourceCompressionMode.DISABLED, FilterResourcesStep.ResourceFilter.EMPTY_FILTER, /* bannedDuplicateResourceTypes */
EnumSet.noneOf(RType.class), /* resourceUnionPackage */
Optional.empty(), /* locales */
ImmutableSet.of(), args.manifest, PackageType.INSTRUMENTED, apkUnderTest.getCpuFilters(), /* shouldBuildStringSourceMap */
false, /* shouldPreDex */
false, primaryDexPath, DexSplitMode.NO_SPLIT, rulesToExcludeFromDex.stream().map(BuildRule::getBuildTarget).collect(MoreCollectors.toImmutableSet()), resourcesToExclude, /* skipCrunchPngs */
false, args.includesVectorDrawables.orElse(false), javacOptions, EnumSet.noneOf(ExopackageMode.class), /* buildConfigValues */
BuildConfigFields.empty(), /* buildConfigValuesFile */
Optional.empty(), /* xzCompressionLevel */
Optional.empty(), /* trimResourceIds */
false, /* keepResourcePattern */
Optional.empty(), nativePlatforms, /* nativeLibraryMergeMap */
Optional.empty(), /* nativeLibraryMergeGlue */
Optional.empty(), /* nativeLibraryMergeCodeGenerator */
Optional.empty(), AndroidBinary.RelinkerMode.DISABLED, dxExecutorService, apkUnderTest.getManifestEntries(), cxxBuckConfig, new APKModuleGraph(targetGraph, params.getBuildTarget(), Optional.empty()), dxConfig);
AndroidGraphEnhancementResult enhancementResult = graphEnhancer.createAdditionalBuildables();
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
return new AndroidInstrumentationApk(params.copyReplacingExtraDeps(Suppliers.ofInstance(enhancementResult.getFinalDeps())).copyAppendingExtraDeps(rulesToExcludeFromDex), ruleFinder, proGuardConfig.getProguardJarOverride(), proGuardConfig.getProguardMaxHeapSize(), proGuardConfig.getProguardAgentPath(), apkUnderTest, rulesToExcludeFromDex, enhancementResult, dxExecutorService);
}
use of com.facebook.buck.jvm.java.JavaLibrary in project buck by facebook.
the class Project method createModuleForProjectConfig.
@SuppressWarnings("PMD.LooseCoupling")
private SerializableModule createModuleForProjectConfig(ProjectConfig projectConfig, Optional<Path> rJava) throws IOException {
BuildRule projectRule = Preconditions.checkNotNull(projectConfig.getProjectRule());
Preconditions.checkState(projectRule instanceof AndroidBinary || projectRule instanceof AndroidLibrary || projectRule instanceof AndroidResource || projectRule instanceof JavaBinary || projectRule instanceof JavaLibrary || projectRule instanceof JavaTest || projectRule instanceof CxxLibrary || projectRule instanceof NdkLibrary, "project_config() does not know how to process a src_target of type %s (%s).", projectRule.getType(), projectRule.getBuildTarget());
LinkedHashSet<SerializableDependentModule> dependencies = Sets.newLinkedHashSet();
final BuildTarget target = projectConfig.getBuildTarget();
SerializableModule module = new SerializableModule(projectRule, target);
module.name = getIntellijNameForRule(projectRule);
module.isIntelliJPlugin = projectConfig.getIsIntelliJPlugin();
Path relativePath = projectConfig.getBuildTarget().getBasePath();
module.pathToImlFile = relativePath.resolve(module.name + ".iml");
// List the module source as the first dependency.
boolean includeSourceFolder = true;
// Do the tests before the sources so they appear earlier in the classpath. When tests are run,
// their classpath entries may be deliberately shadowing production classpath entries.
// tests folder
boolean hasSourceFoldersForTestRule = addSourceFolders(module, projectConfig.getTestRule(), projectConfig.getTestsSourceRoots(), true);
// test dependencies
BuildRule testRule = projectConfig.getTestRule();
if (testRule != null) {
walkRuleAndAdd(testRule, true, /* isForTests */
dependencies, projectConfig.getSrcRule());
}
// src folder
boolean hasSourceFoldersForSrcRule = addSourceFolders(module, projectConfig.getSrcRule(), projectConfig.getSourceRoots(), false);
addRootExcludes(module, projectConfig.getSrcRule(), projectFilesystem);
// non-library Android project with no source roots specified.
if (!hasSourceFoldersForTestRule && !hasSourceFoldersForSrcRule) {
includeSourceFolder = false;
}
// IntelliJ expects all Android projects to have a gen/ folder, even if there is no src/
// directory specified.
boolean isAndroidRule = projectRule.getProperties().is(ANDROID);
if (isAndroidRule) {
boolean hasSourceFolders = !module.sourceFolders.isEmpty();
module.sourceFolders.add(SerializableModule.SourceFolder.GEN);
if (!hasSourceFolders) {
includeSourceFolder = true;
}
}
// src dependencies
// Note that isForTests is false even if projectRule is the project_config's test_target.
walkRuleAndAdd(projectRule, false, /* isForTests */
dependencies, projectConfig.getSrcRule());
Path basePath = projectConfig.getBuildTarget().getBasePath();
// Specify another path for intellij to generate gen/ for each android module,
// so that it will not disturb our glob() rules.
// To specify the location of gen, Intellij requires the relative path from
// the base path of current build target.
module.moduleGenPath = generateRelativeGenPath(projectFilesystem, basePath);
if (turnOffAutoSourceGeneration && rJava.isPresent()) {
module.moduleRJavaPath = basePath.relativize(Paths.get("")).resolve(rJava.get());
}
SerializableDependentModule jdkDependency;
if (isAndroidRule) {
// android details
if (projectRule instanceof NdkLibrary) {
NdkLibrary ndkLibrary = (NdkLibrary) projectRule;
module.isAndroidLibraryProject = true;
module.keystorePath = null;
module.nativeLibs = relativePath.relativize(ndkLibrary.getLibraryPath());
} else if (projectRule instanceof AndroidLibrary) {
module.isAndroidLibraryProject = true;
module.keystorePath = null;
module.resFolder = intellijConfig.getAndroidResources().orElse(null);
module.assetFolder = intellijConfig.getAndroidAssets().orElse(null);
} else if (projectRule instanceof AndroidResource) {
AndroidResource androidResource = (AndroidResource) projectRule;
module.resFolder = createRelativeResourcesPath(Optional.ofNullable(androidResource.getRes()).map(resolver::getAbsolutePath).map(projectFilesystem::relativize).orElse(null), target);
module.isAndroidLibraryProject = true;
module.keystorePath = null;
} else if (projectRule instanceof AndroidBinary) {
AndroidBinary androidBinary = (AndroidBinary) projectRule;
module.resFolder = intellijConfig.getAndroidResources().orElse(null);
module.assetFolder = intellijConfig.getAndroidAssets().orElse(null);
module.isAndroidLibraryProject = false;
module.binaryPath = generateRelativeAPKPath(projectFilesystem, projectRule.getBuildTarget().getShortName(), basePath);
KeystoreProperties keystoreProperties = KeystoreProperties.createFromPropertiesFile(resolver.getAbsolutePath(androidBinary.getKeystore().getPathToStore()), resolver.getRelativePath(androidBinary.getKeystore().getPathToPropertiesFile()), projectFilesystem);
// getKeystore() returns an absolute path, but an IntelliJ module
// expects the path to the keystore to be relative to the module root.
// First, grab the aboslute path to the project config.
BuildTarget projectTarget = projectConfig.getBuildTarget();
Path modulePath = projectTarget.getCellPath().resolve(projectTarget.getBasePath());
// Now relativize to the keystore path, which is absolute too.
module.keystorePath = modulePath.relativize(keystoreProperties.getKeystore());
} else {
module.isAndroidLibraryProject = true;
module.keystorePath = null;
}
module.hasAndroidFacet = true;
module.proguardConfigPath = null;
module.androidManifest = resolveAndroidManifestRelativePath(basePath);
// List this last so that classes from modules can shadow classes in the JDK.
jdkDependency = SerializableDependentModule.newInheritedJdk();
} else {
module.hasAndroidFacet = false;
if (module.isIntelliJPlugin()) {
jdkDependency = SerializableDependentModule.newIntelliJPluginJdk();
} else {
jdkDependency = SerializableDependentModule.newStandardJdk(intellijConfig.getJdkName(), intellijConfig.getJdkType());
}
}
// Assign the dependencies.
module.setModuleDependencies(createDependenciesInOrder(includeSourceFolder, dependencies, jdkDependency));
// Annotation processing generates sources for IntelliJ to consume, but does so outside
// the module directory to avoid messing up globbing.
JavaLibrary javaLibrary = null;
if (projectRule instanceof JavaLibrary) {
javaLibrary = (JavaLibrary) projectRule;
}
if (javaLibrary != null) {
Optional<Path> processingParams = javaLibrary.getGeneratedSourcePath();
if (processingParams.isPresent()) {
module.annotationGenPath = basePath.relativize(processingParams.get());
module.annotationGenIsForTest = !hasSourceFoldersForSrcRule;
}
}
return module;
}
Aggregations