gcc.py 6.3 KB

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