@@ -31,12 +31,20 @@ object SourcegraphEnable {
31
31
)
32
32
33
33
val semanticdbJavacVersion = Versions .semanticdbJavacVersion()
34
+
34
35
val settings = for {
35
36
(p, semanticdbVersion, overriddenScalaVersion) <- collectProjects(
36
37
extracted
37
38
)
38
39
enableSemanticdbPlugin =
39
40
List (
41
+ Option (
42
+ javacOptions.in(p) ++= {
43
+ if (Versions .isJavaAtLeast(17 , home = javaHome.in(p).value))
44
+ javacModuleOptions
45
+ else Nil
46
+ }
47
+ ),
40
48
Option (
41
49
allDependencies.in(p) +=
42
50
" com.sourcegraph" % " semanticdb-javac" % semanticdbJavacVersion
@@ -51,6 +59,11 @@ object SourcegraphEnable {
51
59
Option (SemanticdbPlugin .semanticdbEnabled.in(p) := true ),
52
60
semanticdbVersion.map(ver =>
53
61
SemanticdbPlugin .semanticdbVersion.in(p) := ver
62
+ ),
63
+ Option (
64
+ javaHome.in(p) := {
65
+ javaHome.in(p).value orElse calculateJavaHome
66
+ }
54
67
)
55
68
).flatten
56
69
settings <-
@@ -101,6 +114,14 @@ object SourcegraphEnable {
101
114
)
102
115
)
103
116
}.toSeq
117
+ ),
118
+ Option (
119
+ javacOptions.in(p) ++= {
120
+ if (Versions .isJavaAtLeast(17 )) javacModuleOptions else Nil
121
+ }
122
+ ),
123
+ Option (
124
+ javaHome.in(p) := javaHome.in(p).value orElse calculateJavaHome
104
125
)
105
126
).flatten
106
127
settings <-
@@ -143,4 +164,34 @@ object SourcegraphEnable {
143
164
semanticdbVersion = Versions
144
165
.semanticdbVersion(overriddenScalaVersion.getOrElse(projectScalaVersion))
145
166
} yield (p, semanticdbVersion, overriddenScalaVersion)
167
+
168
+ def javacModuleOptions : List [String ] =
169
+ List (
170
+ " -J--add-exports" ,
171
+ " -Jjdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED" ,
172
+ " -J--add-exports" ,
173
+ " -Jjdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED" ,
174
+ " -J--add-exports" ,
175
+ " -Jjdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED" ,
176
+ " -J--add-exports" ,
177
+ " -Jjdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED" ,
178
+ " -J--add-exports" ,
179
+ " -Jjdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED"
180
+ )
181
+
182
+ private def calculateJavaHome = {
183
+ // We can safely use java.home property
184
+ // on JDK 17+ as it won't be pointing to JRE which
185
+ // doesn't contain a compiler.
186
+ if (Versions .isJavaAtLeast(17 )) {
187
+ // On JDK 17+ we need to explicitly fork the compiler
188
+ // so that we can set the necessary JVM options to access
189
+ // jdk.compiler module
190
+ Some (new File (System .getProperty(" java.home" )))
191
+ } else {
192
+ // If JDK is below 17, we don't actually need to
193
+ // fork the compiler, so we can keep javaHome empty
194
+ None
195
+ }
196
+ }
146
197
}
0 commit comments