Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.sourcegraph.lsif_java.buildtools

import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.nio.file.NoSuchFileException
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.StandardCopyOption
Expand Down Expand Up @@ -70,9 +71,9 @@ case class GradleJavaCompiler(languageVersion: String, javacPath: Path) {
val javaCommand = ListBuffer[String](
javaBinary.toString,
s"-javaagent:$agent",
s"-Dsemanticdb.javacopts=${javacopts}",
s"-Dsemanticdb.pluginpath=${pluginPath}",
s"-Dsemanticdb.targetroot=${targetroot}",
s"-Dsemanticdb.javacopts=$javacopts",
s"-Dsemanticdb.pluginpath=$pluginPath",
s"-Dsemanticdb.targetroot=$targetroot",
s"-Dsemanticdb.sourceroot=${index.workingDirectory}"
)
if (index.verbose) {
Expand All @@ -98,6 +99,39 @@ case class GradleJavaCompiler(languageVersion: String, javacPath: Path) {
)
.toFile
.setExecutable(true)

val copyFiles =
(source: Path, destination: Path) => {
Files
.walk(source)
.forEach(t => {
val destPath = destination.resolve(source.relativize(t))
try {
Files.copy(t, destPath)
} catch {
case _: NoSuchFileException =>
return
}
})
}

// For compile{Test}Kotlin when using jvm toolchains, we need to have access
// to JDK internals found in <java-installation-path>/lib in JDK 9+,
// as well as <java-installation-path>/jre/lib in JDK <=8, else we get
// "no class roots are found in the JDK path" from the compile{Test}Kotlin tasks.
// https://docs.oracle.com/en/java/javase/12/migrate/index.html#JSMIG-GUID-A78CC891-701D-4549-AA4E-B8DD90228B4B
val javaHome = javacPath.getParent.getParent
val libPath = dir.resolve("lib")
val javacLibPath = javaHome.resolve("lib")
copyFiles(javacLibPath, libPath)

if (languageVersion == "8") {
val jreLibPath = dir.resolve("jre").resolve("lib")
Files.createDirectories(jreLibPath.getParent)
val javacJreLibPath = javaHome.resolve("jre").resolve("lib")

copyFiles(javacJreLibPath, jreLibPath)
}
Comment on lines +123 to +134
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice and compact! 🍬

}
}
object GradleJavaCompiler {
Expand All @@ -111,9 +145,9 @@ object GradleJavaCompiler {
/**
* Parses a single space-separated line into a GradleJavaCompiler instance.
*
* Example input: "8 /path/javac"
* Example input: "8 /javacLibPath/javac"
*
* Example output: `Some(GradleJavaCompiler("8", * /path/javac))`
* Example output: `Some(GradleJavaCompiler("8", * /javacLibPath/javac))`
*/
def fromLine(line: String): Option[GradleJavaCompiler] =
line.split(' ') match {
Expand Down
23 changes: 22 additions & 1 deletion tests/buildTools/src/test/scala/tests/GradleBuildToolSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,28 @@ class GradleBuildToolSuite extends BaseBuildToolSuite {
4
)

List("8", "11").foreach { version =>
checkBuild(
s"kotlin-jvm-toolchains-jdk-$version",
s"""|/build.gradle
|plugins {
| id 'java'
| id 'org.jetbrains.kotlin.jvm' version '1.5.31'
|}
|java {
| toolchain {
| languageVersion = JavaLanguageVersion.of($version)
| }
|}
|repositories { mavenCentral() }
|/src/main/kotlin/foo/Example.kt
|package foo
|object Example {}
|""".stripMargin,
1
)
}

List("jvm()" -> 2, "jvm { withJava() }" -> 4).foreach {
case (jvmSettings, expectedSemanticdbFiles) =>
checkBuild(
Expand Down Expand Up @@ -326,5 +348,4 @@ class GradleBuildToolSuite extends BaseBuildToolSuite {
expectedSemanticdbFiles
)
}

}