aboutsummaryrefslogtreecommitdiff
path: root/meson-pgo.sh
blob: ba3ea9544046a56472682961487f30600f078af4 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/sh

set -e

mode=${1}; shift
source_dir=$(realpath "${1}"); shift
build_dir=$(realpath ${1}); shift

if [ -d ${build_dir} ]; then
    echo "${build_dir}: build directory already exists"
    exit 1
fi

case ${mode} in
    auto|partial|full)
        ;;

    *)
        echo "${mode}: invalid PGO mode (must be one of 'auto', 'partial' or 'full')"
        exit 1
        ;;
esac

compiler=other
do_pgo=no

CFLAGS="${CFLAGS} -O3"

case $(${CC-cc} --version) in
    *GCC*)
        compiler=gcc
        do_pgo=yes
        ;;

    *clang*)
        compiler=clang

        if command -v llvm-profdata > /dev/null; then
            do_pgo=yes
            CFLAGS="${CFLAGS} -Wno-ignored-optimization-argument"
        fi
        ;;
esac

if [ ${mode} = auto ]; then
    if [ -n "${WAYLAND_DISPLAY+x}" ]; then
        mode=full
    else
        mode=partial
    fi
fi

# echo "source: ${source_dir}"
# echo "build: ${build_dir}"
# echo "compiler: ${compiler}"
# echo "mode: ${mode}"

export CFLAGS
meson "${@}" "${build_dir}"

if [ ${do_pgo} = yes ]; then
    find . -name "*.gcda" -delete
    meson configure "${build_dir}" -Db_pgo=generate
    ninja -C "${build_dir}"

    # If fcft/tllist are subprojects, we need to ensure their tests
    # have been executed, or we’ll get “profile count data file not
    # found” errors.
    ninja -C "${build_dir}" test

    script_options="--scroll --scroll-region --colors-regular --colors-bright --colors-256 --colors-rgb --attr-bold --attr-italic --attr-underline --sixel"

    tmp_file=$(mktemp)
    pwd=$(pwd)

    cleanup() {
        rm -f "${tmp_file}"
        cd "${pwd}"
    }
    trap cleanup EXIT INT HUP TERM

    cd "${build_dir}"
    if [ ${mode} = full ]; then
        ./footclient --version
        ./foot \
            --config=/dev/null \
            --term=xterm \
            sh -c "${source_dir}/scripts/generate-alt-random-writes.py ${script_options} ${tmp_file} && cat ${tmp_file}"
    else
        ./footclient --version
        ./foot --version
        "${source_dir}/scripts/generate-alt-random-writes.py" \
            --rows=67 \
            --cols=135 \
            ${script_options} \
            "${tmp_file}"
        ./pgo "${tmp_file}"
    fi
    cd "${pwd}"
    rm "${tmp_file}"

    if [ ${compiler} = clang ]; then
        llvm-profdata merge "${build_dir}"/default_*.profraw --output="${build_dir}/default.profdata"
    fi

    meson configure "${build_dir}" -Db_pgo=use
else
    echo "Not doing PGO"
fi