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

Skip to content

Commit 3cb68bd

Browse files
smowtonigfoo
authored andcommitted
kotlin-extractor build: include Java source files
1 parent 124dcb0 commit 3cb68bd

1 file changed

Lines changed: 59 additions & 14 deletions

File tree

java/kotlin-extractor/build.py

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,57 @@
55
import subprocess
66
import shutil
77
import os
8+
import os.path
89

910
kotlinc = 'kotlinc'
11+
javac = 'javac'
1012

1113

12-
def compile(srcs, classpath, output):
14+
def compile_to_dir(srcs, classpath, java_classpath, output):
15+
# Use kotlinc to compile .kt files:
1316
subprocess.run([kotlinc,
1417
'-d', output,
1518
'-module-name', 'codeql-kotlin-extractor',
1619
'-no-reflect',
1720
'-jvm-target', '1.8',
1821
'-classpath', classpath] + srcs, check=True)
19-
subprocess.run(['jar', '-u', '-f', output,
20-
'-C', 'src/main/resources', 'META-INF'], check=True)
22+
# Use javac to compile .java files, referencing the Kotlin class files:
23+
subprocess.run([javac,
24+
'-d', output,
25+
'--release', '8',
26+
'-classpath', "%s:%s:%s" % (output, classpath, java_classpath)] + [s for s in srcs if s.endswith(".java")], check=True)
27+
28+
29+
def compile_to_jar(srcs, classpath, java_classpath, output):
30+
builddir = 'build/classes'
31+
32+
try:
33+
if os.path.exists(builddir):
34+
shutil.rmtree(builddir)
35+
os.makedirs(builddir)
36+
37+
compile_to_dir(srcs, classpath, java_classpath, builddir)
38+
39+
subprocess.run(['jar', '-c', '-f', output,
40+
'-C', builddir, '.',
41+
'-C', 'src/main/resources', 'META-INF'], check=True)
42+
finally:
43+
if os.path.exists(builddir):
44+
shutil.rmtree(builddir)
45+
46+
47+
def find_sources(path):
48+
return glob.glob(path + '/**/*.kt', recursive=True) + glob.glob(path + '/**/*.java', recursive=True)
49+
50+
51+
def jarnames_to_classpath(path, jars):
52+
return ":".join(os.path.join(path, jar) + ".jar" for jar in jars)
2153

2254

2355
def compile_standalone():
24-
srcs = glob.glob('src/**/*.kt', recursive=True)
56+
srcs = find_sources("src")
2557
jars = ['kotlin-compiler']
58+
java_jars = ['kotlin-stdlib']
2659

2760
x = subprocess.run([kotlinc, '-version', '-verbose'],
2861
check=True, capture_output=True)
@@ -33,9 +66,24 @@ def compile_standalone():
3366
raise Exception('Cannot determine kotlinc home directory')
3467
kotlin_home = m.group(1)
3568
kotlin_lib = kotlin_home + '/lib'
36-
classpath = ':'.join(map(lambda j: kotlin_lib + '/' + j + '.jar', jars))
69+
classpath = jarnames_to_classpath(kotlin_lib, jars)
70+
java_classpath = jarnames_to_classpath(kotlin_lib, java_jars)
71+
72+
compile_to_jar(srcs, classpath, java_classpath, 'codeql-extractor-kotlin-standalone.jar')
73+
74+
75+
def find_jar(path, pattern):
76+
result = glob.glob(path + '/' + pattern + '*.jar')
77+
if len(result) == 0:
78+
raise Exception('Cannot find jar file %s under path %s' % (pattern, path))
79+
return result
80+
3781

38-
compile(srcs, classpath, 'codeql-extractor-kotlin-standalone.jar')
82+
def patterns_to_classpath(path, patterns):
83+
result = []
84+
for pattern in patterns:
85+
result += find_jar(path, pattern)
86+
return ':'.join(result)
3987

4088

4189
def compile_embeddable():
@@ -50,18 +98,15 @@ def compile_embeddable():
5098

5199
gradle_lib = gradle_home + '/lib'
52100
jar_patterns = ['kotlin-compiler-embeddable']
53-
jar_files = []
54-
for pattern in jar_patterns:
55-
jar_files += glob.glob(gradle_lib + '/' + pattern + '*.jar')
56-
if len(jar_files) == 0:
57-
raise Exception('Cannot find gradle jar files')
58-
classpath = ':'.join(jar_files)
101+
java_jar_patterns = ['kotlin-stdlib']
102+
classpath = patterns_to_classpath(gradle_lib, jar_patterns)
103+
java_classpath = patterns_to_classpath(gradle_lib, java_jar_patterns)
59104

60105
try:
61106
if os.path.exists('build/temp_src'):
62107
shutil.rmtree('build/temp_src')
63108
shutil.copytree('src', 'build/temp_src')
64-
srcs = glob.glob('build/temp_src/**/*.kt', recursive=True)
109+
srcs = find_sources('build/temp_src')
65110

66111
# replace imports in files:
67112
for src in srcs:
@@ -72,7 +117,7 @@ def compile_embeddable():
72117
with open(src, 'w') as f:
73118
f.write(content)
74119

75-
compile(srcs, classpath, 'codeql-extractor-kotlin-embeddable.jar')
120+
compile_to_jar(srcs, classpath, java_classpath, 'codeql-extractor-kotlin-embeddable.jar')
76121
finally:
77122
if os.path.exists('build/temp_src'):
78123
shutil.rmtree('build/temp_src')

0 commit comments

Comments
 (0)