gcc.py 6.1 KB

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