gcc.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #
  2. # File : gcc.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. # 2018-05-22 Bernard The first version
  23. import os
  24. import re
  25. import platform
  26. def GetGCCRoot(rtconfig):
  27. exec_path = rtconfig.EXEC_PATH
  28. prefix = rtconfig.PREFIX
  29. if prefix.endswith('-'):
  30. prefix = prefix[:-1]
  31. if exec_path == '/usr/bin':
  32. root_path = os.path.join('/usr/lib', prefix)
  33. else:
  34. root_path = os.path.join(exec_path, '..', prefix)
  35. return root_path
  36. def CheckHeader(rtconfig, filename):
  37. root = GetGCCRoot(rtconfig)
  38. fn = os.path.join(root, 'include', filename)
  39. if os.path.isfile(fn):
  40. return True
  41. # Usually the cross compiling gcc toolchain has directory as:
  42. #
  43. # bin
  44. # lib
  45. # share
  46. # arm-none-eabi
  47. # bin
  48. # include
  49. # lib
  50. # share
  51. prefix = rtconfig.PREFIX
  52. if prefix.endswith('-'):
  53. prefix = prefix[:-1]
  54. fn = os.path.join(root, prefix, 'include', filename)
  55. if os.path.isfile(fn):
  56. return True
  57. return False
  58. def GetNewLibVersion(rtconfig):
  59. version = 'unknown'
  60. root = GetGCCRoot(rtconfig)
  61. if CheckHeader(rtconfig, '_newlib_version.h'): # get version from _newlib_version.h file
  62. f = open(os.path.join(root, 'include', '_newlib_version.h'), 'r')
  63. if f:
  64. for line in f:
  65. if line.find('_NEWLIB_VERSION') != -1 and line.find('"') != -1:
  66. version = re.search(r'\"([^"]+)\"', line).groups()[0]
  67. f.close()
  68. elif CheckHeader(rtconfig, 'newlib.h'): # get version from newlib.h
  69. f = open(os.path.join(root, 'include', 'newlib.h'), 'r')
  70. if f:
  71. for line in f:
  72. if line.find('_NEWLIB_VERSION') != -1 and line.find('"') != -1:
  73. version = re.search(r'\"([^"]+)\"', line).groups()[0]
  74. f.close()
  75. return version
  76. def CheckMUSLLibc():
  77. try:
  78. f = open(".config")
  79. if f:
  80. for line in f:
  81. if line.find('CONFIG_RT_USING_MUSL=y') != -1:
  82. return True
  83. f.close()
  84. else:
  85. print("open .config failed")
  86. return False
  87. except Exception as e:
  88. pass
  89. return False
  90. def GCCResult(rtconfig, str):
  91. import subprocess
  92. result = ''
  93. use_musl = CheckMUSLLibc()
  94. def checkAndGetResult(pattern, string):
  95. if re.search(pattern, string):
  96. return re.search(pattern, string).group(0)
  97. return None
  98. gcc_cmd = os.path.join(rtconfig.EXEC_PATH, rtconfig.CC)
  99. # use temp file to get more information
  100. f = open('__tmp.c', 'w')
  101. if f:
  102. f.write(str)
  103. f.close()
  104. # '-fdirectives-only',
  105. if(platform.system() == 'Windows'):
  106. child = subprocess.Popen([gcc_cmd, '-E', '-P', '__tmp.c'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
  107. else:
  108. child = subprocess.Popen(gcc_cmd + ' -E -P __tmp.c', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
  109. stdout, stderr = child.communicate()
  110. # print(stdout)
  111. if stderr != '':
  112. print(stderr)
  113. have_fdset = 0
  114. have_sigaction = 0
  115. have_sigevent = 0
  116. have_siginfo = 0
  117. have_sigval = 0
  118. version = None
  119. stdc = '1989'
  120. posix_thread = 0
  121. for line in stdout.split(b'\n'):
  122. line = line.decode()
  123. if re.search('fd_set', line):
  124. have_fdset = 1
  125. # check for sigal
  126. if re.search('struct[ \t]+sigaction', line):
  127. have_sigaction = 1
  128. if re.search('struct[ \t]+sigevent', line):
  129. have_sigevent = 1
  130. if re.search('siginfo_t', line):
  131. have_siginfo = 1
  132. if re.search('union[ \t]+sigval', line):
  133. have_sigval = 1
  134. if re.search('char\* version', line):
  135. version = re.search(r'\"([^"]+)\"', line).groups()[0]
  136. if re.findall('iso_c_visible = [\d]+', line):
  137. stdc = re.findall('[\d]+', line)[0]
  138. if re.findall('pthread_create', line):
  139. posix_thread = 1
  140. if use_musl:
  141. result += '#define HAVE_SYS_SELECT_H 1\n\n'
  142. if have_fdset:
  143. result += '#define HAVE_FDSET 1\n'
  144. if have_sigaction or use_musl:
  145. result += '#define HAVE_SIGACTION 1\n'
  146. if have_sigevent or use_musl:
  147. result += '#define HAVE_SIGEVENT 1\n'
  148. if have_siginfo or use_musl:
  149. result += '#define HAVE_SIGINFO 1\n'
  150. if have_sigval or use_musl:
  151. result += '#define HAVE_SIGVAL 1\n'
  152. if version:
  153. result += '#define GCC_VERSION_STR "%s"\n' % version
  154. result += '#define STDC "%s"\n' % stdc
  155. if posix_thread:
  156. result += '#define LIBC_POSIX_THREADS 1\n'
  157. os.remove('__tmp.c')
  158. return result
  159. def GenerateGCCConfig(rtconfig):
  160. str = ''
  161. cc_header = ''
  162. cc_header += '#ifndef CCONFIG_H__\n'
  163. cc_header += '#define CCONFIG_H__\n'
  164. cc_header += '/* Automatically generated file; DO NOT EDIT. */\n'
  165. cc_header += '/* compiler configure file for RT-Thread in GCC*/\n\n'
  166. if CheckHeader(rtconfig, 'newlib.h'):
  167. str += '#include <newlib.h>\n'
  168. cc_header += '#define HAVE_NEWLIB_H 1\n'
  169. cc_header += '#define LIBC_VERSION "newlib %s"\n\n' % GetNewLibVersion(rtconfig)
  170. if CheckHeader(rtconfig, 'sys/signal.h'):
  171. str += '#include <sys/signal.h>\n'
  172. cc_header += '#define HAVE_SYS_SIGNAL_H 1\n'
  173. if CheckHeader(rtconfig, 'sys/select.h'):
  174. str += '#include <sys/select.h>\n'
  175. cc_header += '#define HAVE_SYS_SELECT_H 1\n'
  176. if CheckHeader(rtconfig, 'pthread.h'):
  177. str += "#include <pthread.h>\n"
  178. cc_header += '#define HAVE_PTHREAD_H 1\n'
  179. # if CheckHeader(rtconfig, 'sys/dirent.h'):
  180. # str += '#include <sys/dirent.h>\n'
  181. # add some common features
  182. str += 'const char* version = __VERSION__;\n'
  183. str += 'const int iso_c_visible = __ISO_C_VISIBLE;\n'
  184. str += '\n#ifdef HAVE_INITFINI_ARRAY\n'
  185. str += 'const int init_fini_array = HAVE_INITFINI_ARRAY;\n'
  186. str += '#endif\n'
  187. cc_header += '\n'
  188. cc_header += GCCResult(rtconfig, str)
  189. cc_header += '\n#endif\n'
  190. cc_file = open('cconfig.h', 'w')
  191. if cc_file:
  192. cc_file.write(cc_header)
  193. cc_file.close()