The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame^] | 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 | # |
| 16 | import sys, os, string, re |
| 17 | |
| 18 | # location of source file, relative to current program directory |
| 19 | relativeSourcePath = "../avd/hardware-properties.ini" |
| 20 | |
| 21 | # location of target file, relative to current program directory |
| 22 | relativeTargetPath = "../avd/hw-config-defs.h" |
| 23 | |
| 24 | def 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 | # |
| 31 | typesToMacros = { |
| 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 |
| 40 | macroNames = typesToMacros.values() |
| 41 | |
| 42 | # target program header |
| 43 | targetHeader = """\ |
| 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 | # locate source and target |
| 49 | programDir = os.path.dirname(sys.argv[0]) |
| 50 | sourceFile = os.path.normpath(os.path.join(programDir,relativeSourcePath)) |
| 51 | targetFile = os.path.normpath(os.path.join(programDir,relativeTargetPath)) |
| 52 | |
| 53 | # parse the source file and record items |
| 54 | # I would love to use Python's ConfigParser, but it doesn't |
| 55 | # support files without sections, or multiply defined items |
| 56 | # |
| 57 | items = [] |
| 58 | lastItem = None |
| 59 | |
| 60 | class Item: |
| 61 | def __init__(self,name): |
| 62 | self.name = name |
| 63 | self.type = type |
| 64 | self.default = None |
| 65 | self.abstract = "" |
| 66 | self.description = "" |
| 67 | |
| 68 | def add(self,key,val): |
| 69 | if key == 'type': |
| 70 | self.type = val |
| 71 | elif key == 'default': |
| 72 | self.default = val |
| 73 | elif key == 'abstract': |
| 74 | self.abstract = val |
| 75 | elif key == 'description': |
| 76 | self.description = val |
| 77 | |
| 78 | for line in open(sourceFile): |
| 79 | line = line.strip() |
| 80 | # ignore empty lines and comments |
| 81 | if len(line) == 0 or line[0] in ";#": |
| 82 | continue |
| 83 | key, value = line.split('=') |
| 84 | |
| 85 | key = key.strip() |
| 86 | value = value.strip() |
| 87 | |
| 88 | if key == 'name': |
| 89 | if lastItem: items.append(lastItem) |
| 90 | lastItem = Item(value) |
| 91 | else: |
| 92 | lastItem.add(key, value) |
| 93 | |
| 94 | if lastItem: |
| 95 | items.append(lastItem) |
| 96 | |
| 97 | |
| 98 | print targetHeader |
| 99 | |
| 100 | # write guards to prevent bad compiles |
| 101 | for m in macroNames: |
| 102 | print """\ |
| 103 | #ifndef %(macro)s |
| 104 | #error %(macro)s not defined |
| 105 | #endif""" % { 'macro':m } |
| 106 | print "" |
| 107 | |
| 108 | for item in items: |
| 109 | if item.type == None: |
| 110 | sys.stderr.write("ignoring config item with no type '%s'\n" % item.name) |
| 111 | continue |
| 112 | |
| 113 | if not typesToMacros.has_key(item.type): |
| 114 | sys.stderr.write("ignoring config item with unknown type '%s': '%s'\n" % \ |
| 115 | (item.type, item.name)) |
| 116 | continue |
| 117 | |
| 118 | if item.default == None: |
| 119 | sys.stderr.write("ignoring config item with no default '%s' */" % item.name) |
| 120 | continue |
| 121 | |
| 122 | # convert dots into underscores |
| 123 | varMacro = typesToMacros[item.type] |
| 124 | varNameStr = quoteStringForC(item.name) |
| 125 | varName = item.name.replace(".","_") |
| 126 | varDefault = item.default |
| 127 | varAbstract = quoteStringForC(item.abstract) |
| 128 | varDesc = quoteStringForC(item.description) |
| 129 | |
| 130 | if item.type in [ 'string', 'boolean', 'diskSize' ]: |
| 131 | # quote default value for strings |
| 132 | varDefault = quoteStringForC(varDefault) |
| 133 | |
| 134 | print "%s(\n %s,\n %s,\n %s,\n %s,\n %s)\n" % \ |
| 135 | (varMacro,varName,varNameStr,varDefault,varAbstract,varDesc) |
| 136 | |
| 137 | |
| 138 | for m in macroNames: |
| 139 | print "#undef %s" % m |
| 140 | |
| 141 | print "/* end of auto-generated file */" |