|
1 | | -#!/usr/bin/python |
| 1 | +#!/usr/bin/env python3 |
2 | 2 |
|
3 | 3 | import glob |
4 | 4 | import re |
5 | 5 | import subprocess |
| 6 | +import shutil |
6 | 7 |
|
7 | 8 | kotlinc = 'kotlinc' |
8 | | -srcs = glob.glob('src/**/*.kt', recursive=True) |
9 | | -jars = ['kotlin-compiler'] |
10 | | - |
11 | | -x = subprocess.run([kotlinc, '-version', '-verbose'], check=True, capture_output=True) |
12 | | -output = x.stderr.decode(encoding = 'UTF-8',errors = 'strict') |
13 | | -m = re.match(r'.*\nlogging: using Kotlin home directory ([^\n]+)\n.*', output) |
14 | | -if m is None: |
15 | | - raise Exception('Cannot determine kotlinc home directory') |
16 | | -kotlin_home = m.group(1) |
17 | | -kotlin_lib = kotlin_home + '/lib' |
18 | | -classpath = ':'.join(map(lambda j: kotlin_lib + '/' + j + '.jar', jars)) |
19 | | - |
20 | | -subprocess.run([kotlinc, |
21 | | - '-d', 'codeql-extractor-kotlin.jar', |
22 | | - '-module-name', 'codeql-kotlin-extractor', |
23 | | - '-no-reflect', |
24 | | - '-jvm-target', '1.8', |
25 | | - '-classpath', classpath] + srcs , check=True) |
26 | | -subprocess.run(['jar', '-u', '-f', 'codeql-extractor-kotlin.jar', '-C', 'src/main/resources', 'META-INF'], check=True) |
27 | 9 |
|
| 10 | + |
| 11 | +def compile(srcs, classpath, output): |
| 12 | + subprocess.run([kotlinc, |
| 13 | + '-d', output, |
| 14 | + '-module-name', 'codeql-kotlin-extractor', |
| 15 | + '-no-reflect', |
| 16 | + '-jvm-target', '1.8', |
| 17 | + '-classpath', classpath] + srcs, check=True) |
| 18 | + subprocess.run(['jar', '-u', '-f', output, |
| 19 | + '-C', 'src/main/resources', 'META-INF'], check=True) |
| 20 | + |
| 21 | + |
| 22 | +def compile_standalone(): |
| 23 | + srcs = glob.glob('src/**/*.kt', recursive=True) |
| 24 | + jars = ['kotlin-compiler'] |
| 25 | + |
| 26 | + x = subprocess.run([kotlinc, '-version', '-verbose'], |
| 27 | + check=True, capture_output=True) |
| 28 | + output = x.stderr.decode(encoding='UTF-8', errors='strict') |
| 29 | + m = re.match( |
| 30 | + r'.*\nlogging: using Kotlin home directory ([^\n]+)\n.*', output) |
| 31 | + if m is None: |
| 32 | + raise Exception('Cannot determine kotlinc home directory') |
| 33 | + kotlin_home = m.group(1) |
| 34 | + kotlin_lib = kotlin_home + '/lib' |
| 35 | + classpath = ':'.join(map(lambda j: kotlin_lib + '/' + j + '.jar', jars)) |
| 36 | + |
| 37 | + compile(srcs, classpath, 'codeql-extractor-kotlin-standalone.jar') |
| 38 | + |
| 39 | + |
| 40 | +def compile_embeddable(): |
| 41 | + x = subprocess.run(['gradle', 'getHomeDir'], |
| 42 | + check=True, capture_output=True) |
| 43 | + output = x.stdout.decode(encoding='UTF-8', errors='strict') |
| 44 | + m = re.match( |
| 45 | + r'.*\n> Task :getHomeDir\n([^\n]+)\n.*', output) |
| 46 | + if m is None: |
| 47 | + raise Exception('Cannot determine gradle home directory') |
| 48 | + gradle_home = m.group(1) |
| 49 | + |
| 50 | + gradle_lib = gradle_home + '/lib' |
| 51 | + jar_patterns = ['kotlin-compiler-embeddable'] |
| 52 | + jar_files = [] |
| 53 | + for pattern in jar_patterns: |
| 54 | + jar_files += glob.glob(gradle_lib + '/' + pattern + '*.jar') |
| 55 | + if len(jar_files) == 0: |
| 56 | + raise Exception('Cannot find gradle jar files') |
| 57 | + classpath = ':'.join(jar_files) |
| 58 | + |
| 59 | + try: |
| 60 | + shutil.copytree('src', 'build/temp_src') |
| 61 | + srcs = glob.glob('build/temp_src/**/*.kt', recursive=True) |
| 62 | + |
| 63 | + # replace imports in files: |
| 64 | + for src in srcs: |
| 65 | + with open(src, 'r') as f: |
| 66 | + content = f.read() |
| 67 | + content = content.replace('import com.intellij', |
| 68 | + 'import org.jetbrains.kotlin.com.intellij') |
| 69 | + with open(src, 'w') as f: |
| 70 | + f.write(content) |
| 71 | + |
| 72 | + compile(srcs, classpath, 'codeql-extractor-kotlin-embeddable.jar') |
| 73 | + finally: |
| 74 | + shutil.rmtree('build/temp_src') |
| 75 | + |
| 76 | + |
| 77 | +compile_standalone() |
| 78 | +compile_embeddable() |
0 commit comments