configure_file.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env python2.7
  2. import sys
  3. import os.path
  4. import re
  5. cmakeDef01 = "#cmakedefine01"
  6. cmakeDef = "#cmakedefine"
  7. def replaceLine(l, varDict, define):
  8. words = l.split()
  9. if words:
  10. if words[0] == cmakeDef:
  11. sPos = l.find(cmakeDef)
  12. ePos = sPos + len(cmakeDef)
  13. l = l[:sPos] + define + l[ePos:] + '\n'
  14. if words[0] == cmakeDef01:
  15. var = words[1]
  16. cmakeValue = varDict.get(var)
  17. if cmakeValue == 'yes':
  18. val = '1'
  19. else:
  20. val = '0'
  21. sPos = l.find(cmakeDef01)
  22. ePos = l.find(var) + len(var)
  23. l = l[:sPos] + define + ' ' + var + ' ' + val + l[ePos + 1 :] + '\n'
  24. finder = re.compile(".*?(@[a-zA-Z0-9_]+@).*")
  25. while True:
  26. re_result = finder.match(l)
  27. if not re_result:
  28. return l
  29. key = re_result.group(1)[1:-1]
  30. l = l[: re_result.start(1)] + varDict.get(key, '') + l[re_result.end(1) :]
  31. def main(inputPath, outputPath, varDict):
  32. define = '#define' if os.path.splitext(outputPath)[1] != '.asm' else '%define'
  33. with open(outputPath, 'w') as output:
  34. with open(inputPath, 'r') as input:
  35. for l in input:
  36. output.write(replaceLine(l, varDict, define))
  37. def usage():
  38. print("usage: configure_file.py inputPath outputPath key1=value1 ...")
  39. exit(1)
  40. if __name__ == "__main__":
  41. if len(sys.argv) < 3:
  42. usage()
  43. varDict = {}
  44. for x in sys.argv[3:]:
  45. try:
  46. key, value = str(x).split('=', 1)
  47. value = value.replace("#BACKSLASH#", "\\\\")
  48. value = value.replace("#DOUBLE_QUOTE#", '"')
  49. except Exception:
  50. continue
  51. varDict[key] = value
  52. main(sys.argv[1], sys.argv[2], varDict)