Skip to content

Commit 0421117

Browse files
committed
Add generating jars with Manifest for scalac
1 parent 447008e commit 0421117

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

compiler/src/dotty/tools/MainGenericRunner.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import dotty.tools.runner.ObjectRunner
1414
import dotty.tools.dotc.config.Properties.envOrNone
1515
import java.util.jar._
1616
import java.util.jar.Attributes.Name
17+
import dotty.tools.io.Jar
1718

1819
enum ExecuteMode:
1920
case Guess

compiler/src/dotty/tools/backend/jvm/GenBCode.scala

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,55 @@ class GenBCode extends Phase {
6868
// If we close the jar the next run will not be able to write on the jar.
6969
// But if we do not close it we cannot use it as part of the macro classpath of the suspended files.
7070
report.error("Can not suspend and output to a jar at the same time. See suspension with -Xprint-suspension.")
71+
72+
updateJarManifestWithMainClass(units, jar)
7173
jar.close()
7274
case _ =>
7375
}
7476
}
77+
78+
private def updateJarManifestWithMainClass(units: List[CompilationUnit], jarArchive: JarArchive)(using Context): Unit =
79+
val mainClass = Option.when(!ctx.settings.XmainClass.isDefault)(ctx.settings.XmainClass.value).orElse {
80+
val _mainClassesBuffer = new mutable.HashSet[String]
81+
units.map { unit =>
82+
unit.tpdTree.foreachSubTree { tree =>
83+
val sym = tree.symbol
84+
import dotty.tools.dotc.core.NameOps.stripModuleClassSuffix
85+
val name = sym.fullName.stripModuleClassSuffix.toString
86+
// We strip module class suffix. Zinc relies on a class and its companion having the same name
87+
88+
if (sym.isStatic && !sym.is(Flags.Trait) && ctx.platform.hasMainMethod(sym)) {
89+
// If sym is an object, all main methods count, otherwise only @static ones count.
90+
_mainClassesBuffer += name
91+
}
92+
}
93+
}
94+
_mainClassesBuffer.toList.match
95+
case List(mainClass) =>
96+
Some(mainClass)
97+
case Nil =>
98+
report.warning("No Main-Class designated or discovered.")
99+
None
100+
case mcs =>
101+
report.warning(s"No Main-Class due to multiple entry points:\n ${mcs.mkString("\n ")}")
102+
None
103+
}
104+
105+
mainClass.map { mc =>
106+
import scala.util.Properties._
107+
import java.util.jar._
108+
import Attributes.Name._
109+
val manifest = new Manifest
110+
val attrs = manifest.getMainAttributes
111+
attrs.put(MANIFEST_VERSION, "1.0")
112+
attrs.put(ScalaCompilerVersion, versionNumberString)
113+
attrs.put(MAIN_CLASS, mc)
114+
val file = jarArchive.subdirectoryNamed("META-INF").fileNamed("MANIFEST.MF")
115+
val os = file.output
116+
manifest.write(os)
117+
os.close()
118+
}
119+
end updateJarManifestWithMainClass
75120
}
76121

77122
object GenBCode {

compiler/src/dotty/tools/dotc/config/ScalaSettings.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ private sealed trait XSettings:
156156
val Xsemanticdb: Setting[Boolean] = BooleanSetting("-Xsemanticdb", "Store information in SemanticDB.", aliases = List("-Ysemanticdb"))
157157
val Xtarget: Setting[String] = ChoiceSetting("-Xtarget", "target", "Emit bytecode for the specified version of the Java platform. This might produce bytecode that will break at runtime. When on JDK 9+, consider -release as a safer alternative.", ScalaSettings.supportedTargetVersions, "", aliases = List("--Xtarget"))
158158
val XcheckMacros: Setting[Boolean] = BooleanSetting("-Xcheck-macros", "Check some invariants of macro generated code while expanding macros", aliases = List("--Xcheck-macros"))
159+
val XmainClass: Setting[String] = StringSetting("-Xmain-class", "path", "Class for manifest's Main-Class entry (only useful with -d <jar>)", "")
159160

160161
val XmixinForceForwarders = ChoiceSetting(
161162
name = "-Xmixin-force-forwarders",

hihi.jar

3.24 KB
Binary file not shown.

0 commit comments

Comments
 (0)