use of org.jetbrains.jps.model.module.JpsModule in project intellij-community by JetBrains.
the class JavaBuilder method compileJava.
private boolean compileJava(CompileContext context, ModuleChunk chunk, Collection<File> files, Collection<File> originalClassPath, Collection<File> originalPlatformCp, Collection<File> sourcePath, DiagnosticOutputConsumer diagnosticSink, OutputFileConsumer outputSink, JavaCompilingTool compilingTool) throws Exception {
final TasksCounter counter = new TasksCounter();
COUNTER_KEY.set(context, counter);
final JpsJavaExtensionService javaExt = JpsJavaExtensionService.getInstance();
final JpsJavaCompilerConfiguration compilerConfig = javaExt.getCompilerConfiguration(context.getProjectDescriptor().getProject());
assert compilerConfig != null;
final Set<JpsModule> modules = chunk.getModules();
ProcessorConfigProfile profile = null;
if (modules.size() == 1) {
profile = compilerConfig.getAnnotationProcessingProfile(modules.iterator().next());
} else {
String message = validateCycle(chunk, javaExt, compilerConfig, modules);
if (message != null) {
diagnosticSink.report(new PlainMessageDiagnostic(Diagnostic.Kind.ERROR, message));
return true;
}
}
final Map<File, Set<File>> outs = buildOutputDirectoriesMap(context, chunk);
try {
final int targetLanguageLevel = JpsJavaSdkType.parseVersion(getLanguageLevel(chunk.getModules().iterator().next()));
final boolean shouldForkJavac = shouldForkCompilerProcess(context, chunk, targetLanguageLevel);
final boolean hasModules = targetLanguageLevel >= 9 && getJavaModuleIndex(context).hasJavaModules(modules);
// when forking external javac, compilers from SDK 1.6 and higher are supported
Pair<String, Integer> forkSdk = null;
if (shouldForkJavac) {
forkSdk = getForkedJavacSdk(chunk, targetLanguageLevel);
if (forkSdk == null) {
String text = "Cannot start javac process for " + chunk.getName() + ": unknown JDK home path.\nPlease check project configuration.";
diagnosticSink.report(new PlainMessageDiagnostic(Diagnostic.Kind.ERROR, text));
return true;
}
}
final int compilerSdkVersion = forkSdk == null ? getCompilerSdkVersion(context) : forkSdk.getSecond();
final List<String> options = getCompilationOptions(compilerSdkVersion, context, chunk, profile, compilingTool);
if (LOG.isDebugEnabled()) {
LOG.debug("Compiling chunk [" + chunk.getName() + "] with options: \"" + StringUtil.join(options, " ") + "\"");
}
Collection<File> platformCp = calcEffectivePlatformCp(originalPlatformCp, options, compilingTool);
if (platformCp == null) {
String text = "Compact compilation profile was requested, but target platform for module \"" + chunk.getName() + "\"" + " differs from javac's platform (" + System.getProperty("java.version") + ")\n" + "Compilation profiles are not supported for such configuration";
context.processMessage(new CompilerMessage(BUILDER_NAME, BuildMessage.Kind.ERROR, text));
return true;
}
Collection<File> classPath = originalClassPath;
Collection<File> modulePath = Collections.emptyList();
if (hasModules) {
// in Java 9, named modules are not allowed to read classes from the classpath
// moreover, the compiler requires all transitive dependencies to be on the module path
modulePath = ProjectPaths.getCompilationModulePath(chunk, false);
classPath = Collections.emptyList();
}
if (!platformCp.isEmpty()) {
final int chunkSdkVersion;
if (hasModules) {
modulePath = newArrayList(concat(platformCp, modulePath));
platformCp = Collections.emptyList();
} else if ((chunkSdkVersion = getChunkSdkVersion(chunk)) >= 9) {
// if chunk's SDK is 9 or higher, there is no way to specify full platform classpath
// because platform classes are stored in jimage binary files with unknown format.
// Because of this we are clearing platform classpath so that javac will resolve against its own boot classpath
// and prepending additional jars from the JDK configuration to compilation classpath
classPath = newArrayList(concat(platformCp, classPath));
platformCp = Collections.emptyList();
} else if (shouldUseReleaseOption(context, compilerSdkVersion, chunkSdkVersion, targetLanguageLevel)) {
final Collection<File> joined = new ArrayList<>(classPath.size() + 1);
for (File file : platformCp) {
// include only additional jars from sdk distribution, e.g. tools.jar
if (!FileUtil.toSystemIndependentName(file.getAbsolutePath()).contains("/jre/")) {
joined.add(file);
}
}
joined.addAll(classPath);
classPath = joined;
platformCp = Collections.emptyList();
}
}
final ClassProcessingConsumer classesConsumer = new ClassProcessingConsumer(context, outputSink);
final boolean rc;
if (!shouldForkJavac) {
updateCompilerUsageStatistics(context, compilingTool.getDescription(), chunk);
rc = JavacMain.compile(options, files, classPath, platformCp, modulePath, sourcePath, outs, diagnosticSink, classesConsumer, context.getCancelStatus(), compilingTool);
} else {
updateCompilerUsageStatistics(context, "javac " + forkSdk.getSecond(), chunk);
final List<String> vmOptions = getCompilationVMOptions(context, compilingTool);
final ExternalJavacManager server = ensureJavacServerStarted(context);
rc = server.forkJavac(forkSdk.getFirst(), getExternalJavacHeapSize(context), vmOptions, options, platformCp, classPath, modulePath, sourcePath, files, outs, diagnosticSink, classesConsumer, compilingTool, context.getCancelStatus());
}
return rc;
} finally {
counter.await();
}
}
use of org.jetbrains.jps.model.module.JpsModule in project intellij-community by JetBrains.
the class JavaBuilder method getCompilationOptions.
private static List<String> getCompilationOptions(int compilerSdkVersion, CompileContext context, ModuleChunk chunk, @Nullable ProcessorConfigProfile profile, @NotNull JavaCompilingTool compilingTool) {
List<String> cached = JAVAC_OPTIONS.get(context);
if (cached == null) {
loadCommonJavacOptions(context, compilingTool);
cached = JAVAC_OPTIONS.get(context);
assert cached != null : context;
}
List<String> options = new ArrayList<>();
JpsModule module = chunk.representativeTarget().getModule();
File baseDirectory = JpsModelSerializationDataService.getBaseDirectory(module);
if (baseDirectory != null) {
//this is a temporary workaround to allow passing per-module compiler options for Eclipse compiler in form
// -properties $MODULE_DIR$/.settings/org.eclipse.jdt.core.prefs
String stringToReplace = "$" + PathMacroUtil.MODULE_DIR_MACRO_NAME + "$";
String moduleDirPath = FileUtil.toCanonicalPath(baseDirectory.getAbsolutePath());
for (String s : cached) {
options.add(StringUtil.replace(s, stringToReplace, moduleDirPath));
}
} else {
options.addAll(cached);
}
addCompilationOptions(compilerSdkVersion, options, context, chunk, profile);
return options;
}
use of org.jetbrains.jps.model.module.JpsModule in project intellij-community by JetBrains.
the class JavaBuilder method addCrossCompilationOptions.
private static void addCrossCompilationOptions(int compilerSdkVersion, List<String> options, CompileContext context, ModuleChunk chunk) {
final JpsJavaCompilerConfiguration compilerConfiguration = JpsJavaExtensionService.getInstance().getOrCreateCompilerConfiguration(context.getProjectDescriptor().getProject());
final String langLevel = getLanguageLevel(chunk.getModules().iterator().next());
final int chunkSdkVersion = getChunkSdkVersion(chunk);
final int targetLanguageLevel = JpsJavaSdkType.parseVersion(langLevel);
if (shouldUseReleaseOption(context, compilerSdkVersion, chunkSdkVersion, targetLanguageLevel)) {
options.add(getReleaseOptionName());
options.add(String.valueOf(targetLanguageLevel));
return;
}
// using older -source, -target and -bootclasspath options
if (!StringUtil.isEmpty(langLevel)) {
options.add("-source");
options.add(langLevel);
}
String bytecodeTarget = null;
for (JpsModule module : chunk.getModules()) {
final String moduleTarget = compilerConfiguration.getByteCodeTargetLevel(module.getName());
if (moduleTarget == null) {
continue;
}
if (bytecodeTarget == null) {
bytecodeTarget = moduleTarget;
} else {
if (moduleTarget.compareTo(bytecodeTarget) < 0) {
// use the lower possible target among modules that form the chunk
bytecodeTarget = moduleTarget;
}
}
}
if (bytecodeTarget == null) {
if (!StringUtil.isEmpty(langLevel)) {
// according to IDEA rule: if not specified explicitly, set target to be the same as source language level
bytecodeTarget = langLevel;
} else {
// last resort and backward compatibility:
// check if user explicitly defined bytecode target in additional compiler options
bytecodeTarget = USER_DEFINED_BYTECODE_TARGET.get(context);
}
}
if (bytecodeTarget != null) {
options.add("-target");
if (chunkSdkVersion > 0 && compilerSdkVersion > chunkSdkVersion) {
// if compiler is newer than module JDK
final int userSpecifiedTargetVersion = JpsJavaSdkType.parseVersion(bytecodeTarget);
if (userSpecifiedTargetVersion > 0 && userSpecifiedTargetVersion <= compilerSdkVersion) {
// if user-specified bytecode version can be determined and is supported by compiler
if (userSpecifiedTargetVersion > chunkSdkVersion) {
// and user-specified bytecode target level is higher than the highest one supported by the target JDK,
// force compiler to use highest-available bytecode target version that is supported by the chunk JDK.
bytecodeTarget = "1." + chunkSdkVersion;
}
}
// otherwise let compiler display compilation error about incorrectly set bytecode target version
}
options.add(bytecodeTarget);
} else {
if (chunkSdkVersion > 0 && compilerSdkVersion > chunkSdkVersion) {
// force lower bytecode target level to match the version of sdk assigned to this chunk
options.add("-target");
options.add("1." + chunkSdkVersion);
}
}
}
use of org.jetbrains.jps.model.module.JpsModule in project intellij-community by JetBrains.
the class BuildTargetConfiguration method isTargetDirty.
public boolean isTargetDirty(CompileContext context) {
final String currentState = getCurrentState(context);
if (!currentState.equals(myConfiguration)) {
LOG.debug(myTarget + " configuration was changed:");
LOG.debug("Old:");
LOG.debug(myConfiguration);
LOG.debug("New:");
LOG.debug(currentState);
LOG.debug(myTarget + " will be recompiled");
if (myTarget instanceof ModuleBuildTarget) {
final JpsModule module = ((ModuleBuildTarget) myTarget).getModule();
synchronized (MODULES_WITH_TARGET_CONFIG_CHANGED_KEY) {
Set<JpsModule> modules = MODULES_WITH_TARGET_CONFIG_CHANGED_KEY.get(context);
if (modules == null) {
MODULES_WITH_TARGET_CONFIG_CHANGED_KEY.set(context, modules = new THashSet<>());
}
modules.add(module);
}
}
return true;
}
return false;
}
use of org.jetbrains.jps.model.module.JpsModule in project intellij-community by JetBrains.
the class CommonTest method testMoveClassToDependentModule.
public void testMoveClassToDependentModule() throws Exception {
JpsModule moduleA = addModule("moduleA", "moduleA/src");
JpsModule moduleB = addModule("moduleB", "moduleB/src");
JpsModuleRootModificationUtil.addDependency(moduleB, moduleA);
doTestBuild(1).assertSuccessful();
}
Aggregations