diff --git a/src/finn/core/utils.py b/src/finn/core/utils.py index 6c7a1c82a6b9ae16c520b74f072ee2bdf69b81e1..13cf2e77c94fb803b04ba71adffadc9fc4c46237 100644 --- a/src/finn/core/utils.py +++ b/src/finn/core/utils.py @@ -193,3 +193,66 @@ def gen_finn_dt_tensor(finn_dt, tensor_shape): ) # always use float type as container return tensor_values.astype(np.float32) + +class CallCppCompiler(): + + def __init__(self): + self.include_paths = [] + self.cpp_files = [] + self.executable_path = "" + self.code_gen_dir = "" + self.compile_components = [] + self.compile_script = "" + + def append_includes(self, library_path): + self.include_paths.append(library_path) + + def prepare_cpp_files(self, node): + if not self.code_gen_dir: + raise ValueError( + """There is no generated code to compile + for node of op type {}""".format( + node.op_type + ) + ) + else: + self.cpp_files.append(str(self.code_gen_dir) + "/execute_" + str(node.op_type) + ".cpp") + for lib in self.include_paths: + if "cnpy" in lib: + self.cpp_files.append("/workspace/cnpy/cnpy.cpp") + self.append_includes("-lz") + + def set_executable_path(self, node): + if not self.code_gen_dir: + raise ValueError( + """There is no generated code to compile + for node of op type {}""".format( + node.op_type + ) + ) + else: + self.executable_path = str(self.code_gen_dir) + "/execute_" + str(node.op_type) + + def build(self, node): + # raise error if includes are empty + self.code_gen_dir = (get_by_name(node.attribute, "code_gen_dir")).s.decode("UTF-8") + self.prepare_cpp_files(node) + self.set_executable_path(node) + self.compile_components.append("g++ -o " + str(self.executable_path)) + for cpp_file in self.cpp_files: + self.compile_components.append(cpp_file) + for lib in self.include_paths: + self.compile_components.append(lib) + + bash_compile = "" + + for component in self.compile_components: + bash_compile += str(component) + " " + + self.compile_script = str(self.code_gen_dir)+"/compile.sh" + f = open(self.compile_script,"w") + f.write("#!/bin/sh \n") + f.write(bash_compile) + f.close() + +