2018-05-30 20:58:04 +08:00
|
|
|
#
|
|
|
|
# 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
|
2023-11-08 15:09:09 +08:00
|
|
|
# 2023-11-03 idings return file path in GetHeader
|
2018-05-30 20:58:04 +08:00
|
|
|
|
2018-05-14 21:37:11 +08:00
|
|
|
import os
|
|
|
|
import re
|
2020-02-07 15:55:53 +08:00
|
|
|
import platform
|
2023-11-08 15:09:09 +08:00
|
|
|
import subprocess
|
2018-05-14 21:37:11 +08:00
|
|
|
|
|
|
|
def GetGCCRoot(rtconfig):
|
|
|
|
exec_path = rtconfig.EXEC_PATH
|
|
|
|
prefix = rtconfig.PREFIX
|
|
|
|
|
|
|
|
if prefix.endswith('-'):
|
|
|
|
prefix = prefix[:-1]
|
|
|
|
|
2019-08-28 16:52:58 +08:00
|
|
|
if exec_path == '/usr/bin':
|
|
|
|
root_path = os.path.join('/usr/lib', prefix)
|
|
|
|
else:
|
|
|
|
root_path = os.path.join(exec_path, '..', prefix)
|
2018-05-14 21:37:11 +08:00
|
|
|
|
2019-09-11 09:53:38 +08:00
|
|
|
return root_path
|
|
|
|
|
2023-11-08 15:09:09 +08:00
|
|
|
# https://stackoverflow.com/questions/4980819/what-are-the-gcc-default-include-directories
|
|
|
|
# https://stackoverflow.com/questions/53937211/how-can-i-parse-gcc-output-by-regex-to-get-default-include-paths
|
|
|
|
def match_pattern(pattern, input, start = 0, stop = -1, flags = 0):
|
|
|
|
length = len(input)
|
|
|
|
|
|
|
|
if length == 0:
|
|
|
|
return None
|
|
|
|
|
|
|
|
end_it = max(0, length - 1)
|
|
|
|
|
|
|
|
if start >= end_it:
|
|
|
|
return None
|
2019-09-11 09:53:38 +08:00
|
|
|
|
2023-11-08 15:09:09 +08:00
|
|
|
if stop<0:
|
|
|
|
stop = length
|
|
|
|
|
|
|
|
if stop <= start:
|
|
|
|
return None
|
|
|
|
|
|
|
|
for it in range(max(0, start), min(stop, length)):
|
|
|
|
elem = input[it]
|
|
|
|
match = re.match(pattern, elem, flags)
|
|
|
|
if match:
|
|
|
|
return it
|
|
|
|
|
|
|
|
def GetGccDefaultSearchDirs(rtconfig):
|
|
|
|
start_pattern = r' *#include <\.\.\.> search starts here: *'
|
|
|
|
end_pattern = r' *End of search list\. *'
|
|
|
|
|
|
|
|
gcc_cmd = os.path.join(rtconfig.EXEC_PATH, rtconfig.CC)
|
|
|
|
device_flags = rtconfig.DEVICE.split()
|
2024-01-25 15:20:14 +08:00
|
|
|
command = [gcc_cmd] + device_flags + ['-xc', '-E', '-v', os.devnull]
|
2023-11-08 15:09:09 +08:00
|
|
|
|
2023-11-14 18:18:07 +08:00
|
|
|
# if gcc_cmd can not access , return empty list
|
|
|
|
if not os.access(gcc_cmd, os.X_OK):
|
|
|
|
return []
|
|
|
|
|
2024-01-25 15:20:14 +08:00
|
|
|
if(platform.system() == 'Windows'):
|
|
|
|
child = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
|
|
|
|
else:
|
|
|
|
child = subprocess.Popen(' '.join(command), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
|
|
|
|
|
|
|
|
stdout = child.communicate()
|
|
|
|
stdout_string = (b''.join(stdout)).decode()
|
|
|
|
lines = stdout_string.splitlines()
|
2023-11-08 15:09:09 +08:00
|
|
|
|
|
|
|
start_it = match_pattern(start_pattern, lines)
|
|
|
|
if start_it == None:
|
|
|
|
return []
|
|
|
|
|
|
|
|
end_it = match_pattern(end_pattern, lines, start_it)
|
|
|
|
if end_it == None:
|
|
|
|
return []
|
|
|
|
|
|
|
|
# theres no paths between them
|
|
|
|
if (end_it - start_it) == 1:
|
|
|
|
return []
|
|
|
|
|
|
|
|
return lines[start_it + 1 : end_it]
|
|
|
|
|
|
|
|
def GetHeader(rtconfig, filename):
|
|
|
|
include_dirs = GetGccDefaultSearchDirs(rtconfig)
|
|
|
|
for directory in include_dirs:
|
|
|
|
fn = os.path.join(directory, filename).strip()
|
|
|
|
if os.path.isfile(fn):
|
|
|
|
return fn
|
|
|
|
|
|
|
|
# fallback to use fixed method if can't autodetect
|
|
|
|
root = GetGCCRoot(rtconfig)
|
2019-09-11 09:53:38 +08:00
|
|
|
fn = os.path.join(root, 'include', filename)
|
|
|
|
if os.path.isfile(fn):
|
2023-11-08 15:09:09 +08:00
|
|
|
return fn
|
2019-09-11 09:53:38 +08:00
|
|
|
|
2019-09-11 09:03:59 +08:00
|
|
|
# Usually the cross compiling gcc toolchain has directory as:
|
|
|
|
#
|
|
|
|
# bin
|
|
|
|
# lib
|
|
|
|
# share
|
|
|
|
# arm-none-eabi
|
|
|
|
# bin
|
|
|
|
# include
|
|
|
|
# lib
|
|
|
|
# share
|
|
|
|
prefix = rtconfig.PREFIX
|
|
|
|
if prefix.endswith('-'):
|
|
|
|
prefix = prefix[:-1]
|
|
|
|
|
|
|
|
fn = os.path.join(root, prefix, 'include', filename)
|
|
|
|
if os.path.isfile(fn):
|
2023-11-08 15:09:09 +08:00
|
|
|
return fn
|
2020-02-07 15:55:53 +08:00
|
|
|
|
2023-11-08 15:09:09 +08:00
|
|
|
return None
|
2018-05-14 21:37:11 +08:00
|
|
|
|
2023-08-21 01:26:39 +08:00
|
|
|
# GCC like means the toolchains which are compatible with GCC
|
|
|
|
def GetGCCLikePLATFORM():
|
|
|
|
return ['gcc', 'armclang', 'llvm-arm']
|
|
|
|
|
2023-11-08 15:09:09 +08:00
|
|
|
def GetPicoLibcVersion(rtconfig):
|
2023-04-30 12:19:57 +08:00
|
|
|
version = None
|
2023-09-15 09:45:39 +08:00
|
|
|
try:
|
|
|
|
rtconfig.PREFIX
|
|
|
|
except:
|
|
|
|
return version
|
|
|
|
|
2023-11-08 15:09:09 +08:00
|
|
|
# get version from picolibc.h
|
|
|
|
fn = GetHeader(rtconfig, 'picolibc.h')
|
|
|
|
|
|
|
|
if fn:
|
|
|
|
f = open(fn, 'r')
|
2018-05-14 21:37:11 +08:00
|
|
|
if f:
|
|
|
|
for line in f:
|
2023-11-08 15:09:09 +08:00
|
|
|
if line.find('__PICOLIBC_VERSION__') != -1 and line.find('"') != -1:
|
2018-05-14 21:37:11 +08:00
|
|
|
version = re.search(r'\"([^"]+)\"', line).groups()[0]
|
2018-11-07 13:40:41 +08:00
|
|
|
f.close()
|
2023-11-08 15:09:09 +08:00
|
|
|
|
|
|
|
return version
|
|
|
|
|
|
|
|
def GetNewLibVersion(rtconfig):
|
|
|
|
version = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
rtconfig.PREFIX
|
|
|
|
except:
|
|
|
|
return version
|
|
|
|
|
|
|
|
# if find picolibc.h, use picolibc
|
|
|
|
fn = GetHeader(rtconfig, 'picolibc.h')
|
|
|
|
if fn:
|
|
|
|
return version
|
|
|
|
|
|
|
|
# get version from _newlib_version.h file
|
|
|
|
fn = GetHeader(rtconfig, '_newlib_version.h')
|
|
|
|
|
|
|
|
# get version from newlib.h
|
|
|
|
if not fn:
|
|
|
|
fn = GetHeader(rtconfig, 'newlib.h')
|
|
|
|
|
|
|
|
if fn:
|
|
|
|
f = open(fn, 'r')
|
|
|
|
for line in f:
|
|
|
|
if line.find('_NEWLIB_VERSION') != -1 and line.find('"') != -1:
|
|
|
|
version = re.search(r'\"([^"]+)\"', line).groups()[0]
|
|
|
|
f.close()
|
|
|
|
|
2018-05-14 21:37:11 +08:00
|
|
|
return version
|
|
|
|
|
2022-11-23 10:40:50 +08:00
|
|
|
# FIXME: there is no musl version or musl macros can be found officially
|
|
|
|
def GetMuslVersion(rtconfig):
|
2023-04-30 12:19:57 +08:00
|
|
|
version = None
|
2023-09-15 09:45:39 +08:00
|
|
|
|
|
|
|
try:
|
|
|
|
rtconfig.PREFIX
|
|
|
|
except:
|
|
|
|
return version
|
|
|
|
|
2023-04-30 12:19:57 +08:00
|
|
|
if 'musl' in rtconfig.PREFIX:
|
|
|
|
version = 'unknown'
|
2022-11-23 10:40:50 +08:00
|
|
|
return version
|
|
|
|
|
2018-05-14 21:37:11 +08:00
|
|
|
def GCCResult(rtconfig, str):
|
|
|
|
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)
|
|
|
|
|
2020-02-07 15:55:53 +08:00
|
|
|
# use temp file to get more information
|
2018-11-07 13:38:57 +08:00
|
|
|
f = open('__tmp.c', 'w')
|
2018-05-14 21:37:11 +08:00
|
|
|
if f:
|
|
|
|
f.write(str)
|
|
|
|
f.close()
|
|
|
|
|
2020-02-07 15:55:53 +08:00
|
|
|
# '-fdirectives-only',
|
2018-05-17 16:45:16 +08:00
|
|
|
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)
|
2020-02-07 15:55:53 +08:00
|
|
|
|
2018-05-14 21:37:11 +08:00
|
|
|
stdout, stderr = child.communicate()
|
|
|
|
|
2018-05-22 11:32:48 +08:00
|
|
|
# print(stdout)
|
2022-02-23 20:45:27 +08:00
|
|
|
if stderr != '' and stderr != b'':
|
2018-05-14 21:37:11 +08:00
|
|
|
print(stderr)
|
|
|
|
|
|
|
|
have_fdset = 0
|
|
|
|
have_sigaction = 0
|
|
|
|
have_sigevent = 0
|
|
|
|
have_siginfo = 0
|
|
|
|
have_sigval = 0
|
|
|
|
version = None
|
|
|
|
stdc = '1989'
|
|
|
|
posix_thread = 0
|
|
|
|
|
2018-11-07 13:38:57 +08:00
|
|
|
for line in stdout.split(b'\n'):
|
2018-11-10 18:29:08 +08:00
|
|
|
line = line.decode()
|
|
|
|
if re.search('fd_set', line):
|
2018-05-14 21:37:11 +08:00
|
|
|
have_fdset = 1
|
|
|
|
|
|
|
|
# check for sigal
|
2018-11-10 18:29:08 +08:00
|
|
|
if re.search('struct[ \t]+sigaction', line):
|
2018-05-14 21:37:11 +08:00
|
|
|
have_sigaction = 1
|
2018-11-10 18:29:08 +08:00
|
|
|
if re.search('struct[ \t]+sigevent', line):
|
2018-05-14 21:37:11 +08:00
|
|
|
have_sigevent = 1
|
2018-11-10 18:29:08 +08:00
|
|
|
if re.search('siginfo_t', line):
|
2018-05-14 21:37:11 +08:00
|
|
|
have_siginfo = 1
|
2018-11-10 18:29:08 +08:00
|
|
|
if re.search('union[ \t]+sigval', line):
|
2018-05-14 21:37:11 +08:00
|
|
|
have_sigval = 1
|
|
|
|
|
2018-11-10 18:29:08 +08:00
|
|
|
if re.search('char\* version', line):
|
|
|
|
version = re.search(r'\"([^"]+)\"', line).groups()[0]
|
2018-05-14 21:37:11 +08:00
|
|
|
|
2018-11-10 18:29:08 +08:00
|
|
|
if re.findall('iso_c_visible = [\d]+', line):
|
2018-05-14 21:37:11 +08:00
|
|
|
stdc = re.findall('[\d]+', line)[0]
|
|
|
|
|
2018-11-10 18:29:08 +08:00
|
|
|
if re.findall('pthread_create', line):
|
2018-05-14 21:37:11 +08:00
|
|
|
posix_thread = 1
|
2020-02-07 15:55:53 +08:00
|
|
|
|
2018-05-14 21:37:11 +08:00
|
|
|
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:
|
2020-02-07 15:55:53 +08:00
|
|
|
result += '#define GCC_VERSION_STR "%s"\n' % version
|
2018-05-14 21:37:11 +08:00
|
|
|
|
|
|
|
result += '#define STDC "%s"\n' % stdc
|
|
|
|
|
|
|
|
if posix_thread:
|
|
|
|
result += '#define LIBC_POSIX_THREADS 1\n'
|
|
|
|
|
|
|
|
os.remove('__tmp.c')
|
|
|
|
return result
|