menuconfig.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import os
  2. # make rtconfig.h from .config
  3. def mk_rtconfig(filename):
  4. try:
  5. config = file(filename)
  6. except:
  7. print('open config:%s failed' % filename)
  8. return
  9. rtconfig = file('rtconfig.h', 'w')
  10. rtconfig.write('#ifndef RT_CONFIG_H__\n')
  11. rtconfig.write('#define RT_CONFIG_H__\n\n')
  12. empty_line = 1
  13. for line in config:
  14. line = line.lstrip(' ').replace('\n', '').replace('\r', '')
  15. if len(line) == 0: continue
  16. if line[0] == '#':
  17. if len(line) == 1:
  18. if empty_line:
  19. continue
  20. rtconfig.write('\n')
  21. empty_line = 1
  22. continue
  23. comment_line = line[1:]
  24. if line.startswith('# CONFIG_'): line = ' ' + line[9:]
  25. else: line = line[1:]
  26. rtconfig.write('/*%s */\n' % line)
  27. empty_line = 0
  28. else:
  29. empty_line = 0
  30. setting = line.split('=')
  31. if len(setting) >= 2:
  32. if setting[0].startswith('CONFIG_'):
  33. setting[0] = setting[0][7:]
  34. # remove CONFIG_PKG_XX_PATH or CONFIG_PKG_XX_VER
  35. if type(setting[0]) == type('a') and (setting[0].endswith('_PATH') or setting[0].endswith('_VER')):
  36. continue
  37. if setting[1] == 'y':
  38. rtconfig.write('#define %s\n' % setting[0])
  39. else:
  40. rtconfig.write('#define %s %s\n' % (setting[0], setting[1]))
  41. if os.path.isfile('rtconfig_project.h'):
  42. rtconfig.write('#include "rtconfig_project.h"\n')
  43. rtconfig.write('\n')
  44. rtconfig.write('#endif\n')
  45. rtconfig.close()
  46. def config():
  47. mk_rtconfig('.config')
  48. def get_env_dir():
  49. if os.environ.get('ENV_ROOT'):
  50. return os.environ.get('ENV_ROOT')
  51. home_dir = os.environ['HOME']
  52. env_dir = os.path.join(home_dir, '.env')
  53. if not os.path.exists(env_dir):
  54. return None
  55. return env_dir
  56. def touch_env():
  57. home_dir = os.environ['HOME']
  58. env_dir = os.path.join(home_dir, '.env')
  59. if not os.path.exists(env_dir):
  60. os.mkdir(env_dir)
  61. os.mkdir(os.path.join(env_dir, 'local_pkgs'))
  62. os.mkdir(os.path.join(env_dir, 'packages'))
  63. os.system('git clone https://github.com/RT-Thread/packages.git ~/.env/packages/packages')
  64. kconfig = file(os.path.join(env_dir, 'packages', 'Kconfig'), 'w')
  65. kconfig.write('source "$PKGS_DIR/packages/Kconfig"')
  66. os.mkdir(os.path.join(env_dir, 'tools'))
  67. os.system('git clone https://github.com/RT-Thread/env.git ~/.env/tools/scripts')
  68. env_sh = file(os.path.join(env_dir, 'env.sh'), 'w')
  69. env_sh.write('export PATH=~/.env/tools/scripts:$PATH')
  70. # menuconfig for Linux
  71. def menuconfig(RTT_ROOT):
  72. kconfig_dir = os.path.join(RTT_ROOT, 'tools', 'kconfig-frontends')
  73. os.system('scons -C ' + kconfig_dir)
  74. env_dir = get_env_dir()
  75. if not env_dir:
  76. touch_env()
  77. env_dir = get_env_dir()
  78. os.environ['PKGS_ROOT'] = os.path.join(env_dir, 'packages')
  79. fn = '.config'
  80. if os.path.isfile(fn):
  81. mtime = os.path.getmtime(fn)
  82. else:
  83. mtime = -1
  84. kconfig_cmd = os.path.join(RTT_ROOT, 'tools', 'kconfig-frontends', 'kconfig-mconf')
  85. os.system(kconfig_cmd + ' Kconfig')
  86. if os.path.isfile(fn):
  87. mtime2 = os.path.getmtime(fn)
  88. else:
  89. mtime2 = -1
  90. # make rtconfig.h
  91. if mtime != mtime2:
  92. mk_rtconfig(fn)