menuconfig.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. import hashlib
  30. import operator
  31. DEFAULT_RTT_PACKAGE_URL = 'https://github.com/RT-Thread/packages.git'
  32. # you can change the package url by defining RTT_PACKAGE_URL, ex:
  33. # export RTT_PACKAGE_URL=https://github.com/Varanda-Labs/packages.git
  34. # make rtconfig.h from .config
  35. def is_pkg_special_config(config_str):
  36. ''' judge if it's CONFIG_PKG_XX_PATH or CONFIG_PKG_XX_VER'''
  37. if type(config_str) == type('a'):
  38. if config_str.startswith("PKG_") and (config_str.endswith('_PATH') or config_str.endswith('_VER')):
  39. return True
  40. return False
  41. def mk_rtconfig(filename):
  42. try:
  43. config = open(filename, 'r')
  44. except:
  45. print('open config:%s failed' % filename)
  46. return
  47. rtconfig = open('rtconfig.h', 'w')
  48. rtconfig.write('#ifndef RT_CONFIG_H__\n')
  49. rtconfig.write('#define RT_CONFIG_H__\n\n')
  50. empty_line = 1
  51. for line in config:
  52. line = line.lstrip(' ').replace('\n', '').replace('\r', '')
  53. if len(line) == 0:
  54. continue
  55. if line[0] == '#':
  56. if len(line) == 1:
  57. if empty_line:
  58. continue
  59. rtconfig.write('\n')
  60. empty_line = 1
  61. continue
  62. if line.startswith('# CONFIG_'):
  63. line = ' ' + line[9:]
  64. else:
  65. line = line[1:]
  66. rtconfig.write('/*%s */\n' % line)
  67. empty_line = 0
  68. else:
  69. empty_line = 0
  70. setting = line.split('=')
  71. if len(setting) >= 2:
  72. if setting[0].startswith('CONFIG_'):
  73. setting[0] = setting[0][7:]
  74. # remove CONFIG_PKG_XX_PATH or CONFIG_PKG_XX_VER
  75. if is_pkg_special_config(setting[0]):
  76. continue
  77. if setting[1] == 'y':
  78. rtconfig.write('#define %s\n' % setting[0])
  79. else:
  80. rtconfig.write('#define %s %s\n' % (setting[0], re.findall(r"^.*?=(.*)$",line)[0]))
  81. if os.path.isfile('rtconfig_project.h'):
  82. rtconfig.write('#include "rtconfig_project.h"\n')
  83. rtconfig.write('\n')
  84. rtconfig.write('#endif\n')
  85. rtconfig.close()
  86. def get_file_md5(file):
  87. MD5 = hashlib.new('md5')
  88. with open(file, 'r') as fp:
  89. MD5.update(fp.read().encode('utf8'))
  90. fp_md5 = MD5.hexdigest()
  91. return fp_md5
  92. def config():
  93. mk_rtconfig('.config')
  94. def get_env_dir():
  95. if os.environ.get('ENV_ROOT'):
  96. return os.environ.get('ENV_ROOT')
  97. if sys.platform == 'win32':
  98. home_dir = os.environ['USERPROFILE']
  99. env_dir = os.path.join(home_dir, '.env')
  100. else:
  101. home_dir = os.environ['HOME']
  102. env_dir = os.path.join(home_dir, '.env')
  103. if not os.path.exists(env_dir):
  104. return None
  105. return env_dir
  106. def help_info():
  107. print("**********************************************************************************\n"
  108. "* Help infomation:\n"
  109. "* Git tool install step.\n"
  110. "* If your system is linux, you can use command below to install git.\n"
  111. "* $ sudo yum install git\n"
  112. "* $ sudo apt-get install git\n"
  113. "* If your system is windows, you should download git software(msysGit).\n"
  114. "* Download path: http://git-scm.com/download/win\n"
  115. "* After you install it, be sure to add the git command execution PATH \n"
  116. "* to your system PATH.\n"
  117. "* Usually, git command PATH is $YOUR_INSTALL_DIR\\Git\\bin\n"
  118. "* If your system is OSX, please download git and install it.\n"
  119. "* Download path: http://git-scm.com/download/mac\n"
  120. "**********************************************************************************\n")
  121. def touch_env():
  122. if sys.platform != 'win32':
  123. home_dir = os.environ['HOME']
  124. else:
  125. home_dir = os.environ['USERPROFILE']
  126. package_url = os.getenv('RTT_PACKAGE_URL') or DEFAULT_RTT_PACKAGE_URL
  127. env_dir = os.path.join(home_dir, '.env')
  128. if not os.path.exists(env_dir):
  129. os.mkdir(env_dir)
  130. os.mkdir(os.path.join(env_dir, 'local_pkgs'))
  131. os.mkdir(os.path.join(env_dir, 'packages'))
  132. os.mkdir(os.path.join(env_dir, 'tools'))
  133. kconfig = open(os.path.join(env_dir, 'packages', 'Kconfig'), 'w')
  134. kconfig.close()
  135. if not os.path.exists(os.path.join(env_dir, 'packages', 'packages')):
  136. try:
  137. ret = os.system('git clone %s %s' % (package_url, os.path.join(env_dir, 'packages', 'packages')))
  138. if ret != 0:
  139. shutil.rmtree(os.path.join(env_dir, 'packages', 'packages'))
  140. print("********************************************************************************\n"
  141. "* Warnning:\n"
  142. "* Run command error for \"git clone https://github.com/RT-Thread/packages.git\".\n"
  143. "* This error may have been caused by not found a git tool or network error.\n"
  144. "* If the git tool is not installed, install the git tool first.\n"
  145. "* If the git utility is installed, check whether the git command is added to \n"
  146. "* the system PATH.\n"
  147. "* This error may cause the RT-Thread packages to not work properly.\n"
  148. "********************************************************************************\n")
  149. help_info()
  150. else:
  151. kconfig = open(os.path.join(env_dir, 'packages', 'Kconfig'), 'w')
  152. kconfig.write('source "$PKGS_DIR/packages/Kconfig"')
  153. kconfig.close()
  154. except:
  155. print("**********************************************************************************\n"
  156. "* Warnning:\n"
  157. "* Run command error for \"git clone https://github.com/RT-Thread/packages.git\". \n"
  158. "* This error may have been caused by not found a git tool or git tool not in \n"
  159. "* the system PATH. \n"
  160. "* This error may cause the RT-Thread packages to not work properly. \n"
  161. "**********************************************************************************\n")
  162. help_info()
  163. if not os.path.exists(os.path.join(env_dir, 'tools', 'scripts')):
  164. try:
  165. ret = os.system('git clone https://github.com/RT-Thread/env.git %s' % os.path.join(env_dir, 'tools', 'scripts'))
  166. if ret != 0:
  167. shutil.rmtree(os.path.join(env_dir, 'tools', 'scripts'))
  168. print("********************************************************************************\n"
  169. "* Warnning:\n"
  170. "* Run command error for \"git clone https://github.com/RT-Thread/env.git\".\n"
  171. "* This error may have been caused by not found a git tool or network error.\n"
  172. "* If the git tool is not installed, install the git tool first.\n"
  173. "* If the git utility is installed, check whether the git command is added \n"
  174. "* to the system PATH.\n"
  175. "* This error may cause script tools to fail to work properly.\n"
  176. "********************************************************************************\n")
  177. help_info()
  178. except:
  179. print("********************************************************************************\n"
  180. "* Warnning:\n"
  181. "* Run command error for \"git clone https://github.com/RT-Thread/env.git\". \n"
  182. "* This error may have been caused by not found a git tool or git tool not in \n"
  183. "* the system PATH. \n"
  184. "* This error may cause script tools to fail to work properly. \n"
  185. "********************************************************************************\n")
  186. help_info()
  187. if sys.platform != 'win32':
  188. env_sh = open(os.path.join(env_dir, 'env.sh'), 'w')
  189. env_sh.write('export PATH=~/.env/tools/scripts:$PATH')
  190. else:
  191. if os.path.exists(os.path.join(env_dir, 'tools', 'scripts')):
  192. os.environ["PATH"] = os.path.join(env_dir, 'tools', 'scripts') + ';' + os.environ["PATH"]
  193. # Exclude utestcases
  194. def exclude_utestcases(RTT_ROOT):
  195. if os.path.isfile(os.path.join(RTT_ROOT, 'examples/utest/testcases/Kconfig')):
  196. return
  197. if not os.path.isfile(os.path.join(RTT_ROOT, 'Kconfig')):
  198. return
  199. with open(os.path.join(RTT_ROOT, 'Kconfig'), 'r') as f:
  200. data = f.readlines()
  201. with open(os.path.join(RTT_ROOT, 'Kconfig'), 'w') as f:
  202. for line in data:
  203. if line.find('examples/utest/testcases/Kconfig') == -1:
  204. f.write(line)
  205. # menuconfig for Linux
  206. def menuconfig(RTT_ROOT):
  207. # Exclude utestcases
  208. exclude_utestcases(RTT_ROOT)
  209. kconfig_dir = os.path.join(RTT_ROOT, 'tools', 'kconfig-frontends')
  210. os.system('scons -C ' + kconfig_dir)
  211. touch_env()
  212. env_dir = get_env_dir()
  213. if isinstance(env_dir, str):
  214. os.environ['PKGS_ROOT'] = os.path.join(env_dir, 'packages')
  215. fn = '.config'
  216. fn_old = '.config.old'
  217. kconfig_cmd = os.path.join(RTT_ROOT, 'tools', 'kconfig-frontends', 'kconfig-mconf')
  218. os.system(kconfig_cmd + ' Kconfig')
  219. if os.path.isfile(fn):
  220. if os.path.isfile(fn_old):
  221. diff_eq = operator.eq(get_file_md5(fn), get_file_md5(fn_old))
  222. else:
  223. diff_eq = False
  224. else:
  225. sys.exit(-1)
  226. # make rtconfig.h
  227. if diff_eq == False:
  228. shutil.copyfile(fn, fn_old)
  229. mk_rtconfig(fn)
  230. # guiconfig for windows and linux
  231. def guiconfig(RTT_ROOT):
  232. import pyguiconfig
  233. # Exclude utestcases
  234. exclude_utestcases(RTT_ROOT)
  235. if sys.platform != 'win32':
  236. touch_env()
  237. env_dir = get_env_dir()
  238. if isinstance(env_dir, str):
  239. os.environ['PKGS_ROOT'] = os.path.join(env_dir, 'packages')
  240. fn = '.config'
  241. fn_old = '.config.old'
  242. sys.argv = ['guiconfig', 'Kconfig']
  243. pyguiconfig._main()
  244. if os.path.isfile(fn):
  245. if os.path.isfile(fn_old):
  246. diff_eq = operator.eq(get_file_md5(fn), get_file_md5(fn_old))
  247. else:
  248. diff_eq = False
  249. else:
  250. sys.exit(-1)
  251. # make rtconfig.h
  252. if diff_eq == False:
  253. shutil.copyfile(fn, fn_old)
  254. mk_rtconfig(fn)
  255. # guiconfig for windows and linux
  256. def guiconfig_silent(RTT_ROOT):
  257. import defconfig
  258. # Exclude utestcases
  259. exclude_utestcases(RTT_ROOT)
  260. if sys.platform != 'win32':
  261. touch_env()
  262. env_dir = get_env_dir()
  263. if isinstance(env_dir, str):
  264. os.environ['PKGS_ROOT'] = os.path.join(env_dir, 'packages')
  265. fn = '.config'
  266. sys.argv = ['defconfig', '--kconfig', 'Kconfig', '.config']
  267. defconfig.main()
  268. # silent mode, force to make rtconfig.h
  269. mk_rtconfig(fn)