2 * Copyright 2000-2017 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.jetbrains.jps.incremental;
18 import com.intellij.openapi.util.Key;
19 import com.intellij.openapi.util.io.FileUtil;
20 import com.intellij.openapi.util.text.StringUtil;
21 import com.intellij.util.PathUtilRt;
22 import org.jetbrains.annotations.Nullable;
23 import org.jetbrains.jps.builders.BuildTarget;
24 import org.jetbrains.jps.model.serialization.JpsProjectLoader;
25 import org.jetbrains.jps.model.serialization.PathMacroUtil;
28 import java.nio.file.Files;
29 import java.nio.file.Path;
30 import java.nio.file.Paths;
31 import java.util.Collection;
35 * @author Eugene Zhuravlev
38 public static final Key<Map<BuildTarget<?>, Collection<String>>> REMOVED_SOURCES_KEY = Key.create("_removed_sources_");
39 public static final Key<Boolean> PROCEED_ON_ERROR_KEY = Key.create("_proceed_on_error_");
40 public static final Key<Boolean> ERRORS_DETECTED_KEY = Key.create("_errors_detected_");
41 private static volatile File ourSystemRoot = new File(System.getProperty("user.home"), ".idea-build");
42 public static final boolean IS_TEST_MODE = Boolean.parseBoolean(System.getProperty("test.mode", "false"));
43 public static final boolean IS_PROFILING_MODE = Boolean.parseBoolean(System.getProperty("profiling.mode", "false"));
48 public static File getSystemRoot() {
52 public static void setSystemRoot(File systemRoot) {
53 ourSystemRoot = systemRoot;
57 public static File getDataStorageRoot(String projectPath) {
58 return getDataStorageRoot(ourSystemRoot, projectPath);
61 public static File getDataStorageRoot(final File systemRoot, String projectPath) {
62 projectPath = FileUtil.toCanonicalPath(projectPath);
63 if (projectPath == null) {
68 final int locationHash;
70 final Path rootFile = Paths.get(projectPath);
71 if (!Files.isDirectory(rootFile) && projectPath.endsWith(".ipr")) {
72 name = StringUtil.trimEnd(rootFile.getFileName().toString(), ".ipr");
73 locationHash = projectPath.hashCode();
76 Path directoryBased = null;
77 if (rootFile.endsWith(PathMacroUtil.DIRECTORY_STORE_NAME)) {
78 directoryBased = rootFile;
81 Path child = rootFile.resolve(PathMacroUtil.DIRECTORY_STORE_NAME);
82 if (Files.exists(child)) {
83 directoryBased = child;
86 if (directoryBased == null) {
89 name = PathUtilRt.suggestFileName(JpsProjectLoader.getDirectoryBaseProjectName(directoryBased));
90 locationHash = directoryBased.toString().hashCode();
93 return new File(systemRoot, StringUtil.toLowerCase(name) + "_" + Integer.toHexString(locationHash));
96 public static boolean errorsDetected(CompileContext context) {
97 return ERRORS_DETECTED_KEY.get(context, Boolean.FALSE);
100 public static String formatDuration(long duration) {
101 return StringUtil.formatDuration(duration);
104 public static int suggestForkedCompilerHeapSize() {
105 //final JpsProject project = context.getProjectDescriptor().getProject();
106 //final JpsJavaCompilerConfiguration config = JpsJavaExtensionService.getInstance().getOrCreateCompilerConfiguration(project);
107 //final JpsJavaCompilerOptions options = config.getCurrentCompilerOptions();
108 //return options.MAXIMUM_HEAP_SIZE;
109 final int maxMbytes = (int)(Runtime.getRuntime().maxMemory() / 1048576L);
111 return -1; // in case of int overflow, return -1 to let VM choose the heap size
113 return Math.max(maxMbytes / 3, 256); // per-forked process: minimum 256 Mb, maximum 33% from JPS max heap size