menuconfig.py 10 KB

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