summaryrefslogtreecommitdiff
path: root/.github/scripts/tsan-linux-deps-config.sh
blob: 0f4118f87318e764e72fb42b8f6da002e165452d (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/env bash

# Shared, pinned build tuple for Linux ThreadSanitizer dependencies.
# shellcheck disable=SC2034

TSAN_CACHE_EPOCH=1
TSAN_RUNTIME_BASE=ubuntu24.04
TSAN_ARCH=x86_64

TSAN_GCC_SUFFIX=13
TSAN_GCC_VERSION=13.3.0
TSAN_CC=gcc-${TSAN_GCC_SUFFIX}
TSAN_CXX=g++-${TSAN_GCC_SUFFIX}
TSAN_FC=gfortran-${TSAN_GCC_SUFFIX}
TSAN_SANITIZER_FLAGS="-fsanitize=thread -fno-omit-frame-pointer -g1 -O1"
TSAN_LINK_FLAGS="-fsanitize=thread"

TSAN_QT_VERSION=5.15.18
TSAN_QT_SOURCE_SHA256=cea1fbabf02455f3f0e8eaa839f5d6f45cdb56b62c8a83af5c1d00ac05f912ea
TSAN_QT_SOURCE_URL="https://download.qt.io/archive/qt/5.15/${TSAN_QT_VERSION}/single/qt-everywhere-opensource-src-${TSAN_QT_VERSION}.tar.xz"

TSAN_BOOST_VERSION=1.83.0
TSAN_BOOST_SOURCE_SHA256=6478edfe2f3305127cffe8caf73ea0176c53769f4bf1585be237eb30798c3b8e
TSAN_BOOST_SOURCE_URL="https://archives.boost.io/release/${TSAN_BOOST_VERSION}/source/boost_1_83_0.tar.bz2"

TSAN_HAMLIB_REF=4.7.2
TSAN_HAMLIB_COMMIT=40f63488fe0bd751b147f48d62fd217bf53713a0
TSAN_HAMLIB_PATCH_COMMIT=141e1b19a0e2d65571e5b0783c43ab2615c0cdf7

tsan_config_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"

tsan_sha256_files() {
  local file digest hashes=

  for file in "$@"; do
    if [ ! -f "$file" ]; then
      echo "Cannot hash missing file: $file" >&2
      return 1
    fi
    digest="$(sha256sum "$file" | awk '{print $1}')"
    hashes+="${digest}"$'\n'
  done
  printf '%s' "$hashes" | sha256sum | awk '{print $1}'
}

tsan_require_toolchain() {
  local compiler actual_version linker_path

  for compiler in "$TSAN_CC" "$TSAN_CXX" "$TSAN_FC"; do
    if ! command -v "$compiler" >/dev/null 2>&1; then
      echo "Pinned TSan compiler not found: $compiler" >&2
      return 1
    fi
    actual_version="$($compiler -dumpfullversion -dumpversion)"
    if [ "$actual_version" != "$TSAN_GCC_VERSION" ]; then
      echo "Pinned TSan compiler mismatch for $compiler: expected $TSAN_GCC_VERSION, got $actual_version" >&2
      return 1
    fi
  done

  TSAN_TARGET="$($TSAN_CC -dumpmachine)"
  TSAN_CC_PATH="$(readlink -f "$(command -v "$TSAN_CC")")"
  TSAN_CXX_PATH="$(readlink -f "$(command -v "$TSAN_CXX")")"
  TSAN_FC_PATH="$(readlink -f "$(command -v "$TSAN_FC")")"
  linker_path="$($TSAN_CC -print-prog-name=ld)"
  if [[ "$linker_path" != */* ]]; then
    linker_path="$(command -v "$linker_path")"
  fi
  TSAN_LINKER_PATH="$(readlink -f "$linker_path")"
  TSAN_LIBTSAN_PATH="$(readlink -f "$($TSAN_CC -print-file-name=libtsan.so)")"
  TSAN_LIBSTDCXX_PATH="$(readlink -f "$($TSAN_CXX -print-file-name=libstdc++.so)")"
  TSAN_LIBGCC_PATH="$(readlink -f "$($TSAN_CC -print-file-name=libgcc_s.so)")"

  for compiler in \
    "$TSAN_CC_PATH" "$TSAN_CXX_PATH" "$TSAN_FC_PATH" "$TSAN_LINKER_PATH" \
    "$TSAN_LIBTSAN_PATH" "$TSAN_LIBSTDCXX_PATH" "$TSAN_LIBGCC_PATH"; do
    if [ ! -f "$compiler" ]; then
      echo "Pinned TSan toolchain component not found: $compiler" >&2
      return 1
    fi
  done

  TSAN_GLIBC_VERSION="$(getconf GNU_LIBC_VERSION | tr ' ' '-')"
  TSAN_COMPILER_SHA256="$(tsan_sha256_files \
    "$TSAN_CC_PATH" "$TSAN_CXX_PATH" "$TSAN_FC_PATH" "$TSAN_LINKER_PATH")"
  TSAN_RUNTIME_SHA256="$({
    printf '%s\n' "$TSAN_GCC_VERSION" "$TSAN_TARGET" "$TSAN_GLIBC_VERSION"
    sha256sum "$TSAN_LIBTSAN_PATH" "$TSAN_LIBSTDCXX_PATH" "$TSAN_LIBGCC_PATH"
  } | sha256sum | awk '{print $1}')"
  TSAN_TOOLCHAIN_ID="gcc${TSAN_GCC_VERSION}-${TSAN_TARGET}-${TSAN_COMPILER_SHA256:0:12}-${TSAN_RUNTIME_SHA256:0:12}"
}

tsan_dependency_recipe_hash() {
  local dependency=$1 build_script

  case "$dependency" in
    qt) build_script=build-qt-tsan-linux.sh ;;
    boost) build_script=build-boost-tsan-linux.sh ;;
    hamlib) build_script=build-hamlib-tsan-linux.sh ;;
    *)
      echo "Unsupported TSan dependency: $dependency" >&2
      return 2
      ;;
  esac

  tsan_sha256_files \
    "${tsan_config_dir}/tsan-linux-deps-config.sh" \
    "${tsan_config_dir}/${build_script}" \
    "${tsan_config_dir}/verify-tsan-deps-linux.sh"
}

tsan_dependency_version() {
  case "$1" in
    qt) printf '%s\n' "$TSAN_QT_VERSION" ;;
    boost) printf '%s\n' "$TSAN_BOOST_VERSION" ;;
    hamlib) printf '%s\n' "$TSAN_HAMLIB_REF" ;;
    *) return 2 ;;
  esac
}

tsan_dependency_source_id() {
  case "$1" in
    qt) printf '%s\n' "$TSAN_QT_SOURCE_SHA256" ;;
    boost) printf '%s\n' "$TSAN_BOOST_SOURCE_SHA256" ;;
    hamlib) printf '%s+%s\n' "$TSAN_HAMLIB_COMMIT" "$TSAN_HAMLIB_PATCH_COMMIT" ;;
    *) return 2 ;;
  esac
}

tsan_dependency_cache_key() {
  local dependency=$1 version source_id recipe_hash

  if [ -z "${TSAN_TOOLCHAIN_ID:-}" ]; then
    tsan_require_toolchain
  fi
  version="$(tsan_dependency_version "$dependency")"
  source_id="$(tsan_dependency_source_id "$dependency")"
  recipe_hash="$(tsan_dependency_recipe_hash "$dependency")"
  printf '%s\n' "${dependency}-tsan-linux-${TSAN_ARCH}-${TSAN_RUNTIME_BASE}-${TSAN_TOOLCHAIN_ID}-${version}-${source_id}-epoch${TSAN_CACHE_EPOCH}-${recipe_hash}"
}

tsan_write_dependency_manifest() {
  local dependency=$1 prefix=$2 version source_id recipe_hash cache_key manifest

  tsan_require_toolchain
  version="$(tsan_dependency_version "$dependency")"
  source_id="$(tsan_dependency_source_id "$dependency")"
  recipe_hash="$(tsan_dependency_recipe_hash "$dependency")"
  cache_key="$(tsan_dependency_cache_key "$dependency")"
  manifest="${prefix}/.wsjtx-tsan-manifest"

  {
    printf 'schema=%s\n' "$TSAN_CACHE_EPOCH"
    printf 'dependency=%s\n' "$dependency"
    printf 'version=%s\n' "$version"
    printf 'source_id=%s\n' "$source_id"
    printf 'runtime_base=%s\n' "$TSAN_RUNTIME_BASE"
    printf 'architecture=%s\n' "$TSAN_ARCH"
    printf 'compiler=%s\n' "$TSAN_CC"
    printf 'compiler_version=%s\n' "$TSAN_GCC_VERSION"
    printf 'compiler_target=%s\n' "$TSAN_TARGET"
    printf 'compiler_sha256=%s\n' "$TSAN_COMPILER_SHA256"
    printf 'glibc_version=%s\n' "$TSAN_GLIBC_VERSION"
    printf 'runtime_sha256=%s\n' "$TSAN_RUNTIME_SHA256"
    printf 'recipe_sha256=%s\n' "$recipe_hash"
    printf 'cache_key=%s\n' "$cache_key"
    printf 'sanitizer_flags=%s\n' "$TSAN_SANITIZER_FLAGS"
    printf 'link_flags=%s\n' "$TSAN_LINK_FLAGS"
  } > "$manifest"
}

tsan_emit_github_output() {
  if [ -z "${GITHUB_OUTPUT:-}" ]; then
    echo "GITHUB_OUTPUT is required for --github-output" >&2
    return 2
  fi

  tsan_require_toolchain
  {
    printf 'cc=%s\n' "$TSAN_CC"
    printf 'cxx=%s\n' "$TSAN_CXX"
    printf 'fc=%s\n' "$TSAN_FC"
    printf 'gcc_version=%s\n' "$TSAN_GCC_VERSION"
    printf 'toolchain_id=%s\n' "$TSAN_TOOLCHAIN_ID"
    printf 'qt_key=%s\n' "$(tsan_dependency_cache_key qt)"
    printf 'boost_key=%s\n' "$(tsan_dependency_cache_key boost)"
    printf 'hamlib_key=%s\n' "$(tsan_dependency_cache_key hamlib)"
    printf 'hamlib_ref=%s\n' "$TSAN_HAMLIB_REF"
  } >> "$GITHUB_OUTPUT"
}

if [ "${BASH_SOURCE[0]}" = "$0" ]; then
  set -euo pipefail
  case "${1:-}" in
    --github-output)
      tsan_emit_github_output
      ;;
    --print)
      tsan_require_toolchain
      printf 'toolchain_id=%s\n' "$TSAN_TOOLCHAIN_ID"
      printf 'qt_key=%s\n' "$(tsan_dependency_cache_key qt)"
      printf 'boost_key=%s\n' "$(tsan_dependency_cache_key boost)"
      printf 'hamlib_key=%s\n' "$(tsan_dependency_cache_key hamlib)"
      ;;
    *)
      echo "Usage: $0 --github-output|--print" >&2
      exit 2
      ;;
  esac
fi