summaryrefslogtreecommitdiff
path: root/scripts/build/probe-toolchain.sh
blob: 218a821d903cdb412b64c1a044fde3cd54a540a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/sh
# SPDX-License-Identifier: GPL-2.0-or-later

set -eu
output=$1
cc=$2
cxx=$3
ld=$4
family=$5
target=$6
gcc_toolchain=$7
sysroot=$8
ktl_include=$9
ktl_after_include=${10}
libc_include=${11}
libc_after_include=${12}
libcxx_include=${13}
compiler_resource_include=${14}

set --
if [ "$family" = clang ]; then
    set -- "$@" "--target=$target"
    [ -z "$gcc_toolchain" ] || set -- "$@" "--gcc-toolchain=$gcc_toolchain"
    [ -z "$sysroot" ] || set -- "$@" "--sysroot=$sysroot"
fi

probe() {
    language=$1
    standard=$2
    shift 2
    compiler=$cc
    [ "$language" = c++ ] && compiler=$cxx
    printf '' | "$compiler" "$@" "-std=$standard" -x "$language" \
        -fsyntax-only - >/dev/null 2>&1
}

probe_libcxx() {
    printf '%s\n' \
        '#include <ktl/array.h>' \
        '#include <ktl/atomic.h>' \
        '#include <ktl/optional.h>' \
        '#include <ktl/span.h>' \
        '#include <ktl/string_view.h>' \
        'constexpr ktl::array<int, 2> values{1, 2};' \
        'static_assert(values[1] == 2);' | \
        "$cxx" "$@" -std=c++23 -ffreestanding -fno-exceptions -fno-rtti \
        -nostdinc -I include -I "$ktl_include" -I "$libc_include" \
        -isystem "$compiler_resource_include" \
        -isystem "$libcxx_include" -idirafter "$ktl_after_include" \
        -idirafter "$libc_after_include" -fsyntax-only -x c++ - >/dev/null 2>&1
}

tmp="$output.tmp.$$"
mkdir -p "$(dirname "$output")"
{
    echo "cc=$($cc --version | head -n 1)"
    echo "cxx=$($cxx --version | head -n 1)"
    echo "ld=$($ld --version | head -n 1)"
    echo "target=$target"
    printf 'c23='; if probe c gnu23 "$@"; then echo yes; else echo no; fi
    printf 'cxx23='; if probe c++ c++23 "$@"; then echo yes; else echo no; fi
    printf 'libcxx-headers='; if probe_libcxx "$@"; then echo yes; else echo no; fi
    printf 'fno-pie='; if probe c++ c++23 "$@" -fno-pie; then echo yes; else echo no; fi
    printf 'mcmodel-medany='; if probe c++ c++23 "$@" -mcmodel=medany; then echo yes; else echo no; fi
} > "$tmp"
mv -f "$tmp" "$output"

grep -q '^c23=yes$' "$output"
grep -q '^cxx23=yes$' "$output"
grep -q '^libcxx-headers=yes$' "$output"
grep -q '^fno-pie=yes$' "$output"
grep -q '^mcmodel-medany=yes$' "$output"