forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathgenerate_harness.py
executable file
·57 lines (47 loc) · 1.95 KB
/
generate_harness.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python3
# ===--- generate_harness.py ---------------------------------------------===//
#
# This source file is part of the Swift.org open source project
#
# Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
# Licensed under Apache License v2.0 with Runtime Library Exception
#
# See https://swift.org/LICENSE.txt for license information
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
#
# ===---------------------------------------------------------------------===//
# Generate boilerplate, CMakeLists.txt and utils/main.swift from templates.
import argparse
import os
import subprocess
script_dir = os.path.dirname(os.path.realpath(__file__))
perf_dir = os.path.realpath(os.path.join(script_dir, "../.."))
gyb = os.path.realpath(os.path.join(perf_dir, "../utils/gyb"))
parser = argparse.ArgumentParser()
parser.add_argument(
"--output-dir", help="Output directory (for validation test)", default=perf_dir
)
args = parser.parse_args()
output_dir = args.output_dir
def all_files(directory, extension): # matching: [directory]/**/*[extension]
return [
os.path.join(root, f)
for root, _, files in os.walk(directory)
for f in files
if f.endswith(extension)
]
def will_write(filename): # ensure path to file exists before writing
print(filename)
output_path = os.path.split(filename)[0]
if not os.path.exists(output_path):
os.makedirs(output_path)
if __name__ == "__main__":
# Generate Your Boilerplate
# Make sure longer paths are done first as CMakeLists.txt and main.swift
# depend on the other gybs being generated first.
gyb_files = sorted(all_files(perf_dir, ".gyb"), key=len, reverse=True)
for f in gyb_files:
relative_path = os.path.relpath(f[:-4], perf_dir)
out_file = os.path.join(output_dir, relative_path)
will_write(out_file)
subprocess.call([gyb, "--line-directive", "", "-o", out_file, f])