menuconfig.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. # 2019-07-13 armink Support guiconfig
  25. import os
  26. import re
  27. import sys
  28. import shutil
  29. # make rtconfig.h from .config
  30. def mk_rtconfig(filename):
  31. try:
  32. config = open(filename, 'r')
  33. except:
  34. print('open config:%s failed' % filename)
  35. return
  36. rtconfig = open('rtconfig.h', 'w')
  37. rtconfig.write('#ifndef RT_CONFIG_H__\n')
  38. rtconfig.write('#define RT_CONFIG_H__\n\n')
  39. empty_line = 1
  40. for line in config:
  41. line = line.lstrip(' ').replace('\n', '').replace('\r', '')
  42. if len(line) == 0: continue
  43. if line[0] == '#':
  44. if len(line) == 1:
  45. if empty_line:
  46. continue
  47. rtconfig.write('\n')
  48. empty_line = 1
  49. continue
  50. comment_line = line[1:]
  51. if line.startswith('# CONFIG_'): line = ' ' + line[9:]
  52. else: line = line[1:]
  53. rtconfig.write('/*%s */\n' % line)
  54. empty_line = 0
  55. else:
  56. empty_line = 0
  57. setting = line.split('=')
  58. if len(setting) >= 2:
  59. if setting[0].startswith('CONFIG_'):
  60. setting[0] = setting[0][7:]
  61. # remove CONFIG_PKG_XX_PATH or CONFIG_PKG_XX_VER
  62. if type(setting[0]) == type('a') and (setting[0].endswith('_PATH') or setting[0].endswith('_VER')):
  63. continue
  64. if setting[1] == 'y':
  65. rtconfig.write('#define %s\n' % setting[0])
  66. else:
  67. rtconfig.write('#define %s %s\n' % (setting[0], re.findall(r"^.*?=(.*)$",line)[0]))
  68. if os.path.isfile('rtconfig_project.h'):
  69. rtconfig.write('#include "rtconfig_project.h"\n')
  70. rtconfig.write('\n')
  71. rtconfig.write('#endif\n')
  72. rtconfig.close()
  73. def config():
  74. mk_rtconfig('.config')
  75. def get_env_dir():
  76. if os.environ.get('ENV_ROOT'):
  77. return os.environ.get('ENV_ROOT')
  78. if sys.platform == 'win32':
  79. home_dir = os.environ['USERPROFILE']
  80. env_dir = os.path.join(home_dir, '.env')
  81. else:
  82. home_dir = os.environ['HOME']
  83. env_dir = os.path.join(home_dir, '.env')
  84. if not os.path.exists(env_dir):
  85. return None
  86. return env_dir
  87. def help_info():
  88. print("**********************************************************************************\n"
  89. "* Help infomation:\n"
  90. "* Git tool install step.\n"
  91. "* If your system is linux, you can use command below to install git.\n"
  92. "* $ sudo yum install git\n"
  93. "* $ sudo apt-get install git\n"
  94. "* If your system is windows, you should download git software(msysGit).\n"
  95. "* Download path: http://git-scm.com/download/win\n"
  96. "* After you install it, be sure to add the git command execution PATH \n"
  97. "* to your system PATH.\n"
  98. "* Usually, git command PATH is $YOUR_INSTALL_DIR\\Git\\bin\n"
  99. "* If your system is OSX, please download git and install it.\n"
  100. "* Download path: http://git-scm.com/download/mac\n"
  101. "**********************************************************************************\n")
  102. def touch_env():
  103. if sys.platform != 'win32':
  104. home_dir = os.environ['HOME']
  105. else:
  106. home_dir = os.environ['USERPROFILE']
  107. env_dir = os.path.join(home_dir, '.env')
  108. if not os.path.exists(env_dir):
  109. os.mkdir(env_dir)
  110. os.mkdir(os.path.join(env_dir, 'local_pkgs'))
  111. os.mkdir(os.path.join(env_dir, 'packages'))
  112. os.mkdir(os.path.join(env_dir, 'tools'))
  113. kconfig = open(os.path.join(env_dir, 'packages', 'Kconfig'), 'w')
  114. kconfig.close()
  115. if not os.path.exists(os.path.join(env_dir, 'packages', 'packages')):
  116. try:
  117. ret = os.system('git clone https://github.com/RT-Thread/packages.git %s' % os.path.join(env_dir, 'packages', 'packages'))
  118. if ret != 0:
  119. shutil.rmtree(os.path.join(env_dir, 'packages', 'packages'))
  120. print("********************************************************************************\n"
  121. "* Warnning:\n"
  122. "* Run command error for \"git clone https://github.com/RT-Thread/packages.git\".\n"
  123. "* This error may have been caused by not found a git tool or network error.\n"
  124. "* If the git tool is not installed, install the git tool first.\n"
  125. "* If the git utility is installed, check whether the git command is added to \n"
  126. "* the system PATH.\n"
  127. "* This error may cause the RT-Thread packages to not work properly.\n"
  128. "********************************************************************************\n")
  129. help_info()
  130. else:
  131. kconfig = open(os.path.join(env_dir, 'packages', 'Kconfig'), 'w')
  132. kconfig.write('source "$PKGS_DIR/packages/Kconfig"')
  133. kconfig.close()
  134. except:
  135. print("**********************************************************************************\n"
  136. "* Warnning:\n"
  137. "* Run command error for \"git clone https://github.com/RT-Thread/packages.git\". \n"
  138. "* This error may have been caused by not found a git tool or git tool not in \n"
  139. "* the system PATH. \n"
  140. "* This error may cause the RT-Thread packages to not work properly. \n"
  141. "**********************************************************************************\n")
  142. help_info()
  143. if not os.path.exists(os.path.join(env_dir, 'tools', 'scripts')):
  144. try:
  145. ret = os.system('git clone https://github.com/RT-Thread/env.git %s' % os.path.join(env_dir, 'tools', 'scripts'))
  146. if ret != 0:
  147. shutil.rmtree(os.path.join(env_dir, 'tools', 'scripts'))
  148. print("********************************************************************************\n"
  149. "* Warnning:\n"
  150. "* Run command error for \"git clone https://github.com/RT-Thread/env.git\".\n"
  151. "* This error may have been caused by not found a git tool or network error.\n"
  152. "* If the git tool is not installed, install the git tool first.\n"
  153. "* If the git utility is installed, check whether the git command is added \n"
  154. "* to the system PATH.\n"
  155. "* This error may cause script tools to fail to work properly.\n"
  156. "********************************************************************************\n")
  157. help_info()
  158. except:
  159. print("********************************************************************************\n"
  160. "* Warnning:\n"
  161. "* Run command error for \"git clone https://github.com/RT-Thread/env.git\". \n"
  162. "* This error may have been caused by not found a git tool or git tool not in \n"
  163. "* the system PATH. \n"
  164. "* This error may cause script tools to fail to work properly. \n"
  165. "********************************************************************************\n")
  166. help_info()
  167. if sys.platform != 'win32':
  168. env_sh = open(os.path.join(env_dir, 'env.sh'), 'w')
  169. env_sh.write('export PATH=~/.env/tools/scripts:$PATH')
  170. else:
  171. if os.path.exists(os.path.join(env_dir, 'tools', 'scripts')):
  172. os.environ["PATH"] = os.path.join(env_dir, 'tools', 'scripts') + ';' + os.environ["PATH"]
  173. # menuconfig for Linux
  174. def menuconfig(RTT_ROOT):
  175. kconfig_dir = os.path.join(RTT_ROOT, 'tools', 'kconfig-frontends')
  176. os.system('scons -C ' + kconfig_dir)
  177. touch_env()
  178. env_dir = get_env_dir()
  179. os.environ['PKGS_ROOT'] = os.path.join(env_dir, 'packages')
  180. fn = '.config'
  181. if os.path.isfile(fn):
  182. mtime = os.path.getmtime(fn)
  183. else:
  184. mtime = -1
  185. kconfig_cmd = os.path.join(RTT_ROOT, 'tools', 'kconfig-frontends', 'kconfig-mconf')
  186. os.system(kconfig_cmd + ' Kconfig')
  187. if os.path.isfile(fn):
  188. mtime2 = os.path.getmtime(fn)
  189. else:
  190. mtime2 = -1
  191. # make rtconfig.h
  192. if mtime != mtime2:
  193. mk_rtconfig(fn)
  194. # guiconfig for windows and linux
  195. def guiconfig(RTT_ROOT):
  196. import pyguiconfig
  197. if sys.platform != 'win32':
  198. touch_env()
  199. env_dir = get_env_dir()
  200. os.environ['PKGS_ROOT'] = os.path.join(env_dir, 'packages')
  201. fn = '.config'
  202. if os.path.isfile(fn):
  203. mtime = os.path.getmtime(fn)
  204. else:
  205. mtime = -1
  206. sys.argv = ['guiconfig', 'Kconfig'];
  207. pyguiconfig._main()
  208. if os.path.isfile(fn):
  209. mtime2 = os.path.getmtime(fn)
  210. else:
  211. mtime2 = -1
  212. # make rtconfig.h
  213. if mtime != mtime2:
  214. mk_rtconfig(fn)
  215. # guiconfig for windows and linux
  216. def guiconfig_silent(RTT_ROOT):
  217. import defconfig
  218. if sys.platform != 'win32':
  219. touch_env()
  220. env_dir = get_env_dir()
  221. os.environ['PKGS_ROOT'] = os.path.join(env_dir, 'packages')
  222. fn = '.config'
  223. sys.argv = ['defconfig', '--kconfig', 'Kconfig', '.config']
  224. defconfig.main()
  225. # silent mode, force to make rtconfig.h
  226. mk_rtconfig(fn)