enso/project/JPMSPlugin.scala
Pavel Marek 9182f91e35
engine-runner and language-server are separate JPMS modules (#10823)
* Use moduleDependencies instead of modulePath

* Fix compilation of editions

* Fix compilation of distribution-manager

* polyglot-api needs to explicitly compile module-info

* Fix compilationOrder in library-manager and edition-updater

* engine-runner-common is module

* JPMSPlugin provides default implementation of compileModuleInfo

* Remove unused setting key from JPMSUtils.compileModuleInfo

* JPMSPlugin has internalModuleDependencies and exportedModule tasks.

* Use BuildVersion instead of buildInfo

* Manual compilation of module-info.java is reported as warning

* Define org.enso.scalalibs.wrapper meta project.

* Fix module check in JPMSPlugin.

This is a fix for projects that declare `Compile /exportJars := true`

* version-output is a module

* ydoc-server uses internalModuleDependencies

* persistance is module

* engine-common uses internalModuleDependencies

* polyglot-api does not override compileModuleInfo task

* runtime-parser uses internalModuleDependencies

* edition-updater is module

* Update moduleDependencies for distribution-manager

* editions is module

* Fix some dependencies of modules

* scala-yaml is a module

* Add scala-compiler to scala-libs-wrapper

* cli depends on scala-library module

* Add dependencies for distribution-manager module

* Add some scala-library dependencies in some modules

* engine-runner uses internalModuleDependencies

* Fix module dependencies of library-manager

* Rename org.enso.scalalibs.wrapper to org.enso.scala.wrapper

* Add jsoniter-scala-macros to org.enso.scala.wrapper fat module

* Fix dependencies of some projects

* polyglot-api does not depend on truffle-api

* Fix dependencies of some projects

* runtime does not use com.google.common

* runtime is a module

* text-buffer is a module

* refactoring-utils is a module

* runtime-compiler is a module

* runtime-instrument-common is a module

* connected-lock-manager is a module

* JPMSUtils reports project name in some error messages

* Modularize some instruments

* module-info compilation is cached

* runtime-instrument-runtime-server is module

* runtime-language-epb is module

* Remove runtime-fat-jar

* engine-runner is not a fat jar

* JPMSPlugin defines exportedModuleBin task

* Redefine componentModulesPaths task

* interpreter-dsl is module

* Redefine componentModulesPaths task

* fmt sbt

* scala-libs-wrapper is a modular fat jar

* Add some module deps to org.enso.runtime

* engine-runner is not a fat jar

* Rename package in logging-config

* Rename package in logging-service

* Rename package in logging-service-logback

* Fix dependencies of exportedModuleBin task

* Mixed projects have own compileJava task

this task does not compile only module-info.java but all the java sources. So that we can see errors more easily.

When only module-info.java is compiled, the only errors that we can see are that we did not include some modules on module-path.

* Fix definition of exportedModule task.

* Remove usages of non-existing buildInfo and replace it with BuildVersion

* Fix some dependencies of org.enso.runtime module

* module-info compilation is handled directly by FrgaalCompiler

* module-info compilation is forced for projects that has only Scala sources with single module-info.java

* Fix compilation of org.enso.runtime

* manual module-info compilation is not a warning

* Rename packages in logging-utils-akka

* Create org.enso.language.server.deps.wrapper module

* language-server is module

* Creat akka-wrapper modular fat jar

* fmt

* Define common settings for modularFatJarWrapper

* Fix compilation of json-rpc-server

* Use akka and zio wrappers

* language-server depends on org.eclipse.jgit

* Fix some dependencies - update library manifests works now!

* update library manifests invokes runner directly

* buildEngineDistribution does not copy runner.jar

* Remove EngineRunnerBootLoader

* Fix compilation of std libs

* --patch-module and --add-exports are also passed to javac

* Rename package in runtime-integration-tests.

The package name org.enso.compiler clashes with the package from the module

* Remove usage of buildInfo

* FrgaalJavaCompiler can deal with non-existing module-info.java when shouldCompileModuleInfo is true.

It just generates a warning in such case as it suggests that there is something wrong with the project configuration.

* Revert AliasAnalysisTest.scala

* Fix dependencies and java cmdline options for runtime-integration-tests

* Rename test package

* runtime-integration-test depends on logging-service-logback/Test/compile

* Rename package in logging-service-logback/Test

* Fix FrgaalJavaCompiler creation for projects

* Sanitize Test/javaOptions arguments

* Sanitize Test/javaOptions arguments

* All the JPMSPlugin settings are scoped

* Remove unused sbt tasks

* modularFatJarWrapperSettings do not override javacOptions

* Resolve issue "Cannot find TestLoggerProvider" in runtime-integration-tests

* org.enso.runtime module is open

* Test that test classes are unconditionally opened and exported

* polyglot-api-macros is a module

* JPMSPlugin handles --add-opens cmdline option

* RuntimeServerTest ensures instruments are initialized

* Add some exports to org.enso.runtime.compiler

* Add instruments on module-path to runtime-integration-tests

* Replace TestLogProviderOnClassPath with TestLogProviderOnModulePath

* Replace buildInfo with BuildVersion

* Add jpms-wrapper-scalatest

* ReportLogsOnFailure is in non-modular testkit project

* Add necessary dependencies to testkit project

* Revert "Add jpms-wrapper-scalatest"

This reverts commit 732b3427a2.

* modularize filewatcher and wrap its dependencies

* Initial fix for language-server/test

* frgaal compiler setting are scoped for Compile and Test

* Rename package in language-server/test

* Exclude com.sun.jna from wrapper jars

* Rename package in library-manager-test

* testkit is an automatic module

* process-utils is module

* akka-wrapper contains akka-http

* Some fixes for library-manager-test

* Fix dependencies for akka-wrapper

* scala-libs-wrapper exports shapeless

* lang server deps wrapper exports pureconfig

* json-rpc-server requires org.slf4j

* Add some dependencies

* lang server deps wrapper exports pureconfig.generic

* language server test requires bouncycastle provider

* language server depends on cli

* directory-watcher wrapper requires org.slf4j

* WatcherAdapter logs unsuccessful initialization errors

* Fix error reporting in WatcherAdapter

* Fix rest of the language-server tests

* language-server-deps-wrapper depends on scala-libs-wrapper

* Fix rest of the language-server tests

* Missing module-info.class in an internal project is a warning, not an error

* Rename jpms-methvin-directory-watcher-wrapper to a simpler name

* compileOrder has to be specified before libraryDependencies

* exclude module-info.java from polyglot-api-macros

* Remove temporary logging in customFrgaalCompilerSettings

* Fix compilation of logging-service-logback

* Fix compilation of runtime-benchmarks

* Fix runtime-benchmarks/run

* HostClassLoader delegates to org.graalvm.polyglot class loader if org.enso.runtime is not on boot layer

* org.enso.runtime.lnaguage.epb module must be opened to allow it to be used by annnotation processor

* fmt

* Fix afetr merge

* Add module deps after merge

* Print stack trace of the uncaught exception from the annotation processor

* Remove akka-actor-typed from akka-wrapper

* runtime-instrument-common depends on slf4j

* Fix module-path for runtime-instrument-repl-debugger

* runtime-benchmarks depends on runtime-language-arrow

* --module-path is passed directly to frgaal

* Fix some module-related cmd line options for std-benchmarks

* Revert "--module-path is passed directly to frgaal"

This reverts commit da63f66a0e.

* Avoid closing of System.err when closing Context

* Avoid processing altogether when requested annotations are empty

* Pass shouldNotLimitModules opt to frgaal

* Pass module-path and add-modules options with -J prefix to frgaal

* BenchProcessor annotation processor creates its own truffle module layer

* bench-processor and benchmarks-common are modules

* fmt

* Fix after mege

* Enable JMH annotation processor

* Fix compileOrder in some projects

* Insert TruffleBoundary to QualifiedName.

This is a revert

* Fix building of engine-runner native image

* Add more deps to the native image

* Force module-info compilation in instruments.

This fixes some weird sbt bug

* Don't run engine-runner/assembly from Rust build script

* Update docs of JPMSPlugin

* fmt

* runtime-benchmarks depends on benchmarks-common module

* Fix benchmark report writing

* std-benchmarks annot processing does not take settings from runtime-benchmarks

* Suppress interpreter only warning in annotation processor

* Runtime version manager does not expect runtime.jar fat jar

* fmt

* Fix module entry point

* Move some polyglot tests to runtime-integration-tests.

Also make their output silent

* pkg has no dependency on org.graalvm.truffle

* Fix compiler dependencies test

* Rename all runtime.jar in fake releases

* Add language-server with dependencies to component dir

* No module-info.class in target dir is warning not error

* language-server does not depend on netbeans lookup uitl

* Declare LanguageServerApi service provider in module-info

* connected-lock-manager-server is JPMS module

* task-progress-notifications is module

* Add fansi-wrapper module

* Fix compilation of connected-lock-manager-server

* Define correct Test/internalModuleDependencies for project-manager

* fmt

* Fix LauncherRunnerSpec - no runtime.jar

* Add fansi-wrapper to runtime-integration-tests and runtime-benchmarks

* Fix engine-runner native image build

* Use newer JNA version - fixes running of hyperd

* DRY

* scala-compiler DRY

* fmt

* More build.sbt refactoring

* Include runtime-instrument-id-execution in engine-runner native image

* TruffleBoundary for QualifiedName.toString

* Finding a needle in a haystack

🤦

* More scala-library DRY

* more mixed-java/scala goodies

* Fix compilation of syntax-rust-definition

* Test that engine-runner does not depend on language-server

* Append rather than assign `moduleDependencies`

`++=` is less error prone than `:=`. Also discovered some unnecessary
dependencies.

* Replace : with File.pathSeparator

* [WIP] Make logging in ProjectService more verbose

* language-server/test didn't start because of missing lookup and fansi modules

* Formatting

* org.enso.cli.task.notifications needs Akka and Circe to link

* project-manager/test depends on buildEngineDistribution

* [WIP] Even more verbose logging for creating projects

* [WIP] Even more verbose logging for creating projects

* Revert "[WIP] Even more verbose logging for creating projects"

This reverts commit a7067c8472.

* Revert "[WIP] Even more verbose logging for creating projects"

This reverts commit fc6f53d4f1.

* Revert "[WIP] Make logging in ProjectService more verbose"

This reverts commit 427428e142.

* All the project with JPMSPlugin has stripped artifact names

* Revert all placeholder fake release components to runtime.jar without version

* Eliminate a cross version hack

We shouldn't be specifying Scala dependencies with a Scala cross version
in the suffix.

* Address SBT lint warnings

* Revert "Eliminate a cross version hack"

This reverts commit 8861dab288.

* logging-service-logback  is mixedJavaScalaProject

* fmt

* Stripped artifact name contains classifier.

This fixes tests as those were named like `artifact-tests.jar`.

* Don't use LocalProject unless really needed

* Add more logging when BenchProcessor fails

* logging-service-logback is not mixed project

* Work with java.io.File.getPath to avoid mixing slash and backslash on Windows

* Reapply "Eliminate a cross version hack"

This reverts commit edaa436ee8.

* Pass scalaBinaryVersion correctly

* Remove scala-compiler from the distribution

* Fix IllegalAccessErrors from serde

* typos

* License review

* fmt

* Move testLogProviderOnModulePath to TestJPMSConfiguration

* logging-service-logback is not a mixed project

---------

Co-authored-by: Jaroslav Tulach <jaroslav.tulach@enso.org>
Co-authored-by: Hubert Plociniczak <hubert.plociniczak@gmail.com>
2024-09-25 21:33:13 +02:00

511 lines
19 KiB
Scala

import sbt._
import sbt.Keys._
import sbt.internal.util.ManagedLogger
import java.io.File
import java.util.jar.JarFile
import scala.collection.mutable
/** An automatic plugin that handles everything related to JPMS modules. One needs to explicitly
* enable this plugin in a project with `.enablePlugins(JPMSPlugin)`. The keys and tasks provided by this plugin
* corresponds to the module-related options of `javac` and `java` commands.
*
* This plugin injects all the module-specific options to `javaOptions`, based on
* the settings of this plugin.
*
* Note that the settings of this plugin are *scoped* to `Compile` and `Test` configurations, so
* you need to always specify the configuration, for example, `Compile / moduleDependencies` instead
* of just `moduleDependencies`.
*
* If this plugin is enabled, and no settings/tasks from this plugin are used, then the plugin will
* not inject anything into `javaOptions` or `javacOptions`.
*
* == How to work with this plugin ==
*
* - Specify external dependencies in `Compile / moduleDependencies` with something like:
* {{{
* Compile / moduleDependencies := Seq(
* "org.apache.commons" % "commons-lang3" % "3.11",
* )
* }}}
* - Specify internal dependencies in `Compile / internalModuleDependencies` with something like:
* {{{
* Compile / internalModuleDependencies := Seq(
* (myProject / Compile / exportedModule).value
* )
* }}}
* - This ensures that `myProject` will be compiled, along with its `module-info.java`, and
* the resulting directory with all the classes, along with `module-info.class` will be put on
* the module path.
* - Ensure that all the module dependencies were gathered by the plugin correctly by
* `print modulePath`.
* - If not, make sure that these dependencies are in `libraryDependencies`.
* Debug this with `print dependencyClasspath`.
*
* == Mixed projects ==
*
* A project with both Java and Scala sources that call into each other is called *mixed*.
* sbt sets `compileOrder := CompileOrder.Mixed` for these projects.
* Having `module-info.java` and trying to compile such project fails, because sbt first
* tries to parse all the Java sources with its own custom parser, that does not recognize
* `module-info.java`.
* One has to **exclude** the `module-info.java` from the compilation with setting
* {{{
* excludeFilter := excludeFilter.value || "module-info.java"
* }}}
* This plugin tries to determine this case and will force the compilation of `module-info.java`.
* To see if this will be the case, check the value of `shouldCompileModuleInfoManually` task by
* `print shouldCompileModuleInfoManually`.
* In rare case, you have to override either `shouldCompileInfoManually` or `forceModuleInfoCompilation`.
*
* == Caveats ==
*
* - This plugin cannot determine transitive dependencies of modules in `moduleDependencies`.
* As opposed to `libraryDependencies` which automatically gathers all the transitive dependencies.
*/
object JPMSPlugin extends AutoPlugin {
object autoImport {
val javaModuleName =
settingKey[String]("The name of the Java (JPMS) module")
val addModules = settingKey[Seq[String]](
"Module names that will be added to --add-modules option"
)
val moduleDependencies = taskKey[Seq[ModuleID]](
"Modules dependencies that will be added to --module-path option. List all the sbt modules " +
"that should be added on module-path. Use it only for external dependencies."
)
val internalModuleDependencies = taskKey[Seq[File]](
"""
|Inter-project JPMS module dependencies. This task has a different return type than
|`moduleDependencies` task. It returns a sequence of files on purpose - that way,
|projects are able to override their `exportedModule` task to somehow prepare for
|modularization.
|""".stripMargin
)
val modulePath = taskKey[Seq[File]](
"Directories (Jar archives or expanded Jar archives) that will be put into " +
"--module-path option"
)
val patchModules = taskKey[Map[String, Seq[File]]](
"""
|A map of module names to directories (Jar archives or expanded Jar archives) that will be
|put into --patch-module option.
|""".stripMargin
)
val addExports = taskKey[Map[String, Seq[String]]](
"""
|A map of module names to packages that will be put into --add-exports option.
|The format of `--add-exports` option is `module/package=target-module(,target-module)*`
|The key in the map is `module/package` and the value is a sequence of target modules
|""".stripMargin
)
val addReads = taskKey[Map[String, Seq[String]]](
"""
|A map of module names to modules that will be put into --add-reads option.
|When a module A reads a module B, it means that it "depends" on it - it has the same
|effect as if module A would have `requires B` in its module-info.java file.
|""".stripMargin
)
val addOpens = taskKey[Map[String, Seq[String]]](
"""
|A map of module names with packages to modules that will be put into --add-opens option to java.
|Note that this option is not added to `javac`, only to `java`.
|For example `org.enso.runtime/org.enso.my.package=ALL-UNNAMED` will open the package
|`org.enso.my.package` in the module `org.enso.runtime` to all unnamed modules.
|Specify it as `addOpens := Map("org.enso.runtime/org.enso.my.package" -> List("ALL-UNNAMED"))`.
|""".stripMargin
)
val exportedModule = taskKey[File](
"""
|Similarly to `exportedProducts` task, this task returns a file that can be
|directly put on module-path. For majority of projects, this task will have
|the same result as `exportedProducts`. The purpose of this task is to be able
|for the projects to *prepare* for modularization. For example, mixed Scala/Java
|projects are known to be problematic for modularization, and one needs to manually
|compile `module-info.java` file. For this mixed project, this task can be declared
|to depend on `compileModuleInfo`.
|""".stripMargin
)
val exportedModuleBin = taskKey[File](
"Similar to `packageBin` task. This task returns a modular JAR archive that can be " +
"directly put on module-path"
)
val shouldCompileModuleInfoManually = taskKey[Boolean](
"If module-info.java should be compiled by us and not by sbt. " +
"DO NOT USE DIRECTLY."
)
val forceModuleInfoCompilation = taskKey[Boolean](
"Force module-info.java compilation. " +
"DO NOT USE DIRECTLY."
)
val compileModuleInfo = taskKey[Unit](
"Compiles only module-info.java in some special cases. " +
"DO NOT USE DIRECTLY."
)
}
import autoImport._
override lazy val projectSettings: Seq[Setting[_]] = {
// All the settings are scoped for Compile and Test
Seq(Compile, Test).flatMap { config: Configuration =>
Seq(
config / addModules := Seq.empty,
config / moduleDependencies := Seq.empty,
config / internalModuleDependencies := Seq.empty,
config / shouldCompileModuleInfoManually := {
val javaSrcDir = (config / javaSource).value
val modInfo =
javaSrcDir.toPath.resolve("module-info.java").toFile
val hasModInfo = modInfo.exists
val projName = moduleName.value
val logger = streams.value.log
val hasScalaSources = (config / scalaSource).value.exists()
val _compileOrder = (config / compileOrder).value
val res =
_compileOrder == CompileOrder.Mixed &&
hasModInfo &&
hasScalaSources
if (res) {
logger.debug(
s"[JPMSPlugin] Project '$projName' will have `module-info.java` compiled " +
"manually. If this is not the intended behavior, consult the documentation " +
"of JPMSPlugin."
)
}
// Check excludeFilter - there should be module-info.java specified
if (res && !excludeFilter.value.accept(modInfo)) {
logger.error(
s"[JPMSPlugin/$projName] `module-info.java` is not in `excludeFilter`. " +
"You should add module-info.java to " +
"`excludedFilter` so that sbt does not handle the compilation. Check docs of JPMSPlugin."
)
}
res
},
// module-info.java compilation will be forced iff there are no other Java sources except
// for module-info.java.
config / forceModuleInfoCompilation := Def.taskIf {
if ((config / shouldCompileModuleInfoManually).value) {
val javaSources = (config / unmanagedSources).value
.filter(_.getName.endsWith(".java"))
// If there are no Java source in `unmanagedSources`, it means that sbt will
// not call Java compiler. So we force it to compile `module-info.java`.
javaSources.isEmpty
} else {
false
}
}.value,
config / compileModuleInfo := Def.taskIf {
if ((config / forceModuleInfoCompilation).value) {
JPMSUtils.compileModuleInfo().value
} else {
// nop
()
}
}.value,
// modulePath is set based on `moduleDependencies` and `internalModuleDependencies`
config / modulePath := {
// Do not use fullClasspath here - it will result in an infinite recursion
// and sbt will not be able to detect the cycle.
transformModuleDependenciesToModulePath(
(config / moduleDependencies).value,
(config / internalModuleDependencies).value,
(config / dependencyClasspath).value,
streams.value.log,
moduleName.value,
scalaBinaryVersion.value
)
},
// Returns the reference to target/classes directory and ensures that module-info
// is compiled and present in the target directory.
config / exportedModule := Def
.task {
val targetClassDir = (config / exportedProducts).value
.map(_.data)
.head
val logger = streams.value.log
val projName = moduleName.value
if (!isModule(targetClassDir)) {
logger.warn(
s"[JPMSPlugin/$projName] The target classes directory ${targetClassDir.getAbsolutePath} is not " +
"a module - it does not contain module-info.class. Make sure the `compileModuleInfo` task " +
"is set correctly."
)
}
targetClassDir
}
.dependsOn(config / compileModuleInfo)
.dependsOn(config / compile)
.value,
config / exportedModuleBin := {
(config / packageBin)
.dependsOn(config / exportedModule)
.value
},
// All the exported artifact names will be stripped.
// Do not use the default sbt artifact name which inserts scala version and module
// revision.
config / artifactName := stripArtifactName,
config / patchModules := Map.empty,
config / addExports := Map.empty,
config / addReads := Map.empty,
config / addOpens := Map.empty,
// No --add-opens option to javac
config / javacOptions ++= {
constructOptions(
streams.value.log,
moduleName.value,
(config / modulePath).value,
(config / addModules).value,
(config / patchModules).value,
(config / addExports).value,
(config / addReads).value
)
},
config / javaOptions ++= {
constructOptions(
streams.value.log,
moduleName.value,
(config / modulePath).value,
(config / addModules).value,
(config / patchModules).value,
(config / addExports).value,
(config / addReads).value,
(config / addOpens).value
)
},
// Sanitize cmd line arguments
config / javacOptions := joinModulePathOption(
(config / javacOptions).value
),
config / javaOptions := joinModulePathOption(
(config / javaOptions).value
)
)
}
}
/** @param moduleDeps External module dependencies, fetched from `moduleDependencies` task.
* @param classPath Dependency class path of the project. From this class path, external dependencies
* will be searched for.
* @param internalModuleDeps Internal module dependencies, fetched from `internalModuleDependencies` task.
* It is assumed that there is `module-info.class` in the root of the internal
* module dependency.
* @param logger
* @param currProjName Current name of the local project, for debugging purposes.
* @return
*/
private def transformModuleDependenciesToModulePath(
moduleDeps: Seq[ModuleID],
internalModuleDeps: Seq[File],
classPath: Def.Classpath,
logger: ManagedLogger,
scalaBinaryVersion: String,
currProjName: String
): Seq[File] = {
moduleDeps.foreach { moduleDep =>
if (moduleDep.organization == "org.enso") {
logger.warn(
s"[JPMSPlugin/$currProjName] ModuleID $moduleDep specified inside " +
"`moduleDependencies` task. This is and internal dependency " +
"and should be specified in `internalModuleDependencies`. "
)
}
}
internalModuleDeps.foreach { internalModuleDep =>
if (internalModuleDep.isDirectory) {
val modInfo =
internalModuleDep.toPath.resolve("module-info.class").toFile
if (!modInfo.exists()) {
logger.warn(
s"[JPMSPlugin/$currProjName] Internal module dependency $internalModuleDep does not contain " +
"module-info.class file. Ensure it is an automatic module."
)
}
} else if (internalModuleDep.getName.endsWith(".jar")) {
val jarFile = new JarFile(internalModuleDep)
val modInfoEntry = jarFile.getJarEntry("module-info.class")
if (modInfoEntry == null) {
logger.warn(
s"[JPMSPlugin/$currProjName] Internal module dependency (JAR) $internalModuleDep does not contain " +
"module-info.class file. Ensure it is an automatic module."
)
}
} else {
logger.error(
s"[JPMSPlugin/$currProjName] Internal module dependency $internalModuleDep is not a directory " +
"nor a jar file. This is not supported. "
)
}
}
val cp = JPMSUtils.filterModulesFromClasspath(
classPath,
moduleDeps,
logger,
currProjName,
scalaBinaryVersion,
shouldContainAll = true
)
val externalFiles = cp.map(_.data)
externalFiles ++ internalModuleDeps
}
private def isModule(file: File): Boolean = {
if (file.isDirectory) {
val modInfo = file.toPath.resolve("module-info.class").toFile
modInfo.exists()
} else if (file.getName.endsWith(".jar")) {
val jarFile = new JarFile(file)
val modInfoEntry = jarFile.getJarEntry("module-info")
modInfoEntry == null
} else {
false
}
}
private def constructOptions(
log: Logger,
curProjName: String,
modulePath: Seq[File],
addModules: Seq[String] = Seq.empty,
patchModules: Map[String, Seq[File]] = Map.empty,
addExports: Map[String, Seq[String]] = Map.empty,
addReads: Map[String, Seq[String]] = Map.empty,
addOpens: Map[String, Seq[String]] = Map.empty
): Seq[String] = {
val patchOpts: Seq[String] = patchModules.flatMap {
case (moduleName, dirsToPatch) =>
val patchStr = dirsToPatch
.map(_.getAbsolutePath)
.mkString(File.pathSeparator)
Seq(
"--patch-module",
s"$moduleName=$patchStr"
)
}.toSeq
val addExportsOpts: Seq[String] = addExports.flatMap {
case (modPkgName, targetModules) =>
if (!modPkgName.contains("/")) {
log.error(
s"[JPMSPlugin/$curProjName] Invalid module/package name: $modPkgName " +
"in `addExports` task."
)
}
Seq(
"--add-exports",
modPkgName + "=" + targetModules.mkString(",")
)
}.toSeq
val modulePathOpts = if (modulePath.isEmpty) {
Seq.empty
} else {
Seq(
"--module-path",
modulePath.map(_.getAbsolutePath).mkString(File.pathSeparator)
)
}
val addModsOpts = if (addModules.isEmpty) {
Seq.empty
} else {
Seq(
"--add-modules",
addModules.mkString(",")
)
}
val addReadsOpts = addReads.flatMap { case (modName, targetModules) =>
Seq(
"--add-reads",
modName + "=" + targetModules.mkString(",")
)
}.toSeq
val addOpensOpts = addOpens.flatMap { case (modPkgName, targetModules) =>
if (!modPkgName.contains("/")) {
log.error(
s"[JPMSPlugin/$curProjName] Invalid module/package name: $modPkgName " +
"in `addOpens` task."
)
}
Seq(
"--add-opens",
modPkgName + "=" + targetModules.mkString(",")
)
}.toSeq
modulePathOpts ++ addModsOpts ++ patchOpts ++ addExportsOpts ++ addReadsOpts ++ addOpensOpts
}
/** Searches for multiple `--module-path` cmd line options and joins them into a single
* option.
* If there are multiple `--module-path` options passed to `java` or `javac`, only the
* last one specified is considered.
* Note that this is not an issue for other JPMS-related cmd line options, like
* `--add-modules`
* @param opts Current value of cmd line options
* @return
*/
private def joinModulePathOption(
opts: Seq[String]
): Seq[String] = {
val modulePathOpt = new StringBuilder()
val optIdxToRemove = mutable.HashSet[Int]()
// Find all `--module-path` options and join them into a single option
for ((opt, idx) <- opts.zipWithIndex) {
if (opt == "--module-path" || opt == "-p") {
optIdxToRemove += idx
optIdxToRemove += idx + 1
modulePathOpt.append(opts(idx + 1))
modulePathOpt.append(File.pathSeparator)
}
}
if (modulePathOpt.nonEmpty) {
// Remove the last colon
modulePathOpt.deleteCharAt(modulePathOpt.length - 1)
val newOpts = mutable.ArrayBuffer[String]()
for ((opt, idx) <- opts.zipWithIndex) {
if (!optIdxToRemove.contains(idx)) {
newOpts += opt
}
}
Seq(
"--module-path",
modulePathOpt.toString
) ++ newOpts
} else {
opts
}
}
/** Does not use the default artifact name which inserts scala version and module version.
*/
private def stripArtifactName(
scalaVersion: ScalaVersion,
modId: ModuleID,
artifact: Artifact
): String = {
// Classifier optionally adds e.g. `-test` or `-sources` to the artifact name
// This needs to be retained for the tests to work.
val classifierStr = artifact.classifier match {
case None => ""; case Some(c) => "-" + c
}
artifact.name + classifierStr + "." + artifact.extension
}
}