Skip to content

Commit b8d28f7

Browse files
committed
Groovy Compiler options for bytecode version, preview features and ast transoformations
1 parent 9a26ea3 commit b8d28f7

File tree

5 files changed

+76
-29
lines changed

5 files changed

+76
-29
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package mill.groovylib.worker.api
2+
3+
case class GroovyCompilerConfiguration (
4+
enablePreview: Boolean = false,
5+
disabledGlobalAstTransformations: Set[String]= Set.empty,
6+
targetBytecode: Option[String]= None)

libs/groovylib/api/src/mill/groovylib/worker/api/GroovyWorker.scala

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package mill.groovylib.worker.api
22

3-
import mill.api.TaskCtx
4-
import mill.api.Result
3+
import mill.api.{Result, TaskCtx}
54
import mill.javalib.api.CompilationResult
65

76
/**
@@ -20,7 +19,8 @@ trait GroovyWorker {
2019
def compileGroovyStubs(
2120
sourceFiles: Seq[os.Path],
2221
classpath: Seq[os.Path],
23-
outputDir: os.Path
22+
outputDir: os.Path,
23+
config: GroovyCompilerConfiguration
2424
)(implicit
2525
ctx: TaskCtx
2626
)
@@ -30,7 +30,12 @@ trait GroovyWorker {
3030
* Compiles the Groovy sources. In a mixed setup this method assumes that the Java stubs
3131
* are already present in the outputDir.
3232
*/
33-
def compile(sourceFiles: Seq[os.Path], classpath: Seq[os.Path], outputDir: os.Path)(implicit
33+
def compile(
34+
sourceFiles: Seq[os.Path],
35+
classpath: Seq[os.Path],
36+
outputDir: os.Path,
37+
config: GroovyCompilerConfiguration
38+
)(implicit
3439
ctx: TaskCtx
3540
)
3641
: Result[CompilationResult]

libs/groovylib/src/mill/groovylib/GroovyModule.scala

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import mill.javalib.{Dep, JavaModule, JvmWorkerModule, Lib}
99
import mill.*
1010
import mainargs.Flag
1111
import mill.api.daemon.internal.bsp.{BspBuildTarget, BspModuleApi}
12+
import mill.groovylib.worker.api.GroovyCompilerConfiguration
1213
import mill.javalib.api.internal.{JavaCompilerOptions, JvmWorkerApi, ZincCompileJava}
1314
import mill.util.Version
1415

@@ -49,6 +50,29 @@ trait GroovyModule extends JavaModule with GroovyModuleApi { outer =>
4950
allGroovySourceFiles() ++ allJavaSourceFiles()
5051
}
5152

53+
/**
54+
* Specifiy the bytecode version for the Groovy compiler.
55+
* {{{
56+
* def targetBytecode = Some("17")
57+
* }}}
58+
*/
59+
def targetBytecode: T[Option[String]] = None
60+
61+
/**
62+
* Specify if the Groovy compiler should enable preview features.
63+
*/
64+
def enablePreview: T[Boolean] = false
65+
66+
/**
67+
* Specify which global AST transformations should be disabled. Be aware that transformations
68+
* like [[groovy.transform.Immutable]] are so-called "local" transformations and will not be
69+
* affected.
70+
*
71+
* see [[https://docs.groovy-lang.org/latest/html/api/org/codehaus/groovy/control/CompilerConfiguration.html#setDisabledGlobalASTTransformations(java.util.Set) Groovy-Docs]]
72+
*/
73+
def disabledGlobalAstTransformations: T[Set[String]] = Set.empty
74+
75+
5276
/**
5377
* All individual Java source files fed into the compiler.
5478
* Subset of [[allSourceFiles]].
@@ -92,7 +116,7 @@ trait GroovyModule extends JavaModule with GroovyModuleApi { outer =>
92116
}
93117

94118
/**
95-
* The Ivy/Coursier dependencies resembling the Groovy compiler.
119+
* The Coursier dependencies resembling the Groovy compiler.
96120
*
97121
* Default is derived from [[groovyVersion]].
98122
*/
@@ -128,6 +152,12 @@ trait GroovyModule extends JavaModule with GroovyModuleApi { outer =>
128152
val compileCp = compileClasspath().map(_.path).filter(os.exists)
129153
val updateCompileOutput = upstreamCompileOutput()
130154

155+
val config = GroovyCompilerConfiguration(
156+
enablePreview = enablePreview(),
157+
targetBytecode = targetBytecode(),
158+
disabledGlobalAstTransformations = disabledGlobalAstTransformations(),
159+
)
160+
131161
def compileJava: Result[CompilationResult] = {
132162
ctx.log.info(
133163
s"Compiling ${javaSourceFiles.size} Java sources to $classes ..."
@@ -150,7 +180,7 @@ trait GroovyModule extends JavaModule with GroovyModuleApi { outer =>
150180

151181
val workerStubResult =
152182
GroovyWorkerManager.groovyWorker().withValue(groovyCompilerClasspath()) {
153-
_.compileGroovyStubs(groovySourceFiles, compileCp, classes)
183+
_.compileGroovyStubs(groovySourceFiles, compileCp, classes, config)
154184
}
155185
workerStubResult match {
156186
case Result.Success(_) => compileJava
@@ -165,7 +195,7 @@ trait GroovyModule extends JavaModule with GroovyModuleApi { outer =>
165195

166196
val workerGroovyResult =
167197
GroovyWorkerManager.groovyWorker().withValue(groovyCompilerClasspath()) {
168-
_.compile(groovySourceFiles, compileCp, classes)
198+
_.compile(groovySourceFiles, compileCp, classes, config)
169199
}
170200

171201
// TODO figure out if there is a better way to do this

libs/groovylib/src/mill/groovylib/publish/exports.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export mill.javalib.publish.PackagingType
2626

2727
export mill.javalib.publish.SonatypeHelpers
2828
export mill.javalib.publish.SonatypeHttpApi
29-
export mill.javalib.publish.SonatypePublisher
29+
export mill.javalib.SonatypeCentralPublisher
3030

3131
export mill.javalib.publish.VersionControl
3232

libs/groovylib/worker/src/mill/groovylib/worker/impl/GroovyWorkerImpl.scala

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import groovy.lang.GroovyClassLoader
44
import mill.api.Result
55
import mill.api.TaskCtx
66
import mill.javalib.api.CompilationResult
7-
import mill.groovylib.worker.api.GroovyWorker
7+
import mill.groovylib.worker.api.{GroovyCompilerConfiguration, GroovyWorker}
88
import org.codehaus.groovy.control.{CompilationUnit, CompilerConfiguration, Phases}
99
import org.codehaus.groovy.tools.javac.JavaStubCompilationUnit
1010
import os.Path
@@ -17,22 +17,26 @@ class GroovyWorkerImpl extends GroovyWorker {
1717
override def compileGroovyStubs(
1818
sourceFiles: Seq[Path],
1919
classpath: Seq[Path],
20-
outputDir: Path
20+
outputDir: Path,
21+
config: GroovyCompilerConfiguration,
2122
)(implicit ctx: TaskCtx): Result[CompilationResult] = {
22-
val config = new CompilerConfiguration()
23-
config.setTargetDirectory(outputDir.toIO)
24-
config.setClasspathList(classpath.map(_.toIO.getAbsolutePath).asJava)
25-
config.setJointCompilationOptions(Map(
23+
val compilerConfig = new CompilerConfiguration()
24+
compilerConfig.setTargetDirectory(outputDir.toIO)
25+
compilerConfig.setClasspathList(classpath.map(_.toIO.getAbsolutePath).asJava)
26+
compilerConfig.setJointCompilationOptions(Map(
2627
"stubDir" -> outputDir.toIO,
2728
"keepStubs" -> false
2829
).asJava)
30+
compilerConfig.setDisabledGlobalASTTransformations(config.disabledGlobalAstTransformations.asJava)
31+
compilerConfig.setPreviewFeatures(config.enablePreview)
32+
config.targetBytecode.foreach(compilerConfig.setTargetBytecode)
2933

3034
// we need to set the classloader for groovy to use the worker classloader
3135
val parentCl: ClassLoader = this.getClass.getClassLoader
32-
// config in the GroovyClassLoader is needed when the CL itself is compiling classes
33-
val gcl = new GroovyClassLoader(parentCl, config)
34-
// config for actual compilation
35-
val stubUnit = JavaStubCompilationUnit(config, gcl)
36+
// compilerConfig in the GroovyClassLoader is needed when the CL itself is compiling classes
37+
val gcl = new GroovyClassLoader(parentCl, compilerConfig)
38+
// compilerConfig for actual compilation
39+
val stubUnit = JavaStubCompilationUnit(compilerConfig, gcl)
3640

3741
sourceFiles.foreach { sourceFile =>
3842
stubUnit.addSource(sourceFile.toIO)
@@ -51,26 +55,28 @@ class GroovyWorkerImpl extends GroovyWorker {
5155
def compile(
5256
sourceFiles: Seq[os.Path],
5357
classpath: Seq[os.Path],
54-
outputDir: os.Path
58+
outputDir: os.Path,
59+
config: GroovyCompilerConfiguration,
5560
)(implicit
5661
ctx: TaskCtx
5762
): Result[CompilationResult] = {
5863

5964
val extendedClasspath = classpath :+ outputDir
6065

61-
val config = new CompilerConfiguration()
62-
config.setTargetDirectory(outputDir.toIO)
63-
config.setClasspathList(extendedClasspath.map(_.toIO.getAbsolutePath).asJava)
64-
// TODO
65-
// config.setDisabledGlobalASTTransformations()
66-
// config.setSourceEncoding()
66+
val compilerConfig = new CompilerConfiguration()
67+
compilerConfig.setTargetDirectory(outputDir.toIO)
68+
compilerConfig.setClasspathList(extendedClasspath.map(_.toIO.getAbsolutePath).asJava)
69+
compilerConfig.setDisabledGlobalASTTransformations(config.disabledGlobalAstTransformations.asJava)
70+
compilerConfig.setPreviewFeatures(config.enablePreview)
71+
config.targetBytecode.foreach(compilerConfig.setTargetBytecode)
6772

6873
// we need to set the classloader for groovy to use the worker classloader
6974
val parentCl: ClassLoader = this.getClass.getClassLoader
70-
// config in the GroovyClassLoader is needed when the CL itself is compiling classes
71-
val gcl = new GroovyClassLoader(parentCl, config)
72-
// config for actual compilation
73-
val unit = new CompilationUnit(config, null, gcl)
75+
// compilerConfig in the GroovyClassLoader is needed when the CL itself is compiling classes
76+
val gcl = new GroovyClassLoader(parentCl, compilerConfig)
77+
78+
// compilerConfig for actual compilation
79+
val unit = new CompilationUnit(compilerConfig, null, gcl)
7480

7581
sourceFiles.foreach { sourceFile =>
7682
unit.addSource(sourceFile.toIO)

0 commit comments

Comments
 (0)