Skip to content

Instantly share code, notes, and snippets.

@udalov
Last active September 12, 2019 14:34
Show Gist options
  • Save udalov/9e82bdcd70646017fe75e1433e44ec7c to your computer and use it in GitHub Desktop.
Save udalov/9e82bdcd70646017fe75e1433e44ec7c to your computer and use it in GitHub Desktop.
Test compilation of the Kotlin project with JVM IR
#!/usr/bin/env python3
import atexit, os, re, shutil, subprocess, sys, time
from pathlib import Path
def to_str(buf):
return "".join(map(chr, buf))
def dump_stdout_stderr(f, result):
f.write("=== STDOUT ===\n\n")
f.write(to_str(result.stdout))
f.write("\n\n=== STDERR ===\n\n")
f.write(to_str(result.stderr))
def write_use_ir_flag(useIR):
with override_file_path.open("w") as f:
f.write(useIR)
def remove_override_file():
if override_file_path.exists():
override_file_path.unlink()
def run_successfully(cmd):
result = subprocess.run(cmd, capture_output=True)
if result.returncode != 0:
print("Failed to run: " + " ".join(cmd))
dump_stdout_stderr(sys.stdout, result)
sys.exit(1)
return to_str(result.stdout)
def get_tasks():
task_list = run_successfully(["./gradlew", "tasks", "--all"])
task_regex = re.compile("[^>].*compile\w*Kotlin\\b.*")
tasks = list(map(lambda line: line.split(" ")[0], filter(task_regex.match, task_list.split("\n"))))
tasks.remove("kotlin-compiler-client-embeddable:compileTestKotlin") # This task fails even without JVM IR.
print(str(len(tasks)) + " tasks found.")
print("Running build...")
tasks_output = run_successfully(["./gradlew"] + tasks)
no_source_tasks = [ line.split(" ")[2][1:] for line in tasks_output.split("\n") if line.endswith("NO-SOURCE") ]
tasks = [ task for task in tasks if task not in no_source_tasks and not task.endswith("compileJava9Kotlin") ] # There are no Kotlin sources in java9 source roots.
print("Without NO-SOURCE: " + str(len(tasks)) + " total tasks.")
return tasks
def process_tasks(tasks):
for i, task in enumerate(tasks):
sep = task.rfind(":") + 1
clean_task = task[:sep] + "clean" + task[sep].capitalize() + task[sep + 1:]
run_successfully(["./gradlew", clean_task])
write_use_ir_flag("true")
start_time = time.time()
result = subprocess.run(["./gradlew", task], capture_output=True)
end_time = time.time()
status = "OK"
if result.returncode != 0:
status = "ERR"
log_path = log_dir_path / (task.replace(":", "_") + ".log")
with log_path.open("w") as f:
dump_stdout_stderr(f, result)
print(i, status, task, "%.3f" % (end_time - start_time), end="")
sys.stdout.flush()
run_successfully(["./gradlew", clean_task])
write_use_ir_flag("false")
start_time = time.time()
run_successfully(["./gradlew", task])
end_time = time.time()
print("", "%.3f" % (end_time - start_time))
log_dir_path = Path("bootstrap-logs")
override_file_path = Path.home() / "kotlin-jvm-ir.txt"
if log_dir_path.is_dir():
shutil.rmtree(log_dir_path)
log_dir_path.mkdir()
atexit.register(remove_override_file)
tasks = get_tasks()
with (log_dir_path / "tasks.txt").open("w") as f:
f.write("\n".join(tasks))
total_start_time = time.time()
process_tasks(tasks)
total_end_time = time.time()
print("Total time: %.3f." % (total_end_time - total_start_time))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment