Skip to content

Commit

Permalink
Truffle dsl processor issue (#332)
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin Gaidioz <ben@raw-labs.com>
Co-authored-by: Alex Zerntev <alex.zerntev@raw-labs.com>
  • Loading branch information
3 people authored Jan 22, 2024
1 parent 84de886 commit bfbe75b
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 14 deletions.
2 changes: 1 addition & 1 deletion hard-rebuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ sbt clean publishLocal

cd ../snapi-truffle
rm -rf target/
sbt clean publishLocal
sbt clean runJavaAnnotationProcessor publishLocal

cd ../snapi-client
rm -rf target/
Expand Down
1 change: 1 addition & 0 deletions snapi-frontend/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
exports raw.compiler.rql2.errors;
exports raw.compiler.rql2.lsp;
exports raw.compiler.rql2.source;
exports raw.compiler.rql2.antlr4;
exports raw.compiler.utils;
exports raw.inferrer.api;
exports raw.inferrer.local;
Expand Down
4 changes: 1 addition & 3 deletions snapi-parser/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,7 @@ Compile / packageSrc / publishArtifact := true

// Dependencies
libraryDependencies ++= Seq(
// We depend directly on the Truffle DSL processor to use their Antlr4.
// If we'd use ours, they would conflict as Truffle DSL package defines the org.antlr4 package.
"org.graalvm.truffle" % "truffle-dsl-processor" % "23.1.0"
"org.antlr" % "antlr4-runtime" % "4.12.0"
)

val generateParser = taskKey[Unit]("Generated antlr4 base parser and lexer")
Expand Down
4 changes: 2 additions & 2 deletions snapi-parser/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

module raw.snapi.parser {
requires truffle.dsl.processor;

exports raw.compiler.rql2.generated;

requires org.antlr.antlr4.runtime;
}
77 changes: 71 additions & 6 deletions snapi-truffle/build.sbt
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import de.heikoseeberger.sbtheader.HeaderPlugin.autoImport._
import de.heikoseeberger.sbtheader.HeaderPlugin.autoImport.*
import sbt.Keys.*
import sbt.*

import sbt.Keys._
import sbt._
import Dependencies.*

import java.time.Year

import Dependencies._
import scala.sys.process.Process

ThisBuild / sonatypeCredentialHost := "s01.oss.sonatype.org"

Expand Down Expand Up @@ -87,6 +86,12 @@ Test / doc / sources := {
(Compile / doc / sources).value.filterNot(_.getName.endsWith(".java"))
}

Compile / unmanagedSourceDirectories += baseDirectory.value / "target" / "java-processed-sources"

Compile / unmanagedResourceDirectories += baseDirectory.value / "target" / "java-processed-sources" / "META-INF"

Compile / resourceDirectories += baseDirectory.value / "target" / "java-processed-sources" / "META-INF"

// Add all the classpath to the module path.
Compile / javacOptions ++= Seq(
"--module-path",
Expand Down Expand Up @@ -121,6 +126,66 @@ Test / javaOptions ++= Seq(
"-Dpolyglotimpl.CompilationFailureAction=Throw"
)

val annotationProcessors = Seq(
"com.oracle.truffle.dsl.processor.TruffleProcessor",
"com.oracle.truffle.dsl.processor.verify.VerifyTruffleProcessor",
"com.oracle.truffle.dsl.processor.LanguageRegistrationProcessor",
"com.oracle.truffle.dsl.processor.InstrumentRegistrationProcessor",
"com.oracle.truffle.dsl.processor.OptionalResourceRegistrationProcessor",
"com.oracle.truffle.dsl.processor.InstrumentableProcessor",
"com.oracle.truffle.dsl.processor.verify.VerifyCompilationFinalProcessor",
"com.oracle.truffle.dsl.processor.OptionProcessor"
).mkString(",")

val calculateClasspath = taskKey[Seq[File]]("Calculate the full classpath")

calculateClasspath := {
val dependencyFiles = (Compile / dependencyClasspath).value.files
val unmanagedFiles = (Compile / unmanagedClasspath).value.files
val classesDir = (Compile / classDirectory).value

dependencyFiles ++ unmanagedFiles ++ Seq(classesDir)
}

val runJavaAnnotationProcessor = taskKey[Unit]("Runs the Java annotation processor")

runJavaAnnotationProcessor := {
println("Running Java annotation processor")

val annotationProcessorJar = baseDirectory.value / "truffle-dsl-processor-23.1.0.jar"

val javaSources = baseDirectory.value / "src" / "main" / "java"
val targetDir = baseDirectory.value / "target" / "java-processed-sources"

val projectClasspath = calculateClasspath.value.mkString(":")

val javacOptions = Seq(
"javac",
"-source",
"21",
"-target",
"21",
"-d",
targetDir.getAbsolutePath,
"--module-path",
projectClasspath,
"-cp",
annotationProcessorJar.getAbsolutePath,
"-processor",
annotationProcessors,
"-proc:only"
) ++ (javaSources ** "*.java").get.map(_.absolutePath)

// Create the target directory if it doesn't exist
targetDir.mkdirs()

// Execute the Java compiler
val result = Process(javacOptions).!
if (result != 0) {
throw new RuntimeException("Java annotation processing failed.")
}
}

// Add dependency resolvers
resolvers += Resolver.mavenLocal
resolvers += Resolver.sonatypeRepo("releases")
Expand Down
2 changes: 1 addition & 1 deletion snapi-truffle/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ yes | sdk install java 21.0.1-graalce || true
sdk use java 21.0.1-graalce

cd "$SCRIPT_HOME"
sbt clean publishLocal
sbt clean runJavaAnnotationProcessor publishLocal
1 change: 0 additions & 1 deletion snapi-truffle/project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object Dependencies {

val truffleCompiler = Seq(
"org.graalvm.truffle" % "truffle-api" % "23.1.0",
"org.graalvm.truffle" % "truffle-dsl-processor" % "23.1.0" % Provided,
"org.graalvm.truffle" % "truffle-api" % "23.1.0",
"org.graalvm.truffle" % "truffle-compiler" % "23.1.0",
"org.graalvm.truffle" % "truffle-nfi" % "23.1.0",
Expand Down
Binary file added snapi-truffle/truffle-dsl-processor-23.1.0.jar
Binary file not shown.

0 comments on commit bfbe75b

Please sign in to comment.