gcc.py 6.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#
# File      : gcc.py
# This file is part of RT-Thread RTOS
# COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Change Logs:
# Date           Author       Notes
# 2018-05-22     Bernard      The first version

B
Bernard Xiong 已提交
24 25
import os
import re
lymzzyh's avatar
lymzzyh 已提交
26
import platform 
B
Bernard Xiong 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

def GetGCCRoot(rtconfig):
    exec_path = rtconfig.EXEC_PATH
    prefix = rtconfig.PREFIX

    if prefix.endswith('-'):
        prefix = prefix[:-1]

    root_path = os.path.join(exec_path, '..', prefix)

    return root_path

def CheckHeader(rtconfig, filename):
    root = GetGCCRoot(rtconfig)

    fn = os.path.join(root, 'include', filename)
    if os.path.isfile(fn):
        return True

    return False

def GetNewLibVersion(rtconfig):
    version = 'unknown'
    root = GetGCCRoot(rtconfig)

    if CheckHeader(rtconfig, '_newlib_version.h'): # get version from _newlib_version.h file
53
        f = open(os.path.join(root, 'include', '_newlib_version.h'), 'r')
B
Bernard Xiong 已提交
54 55 56 57
        if f:
            for line in f:
                if line.find('_NEWLIB_VERSION') != -1 and line.find('"') != -1:
                    version = re.search(r'\"([^"]+)\"', line).groups()[0]
58
        f.close()
B
Bernard Xiong 已提交
59
    elif CheckHeader(rtconfig, 'newlib.h'): # get version from newlib.h
60
        f = open(os.path.join(root, 'include', 'newlib.h'), 'r')
B
Bernard Xiong 已提交
61 62 63 64
        if f:
            for line in f:
                if line.find('_NEWLIB_VERSION') != -1 and line.find('"') != -1:
                    version = re.search(r'\"([^"]+)\"', line).groups()[0]
65
        f.close()
B
Bernard Xiong 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    return version

def GCCResult(rtconfig, str):
    import subprocess

    result = ''

    def checkAndGetResult(pattern, string):
        if re.search(pattern, string):
            return re.search(pattern, string).group(0)
        return None

    gcc_cmd = os.path.join(rtconfig.EXEC_PATH, rtconfig.CC)

    # use temp file to get more information 
81
    f = open('__tmp.c', 'w')
B
Bernard Xiong 已提交
82 83 84 85 86
    if f:
        f.write(str)
        f.close()

        # '-fdirectives-only', 
lymzzyh's avatar
lymzzyh 已提交
87 88 89 90 91
        if(platform.system() == 'Windows'):
            child = subprocess.Popen([gcc_cmd, '-E', '-P', '__tmp.c'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
        else:
            child = subprocess.Popen(gcc_cmd + ' -E -P __tmp.c', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
        
B
Bernard Xiong 已提交
92 93
        stdout, stderr = child.communicate()

94
        # print(stdout)
B
Bernard Xiong 已提交
95 96 97 98 99 100 101 102 103 104 105 106
        if stderr != '':
            print(stderr)

        have_fdset = 0
        have_sigaction = 0
        have_sigevent = 0
        have_siginfo = 0
        have_sigval = 0
        version = None
        stdc = '1989'
        posix_thread = 0

107 108
        for line in stdout.split(b'\n'):
            if re.search(b'fd_set', line):
B
Bernard Xiong 已提交
109 110 111
                have_fdset = 1

            # check for sigal
112
            if re.search(b'struct[ \t]+sigaction', line):
B
Bernard Xiong 已提交
113
                have_sigaction = 1
114
            if re.search(b'struct[ \t]+sigevent', line):
B
Bernard Xiong 已提交
115
                have_sigevent = 1
116
            if re.search(b'siginfo_t', line):
B
Bernard Xiong 已提交
117
                have_siginfo = 1
118
            if re.search(b'union[ \t]+sigval', line):
B
Bernard Xiong 已提交
119 120
                have_sigval = 1

121 122
            if re.search(b'char\* version', line):
                version = re.search(br'\"([^"]+)\"', line).groups()[0]
B
Bernard Xiong 已提交
123

124
            if re.findall(b'iso_c_visible = [\d]+', line):
B
Bernard Xiong 已提交
125 126
                stdc = re.findall('[\d]+', line)[0]

127
            if re.findall(b'pthread_create', line):
B
Bernard Xiong 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
                posix_thread = 1
    
        if have_fdset:
            result += '#define HAVE_FDSET 1\n'

        if have_sigaction:
            result += '#define HAVE_SIGACTION 1\n'
        if have_sigevent:
            result += '#define HAVE_SIGEVENT 1\n'
        if have_siginfo:
            result += '#define HAVE_SIGINFO 1\n'
        if have_sigval:
            result += '#define HAVE_SIGVAL 1\n'

        if version:
            result += '#define GCC_VERSION "%s"\n' % version

        result += '#define STDC "%s"\n' % stdc

        if posix_thread:
            result += '#define LIBC_POSIX_THREADS 1\n'

        os.remove('__tmp.c')
151
    f.close()
B
Bernard Xiong 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    return result

def GenerateGCCConfig(rtconfig):
    str = ''
    cc_header = ''
    cc_header += '#ifndef CCONFIG_H__\n'
    cc_header += '#define CCONFIG_H__\n'
    cc_header += '/* Automatically generated file; DO NOT EDIT. */\n'
    cc_header += '/* compiler configure file for RT-Thread in GCC*/\n\n'

    if CheckHeader(rtconfig, 'newlib.h'):
        str += '#include <newlib.h>\n'
        cc_header += '#define HAVE_NEWLIB_H 1\n'
        cc_header += '#define LIBC_VERSION "newlib %s"\n\n' % GetNewLibVersion(rtconfig)

    if CheckHeader(rtconfig, 'sys/signal.h'):
        str += '#include <sys/signal.h>\n'
        cc_header += '#define HAVE_SYS_SIGNAL_H 1\n'
    if CheckHeader(rtconfig, 'sys/select.h'):
        str += '#include <sys/select.h>\n'
        cc_header += '#define HAVE_SYS_SELECT_H 1\n'
    if CheckHeader(rtconfig, 'pthread.h'):
        str += "#include <pthread.h>\n"
        cc_header += '#define HAVE_PTHREAD_H 1\n'

    # if CheckHeader(rtconfig, 'sys/dirent.h'):
    #    str += '#include <sys/dirent.h>\n'

    # add some common features
    str += 'const char* version = __VERSION__;\n'
    str += 'const int iso_c_visible = __ISO_C_VISIBLE;\n'
    str += '\n#ifdef HAVE_INITFINI_ARRAY\n'
    str += 'const int init_fini_array = HAVE_INITFINI_ARRAY;\n'
    str += '#endif\n'

    cc_header += '\n'
    cc_header += GCCResult(rtconfig, str)
    cc_header += '\n#endif\n'

191
    cc_file = open('cconfig.h', 'w')
B
Bernard Xiong 已提交
192 193 194
    if cc_file:
        cc_file.write(cc_header)
        cc_file.close()