Skip to content

Commit

Permalink
Better workaround for builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
Kartatz committed Aug 9, 2024
1 parent aa551c0 commit e04e66d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 34 deletions.
24 changes: 1 addition & 23 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 <builtin_ctype.h>' >> './ctype.h'
fi

if [ -f './builtin_math.h' ]; then
echo '#include <builtin_math.h>' >> './math.h'
fi

if [ -f './builtin_stdio.h' ]; then
echo '#include <builtin_stdio.h>' >> './stdio.h'
fi

if [ -f './builtin_complex.h' ]; then
echo '#include <builtin_complex.h>' >> './complex.h'
fi

if [ -f './builtin_stdlib.h' ]; then
echo '#include <builtin_stdlib.h>' >> './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"
Expand Down
4 changes: 2 additions & 2 deletions tools/builtin_symbols.json
Original file line number Diff line number Diff line change
Expand Up @@ -445,15 +445,15 @@
},
{
"name": "imaxabs",
"header": "ctype"
"header": "inttypes"
},
{
"name": "isblank",
"header": "ctype"
},
{
"name": "iswblank",
"header": "ctype"
"header": "wctype"
},
{
"name": "lgammaf",
Expand Down
52 changes: 43 additions & 9 deletions tools/make_builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -12,7 +23,7 @@
}
"""

destination = """
destination = """\
#if !defined(BUILTIN_COMPAT_%s_H)
#define BUILTIN_COMPAT_%s_H
%s
Expand All @@ -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):

Expand All @@ -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

Expand All @@ -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)
Expand All @@ -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)

0 comments on commit e04e66d

Please sign in to comment.