use of com.intellij.compiler.CompilerConfiguration in project intellij-community by JetBrains.
the class MavenResourceCompilerConfigurationGenerator method addNonMavenResources.
private void addNonMavenResources(MavenProjectConfiguration projectCfg) {
Set<VirtualFile> processedRoots = new HashSet<>();
for (MavenProject project : myMavenProjectsManager.getProjects()) {
for (String dir : ContainerUtil.concat(project.getSources(), project.getTestSources())) {
VirtualFile file = LocalFileSystem.getInstance().findFileByPath(dir);
if (file != null) {
processedRoots.add(file);
}
}
for (MavenResource resource : ContainerUtil.concat(project.getResources(), project.getTestResources())) {
VirtualFile file = LocalFileSystem.getInstance().findFileByPath(resource.getDirectory());
if (file != null) {
processedRoots.add(file);
}
}
}
CompilerConfiguration compilerConfiguration = CompilerConfiguration.getInstance(myProject);
for (Module module : ModuleManager.getInstance(myProject).getModules()) {
if (!myMavenProjectsManager.isMavenizedModule(module))
continue;
for (ContentEntry contentEntry : ModuleRootManager.getInstance(module).getContentEntries()) {
for (SourceFolder folder : contentEntry.getSourceFolders()) {
VirtualFile file = folder.getFile();
if (file == null)
continue;
if (!compilerConfiguration.isExcludedFromCompilation(file) && !isUnderRoots(processedRoots, file)) {
MavenModuleResourceConfiguration configuration = projectCfg.moduleConfigurations.get(module.getName());
if (configuration == null)
continue;
List<ResourceRootConfiguration> resourcesList = folder.isTestSource() ? configuration.testResources : configuration.resources;
final ResourceRootConfiguration cfg = new ResourceRootConfiguration();
cfg.directory = FileUtil.toSystemIndependentName(FileUtil.toSystemIndependentName(file.getPath()));
CompilerModuleExtension compilerModuleExtension = CompilerModuleExtension.getInstance(module);
if (compilerModuleExtension == null)
continue;
String compilerOutputUrl = folder.isTestSource() ? compilerModuleExtension.getCompilerOutputUrlForTests() : compilerModuleExtension.getCompilerOutputUrl();
cfg.targetPath = VfsUtilCore.urlToPath(compilerOutputUrl);
convertIdeaExcludesToMavenExcludes(cfg, (CompilerConfigurationImpl) compilerConfiguration);
resourcesList.add(cfg);
}
}
}
}
}
use of com.intellij.compiler.CompilerConfiguration in project intellij-community by JetBrains.
the class CompileAction method update.
public void update(AnActionEvent e) {
super.update(e);
Presentation presentation = e.getPresentation();
if (!presentation.isEnabled()) {
return;
}
presentation.setText(ActionsBundle.actionText(RECOMPILE_FILES_ID_MOD));
presentation.setVisible(true);
Project project = e.getProject();
if (project == null) {
presentation.setEnabled(false);
return;
}
CompilerConfiguration compilerConfiguration = CompilerConfiguration.getInstance(project);
final Module module = e.getData(LangDataKeys.MODULE_CONTEXT);
boolean forFiles = false;
final VirtualFile[] files = getCompilableFiles(project, e.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY));
if (module == null && files.length == 0) {
presentation.setEnabled(false);
presentation.setVisible(!ActionPlaces.isPopupPlace(e.getPlace()));
return;
}
String elementDescription = null;
if (module != null) {
elementDescription = CompilerBundle.message("action.compile.description.module", module.getName());
} else {
PsiPackage aPackage = null;
if (files.length == 1) {
final PsiDirectory directory = PsiManager.getInstance(project).findDirectory(files[0]);
if (directory != null) {
aPackage = JavaDirectoryService.getInstance().getPackage(directory);
}
} else {
PsiElement element = e.getData(CommonDataKeys.PSI_ELEMENT);
if (element instanceof PsiPackage) {
aPackage = (PsiPackage) element;
}
}
if (aPackage != null) {
String name = aPackage.getQualifiedName();
if (name.length() == 0) {
//noinspection HardCodedStringLiteral
name = "<default>";
}
elementDescription = "'" + name + "'";
} else if (files.length == 1) {
forFiles = true;
final VirtualFile file = files[0];
FileType fileType = file.getFileType();
if (CompilerManager.getInstance(project).isCompilableFileType(fileType) || compilerConfiguration.isCompilableResourceFile(project, file)) {
elementDescription = "'" + file.getName() + "'";
} else {
if (!ActionPlaces.isMainMenuOrActionSearch(e.getPlace())) {
// the action should be invisible in popups for non-java files
presentation.setEnabled(false);
presentation.setVisible(false);
return;
}
}
} else {
forFiles = true;
elementDescription = CompilerBundle.message("action.compile.description.selected.files");
}
}
if (elementDescription == null) {
presentation.setEnabled(false);
return;
}
presentation.setText(createPresentationText(elementDescription, forFiles), true);
presentation.setEnabled(true);
}
use of com.intellij.compiler.CompilerConfiguration in project intellij-community by JetBrains.
the class CompileAction method getCompilableFiles.
private static VirtualFile[] getCompilableFiles(Project project, VirtualFile[] files) {
if (files == null || files.length == 0) {
return VirtualFile.EMPTY_ARRAY;
}
final PsiManager psiManager = PsiManager.getInstance(project);
final CompilerConfiguration compilerConfiguration = CompilerConfiguration.getInstance(project);
final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
final CompilerManager compilerManager = CompilerManager.getInstance(project);
final List<VirtualFile> filesToCompile = new ArrayList<>();
for (final VirtualFile file : files) {
if (!fileIndex.isInSourceContent(file)) {
continue;
}
if (!file.isInLocalFileSystem()) {
continue;
}
if (file.isDirectory()) {
final PsiDirectory directory = psiManager.findDirectory(file);
if (directory == null || JavaDirectoryService.getInstance().getPackage(directory) == null) {
continue;
}
} else {
FileType fileType = file.getFileType();
if (!(compilerManager.isCompilableFileType(fileType) || compilerConfiguration.isCompilableResourceFile(project, file))) {
continue;
}
}
filesToCompile.add(file);
}
return VfsUtilCore.toVirtualFileArray(filesToCompile);
}
use of com.intellij.compiler.CompilerConfiguration in project intellij-community by JetBrains.
the class BuildManager method launchBuildProcess.
private OSProcessHandler launchBuildProcess(Project project, final int port, final UUID sessionId, boolean requestProjectPreload) throws ExecutionException {
final String compilerPath;
final String vmExecutablePath;
JavaSdkVersion sdkVersion = null;
final String forcedCompiledJdkHome = Registry.stringValue(COMPILER_PROCESS_JDK_PROPERTY);
if (StringUtil.isEmptyOrSpaces(forcedCompiledJdkHome)) {
// choosing sdk with which the build process should be run
final Pair<Sdk, JavaSdkVersion> pair = getBuildProcessRuntimeSdk(project);
final Sdk projectJdk = pair.first;
sdkVersion = pair.second;
final Sdk internalJdk = JavaAwareProjectJdkTableImpl.getInstanceEx().getInternalJdk();
// validate tools.jar presence
final JavaSdkType projectJdkType = (JavaSdkType) projectJdk.getSdkType();
if (FileUtil.pathsEqual(projectJdk.getHomePath(), internalJdk.getHomePath())) {
// important: because internal JDK can be either JDK or JRE,
// this is the most universal way to obtain tools.jar path in this particular case
final JavaCompiler systemCompiler = ToolProvider.getSystemJavaCompiler();
if (systemCompiler == null) {
throw new ExecutionException("No system java compiler is provided by the JRE. Make sure tools.jar is present in IntelliJ IDEA classpath.");
}
compilerPath = ClasspathBootstrap.getResourcePath(systemCompiler.getClass());
} else {
compilerPath = projectJdkType.getToolsPath(projectJdk);
if (compilerPath == null) {
throw new ExecutionException("Cannot determine path to 'tools.jar' library for " + projectJdk.getName() + " (" + projectJdk.getHomePath() + ")");
}
}
vmExecutablePath = projectJdkType.getVMExecutablePath(projectJdk);
} else {
compilerPath = new File(forcedCompiledJdkHome, "lib/tools.jar").getAbsolutePath();
vmExecutablePath = new File(forcedCompiledJdkHome, "bin/java").getAbsolutePath();
}
final CompilerConfiguration projectConfig = CompilerConfiguration.getInstance(project);
final CompilerWorkspaceConfiguration config = CompilerWorkspaceConfiguration.getInstance(project);
final GeneralCommandLine cmdLine = new GeneralCommandLine();
cmdLine.setExePath(vmExecutablePath);
//cmdLine.addParameter("-XX:MaxPermSize=150m");
//cmdLine.addParameter("-XX:ReservedCodeCacheSize=64m");
boolean isProfilingMode = false;
String userDefinedHeapSize = null;
final List<String> userAdditionalOptionsList = new SmartList<>();
final String userAdditionalVMOptions = config.COMPILER_PROCESS_ADDITIONAL_VM_OPTIONS;
final boolean userLocalOptionsActive = !StringUtil.isEmptyOrSpaces(userAdditionalVMOptions);
final String additionalOptions = userLocalOptionsActive ? userAdditionalVMOptions : projectConfig.getBuildProcessVMOptions();
if (!StringUtil.isEmptyOrSpaces(additionalOptions)) {
final StringTokenizer tokenizer = new StringTokenizer(additionalOptions, " ", false);
while (tokenizer.hasMoreTokens()) {
final String option = tokenizer.nextToken();
if (StringUtil.startsWithIgnoreCase(option, "-Xmx")) {
if (userLocalOptionsActive) {
userDefinedHeapSize = option;
}
} else {
if ("-Dprofiling.mode=true".equals(option)) {
isProfilingMode = true;
}
userAdditionalOptionsList.add(option);
}
}
}
if (userDefinedHeapSize != null) {
cmdLine.addParameter(userDefinedHeapSize);
} else {
final int heapSize = projectConfig.getBuildProcessHeapSize(JavacConfiguration.getOptions(project, JavacConfiguration.class).MAXIMUM_HEAP_SIZE);
cmdLine.addParameter("-Xmx" + heapSize + "m");
}
if (SystemInfo.isMac && sdkVersion != null && JavaSdkVersion.JDK_1_6.equals(sdkVersion) && Registry.is("compiler.process.32bit.vm.on.mac")) {
// unfortunately -d32 is supported on jdk 1.6 only
cmdLine.addParameter("-d32");
}
cmdLine.addParameter("-Djava.awt.headless=true");
if (sdkVersion != null && sdkVersion.ordinal() < JavaSdkVersion.JDK_1_9.ordinal()) {
//-Djava.endorsed.dirs is not supported in JDK 9+, may result in abnormal process termination
// turn off all jre customizations for predictable behaviour
cmdLine.addParameter("-Djava.endorsed.dirs=\"\"");
}
if (IS_UNIT_TEST_MODE) {
cmdLine.addParameter("-Dtest.mode=true");
}
// always run eclipse compiler in single-threaded mode
cmdLine.addParameter("-Djdt.compiler.useSingleThread=true");
if (requestProjectPreload) {
cmdLine.addParameter("-Dpreload.project.path=" + FileUtil.toCanonicalPath(getProjectPath(project)));
cmdLine.addParameter("-Dpreload.config.path=" + FileUtil.toCanonicalPath(PathManager.getOptionsPath()));
}
final String shouldGenerateIndex = System.getProperty(GlobalOptions.GENERATE_CLASSPATH_INDEX_OPTION);
if (shouldGenerateIndex != null) {
cmdLine.addParameter("-D" + GlobalOptions.GENERATE_CLASSPATH_INDEX_OPTION + "=" + shouldGenerateIndex);
}
cmdLine.addParameter("-D" + GlobalOptions.COMPILE_PARALLEL_OPTION + "=" + Boolean.toString(config.PARALLEL_COMPILATION));
cmdLine.addParameter("-D" + GlobalOptions.REBUILD_ON_DEPENDENCY_CHANGE_OPTION + "=" + Boolean.toString(config.REBUILD_ON_DEPENDENCY_CHANGE));
if (Boolean.TRUE.equals(Boolean.valueOf(System.getProperty("java.net.preferIPv4Stack", "false")))) {
cmdLine.addParameter("-Djava.net.preferIPv4Stack=true");
}
// this will make netty initialization faster on some systems
cmdLine.addParameter("-Dio.netty.initialSeedUniquifier=" + ThreadLocalRandom.getInitialSeedUniquifier());
for (String option : userAdditionalOptionsList) {
cmdLine.addParameter(option);
}
if (isProfilingMode) {
cmdLine.addParameter("-agentlib:yjpagent=disablealloc,delay=10000,sessionname=ExternalBuild");
}
// debugging
int debugPort = -1;
if (myBuildProcessDebuggingEnabled) {
debugPort = Registry.intValue("compiler.process.debug.port");
if (debugPort <= 0) {
try {
debugPort = NetUtils.findAvailableSocketPort();
} catch (IOException e) {
throw new ExecutionException("Cannot find free port to debug build process", e);
}
}
if (debugPort > 0) {
cmdLine.addParameter("-XX:+HeapDumpOnOutOfMemoryError");
cmdLine.addParameter("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=" + debugPort);
}
}
if (!Registry.is("compiler.process.use.memory.temp.cache")) {
cmdLine.addParameter("-D" + GlobalOptions.USE_MEMORY_TEMP_CACHE_OPTION + "=false");
}
// javac's VM should use the same default locale that IDEA uses in order for javac to print messages in 'correct' language
cmdLine.setCharset(mySystemCharset);
cmdLine.addParameter("-D" + CharsetToolkit.FILE_ENCODING_PROPERTY + "=" + mySystemCharset.name());
cmdLine.addParameter("-D" + JpsGlobalLoader.FILE_TYPES_COMPONENT_NAME_KEY + "=" + FileTypeManagerImpl.getFileTypeComponentName());
String[] propertiesToPass = { "user.language", "user.country", "user.region", PathManager.PROPERTY_PATHS_SELECTOR, "idea.case.sensitive.fs" };
for (String name : propertiesToPass) {
final String value = System.getProperty(name);
if (value != null) {
cmdLine.addParameter("-D" + name + "=" + value);
}
}
cmdLine.addParameter("-D" + PathManager.PROPERTY_HOME_PATH + "=" + PathManager.getHomePath());
cmdLine.addParameter("-D" + PathManager.PROPERTY_CONFIG_PATH + "=" + PathManager.getConfigPath());
cmdLine.addParameter("-D" + PathManager.PROPERTY_PLUGINS_PATH + "=" + PathManager.getPluginsPath());
cmdLine.addParameter("-D" + GlobalOptions.LOG_DIR_OPTION + "=" + FileUtil.toSystemIndependentName(getBuildLogDirectory().getAbsolutePath()));
cmdLine.addParameters(myFallbackJdkParams);
cmdLine.addParameter("-Dio.netty.noUnsafe=true");
final File workDirectory = getBuildSystemDirectory();
//noinspection ResultOfMethodCallIgnored
workDirectory.mkdirs();
final File projectSystemRoot = getProjectSystemDirectory(project);
if (projectSystemRoot != null) {
cmdLine.addParameter("-Djava.io.tmpdir=" + FileUtil.toSystemIndependentName(projectSystemRoot.getPath()) + "/" + TEMP_DIR_NAME);
}
for (BuildProcessParametersProvider provider : project.getExtensions(BuildProcessParametersProvider.EP_NAME)) {
final List<String> args = provider.getVMArguments();
cmdLine.addParameters(args);
}
@SuppressWarnings("UnnecessaryFullyQualifiedName") final Class<?> launcherClass = org.jetbrains.jps.cmdline.Launcher.class;
final List<String> launcherCp = new ArrayList<>();
launcherCp.add(ClasspathBootstrap.getResourcePath(launcherClass));
launcherCp.addAll(BuildProcessClasspathManager.getLauncherClasspath(project));
launcherCp.add(compilerPath);
ClasspathBootstrap.appendJavaCompilerClasspath(launcherCp, shouldIncludeEclipseCompiler(projectConfig));
cmdLine.addParameter("-classpath");
cmdLine.addParameter(classpathToString(launcherCp));
cmdLine.addParameter(launcherClass.getName());
final List<String> cp = ClasspathBootstrap.getBuildProcessApplicationClasspath();
cp.addAll(myClasspathManager.getBuildProcessPluginsClasspath(project));
if (isProfilingMode) {
cp.add(new File(workDirectory, "yjp-controller-api-redist.jar").getPath());
}
cmdLine.addParameter(classpathToString(cp));
cmdLine.addParameter(BuildMain.class.getName());
cmdLine.addParameter(Boolean.valueOf(System.getProperty("java.net.preferIPv6Addresses", "false")) ? "::1" : "127.0.0.1");
cmdLine.addParameter(Integer.toString(port));
cmdLine.addParameter(sessionId.toString());
cmdLine.addParameter(FileUtil.toSystemIndependentName(workDirectory.getPath()));
cmdLine.setWorkDirectory(workDirectory);
try {
ApplicationManager.getApplication().getMessageBus().syncPublisher(BuildManagerListener.TOPIC).beforeBuildProcessStarted(project, sessionId);
} catch (Throwable e) {
LOG.error(e);
}
final OSProcessHandler processHandler = new OSProcessHandler(cmdLine) {
@Override
protected boolean shouldDestroyProcessRecursively() {
return true;
}
@NotNull
@Override
protected BaseOutputReader.Options readerOptions() {
return BaseOutputReader.Options.BLOCKING;
}
};
processHandler.addProcessListener(new ProcessAdapter() {
@Override
public void onTextAvailable(ProcessEvent event, Key outputType) {
// re-translate builder's output to idea.log
final String text = event.getText();
if (!StringUtil.isEmptyOrSpaces(text)) {
if (ProcessOutputTypes.SYSTEM.equals(outputType)) {
if (LOG.isDebugEnabled()) {
LOG.debug("BUILDER_PROCESS [" + outputType.toString() + "]: " + text.trim());
}
} else {
LOG.info("BUILDER_PROCESS [" + outputType.toString() + "]: " + text.trim());
}
}
}
});
if (debugPort > 0) {
processHandler.putUserData(COMPILER_PROCESS_DEBUG_PORT, debugPort);
}
return processHandler;
}
use of com.intellij.compiler.CompilerConfiguration in project intellij-community by JetBrains.
the class ArtifactUtil method findSourceFilesByOutputPath.
public static List<VirtualFile> findSourceFilesByOutputPath(CompositePackagingElement<?> parent, final String outputPath, final PackagingElementResolvingContext context, final ArtifactType artifactType) {
final String path = StringUtil.trimStart(outputPath, "/");
if (path.isEmpty()) {
return Collections.emptyList();
}
int i = path.indexOf('/');
final String firstName = i != -1 ? path.substring(0, i) : path;
final String tail = i != -1 ? path.substring(i + 1) : "";
final List<VirtualFile> result = new SmartList<>();
processElementsWithSubstitutions(parent.getChildren(), context, artifactType, PackagingElementPath.EMPTY, new PackagingElementProcessor<PackagingElement<?>>() {
@Override
public boolean process(@NotNull PackagingElement<?> element, @NotNull PackagingElementPath elementPath) {
//todo[nik] replace by method findSourceFile() in PackagingElement
if (element instanceof CompositePackagingElement) {
final CompositePackagingElement<?> compositeElement = (CompositePackagingElement<?>) element;
if (firstName.equals(compositeElement.getName())) {
result.addAll(findSourceFilesByOutputPath(compositeElement, tail, context, artifactType));
}
} else if (element instanceof FileCopyPackagingElement) {
final FileCopyPackagingElement fileCopyElement = (FileCopyPackagingElement) element;
if (firstName.equals(fileCopyElement.getOutputFileName()) && tail.isEmpty()) {
ContainerUtil.addIfNotNull(result, fileCopyElement.findFile());
}
} else if (element instanceof DirectoryCopyPackagingElement || element instanceof ExtractedDirectoryPackagingElement) {
final VirtualFile sourceRoot = ((FileOrDirectoryCopyPackagingElement<?>) element).findFile();
if (sourceRoot != null) {
ContainerUtil.addIfNotNull(result, sourceRoot.findFileByRelativePath(path));
}
} else if (element instanceof ModuleOutputPackagingElement) {
final CompilerConfiguration compilerConfiguration = CompilerConfiguration.getInstance(context.getProject());
for (VirtualFile sourceRoot : ((ModuleOutputPackagingElement) element).getSourceRoots(context)) {
final VirtualFile sourceFile = sourceRoot.findFileByRelativePath(path);
if (sourceFile != null && compilerConfiguration.isResourceFile(sourceFile)) {
result.add(sourceFile);
}
}
}
return true;
}
});
return result;
}
Aggregations