Home | History | Annotate | Download | only in khr_util
      1 # -*- coding: utf-8 -*-
      2 
      3 #-------------------------------------------------------------------------
      4 # drawElements Quality Program utilities
      5 # --------------------------------------
      6 #
      7 # Copyright 2015 The Android Open Source Project
      8 #
      9 # Licensed under the Apache License, Version 2.0 (the "License");
     10 # you may not use this file except in compliance with the License.
     11 # You may obtain a copy of the License at
     12 #
     13 #      http://www.apache.org/licenses/LICENSE-2.0
     14 #
     15 # Unless required by applicable law or agreed to in writing, software
     16 # distributed under the License is distributed on an "AS IS" BASIS,
     17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     18 # See the License for the specific language governing permissions and
     19 # limitations under the License.
     20 #
     21 #-------------------------------------------------------------------------
     22 
     23 from itertools import chain
     24 
     25 INL_HEADER_TMPL = """\
     26 /* WARNING: This is auto-generated file. Do not modify, since changes will
     27  * be lost! Modify the generating script instead.
     28  *
     29  * Generated from {registryName} revision {revision}.
     30  */\
     31 """
     32 
     33 def genInlHeader (registryName, revision):
     34 	return INL_HEADER_TMPL.format(
     35 		registryName	= registryName,
     36 		revision		= str(revision))
     37 
     38 def genInlHeaderForSource (registrySource):
     39 	return genInlHeaderForSource(registrySource.getFilename(), registrySource.getRevision())
     40 
     41 def nextMod (val, mod):
     42 	if val % mod == 0:
     43 		return val + mod
     44 	else:
     45 		return int(val/mod)*mod + mod
     46 
     47 def indentLines (lines):
     48 	tabSize = 4
     49 
     50 	# Split into columns
     51 	lineColumns = [line.split("\t") for line in lines if line is not None]
     52 	if len(lineColumns) == 0:
     53 		return
     54 
     55 	numColumns = max(len(line) for line in lineColumns)
     56 
     57 	# Figure out max length per column
     58 	columnLengths = [nextMod(max(len(line[ndx]) for line in lineColumns if len(line) > ndx), tabSize) for ndx in range(numColumns)]
     59 
     60 	for line in lineColumns:
     61 		indented = []
     62 		for columnNdx, col in enumerate(line[:-1]):
     63 			colLen	= len(col)
     64 			while colLen < columnLengths[columnNdx]:
     65 				col		+= "\t"
     66 				colLen	 = nextMod(colLen, tabSize)
     67 			indented.append(col)
     68 
     69 		# Append last col
     70 		indented.append(line[-1])
     71 		yield "".join(indented)
     72 
     73 def writeLines (filename, lines):
     74 	with open(filename, 'wb') as f:
     75 		for line in lines:
     76 			if line is not None:
     77 				f.write(line)
     78 				f.write('\n')
     79 	print filename
     80 
     81 def writeInlFile (filename, header, source):
     82 	writeLines(filename, chain([header], source))
     83 
     84 def normalizeConstant (constant):
     85 	value = int(constant, base=0)
     86 	if value >= 1 << 63:
     87 		suffix = 'ull'
     88 	elif value >= 1 << 32:
     89 		suffix = 'll'
     90 	elif value >= 1 << 31:
     91 		suffix = 'u'
     92 	else:
     93 		suffix = ''
     94 	return constant + suffix
     95 
     96 def commandParams (command):
     97 	if len(command.params) > 0:
     98 		return ", ".join(param.declaration for param in command.params)
     99 	else:
    100 		return "void"
    101 
    102 def commandArgs (command):
    103 	return ", ".join(param.name for param in command.params)
    104