OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TableFunctionsFactory_linker.py
Go to the documentation of this file.
1 import os
2 import abc
3 import sys
4 import math
5 import textwrap
6 
7 
9  if not os.path.exists(filepath):
10  return ""
11 
12  with open(filepath, "r") as f:
13  return "".join(f.readlines())
14 
15 
16 class BaseGenerateFiles(object):
17 
18  CHUNK_SIZE = 200
19  _generated_header_files = []
20 
21  def __init__(self, dirname, kind):
22  self.dirname = dirname
23  self.kind = kind
24 
25  @classmethod
26  def file_counter(cls):
27  return cls._file_counter
28 
29  @classmethod
31  return self._generated_header_files
32 
33  @classmethod
35  cls._file_counter += 1
36 
37  @abc.abstractmethod
39  raise NotImplementedError()
40 
41  @abc.abstractmethod
42  def get_filename(self, prefix, kind, fileno, ext):
43  raise NotImplementedError()
44 
45  @classmethod
47  return cls.file_counter()
48 
49  def _get_filename(self, ext):
50  fileno = self.file_counter()
51  filename = self.get_filename(self.filename_prefix, self.kind, fileno, ext)
52  return os.path.join(self.dirname, filename)
53 
54  def _generate_files(self, chunk):
56  fileno = self.file_counter()
57  cpp_filename = self._get_filename(ext="cpp")
58  hpp_filename = self._get_filename(ext="h")
59 
60  BaseGenerateFiles._generated_header_files.append(hpp_filename)
61 
62  cpp_content = self._generate_cpp_content(chunk, hpp_filename, fileno)
63  hpp_content = self._generate_hpp_content(chunk, fileno)
64 
65  cpp_ec = get_existing_file_content(cpp_filename)
66  hpp_ec = get_existing_file_content(hpp_filename)
67 
68  if cpp_content == cpp_ec and hpp_content == hpp_ec:
69  return
70 
71  with open(cpp_filename, "w") as f:
72  f.write(cpp_content)
73 
74  with open(hpp_filename, "w") as f:
75  f.write(hpp_content)
76 
78  stmts = self.stmts
79  L = len(stmts)
80  chunks = []
81  for i in range(0, L, self.CHUNK_SIZE):
82  chunks.append(stmts[i : i + self.CHUNK_SIZE])
83  return chunks
84 
85  def generate_files(self):
86  chunks = self._split_stmts_into_chunks()
87  for chunk in chunks:
88  self._generate_files(chunk)
89 
90 
92 
93  _file_counter = -1
94 
95  def __init__(self, dirname, stmts, header_file, kind):
96  assert kind in ("cpu", "gpu")
97  self.stmts = stmts
98  self.header_file = header_file
99  super(
100  GenerateTemplateFiles,
101  self,
102  ).__init__(dirname=dirname, kind=kind)
103 
104  @property
105  def filename_prefix(self):
106  return "TableFunctionsFactory_init"
107 
108  def get_filename(self, prefix, kind, fileno, ext):
109  return "%s_%s_%s.%s" % (prefix, kind, fileno, ext)
110 
111  def _get_decl_from_cpp_functions(self, cpp_functions):
112  decls = []
113  for func in cpp_functions:
114  header = func.split("{", 1)[0].rstrip() + ";"
115  decls.append(header)
116  return decls
117 
118  def _generate_cpp_content(self, stmts, hpp_filename, fileno):
119  content = '''
120  /*
121  This file is generated by %s. Do no edit!
122 
123  */
124  #include "%s"
125 
126  %s
127  '''
128  content = textwrap.dedent(content)
129  funcs_formatted = '\n'.join(stmts)
130  return content % (sys.argv[0], hpp_filename, funcs_formatted)
131 
132  def _generate_hpp_content(self, funcs, fileno):
133  content = '''
134  /*
135  This file is generated by %s. Do no edit!
136  */
137  #include "%s"
138 
139  %s
140  '''
141  content = textwrap.dedent(content)
142 
143  decls = '\n\n'.join(self._get_decl_from_cpp_functions(funcs))
144 
145  return content % (sys.argv[0], self.header_file, decls)
146 
147 
148 
150 
151  _file_counter = -1
152 
153  def __init__(self, dirname, stmts, header_file):
154  self.stmts = stmts
155  self.header_file = header_file
156  super(GenerateAddTableFunctionsFiles, self).__init__(dirname=dirname, kind="")
157 
158  @property
159  def filename_prefix(self):
160  return "TableFunctionsFactory_add"
161 
162  def get_filename(self, prefix, kind, fileno, ext):
163  return "%s_%s.%s" % (prefix, fileno, ext)
164 
166  return len(self.stmts) > 0
167 
168  def _generate_cpp_content(self, chunk, hpp_filename, fileno):
169  content = """
170  /*
171  This file is generated by %s. Do no edit!
172  */
173 
174  #include "QueryEngine/TableFunctions/TableFunctionsFactory.h"
175  #include "%s"
176 
177  namespace table_functions {
178 
179  NO_OPT_ATTRIBUTE void add_table_functions_%d() {
180  %s
181  }
182  };
183 
184  """
185  content = textwrap.dedent(content)
186  chunk = '\n '.join(chunk)
187  return content % (sys.argv[0], hpp_filename, fileno, chunk)
188 
189  def _generate_hpp_content(self, funcs, fileno):
190  content = """
191  /*
192  This file is generated by %s. Do no edit!
193  */
194 
195  #include "QueryEngine/TableFunctions/TableFunctionsFactory.h"
196  #include "%s"
197 
198  #if defined(__clang__)
199  #define NO_OPT_ATTRIBUTE __attribute__((optnone))
200 
201  #elif defined(__GNUC__) || defined(__GNUG__)
202  #define NO_OPT_ATTRIBUTE __attribute((optimize("O0")))
203 
204  #elif defined(_MSC_VER)
205  #define NO_OPT_ATTRIBUTE
206 
207  #endif
208 
209  namespace table_functions {
210  NO_OPT_ATTRIBUTE void add_table_functions_%s();
211  };
212  """
213 
214  content = textwrap.dedent(content)
215  return content % (sys.argv[0], self.header_file, fileno)
std::string join(T const &container, std::string const &delim)
int open(const char *path, int flags, int mode)
Definition: heavyai_fs.cpp:66