1
1
#!/usr/bin/env python3
2
2
3
+ import kotlin_plugin_versions
3
4
import glob
4
5
import re
5
6
import subprocess
6
7
import shutil
7
8
import os
8
9
import os .path
9
10
import sys
11
+ import shlex
10
12
11
13
kotlinc = 'kotlinc'
12
14
javac = 'javac'
13
15
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
+
14
31
15
32
def compile_to_dir (srcs , classpath , java_classpath , output ):
16
33
# 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
+
25
43
# 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" )])
30
48
31
49
32
50
def compile_to_jar (srcs , classpath , java_classpath , output ):
@@ -39,9 +57,9 @@ def compile_to_jar(srcs, classpath, java_classpath, output):
39
57
40
58
compile_to_dir (srcs , classpath , java_classpath , builddir )
41
59
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' ])
45
63
finally :
46
64
if os .path .exists (builddir ):
47
65
shutil .rmtree (builddir )
@@ -51,34 +69,35 @@ def find_sources(path):
51
69
return glob .glob (path + '/**/*.kt' , recursive = True ) + glob .glob (path + '/**/*.java' , recursive = True )
52
70
53
71
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' ])
65
74
output = x .stderr .decode (encoding = 'UTF-8' , errors = 'strict' )
66
75
m = re .match (
67
76
r'.*\nlogging: using Kotlin home directory ([^\n]+)\n.*' , output )
68
77
if m is None :
69
78
raise Exception ('Cannot determine kotlinc home directory' )
70
79
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
+
74
83
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'
76
94
77
95
78
96
def find_jar (path , pattern ):
79
97
result = glob .glob (path + '/' + pattern + '*.jar' )
80
98
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 ))
82
101
return result
83
102
84
103
@@ -89,42 +108,61 @@ def patterns_to_classpath(path, patterns):
89
108
return ':' .join (result )
90
109
91
110
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
+
101
121
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 )
107
125
108
126
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 )
124
141
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 )
127
164
128
165
129
- compile_standalone ()
130
- compile_embeddable ()
166
+ for version in kotlin_plugin_versions .versions :
167
+ compile_standalone (version )
168
+ compile_embeddable (version )
0 commit comments