menukconfig.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. # if fish config exists, generate env.fish
  191. if os.path.exists(os.path.join(home_dir, '.config', 'fish', 'config.fish')):
  192. env_fish = open(os.path.join(env_dir, 'env.fish'), 'w')
  193. env_fish.write('set -gx PATH ~/.env/tools/scripts $PATH')
  194. else:
  195. if os.path.exists(os.path.join(env_dir, 'tools', 'scripts')):
  196. os.environ["PATH"] = os.path.join(env_dir, 'tools', 'scripts') + ';' + os.environ["PATH"]
  197. # Exclude utestcases
  198. def exclude_utestcases(RTT_ROOT):
  199. if os.path.isfile(os.path.join(RTT_ROOT, 'examples/utest/testcases/Kconfig')):
  200. return
  201. if not os.path.isfile(os.path.join(RTT_ROOT, 'Kconfig')):
  202. return
  203. with open(os.path.join(RTT_ROOT, 'Kconfig'), 'r') as f:
  204. data = f.readlines()
  205. with open(os.path.join(RTT_ROOT, 'Kconfig'), 'w') as f:
  206. for line in data:
  207. if line.find('examples/utest/testcases/Kconfig') == -1:
  208. f.write(line)
  209. # fix locale for kconfiglib
  210. def kconfiglib_fix_locale():
  211. import os
  212. import locale
  213. # Get the list of supported locales
  214. supported_locales = set(locale.locale_alias.keys())
  215. # Check if LANG is set and its value is not in the supported locales
  216. if 'LANG' in os.environ and os.environ['LANG'] not in supported_locales:
  217. os.environ['LANG'] = 'C'
  218. # menuconfig for Linux and Windows
  219. def menuconfig(RTT_ROOT):
  220. import menuconfig
  221. # Exclude utestcases
  222. exclude_utestcases(RTT_ROOT)
  223. if sys.platform != 'win32':
  224. touch_env()
  225. env_dir = get_env_dir()
  226. if isinstance(env_dir, str):
  227. os.environ['PKGS_ROOT'] = os.path.join(env_dir, 'packages')
  228. fn = '.config'
  229. fn_old = '.config.old'
  230. sys.argv = ['menuconfig', 'Kconfig']
  231. # fix vscode console
  232. kconfiglib_fix_locale()
  233. menuconfig._main()
  234. if os.path.isfile(fn):
  235. if os.path.isfile(fn_old):
  236. diff_eq = operator.eq(get_file_md5(fn), get_file_md5(fn_old))
  237. else:
  238. diff_eq = False
  239. else:
  240. sys.exit(-1)
  241. # make rtconfig.h
  242. if diff_eq == False:
  243. shutil.copyfile(fn, fn_old)
  244. mk_rtconfig(fn)
  245. # guiconfig for windows and linux
  246. def guiconfig(RTT_ROOT):
  247. import guiconfig
  248. # Exclude utestcases
  249. exclude_utestcases(RTT_ROOT)
  250. if sys.platform != 'win32':
  251. touch_env()
  252. env_dir = get_env_dir()
  253. if isinstance(env_dir, str):
  254. os.environ['PKGS_ROOT'] = os.path.join(env_dir, 'packages')
  255. fn = '.config'
  256. fn_old = '.config.old'
  257. sys.argv = ['guiconfig', 'Kconfig']
  258. guiconfig._main()
  259. if os.path.isfile(fn):
  260. if os.path.isfile(fn_old):
  261. diff_eq = operator.eq(get_file_md5(fn), get_file_md5(fn_old))
  262. else:
  263. diff_eq = False
  264. else:
  265. sys.exit(-1)
  266. # make rtconfig.h
  267. if diff_eq == False:
  268. shutil.copyfile(fn, fn_old)
  269. mk_rtconfig(fn)
  270. # guiconfig for windows and linux
  271. def guiconfig_silent(RTT_ROOT):
  272. import defconfig
  273. # Exclude utestcases
  274. exclude_utestcases(RTT_ROOT)
  275. if sys.platform != 'win32':
  276. touch_env()
  277. env_dir = get_env_dir()
  278. if isinstance(env_dir, str):
  279. os.environ['PKGS_ROOT'] = os.path.join(env_dir, 'packages')
  280. fn = '.config'
  281. sys.argv = ['defconfig', '--kconfig', 'Kconfig', '.config']
  282. defconfig.main()
  283. # silent mode, force to make rtconfig.h
  284. mk_rtconfig(fn)
  285. def genconfig() :
  286. from SCons.Script import SCons
  287. PreProcessor = SCons.cpp.PreProcessor()
  288. try:
  289. f = open('rtconfig.h', 'r')
  290. contents = f.read()
  291. f.close()
  292. except :
  293. print("Open rtconfig.h file failed.")
  294. PreProcessor.process_contents(contents)
  295. options = PreProcessor.cpp_namespace
  296. try:
  297. f = open('.config', 'w')
  298. for (opt, value) in options.items():
  299. if type(value) == type(1):
  300. f.write("CONFIG_%s=%d\n" % (opt, value))
  301. if type(value) == type('') and value == '':
  302. f.write("CONFIG_%s=y\n" % opt)
  303. elif type(value) == type('str'):
  304. f.write("CONFIG_%s=%s\n" % (opt, value))
  305. print("Generate .config done!")
  306. f.close()
  307. except:
  308. print("Generate .config file failed.")