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 9c9edbf
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 54 deletions.
54 changes: 16 additions & 38 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,22 @@ make all --jobs
make install

declare -ra targets=(
'ia64-unknown-linux-gnu'
'alpha-unknown-linux-gnu'
# 'ia64-unknown-linux-gnu'
# 'alpha-unknown-linux-gnu'
'x86_64-unknown-linux-gnu'
'i386-unknown-linux-gnu'
'arm-unknown-linux-gnueabi'
'arm-unknown-linux-gnueabihf'
'hppa-unknown-linux-gnu'
'aarch64-unknown-linux-gnu'
'mips-unknown-linux-gnu'
'mipsel-unknown-linux-gnu'
'powerpc-unknown-linux-gnu'
's390-unknown-linux-gnu'
's390x-unknown-linux-gnu'
'sparc-unknown-linux-gnu'
'powerpc64le-unknown-linux-gnu'
'mips64el-unknown-linux-gnuabi64'
# 'i386-unknown-linux-gnu'
# 'arm-unknown-linux-gnueabi'
# 'arm-unknown-linux-gnueabihf'
# 'hppa-unknown-linux-gnu'
# 'aarch64-unknown-linux-gnu'
# 'mips-unknown-linux-gnu'
# 'mipsel-unknown-linux-gnu'
# 'powerpc-unknown-linux-gnu'
# 's390-unknown-linux-gnu'
# 's390x-unknown-linux-gnu'
# 'sparc-unknown-linux-gnu'
# 'powerpc64le-unknown-linux-gnu'
# 'mips64el-unknown-linux-gnuabi64'
)

for target in "${targets[@]}"; do
Expand Down 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
57 changes: 43 additions & 14 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,12 +23,9 @@
}
"""

destination = """
#if !defined(BUILTIN_COMPAT_%s_H)
#define BUILTIN_COMPAT_%s_H
destination = """\
/* This file is auto generated. */
%s
#endif
"""

temporary_file = os.path.join(tempfile.gettempdir(), "main.c")
Expand All @@ -35,12 +43,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 +63,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, "-ansi", "-w", "-fno-builtin", temporary_file, "-o", "%s.o" % temporary_file])

return process.returncode == 0

Expand All @@ -67,9 +79,11 @@ def dump_builtins(builtins, name):
items.sort()

for item in items:
s += ("\n#define %s __builtin_%s" % (item, item))
s += ("\n#if !defined(%s)\n#define %s __builtin_%s\n#endif\n" % (item, item, item))

dump = destination % (name.upper(), name.upper(), s)
dump = destination % (
s
)

with open(file = "builtin_%s.h" % (name), mode = "w") as file:
file.write(dump)
Expand All @@ -95,8 +109,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 9c9edbf

Please sign in to comment.