Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3939a2a

Browse files
tamasvajkigfoo
authored andcommitted
Change build script to build multiple versions of the plugin
1 parent 22c87b2 commit 3939a2a

2 files changed

Lines changed: 95 additions & 64 deletions

File tree

java/kotlin-extractor/build.py

Lines changed: 94 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,53 @@ 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):
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+
srcs = find_sources(tmp_dir)
131+
132+
transform_to_embeddable(srcs)
133+
134+
compile_to_jar(srcs, classpath, java_classpath, output)
124135
finally:
125-
if os.path.exists('build/temp_src'):
126-
shutil.rmtree('build/temp_src')
136+
if os.path.exists(tmp_dir):
137+
shutil.rmtree(tmp_dir)
138+
139+
140+
def compile_embeddable(version):
141+
compile(['kotlin-stdlib-' + version, 'kotlin-compiler-embeddable-' + version],
142+
['kotlin-stdlib-' + version],
143+
kotlin_dependency_folder,
144+
transform_to_embeddable,
145+
'codeql-extractor-kotlin-embeddable-%s.jar' % (version),
146+
'build/temp_src')
147+
148+
149+
def compile_standalone(version):
150+
compile(['kotlin-stdlib-' + version, 'kotlin-compiler-' + version],
151+
['kotlin-stdlib-' + version],
152+
kotlin_dependency_folder,
153+
lambda srcs: None,
154+
'codeql-extractor-kotlin-standalone-%s.jar' % (version),
155+
'build/temp_src')
127156

128157

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

0 commit comments

Comments
 (0)