interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / scripts / check-apiabi-compat.sh

292 lines · 8841 bytes Raw Blame History
1#!/bin/sh
2# Check for backwards-incompatible API and ABI changes between two builds
3#
4# Backwards-incompatible API changes, such as removing a value from an enum,
5# are checked by abi-compliance-checker. Such changes can break compilation of
6# existing programs.
7#
8# Backwards-incompatible ABI changes, such as the removal of a public function,
9# are checked by libigail-tools. Such changes could break run-time dynamic
10# linking of existing binaries. (We don't use a-c-c for ABI checks because it
11# needs a debug build, whereas abigail does not.)
12#
13# Commands:
14# --generate <build-dir>: Creates API/ABI dumps in <build-dir>
15# <build-dir> is expected to be a CMake build result
16# --check <dir1> <dir2>: Compares dumps in <dir1> and <dir2>
17# Comparison exit codes
18# 0: all good
19# 1: backwards-incompatible changes found
20#
21# Options:
22# --include-path <dir>: a-c-c calls gcc on headers; use this option to add
23# directories to gcc's search path
24#
25#
26# This script would typically be used before cutting a release:
27#
28# 1. Generate API/ABI dump for the old version
29#
30# $ check-apiabi-compat.sh --generate <build-dir-old> libfoo [ libbar ...]
31#
32# 2. <update source>
33#
34# 3. Generate API/ABI dump for the new version
35#
36# $ check-apiabi-compat.sh --generate <build-dir-new> libfoo [ libbar ...]
37#
38# 4. Compare the two dumps
39#
40# $ check-apiabi-compat.sh --check <old-build-dir> <new-build-dir>
41#
42# If the check exits 0, all is fine. Otherwise, backwards-incompatible
43# changes were found, and the librar(ies) need a SOVER bump.
44set -eu
45
46# Preconditions
47if ! command -v abi-compliance-checker >/dev/null 2>&1; then
48 echo "abi-compliance-checker is not installed." >&2
49 exit 1
50elif ! command -v abidw >/dev/null 2>&1; then
51 echo "abigail-tools are not installed." >&2
52 exit 1
53fi
54
55# Some generic functions
56usage() {
57 echo "Usage: $0 [ --include-path <dir> ] --generate <build-dir> libXXX [ libYYY ... ]" >&2
58 echo " $0 [ --strict ] --check <old-build-dir> <new-build-dir>" >&2
59 echo "" >&2
60 echo " --strict: fail on any API/ABI change, including backwards-compatible additions" >&2
61}
62
63get_cmake_project_name() {
64 sed -nr 's/^project\("(.*)".*$/\1/p' CMakeLists.txt
65}
66
67get_cmake_version() {
68 major="$(sed -nr 's/^set\([A-Z]+_VERSION_MAJOR ([0-9]+)\)$/\1/p' CMakeLists.txt)"
69 minor="$(sed -nr 's/^set\([A-Z]+_VERSION_MINOR ([0-9]+)\)$/\1/p' CMakeLists.txt)"
70 patch="$(sed -nr 's/^set\([A-Z]+_VERSION_PATCH ([0-9]+)\)$/\1/p' CMakeLists.txt)"
71 echo "$major.$minor.$patch"
72}
73
74# Option parsing and validation
75DO_GEN=0
76DO_CHECK=0
77STRICT=0
78BUILD_DIR=
79BUILD_DIR_NEW=
80INCLUDE_PATHS=
81while [ "$#" -gt 0 ]; do
82 case "$1" in
83 --generate=*)
84 DO_GEN=1
85 BUILD_DIR="${1#*=}"
86 shift
87 ;;
88 --generate)
89 DO_GEN=1
90 if [ -z "${2:-}" ]; then
91 usage
92 exit 1
93 fi
94 BUILD_DIR="$2"
95 shift 2
96 ;;
97 --check)
98 DO_CHECK=1
99 if [ -z "${2:-}" ] || [ -z "${3:-}" ]; then
100 usage
101 exit 1
102 elif ! [ -d "$2" ]; then
103 echo "$2 is not a directory." >&2
104 exit 1
105 elif ! [ -d "$3" ]; then
106 echo "$3 is not a directory." >&2
107 exit 1
108 fi
109 BUILD_DIR="$2"
110 BUILD_DIR_NEW="$3"
111 shift 3
112 ;;
113 --include-path=*)
114 INCLUDE_PATHS="$INCLUDE_PATHS ${1#*=}"
115 shift
116 ;;
117 --include-path)
118 if [ -z "${2:-}" ]; then
119 usage
120 exit 1
121 fi
122 INCLUDE_PATHS="$INCLUDE_PATHS $2"
123 shift 2
124 ;;
125 --strict)
126 STRICT=1
127 shift
128 ;;
129 -h | --help)
130 usage
131 exit 1
132 ;;
133 -?*)
134 usage
135 exit 1
136 ;;
137 *)
138 break
139 ;;
140 esac
141done
142if [ $((DO_GEN + DO_CHECK)) -gt 1 ]; then
143 echo "Can only use one --command." >&2
144 exit
145fi
146PROJECT_NAME="$(get_cmake_project_name)"
147PROJECT_VERSION="$(get_cmake_version)"
148LIB_NAMES=""
149while [ "$#" -gt 0 ]; do
150 if [ "${1#lib}" = "$1" ]; then
151 echo "Library to check must start with libXXX." >&2
152 exit 1
153 fi
154 LIB_NAMES="$LIB_NAMES $1"
155 shift
156done
157
158dump_current_api() {
159 echo "Dumping API..."
160
161 DESCRIPTOR="$BUILD_DIR/apiabi/acc-descriptor.xml"
162 mkdir -p "$BUILD_DIR/apiabi"
163 cat >"$DESCRIPTOR" <<EOF
164<version>$PROJECT_VERSION</version>
165<headers>include</headers>
166<add_include_paths>$INCLUDE_PATHS</add_include_paths>
167EOF
168
169 # This addresses a bug between a-c-c and universal-ctags, manifested when
170 # a name is use both for a tag and a function name
171 mkdir -p "$BUILD_DIR/apiabi/.ctags.d"
172 echo "--fields=-t" >"$BUILD_DIR/apiabi/.ctags.d/acc.ctags"
173
174 # Change HOME so that .ctags.d gets picked up by universal-ctags, if used
175 HOME="$BUILD_DIR/apiabi" abi-compliance-checker \
176 -headers-only \
177 -lib "$PROJECT_NAME" \
178 -dump "$DESCRIPTOR" \
179 -log-path "$BUILD_DIR/apiabi/acc.log" \
180 -dump-path "$BUILD_DIR/apiabi/api.dump"
181 # acc generates this file with an ancient timestamp, which confuses gzip
182 touch "$BUILD_DIR/apiabi/api.dump"
183}
184
185dump_current_abi() {
186 echo "Dumping ABIs ..."
187 mkdir -p "$BUILD_DIR/apiabi"
188 # The suppressions are needed to avoid including all the internal C++
189 # symbols, and system types
190 cat >"$BUILD_DIR/apiabi/abidw.suppress" <<EOF
191[suppress_function]
192
193label = suppress internal C++ mangled functions
194symbol_name_regexp = ^_Z
195drop = yes
196
197[suppress_variable]
198label = suppress internal C++ mangled variables
199symbol_name_regexp = ^_Z
200drop = yes
201
202[suppress_type]
203label = Suppress types outside of our own source
204source_location_not_regexp = ^include/
205drop = yes
206EOF
207
208 # In abidw 2.5, handling of undefined stuff was changed a bit
209 abidw_version="$(abidw --version | sed -r 's/^abidw: ([0-9]+\.[0-9]+).*$/\1/')"
210 abidw_major="${abidw_version%.*}"
211 abidw_minor="${abidw_version#*.}"
212 if [ "$abidw_major" -gt 2 ] || [ "$abidw_minor" -gt 4 ]; then
213 abidw_undefined_syms_options="--no-load-undefined-interfaces"
214 else
215 abidw_undefined_syms_options="--drop-undefined-syms"
216 fi
217
218 for lib_name in $LIB_NAMES; do
219 # Depending on where add_library resides, the libraries can end up in
220 # build/src or build/bin
221 lib_path="$BUILD_DIR/src/$lib_name.so"
222 if ! [ -f "$lib_path" ]; then
223 lib_path="$BUILD_DIR/bin/$lib_name.so"
224 if ! [ -f "$lib_path" ]; then
225 echo "Cannot find library $lib_name.so" >&2
226 exit 1
227 fi
228 fi
229 abidw \
230 --headers-dir include \
231 "$abidw_undefined_syms_options" \
232 --suppressions "$BUILD_DIR/apiabi/abidw.suppress" \
233 --out-file "${BUILD_DIR}/apiabi/$lib_name.abi.xml" \
234 "$lib_path"
235 done
236}
237
238# Run the actual commands
239if [ "$DO_GEN" -eq 1 ]; then
240 dump_current_api
241 dump_current_abi
242 exit 0
243elif [ "$DO_CHECK" -eq 1 ]; then
244 # From here on, we don't want to exit on first error
245 set +e
246
247 ACC_STRICT=
248 [ "$STRICT" -eq 0 ] || ACC_STRICT="-strict"
249 abi-compliance-checker \
250 $ACC_STRICT \
251 -source \
252 -library "$PROJECT_NAME" \
253 -old "$BUILD_DIR/apiabi/api.dump" \
254 -new "$BUILD_DIR_NEW/apiabi/api.dump" \
255 -src-report-path "$BUILD_DIR_NEW/apiabi/api_compat_report.html"
256 API_RESULT=$?
257
258 ABI_RESULT=0
259 for xml_file in "$BUILD_DIR/apiabi/"lib*.abi.xml; do
260 xml_file_new="$BUILD_DIR_NEW/apiabi/$(basename "$xml_file")"
261
262 if ! [ -f "$xml_file_new" ]; then
263 echo "Cannot compare, missing file: $xml_file_new" >&2
264 exit 1
265 fi
266
267 abidiff "$xml_file" "$xml_file_new"
268 res=$?
269 [ "$((res & 8))" -ne 0 ] && ABI_RESULT=1
270 # check bit 2 for compatible ABI changes (like new symbols) and if
271 # STRICT is set then handle this as an error.
272 [ "$STRICT" -eq 1 ] && [ "$((res & 4))" -ne 0 ] && ABI_RESULT=1
273 done
274
275 if [ "$API_RESULT" -gt 0 ]; then
276 if [ "$STRICT" -eq 1 ]; then
277 echo "ERROR: API changed — a minor version bump is required." >&2
278 else
279 echo "ERROR: API changed with backwards-incompatible problems — a major version bump is required." >&2
280 fi
281 fi
282 if [ "$ABI_RESULT" -gt 0 ]; then
283 if [ "$STRICT" -eq 1 ]; then
284 echo "ERROR: ABI changed — a minor version bump is required." >&2
285 else
286 echo "ERROR: ABI changed with backwards-incompatible problems — a major version bump is required." >&2
287 fi
288 fi
289 if [ "$((API_RESULT + ABI_RESULT))" -gt 0 ]; then
290 exit 1
291 fi
292fi