menu.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. cc_header = '''/**
  49. * RT-Thread RuiChing
  50. *
  51. * COPYRIGHT (C) 2024-2025 Shanghai Real-Thread Electronic Technology Co., Ltd.
  52. * All rights reserved.
  53. *
  54. * The license and distribution terms for this file may be
  55. * found in the file LICENSE in this distribution.
  56. */
  57. '''
  58. rtconfig.write(cc_header)
  59. rtconfig.write('#ifndef __RTCONFIG_APP_H__\n')
  60. rtconfig.write('#define __RTCONFIG_APP_H__\n\n')
  61. rtconfig.write('/* Automatically generated file; DO NOT EDIT. */\n')
  62. rtconfig.write('#include <kconfig.h>\n\n')
  63. empty_line = 1
  64. for line in config:
  65. line = line.lstrip(' ').replace('\n', '').replace('\r', '')
  66. if len(line) == 0:
  67. continue
  68. if line[0] == '#':
  69. if len(line) == 1:
  70. if empty_line:
  71. continue
  72. rtconfig.write('\n')
  73. empty_line = 1
  74. continue
  75. if line.startswith('# CONFIG_'):
  76. line = ' ' + line[9:]
  77. else:
  78. line = line[1:]
  79. rtconfig.write('/*%s */\n' % line)
  80. empty_line = 0
  81. else:
  82. empty_line = 0
  83. setting = line.split('=')
  84. if len(setting) >= 2:
  85. if setting[0].startswith('CONFIG_'):
  86. setting[0] = setting[0][7:]
  87. # remove CONFIG_PKG_XX_PATH or CONFIG_PKG_XX_VER
  88. if is_pkg_special_config(setting[0]):
  89. continue
  90. if setting[1] == 'y':
  91. rtconfig.write('#define %s\n' % setting[0])
  92. else:
  93. rtconfig.write('#define %s %s\n' % (setting[0], re.findall(r"^.*?=(.*)$",line)[0]))
  94. if os.path.isfile('rtconfig_project.h'):
  95. rtconfig.write('#include "rtconfig_project.h"\n')
  96. rtconfig.write('\n')
  97. rtconfig.write('#endif /* __RTCONFIG_APP_H__ */ \n')
  98. rtconfig.close()
  99. def get_file_md5(file):
  100. MD5 = hashlib.new('md5')
  101. with open(file, 'r') as fp:
  102. MD5.update(fp.read().encode('utf8'))
  103. fp_md5 = MD5.hexdigest()
  104. return fp_md5
  105. def config():
  106. mk_rtconfig('.config')
  107. def get_env_dir():
  108. if os.environ.get('ENV_ROOT'):
  109. return os.environ.get('ENV_ROOT')
  110. if sys.platform == 'win32':
  111. home_dir = os.environ['USERPROFILE']
  112. env_dir = os.path.join(home_dir, '.env')
  113. else:
  114. home_dir = os.environ['HOME']
  115. env_dir = os.path.join(home_dir, '.env')
  116. if not os.path.exists(env_dir):
  117. return None
  118. return env_dir
  119. def help_info():
  120. print("**********************************************************************************\n"
  121. "* Help infomation:\n"
  122. "* Git tool install step.\n"
  123. "* If your system is linux, you can use command below to install git.\n"
  124. "* $ sudo yum install git\n"
  125. "* $ sudo apt-get install git\n"
  126. "* If your system is windows, you should download git software(msysGit).\n"
  127. "* Download path: http://git-scm.com/download/win\n"
  128. "* After you install it, be sure to add the git command execution PATH \n"
  129. "* to your system PATH.\n"
  130. "* Usually, git command PATH is $YOUR_INSTALL_DIR\\Git\\bin\n"
  131. "* If your system is OSX, please download git and install it.\n"
  132. "* Download path: http://git-scm.com/download/mac\n"
  133. "**********************************************************************************\n")
  134. def touch_env():
  135. if sys.platform != 'win32':
  136. home_dir = os.environ['HOME']
  137. else:
  138. home_dir = os.environ['USERPROFILE']
  139. package_url = os.getenv('RTT_PACKAGE_URL') or DEFAULT_RTT_PACKAGE_URL
  140. env_dir = os.path.join(home_dir, '.env')
  141. if not os.path.exists(env_dir):
  142. os.mkdir(env_dir)
  143. os.mkdir(os.path.join(env_dir, 'local_pkgs'))
  144. os.mkdir(os.path.join(env_dir, 'packages'))
  145. os.mkdir(os.path.join(env_dir, 'tools'))
  146. kconfig = open(os.path.join(env_dir, 'packages', 'Kconfig'), 'w')
  147. kconfig.close()
  148. if not os.path.exists(os.path.join(env_dir, 'packages', 'packages')):
  149. try:
  150. ret = os.system('git clone %s %s' % (package_url, os.path.join(env_dir, 'packages', 'packages')))
  151. if ret != 0:
  152. shutil.rmtree(os.path.join(env_dir, 'packages', 'packages'))
  153. print("********************************************************************************\n"
  154. "* Warnning:\n"
  155. "* Run command error for \"git clone https://github.com/RT-Thread/packages.git\".\n"
  156. "* This error may have been caused by not found a git tool or network error.\n"
  157. "* If the git tool is not installed, install the git tool first.\n"
  158. "* If the git utility is installed, check whether the git command is added to \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. else:
  164. kconfig = open(os.path.join(env_dir, 'packages', 'Kconfig'), 'w')
  165. kconfig.write('source "$PKGS_DIR/packages/Kconfig"')
  166. kconfig.close()
  167. except:
  168. print("**********************************************************************************\n"
  169. "* Warnning:\n"
  170. "* Run command error for \"git clone https://github.com/RT-Thread/packages.git\". \n"
  171. "* This error may have been caused by not found a git tool or git tool not in \n"
  172. "* the system PATH. \n"
  173. "* This error may cause the RT-Thread packages to not work properly. \n"
  174. "**********************************************************************************\n")
  175. help_info()
  176. if not os.path.exists(os.path.join(env_dir, 'tools', 'scripts')):
  177. try:
  178. ret = os.system('git clone https://github.com/RT-Thread/env.git %s' % os.path.join(env_dir, 'tools', 'scripts'))
  179. if ret != 0:
  180. shutil.rmtree(os.path.join(env_dir, 'tools', 'scripts'))
  181. print("********************************************************************************\n"
  182. "* Warnning:\n"
  183. "* Run command error for \"git clone https://github.com/RT-Thread/env.git\".\n"
  184. "* This error may have been caused by not found a git tool or network error.\n"
  185. "* If the git tool is not installed, install the git tool first.\n"
  186. "* If the git utility is installed, check whether the git command is added \n"
  187. "* to the system PATH.\n"
  188. "* This error may cause script tools to fail to work properly.\n"
  189. "********************************************************************************\n")
  190. help_info()
  191. except:
  192. print("********************************************************************************\n"
  193. "* Warnning:\n"
  194. "* Run command error for \"git clone https://github.com/RT-Thread/env.git\". \n"
  195. "* This error may have been caused by not found a git tool or git tool not in \n"
  196. "* the system PATH. \n"
  197. "* This error may cause script tools to fail to work properly. \n"
  198. "********************************************************************************\n")
  199. help_info()
  200. if sys.platform != 'win32':
  201. env_sh = open(os.path.join(env_dir, 'env.sh'), 'w')
  202. env_sh.write('export PATH=~/.env/tools/scripts:$PATH')
  203. # if fish config exists, generate env.fish
  204. if os.path.exists(os.path.join(home_dir, '.config', 'fish', 'config.fish')):
  205. env_fish = open(os.path.join(env_dir, 'env.fish'), 'w')
  206. env_fish.write('set -gx PATH ~/.env/tools/scripts $PATH')
  207. else:
  208. if os.path.exists(os.path.join(env_dir, 'tools', 'scripts')):
  209. os.environ["PATH"] = os.path.join(env_dir, 'tools', 'scripts') + ';' + os.environ["PATH"]
  210. # Exclude utestcases
  211. def exclude_utestcases(RTT_ROOT):
  212. if os.path.isfile(os.path.join(RTT_ROOT, 'examples/utest/testcases/Kconfig')):
  213. return
  214. if not os.path.isfile(os.path.join(RTT_ROOT, 'Kconfig')):
  215. return
  216. with open(os.path.join(RTT_ROOT, 'Kconfig'), 'r') as f:
  217. data = f.readlines()
  218. with open(os.path.join(RTT_ROOT, 'Kconfig'), 'w') as f:
  219. for line in data:
  220. if line.find('examples/utest/testcases/Kconfig') == -1:
  221. f.write(line)
  222. def menu(RTT_ROOT):
  223. # sys.path.append(os.path.join(RTT_ROOT, 'tools', 'Kconfiglib'))
  224. sys.path.append(os.path.join(RTT_ROOT, 'scripts', 'Kconfiglib'))
  225. import kconfiglib
  226. from menuconfig import menuconfig
  227. # os.environ["MENUCONFIG_STYLE"] = "default selection=fg:white,bg:blue"
  228. os.environ["KCONFIG_CONFIG"] = os.path.join(".config")
  229. # os.environ["KCONFIG_CONFIG_HEADER"] = "# Generated by My Kconfig Demo"
  230. os.environ["KCONFIG_AUTOHEADER"] = os.path.join("config.h")
  231. os.environ["CONFIG_"] = "CONFIG_"
  232. # Exclude utestcases
  233. exclude_utestcases(RTT_ROOT)
  234. if sys.platform != 'win32':
  235. touch_env()
  236. env_dir = get_env_dir()
  237. if isinstance(env_dir, str):
  238. os.environ['PKGS_DIR'] = os.path.join(env_dir, 'packages')
  239. os.environ['RTT_DIR'] = RTT_ROOT
  240. os.environ['ROOT_SDK'] = RTT_ROOT
  241. fn = '.config'
  242. fn_old = '.config.old'
  243. kconf = kconfiglib.Kconfig('Kconfig')
  244. if os.path.exists(fn):
  245. print(fn)
  246. kconf.load_config(fn)
  247. menuconfig(kconf)
  248. if os.path.isfile(fn):
  249. if os.path.isfile(fn_old):
  250. diff_eq = operator.eq(get_file_md5(fn), get_file_md5(fn_old))
  251. else:
  252. diff_eq = False
  253. else:
  254. sys.exit(-1)
  255. # make rtconfig.h
  256. if diff_eq == False:
  257. shutil.copyfile(fn, fn_old)
  258. mk_rtconfig(fn)
  259. # guiconfig for windows and linux
  260. def guiconfig(RTT_ROOT):
  261. sys.path.append(os.path.join(RTT_ROOT, 'tools', 'Kconfiglib'))
  262. import guiconfig
  263. # Exclude utestcases
  264. exclude_utestcases(RTT_ROOT)
  265. if sys.platform != 'win32':
  266. touch_env()
  267. env_dir = get_env_dir()
  268. if isinstance(env_dir, str):
  269. os.environ['PKGS_DIR'] = os.path.join(env_dir, 'packages')
  270. os.environ['RTT_DIR'] = RTT_ROOT
  271. fn = '.config'
  272. fn_old = '.config.old'
  273. sys.argv = ['guiconfig', 'Kconfig']
  274. guiconfig._main()
  275. if os.path.isfile(fn):
  276. if os.path.isfile(fn_old):
  277. diff_eq = operator.eq(get_file_md5(fn), get_file_md5(fn_old))
  278. else:
  279. diff_eq = False
  280. else:
  281. sys.exit(-1)
  282. # make rtconfig.h
  283. if diff_eq == False:
  284. shutil.copyfile(fn, fn_old)
  285. mk_rtconfig(fn)
  286. # guiconfig for windows and linux
  287. def guiconfig_silent(RTT_ROOT):
  288. sys.path.append(os.path.join(RTT_ROOT, 'tools', 'Kconfiglib'))
  289. import defconfig
  290. # Exclude utestcases
  291. exclude_utestcases(RTT_ROOT)
  292. if sys.platform != 'win32':
  293. touch_env()
  294. env_dir = get_env_dir()
  295. if isinstance(env_dir, str):
  296. os.environ['PKGS_DIR'] = os.path.join(env_dir, 'packages')
  297. os.environ['RTT_DIR'] = RTT_ROOT
  298. fn = '.config'
  299. sys.argv = ['defconfig', '--kconfig', 'Kconfig', '.config']
  300. defconfig.main()
  301. # silent mode, force to make rtconfig.h
  302. mk_rtconfig(fn)
  303. def main():
  304. if len(sys.argv) > 1:
  305. option = sys.argv[1]
  306. else:
  307. option = "../"
  308. menu(option)
  309. if __name__ == "__main__":
  310. main()