|
| 1 | +package mill |
| 2 | +package groovylib |
| 3 | + |
| 4 | +import mill.api.{ModuleRef, Result} |
| 5 | +import mill.javalib.api.CompilationResult |
| 6 | +import mill.javalib.api.JvmWorkerApi as PublicJvmWorkerApi |
| 7 | +import mill.api.daemon.internal.{CompileProblemReporter, GroovyModuleApi, internal} |
| 8 | +import mill.javalib.{Dep, JavaModule, JvmWorkerModule, Lib} |
| 9 | +import mill.* |
| 10 | +import mainargs.Flag |
| 11 | +import mill.api.daemon.internal.bsp.{BspBuildTarget, BspModuleApi} |
| 12 | +import mill.javalib.api.internal.{JavaCompilerOptions, JvmWorkerApi, ZincCompileJava} |
| 13 | + |
| 14 | +/** |
| 15 | + * Core configuration required to compile a single Groovy module |
| 16 | + */ |
| 17 | +trait GroovyModule extends JavaModule with GroovyModuleApi { outer => |
| 18 | + |
| 19 | + /** |
| 20 | + * The Groovy version to be used (for API and Language level settings). |
| 21 | + */ |
| 22 | + def groovyVersion: T[String] |
| 23 | + |
| 24 | + /** |
| 25 | + * The compiler language version. Default is derived from [[groovyVersion]]. |
| 26 | + */ |
| 27 | + def groovyLanguageVersion: T[String] = Task { groovyVersion().split("[.]").take(2).mkString(".") } |
| 28 | + |
| 29 | + override def bomMvnDeps: T[Seq[Dep]] = super.bomMvnDeps() ++ Seq( |
| 30 | + mvn"org.apache.groovy:groovy-bom:${groovyVersion()}" |
| 31 | + ) |
| 32 | + |
| 33 | + /** |
| 34 | + * All individual source files fed into the compiler. |
| 35 | + */ |
| 36 | + override def allSourceFiles: T[Seq[PathRef]] = Task { |
| 37 | + Lib.findSourceFiles(allSources(), Seq("groovy", "java")).map(PathRef(_)) |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * All individual Java source files fed into the compiler. |
| 42 | + * Subset of [[allSourceFiles]]. |
| 43 | + */ |
| 44 | + private def allJavaSourceFiles: T[Seq[PathRef]] = Task { |
| 45 | + allSourceFiles().filter(_.path.ext.toLowerCase() == "java") |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * All individual Groovy source files fed into the compiler. |
| 50 | + * Subset of [[allSourceFiles]]. |
| 51 | + */ |
| 52 | + private def allGroovySourceFiles: T[Seq[PathRef]] = Task { |
| 53 | + allSourceFiles().filter(path => Seq("groovy").contains(path.path.ext.toLowerCase())) |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * The dependencies of this module. |
| 58 | + * Defaults to add the groovy-stdlib dependency matching the [[groovyVersion]]. |
| 59 | + */ |
| 60 | + override def mandatoryMvnDeps: T[Seq[Dep]] = Task { |
| 61 | + super.mandatoryMvnDeps() ++ Seq( |
| 62 | + mvn"org.apache.groovy:groovy:${groovyVersion()}" |
| 63 | + ) |
| 64 | + } |
| 65 | + |
| 66 | + private def jvmWorkerRef: ModuleRef[JvmWorkerModule] = jvmWorker |
| 67 | + |
| 68 | + override def checkGradleModules: T[Boolean] = true |
| 69 | + |
| 70 | + /** |
| 71 | + * The Java classpath resembling the Groovy compiler. |
| 72 | + * Default is derived from [[groovyCompilerMvnDeps]]. |
| 73 | + */ |
| 74 | + def groovyCompilerClasspath: T[Seq[PathRef]] = Task { |
| 75 | + val deps = groovyCompilerMvnDeps() ++ Seq( |
| 76 | + Dep.millProjectModule("mill-libs-groovylib-worker") |
| 77 | + ) |
| 78 | + defaultResolver().classpath( |
| 79 | + deps, |
| 80 | + resolutionParamsMapOpt = None |
| 81 | + ) |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * The Ivy/Coursier dependencies resembling the Groovy compiler. |
| 86 | + * |
| 87 | + * Default is derived from [[groovyCompilerVersion]]. |
| 88 | + */ |
| 89 | + def groovyCompilerMvnDeps: T[Seq[Dep]] = Task { |
| 90 | + val gv = groovyVersion() |
| 91 | + |
| 92 | + val compilerDep = mvn"org.apache.groovy:groovy:$gv" |
| 93 | + |
| 94 | + Seq(compilerDep) |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Compiler Plugin dependencies. |
| 99 | + */ |
| 100 | + def groovycPluginMvnDeps: T[Seq[Dep]] = Task { Seq.empty[Dep] } |
| 101 | + |
| 102 | + /** |
| 103 | + * The resolved plugin jars |
| 104 | + */ |
| 105 | + def groovycPluginJars: T[Seq[PathRef]] = Task { |
| 106 | + val jars = defaultResolver().classpath( |
| 107 | + groovycPluginMvnDeps() |
| 108 | + // Don't resolve transitive jars |
| 109 | + .map(d => d.exclude("*" -> "*")), |
| 110 | + resolutionParamsMapOpt = None |
| 111 | + ) |
| 112 | + jars.toSeq |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Compiles all the sources to JVM class files. |
| 117 | + */ |
| 118 | + override def compile: T[CompilationResult] = Task { |
| 119 | + groovyCompileTask()() |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * The actual Groovy compile task (used by [[compile]] and [[groovycHelp]]). |
| 124 | + */ |
| 125 | + protected def groovyCompileTask(): Task[CompilationResult] = |
| 126 | + Task.Anon { |
| 127 | + val ctx = Task.ctx() |
| 128 | + val dest = ctx.dest |
| 129 | + val classes = dest / "classes" |
| 130 | + os.makeDir.all(classes) |
| 131 | + |
| 132 | + val javaSourceFiles = allJavaSourceFiles().map(_.path) |
| 133 | + val groovySourceFiles = allGroovySourceFiles().map(_.path) |
| 134 | + |
| 135 | + val isGroovy = groovySourceFiles.nonEmpty |
| 136 | + val isJava = javaSourceFiles.nonEmpty |
| 137 | + val isMixed = isGroovy && isJava |
| 138 | + |
| 139 | + val compileCp = compileClasspath().map(_.path).filter(os.exists) |
| 140 | + val updateCompileOutput = upstreamCompileOutput() |
| 141 | + |
| 142 | + def compileJava: Result[CompilationResult] = { |
| 143 | + ctx.log.info( |
| 144 | + s"Compiling ${javaSourceFiles.size} Java sources to ${classes} ..." |
| 145 | + ) |
| 146 | + // The compile step is lazy, but its dependencies are not! |
| 147 | + internalCompileJavaFiles( |
| 148 | + worker = jvmWorkerRef().internalWorker(), |
| 149 | + upstreamCompileOutput = updateCompileOutput, |
| 150 | + javaSourceFiles = javaSourceFiles, |
| 151 | + compileCp = compileCp, |
| 152 | + javaHome = javaHome().map(_.path), |
| 153 | + javacOptions = javacOptions(), |
| 154 | + compileProblemReporter = ctx.reporter(hashCode), |
| 155 | + reportOldProblems = internalReportOldProblems() |
| 156 | + ) |
| 157 | + } |
| 158 | + |
| 159 | + if (isMixed || isGroovy) { |
| 160 | + ctx.log.info( |
| 161 | + s"Compiling ${groovySourceFiles.size} Groovy sources to ${classes} ..." |
| 162 | + ) |
| 163 | + |
| 164 | + val compileCp = compileClasspath().map(_.path).filter(os.exists) |
| 165 | + |
| 166 | + val workerResult = |
| 167 | + GroovyWorkerManager.groovyWorker().withValue(groovyCompilerClasspath()) { |
| 168 | + _.compile(groovySourceFiles, compileCp, classes) |
| 169 | + } |
| 170 | + |
| 171 | + val analysisFile = dest / "groovy.analysis.dummy" // FIXME |
| 172 | + os.write(target = analysisFile, data = "", createFolders = true) |
| 173 | + |
| 174 | + workerResult match { |
| 175 | + case Result.Success(_) => |
| 176 | + val cr = CompilationResult(analysisFile, PathRef(classes)) |
| 177 | + if (!isJava) { |
| 178 | + // pure Groovy project |
| 179 | + cr |
| 180 | + } else { |
| 181 | + // also run Java compiler and use it's returned result |
| 182 | + compileJava |
| 183 | + } |
| 184 | + case Result.Failure(reason) => Result.Failure(reason) |
| 185 | + } |
| 186 | + } else { |
| 187 | + // it's Java only |
| 188 | + compileJava |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Additional Groovy compiler options to be used by [[compile]]. |
| 194 | + */ |
| 195 | + def groovycOptions: T[Seq[String]] = Task { Seq.empty[String] } |
| 196 | + |
| 197 | + /** |
| 198 | + * Aggregation of all the options passed to the Groovy compiler. |
| 199 | + * In most cases, instead of overriding this Target you want to override `groovycOptions` instead. |
| 200 | + */ |
| 201 | + def allGroovycOptions: T[Seq[String]] = Task { |
| 202 | + groovycOptions() |
| 203 | + } |
| 204 | + |
| 205 | + private[groovylib] def internalCompileJavaFiles( |
| 206 | + worker: JvmWorkerApi, |
| 207 | + upstreamCompileOutput: Seq[CompilationResult], |
| 208 | + javaSourceFiles: Seq[os.Path], |
| 209 | + compileCp: Seq[os.Path], |
| 210 | + javaHome: Option[os.Path], |
| 211 | + javacOptions: Seq[String], |
| 212 | + compileProblemReporter: Option[CompileProblemReporter], |
| 213 | + reportOldProblems: Boolean |
| 214 | + )(implicit ctx: PublicJvmWorkerApi.Ctx): Result[CompilationResult] = { |
| 215 | + val jOpts = JavaCompilerOptions(javacOptions) |
| 216 | + worker.compileJava( |
| 217 | + ZincCompileJava( |
| 218 | + upstreamCompileOutput = upstreamCompileOutput, |
| 219 | + sources = javaSourceFiles, |
| 220 | + compileClasspath = compileCp, |
| 221 | + javacOptions = jOpts.compiler, |
| 222 | + incrementalCompilation = true |
| 223 | + ), |
| 224 | + javaHome = javaHome, |
| 225 | + javaRuntimeOptions = jOpts.runtime, |
| 226 | + reporter = compileProblemReporter, |
| 227 | + reportCachedProblems = reportOldProblems |
| 228 | + ) |
| 229 | + } |
| 230 | + |
| 231 | + private[groovylib] def internalReportOldProblems: Task[Boolean] = zincReportCachedProblems |
| 232 | + |
| 233 | + @internal |
| 234 | + override def bspBuildTarget: BspBuildTarget = super.bspBuildTarget.copy( |
| 235 | + languageIds = Seq( |
| 236 | + BspModuleApi.LanguageId.Java, |
| 237 | + BspModuleApi.LanguageId.Groovy |
| 238 | + ), |
| 239 | + canCompile = true, |
| 240 | + canRun = true |
| 241 | + ) |
| 242 | + |
| 243 | + override def prepareOffline(all: Flag): Command[Seq[PathRef]] = Task.Command { |
| 244 | + ( |
| 245 | + super.prepareOffline(all)() ++ |
| 246 | + groovyCompilerClasspath() |
| 247 | + ).distinct |
| 248 | + } |
| 249 | + |
| 250 | + /** |
| 251 | + * A test sub-module linked to its parent module best suited for unit-tests. |
| 252 | + */ |
| 253 | + trait GroovyTests extends JavaTests with GroovyModule { |
| 254 | + |
| 255 | + override def groovyLanguageVersion: T[String] = outer.groovyLanguageVersion() |
| 256 | + override def groovyVersion: T[String] = Task { outer.groovyVersion() } |
| 257 | + override def groovycPluginMvnDeps: T[Seq[Dep]] = |
| 258 | + Task { outer.groovycPluginMvnDeps() } |
| 259 | + // TODO: make Xfriend-path an explicit setting |
| 260 | + override def groovycOptions: T[Seq[String]] = Task { |
| 261 | + // FIXME |
| 262 | + outer.groovycOptions().filterNot(_.startsWith("-Xcommon-sources")) ++ |
| 263 | + Seq(s"-Xfriend-paths=${outer.compile().classes.path.toString()}") |
| 264 | + } |
| 265 | + } |
| 266 | + |
| 267 | +} |
| 268 | + |
| 269 | +// TODO maybe an StandaloneGroovyTestsModule |
0 commit comments