From 2725cbd2b887d84b965e55c7f8f1b257c629cfae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Thu, 11 Jan 2024 13:20:42 +0100 Subject: [PATCH] Support javac as a compiler for Tycho Currently Tycho is strictly using ecj as a compiler but in general it might be there are situations one wants to use javac for compilation even though it might not has full features supported (e.g. package restrictions). This is an attempt to make it possible to use javac at a very basic level. --- RELEASE_NOTES.md | 1 + tycho-compiler-plugin/pom.xml | 5 ++ .../maven/plugin/AbstractCompilerMojo.java | 6 ++- .../compiler/AbstractOsgiCompilerMojo.java | 50 ++++++++++++------- .../tycho-compiler-plugin/javac/pom.xml | 36 +++++++++++++ .../javac/simple/META-INF/MANIFEST.MF | 4 ++ .../javac/simple/build.properties | 4 ++ .../javac/simple/pom.xml | 13 +++++ .../javac/simple/src/Test.java | 19 +++++++ .../tycho/test/CompilerPluginTest.java | 30 +++++++++++ 10 files changed, 147 insertions(+), 21 deletions(-) create mode 100644 tycho-its/projects/tycho-compiler-plugin/javac/pom.xml create mode 100644 tycho-its/projects/tycho-compiler-plugin/javac/simple/META-INF/MANIFEST.MF create mode 100644 tycho-its/projects/tycho-compiler-plugin/javac/simple/build.properties create mode 100644 tycho-its/projects/tycho-compiler-plugin/javac/simple/pom.xml create mode 100644 tycho-its/projects/tycho-compiler-plugin/javac/simple/src/Test.java create mode 100644 tycho-its/src/test/java/org/eclipse/tycho/test/CompilerPluginTest.java diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index ec262bbd13..cc1b9f6725 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -6,6 +6,7 @@ This page describes the noteworthy improvements provided by each release of Ecli ### backports: +- using javac as the compiler for Tycho - new `mirror-target-platform` mojo - new director mojo - support for PDE Api Tools Annotations diff --git a/tycho-compiler-plugin/pom.xml b/tycho-compiler-plugin/pom.xml index 9b1bc2378c..e0db42e133 100644 --- a/tycho-compiler-plugin/pom.xml +++ b/tycho-compiler-plugin/pom.xml @@ -86,6 +86,11 @@ bcel test + + org.codehaus.plexus + plexus-compiler-javac + 2.14.2 + diff --git a/tycho-compiler-plugin/src/main/java/copied/org/apache/maven/plugin/AbstractCompilerMojo.java b/tycho-compiler-plugin/src/main/java/copied/org/apache/maven/plugin/AbstractCompilerMojo.java index 37f94ae550..b3daec6d0c 100644 --- a/tycho-compiler-plugin/src/main/java/copied/org/apache/maven/plugin/AbstractCompilerMojo.java +++ b/tycho-compiler-plugin/src/main/java/copied/org/apache/maven/plugin/AbstractCompilerMojo.java @@ -57,6 +57,8 @@ */ public abstract class AbstractCompilerMojo extends AbstractMojo { + protected static final String JDT_COMPILER_ID = "jdt"; + public static final String DEFAULT_SOURCE_VERSION = "11"; public static final String DEFAULT_TARGET_VERSION = "11"; @@ -144,8 +146,8 @@ public abstract class AbstractCompilerMojo extends AbstractMojo { /** * The compiler id of the compiler to use. */ - @Parameter(property = "maven.compiler.compilerId", defaultValue = "jdt") - private String compilerId; + @Parameter(property = "maven.compiler.compilerId", defaultValue = JDT_COMPILER_ID) + protected String compilerId; /** * Version of the compiler to use, ex. "1.3", "1.5", if fork is set to true diff --git a/tycho-compiler-plugin/src/main/java/org/eclipse/tycho/compiler/AbstractOsgiCompilerMojo.java b/tycho-compiler-plugin/src/main/java/org/eclipse/tycho/compiler/AbstractOsgiCompilerMojo.java index e052d7cc59..5551f78b79 100644 --- a/tycho-compiler-plugin/src/main/java/org/eclipse/tycho/compiler/AbstractOsgiCompilerMojo.java +++ b/tycho-compiler-plugin/src/main/java/org/eclipse/tycho/compiler/AbstractOsgiCompilerMojo.java @@ -573,6 +573,7 @@ public List getClasspathElements() throws MojoExecutionException { final List classpath = new ArrayList<>(); Set seen = new HashSet<>(); Set includedPathes = new HashSet<>(); + boolean useAccessRules = JDT_COMPILER_ID.equals(compilerId); for (ClasspathEntry cpe : getClasspath()) { Stream classpathLocations = Stream .concat(cpe.getLocations().stream(), @@ -581,7 +582,7 @@ public List getClasspathElements() throws MojoExecutionException { .filter(AbstractOsgiCompilerMojo::isValidLocation).distinct(); classpathLocations.forEach(location -> { String path = location.getAbsolutePath(); - String entry = path + toString(cpe.getAccessRules()); + String entry = path + toString(cpe.getAccessRules(), useAccessRules); if (seen.add(entry)) { includedPathes.add(path); classpath.add(entry); @@ -646,19 +647,22 @@ protected BundleProject getBundleProject() throws MojoExecutionException { return (BundleProject) projectType; } - private String toString(Collection rules) { - StringJoiner result = new StringJoiner(RULE_SEPARATOR, "[", "]"); // include all - if (rules != null) { - for (AccessRule rule : rules) { - result.add((rule.isDiscouraged() ? "~" : "+") + rule.getPattern()); + private String toString(Collection rules, boolean useAccessRules) { + if (useAccessRules) { + StringJoiner result = new StringJoiner(RULE_SEPARATOR, "[", "]"); // include all + if (rules != null) { + for (AccessRule rule : rules) { + result.add((rule.isDiscouraged() ? "~" : "+") + rule.getPattern()); + } + result.add(RULE_EXCLUDE_ALL); + } else { + // include everything, not strictly necessary, but lets make this obvious + //result.append("[+**/*]"); + return ""; } - result.add(RULE_EXCLUDE_ALL); - } else { - // include everything, not strictly necessary, but lets make this obvious - //result.append("[+**/*]"); - return ""; + return result.toString(); } - return result.toString(); + return ""; } @Override @@ -719,7 +723,7 @@ protected CompilerConfiguration getCompilerConfiguration(List compileSou List> copy = new ArrayList<>( compilerConfiguration.getCustomCompilerArgumentsEntries()); compilerConfiguration.getCustomCompilerArgumentsEntries().clear(); - compilerConfiguration.addCompilerCustomArgument("-properties", prefsFilePath); + addCompilerCustomArgument(compilerConfiguration, "-properties", prefsFilePath); compilerConfiguration.getCustomCompilerArgumentsEntries().addAll(copy); } } @@ -738,7 +742,7 @@ protected CompilerConfiguration getCompilerConfiguration(List compileSou if (jreClasspathEntry.isModule()) { Collection modules = jreClasspathEntry.getLimitModules(); if (!modules.isEmpty()) { - compilerConfiguration.addCompilerCustomArgument("--limit-modules", String.join(",", modules)); + addCompilerCustomArgument(compilerConfiguration, "--limit-modules", String.join(",", modules)); } } } @@ -782,7 +786,7 @@ private void configureCompilerLog(CompilerConfiguration compilerConfiguration) t fileExtension = "log"; } logPath = logPath + logFileName + "." + fileExtension; - compilerConfiguration.addCompilerCustomArgument("-log", logPath); + addCompilerCustomArgument(compilerConfiguration, "-log", logPath); } private void configureBootclasspathAccessRules(CompilerConfiguration compilerConfiguration) @@ -811,8 +815,8 @@ private void configureBootclasspathAccessRules(CompilerConfiguration compilerCon .addAll(getBundleProject().getBootClasspathExtraAccessRules(DefaultReactorProject.adapt(project))); } if (!accessRules.isEmpty()) { - compilerConfiguration.addCompilerCustomArgument("org.osgi.framework.system.packages", - toString(accessRules)); + addCompilerCustomArgument(compilerConfiguration, "org.osgi.framework.system.packages", + toString(accessRules, true)); } } @@ -837,7 +841,7 @@ private void configureJavaHome(CompilerConfiguration compilerConfiguration) thro .orElseThrow(() -> new MojoExecutionException( "useJDK = BREE configured, but no toolchain of type 'jdk' with id '" + toolchainId + "' found. See https://maven.apache.org/guides/mini/guide-using-toolchains.html")); - compilerConfiguration.addCompilerCustomArgument("use.java.home", osgiToolchain.getJavaHome()); + addCompilerCustomArgument(compilerConfiguration, "use.java.home", osgiToolchain.getJavaHome()); configureBootClassPath(compilerConfiguration, osgiToolchain); } } @@ -851,7 +855,7 @@ private void configureBootClassPath(CompilerConfiguration compilerConfiguration, if (includeParent != null) { Xpp3Dom[] includes = includeParent.getChildren("include"); if (includes.length > 0) { - compilerConfiguration.addCompilerCustomArgument("-bootclasspath", scanBootclasspath( + addCompilerCustomArgument(compilerConfiguration, "-bootclasspath", scanBootclasspath( osgiToolchain.getJavaHome(), includes, bootClassPath.getChild("excludes"))); } } @@ -859,6 +863,14 @@ private void configureBootClassPath(CompilerConfiguration compilerConfiguration, } } + protected boolean addCompilerCustomArgument(CompilerConfiguration compilerConfiguration, String key, String value) { + if (JDT_COMPILER_ID.equals(compilerId)) { + compilerConfiguration.addCompilerCustomArgument(key, value); + return true; + } + return false; + } + private String scanBootclasspath(String javaHome, Xpp3Dom[] includes, Xpp3Dom excludeParent) { DirectoryScanner scanner = new DirectoryScanner(); scanner.setBasedir(javaHome); diff --git a/tycho-its/projects/tycho-compiler-plugin/javac/pom.xml b/tycho-its/projects/tycho-compiler-plugin/javac/pom.xml new file mode 100644 index 0000000000..c6de9f2183 --- /dev/null +++ b/tycho-its/projects/tycho-compiler-plugin/javac/pom.xml @@ -0,0 +1,36 @@ + + 4.0.0 + + org.eclipse.tycho.it + javac.parent + 1.0.0-SNAPSHOT + pom + + 5.0.0-SNAPSHOT + + + + + + org.eclipse.tycho + tycho-maven-plugin + ${tycho-version} + true + + + org.eclipse.tycho + tycho-compiler-plugin + ${tycho-version} + + javac + + + + + + + simple + + diff --git a/tycho-its/projects/tycho-compiler-plugin/javac/simple/META-INF/MANIFEST.MF b/tycho-its/projects/tycho-compiler-plugin/javac/simple/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..ac88e3c0dc --- /dev/null +++ b/tycho-its/projects/tycho-compiler-plugin/javac/simple/META-INF/MANIFEST.MF @@ -0,0 +1,4 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-SymbolicName: simple +Bundle-Version: 1.0.0.qualifier diff --git a/tycho-its/projects/tycho-compiler-plugin/javac/simple/build.properties b/tycho-its/projects/tycho-compiler-plugin/javac/simple/build.properties new file mode 100644 index 0000000000..34d2e4d2da --- /dev/null +++ b/tycho-its/projects/tycho-compiler-plugin/javac/simple/build.properties @@ -0,0 +1,4 @@ +source.. = src/ +output.. = bin/ +bin.includes = META-INF/,\ + . diff --git a/tycho-its/projects/tycho-compiler-plugin/javac/simple/pom.xml b/tycho-its/projects/tycho-compiler-plugin/javac/simple/pom.xml new file mode 100644 index 0000000000..89b6ed014a --- /dev/null +++ b/tycho-its/projects/tycho-compiler-plugin/javac/simple/pom.xml @@ -0,0 +1,13 @@ + + 4.0.0 + + org.eclipse.tycho.it + javac.parent + 1.0.0-SNAPSHOT + + simple + + eclipse-plugin + + \ No newline at end of file diff --git a/tycho-its/projects/tycho-compiler-plugin/javac/simple/src/Test.java b/tycho-its/projects/tycho-compiler-plugin/javac/simple/src/Test.java new file mode 100644 index 0000000000..461755c786 --- /dev/null +++ b/tycho-its/projects/tycho-compiler-plugin/javac/simple/src/Test.java @@ -0,0 +1,19 @@ +/******************************************************************************* + * Copyright (c) 2012 SAP AG and others. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * SAP AG - initial API and implementation + *******************************************************************************/ + +public class Test +{ + public static void main(String[] args) { + int a = 0; + } +} diff --git a/tycho-its/src/test/java/org/eclipse/tycho/test/CompilerPluginTest.java b/tycho-its/src/test/java/org/eclipse/tycho/test/CompilerPluginTest.java new file mode 100644 index 0000000000..fedcd0cce0 --- /dev/null +++ b/tycho-its/src/test/java/org/eclipse/tycho/test/CompilerPluginTest.java @@ -0,0 +1,30 @@ +/******************************************************************************* + * Copyright (c) 2024 Christoph Läubrich and others. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Christoph Läubrich - initial API and implementation + *******************************************************************************/ +package org.eclipse.tycho.test; + +import org.apache.maven.it.Verifier; +import org.junit.Test; + +/** + * Test for the tycho-compiler-plugin + */ +public class CompilerPluginTest extends AbstractTychoIntegrationTest { + + @Test + public void testJavac() throws Exception { + Verifier verifier = getVerifier("tycho-compiler-plugin/javac", true, true); + verifier.executeGoal("compile"); + verifier.verifyErrorFreeLog(); + } + +}