24
24
from .transformations .ConstantPropagationTransform import ConstantPropagationTransform
25
25
from .transformations .PythonASTTransform import PythonASTTransform
26
26
27
+ from collections import namedtuple
28
+
27
29
# These templates are all formatted, so double up curly braces.
28
30
source_header_header = """#pragma once
29
31
#if _MSC_VER && !__INTEL_COMPILER
48
50
49
51
source_header_test_footer = """}}"""
50
52
53
+ TemplateData = namedtuple ('TemplateData' , ['source' , 'destination' ])
54
+
51
55
52
56
class Compiler (object ):
53
57
def __init__ (self , in_schema : pa .Schema , out_schema : pa .Schema ):
@@ -75,7 +79,7 @@ def __call__(self, query_str: str, output_dir: Path = Path('.'), query_name: str
75
79
include_build_system : bool = True , include_test_system : bool = True ,
76
80
extra_include_dirs : List [PurePath ] = '' ,
77
81
extra_link_dirs : List [PurePath ] = '' ,
78
- extra_link_libraries : List [str ] = '' , part_name : str = 'xa7a12tcsg325-1q' ):
82
+ extra_link_libraries : List [str ] = '' , part_name : str = settings . PART_NAME ):
79
83
assert isinstance (query_str , str )
80
84
81
85
queries = self .parse (query_str )
@@ -100,6 +104,7 @@ def __call__(self, query_str: str, output_dir: Path = Path('.'), query_name: str
100
104
out_str_columns = [x .name for x in self .out_schema if x .type == pa .string ()]
101
105
102
106
template_data = {
107
+ 'VAR_LENGTH' : settings .VAR_LENGTH ,
103
108
'query_name' : query_name ,
104
109
'generated_files' : " " .join (generated_files ),
105
110
'extra_include_dirs' : ' ' .join ([d .as_posix () for d in extra_include_dirs ]),
@@ -113,32 +118,37 @@ def __call__(self, query_str: str, output_dir: Path = Path('.'), query_name: str
113
118
'out_columns_tb_new' : "\n " .join ([tb_new .format (x , settings .VAR_LENGTH + 1 ) for x in out_str_columns ]),
114
119
}
115
120
116
- if include_build_system :
117
- self .copy_files (self .template_path , output_dir .resolve (),
118
- [Path ('fletcherfiltering.cpp' ), Path ('fletcherfiltering.h' )])
121
+ build_system_files = [
122
+ TemplateData (self .template_path / Path ('template.fletcherfiltering.cpp' ),
123
+ output_dir / Path ('fletcherfiltering.cpp' )),
124
+ TemplateData (self .template_path / Path ('template.fletcherfiltering.h' ),
125
+ output_dir / Path ('fletcherfiltering.h' )),
126
+ TemplateData (self .template_path / Path ('template.CMakeLists.txt' ),
127
+ output_dir / Path ('CMakeLists.txt' )),
128
+ ]
129
+
130
+ test_system_files = [
131
+ TemplateData (self .template_path / Path ('template.run_complete_hls.tcl' ),
132
+ output_dir / Path ('run_complete_hls.tcl' )),
133
+ TemplateData (self .template_path / Path ('template.testbench.cpp' ),
134
+ output_dir / Path ('{0}{1}.cpp' .format (query_name , settings .TESTBENCH_SUFFIX ))),
135
+ TemplateData (self .template_path / Path ('template.data.h' ),
136
+ output_dir / Path ('{0}{1}.h' .format (query_name , settings .DATA_SUFFIX ))),
137
+ ]
119
138
120
- with open (self .template_path / 'template.CMakeLists.txt' , 'r' ) as template_file :
121
- cmake_list = string .Template (template_file .read ())
122
- with open (output_dir / Path ('CMakeLists.txt' ), 'w' ) as cmake_file :
123
- cmake_file .write (cmake_list .safe_substitute (template_data ))
139
+ if include_build_system :
140
+ for file in build_system_files :
141
+ with open (file .source , 'r' ) as template_file :
142
+ output_data = string .Template (template_file .read ())
143
+ with open (file .destination , 'w' ) as output_file :
144
+ output_file .write (output_data .safe_substitute (template_data ))
124
145
125
146
if include_test_system :
126
- with open (self .template_path / 'template.run_complete_hls.tcl' , 'r' ) as template_file :
127
- hls_tcl = string .Template (template_file .read ())
128
- with open (output_dir / Path ('run_complete_hls.tcl' ), 'w' ) as hls_tcl_file :
129
- hls_tcl_file .write (hls_tcl .safe_substitute (template_data ))
130
-
131
- with open (self .template_path / 'template.testbench.cpp' , 'r' ) as template_file :
132
- teshbench_cpp = string .Template (template_file .read ())
133
- with open (output_dir / Path ('{0}{1}.cpp' .format (query_name , settings .TESTBENCH_SUFFIX )),
134
- 'w' ) as teshbench_cpp_file :
135
- teshbench_cpp_file .write (teshbench_cpp .safe_substitute (template_data ))
136
-
137
- with open (self .template_path / 'template.data.h' , 'r' ) as template_file :
138
- data_cpp = string .Template (template_file .read ())
139
- with open (output_dir / Path ('{0}{1}.h' .format (query_name , settings .DATA_SUFFIX )),
140
- 'w' ) as data_cpp_file :
141
- data_cpp_file .write (data_cpp .safe_substitute (template_data ))
147
+ for file in test_system_files :
148
+ with open (file .source , 'r' ) as template_file :
149
+ output_data = string .Template (template_file .read ())
150
+ with open (file .destination , 'w' ) as output_file :
151
+ output_file .write (output_data .safe_substitute (template_data ))
142
152
143
153
def copy_files (self , source_dir : PurePath , output_dir : PurePath , file_list : List [Path ]):
144
154
if source_dir == output_dir :
0 commit comments