mirror of
https://github.com/github/codeql.git
synced 2026-05-05 13:45:19 +02:00
kotlin-extractor build: include Java source files
This commit is contained in:
committed by
Ian Lynagh
parent
124dcb0e5f
commit
3cb68bd7be
@@ -5,24 +5,57 @@ import re
|
||||
import subprocess
|
||||
import shutil
|
||||
import os
|
||||
import os.path
|
||||
|
||||
kotlinc = 'kotlinc'
|
||||
javac = 'javac'
|
||||
|
||||
|
||||
def compile(srcs, classpath, output):
|
||||
def compile_to_dir(srcs, classpath, java_classpath, output):
|
||||
# Use kotlinc to compile .kt files:
|
||||
subprocess.run([kotlinc,
|
||||
'-d', output,
|
||||
'-module-name', 'codeql-kotlin-extractor',
|
||||
'-no-reflect',
|
||||
'-jvm-target', '1.8',
|
||||
'-classpath', classpath] + srcs, check=True)
|
||||
subprocess.run(['jar', '-u', '-f', output,
|
||||
'-C', 'src/main/resources', 'META-INF'], check=True)
|
||||
# Use javac to compile .java files, referencing the Kotlin class files:
|
||||
subprocess.run([javac,
|
||||
'-d', output,
|
||||
'--release', '8',
|
||||
'-classpath', "%s:%s:%s" % (output, classpath, java_classpath)] + [s for s in srcs if s.endswith(".java")], check=True)
|
||||
|
||||
|
||||
def compile_to_jar(srcs, classpath, java_classpath, output):
|
||||
builddir = 'build/classes'
|
||||
|
||||
try:
|
||||
if os.path.exists(builddir):
|
||||
shutil.rmtree(builddir)
|
||||
os.makedirs(builddir)
|
||||
|
||||
compile_to_dir(srcs, classpath, java_classpath, builddir)
|
||||
|
||||
subprocess.run(['jar', '-c', '-f', output,
|
||||
'-C', builddir, '.',
|
||||
'-C', 'src/main/resources', 'META-INF'], check=True)
|
||||
finally:
|
||||
if os.path.exists(builddir):
|
||||
shutil.rmtree(builddir)
|
||||
|
||||
|
||||
def find_sources(path):
|
||||
return glob.glob(path + '/**/*.kt', recursive=True) + glob.glob(path + '/**/*.java', recursive=True)
|
||||
|
||||
|
||||
def jarnames_to_classpath(path, jars):
|
||||
return ":".join(os.path.join(path, jar) + ".jar" for jar in jars)
|
||||
|
||||
|
||||
def compile_standalone():
|
||||
srcs = glob.glob('src/**/*.kt', recursive=True)
|
||||
srcs = find_sources("src")
|
||||
jars = ['kotlin-compiler']
|
||||
java_jars = ['kotlin-stdlib']
|
||||
|
||||
x = subprocess.run([kotlinc, '-version', '-verbose'],
|
||||
check=True, capture_output=True)
|
||||
@@ -33,9 +66,24 @@ def compile_standalone():
|
||||
raise Exception('Cannot determine kotlinc home directory')
|
||||
kotlin_home = m.group(1)
|
||||
kotlin_lib = kotlin_home + '/lib'
|
||||
classpath = ':'.join(map(lambda j: kotlin_lib + '/' + j + '.jar', jars))
|
||||
classpath = jarnames_to_classpath(kotlin_lib, jars)
|
||||
java_classpath = jarnames_to_classpath(kotlin_lib, java_jars)
|
||||
|
||||
compile(srcs, classpath, 'codeql-extractor-kotlin-standalone.jar')
|
||||
compile_to_jar(srcs, classpath, java_classpath, 'codeql-extractor-kotlin-standalone.jar')
|
||||
|
||||
|
||||
def find_jar(path, pattern):
|
||||
result = glob.glob(path + '/' + pattern + '*.jar')
|
||||
if len(result) == 0:
|
||||
raise Exception('Cannot find jar file %s under path %s' % (pattern, path))
|
||||
return result
|
||||
|
||||
|
||||
def patterns_to_classpath(path, patterns):
|
||||
result = []
|
||||
for pattern in patterns:
|
||||
result += find_jar(path, pattern)
|
||||
return ':'.join(result)
|
||||
|
||||
|
||||
def compile_embeddable():
|
||||
@@ -50,18 +98,15 @@ def compile_embeddable():
|
||||
|
||||
gradle_lib = gradle_home + '/lib'
|
||||
jar_patterns = ['kotlin-compiler-embeddable']
|
||||
jar_files = []
|
||||
for pattern in jar_patterns:
|
||||
jar_files += glob.glob(gradle_lib + '/' + pattern + '*.jar')
|
||||
if len(jar_files) == 0:
|
||||
raise Exception('Cannot find gradle jar files')
|
||||
classpath = ':'.join(jar_files)
|
||||
java_jar_patterns = ['kotlin-stdlib']
|
||||
classpath = patterns_to_classpath(gradle_lib, jar_patterns)
|
||||
java_classpath = patterns_to_classpath(gradle_lib, java_jar_patterns)
|
||||
|
||||
try:
|
||||
if os.path.exists('build/temp_src'):
|
||||
shutil.rmtree('build/temp_src')
|
||||
shutil.copytree('src', 'build/temp_src')
|
||||
srcs = glob.glob('build/temp_src/**/*.kt', recursive=True)
|
||||
srcs = find_sources('build/temp_src')
|
||||
|
||||
# replace imports in files:
|
||||
for src in srcs:
|
||||
@@ -72,7 +117,7 @@ def compile_embeddable():
|
||||
with open(src, 'w') as f:
|
||||
f.write(content)
|
||||
|
||||
compile(srcs, classpath, 'codeql-extractor-kotlin-embeddable.jar')
|
||||
compile_to_jar(srcs, classpath, java_classpath, 'codeql-extractor-kotlin-embeddable.jar')
|
||||
finally:
|
||||
if os.path.exists('build/temp_src'):
|
||||
shutil.rmtree('build/temp_src')
|
||||
|
||||
Reference in New Issue
Block a user