swr: [rasterizer] Cleanup naming of codegen files
[mesa.git] / src / gallium / drivers / swr / rasterizer / codegen / gen_backends.py
1 # Copyright (C) 2017 Intel Corporation. All Rights Reserved.
2 #
3 # Permission is hereby granted, free of charge, to any person obtaining a
4 # copy of this software and associated documentation files (the "Software"),
5 # to deal in the Software without restriction, including without limitation
6 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 # and/or sell copies of the Software, and to permit persons to whom the
8 # Software is furnished to do so, subject to the following conditions:
9 #
10 # The above copyright notice and this permission notice (including the next
11 # paragraph) shall be included in all copies or substantial portions of the
12 # Software.
13 #
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 # IN THE SOFTWARE.
21
22 # Python source
23 # Compatible with Python2.X and Python3.X
24
25 from __future__ import print_function
26 import itertools
27 import math
28 import argparse
29 import os
30 import sys
31 from mako.template import Template
32 from mako.exceptions import RichTraceback
33
34 def write_template_to_string(template_filename, **kwargs):
35 try:
36 template = Template(filename=os.path.abspath(template_filename))
37 # Split + Join fixes line-endings for whatever platform you are using
38 return '\n'.join(template.render(**kwargs).splitlines())
39 except:
40 traceback = RichTraceback()
41 for (filename, lineno, function, line) in traceback.traceback:
42 print("File %s, line %s, in %s" % (filename, lineno, function))
43 print(line, "\n")
44 print("%s: %s" % (str(traceback.error.__class__.__name__), traceback.error))
45
46 def write_template_to_file(template_filename, output_filename, **kwargs):
47 output_dirname = os.path.dirname(output_filename)
48 if not os.path.exists(output_dirname):
49 os.makedirs(output_dirname)
50 with open(output_filename, "w") as outfile:
51 print(write_template_to_string(template_filename, **kwargs), file=outfile)
52
53
54 def main(args=sys.argv[1:]):
55 thisDir = os.path.dirname(os.path.realpath(__file__))
56 parser = argparse.ArgumentParser("Generate files and initialization functions for all permutuations of BackendPixelRate.")
57 parser.add_argument('--dim', help="gBackendPixelRateTable array dimensions", nargs='+', type=int, required=True)
58 parser.add_argument('--outdir', help="output directory", nargs='?', type=str, default=thisDir)
59 parser.add_argument('--split', help="how many lines of initialization per file [0=no split]", nargs='?', type=int, default='512')
60 parser.add_argument('--cpp', help="Generate cpp file(s)", action='store_true', default=False)
61 parser.add_argument('--cmake', help="Generate cmake file", action='store_true', default=False)
62
63
64 args = parser.parse_args(args);
65
66 output_list = []
67 for x in args.dim:
68 output_list.append(list(range(x)))
69
70 # generate all permutations possible for template paremeter inputs
71 output_combinations = list(itertools.product(*output_list))
72 output_list = []
73
74 # for each permutation
75 for x in range(len(output_combinations)):
76 # separate each template peram into its own list member
77 new_list = [output_combinations[x][i] for i in range(len(output_combinations[x]))]
78 tempStr = 'gBackendPixelRateTable'
79 #print each list member as an index in the multidimensional array
80 for i in new_list:
81 tempStr += '[' + str(i) + ']'
82 #map each entry in the permuation as its own string member, store as the template instantiation string
83 tempStr += " = BackendPixelRate<SwrBackendTraits<" + ','.join(map(str, output_combinations[x])) + '>>;'
84 #append the line of c++ code in the list of output lines
85 output_list.append(tempStr)
86
87 # how many files should we split the global template initialization into?
88 if (args.split == 0):
89 numFiles = 1
90 else:
91 numFiles = (len(output_list) + args.split - 1) // args.split
92 linesPerFile = (len(output_list) + numFiles - 1) // numFiles
93 chunkedList = [output_list[x:x+linesPerFile] for x in range(0, len(output_list), linesPerFile)]
94
95 # generate .cpp files
96 if args.cpp:
97 baseCppName = os.path.join(args.outdir, 'gen_BackendPixelRate%s.cpp')
98 templateCpp = os.path.join(thisDir, 'templates', 'gen_backend.cpp')
99
100 for fileNum in range(numFiles):
101 filename = baseCppName % str(fileNum)
102 #print('Generating', filename)
103 write_template_to_file(
104 templateCpp,
105 baseCppName % str(fileNum),
106 cmdline=sys.argv,
107 fileNum=fileNum,
108 funcList=chunkedList[fileNum])
109
110 # generate gen_backend.cmake file
111 if args.cmake:
112 templateCmake = os.path.join(thisDir, 'templates', 'gen_backend.cmake')
113 cmakeFile = os.path.join(args.outdir, 'gen_backends.cmake')
114 #print('Generating', cmakeFile)
115 write_template_to_file(
116 templateCmake,
117 cmakeFile,
118 cmdline=sys.argv,
119 numFiles=numFiles,
120 baseCppName='${RASTY_GEN_SRC_DIR}/backends/' + os.path.basename(baseCppName))
121
122 #print("Generated %d template instantiations in %d files" % (len(output_list), numFiles))
123
124 return 0
125
126 if __name__ == '__main__':
127 sys.exit(main())