Search in sources :

Example 46 with CompilerMessage

use of org.jetbrains.jps.incremental.messages.CompilerMessage in project android by JetBrains.

the class AndroidPackagingBuilder method doPackagingForModule.

private static boolean doPackagingForModule(@NotNull CompileContext context, @NotNull JpsModule module, @NotNull AndroidApkBuilderConfigStateStorage apkBuilderConfigStateStorage, @NotNull AndroidPackagingStateStorage packagingStateStorage, boolean release, boolean hasDirtyFiles, @NotNull BuildOutputConsumer outputConsumer) throws IOException {
    final JpsAndroidModuleExtension extension = AndroidJpsUtil.getExtension(module);
    if (extension == null || extension.isLibrary()) {
        return true;
    }
    final String[] resourceRoots = AndroidJpsUtil.toPaths(AndroidJpsUtil.getJavaOutputRootsForModuleAndDependencies(module));
    Arrays.sort(resourceRoots);
    final File moduleOutputDir = ProjectPaths.getModuleOutputDir(module, false);
    if (moduleOutputDir == null) {
        context.processMessage(new CompilerMessage(BUILDER_NAME, BuildMessage.Kind.ERROR, AndroidJpsBundle.message("android.jps.errors.output.dir.not.specified", module.getName())));
        return false;
    }
    final AndroidPlatform platform = AndroidJpsUtil.getAndroidPlatform(module, context, BUILDER_NAME);
    if (platform == null) {
        return false;
    }
    final Set<String> externalJarsSet = new HashSet<String>();
    for (String jarPath : AndroidJpsUtil.getExternalLibraries(context, module, platform)) {
        if (new File(jarPath).exists()) {
            externalJarsSet.add(jarPath);
        }
    }
    final BuildDataPaths dataPaths = context.getProjectDescriptor().dataManager.getDataPaths();
    final File resPackage = AndroidResourcePackagingBuildTarget.getOutputFile(dataPaths, module);
    final File classesDexFile = AndroidDexBuildTarget.getOutputFile(dataPaths, module);
    final String sdkPath = platform.getSdk().getHomePath();
    final String outputPath = AndroidJpsUtil.getApkPath(extension, moduleOutputDir);
    if (outputPath == null) {
        context.processMessage(new CompilerMessage(BUILDER_NAME, BuildMessage.Kind.ERROR, AndroidJpsBundle.message("android.jps.errors.cannot.compute.output.apk", module.getName())));
        return false;
    }
    final String customKeyStorePath = FileUtil.toSystemDependentName(extension.getCustomDebugKeyStorePath());
    final String[] nativeLibDirs = AndroidPackagingBuildTarget.collectNativeLibsFolders(extension, true);
    final String resPackagePath = resPackage.getPath();
    final String classesDexFilePath = classesDexFile.getPath();
    final String[] externalJars = ArrayUtil.toStringArray(externalJarsSet);
    Arrays.sort(externalJars);
    final List<AndroidNativeLibData> additionalNativeLibs = extension.getAdditionalNativeLibs();
    final AndroidApkBuilderConfigState currentApkBuilderConfigState = new AndroidApkBuilderConfigState(outputPath, customKeyStorePath, additionalNativeLibs);
    if (!hasDirtyFiles) {
        final AndroidApkBuilderConfigState savedApkBuilderConfigState = apkBuilderConfigStateStorage.getState(module.getName());
        final AndroidPackagingStateStorage.MyState packagingState = packagingStateStorage.read();
        if (currentApkBuilderConfigState.equalsTo(savedApkBuilderConfigState) && packagingState != null && packagingState.isRelease() == release) {
            return true;
        }
    }
    context.processMessage(new ProgressMessage(AndroidJpsBundle.message("android.jps.progress.packaging", AndroidJpsUtil.getApkName(module))));
    final Map<AndroidCompilerMessageKind, List<String>> messages = AndroidApkBuilder.execute(resPackagePath, classesDexFilePath, resourceRoots, externalJars, nativeLibDirs, additionalNativeLibs, outputPath, release, sdkPath, platform.getTarget(), customKeyStorePath, new MyExcludedSourcesFilter(context.getProjectDescriptor().getProject()));
    if (messages.get(AndroidCompilerMessageKind.ERROR).size() == 0) {
        final List<String> srcFiles = new ArrayList<String>();
        srcFiles.add(resPackagePath);
        srcFiles.add(classesDexFilePath);
        for (String resourceRoot : resourceRoots) {
            FileUtil.processFilesRecursively(new File(resourceRoot), new Processor<File>() {

                @Override
                public boolean process(File file) {
                    if (file.isFile() && AndroidApkBuilder.checkFileForPackaging(file)) {
                        srcFiles.add(file.getPath());
                    }
                    return true;
                }
            });
        }
        Collections.addAll(srcFiles, externalJars);
        for (String nativeLibDir : nativeLibDirs) {
            FileUtil.processFilesRecursively(new File(nativeLibDir), new Processor<File>() {

                @Override
                public boolean process(File file) {
                    if (file.isFile()) {
                        srcFiles.add(file.getPath());
                    }
                    return true;
                }
            });
        }
        outputConsumer.registerOutputFile(new File(outputPath), srcFiles);
    }
    AndroidJpsUtil.addMessages(context, messages, BUILDER_NAME, module.getName());
    final boolean success = messages.get(AndroidCompilerMessageKind.ERROR).isEmpty();
    apkBuilderConfigStateStorage.update(module.getName(), success ? currentApkBuilderConfigState : null);
    packagingStateStorage.saveState(new AndroidPackagingStateStorage.MyState(release));
    return success;
}
Also used : ProgressMessage(org.jetbrains.jps.incremental.messages.ProgressMessage) CompilerMessage(org.jetbrains.jps.incremental.messages.CompilerMessage) AndroidCompilerMessageKind(org.jetbrains.android.util.AndroidCompilerMessageKind) AndroidNativeLibData(org.jetbrains.android.util.AndroidNativeLibData) BuildDataPaths(org.jetbrains.jps.builders.storage.BuildDataPaths) JpsAndroidModuleExtension(org.jetbrains.jps.android.model.JpsAndroidModuleExtension) File(java.io.File) HashSet(com.intellij.util.containers.HashSet)

Example 47 with CompilerMessage

use of org.jetbrains.jps.incremental.messages.CompilerMessage in project android by JetBrains.

the class AndroidDexBuilder method doDexBuild.

private static boolean doDexBuild(@NotNull AndroidDexBuildTarget target, @NotNull CompileContext context, boolean hasDirtyFiles, @NotNull BuildOutputConsumer outputConsumer) throws IOException {
    final JpsModule module = target.getModule();
    final JpsAndroidModuleExtension extension = AndroidJpsUtil.getExtension(module);
    assert extension != null;
    assert !extension.isLibrary();
    final AndroidPlatform platform = AndroidJpsUtil.getAndroidPlatform(module, context, DEX_BUILDER_NAME);
    if (platform == null) {
        return false;
    }
    File dexOutputDir = AndroidJpsUtil.getDirectoryForIntermediateArtifacts(context, module);
    dexOutputDir = AndroidJpsUtil.createDirIfNotExist(dexOutputDir, context, DEX_BUILDER_NAME);
    if (dexOutputDir == null) {
        return false;
    }
    final ProGuardOptions proGuardOptions = AndroidJpsUtil.getProGuardConfigIfShouldRun(context, extension);
    if (proGuardOptions != null) {
        if (proGuardOptions.getCfgFiles() == null) {
            context.processMessage(new CompilerMessage(DEX_BUILDER_NAME, BuildMessage.Kind.ERROR, AndroidJpsBundle.message("android.jps.errors.cannot.find.proguard.cfg", module.getName())));
            return false;
        }
    }
    final File proguardCfgOutputFile = new File(dexOutputDir, AndroidCommonUtils.PROGUARD_CFG_OUTPUT_FILE_NAME);
    final AndroidProGuardStateStorage proGuardOptionsStorage = context.getProjectDescriptor().dataManager.getStorage(target, AndroidProGuardOptionsStorageProvider.INSTANCE);
    final AndroidProGuardStateStorage.MyState oldProGuardState = proGuardOptionsStorage.read();
    final Set<String> fileSet;
    AndroidProGuardStateStorage.MyState newProGuardState = null;
    try {
        if (proGuardOptions != null) {
            final List<String> proguardCfgFilePathsList = new ArrayList<String>();
            for (File file : proGuardOptions.getCfgFiles()) {
                proguardCfgFilePathsList.add(file.getAbsolutePath());
            }
            proguardCfgFilePathsList.add(proguardCfgOutputFile.getPath());
            final String[] proguardCfgFilePaths = ArrayUtil.toStringArray(proguardCfgFilePathsList);
            final String outputJarPath = FileUtil.toSystemDependentName(dexOutputDir.getPath() + '/' + AndroidCommonUtils.PROGUARD_OUTPUT_JAR_NAME);
            final Pair<Boolean, AndroidProGuardStateStorage.MyState> pair = runProguardIfNecessary(extension, target, platform, context, outputJarPath, proguardCfgFilePaths, hasDirtyFiles, oldProGuardState);
            if (pair == null) {
                // error reported
                return false;
            }
            if (!pair.getFirst()) {
                // nothing changed
                return true;
            }
            newProGuardState = pair.getSecond();
            assert newProGuardState != null;
            fileSet = Collections.singleton(outputJarPath);
        } else {
            if (!hasDirtyFiles && oldProGuardState == null) {
                return true;
            }
            final List<BuildRootDescriptor> roots = context.getProjectDescriptor().getBuildRootIndex().getTargetRoots(target, context);
            fileSet = new HashSet<String>();
            final boolean predexingEnabled = extension.isPreDexingEnabled() && isPredexingInScope(context);
            for (BuildRootDescriptor root : roots) {
                final File rootFile = root.getRootFile();
                if (!rootFile.exists()) {
                    continue;
                }
                if (root instanceof AndroidDexBuildTarget.MyClassesDirBuildRootDescriptor) {
                    final AndroidDexBuildTarget.ClassesDirType type = ((AndroidDexBuildTarget.MyClassesDirBuildRootDescriptor) root).getClassesDirType();
                    if (type == AndroidDexBuildTarget.ClassesDirType.JAVA) {
                        fileSet.add(rootFile.getPath());
                    } else if (type == AndroidDexBuildTarget.ClassesDirType.ANDROID_APP) {
                        AndroidJpsUtil.addSubdirectories(rootFile, fileSet);
                    }
                } else if (root instanceof AndroidDexBuildTarget.MyJarBuildRootDescriptor) {
                    if (((AndroidDexBuildTarget.MyJarBuildRootDescriptor) root).isPreDexed() == predexingEnabled) {
                        fileSet.add(rootFile.getPath());
                    }
                }
            }
        }
        final boolean success;
        if (fileSet.size() > 0) {
            final String[] files = new String[fileSet.size()];
            int i = 0;
            for (String filePath : fileSet) {
                files[i++] = FileUtil.toSystemDependentName(filePath);
            }
            context.processMessage(new ProgressMessage(AndroidJpsBundle.message("android.jps.progress.dex", module.getName())));
            Arrays.sort(files);
            success = runDex(platform, dexOutputDir.getPath(), files, context, module, outputConsumer);
        } else {
            success = true;
        }
        if (success) {
            proGuardOptionsStorage.update(newProGuardState);
        }
        return success;
    } catch (IOException e) {
        AndroidJpsUtil.reportExceptionError(context, null, e, DEX_BUILDER_NAME);
        return false;
    }
}
Also used : ProgressMessage(org.jetbrains.jps.incremental.messages.ProgressMessage) CompilerMessage(org.jetbrains.jps.incremental.messages.CompilerMessage) AndroidDexBuildTarget(org.jetbrains.jps.android.builder.AndroidDexBuildTarget) BuildRootDescriptor(org.jetbrains.jps.builders.BuildRootDescriptor) IOException(java.io.IOException) JpsModule(org.jetbrains.jps.model.module.JpsModule) JpsAndroidModuleExtension(org.jetbrains.jps.android.model.JpsAndroidModuleExtension) File(java.io.File)

Example 48 with CompilerMessage

use of org.jetbrains.jps.incremental.messages.CompilerMessage in project android by JetBrains.

the class AndroidDexBuilder method getJavaExecutable.

@Nullable
private static String getJavaExecutable(@NotNull AndroidPlatform platform, @NotNull CompileContext context, @NotNull String builderName) {
    final JpsSdk<JpsSimpleElement<JpsAndroidSdkProperties>> sdk = platform.getSdk();
    final String jdkName = sdk.getSdkProperties().getData().getJdkName();
    final JpsLibrary javaSdk = context.getProjectDescriptor().getModel().getGlobal().getLibraryCollection().findLibrary(jdkName);
    if (javaSdk == null || !javaSdk.getType().equals(JpsJavaSdkType.INSTANCE)) {
        context.processMessage(new CompilerMessage(builderName, BuildMessage.Kind.ERROR, AndroidJpsBundle.message("android.jps.errors.java.sdk.not.specified", jdkName)));
        return null;
    }
    return JpsJavaSdkType.getJavaExecutable((JpsSdk<?>) javaSdk.getProperties());
}
Also used : CompilerMessage(org.jetbrains.jps.incremental.messages.CompilerMessage) JpsSimpleElement(org.jetbrains.jps.model.JpsSimpleElement) JpsLibrary(org.jetbrains.jps.model.library.JpsLibrary) Nullable(org.jetbrains.annotations.Nullable)

Example 49 with CompilerMessage

use of org.jetbrains.jps.incremental.messages.CompilerMessage in project android by JetBrains.

the class AndroidJpsUtil method reportExceptionError.

public static void reportExceptionError(@NotNull CompileContext context, @Nullable String filePath, @NotNull Exception exception, @NotNull String builderName) {
    final String message = exception.getMessage();
    if (message != null) {
        context.processMessage(new CompilerMessage(builderName, BuildMessage.Kind.ERROR, message, filePath));
        LOG.debug(exception);
    } else {
        context.processMessage(new CompilerMessage(builderName, exception));
    }
}
Also used : CompilerMessage(org.jetbrains.jps.incremental.messages.CompilerMessage)

Example 50 with CompilerMessage

use of org.jetbrains.jps.incremental.messages.CompilerMessage in project android by JetBrains.

the class AndroidJpsUtil method getAndroidTarget.

@Nullable
public static Pair<IAndroidTarget, AndroidSdkHandler> getAndroidTarget(@NotNull JpsSdk<JpsSimpleElement<JpsAndroidSdkProperties>> sdk, @Nullable CompileContext context, String builderName) {
    JpsAndroidSdkProperties sdkProperties = sdk.getSdkProperties().getData();
    final String targetHashString = sdkProperties.getBuildTargetHashString();
    if (targetHashString == null) {
        if (context != null) {
            context.processMessage(new CompilerMessage(builderName, BuildMessage.Kind.ERROR, "Cannot parse SDK " + sdk.getParent().getName() + ": build target is not specified"));
        }
        return null;
    }
    final AndroidSdkHandler sdkHandler = AndroidSdkHandler.getInstance(new File(sdk.getHomePath()));
    RepoLogger log = new RepoLogger();
    final IAndroidTarget target = sdkHandler.getAndroidTargetManager(log).getTargetFromHashString(targetHashString, log);
    if (target == null) {
        if (context != null) {
            context.processMessage(new CompilerMessage(builderName, BuildMessage.Kind.ERROR, "Cannot parse SDK '" + sdk.getParent().getName() + "': unknown target " + targetHashString));
        }
        return null;
    }
    return Pair.create(target, sdkHandler);
}
Also used : CompilerMessage(org.jetbrains.jps.incremental.messages.CompilerMessage) AndroidSdkHandler(com.android.sdklib.repository.AndroidSdkHandler) IAndroidTarget(com.android.sdklib.IAndroidTarget) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

CompilerMessage (org.jetbrains.jps.incremental.messages.CompilerMessage)75 File (java.io.File)34 IOException (java.io.IOException)22 ProgressMessage (org.jetbrains.jps.incremental.messages.ProgressMessage)18 JpsModule (org.jetbrains.jps.model.module.JpsModule)15 HashMap (com.intellij.util.containers.HashMap)11 JpsAndroidModuleExtension (org.jetbrains.jps.android.model.JpsAndroidModuleExtension)11 TObjectLongHashMap (gnu.trove.TObjectLongHashMap)9 ArrayList (java.util.ArrayList)9 THashSet (gnu.trove.THashSet)8 Nullable (org.jetbrains.annotations.Nullable)8 ProjectBuildException (org.jetbrains.jps.incremental.ProjectBuildException)8 IAndroidTarget (com.android.sdklib.IAndroidTarget)7 NotNull (org.jetbrains.annotations.NotNull)7 BuildMessage (org.jetbrains.jps.incremental.messages.BuildMessage)6 AndroidCompilerMessageKind (org.jetbrains.android.util.AndroidCompilerMessageKind)5 FailSafeClassReader (com.intellij.compiler.instrumentation.FailSafeClassReader)4 JpsDummyElement (org.jetbrains.jps.model.JpsDummyElement)4 Pair (com.intellij.openapi.util.Pair)3 THashMap (gnu.trove.THashMap)3