Skip to content

Commit 13a1799

Browse files
authored
Merge pull request github#99 from github/kotlin-build-versions
Change build script to build multiple versions of the plugin
2 parents a71de45 + fd6d0ae commit 13a1799

File tree

12 files changed

+184
-71
lines changed

12 files changed

+184
-71
lines changed

java/kotlin-extractor/build.gradle

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ repositories {
1818
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
1919
kotlinOptions {
2020
jvmTarget = "1.8"
21+
freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"
22+
// enable the below for building with kotlinVersion=1.4.32:
23+
// languageVersion = "1.5"
24+
}
25+
}
26+
27+
sourceSets {
28+
main {
29+
kotlin {
30+
excludes = ["utils/versions/v_1_4/*.kt"]
31+
// change the above line to the below for building with kotlinVersion=1.4.32:
32+
//excludes = ["utils/versions/default/*.kt"]
33+
}
2134
}
2235
}
2336

java/kotlin-extractor/build.py

Lines changed: 102 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,50 @@
11
#!/usr/bin/env python3
22

3+
import kotlin_plugin_versions
34
import glob
45
import re
56
import subprocess
67
import shutil
78
import os
89
import os.path
910
import sys
11+
import shlex
1012

1113
kotlinc = 'kotlinc'
1214
javac = 'javac'
1315

16+
kotlin_dependency_folder = '../../../resources/kotlin-dependencies'
17+
if (len(sys.argv) > 1):
18+
kotlin_dependency_folder = sys.argv[1]
19+
20+
21+
def run_process(cmd):
22+
try:
23+
# print("Running command: " + shlex.join(cmd))
24+
return subprocess.run(cmd, check=True, capture_output=True)
25+
except subprocess.CalledProcessError as e:
26+
print("Command failed: " + shlex.join(cmd), file=sys.stderr)
27+
print("Output: " + e.stderr.decode(encoding='UTF-8',
28+
errors='strict'), file=sys.stderr)
29+
raise e
30+
1431

1532
def compile_to_dir(srcs, classpath, java_classpath, output):
1633
# Use kotlinc to compile .kt files:
17-
subprocess.run([kotlinc,
18-
# kotlinc can default to 256M, which isn't enough when we are extracting the build
19-
'-J-Xmx2G',
20-
'-d', output,
21-
'-module-name', 'codeql-kotlin-extractor',
22-
'-no-reflect',
23-
'-jvm-target', '1.8',
24-
'-classpath', classpath] + srcs, check=True)
34+
run_process([kotlinc,
35+
# kotlinc can default to 256M, which isn't enough when we are extracting the build
36+
'-J-Xmx2G',
37+
'-d', output,
38+
'-module-name', 'codeql-kotlin-extractor',
39+
'-no-reflect', '-no-stdlib',
40+
'-jvm-target', '1.8',
41+
'-classpath', classpath] + srcs)
42+
2543
# Use javac to compile .java files, referencing the Kotlin class files:
26-
subprocess.run([javac,
27-
'-d', output,
28-
'-source', '8', '-target', '8',
29-
'-classpath', "%s:%s:%s" % (output, classpath, java_classpath)] + [s for s in srcs if s.endswith(".java")], check=True)
44+
run_process([javac,
45+
'-d', output,
46+
'-source', '8', '-target', '8',
47+
'-classpath', "%s:%s:%s" % (output, classpath, java_classpath)] + [s for s in srcs if s.endswith(".java")])
3048

3149

3250
def compile_to_jar(srcs, classpath, java_classpath, output):
@@ -39,9 +57,9 @@ def compile_to_jar(srcs, classpath, java_classpath, output):
3957

4058
compile_to_dir(srcs, classpath, java_classpath, builddir)
4159

42-
subprocess.run(['jar', '-c', '-f', output,
43-
'-C', builddir, '.',
44-
'-C', 'src/main/resources', 'META-INF'], check=True)
60+
run_process(['jar', '-c', '-f', output,
61+
'-C', builddir, '.',
62+
'-C', 'src/main/resources', 'META-INF'])
4563
finally:
4664
if os.path.exists(builddir):
4765
shutil.rmtree(builddir)
@@ -51,34 +69,35 @@ def find_sources(path):
5169
return glob.glob(path + '/**/*.kt', recursive=True) + glob.glob(path + '/**/*.java', recursive=True)
5270

5371

54-
def jarnames_to_classpath(path, jars):
55-
return ":".join(os.path.join(path, jar) + ".jar" for jar in jars)
56-
57-
58-
def compile_standalone():
59-
srcs = find_sources("src")
60-
jars = ['kotlin-compiler']
61-
java_jars = ['kotlin-stdlib']
62-
63-
x = subprocess.run([kotlinc, '-version', '-verbose'],
64-
check=True, capture_output=True)
72+
def get_kotlin_lib_folder():
73+
x = run_process([kotlinc, '-version', '-verbose'])
6574
output = x.stderr.decode(encoding='UTF-8', errors='strict')
6675
m = re.match(
6776
r'.*\nlogging: using Kotlin home directory ([^\n]+)\n.*', output)
6877
if m is None:
6978
raise Exception('Cannot determine kotlinc home directory')
7079
kotlin_home = m.group(1)
71-
kotlin_lib = kotlin_home + '/lib'
72-
classpath = jarnames_to_classpath(kotlin_lib, jars)
73-
java_classpath = jarnames_to_classpath(kotlin_lib, java_jars)
80+
print("Kotlin home directory: " + kotlin_home)
81+
return kotlin_home + '/lib'
82+
7483

75-
compile_to_jar(srcs, classpath, java_classpath, 'codeql-extractor-kotlin-standalone.jar')
84+
def get_gradle_lib_folder():
85+
x = run_process(['gradle', 'getHomeDir'])
86+
output = x.stdout.decode(encoding='UTF-8', errors='strict')
87+
m = re.search(r'(?m)^> Task :getHomeDir\n([^\n]+)$', output)
88+
if m is None:
89+
print("gradle getHomeDir output:\n" + output, file=sys.stderr)
90+
raise Exception('Cannot determine gradle home directory')
91+
gradle_home = m.group(1)
92+
print("Gradle home directory: " + gradle_home)
93+
return gradle_home + '/lib'
7694

7795

7896
def find_jar(path, pattern):
7997
result = glob.glob(path + '/' + pattern + '*.jar')
8098
if len(result) == 0:
81-
raise Exception('Cannot find jar file %s under path %s' % (pattern, path))
99+
raise Exception('Cannot find jar file %s under path %s' %
100+
(pattern, path))
82101
return result
83102

84103

@@ -89,42 +108,61 @@ def patterns_to_classpath(path, patterns):
89108
return ':'.join(result)
90109

91110

92-
def compile_embeddable():
93-
x = subprocess.run(['gradle', 'getHomeDir'],
94-
check=True, capture_output=True)
95-
output = x.stdout.decode(encoding='UTF-8', errors='strict')
96-
m = re.search(r'(?m)^> Task :getHomeDir\n([^\n]+)$', output)
97-
if m is None:
98-
print("gradle getHomeDir output:\n" + output, file = sys.stderr)
99-
raise Exception('Cannot determine gradle home directory')
100-
gradle_home = m.group(1)
111+
def transform_to_embeddable(srcs):
112+
# replace imports in files:
113+
for src in srcs:
114+
with open(src, 'r') as f:
115+
content = f.read()
116+
content = content.replace('import com.intellij',
117+
'import org.jetbrains.kotlin.com.intellij')
118+
with open(src, 'w') as f:
119+
f.write(content)
120+
101121

102-
gradle_lib = gradle_home + '/lib'
103-
jar_patterns = ['kotlin-compiler-embeddable']
104-
java_jar_patterns = ['kotlin-stdlib']
105-
classpath = patterns_to_classpath(gradle_lib, jar_patterns)
106-
java_classpath = patterns_to_classpath(gradle_lib, java_jar_patterns)
122+
def compile(jars, java_jars, dependency_folder, transform_to_embeddable, output, tmp_dir, version):
123+
classpath = patterns_to_classpath(dependency_folder, jars)
124+
java_classpath = patterns_to_classpath(dependency_folder, java_jars)
107125

108126
try:
109-
if os.path.exists('build/temp_src'):
110-
shutil.rmtree('build/temp_src')
111-
shutil.copytree('src', 'build/temp_src')
112-
srcs = find_sources('build/temp_src')
113-
114-
# replace imports in files:
115-
for src in srcs:
116-
with open(src, 'r') as f:
117-
content = f.read()
118-
content = content.replace('import com.intellij',
119-
'import org.jetbrains.kotlin.com.intellij')
120-
with open(src, 'w') as f:
121-
f.write(content)
122-
123-
compile_to_jar(srcs, classpath, java_classpath, 'codeql-extractor-kotlin-embeddable.jar')
127+
if os.path.exists(tmp_dir):
128+
shutil.rmtree(tmp_dir)
129+
shutil.copytree('src', tmp_dir)
130+
131+
if version.startswith('1.4'):
132+
shutil.rmtree(tmp_dir + '/main/kotlin/utils/versions/default')
133+
else:
134+
shutil.rmtree(tmp_dir + '/main/kotlin/utils/versions/v_1_4')
135+
136+
srcs = find_sources(tmp_dir)
137+
138+
transform_to_embeddable(srcs)
139+
140+
compile_to_jar(srcs, classpath, java_classpath, output)
124141
finally:
125-
if os.path.exists('build/temp_src'):
126-
shutil.rmtree('build/temp_src')
142+
if os.path.exists(tmp_dir):
143+
shutil.rmtree(tmp_dir)
144+
145+
146+
def compile_embeddable(version):
147+
compile(['kotlin-stdlib-' + version, 'kotlin-compiler-embeddable-' + version],
148+
['kotlin-stdlib-' + version],
149+
kotlin_dependency_folder,
150+
transform_to_embeddable,
151+
'codeql-extractor-kotlin-embeddable-%s.jar' % (version),
152+
'build/temp_src',
153+
version)
154+
155+
156+
def compile_standalone(version):
157+
compile(['kotlin-stdlib-' + version, 'kotlin-compiler-' + version],
158+
['kotlin-stdlib-' + version],
159+
kotlin_dependency_folder,
160+
lambda srcs: None,
161+
'codeql-extractor-kotlin-standalone-%s.jar' % (version),
162+
'build/temp_src',
163+
version)
127164

128165

129-
compile_standalone()
130-
compile_embeddable()
166+
for version in kotlin_plugin_versions.versions:
167+
compile_standalone(version)
168+
compile_embeddable(version)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
versions = [ '1.4.32', '1.5.31', '1.6.0-RC2' ]

java/kotlin-extractor/src/main/kotlin/KotlinExtractorExtension.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ class KotlinSourceFileExtractor(
243243
CommentExtractor(this).extract()
244244
}
245245

246+
@OptIn(kotlin.ExperimentalStdlibApi::class) // Annotation required by kotlin versions < 1.5
246247
fun extractFileClass(f: IrFile): Label<out DbClass> {
247248
val fileName = f.fileEntry.name
248249
val pkg = f.fqName.asString()

java/kotlin-extractor/src/main/kotlin/TrapWriter.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.github.codeql
22

3+
import com.github.codeql.utils.versions.FileEntry
34
import java.io.BufferedWriter
45
import java.io.File
56
import org.jetbrains.kotlin.ir.IrElement
6-
import org.jetbrains.kotlin.ir.IrFileEntry
77
import org.jetbrains.kotlin.ir.declarations.path
88
import org.jetbrains.kotlin.ir.declarations.IrFile
99
import org.jetbrains.kotlin.ir.declarations.IrVariable
@@ -82,7 +82,7 @@ open class TrapWriter (val lm: TrapLabelManager, val bw: BufferedWriter) {
8282
* Gets a FileTrapWriter like this one (using the same label manager, writer etc), but with the given
8383
* default file used in getLocation etc.
8484
*/
85-
fun withTargetFile(filePath: String, fileEntry: IrFileEntry?, populateFileTables: Boolean = true) =
85+
fun withTargetFile(filePath: String, fileEntry: FileEntry?, populateFileTables: Boolean = true) =
8686
FileTrapWriter(lm, bw, filePath, fileEntry, populateFileTables)
8787
}
8888

@@ -104,7 +104,7 @@ open class FileTrapWriter (
104104
lm: TrapLabelManager,
105105
bw: BufferedWriter,
106106
val filePath: String,
107-
val sourceFileEntry: IrFileEntry?,
107+
val sourceFileEntry: FileEntry?,
108108
populateFileTables: Boolean = true
109109
): TrapWriter (lm, bw) {
110110
val populateFile = PopulateFile(this)

java/kotlin-extractor/src/main/kotlin/comments/CommentExtractor.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package com.github.codeql.comments
22

33
import com.github.codeql.*
44
import com.github.codeql.utils.IrVisitorLookup
5+
import com.github.codeql.utils.versions.Psi2Ir
56
import com.intellij.psi.PsiComment
67
import com.intellij.psi.PsiElement
7-
import org.jetbrains.kotlin.backend.jvm.ir.getKtFile
88
import org.jetbrains.kotlin.ir.IrElement
99
import org.jetbrains.kotlin.ir.declarations.path
1010
import org.jetbrains.kotlin.kdoc.psi.api.KDoc
@@ -17,7 +17,7 @@ class CommentExtractor(private val fileExtractor: KotlinSourceFileExtractor) {
1717
private val file = fileExtractor.file
1818
private val tw = fileExtractor.tw
1919
private val logger = fileExtractor.logger
20-
private val ktFile = file.getKtFile()
20+
private val ktFile = Psi2Ir().getKtFile(file)
2121

2222
init {
2323
if (ktFile == null) {

java/kotlin-extractor/src/main/kotlin/utils/IrVisitorLookup.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.github.codeql.utils
22

3+
import com.github.codeql.utils.versions.Psi2Ir
34
import com.intellij.psi.PsiElement
4-
import org.jetbrains.kotlin.backend.common.psi.PsiSourceManager
55
import org.jetbrains.kotlin.ir.IrElement
66
import org.jetbrains.kotlin.ir.declarations.IrFile
77
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
@@ -19,7 +19,7 @@ class IrVisitorLookup(private val psi: PsiElement, private val file: IrFile) :
1919
}
2020

2121
if (location.contains(elementLocation)) {
22-
val psiElement = PsiSourceManager.findPsiElement(element, file)
22+
val psiElement = Psi2Ir().findPsiElement(element, file)
2323
if (psiElement == psi) {
2424
// There can be multiple IrElements that match the same PSI element.
2525
data.add(element)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.github.codeql.utils.versions
2+
3+
import com.intellij.psi.PsiElement
4+
import org.jetbrains.kotlin.ir.IrElement
5+
import org.jetbrains.kotlin.ir.declarations.IrFile
6+
import org.jetbrains.kotlin.psi.KtFile
7+
8+
interface Psi2IrFacade {
9+
fun getKtFile(irFile: IrFile): KtFile?
10+
fun findPsiElement(irElement: IrElement, irFile: IrFile): PsiElement?
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.github.codeql.utils.versions
2+
3+
import org.jetbrains.kotlin.ir.IrFileEntry
4+
5+
typealias FileEntry = IrFileEntry
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.github.codeql.utils.versions
2+
3+
import com.intellij.psi.PsiElement
4+
import org.jetbrains.kotlin.backend.common.psi.PsiSourceManager
5+
import org.jetbrains.kotlin.backend.jvm.ir.getKtFile
6+
import org.jetbrains.kotlin.ir.IrElement
7+
import org.jetbrains.kotlin.ir.declarations.IrFile
8+
import org.jetbrains.kotlin.psi.KtFile
9+
10+
class Psi2Ir: Psi2IrFacade {
11+
override fun getKtFile(irFile: IrFile): KtFile? {
12+
return irFile.getKtFile()
13+
}
14+
15+
override fun findPsiElement(irElement: IrElement, irFile: IrFile): PsiElement? {
16+
return PsiSourceManager.findPsiElement(irElement, irFile)
17+
}
18+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.github.codeql.utils.versions
2+
3+
import org.jetbrains.kotlin.ir.SourceManager
4+
5+
typealias FileEntry = SourceManager.FileEntry
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.github.codeql.utils.versions
2+
3+
import com.intellij.psi.PsiElement
4+
import org.jetbrains.kotlin.ir.IrElement
5+
import org.jetbrains.kotlin.ir.declarations.IrFile
6+
import org.jetbrains.kotlin.psi.KtFile
7+
import org.jetbrains.kotlin.psi2ir.PsiSourceManager
8+
9+
class Psi2Ir : Psi2IrFacade {
10+
companion object {
11+
val psiManager = PsiSourceManager()
12+
}
13+
14+
override fun getKtFile(irFile: IrFile): KtFile? {
15+
return psiManager.getKtFile(irFile)
16+
}
17+
18+
override fun findPsiElement(irElement: IrElement, irFile: IrFile): PsiElement? {
19+
return psiManager.findPsiElement(irElement, irFile)
20+
}
21+
}

0 commit comments

Comments
 (0)