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 import os
     24 from itertools import chain
     25 
     26 INL_HEADER_TMPL = """\
     27 /* WARNING: This is auto-generated file. Do not modify, since changes will
     28  * be lost! Modify the generating script instead.
     29  *
     30  * Generated from {registryName} revision {revision}.
     31  */\
     32 """
     33 
     34 def genInlHeader (registryName, revision):
     35 	return INL_HEADER_TMPL.format(
     36 		registryName	= registryName,
     37 		revision		= str(revision))
     38 
     39 def genInlHeaderForSource (registrySource):
     40 	return genInlHeaderForSource(registrySource.getFilename(), registrySource.getRevision())
     41 
     42 def nextMod (val, mod):
     43 	if val % mod == 0:
     44 		return val + mod
     45 	else:
     46 		return int(val/mod)*mod + mod
     47 
     48 def indentLines (lines):
     49 	tabSize = 4
     50 
     51 	# Split into columns
     52 	lineColumns = [line.split("\t") for line in lines if line is not None]
     53 	if len(lineColumns) == 0:
     54 		return
     55 
     56 	numColumns = max(len(line) for line in lineColumns)
     57 
     58 	# Figure out max length per column
     59 	columnLengths = [nextMod(max(len(line[ndx]) for line in lineColumns if len(line) > ndx), tabSize) for ndx in range(numColumns)]
     60 
     61 	for line in lineColumns:
     62 		indented = []
     63 		for columnNdx, col in enumerate(line[:-1]):
     64 			colLen	= len(col)
     65 			while colLen < columnLengths[columnNdx]:
     66 				col		+= "\t"
     67 				colLen	 = nextMod(colLen, tabSize)
     68 			indented.append(col)
     69 
     70 		# Append last col
     71 		indented.append(line[-1])
     72 		yield "".join(indented)
     73 
     74 def readFile (filename):
     75 	f = open(filename, 'rb')
     76 	data = f.read()
     77 	f.close()
     78 	return data
     79 
     80 def writeFileIfChanged (filename, data):
     81 	if not os.path.exists(filename) or readFile(filename) != data:
     82 		f = open(filename, 'wb')
     83 		f.write(data)
     84 		f.close()
     85 
     86 def writeLines (filename, lines):
     87 	text = ""
     88 	for line in lines:
     89 		text += line
     90 		text += "\n"
     91 
     92 	writeFileIfChanged(filename, text)
     93 	print filename
     94 
     95 def writeInlFile (filename, header, source):
     96 	writeLines(filename, chain([header], source))
     97 
     98 def normalizeConstant (constant):
     99 	value = int(constant, base=0)
    100 	if value >= 1 << 63:
    101 		suffix = 'ull'
    102 	elif value >= 1 << 32:
    103 		suffix = 'll'
    104 	elif value >= 1 << 31:
    105 		suffix = 'u'
    106 	else:
    107 		suffix = ''
    108 	return constant + suffix
    109 
    110 def commandParams (command):
    111 	if len(command.params) > 0:
    112 		return ", ".join(param.declaration for param in command.params)
    113 	else:
    114 		return "void"
    115 
    116 def commandArgs (command):
    117 	return ", ".join(param.name for param in command.params)
    118