Safetensors
GGUF
File size: 8,773 Bytes
572ef52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/bin/sh
# Check for backwards-incompatible API and ABI changes between two builds
#
# Backwards-incompatible API changes, such as removing a value from an enum,
# are checked by abi-compliance-checker. Such changes can break compilation of
# existing programs.
#
# Backwards-incompatible ABI changes, such as the removal of a public function,
# are checked by libigail-tools. Such changes could break run-time dynamic
# linking of existing binaries. (We don't use a-c-c for ABI checks because it
# needs a debug build, whereas abigail does not.)
#
# Commands:
#   --generate <build-dir>: Creates API/ABI dumps in <build-dir>
#                           <build-dir> is expected to be a CMake build result
#   --check <dir1> <dir2>:  Compares dumps in <dir1> and <dir2>
#                           Comparison exit codes
#                              0: all good
#                              1: backwards-incompatible changes found
#
# Options:
#   --include-path <dir>:   a-c-c calls gcc on headers; use this option to add
#                           directories to gcc's search path
#
#
# This script would typically be used before cutting a release:
#
#   1. Generate API/ABI dump for the old version
#
#      $ check-apiabi-compat.sh --generate <build-dir-old> libfoo [ libbar ...]
#
#   2. <update source>
#
#   3. Generate API/ABI dump for the new version
#
#      $ check-apiabi-compat.sh --generate <build-dir-new> libfoo [ libbar ...]
#
#   4. Compare the two dumps
#
#      $ check-apiabi-compat.sh --check <old-build-dir> <new-build-dir>
#
#      If the check exits 0, all is fine. Otherwise, backwards-incompatible
#      changes were found, and the librar(ies) need a SOVER bump.
set -eu

# Preconditions
if ! command -v abi-compliance-checker >/dev/null 2>&1; then
    echo "abi-compliance-checker is not installed." >&2
    exit 1
elif ! command -v abidw >/dev/null 2>&1; then
    echo "abigail-tools are not installed." >&2
    exit 1
fi

# Some generic functions
usage() {
    echo "Usage: $0 [ --include-path <dir> ] --generate <build-dir> libXXX [ libYYY ... ]" >&2
    echo "       $0 [ --strict ] --check <old-build-dir> <new-build-dir>" >&2
    echo "" >&2
    echo "  --strict: fail on any API/ABI change, including backwards-compatible additions" >&2
}

get_cmake_project_name() {
    sed -nr 's/^project\("(.*)".*$/\1/p' CMakeLists.txt
}

get_cmake_version() {
    major="$(sed -nr 's/^set\([A-Z]+_VERSION_MAJOR ([0-9]+)\)$/\1/p' CMakeLists.txt)"
    minor="$(sed -nr 's/^set\([A-Z]+_VERSION_MINOR ([0-9]+)\)$/\1/p' CMakeLists.txt)"
    patch="$(sed -nr 's/^set\([A-Z]+_VERSION_PATCH ([0-9]+)\)$/\1/p' CMakeLists.txt)"
    echo "$major.$minor.$patch"
}

# Option parsing and validation
DO_GEN=0
DO_CHECK=0
STRICT=0
BUILD_DIR=
BUILD_DIR_NEW=
INCLUDE_PATHS=
while [ "$#" -gt 0 ]; do
    case "$1" in
    --generate=*)
        DO_GEN=1
        BUILD_DIR="${1#*=}"
        shift
        ;;
    --generate)
        DO_GEN=1
        if [ -z "${2:-}" ]; then
            usage
            exit 1
        fi
        BUILD_DIR="$2"
        shift 2
        ;;
    --check)
        DO_CHECK=1
        if [ -z "${2:-}" ] || [ -z "${3:-}" ]; then
            usage
            exit 1
        elif ! [ -d "$2" ]; then
            echo "$2 is not a directory." >&2
            exit 1
        elif ! [ -d "$3" ]; then
            echo "$3 is not a directory." >&2
            exit 1
        fi
        BUILD_DIR="$2"
        BUILD_DIR_NEW="$3"
        shift 3
        ;;
    --include-path=*)
        INCLUDE_PATHS="$INCLUDE_PATHS ${1#*=}"
        shift
        ;;
    --include-path)
        if [ -z "${2:-}" ]; then
            usage
            exit 1
        fi
        INCLUDE_PATHS="$INCLUDE_PATHS $2"
        shift 2
        ;;
    --strict)
        STRICT=1
        shift
        ;;
    -h | --help)
        usage
        exit 1
        ;;
    -?*)
        usage
        exit 1
        ;;
    *)
        break
        ;;
    esac
done
if [ $((DO_GEN + DO_CHECK)) -gt 1 ]; then
    echo "Can only use one --command." >&2
    exit
fi
PROJECT_NAME="$(get_cmake_project_name)"
PROJECT_VERSION="$(get_cmake_version)"
LIB_NAMES=""
while [ "$#" -gt 0 ]; do
    if [ "${1#lib}" = "$1" ]; then
        echo "Library to check must start with libXXX." >&2
        exit 1
    fi
    LIB_NAMES="$LIB_NAMES $1"
    shift
done

dump_current_api() {
    echo "Dumping API..."

    DESCRIPTOR="$BUILD_DIR/apiabi/acc-descriptor.xml"
    mkdir -p "$BUILD_DIR/apiabi"
    cat >"$DESCRIPTOR" <<EOF
<version>$PROJECT_VERSION</version>
<headers>include</headers>
<add_include_paths>$INCLUDE_PATHS</add_include_paths>
EOF

    # This addresses a bug between a-c-c and universal-ctags, manifested when
    # a name is use both for a tag and a function name
    mkdir -p "$BUILD_DIR/apiabi/.ctags.d"
    echo "--fields=-t" >"$BUILD_DIR/apiabi/.ctags.d/acc.ctags"

    # Change HOME so that .ctags.d gets picked up by universal-ctags, if used
    HOME="$BUILD_DIR/apiabi" abi-compliance-checker \
        -headers-only \
        -lib "$PROJECT_NAME" \
        -dump "$DESCRIPTOR" \
        -log-path "$BUILD_DIR/apiabi/acc.log" \
        -dump-path "$BUILD_DIR/apiabi/api.dump"
    # acc generates this file with an ancient timestamp, which confuses gzip
    touch "$BUILD_DIR/apiabi/api.dump"
}

dump_current_abi() {
    echo "Dumping ABIs ..."
    mkdir -p "$BUILD_DIR/apiabi"
    # The suppressions are needed to avoid including all the internal C++
    # symbols, and system types
    cat >"$BUILD_DIR/apiabi/abidw.suppress" <<EOF
[suppress_function]

label = suppress internal C++ mangled functions
symbol_name_regexp = ^_Z
drop = yes

[suppress_variable]
label = suppress internal C++ mangled variables
symbol_name_regexp = ^_Z
drop = yes

[suppress_type]
label = Suppress types outside of our own source
source_location_not_regexp = ^include/
drop = yes
EOF

    # In abidw 2.5, handling of undefined stuff was changed a bit
    abidw_version="$(abidw --version | sed -r 's/^abidw: ([0-9]+\.[0-9]+).*$/\1/')"
    abidw_major="${abidw_version%.*}"
    abidw_minor="${abidw_version#*.}"
    if [ "$abidw_major" -gt 2 ] || [ "$abidw_minor" -gt 4 ]; then
        abidw_undefined_syms_options="--no-load-undefined-interfaces"
    else
        abidw_undefined_syms_options="--drop-undefined-syms"
    fi

    for lib_name in $LIB_NAMES; do
        # Depending on where add_library resides, the libraries can end up in
        # build/src or build/bin
        lib_path="$BUILD_DIR/src/$lib_name.so"
        if ! [ -f "$lib_path" ]; then
            lib_path="$BUILD_DIR/bin/$lib_name.so"
            if ! [ -f "$lib_path" ]; then
                echo "Cannot find library $lib_name.so" >&2
                exit 1
            fi
        fi
        abidw \
            --headers-dir include \
            "$abidw_undefined_syms_options" \
            --suppressions "$BUILD_DIR/apiabi/abidw.suppress" \
            --out-file "${BUILD_DIR}/apiabi/$lib_name.abi.xml" \
            "$lib_path"
    done
}

# Run the actual commands
if [ "$DO_GEN" -eq 1 ]; then
    dump_current_api
    dump_current_abi
    exit 0
elif [ "$DO_CHECK" -eq 1 ]; then
    # From here on, we don't want to exit on first error
    set +e

    abi-compliance-checker \
        -strict \
        -source \
        -library "$PROJECT_NAME" \
        -old "$BUILD_DIR/apiabi/api.dump" \
        -new "$BUILD_DIR_NEW/apiabi/api.dump" \
        -src-report-path "$BUILD_DIR_NEW/apiabi/api_compat_report.html"
    API_RESULT=$?

    ABI_RESULT=0
    for xml_file in "$BUILD_DIR/apiabi/"lib*.abi.xml; do
        xml_file_new="$BUILD_DIR_NEW/apiabi/$(basename "$xml_file")"

        if ! [ -f "$xml_file_new" ]; then
            echo "Cannot compare, missing file: $xml_file_new" >&2
            exit 1
        fi

        abidiff "$xml_file" "$xml_file_new"
        res=$?
        [ "$((res & 8))" -ne 0 ] && ABI_RESULT=1
        # check bit 2 for compatible ABI changes (like new symbols) and if
        # STRICT is set then handle this as an error.
        [ "$STRICT" -eq 1 ] && [ "$((res & 4))" -ne 0 ] && ABI_RESULT=1
    done

    if [ "$API_RESULT" -gt 0 ]; then
        if [ "$STRICT" -eq 1 ]; then
            echo "ERROR: API changed — a minor version bump is required." >&2
        else
            echo "ERROR: API changed with backwards-incompatible problems — a major version bump is required." >&2
        fi
    fi
    if [ "$ABI_RESULT" -gt 0 ]; then
        if [ "$STRICT" -eq 1 ]; then
            echo "ERROR: ABI changed — a minor version bump is required." >&2
        else
            echo "ERROR: ABI changed with backwards-incompatible problems — a major version bump is required." >&2
        fi
    fi
    if [ "$((API_RESULT + ABI_RESULT))" -gt 0 ]; then
        exit 1
    fi
fi