From 73c53cd7e648af8f1787e8d7a4985f48e14be801 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. --- tycho-compiler-plugin/pom.xml | 5 +++ .../maven/plugin/AbstractCompilerMojo.java | 6 ++-- .../compiler/AbstractOsgiCompilerMojo.java | 18 ++++++---- .../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 ++++++++++++++++ 9 files changed, 127 insertions(+), 8 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/tycho-compiler-plugin/pom.xml b/tycho-compiler-plugin/pom.xml index c99d3203ca..70cff57273 100644 --- a/tycho-compiler-plugin/pom.xml +++ b/tycho-compiler-plugin/pom.xml @@ -87,6 +87,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..3d9256fe7e 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 @@ -719,7 +719,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 +738,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 +782,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,7 +811,7 @@ private void configureBootclasspathAccessRules(CompilerConfiguration compilerCon .addAll(getBundleProject().getBootClasspathExtraAccessRules(DefaultReactorProject.adapt(project))); } if (!accessRules.isEmpty()) { - compilerConfiguration.addCompilerCustomArgument("org.osgi.framework.system.packages", + addCompilerCustomArgument(compilerConfiguration, "org.osgi.framework.system.packages", toString(accessRules)); } } @@ -837,7 +837,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 +851,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 +859,12 @@ private void configureBootClassPath(CompilerConfiguration compilerConfiguration, } } + protected void addCompilerCustomArgument(CompilerConfiguration compilerConfiguration, String key, String value) { + if (JDT_COMPILER_ID.equals(compilerId)) { + compilerConfiguration.addCompilerCustomArgument(key, value); + } + } + 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(); + } + +}