Skip to content

Commit

Permalink
Support javac processorpath. Import processorpath from Gradle.
Browse files Browse the repository at this point in the history
  • Loading branch information
pkolaczk committed Jul 27, 2019
1 parent 62af02e commit b8c8807
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 32 deletions.
57 changes: 31 additions & 26 deletions backend/src/main/scala/bloop/Compiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,32 @@ import bloop.CompileMode.Sequential
import monix.execution.ExecutionModel

case class CompileInputs(
javaInstance: JdkInstance,
scalaInstance: ScalaInstance,
compilerCache: CompilerCache,
sources: Array[AbsolutePath],
classpath: Array[AbsolutePath],
uniqueInputs: UniqueCompileInputs,
//store: IRStore,
out: CompileOutPaths,
baseDirectory: AbsolutePath,
scalacOptions: Array[String],
javacOptions: Array[String],
compileOrder: CompileOrder,
classpathOptions: ClasspathOptions,
previousResult: PreviousResult,
previousCompilerResult: Compiler.Result,
reporter: ZincReporter,
logger: ObservedLogger[Logger],
mode: CompileMode,
dependentResults: Map[File, PreviousResult],
cancelPromise: Promise[Unit],
tracer: BraveTracer,
ioScheduler: Scheduler,
ioExecutor: Executor,
invalidatedClassFilesInDependentProjects: Set[File],
generatedClassFilePathsInDependentProjects: Map[String, File]
javaInstance: JdkInstance,
scalaInstance: ScalaInstance,
compilerCache: CompilerCache,
sources: Array[AbsolutePath],
classpath: Array[AbsolutePath],
processorpath: Array[AbsolutePath],
uniqueInputs: UniqueCompileInputs,
//store: IRStore,
out: CompileOutPaths,
baseDirectory: AbsolutePath,
scalacOptions: Array[String],
javacOptions: Array[String],
compileOrder: CompileOrder,
classpathOptions: ClasspathOptions,
previousResult: PreviousResult,
previousCompilerResult: Compiler.Result,
reporter: ZincReporter,
logger: ObservedLogger[Logger],
mode: CompileMode,
dependentResults: Map[File, PreviousResult],
cancelPromise: Promise[Unit],
tracer: BraveTracer,
ioScheduler: Scheduler,
ioExecutor: Executor,
invalidatedClassFilesInDependentProjects: Set[File],
generatedClassFilePathsInDependentProjects: Map[String, File]
)

case class CompileOutPaths(
Expand Down Expand Up @@ -370,14 +371,18 @@ object Compiler {
def getCompilationOptions(inputs: CompileInputs): CompileOptions = {
val sources = inputs.sources // Sources are all files
val classpath = inputs.classpath.map(_.toFile)
val annotationProcessorOptions =
if (inputs.processorpath.isEmpty) List.empty[String]
else List("-processorpath", inputs.processorpath.mkString(":"))
val javacOptions = inputs.javacOptions ++ annotationProcessorOptions

CompileOptions
.create()
.withClassesDirectory(newClassesDir.toFile)
.withSources(sources.map(_.toFile))
.withClasspath(classpath)
.withScalacOptions(inputs.scalacOptions)
.withJavacOptions(inputs.javacOptions)
.withJavacOptions(javacOptions)
.withClasspathOptions(inputs.classpathOptions)
.withOrder(inputs.compileOrder)
}
Expand Down
8 changes: 7 additions & 1 deletion config/src/main/scala/bloop/config/Config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ object Config {
sources: List[Path],
dependencies: List[String],
classpath: List[Path],
processorpath: List[Path],
out: Path,
classesDir: Path,
resources: Option[List[Path]],
Expand All @@ -232,7 +233,7 @@ object Config {

object Project {
// FORMAT: OFF
private[bloop] val empty: Project = Project("", emptyPath, List(), List(), List(), emptyPath, emptyPath, None, None, None, None, None, None, None)
private[bloop] val empty: Project = Project("", emptyPath, List(), List(), List(), List(), emptyPath, emptyPath, None, None, None, None, None, None, None)
// FORMAT: ON

def analysisFileName(projectName: String) = s"$projectName-analysis.bin"
Expand All @@ -253,6 +254,10 @@ object Config {
val scalaLibraryJar = Files.createTempFile("scala-library", ".jar")
scalaLibraryJar.toFile.deleteOnExit()

val annotationProcessorJar = Files.createTempFile("annotation-processor", ".jar")
annotationProcessorJar.toFile.deleteOnExit()


// This is like `target` in sbt.
val outDir = Files.createTempFile("out", "test")
outDir.toFile.deleteOnExit()
Expand All @@ -274,6 +279,7 @@ object Config {
List(sourceFile),
List("dummy-2"),
List(scalaLibraryJar),
List(annotationProcessorJar),
outDir,
classesDir,
Some(List(outDir.resolve("resource1.xml"))),
Expand Down
1 change: 1 addition & 0 deletions frontend/src/it/scala/bloop/CommunityBuild.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ abstract class CommunityBuild(val buildpressHomeDir: AbsolutePath) {
dependencies = allProjectsInBuild.map(_.name),
scalaInstance = allProjectsInBuild.head.scalaInstance,
rawClasspath = Nil,
annotationProcessorPath = Nil,
resources = Nil,
compileSetup = Config.CompileSetup.empty,
genericClassesDir = dummyClassesDir,
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/main/scala/bloop/data/Project.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ final case class Project(
dependencies: List[String],
scalaInstance: Option[ScalaInstance],
rawClasspath: List[AbsolutePath],
annotationProcessorPath: List[AbsolutePath],
resources: List[AbsolutePath],
compileSetup: Config.CompileSetup,
genericClassesDir: AbsolutePath,
Expand Down Expand Up @@ -166,6 +167,7 @@ object Project {
project.dependencies,
instance,
project.classpath.map(AbsolutePath.apply),
project.processorpath.map(AbsolutePath.apply),
resources,
setup,
AbsolutePath(project.classesDir),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ object CompileTask {
state.compilerCache,
sources.toArray,
classpath,
project.annotationProcessorPath.toArray,
bundle.uniqueInputs,
compileOut,
project.out,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ object CompileBundle {
val compileDependenciesData = {
tracer.trace("dependency classpath") { _ =>
CompileDependenciesData.compute(
project.rawClasspath.toArray,
(project.rawClasspath ++ project.annotationProcessorPath).toArray,
dependentProducts
)
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/test/scala/bloop/DagSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class DagSpec {
// format: OFF
def dummyOrigin = TestUtil.syntheticOriginFor(dummyPath)
def dummyProject(name: String, dependencies: List[String]): Project =
Project(name, dummyPath, dependencies, Some(dummyInstance), Nil, Nil, compileOptions,
Project(name, dummyPath, dependencies, Some(dummyInstance), Nil, Nil, Nil, compileOptions,
dummyPath, Nil, Nil, Nil, Nil, Config.TestOptions.empty, dummyPath, dummyPath,
Project.defaultPlatform(logger), None, None, dummyOrigin)
// format: ON
Expand Down
1 change: 1 addition & 0 deletions frontend/src/test/scala/bloop/util/TestProject.scala
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ object TestProject {
List(sourceDir.underlying),
directDependencies.map(_.config.name),
classpath,
List.empty,
outDir.underlying,
classes.underlying,
resources = Some(List(resourceDir.underlying)),
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/test/scala/bloop/util/TestUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ object TestUtil {
dependencies = dependencies.toList,
scalaInstance = scalaInstance,
rawClasspath = classpath,
annotationProcessorPath = Nil,
resources = Nil,
compileSetup = Config.CompileSetup.empty.copy(order = compileOrder),
genericClassesDir = AbsolutePath(target),
Expand Down Expand Up @@ -451,7 +452,7 @@ object TestUtil {
val classesDir = Files.createDirectory(outDir.resolve("classes"))

// format: OFF
val configFileG = bloop.config.Config.File(Config.File.LatestVersion, Config.Project("g", baseDir, Nil, List("g"), Nil, outDir, classesDir, None, None, None, None, None, None, None))
val configFileG = bloop.config.Config.File(Config.File.LatestVersion, Config.Project("g", baseDir, Nil, List("g"), Nil, Nil, outDir, classesDir, None, None, None, None, None, None, None))
bloop.config.write(configFileG, jsonTargetG)
// format: ON

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ final class BloopConverter(parameters: BloopParameters) {
val modules = (nonProjectDependencies.map(artifactToConfigModule(_, project)) ++
additionalArtifacts.map(artifactToConfigModule(_, project))).distinct

val annotationProcessorPaths =
Option(project.getConfiguration("annotationProcessor")) match {
case None => List.empty
case Some(conf) => conf.getResolvedConfiguration.getFiles.asScala.toList.map(_.toPath)
}

for {
scalaConfig <- getScalaConfig(project, sourceSet, compileArtifacts)
resolution = Config.Resolution(modules)
Expand All @@ -219,6 +225,7 @@ final class BloopConverter(parameters: BloopParameters) {
sources = sources,
dependencies = allDependencies,
classpath = classpath,
processorpath = annotationProcessorPaths,
out = project.getBuildDir.toPath,
classesDir = classesDir,
resources = if (resources.isEmpty) None else Some(resources),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,48 @@ abstract class ConfigGenerationSuite {
assert(compileBloopProject("b-test", bloopDir).status.isOk)
}

@Test def worksWithAnnotationProcessorDependencies(): Unit = {
val buildFile = testProjectDir.newFile("build.gradle")
testProjectDir.newFolder("src", "main", "scala")

writeBuildScript(
buildFile,
s"""
|plugins {
| id 'bloop'
|}
|
|apply plugin: 'scala'
|apply plugin: 'bloop'
|
|repositories {
| mavenCentral()
|}
|
|dependencies {
| compile 'org.scala-lang:scala-library:2.12.8'
| compile 'com.google.dagger:dagger:2.2'
| annotationProcessor 'com.google.dagger:dagger-compiler:2.2'
|}
""".stripMargin
)

createHelloWorldScalaSource(testProjectDir.getRoot)

GradleRunner
.create()
.withGradleVersion(gradleVersion)
.withProjectDir(testProjectDir.getRoot)
.withPluginClasspath(getClasspath.asJava)
.withArguments("bloopInstall", "-Si")
.build()

val projectName = testProjectDir.getRoot.getName
val bloopFile = new File(new File(testProjectDir.getRoot, ".bloop"), projectName + ".json")
val resultConfig = readValidBloopConfig(bloopFile)
assert(resultConfig.project.processorpath.nonEmpty)
}

@Test def encodingOptionGeneratedCorrectly(): Unit = {
val buildFile = testProjectDir.newFile("build.gradle")
testProjectDir.newFolder("src", "main", "scala")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ object MojoImplementation {
val platform = Some(Config.Platform.Jvm(Config.JvmConfig(javaHome, launcher.getJvmArgs().toList), mainClass))
// Resources in Maven require
val resources = Some(resources0.asScala.toList.flatMap(a => Option(a.getTargetPath).toList).map(classesDir.resolve))
val project = Config.Project(name, baseDirectory, sourceDirs, dependencyNames, classpath, out, classesDir, resources, `scala`, java, sbt, test, platform, resolution)
val project = Config.Project(name, baseDirectory, sourceDirs, dependencyNames, classpath, List.empty, out, classesDir, resources, `scala`, java, sbt, test, platform, resolution)
Config.File(Config.File.LatestVersion, project)
}
// FORMAT: ON
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ object Bloop extends ExternalModule {
sources = module.allSources().map(_.path.toNIO).toList,
dependencies = module.moduleDeps.map(name).toList,
classpath = classpath().map(_.toNIO).toList,
processorpath = List.empty,
out = out(module).toNIO,
classesDir = classes(module).toNIO,
resources = Some(resources()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ object BloopDefaults {

val sbt = computeSbtMetadata.value.map(_.config)
val project = Config.Project(projectName, baseDirectory, sources, dependenciesAndAggregates,
classpath, out, classesDir, resources, Some(`scala`), Some(java), sbt, Some(testOptions), Some(platform), resolution)
classpath, List.empty, out, classesDir, resources, Some(`scala`), Some(java), sbt, Some(testOptions), Some(platform), resolution)
Config.File(Config.File.LatestVersion, project)
}
// format: ON
Expand Down

0 comments on commit b8c8807

Please sign in to comment.