menuconfig.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. import os
  24. # make rtconfig.h from .config
  25. def mk_rtconfig(filename):
  26. try:
  27. config = file(filename)
  28. except:
  29. print('open config:%s failed' % filename)
  30. return
  31. rtconfig = file('rtconfig.h', 'w')
  32. rtconfig.write('#ifndef RT_CONFIG_H__\n')
  33. rtconfig.write('#define RT_CONFIG_H__\n\n')
  34. empty_line = 1
  35. for line in config:
  36. line = line.lstrip(' ').replace('\n', '').replace('\r', '')
  37. if len(line) == 0: continue
  38. if line[0] == '#':
  39. if len(line) == 1:
  40. if empty_line:
  41. continue
  42. rtconfig.write('\n')
  43. empty_line = 1
  44. continue
  45. comment_line = line[1:]
  46. if line.startswith('# CONFIG_'): line = ' ' + line[9:]
  47. else: line = line[1:]
  48. rtconfig.write('/*%s */\n' % line)
  49. empty_line = 0
  50. else:
  51. empty_line = 0
  52. setting = line.split('=')
  53. if len(setting) >= 2:
  54. if setting[0].startswith('CONFIG_'):
  55. setting[0] = setting[0][7:]
  56. # remove CONFIG_PKG_XX_PATH or CONFIG_PKG_XX_VER
  57. if type(setting[0]) == type('a') and (setting[0].endswith('_PATH') or setting[0].endswith('_VER')):
  58. continue
  59. if setting[1] == 'y':
  60. rtconfig.write('#define %s\n' % setting[0])
  61. else:
  62. rtconfig.write('#define %s %s\n' % (setting[0], setting[1]))
  63. if os.path.isfile('rtconfig_project.h'):
  64. rtconfig.write('#include "rtconfig_project.h"\n')
  65. rtconfig.write('\n')
  66. rtconfig.write('#endif\n')
  67. rtconfig.close()
  68. def config():
  69. mk_rtconfig('.config')
  70. def get_env_dir():
  71. if os.environ.get('ENV_ROOT'):
  72. return os.environ.get('ENV_ROOT')
  73. home_dir = os.environ['HOME']
  74. env_dir = os.path.join(home_dir, '.env')
  75. if not os.path.exists(env_dir):
  76. return None
  77. return env_dir
  78. def touch_env():
  79. home_dir = os.environ['HOME']
  80. env_dir = os.path.join(home_dir, '.env')
  81. if not os.path.exists(env_dir):
  82. os.mkdir(env_dir)
  83. os.mkdir(os.path.join(env_dir, 'local_pkgs'))
  84. os.mkdir(os.path.join(env_dir, 'packages'))
  85. os.system('git clone https://github.com/RT-Thread/packages.git ~/.env/packages/packages')
  86. kconfig = file(os.path.join(env_dir, 'packages', 'Kconfig'), 'w')
  87. kconfig.write('source "$PKGS_DIR/packages/Kconfig"')
  88. os.mkdir(os.path.join(env_dir, 'tools'))
  89. os.system('git clone https://github.com/RT-Thread/env.git ~/.env/tools/scripts')
  90. env_sh = file(os.path.join(env_dir, 'env.sh'), 'w')
  91. env_sh.write('export PATH=~/.env/tools/scripts:$PATH')
  92. # menuconfig for Linux
  93. def menuconfig(RTT_ROOT):
  94. kconfig_dir = os.path.join(RTT_ROOT, 'tools', 'kconfig-frontends')
  95. os.system('scons -C ' + kconfig_dir)
  96. env_dir = get_env_dir()
  97. if not env_dir:
  98. touch_env()
  99. env_dir = get_env_dir()
  100. os.environ['PKGS_ROOT'] = os.path.join(env_dir, 'packages')
  101. fn = '.config'
  102. if os.path.isfile(fn):
  103. mtime = os.path.getmtime(fn)
  104. else:
  105. mtime = -1
  106. kconfig_cmd = os.path.join(RTT_ROOT, 'tools', 'kconfig-frontends', 'kconfig-mconf')
  107. os.system(kconfig_cmd + ' Kconfig')
  108. if os.path.isfile(fn):
  109. mtime2 = os.path.getmtime(fn)
  110. else:
  111. mtime2 = -1
  112. # make rtconfig.h
  113. if mtime != mtime2:
  114. mk_rtconfig(fn)