Skip to content

Commit f0cc0b8

Browse files
committed
Cleanup and Testcase for targetBytecode and previewEnabled
1 parent 485a8c3 commit f0cc0b8

File tree

4 files changed

+83
-11
lines changed

4 files changed

+83
-11
lines changed

libs/groovylib/package.mill

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ object `package` extends MillPublishScalaModule with BuildInfo {
1111
def localTestExtraModules: Seq[MillJavaModule] =
1212
super.localTestExtraModules ++ Seq(worker)
1313

14+
override def testMvnDeps: T[Seq[Dep]] = super.testMvnDeps() ++ Seq(
15+
Deps.asmTree
16+
)
17+
1418
def buildInfoPackageName = "mill.groovylib"
1519
def buildInfoObjectName = "Versions"
1620
def buildInfoMembers = Seq(

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -174,24 +174,17 @@ trait GroovyModule extends JavaModule with GroovyModuleApi { outer =>
174174
)
175175
}
176176

177-
if (isMixed) {
177+
def compileGroovyStubs(): Result[CompilationResult] = {
178178
ctx.log.info("Compiling Groovy stubs for mixed compilation")
179-
180-
val workerStubResult =
181-
GroovyWorkerManager.groovyWorker().withValue(groovyCompilerClasspath()) {
182-
_.compileGroovyStubs(groovySourceFiles, compileCp, classes, config)
183-
}
184-
workerStubResult match {
185-
case Result.Success(_) => compileJava
186-
case Result.Failure(reason) => Result.Failure(reason)
179+
GroovyWorkerManager.groovyWorker().withValue(groovyCompilerClasspath()) {
180+
_.compileGroovyStubs(groovySourceFiles, compileCp, classes, config)
187181
}
188182
}
189183

190-
if (isMixed || isGroovy) {
184+
def compileGroovy(): Result[CompilationResult] = {
191185
ctx.log.info(
192186
s"Compiling ${groovySourceFiles.size} Groovy sources to $classes ..."
193187
)
194-
195188
val workerGroovyResult =
196189
GroovyWorkerManager.groovyWorker().withValue(groovyCompilerClasspath()) {
197190
_.compile(groovySourceFiles, compileCp, classes, config)
@@ -206,6 +199,17 @@ trait GroovyModule extends JavaModule with GroovyModuleApi { outer =>
206199
CompilationResult(analysisFile, PathRef(classes))
207200
case Result.Failure(reason) => Result.Failure(reason)
208201
}
202+
}
203+
204+
val firstAndSecondStage = if (isMixed) {
205+
// only compile Java if Stubs are successfully generated
206+
compileGroovyStubs().flatMap(_ => compileJava)
207+
}else{
208+
Result.Success
209+
}
210+
211+
if (isMixed || isGroovy) {
212+
firstAndSecondStage.flatMap(_ => compileGroovy())
209213
} else {
210214
compileJava
211215
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package compileroptions
2+
3+
class HelloCompilerOptions {
4+
5+
static String getHelloString() {
6+
return "Hello, Java 11 Preview!"
7+
}
8+
9+
static void main(String[] args) {
10+
println(getHelloString())
11+
}
12+
}

libs/groovylib/test/src/mill/groovylib/HelloGroovyTests.scala

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import mill.api.Discover
77
import mill.testkit.{TestRootModule, UnitTester}
88
import utest.*
99

10+
import java.io.FileInputStream
11+
1012
object HelloGroovyTests extends TestSuite {
1113

1214
val groovy4Version = "4.0.28"
@@ -101,6 +103,13 @@ object HelloGroovyTests extends TestSuite {
101103
override def jupiterVersion: T[String] = junit5Version
102104
override def junitPlatformVersion = "1.13.4"
103105
}
106+
107+
object compileroptions extends GroovyModule {
108+
override def groovyVersion: T[String] = crossValue
109+
override def targetBytecode: Task.Simple[Option[String]] = Some("11")
110+
override def enablePreview: Task.Simple[Boolean] = true
111+
override def mainClass = Some("compileroptions.HelloCompilerOptions")
112+
}
104113
}
105114
object main extends Cross[Test](groovyVersions)
106115
}
@@ -195,6 +204,49 @@ object HelloGroovyTests extends TestSuite {
195204
}
196205
}
197206

207+
test("compiles to Java 11 with Preview enabled") {
208+
import org.objectweb.asm.ClassReader
209+
210+
case class BytecodeVersion(major: Int, minor: Int) {
211+
def javaVersion: String = major match {
212+
case 55 => "11"
213+
case _ => "Irrelevant"
214+
}
215+
216+
def is11PreviewEnabled: Boolean = minor == 65535 // 0xFFFF
217+
}
218+
219+
def getBytecodeVersion(classFilePath: os.Path): BytecodeVersion = {
220+
val classReader = new ClassReader(new FileInputStream(classFilePath.toIO))
221+
val buffer = classReader.b
222+
223+
// Class file format: magic(4) + minor(2) + major(2) + ...
224+
val minor = ((buffer(4) & 0xFF) << 8) | (buffer(5) & 0xFF)
225+
val major = ((buffer(6) & 0xFF) << 8) | (buffer(7) & 0xFF)
226+
227+
BytecodeVersion(major, minor)
228+
}
229+
230+
testEval().scoped { eval =>
231+
main.crossModules.foreach(m => {
232+
val Right(result) = eval.apply(m.compileroptions.compile): @unchecked
233+
234+
val compiledClassFile = os.walk(result.value.classes.path).find(_.last == "HelloCompilerOptions.class")
235+
236+
assert(
237+
compiledClassFile.isDefined
238+
)
239+
240+
val bytecodeVersion = getBytecodeVersion(compiledClassFile.get)
241+
242+
assert(bytecodeVersion.major == 55)
243+
assert(bytecodeVersion.is11PreviewEnabled)
244+
245+
val Right(_) = eval.apply(m.compileroptions.run()): @unchecked
246+
})
247+
}
248+
}
249+
198250
test("compile & test module (only test uses Groovy)") {
199251
testEval().scoped { eval =>
200252

0 commit comments

Comments
 (0)