1#!/usr/bin/env python 2# 3# This software is licensed under the terms of the GNU General Public 4# License version 2, as published by the Free Software Foundation, and 5# may be copied, distributed, and modified under those terms. 6# 7# This program is distributed in the hope that it will be useful, 8# but WITHOUT ANY WARRANTY; without even the implied warranty of 9# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10# GNU General Public License for more details. 11# 12# this script is used to generate 'android/avd/hw-config.h' by 13# parsing 'android/avd/hardware-properties.ini' 14# 15# 16import sys, os, string, re 17 18# location of source file, relative to current program directory 19relativeSourcePath = "../avd/hardware-properties.ini" 20 21# location of target file, relative to current program directory 22relativeTargetPath = "../avd/hw-config-defs.h" 23 24def quoteStringForC(str): 25 """quote a string so it can be used in C""" 26 return '\\"'.join('"'+p+'"' for p in str.split('"')) 27 28# a dictionary that maps item types as they appear in the .ini 29# file into macro names in the generated C header 30# 31typesToMacros = { 32 'integer': 'HWCFG_INT', 33 'string': 'HWCFG_STRING', 34 'boolean': 'HWCFG_BOOL', 35 'diskSize': 'HWCFG_DISKSIZE', 36 'double': 'HWCFG_DOUBLE' 37 } 38 39# the list of macro names 40macroNames = typesToMacros.values() 41 42# target program header 43targetHeader = """\ 44/* this file is automatically generated from 'hardware-properties.ini' 45 * DO NOT EDIT IT. To re-generate it, use android/tools/gen-hw-config.py' 46 */ 47""" 48 49# locate source and target 50programDir = os.path.dirname(sys.argv[0]) 51if len(sys.argv) != 3: 52 print "Usage: %s source target\n" % os.path.basename(sys.argv[0]) 53 sys.exit(1) 54 55sourceFile = sys.argv[1] 56targetFile = sys.argv[2] 57 58# parse the source file and record items 59# I would love to use Python's ConfigParser, but it doesn't 60# support files without sections, or multiply defined items 61# 62items = [] 63lastItem = None 64 65class Item: 66 def __init__(self,name): 67 self.name = name 68 self.type = type 69 self.default = None 70 self.abstract = "" 71 self.description = "" 72 73 def add(self,key,val): 74 if key == 'type': 75 self.type = val 76 elif key == 'default': 77 self.default = val 78 elif key == 'abstract': 79 self.abstract = val 80 elif key == 'description': 81 self.description = val 82 83for line in open(sourceFile): 84 line = line.strip() 85 # ignore empty lines and comments 86 if len(line) == 0 or line[0] in ";#": 87 continue 88 key, value = line.split('=') 89 90 key = key.strip() 91 value = value.strip() 92 93 if key == 'name': 94 if lastItem: items.append(lastItem) 95 lastItem = Item(value) 96 else: 97 lastItem.add(key, value) 98 99if lastItem: 100 items.append(lastItem) 101 102if targetFile == '--': 103 out = sys.stdout 104else: 105 out = open(targetFile,"wb") 106 107out.write(targetHeader) 108 109# write guards to prevent bad compiles 110for m in macroNames: 111 out.write("""\ 112#ifndef %(macro)s 113#error %(macro)s not defined 114#endif 115""" % { 'macro':m }) 116out.write("\n") 117 118for item in items: 119 if item.type == None: 120 sys.stderr.write("ignoring config item with no type '%s'\n" % item.name) 121 continue 122 123 if not typesToMacros.has_key(item.type): 124 sys.stderr.write("ignoring config item with unknown type '%s': '%s'\n" % \ 125 (item.type, item.name)) 126 continue 127 128 if item.default == None: 129 sys.stderr.write("ignoring config item with no default '%s' */" % item.name) 130 continue 131 132 # convert dots into underscores 133 varMacro = typesToMacros[item.type] 134 varNameStr = quoteStringForC(item.name) 135 varName = item.name.replace(".","_") 136 varDefault = item.default 137 varAbstract = quoteStringForC(item.abstract) 138 varDesc = quoteStringForC(item.description) 139 140 if item.type in [ 'string', 'boolean', 'diskSize' ]: 141 # quote default value for strings 142 varDefault = quoteStringForC(varDefault) 143 144 out.write("%s(\n %s,\n %s,\n %s,\n %s,\n %s)\n\n" % \ 145 (varMacro,varName,varNameStr,varDefault,varAbstract,varDesc)) 146 147 148for m in macroNames: 149 out.write("#undef %s\n" % m) 150 151out.write("/* end of auto-generated file */\n") 152out.close() 153