menukconfig.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. # test kconfiglib is installed
  32. try:
  33. import kconfiglib
  34. except ImportError as e:
  35. print("Failed to import kconfiglib: " + str(e))
  36. print("")
  37. print("You may need to install it using:")
  38. print(" pip install kconfiglib")
  39. print("")
  40. sys.exit(1)
  41. DEFAULT_RTT_PACKAGE_URL = 'https://github.com/RT-Thread/packages.git'
  42. # you can change the package url by defining RTT_PACKAGE_URL, ex:
  43. # export RTT_PACKAGE_URL=https://github.com/Varanda-Labs/packages.git
  44. # make rtconfig.h from .config
  45. def is_pkg_special_config(config_str):
  46. ''' judge if it's CONFIG_PKG_XX_PATH or CONFIG_PKG_XX_VER'''
  47. if type(config_str) == type('a'):
  48. if config_str.startswith("PKG_") and (config_str.endswith('_PATH') or config_str.endswith('_VER')):
  49. return True
  50. return False
  51. def mk_rtconfig(filename):
  52. try:
  53. config = open(filename, 'r')
  54. except:
  55. print('open config:%s failed' % filename)
  56. return
  57. rtconfig = open('rtconfig.h', 'w')
  58. rtconfig.write('#ifndef RT_CONFIG_H__\n')
  59. rtconfig.write('#define RT_CONFIG_H__\n\n')
  60. empty_line = 1
  61. for line in config:
  62. line = line.lstrip(' ').replace('\n', '').replace('\r', '')
  63. if len(line) == 0:
  64. continue
  65. if line[0] == '#':
  66. if len(line) == 1:
  67. if empty_line:
  68. continue
  69. rtconfig.write('\n')
  70. empty_line = 1
  71. continue
  72. if line.startswith('# CONFIG_'):
  73. line = ' ' + line[9:]
  74. else:
  75. line = line[1:]
  76. rtconfig.write('/*%s */\n' % line)
  77. empty_line = 0
  78. else:
  79. empty_line = 0
  80. setting = line.split('=')
  81. if len(setting) >= 2:
  82. if setting[0].startswith('CONFIG_'):
  83. setting[0] = setting[0][7:]
  84. # remove CONFIG_PKG_XX_PATH or CONFIG_PKG_XX_VER
  85. if is_pkg_special_config(setting[0]):
  86. continue
  87. if setting[1] == 'y':
  88. rtconfig.write('#define %s\n' % setting[0])
  89. else:
  90. rtconfig.write('#define %s %s\n' % (setting[0], re.findall(r"^.*?=(.*)$",line)[0]))
  91. if os.path.isfile('rtconfig_project.h'):
  92. rtconfig.write('#include "rtconfig_project.h"\n')
  93. rtconfig.write('\n')
  94. rtconfig.write('#endif\n')
  95. rtconfig.close()
  96. def get_file_md5(file):
  97. MD5 = hashlib.new('md5')
  98. with open(file, 'r') as fp:
  99. MD5.update(fp.read().encode('utf8'))
  100. fp_md5 = MD5.hexdigest()
  101. return fp_md5
  102. def config():
  103. mk_rtconfig('.config')
  104. def get_env_dir():
  105. if os.environ.get('ENV_ROOT'):
  106. return os.environ.get('ENV_ROOT')
  107. if sys.platform == 'win32':
  108. home_dir = os.environ['USERPROFILE']
  109. env_dir = os.path.join(home_dir, '.env')
  110. else:
  111. home_dir = os.environ['HOME']
  112. env_dir = os.path.join(home_dir, '.env')
  113. if not os.path.exists(env_dir):
  114. return None
  115. return env_dir
  116. def help_info():
  117. print("**********************************************************************************\n"
  118. "* Help infomation:\n"
  119. "* Git tool install step.\n"
  120. "* If your system is linux, you can use command below to install git.\n"
  121. "* $ sudo yum install git\n"
  122. "* $ sudo apt-get install git\n"
  123. "* If your system is windows, you should download git software(msysGit).\n"
  124. "* Download path: http://git-scm.com/download/win\n"
  125. "* After you install it, be sure to add the git command execution PATH \n"
  126. "* to your system PATH.\n"
  127. "* Usually, git command PATH is $YOUR_INSTALL_DIR\\Git\\bin\n"
  128. "* If your system is OSX, please download git and install it.\n"
  129. "* Download path: http://git-scm.com/download/mac\n"
  130. "**********************************************************************************\n")
  131. def touch_env():
  132. if sys.platform != 'win32':
  133. home_dir = os.environ['HOME']
  134. else:
  135. home_dir = os.environ['USERPROFILE']
  136. package_url = os.getenv('RTT_PACKAGE_URL') or DEFAULT_RTT_PACKAGE_URL
  137. env_dir = os.path.join(home_dir, '.env')
  138. if not os.path.exists(env_dir):
  139. os.mkdir(env_dir)
  140. os.mkdir(os.path.join(env_dir, 'local_pkgs'))
  141. os.mkdir(os.path.join(env_dir, 'packages'))
  142. os.mkdir(os.path.join(env_dir, 'tools'))
  143. kconfig = open(os.path.join(env_dir, 'packages', 'Kconfig'), 'w')
  144. kconfig.close()
  145. if not os.path.exists(os.path.join(env_dir, 'packages', 'packages')):
  146. try:
  147. ret = os.system('git clone %s %s' % (package_url, os.path.join(env_dir, 'packages', 'packages')))
  148. if ret != 0:
  149. shutil.rmtree(os.path.join(env_dir, 'packages', 'packages'))
  150. print("********************************************************************************\n"
  151. "* Warnning:\n"
  152. "* Run command error for \"git clone https://github.com/RT-Thread/packages.git\".\n"
  153. "* This error may have been caused by not found a git tool or network error.\n"
  154. "* If the git tool is not installed, install the git tool first.\n"
  155. "* If the git utility is installed, check whether the git command is added to \n"
  156. "* the system PATH.\n"
  157. "* This error may cause the RT-Thread packages to not work properly.\n"
  158. "********************************************************************************\n")
  159. help_info()
  160. else:
  161. kconfig = open(os.path.join(env_dir, 'packages', 'Kconfig'), 'w')
  162. kconfig.write('source "$PKGS_DIR/packages/Kconfig"')
  163. kconfig.close()
  164. except:
  165. print("**********************************************************************************\n"
  166. "* Warnning:\n"
  167. "* Run command error for \"git clone https://github.com/RT-Thread/packages.git\". \n"
  168. "* This error may have been caused by not found a git tool or git tool not in \n"
  169. "* the system PATH. \n"
  170. "* This error may cause the RT-Thread packages to not work properly. \n"
  171. "**********************************************************************************\n")
  172. help_info()
  173. if not os.path.exists(os.path.join(env_dir, 'tools', 'scripts')):
  174. try:
  175. ret = os.system('git clone https://github.com/RT-Thread/env.git %s' % os.path.join(env_dir, 'tools', 'scripts'))
  176. if ret != 0:
  177. shutil.rmtree(os.path.join(env_dir, 'tools', 'scripts'))
  178. print("********************************************************************************\n"
  179. "* Warnning:\n"
  180. "* Run command error for \"git clone https://github.com/RT-Thread/env.git\".\n"
  181. "* This error may have been caused by not found a git tool or network error.\n"
  182. "* If the git tool is not installed, install the git tool first.\n"
  183. "* If the git utility is installed, check whether the git command is added \n"
  184. "* to the system PATH.\n"
  185. "* This error may cause script tools to fail to work properly.\n"
  186. "********************************************************************************\n")
  187. help_info()
  188. except:
  189. print("********************************************************************************\n"
  190. "* Warnning:\n"
  191. "* Run command error for \"git clone https://github.com/RT-Thread/env.git\". \n"
  192. "* This error may have been caused by not found a git tool or git tool not in \n"
  193. "* the system PATH. \n"
  194. "* This error may cause script tools to fail to work properly. \n"
  195. "********************************************************************************\n")
  196. help_info()
  197. if sys.platform != 'win32':
  198. env_sh = open(os.path.join(env_dir, 'env.sh'), 'w')
  199. env_sh.write('export PATH=~/.env/tools/scripts:$PATH')
  200. # if fish config exists, generate env.fish
  201. if os.path.exists(os.path.join(home_dir, '.config', 'fish', 'config.fish')):
  202. env_fish = open(os.path.join(env_dir, 'env.fish'), 'w')
  203. env_fish.write('set -gx PATH ~/.env/tools/scripts $PATH')
  204. else:
  205. if os.path.exists(os.path.join(env_dir, 'tools', 'scripts')):
  206. os.environ["PATH"] = os.path.join(env_dir, 'tools', 'scripts') + ';' + os.environ["PATH"]
  207. # Exclude utestcases
  208. def exclude_utestcases(RTT_ROOT):
  209. if os.path.isfile(os.path.join(RTT_ROOT, 'examples/utest/testcases/Kconfig')):
  210. return
  211. if not os.path.isfile(os.path.join(RTT_ROOT, 'Kconfig')):
  212. return
  213. with open(os.path.join(RTT_ROOT, 'Kconfig'), 'r') as f:
  214. data = f.readlines()
  215. with open(os.path.join(RTT_ROOT, 'Kconfig'), 'w') as f:
  216. for line in data:
  217. if line.find('examples/utest/testcases/Kconfig') == -1:
  218. f.write(line)
  219. # fix locale for kconfiglib
  220. def kconfiglib_fix_locale():
  221. import os
  222. import locale
  223. # Get the list of supported locales
  224. supported_locales = set(locale.locale_alias.keys())
  225. # Check if LANG is set and its value is not in the supported locales
  226. if 'LANG' in os.environ and os.environ['LANG'] not in supported_locales:
  227. os.environ['LANG'] = 'C'
  228. # menuconfig for Linux and Windows
  229. def menuconfig(RTT_ROOT):
  230. import menuconfig
  231. # Exclude utestcases
  232. exclude_utestcases(RTT_ROOT)
  233. if sys.platform != 'win32':
  234. touch_env()
  235. env_dir = get_env_dir()
  236. if isinstance(env_dir, str):
  237. os.environ['PKGS_ROOT'] = os.path.join(env_dir, 'packages')
  238. fn = '.config'
  239. fn_old = '.config.old'
  240. sys.argv = ['menuconfig', 'Kconfig']
  241. # fix vscode console
  242. kconfiglib_fix_locale()
  243. menuconfig._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(RTT_ROOT):
  257. import guiconfig
  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. fn_old = '.config.old'
  267. sys.argv = ['guiconfig', 'Kconfig']
  268. guiconfig._main()
  269. if os.path.isfile(fn):
  270. if os.path.isfile(fn_old):
  271. diff_eq = operator.eq(get_file_md5(fn), get_file_md5(fn_old))
  272. else:
  273. diff_eq = False
  274. else:
  275. sys.exit(-1)
  276. # make rtconfig.h
  277. if diff_eq == False:
  278. shutil.copyfile(fn, fn_old)
  279. mk_rtconfig(fn)
  280. # guiconfig for windows and linux
  281. def guiconfig_silent(RTT_ROOT):
  282. import defconfig
  283. # Exclude utestcases
  284. exclude_utestcases(RTT_ROOT)
  285. if sys.platform != 'win32':
  286. touch_env()
  287. env_dir = get_env_dir()
  288. if isinstance(env_dir, str):
  289. os.environ['PKGS_ROOT'] = os.path.join(env_dir, 'packages')
  290. fn = '.config'
  291. sys.argv = ['defconfig', '--kconfig', 'Kconfig', '.config']
  292. defconfig.main()
  293. # silent mode, force to make rtconfig.h
  294. mk_rtconfig(fn)
  295. def genconfig() :
  296. from SCons.Script import SCons
  297. PreProcessor = SCons.cpp.PreProcessor()
  298. try:
  299. f = open('rtconfig.h', 'r')
  300. contents = f.read()
  301. f.close()
  302. except :
  303. print("Open rtconfig.h file failed.")
  304. PreProcessor.process_contents(contents)
  305. options = PreProcessor.cpp_namespace
  306. try:
  307. f = open('.config', 'w')
  308. for (opt, value) in options.items():
  309. if type(value) == type(1):
  310. f.write("CONFIG_%s=%d\n" % (opt, value))
  311. if type(value) == type('') and value == '':
  312. f.write("CONFIG_%s=y\n" % opt)
  313. elif type(value) == type('str'):
  314. f.write("CONFIG_%s=%s\n" % (opt, value))
  315. print("Generate .config done!")
  316. f.close()
  317. except:
  318. print("Generate .config file failed.")