menuconfig.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #
  2. # File : menuconfig.py
  3. # This file is part of RT-Thread RTOS
  4. # COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License along
  17. # with this program; if not, write to the Free Software Foundation, Inc.,
  18. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. #
  20. # Change Logs:
  21. # Date Author Notes
  22. # 2017-12-29 Bernard The first version
  23. # 2018-07-31.....weety Support pyconfig
  24. import os
  25. import sys
  26. import shutil
  27. import pymenuconfig
  28. # make rtconfig.h from .config
  29. def mk_rtconfig(filename):
  30. try:
  31. config = file(filename)
  32. except:
  33. print('open config:%s failed' % filename)
  34. return
  35. rtconfig = file('rtconfig.h', 'wb')
  36. rtconfig.write('#ifndef RT_CONFIG_H__\n')
  37. rtconfig.write('#define RT_CONFIG_H__\n\n')
  38. empty_line = 1
  39. for line in config:
  40. line = line.lstrip(' ').replace('\n', '').replace('\r', '')
  41. if len(line) == 0: continue
  42. if line[0] == '#':
  43. if len(line) == 1:
  44. if empty_line:
  45. continue
  46. rtconfig.write('\n')
  47. empty_line = 1
  48. continue
  49. comment_line = line[1:]
  50. if line.startswith('# CONFIG_'): line = ' ' + line[9:]
  51. else: line = line[1:]
  52. rtconfig.write('/*%s */\n' % line)
  53. empty_line = 0
  54. else:
  55. empty_line = 0
  56. setting = line.split('=')
  57. if len(setting) >= 2:
  58. if setting[0].startswith('CONFIG_'):
  59. setting[0] = setting[0][7:]
  60. # remove CONFIG_PKG_XX_PATH or CONFIG_PKG_XX_VER
  61. if type(setting[0]) == type('a') and (setting[0].endswith('_PATH') or setting[0].endswith('_VER')):
  62. continue
  63. if setting[1] == 'y':
  64. rtconfig.write('#define %s\n' % setting[0])
  65. else:
  66. rtconfig.write('#define %s %s\n' % (setting[0], setting[1]))
  67. if os.path.isfile('rtconfig_project.h'):
  68. rtconfig.write('#include "rtconfig_project.h"\n')
  69. rtconfig.write('\n')
  70. rtconfig.write('#endif\n')
  71. rtconfig.close()
  72. def config():
  73. mk_rtconfig('.config')
  74. def get_env_dir():
  75. if os.environ.get('ENV_ROOT'):
  76. return os.environ.get('ENV_ROOT')
  77. if sys.platform == 'win32':
  78. home_dir = os.environ['USERPROFILE']
  79. env_dir = os.path.join(home_dir, '.env')
  80. else:
  81. home_dir = os.environ['HOME']
  82. env_dir = os.path.join(home_dir, '.env')
  83. if not os.path.exists(env_dir):
  84. return None
  85. return env_dir
  86. def touch_env():
  87. if sys.platform != 'win32':
  88. home_dir = os.environ['HOME']
  89. env_dir = os.path.join(home_dir, '.env')
  90. if not os.path.exists(env_dir):
  91. os.mkdir(env_dir)
  92. os.mkdir(os.path.join(env_dir, 'local_pkgs'))
  93. os.mkdir(os.path.join(env_dir, 'packages'))
  94. os.system('git clone https://github.com/RT-Thread/packages.git ~/.env/packages/packages')
  95. kconfig = file(os.path.join(env_dir, 'packages', 'Kconfig'), 'w')
  96. kconfig.write('source "$PKGS_DIR/packages/Kconfig"')
  97. os.mkdir(os.path.join(env_dir, 'tools'))
  98. os.system('git clone https://github.com/RT-Thread/env.git ~/.env/tools/scripts')
  99. env_sh = file(os.path.join(env_dir, 'env.sh'), 'w')
  100. env_sh.write('export PATH=~/.env/tools/scripts:$PATH')
  101. else:
  102. home_dir = os.environ['USERPROFILE']
  103. env_dir = os.path.join(home_dir, '.env')
  104. if not os.path.exists(env_dir):
  105. os.mkdir(env_dir)
  106. os.mkdir(os.path.join(env_dir, 'local_pkgs'))
  107. os.mkdir(os.path.join(env_dir, 'packages'))
  108. os.mkdir(os.path.join(env_dir, 'tools'))
  109. kconfig = file(os.path.join(env_dir, 'packages', 'Kconfig'), 'wb')
  110. kconfig.close()
  111. if not os.path.exists(os.path.join(env_dir, 'packages', 'packages')):
  112. try:
  113. ret = os.system('git clone https://github.com/RT-Thread/packages.git %s' % os.path.join(env_dir, 'packages', 'packages'))
  114. if ret != 0:
  115. shutil.rmtree(os.path.join(env_dir, 'packages', 'packages'))
  116. print("********************************************************************************\n"
  117. "* Run command error for \"git clone https://github.com/RT-Thread/packages.git\".\n"
  118. "* This error may have been caused by not found a git tool or network error.\n"
  119. "* If the git tool is not installed, install the git tool first.\n"
  120. "* If the git utility is installed, check whether the git command is added to the system PATH.\n"
  121. "* This error may cause the RT-Thread packages to not work properly.\n"
  122. "********************************************************************************\n")
  123. else:
  124. kconfig = file(os.path.join(env_dir, 'packages', 'Kconfig'), 'wb')
  125. kconfig.write('source "$PKGS_DIR/packages/Kconfig"')
  126. kconfig.close()
  127. except:
  128. print("********************************************************************************\n"
  129. "* Run command error for \"git clone https://github.com/RT-Thread/packages.git\". \n"
  130. "* This error may have been caused by not found a git tool or git tool not in the system PATH. \n"
  131. "* This error may cause the RT-Thread packages to not work properly. \n"
  132. "********************************************************************************\n")
  133. if not os.path.exists(os.path.join(env_dir, 'tools', 'scripts')):
  134. try:
  135. ret = os.system('git clone https://github.com/RT-Thread/env.git %s' % os.path.join(env_dir, 'tools', 'scripts'))
  136. if ret != 0:
  137. shutil.rmtree(os.path.join(env_dir, 'tools', 'scripts'))
  138. print("********************************************************************************\n"
  139. "* Run command error for \"git clone https://github.com/RT-Thread/env.git\".\n"
  140. "* This error may have been caused by not found a git tool or network error.\n"
  141. "* If the git tool is not installed, install the git tool first.\n"
  142. "* If the git utility is installed, check whether the git command is added to the system PATH.\n"
  143. "* This error may cause script tools to fail to work properly.\n"
  144. "********************************************************************************\n")
  145. except:
  146. print("********************************************************************************\n"
  147. "* Run command error for \"git clone https://github.com/RT-Thread/env.git\". \n"
  148. "* This error may have been caused by not found a git tool or git tool not in the system PATH. \n"
  149. "* This error may cause script tools to fail to work properly. \n"
  150. "********************************************************************************\n")
  151. if os.path.exists(os.path.join(env_dir, 'tools', 'scripts')):
  152. os.environ["PATH"] = os.path.join(env_dir, 'tools', 'scripts') + ';' + os.environ["PATH"]
  153. # menuconfig for Linux
  154. def menuconfig(RTT_ROOT):
  155. kconfig_dir = os.path.join(RTT_ROOT, 'tools', 'kconfig-frontends')
  156. os.system('scons -C ' + kconfig_dir)
  157. env_dir = get_env_dir()
  158. if not env_dir:
  159. touch_env()
  160. env_dir = get_env_dir()
  161. os.environ['PKGS_ROOT'] = os.path.join(env_dir, 'packages')
  162. fn = '.config'
  163. if os.path.isfile(fn):
  164. mtime = os.path.getmtime(fn)
  165. else:
  166. mtime = -1
  167. kconfig_cmd = os.path.join(RTT_ROOT, 'tools', 'kconfig-frontends', 'kconfig-mconf')
  168. os.system(kconfig_cmd + ' Kconfig')
  169. if os.path.isfile(fn):
  170. mtime2 = os.path.getmtime(fn)
  171. else:
  172. mtime2 = -1
  173. # make rtconfig.h
  174. if mtime != mtime2:
  175. mk_rtconfig(fn)
  176. # pyconfig for windows and linux
  177. def pyconfig(RTT_ROOT):
  178. env_dir = get_env_dir()
  179. if not env_dir:
  180. touch_env()
  181. env_dir = get_env_dir()
  182. os.environ['PKGS_ROOT'] = os.path.join(env_dir, 'packages')
  183. fn = '.config'
  184. if os.path.isfile(fn):
  185. mtime = os.path.getmtime(fn)
  186. else:
  187. mtime = -1
  188. pymenuconfig.main(['--kconfig', 'Kconfig', '--config', '.config'])
  189. if os.path.isfile(fn):
  190. mtime2 = os.path.getmtime(fn)
  191. else:
  192. mtime2 = -1
  193. # make rtconfig.h
  194. if mtime != mtime2:
  195. mk_rtconfig(fn)