swr/rast: gen_backends.py remove extraneous semicolon
[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 os
28 import sys
29 from gen_common import ArgumentParser, MakoTemplateWriter
30
31
32 def main(args=sys.argv[1:]):
33 thisDir = os.path.dirname(os.path.realpath(__file__))
34 parser = ArgumentParser('Generate files and initialization functions for all permutuations of BackendPixelRate.')
35 parser.add_argument('--dim', help='gBackendPixelRateTable array dimensions', nargs='+', type=int, required=True)
36 parser.add_argument('--outdir', help='output directory', nargs='?', type=str, default=thisDir)
37 parser.add_argument('--split', help='how many lines of initialization per file [0=no split]', nargs='?', type=int, default='512')
38 parser.add_argument('--numfiles', help='how many output files to generate', nargs='?', type=int, default='0')
39 parser.add_argument('--cpp', help='Generate cpp file(s)', action='store_true', default=False)
40 parser.add_argument('--hpp', help='Generate hpp file', action='store_true', default=False)
41 parser.add_argument('--cmake', help='Generate cmake file', action='store_true', default=False)
42
43 args = parser.parse_args(args)
44
45
46 class backendStrs :
47 def __init__(self) :
48 self.outFileName = 'gen_BackendPixelRate%s.cpp'
49 self.outHeaderName = 'gen_BackendPixelRate.hpp'
50 self.functionTableName = 'gBackendPixelRateTable'
51 self.funcInstanceHeader = ' = BackendPixelRate<SwrBackendTraits<'
52 self.template = 'gen_backend.cpp'
53 self.hpp_template = 'gen_header_init.hpp'
54 self.cmakeFileName = 'gen_backends.cmake'
55 self.cmakeSrcVar = 'GEN_BACKEND_SOURCES'
56 self.tableName = 'BackendPixelRate'
57
58 backend = backendStrs()
59
60 output_list = []
61 for x in args.dim:
62 output_list.append(list(range(x)))
63
64 # generate all permutations possible for template parameter inputs
65 output_combinations = list(itertools.product(*output_list))
66 output_list = []
67
68 # for each permutation
69 for x in range(len(output_combinations)):
70 # separate each template peram into its own list member
71 new_list = [output_combinations[x][i] for i in range(len(output_combinations[x]))]
72 tempStr = backend.functionTableName
73 #print each list member as an index in the multidimensional array
74 for i in new_list:
75 tempStr += '[' + str(i) + ']'
76 #map each entry in the permuation as its own string member, store as the template instantiation string
77 tempStr += backend.funcInstanceHeader + ','.join(map(str, output_combinations[x])) + '>>;'
78 #append the line of c++ code in the list of output lines
79 output_list.append(tempStr)
80
81 # how many files should we split the global template initialization into?
82 if (args.split == 0):
83 numFiles = 1
84 else:
85 numFiles = (len(output_list) + args.split - 1) // args.split
86 if (args.numfiles != 0):
87 numFiles = args.numfiles
88 linesPerFile = (len(output_list) + numFiles - 1) // numFiles
89 chunkedList = [output_list[x:x+linesPerFile] for x in range(0, len(output_list), linesPerFile)]
90
91 # generate .cpp files
92 if args.cpp:
93 baseCppName = os.path.join(args.outdir, backend.outFileName)
94 templateCpp = os.path.join(thisDir, 'templates', backend.template)
95
96 for fileNum in range(numFiles):
97 filename = baseCppName % str(fileNum)
98 MakoTemplateWriter.to_file(
99 templateCpp,
100 baseCppName % str(fileNum),
101 cmdline=sys.argv,
102 fileNum=fileNum,
103 funcList=chunkedList[fileNum])
104
105 if args.hpp:
106 baseHppName = os.path.join(args.outdir, backend.outHeaderName)
107 templateHpp = os.path.join(thisDir, 'templates', backend.hpp_template)
108
109 MakoTemplateWriter.to_file(
110 templateHpp,
111 baseHppName,
112 cmdline=sys.argv,
113 numFiles=numFiles,
114 filename=backend.outHeaderName,
115 tableName=backend.tableName)
116
117 # generate gen_backend.cmake file
118 if args.cmake:
119 templateCmake = os.path.join(thisDir, 'templates', 'gen_backend.cmake')
120 cmakeFile = os.path.join(args.outdir, backend.cmakeFileName)
121
122 MakoTemplateWriter.to_file(
123 templateCmake,
124 cmakeFile,
125 cmdline=sys.argv,
126 srcVar=backend.cmakeSrcVar,
127 numFiles=numFiles,
128 baseCppName='${RASTY_GEN_SRC_DIR}/backends/' + os.path.basename(baseCppName))
129
130 return 0
131
132 if __name__ == '__main__':
133 sys.exit(main())