12
12
#
13
13
# ===---------------------------------------------------------------------===//
14
14
15
- # Generate CMakeLists.txt and utils/main.swift from templates.
15
+ # Generate boilerplate, CMakeLists.txt and utils/main.swift from templates.
16
16
17
17
from __future__ import print_function
18
18
19
- import glob
19
+ import argparse
20
+ import jinja2
20
21
import os
21
22
import re
22
23
import subprocess
23
24
24
- import jinja2
25
-
26
25
script_dir = os .path .dirname (os .path .realpath (__file__ ))
27
26
perf_dir = os .path .realpath (os .path .join (script_dir , '../..' ))
27
+ gyb_script = os .path .realpath (os .path .join (perf_dir , '../utils/gyb' ))
28
28
single_source_dir = os .path .join (perf_dir , 'single-source' )
29
29
multi_source_dir = os .path .join (perf_dir , 'multi-source' )
30
+ parser = argparse .ArgumentParser ()
31
+ parser .add_argument ("--output-dir" ,
32
+ help = "Output directory (for validation test)" ,
33
+ default = perf_dir )
34
+ args = parser .parse_args ()
35
+ output_dir = args .output_dir
30
36
31
37
template_map = {
32
- 'CMakeLists.txt_template' : os .path .join (perf_dir , 'CMakeLists.txt' ),
33
- 'main.swift_template' : os .path .join (perf_dir , 'utils/main.swift' )
38
+ 'CMakeLists.txt_template' : os .path .join (output_dir , 'CMakeLists.txt' ),
39
+ 'main.swift_template' : os .path .join (output_dir , 'utils/main.swift' )
34
40
}
35
41
ignored_run_funcs = ["Ackermann" , "Fibonacci" ]
36
42
37
43
template_loader = jinja2 .FileSystemLoader (searchpath = "/" )
38
44
template_env = jinja2 .Environment (loader = template_loader , trim_blocks = True ,
39
45
lstrip_blocks = True )
40
46
47
+ def all_files (directory , extension ): # matching: [directory]/**/*[extension]
48
+ return [
49
+ os .path .join (root , f )
50
+ for root , _ , files in os .walk (directory )
51
+ for f in files if f .endswith (extension )
52
+ ]
53
+
54
+ def will_write (filename ): # ensure path to file exists before writing
55
+ print (filename )
56
+ output_path = os .path .split (filename )[0 ]
57
+ if not os .path .exists (output_path ): os .makedirs (output_path )
58
+
41
59
if __name__ == '__main__' :
42
- # Generate Your Boilerplate single-source
43
- gyb_files = glob .glob (os .path .join (single_source_dir , '*.swift.gyb' ))
44
- gyb_script = os .path .realpath (os .path .join (perf_dir , '../utils/gyb' ))
60
+ # Generate Your Boilerplate
61
+ gyb_files = all_files (perf_dir , '.swift.gyb' )
45
62
for f in gyb_files :
46
- print (f [:- 4 ])
47
- subprocess .call ([gyb_script , '--line-directive' , '' , '-o' , f [:- 4 ], f ])
63
+ relative_path = os .path .relpath (f [:- 4 ], perf_dir )
64
+ out_file = os .path .join (output_dir , relative_path )
65
+ will_write (out_file )
66
+ subprocess .call ([gyb_script , '--line-directive' , '' , '-o' , out_file , f ])
48
67
49
68
# CMakeList single-source
50
- test_files = glob . glob ( os . path . join ( single_source_dir , '* .swift') )
69
+ test_files = all_files ( single_source_dir ,' .swift' )
51
70
tests = sorted (os .path .basename (x ).split ('.' )[0 ] for x in test_files )
52
71
53
72
# CMakeList multi-source
54
73
class MultiSourceBench (object ):
55
-
56
74
def __init__ (self , path ):
57
75
self .name = os .path .basename (path )
58
76
self .files = [x for x in os .listdir (path )
59
77
if x .endswith ('.swift' )]
78
+
60
79
if os .path .isdir (multi_source_dir ):
61
80
multisource_benches = [
62
81
MultiSourceBench (os .path .join (multi_source_dir , x ))
@@ -75,26 +94,19 @@ def get_run_funcs(filepath):
75
94
matches = re .findall (r'func run_(.*?)\(' , content )
76
95
return filter (lambda x : x not in ignored_run_funcs , matches )
77
96
78
- def find_run_funcs (dirs ):
79
- ret_run_funcs = []
80
- for d in dirs :
81
- for root , _ , files in os .walk (d ):
82
- for name in filter (lambda x : x .endswith ('.swift' ), files ):
83
- run_funcs = get_run_funcs (os .path .join (root , name ))
84
- ret_run_funcs .extend (run_funcs )
85
- return ret_run_funcs
86
- run_funcs = sorted (
87
- [(x , x )
88
- for x in find_run_funcs ([single_source_dir , multi_source_dir ])],
89
- key = lambda x : x [0 ]
90
- )
97
+ def find_run_funcs ():
98
+ swift_files = all_files (perf_dir , '.swift' )
99
+ return [func for f in swift_files for func in get_run_funcs (f )]
100
+
101
+ run_funcs = [(f , f ) for f in sorted (find_run_funcs ())]
91
102
92
103
# Replace originals with files generated from templates
93
104
for template_file in template_map :
94
105
template_path = os .path .join (script_dir , template_file )
95
106
template = template_env .get_template (template_path )
96
- print (template_map [template_file ])
97
- open (template_map [template_file ], 'w' ).write (
107
+ out_file = template_map [template_file ]
108
+ will_write (out_file )
109
+ open (out_file , 'w' ).write (
98
110
template .render (tests = tests ,
99
111
multisource_benches = multisource_benches ,
100
112
imports = imports ,
0 commit comments