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 25import os 26import sys 27import shutil 28 29# make rtconfig.h from .config 30 31def mk_rtconfig(filename): 32 try: 33 config = open(filename, 'r') 34 except: 35 print('open config:%s failed' % filename) 36 return 37 38 rtconfig = open('rtconfig.h', 'w') 39 rtconfig.write('#ifndef RT_CONFIG_H__\n') 40 rtconfig.write('#define RT_CONFIG_H__\n\n') 41 42 empty_line = 1 43 44 for line in config: 45 line = line.lstrip(' ').replace('\n', '').replace('\r', '') 46 47 if len(line) == 0: continue 48 49 if line[0] == '#': 50 if len(line) == 1: 51 if empty_line: 52 continue 53 54 rtconfig.write('\n') 55 empty_line = 1 56 continue 57 58 comment_line = line[1:] 59 if line.startswith('# CONFIG_'): line = ' ' + line[9:] 60 else: line = line[1:] 61 62 rtconfig.write('/*%s */\n' % line) 63 empty_line = 0 64 else: 65 empty_line = 0 66 setting = line.split('=') 67 if len(setting) >= 2: 68 if setting[0].startswith('CONFIG_'): 69 setting[0] = setting[0][7:] 70 71 # remove CONFIG_PKG_XX_PATH or CONFIG_PKG_XX_VER 72 if type(setting[0]) == type('a') and (setting[0].endswith('_PATH') or setting[0].endswith('_VER')): 73 continue 74 75 if setting[1] == 'y': 76 rtconfig.write('#define %s\n' % setting[0]) 77 else: 78 rtconfig.write('#define %s %s\n' % (setting[0], setting[1])) 79 80 if os.path.isfile('rtconfig_project.h'): 81 rtconfig.write('#include "rtconfig_project.h"\n') 82 83 rtconfig.write('\n') 84 rtconfig.write('#endif\n') 85 rtconfig.close() 86 87def config(): 88 mk_rtconfig('.config') 89 90def get_env_dir(): 91 if os.environ.get('ENV_ROOT'): 92 return os.environ.get('ENV_ROOT') 93 94 if sys.platform == 'win32': 95 home_dir = os.environ['USERPROFILE'] 96 env_dir = os.path.join(home_dir, '.env') 97 else: 98 home_dir = os.environ['HOME'] 99 env_dir = os.path.join(home_dir, '.env') 100 101 if not os.path.exists(env_dir): 102 return None 103 104 return env_dir 105 106def 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 122def touch_env(): 123 if sys.platform != 'win32': 124 home_dir = os.environ['HOME'] 125 else: 126 home_dir = os.environ['USERPROFILE'] 127 128 env_dir = os.path.join(home_dir, '.env') 129 if not os.path.exists(env_dir): 130 os.mkdir(env_dir) 131 os.mkdir(os.path.join(env_dir, 'local_pkgs')) 132 os.mkdir(os.path.join(env_dir, 'packages')) 133 os.mkdir(os.path.join(env_dir, 'tools')) 134 kconfig = open(os.path.join(env_dir, 'packages', 'Kconfig'), 'w') 135 kconfig.close() 136 137 if not os.path.exists(os.path.join(env_dir, 'packages', 'packages')): 138 try: 139 ret = os.system('git clone https://github.com/RT-Thread/packages.git %s' % os.path.join(env_dir, 'packages', 'packages')) 140 if ret != 0: 141 shutil.rmtree(os.path.join(env_dir, 'packages', 'packages')) 142 print("********************************************************************************\n" 143 "* Warnning:\n" 144 "* Run command error for \"git clone https://github.com/RT-Thread/packages.git\".\n" 145 "* This error may have been caused by not found a git tool or network error.\n" 146 "* If the git tool is not installed, install the git tool first.\n" 147 "* If the git utility is installed, check whether the git command is added to \n" 148 "* the system PATH.\n" 149 "* This error may cause the RT-Thread packages to not work properly.\n" 150 "********************************************************************************\n") 151 help_info() 152 else: 153 kconfig = open(os.path.join(env_dir, 'packages', 'Kconfig'), 'w') 154 kconfig.write('source "$PKGS_DIR/packages/Kconfig"') 155 kconfig.close() 156 except: 157 print("**********************************************************************************\n" 158 "* Warnning:\n" 159 "* Run command error for \"git clone https://github.com/RT-Thread/packages.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 the RT-Thread packages to not work properly. \n" 163 "**********************************************************************************\n") 164 help_info() 165 166 if not os.path.exists(os.path.join(env_dir, 'tools', 'scripts')): 167 try: 168 ret = os.system('git clone https://github.com/RT-Thread/env.git %s' % os.path.join(env_dir, 'tools', 'scripts')) 169 if ret != 0: 170 shutil.rmtree(os.path.join(env_dir, 'tools', 'scripts')) 171 print("********************************************************************************\n" 172 "* Warnning:\n" 173 "* Run command error for \"git clone https://github.com/RT-Thread/env.git\".\n" 174 "* This error may have been caused by not found a git tool or network error.\n" 175 "* If the git tool is not installed, install the git tool first.\n" 176 "* If the git utility is installed, check whether the git command is added \n" 177 "* to the system PATH.\n" 178 "* This error may cause script tools to fail to work properly.\n" 179 "********************************************************************************\n") 180 help_info() 181 except: 182 print("********************************************************************************\n" 183 "* Warnning:\n" 184 "* Run command error for \"git clone https://github.com/RT-Thread/env.git\". \n" 185 "* This error may have been caused by not found a git tool or git tool not in \n" 186 "* the system PATH. \n" 187 "* This error may cause script tools to fail to work properly. \n" 188 "********************************************************************************\n") 189 help_info() 190 191 if sys.platform != 'win32': 192 env_sh = open(os.path.join(env_dir, 'env.sh'), 'w') 193 env_sh.write('export 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 198# menuconfig for Linux 199def menuconfig(RTT_ROOT): 200 kconfig_dir = os.path.join(RTT_ROOT, 'tools', 'kconfig-frontends') 201 os.system('scons -C ' + kconfig_dir) 202 203 touch_env() 204 env_dir = get_env_dir() 205 206 os.environ['PKGS_ROOT'] = os.path.join(env_dir, 'packages') 207 208 fn = '.config' 209 210 if os.path.isfile(fn): 211 mtime = os.path.getmtime(fn) 212 else: 213 mtime = -1 214 215 kconfig_cmd = os.path.join(RTT_ROOT, 'tools', 'kconfig-frontends', 'kconfig-mconf') 216 os.system(kconfig_cmd + ' Kconfig') 217 218 if os.path.isfile(fn): 219 mtime2 = os.path.getmtime(fn) 220 else: 221 mtime2 = -1 222 223 # make rtconfig.h 224 if mtime != mtime2: 225 mk_rtconfig(fn) 226 227# pyconfig for windows and linux 228def pyconfig(RTT_ROOT): 229 import pymenuconfig 230 231 touch_env() 232 env_dir = get_env_dir() 233 234 os.environ['PKGS_ROOT'] = os.path.join(env_dir, 'packages') 235 236 fn = '.config' 237 238 if os.path.isfile(fn): 239 mtime = os.path.getmtime(fn) 240 else: 241 mtime = -1 242 243 pymenuconfig.main(['--kconfig', 'Kconfig', '--config', '.config']) 244 245 if os.path.isfile(fn): 246 mtime2 = os.path.getmtime(fn) 247 else: 248 mtime2 = -1 249 250 # make rtconfig.h 251 if mtime != mtime2: 252 mk_rtconfig(fn) 253 254 255# pyconfig_silent for windows and linux 256def pyconfig_silent(RTT_ROOT): 257 import pymenuconfig 258 print("In pyconfig silent mode. Don`t display menuconfig window.") 259 260 touch_env() 261 env_dir = get_env_dir() 262 263 os.environ['PKGS_ROOT'] = os.path.join(env_dir, 'packages') 264 265 fn = '.config' 266 267 pymenuconfig.main(['--kconfig', 'Kconfig', '--config', '.config', '--silent', 'True']) 268 269 # silent mode, force to make rtconfig.h 270 mk_rtconfig(fn) 271