gcc.py 4.9 KB

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