From e04e66dff9d7fe58d1b110c9e3f1a66d59dc0b9a Mon Sep 17 00:00:00 2001 From: Kartatz <105828205+Kartatz@users.noreply.github.com> Date: Fri, 9 Aug 2024 00:05:13 -0300 Subject: [PATCH] Better workaround for builtins --- build.sh | 24 +----------------- tools/builtin_symbols.json | 4 +-- tools/make_builtins.py | 52 +++++++++++++++++++++++++++++++------- 3 files changed, 46 insertions(+), 34 deletions(-) diff --git a/build.sh b/build.sh index 01c44ca..eb49bad 100644 --- a/build.sh +++ b/build.sh @@ -207,30 +207,8 @@ for target in "${targets[@]}"; do patch --directory="${toolchain_directory}/${triple}" --strip='1' --input="${workdir}/patches/linux_pim.patch" fi - cd "${toolchain_directory}/${triple}/include" - if ! (( is_native )); then - CC="${triple}-gcc" python "${workdir}/tools/make_builtins.py" - - if [ -f './builtin_ctype.h' ]; then - echo '#include ' >> './ctype.h' - fi - - if [ -f './builtin_math.h' ]; then - echo '#include ' >> './math.h' - fi - - if [ -f './builtin_stdio.h' ]; then - echo '#include ' >> './stdio.h' - fi - - if [ -f './builtin_complex.h' ]; then - echo '#include ' >> './complex.h' - fi - - if [ -f './builtin_stdlib.h' ]; then - echo '#include ' >> './stdlib.h' - fi + C_INCLUDE_PATH="${toolchain_directory}/${triple}/include" CC="${triple}-gcc" python "${workdir}/tools/make_builtins.py" fi [ -d "${binutils_directory}/build" ] || mkdir "${binutils_directory}/build" diff --git a/tools/builtin_symbols.json b/tools/builtin_symbols.json index 782064b..ef680da 100644 --- a/tools/builtin_symbols.json +++ b/tools/builtin_symbols.json @@ -445,7 +445,7 @@ }, { "name": "imaxabs", - "header": "ctype" + "header": "inttypes" }, { "name": "isblank", @@ -453,7 +453,7 @@ }, { "name": "iswblank", - "header": "ctype" + "header": "wctype" }, { "name": "lgammaf", diff --git a/tools/make_builtins.py b/tools/make_builtins.py index 559a63b..c0031bd 100644 --- a/tools/make_builtins.py +++ b/tools/make_builtins.py @@ -2,8 +2,19 @@ import os import subprocess import tempfile +import shutil + +headers = ( + "math", + "stdio", + "complex", + "stdlib", + "ctype", + "wctype", + "inttypes" +) -source = """ +source = """\ #include <%s.h> int main(void) { @@ -12,7 +23,7 @@ } """ -destination = """ +destination = """\ #if !defined(BUILTIN_COMPAT_%s_H) #define BUILTIN_COMPAT_%s_H %s @@ -35,12 +46,16 @@ def __init__(self): self.complex = [] self.stdlib = [] self.ctype = [] + self.wctype = [] + self.inttypes = [] content = None cc = os.getenv(key = "CC") +c_include_path = os.getenv(key = "C_INCLUDE_PATH") assert cc is not None +assert c_include_path is not None def check_symbols_exists(symbol): @@ -51,7 +66,7 @@ def check_symbols_exists(symbol): with open(file = temporary_file, mode = "w") as file: file.write(text) - process = subprocess.run([cc, "-w", "-fno-builtin", temporary_file, "-o", "%s.o" % temporary_file]) + process = subprocess.run([cc, "-std=c23", "-w", "-fno-builtin", temporary_file, "-o", "%s.o" % temporary_file]) return process.returncode == 0 @@ -69,7 +84,11 @@ def dump_builtins(builtins, name): for item in items: s += ("\n#define %s __builtin_%s" % (item, item)) - dump = destination % (name.upper(), name.upper(), s) + dump = destination % ( + name.upper(), + name.upper(), + s + ) with open(file = "builtin_%s.h" % (name), mode = "w") as file: file.write(dump) @@ -95,8 +114,23 @@ def dump_builtins(builtins, name): items = getattr(builtins, header) items.append(name) -dump_builtins(builtins, "math") -dump_builtins(builtins, "stdio") -dump_builtins(builtins, "complex") -dump_builtins(builtins, "stdlib") -dump_builtins(builtins, "ctype") +for name in headers: + dump_builtins(builtins = builtins, name = name) + + src = "builtin_%s.h" % (name) + dst = os.path.join(c_include_path, "%s.h" % (name)) + + if not os.path.exists(path = src): + continue + + print("- Modifying '%s'" % (dst)) + + with open(file = dst, mode = "a+") as file: + file.write("\n\n#include <%s>" % (src)) + + dst = os.path.join(c_include_path, src) + + if os.path.exists(path = dst): + os.remove(dst) + + shutil.move(src = src, dst = dst)