gcc.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. return False
  42. def GetNewLibVersion(rtconfig):
  43. version = 'unknown'
  44. root = GetGCCRoot(rtconfig)
  45. if CheckHeader(rtconfig, '_newlib_version.h'): # get version from _newlib_version.h file
  46. f = open(os.path.join(root, 'include', '_newlib_version.h'), 'r')
  47. if f:
  48. for line in f:
  49. if line.find('_NEWLIB_VERSION') != -1 and line.find('"') != -1:
  50. version = re.search(r'\"([^"]+)\"', line).groups()[0]
  51. f.close()
  52. elif CheckHeader(rtconfig, 'newlib.h'): # get version from newlib.h
  53. f = open(os.path.join(root, 'include', 'newlib.h'), 'r')
  54. if f:
  55. for line in f:
  56. if line.find('_NEWLIB_VERSION') != -1 and line.find('"') != -1:
  57. version = re.search(r'\"([^"]+)\"', line).groups()[0]
  58. f.close()
  59. return version
  60. def GCCResult(rtconfig, str):
  61. import subprocess
  62. result = ''
  63. def checkAndGetResult(pattern, string):
  64. if re.search(pattern, string):
  65. return re.search(pattern, string).group(0)
  66. return None
  67. gcc_cmd = os.path.join(rtconfig.EXEC_PATH, rtconfig.CC)
  68. # use temp file to get more information
  69. f = open('__tmp.c', 'w')
  70. if f:
  71. f.write(str)
  72. f.close()
  73. # '-fdirectives-only',
  74. if(platform.system() == 'Windows'):
  75. child = subprocess.Popen([gcc_cmd, '-E', '-P', '__tmp.c'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
  76. else:
  77. child = subprocess.Popen(gcc_cmd + ' -E -P __tmp.c', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
  78. stdout, stderr = child.communicate()
  79. # print(stdout)
  80. if stderr != '':
  81. print(stderr)
  82. have_fdset = 0
  83. have_sigaction = 0
  84. have_sigevent = 0
  85. have_siginfo = 0
  86. have_sigval = 0
  87. version = None
  88. stdc = '1989'
  89. posix_thread = 0
  90. for line in stdout.split(b'\n'):
  91. line = line.decode()
  92. if re.search('fd_set', line):
  93. have_fdset = 1
  94. # check for sigal
  95. if re.search('struct[ \t]+sigaction', line):
  96. have_sigaction = 1
  97. if re.search('struct[ \t]+sigevent', line):
  98. have_sigevent = 1
  99. if re.search('siginfo_t', line):
  100. have_siginfo = 1
  101. if re.search('union[ \t]+sigval', line):
  102. have_sigval = 1
  103. if re.search('char\* version', line):
  104. version = re.search(r'\"([^"]+)\"', line).groups()[0]
  105. if re.findall('iso_c_visible = [\d]+', line):
  106. stdc = re.findall('[\d]+', line)[0]
  107. if re.findall('pthread_create', line):
  108. posix_thread = 1
  109. if have_fdset:
  110. result += '#define HAVE_FDSET 1\n'
  111. if have_sigaction:
  112. result += '#define HAVE_SIGACTION 1\n'
  113. if have_sigevent:
  114. result += '#define HAVE_SIGEVENT 1\n'
  115. if have_siginfo:
  116. result += '#define HAVE_SIGINFO 1\n'
  117. if have_sigval:
  118. result += '#define HAVE_SIGVAL 1\n'
  119. if version:
  120. result += '#define GCC_VERSION "%s"\n' % version
  121. result += '#define STDC "%s"\n' % stdc
  122. if posix_thread:
  123. result += '#define LIBC_POSIX_THREADS 1\n'
  124. os.remove('__tmp.c')
  125. return result
  126. def GenerateGCCConfig(rtconfig):
  127. str = ''
  128. cc_header = ''
  129. cc_header += '#ifndef CCONFIG_H__\n'
  130. cc_header += '#define CCONFIG_H__\n'
  131. cc_header += '/* Automatically generated file; DO NOT EDIT. */\n'
  132. cc_header += '/* compiler configure file for RT-Thread in GCC*/\n\n'
  133. if CheckHeader(rtconfig, 'newlib.h'):
  134. str += '#include <newlib.h>\n'
  135. cc_header += '#define HAVE_NEWLIB_H 1\n'
  136. cc_header += '#define LIBC_VERSION "newlib %s"\n\n' % GetNewLibVersion(rtconfig)
  137. if CheckHeader(rtconfig, 'sys/signal.h'):
  138. str += '#include <sys/signal.h>\n'
  139. cc_header += '#define HAVE_SYS_SIGNAL_H 1\n'
  140. if CheckHeader(rtconfig, 'sys/select.h'):
  141. str += '#include <sys/select.h>\n'
  142. cc_header += '#define HAVE_SYS_SELECT_H 1\n'
  143. if CheckHeader(rtconfig, 'pthread.h'):
  144. str += "#include <pthread.h>\n"
  145. cc_header += '#define HAVE_PTHREAD_H 1\n'
  146. # if CheckHeader(rtconfig, 'sys/dirent.h'):
  147. # str += '#include <sys/dirent.h>\n'
  148. # add some common features
  149. str += 'const char* version = __VERSION__;\n'
  150. str += 'const int iso_c_visible = __ISO_C_VISIBLE;\n'
  151. str += '\n#ifdef HAVE_INITFINI_ARRAY\n'
  152. str += 'const int init_fini_array = HAVE_INITFINI_ARRAY;\n'
  153. str += '#endif\n'
  154. cc_header += '\n'
  155. cc_header += GCCResult(rtconfig, str)
  156. cc_header += '\n#endif\n'
  157. cc_file = open('cconfig.h', 'w')
  158. if cc_file:
  159. cc_file.write(cc_header)
  160. cc_file.close()