summaryrefslogtreecommitdiff
path: root/src/util/merge_driinfo.py
blob: e6ccca5e0f362afd3255e9398cbc3c43afd64266 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#
# Copyright 2017 Advanced Micro Devices, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# on the rights to use, copy, modify, merge, publish, distribute, sub
# license, and/or sell copies of the Software, and to permit persons to whom
# the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
# THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
# USE OR OTHER DEALINGS IN THE SOFTWARE.

"""
usage: merge_driinfo.py <list of input files>

Generates a source file which contains the DRI_CONF_xxx macros for generating
the driinfo XML that describes the available DriConf options for a driver and
its supported state trackers, based on the merged information from the input
files.
"""

from __future__ import print_function

import mako.template
import re
import sys


# Some regexps used during input parsing
RE_section_begin = re.compile(r'DRI_CONF_SECTION_(.*)')
RE_option = re.compile(r'DRI_CONF_(.*)\((.*)\)')


class Option(object):
   """
   Represent a config option as:
    * name: the xxx part of the DRI_CONF_xxx macro
    * defaults: the defaults parameters that are passed into the macro
   """
   def __init__(self, name, defaults):
      self.name = name
      self.defaults = defaults


class Section(object):
   """
   Represent a config section description as:
    * name: the xxx part of the DRI_CONF_SECTION_xxx macro
    * options: list of options
   """
   def __init__(self, name):
      self.name = name
      self.options = []


def parse_inputs(input_filenames):
   success = True
   sections_lists = []

   for input_filename in input_filenames:
      with open(input_filename, 'r') as infile:
         sections = []
         sections_lists.append(sections)

         section = None

         linenum = 0
         for line in infile:
            linenum += 1
            line = line.strip()
            if not line:
               continue

            if line.startswith('//'):
               continue

            if line == 'DRI_CONF_SECTION_END':
               if section is None:
                  print('{}:{}: no open section'
                        .format(input_filename, linenum))
                  success = False
                  continue
               section = None
               continue

            m = RE_section_begin.match(line)
            if m:
               if section is not None:
                  print('{}:{}: nested sections are not supported'
                        .format(input_filename, linenum))
                  success = False
                  continue
               if sections is None:
                  print('{}:{}: missing DRIINFO line'
                        .format(input_filename, linenum))
                  success = False
                  break # parsing the rest really makes no sense
               section = Section(m.group(1))
               sections.append(section)
               continue

            m = RE_option.match(line)
            if m:
               if section is None:
                  print('{}:{}: no open section'
                        .format(input_filename, linenum))
                  success = False
                  break
               section.options.append(Option(m.group(1), m.group(2)))
               continue

            print('{}:{}: do not understand this line'
                  .format(input_filename, linenum))
            success = False

         if section is not None:
            print('{}:end-of-file: missing end of section'
                  .format(input_filename))
            success = False

   if success:
      return sections_lists
   return None


def merge_sections(section_list):
   """
   section_list: list of Section objects to be merged, all of the same name
   Return a merged Section object (everything is deeply copied)
   """
   merged_section = Section(section_list[0].name)

   for section in section_list:
      assert section.name == merged_section.name

      for orig_option in section.options:
         for merged_option in merged_section.options:
            if orig_option.name == merged_option.name:
               merged_option.defaults = orig_option.defaults
               break
         else:
            merged_section.options.append(Option(orig_option.name, orig_option.defaults))

   return merged_section


def merge_sections_lists(sections_lists):
   """
   sections_lists: list of lists of Section objects to be merged
   Return a merged list of merged Section objects; everything is deeply copied.
   Default values for options in later lists override earlier default values.
   """
   merged_sections = []

   for idx,sections in enumerate(sections_lists):
      for base_section in sections:
         original_sections = [base_section]
         for next_sections in sections_lists[idx+1:]:
            for j,section in enumerate(next_sections):
               if section.name == base_section.name:
                  original_sections.append(section)
                  del next_sections[j]
                  break

         merged_section = merge_sections(original_sections)

         merged_sections.append(merged_section)

   return merged_sections


def main(input_filenames):
   sections_lists = parse_inputs(input_filenames)
   if sections_lists is None:
      return False

   merged_sections_list = merge_sections_lists(sections_lists)

   driinfo_h_template = mako.template.Template("""\
// DO NOT EDIT - this file is automatically generated by merge_driinfo.py

/*
Use as:

#include "xmlpool.h"

static const char driinfo_xml[] =
#include "this_file"
;
*/

DRI_CONF_BEGIN
% for section in sections:
   DRI_CONF_SECTION_${section.name}
% for option in section.options:
      DRI_CONF_${option.name}(${option.defaults})
% endfor
   DRI_CONF_SECTION_END
% endfor
DRI_CONF_END""")

   print(driinfo_h_template.render(sections=merged_sections_list))
   return True


if __name__ == '__main__':
   if len(sys.argv) <= 1:
      print('Missing arguments')
      sys.exit(1)

   if not main(sys.argv[1:]):
      sys.exit(1)