summaryrefslogtreecommitdiff
path: root/java/compiler/impl/src/com/intellij/compiler/impl/CompilerUtil.java
blob: 9b227da1dbf809de9e4a9ba607de511e95eff618 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
 * Copyright 2000-2012 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * created at Jan 3, 2002
 * @author Jeka
 */
package com.intellij.compiler.impl;

import com.intellij.CommonBundle;
import com.intellij.compiler.CompilerConfiguration;
import com.intellij.compiler.impl.javaCompiler.ModuleChunk;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.compiler.CompileContext;
import com.intellij.openapi.compiler.CompilerBundle;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.CharsetToolkit;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.newvfs.RefreshQueue;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.util.ThrowableRunnable;
import com.intellij.util.containers.ContainerUtil;
import gnu.trove.THashMap;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.FileFilter;
import java.util.*;

public class CompilerUtil {
  private static final Logger LOG = Logger.getInstance("#com.intellij.compiler.impl.CompilerUtil");

  public static String quotePath(String path) {
    if(path != null && path.indexOf(' ') != -1) {
      path = path.replaceAll("\\\\", "\\\\\\\\");
      path = '"' + path + '"';
    }
    return path;
  }

  public static void collectFiles(Collection<File> container, File rootDir, FileFilter fileFilter) {
    final File[] files = rootDir.listFiles(fileFilter);
    if (files == null) {
      return;
    }
    for (File file : files) {
      if (file.isDirectory()) {
        collectFiles(container, file, fileFilter);
      }
      else {
        container.add(file);
      }
    }
  }

  public static Map<Module, List<VirtualFile>> buildModuleToFilesMap(CompileContext context, VirtualFile[] files) {
    return buildModuleToFilesMap(context, Arrays.asList(files));
  }


  public static Map<Module, List<VirtualFile>> buildModuleToFilesMap(final CompileContext context, final List<VirtualFile> files) {
    //assertion: all files are different
    final Map<Module, List<VirtualFile>> map = new THashMap<Module, List<VirtualFile>>();
    ApplicationManager.getApplication().runReadAction(new Runnable() {
      public void run() {
        for (VirtualFile file : files) {
          final Module module = context.getModuleByFile(file);

          if (module == null) {
            continue; // looks like file invalidated
          }

          List<VirtualFile> moduleFiles = map.get(module);
          if (moduleFiles == null) {
            moduleFiles = new ArrayList<VirtualFile>();
            map.put(module, moduleFiles);
          }
          moduleFiles.add(file);
        }
      }
    });
    return map;
  }


  /**
   * must not be called inside ReadAction
   * @param files
   */
  public static void refreshIOFiles(@NotNull final Collection<File> files) {
    if (!files.isEmpty()) {
      LocalFileSystem.getInstance().refreshIoFiles(files);
    }
  }

  public static void refreshIODirectories(@NotNull final Collection<File> files) {
    final LocalFileSystem lfs = LocalFileSystem.getInstance();
    final List<VirtualFile> filesToRefresh = new ArrayList<VirtualFile>();
    for (File file : files) {
      final VirtualFile virtualFile = lfs.refreshAndFindFileByIoFile(file);
      if (virtualFile != null) {
        filesToRefresh.add(virtualFile);
      }
    }
    if (!filesToRefresh.isEmpty()) {
      RefreshQueue.getInstance().refresh(false, true, null, filesToRefresh);
    }
  }

  public static void refreshOutputDirectories(Set<File> outputs, boolean async) {
    LocalFileSystem fileSystem = LocalFileSystem.getInstance();
    List<VirtualFile> toRefresh = new ArrayList<VirtualFile>();

    int newDirectories = 0;
    for (File ioOutput : outputs) {
      VirtualFile output = fileSystem.findFileByIoFile(ioOutput);
      if (output != null) {
        toRefresh.add(output);
      }
      else if (ioOutput.exists()) {
        VirtualFile parent = fileSystem.refreshAndFindFileByIoFile(ioOutput.getParentFile());
        if (parent != null) {
          parent.getChildren();
          toRefresh.add(parent);
          newDirectories++;
        }
      }
    }
    if (newDirectories > 10) {
      LOG.info(newDirectories + " new output directories were created, refreshing their parents together to avoid too many rootsChange events");
      RefreshQueue.getInstance().refresh(async, false, null, toRefresh);
    }
    else {
      LOG.debug("Refreshing " + outputs.size() + " outputs");
      fileSystem.refreshIoFiles(outputs, async, false, null);
    }
  }

  public static void refreshIOFile(final File file) {
    final VirtualFile vFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);
    if (vFile != null) {
      vFile.refresh(false, false);
    }
  }

  public static void addLocaleOptions(final List<String> commandLine, final boolean launcherUsed) {
    // need to specify default encoding so that javac outputs messages in 'correct' language
    //noinspection HardCodedStringLiteral
    commandLine.add((launcherUsed? "-J" : "") + "-D" + CharsetToolkit.FILE_ENCODING_PROPERTY + "=" + CharsetToolkit.getDefaultSystemCharset().name());
    // javac's VM should use the same default locale that IDEA uses in order for javac to print messages in 'correct' language
    //noinspection HardCodedStringLiteral
    final String lang = System.getProperty("user.language");
    if (lang != null) {
      //noinspection HardCodedStringLiteral
      commandLine.add((launcherUsed? "-J" : "") + "-Duser.language=" + lang);
    }
    //noinspection HardCodedStringLiteral
    final String country = System.getProperty("user.country");
    if (country != null) {
      //noinspection HardCodedStringLiteral
      commandLine.add((launcherUsed? "-J" : "") + "-Duser.country=" + country);
    }
    //noinspection HardCodedStringLiteral
    final String region = System.getProperty("user.region");
    if (region != null) {
      //noinspection HardCodedStringLiteral
      commandLine.add((launcherUsed? "-J" : "") + "-Duser.region=" + region);
    }
  }

  public static void addTargetCommandLineSwitch(final ModuleChunk chunk, final List<String> commandLine) {
    String optionValue = null;
    CompilerConfiguration config = null;
    final Module[] modules = chunk.getModules();
    for (Module module : modules) {
      if (config == null) {
        config = CompilerConfiguration.getInstance(module.getProject());
      }
      final String moduleTarget = config.getBytecodeTargetLevel(module);
      if (moduleTarget == null) {
        continue;
      }
      if (optionValue == null) {
        optionValue = moduleTarget;
      }
      else {
        if (moduleTarget.compareTo(optionValue) < 0) {
          optionValue = moduleTarget; // use the lower possible target among modules that form the chunk
        }
      }
    }
    if (optionValue != null) {
      commandLine.add("-target");
      commandLine.add(optionValue);
    }
  }

  public static void addSourceCommandLineSwitch(final Sdk jdk, LanguageLevel chunkLanguageLevel, @NonNls final List<String> commandLine) {
    final String versionString = jdk.getVersionString();
    if (StringUtil.isEmpty(versionString)) {
      throw new IllegalArgumentException(CompilerBundle.message("javac.error.unknown.jdk.version", jdk.getName()));
    }

    final LanguageLevel applicableLanguageLevel = getApplicableLanguageLevel(versionString, chunkLanguageLevel);
    if (applicableLanguageLevel.equals(LanguageLevel.JDK_1_8)) {
      commandLine.add("-source");
      commandLine.add("8");
    }
    else if (applicableLanguageLevel.equals(LanguageLevel.JDK_1_7)) {
      commandLine.add("-source");
      commandLine.add("1.7");
    }
    else if (applicableLanguageLevel.equals(LanguageLevel.JDK_1_6)) {
      commandLine.add("-source");
      commandLine.add("1.6");
    }
    else if (applicableLanguageLevel.equals(LanguageLevel.JDK_1_5)) {
      commandLine.add("-source");
      commandLine.add("1.5");
    }
    else if (applicableLanguageLevel.equals(LanguageLevel.JDK_1_4)) {
      commandLine.add("-source");
      commandLine.add("1.4");
    }
    else if (applicableLanguageLevel.equals(LanguageLevel.JDK_1_3)) {
      if (!(isOfVersion(versionString, "1.3") || isOfVersion(versionString, "1.2") || isOfVersion(versionString, "1.1"))) {
        //noinspection HardCodedStringLiteral
        commandLine.add("-source");
        commandLine.add("1.3");
      }
    }
  }

  //todo[nik] rewrite using JavaSdkVersion#getMaxLanguageLevel
  @NotNull
  public static LanguageLevel getApplicableLanguageLevel(String versionString, @NotNull LanguageLevel languageLevel) {
    final boolean is8OrNewer = isOfVersion(versionString, "1.8") || isOfVersion(versionString, "8.0");
    final boolean is7OrNewer = is8OrNewer || isOfVersion(versionString, "1.7") || isOfVersion(versionString, "7.0");
    final boolean is6OrNewer = is7OrNewer || isOfVersion(versionString, "1.6") || isOfVersion(versionString, "6.0");
    final boolean is5OrNewer = is6OrNewer || isOfVersion(versionString, "1.5") || isOfVersion(versionString, "5.0");
    final boolean is4OrNewer = is5OrNewer || isOfVersion(versionString, "1.4");
    final boolean is3OrNewer = is4OrNewer || isOfVersion(versionString, "1.3");
    final boolean is2OrNewer = is3OrNewer || isOfVersion(versionString, "1.2");
    final boolean is1OrNewer = is2OrNewer || isOfVersion(versionString, "1.0") || isOfVersion(versionString, "1.1");

    if (!is1OrNewer) {
      // unknown jdk version, cannot say anything about the corresponding language level, so leave it unchanged
      return languageLevel;
    }
    // now correct the language level to be not higher than jdk used to compile
    if (LanguageLevel.JDK_1_8.equals(languageLevel) && !is8OrNewer) {
      languageLevel = LanguageLevel.JDK_1_7;
    }
    if (LanguageLevel.JDK_1_7.equals(languageLevel) && !is7OrNewer) {
      languageLevel = LanguageLevel.JDK_1_6;
    }
    if (LanguageLevel.JDK_1_6.equals(languageLevel) && !is6OrNewer) {
      languageLevel = LanguageLevel.JDK_1_5;
    }
    if (LanguageLevel.JDK_1_5.equals(languageLevel) && !is5OrNewer) {
      languageLevel = LanguageLevel.JDK_1_4;
    }
    if (LanguageLevel.JDK_1_4.equals(languageLevel) && !is4OrNewer) {
      languageLevel = LanguageLevel.JDK_1_3;
    }
    return languageLevel;
  }

  public static boolean isOfVersion(String versionString, String checkedVersion) {
    return versionString.contains(checkedVersion);
  }

  public static <T extends Throwable> void runInContext(CompileContext context, String title, ThrowableRunnable<T> action) throws T {
    if (title != null) {
      context.getProgressIndicator().pushState();
      context.getProgressIndicator().setText(title);
    }
    try {
      action.run();
    }
    finally {
      if (title != null) {
        context.getProgressIndicator().popState();
      }
    }
  }

  public static void logDuration(final String activityName, long duration) {
    LOG.info(activityName + " took " + duration + " ms: " + duration /60000 + " min " +(duration %60000)/1000 + "sec");
  }

  public static void clearOutputDirectories(final Collection<File> outputDirectories) {
    final long start = System.currentTimeMillis();
    // do not delete directories themselves, or we'll get rootsChanged() otherwise
    final Collection<File> filesToDelete = new ArrayList<File>(outputDirectories.size() * 2);
    for (File outputDirectory : outputDirectories) {
      File[] files = outputDirectory.listFiles();
      if (files != null) {
        ContainerUtil.addAll(filesToDelete, files);
      }
    }
    if (filesToDelete.size() > 0) {
      FileUtil.asyncDelete(filesToDelete);

      // ensure output directories exist
      for (final File file : outputDirectories) {
        file.mkdirs();
      }
      final long clearStop = System.currentTimeMillis();

      refreshIODirectories(outputDirectories);

      final long refreshStop = System.currentTimeMillis();

      logDuration("Clearing output dirs", clearStop - start);
      logDuration("Refreshing output directories", refreshStop - clearStop);
    }
  }

  public static void computeIntersectingPaths(final Project project,
                                                          final Collection<VirtualFile> outputPaths,
                                                          final Collection<VirtualFile> result) {
    for (Module module : ModuleManager.getInstance(project).getModules()) {
      final ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
      final VirtualFile[] sourceRoots = rootManager.getSourceRoots();
      for (final VirtualFile outputPath : outputPaths) {
        for (VirtualFile sourceRoot : sourceRoots) {
          if (VfsUtilCore.isAncestor(outputPath, sourceRoot, true) || VfsUtilCore.isAncestor(sourceRoot, outputPath, false)) {
            result.add(outputPath);
          }
        }
      }
    }
  }

  public static boolean askUserToContinueWithNoClearing(Project project, Collection<VirtualFile> affectedOutputPaths) {
    final StringBuilder paths = new StringBuilder();
    for (final VirtualFile affectedOutputPath : affectedOutputPaths) {
      if (paths.length() > 0) {
        paths.append(",\n");
      }
      paths.append(affectedOutputPath.getPath().replace('/', File.separatorChar));
    }
    final int answer = Messages.showOkCancelDialog(project,
                                                   CompilerBundle.message("warning.sources.under.output.paths", paths.toString()),
                                                   CommonBundle.getErrorTitle(), Messages.getWarningIcon());
    if (answer == Messages.OK) { // ok
      return true;
    }
    else {
      return false;
    }
  }
}