menuconfig.py 10 KB

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