mkconfig.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/python3
  2. # this script is using to create a .config configuration file
  3. import sys
  4. import os
  5. import json
  6. argc = 0
  7. for name in sys.argv:
  8. argc += 1
  9. def fatal(msg):
  10. sys.stderr.write("%s: fatal: %s\n" % (sys.argv[0], msg))
  11. exit(1)
  12. if argc != 1:
  13. fatal("no argument this requires")
  14. if os.getcwd()[-7 : -1] == "script":
  15. fatal("call this as scripts/do_mkconfig.py, not ./do_mkconfig.py")
  16. basedir = os.getcwd()
  17. config_filename = basedir + "/include/config.h"
  18. kconfig_filename = basedir + "/Kconfig"
  19. # unlike that in linux, Kconfig is just a json likes that:
  20. # [
  21. # {
  22. # "name": "descr"
  23. # },
  24. # {
  25. # "DISABLE_A": "will you disable A"
  26. # }
  27. # ]
  28. try:
  29. open(config_filename, "r")
  30. print("%s exists. remove it if you want to config again. " % basedir + "/include/config.h")
  31. exit(0)
  32. except FileNotFoundError:
  33. pass
  34. try:
  35. kconfig_fd = open(kconfig_filename, "r")
  36. except FileNotFoundError:
  37. fatal("Kconfig not exists")
  38. out_fd = open(config_filename, "w")
  39. kconfig_jsonobj = json.load(kconfig_fd)
  40. kconfig_fd.close()
  41. if type(kconfig_jsonobj) != list:
  42. fatal("Kconfig should be a json list object, but %s gave" % type(kconfig_jsonobj))
  43. for config in kconfig_jsonobj:
  44. for key, descr in config.items():
  45. print("CONFIG %s: " % key)
  46. print("\t(" + descr + ")")
  47. value = input("> (int) ")
  48. try:
  49. value = int(value)
  50. except ValueError:
  51. fatal("an integer requires for %s" % key)
  52. out_fd.write("#define %s \t\t\t(%d)\n" % (key, value))
  53. print("\n")