summaryrefslogtreecommitdiff
path: root/expat/coverage.sh
blob: 49642aec87ecff088e67ba7fe2a8b5057d74c419 (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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#! /usr/bin/env bash
#                          __  __            _
#                       ___\ \/ /_ __   __ _| |_
#                      / _ \\  /| '_ \ / _` | __|
#                     |  __//  \| |_) | (_| | |_
#                      \___/_/\_\ .__/ \__,_|\__|
#                               |_| XML parser
#
# Copyright (c) 2017-2019 Sebastian Pipping <sebastian@pipping.org>
# Copyright (c) 2018      Marco Maggi <marco.maggi-ipsu@poste.it>
# Copyright (c) 2019      Mohammed Khajapasha <mohammed.khajapasha@intel.com>
# Licensed under the MIT license:
#
# Permission is  hereby granted,  free of charge,  to any  person obtaining
# a  copy  of  this  software   and  associated  documentation  files  (the
# "Software"),  to  deal in  the  Software  without restriction,  including
# without  limitation the  rights  to use,  copy,  modify, merge,  publish,
# distribute, sublicense, and/or sell copies of the Software, and to permit
# persons  to whom  the Software  is  furnished to  do so,  subject to  the
# following conditions:
#
# The above copyright  notice and this permission notice  shall be included
# in all copies or substantial portions of the Software.
#
# THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
# EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
# NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
# USE OR OTHER DEALINGS IN THE SOFTWARE.

export PS4='# '


_get_source_dir() {
    echo "source__${version}"
}


_get_build_dir() {
    local libbsd_part=
    if ${with_libbsd}; then
        libbsd_part=__libbsd
    fi

    local mingw_part=
    if ${with_mingw}; then
        mingw_part=__windows
    fi

    local char_part=
    if ${unicode_enabled}; then
        if ${with_unsigned_char}; then
            char_part=__ushort
        else
            char_part=__wchar_t
        fi
    else
        char_part=__char
    fi

    local xml_attr_part=
    if ${xml_attr_info_enabled}; then
        xml_attr_part=__attr_info
    fi

    local m32_part=
    if ${with_m32}; then
        m32_part=__m32
    fi

    echo "build__${version}__xml_context_${xml_context}${libbsd_part}${mingw_part}${char_part}${xml_attr_part}${m32_part}"
}


_get_coverage_dir() {
    echo "coverage__${version}"
}


_call_cmake() {
    local cmake_args=()

    ${unicode_enabled} \
            && cmake_args+=( -DEXPAT_CHAR_TYPE=wchar_t )

    ${xml_attr_info_enabled} \
            && cmake_args+=( -DEXPAT_ATTR_INFO=ON )

    if [[ ${xml_context} -eq 0 ]]; then
        cmake_args+=( -DEXPAT_CONTEXT_BYTES=OFF )
    else
        cmake_args+=( -DEXPAT_CONTEXT_BYTES=${xml_context} )
    fi

    ${with_libbsd} && cmake_args+=( -DEXPAT_WITH_LIBBSD=ON )
    ${with_mingw} && cmake_args+=( -DCMAKE_TOOLCHAIN_FILE="${abs_source_dir}"/cmake/mingw-toolchain.cmake )
    ${with_m32} && cmake_args+=( -D_EXPAT_M32=ON )

    (
        set -x
        cmake "${cmake_args[@]}" "$@" . &>> cmake.log
    )
}


_copy_to() {
    local target_dir="$1"
    [[ -d "${target_dir}" ]] && return 0

    mkdir "${target_dir}"
    git archive --format=tar "${version}" | ( cd "${target_dir}" && tar x )
}


_copy_missing_mingw_libaries() {
    # These extra files are copied because
    # * coverage GCC flags make them needed
    # * With WINEDLLPATH Wine looks for .dll.so in these folders, not .dll
    local target="$1"
    local mingw_gcc_dll_dir="$(dirname "$(ls -1 /usr/lib*/gcc/i686-w64-mingw32/*/libgcc_s_sjlj-1.dll | head -n1)")"
    for dll in libgcc_s_sjlj-1.dll libstdc++-6.dll; do
        (
            set -x
            ln -s "${mingw_gcc_dll_dir}"/${dll} "${target}"/${dll}
        )
    done

    local mingw_pthread_dll="$(ls -1 /usr/i686-w64-mingw32/lib*/libwinpthread-1.dll 2>/dev/null | head -n1)"
    if [[ -n ${mingw_pthread_dll} ]]; then
        local mingw_pthread_dll_dir="$(dirname "${mingw_pthread_dll}")"
        for dll in libwinpthread-1.dll; do
            source="${mingw_pthread_dll_dir}"/${dll}
            (
                set -x
                ln -s "${source}" "${target}"/${dll}
            )
        done
    fi

    for dll in libexpat{,w}.dll; do
        (
            set -x
            ln -s "${abs_build_dir}"/${dll} "${target}"/${dll}
        )
    done
}


_run() {
    local source_dir="$1"
    local build_dir="$2"
    local abs_source_dir="${PWD}/${source_dir}"
    local abs_build_dir="${PWD}/${build_dir}"
    local capture_dir=.

    local BASE_FLAGS='-pipe -Wall -Wextra -pedantic -Wno-overlength-strings'
    BASE_FLAGS+=' --coverage --no-inline'

    ${with_unsigned_char} && BASE_FLAGS="${BASE_FLAGS} -funsigned-char"

    local CFLAGS="-std=c99 ${BASE_FLAGS}"
    local CXXFLAGS="-std=c++98 ${BASE_FLAGS}"

    (
        set -e
        cd "${build_dir}"

        _call_cmake \
                -DCMAKE_C_FLAGS="${CFLAGS}" \
                -DCMAKE_CXX_FLAGS="${CXXFLAGS}"

        (
            set -x
            make &> build.log

            lcov -c -d "${capture_dir}" -i -o "${coverage_info}-zero" &> run.log
        )

        if ${with_mingw}; then
            for d in tests xmlwf ; do
                mkdir -p "${d}"
                _copy_missing_mingw_libaries "${d}"
            done
        fi

        set -x
        make CTEST_OUTPUT_ON_FAILURE=1 test run-xmltest

        lcov -c -d "${capture_dir}" -o "${coverage_info}-test" &>> run.log
        lcov \
                -a "${coverage_info}-zero" \
                -a "${coverage_info}-test" \
                -o "${coverage_info}-all" \
                &>> run.log

        # Make sure that files overlap in report despite different build folders
        sed "/SF:/ s,${build_dir}/,${source_dir}/," "${coverage_info}-all" > "${coverage_info}"
    ) |& sed 's,^,  ,'
    res=${PIPESTATUS[0]}

    if [[ ${res} -eq 0 ]]; then
        echo PASSED
    else
        echo FAILED >&2
        return 1
    fi
}


_merge_coverage_info() {
    local coverage_dir="$1"
    shift
    local build_dirs=( "$@" )

    mkdir -p "${coverage_dir}"
    (
        local lcov_merge_args=( -q )
        for build_dir in "${build_dirs[@]}"; do
            lcov_merge_args+=( -a "${build_dir}/${coverage_info}" )
        done
        lcov_merge_args+=( -o "${coverage_dir}/${coverage_info}" )

        set -x
        lcov "${lcov_merge_args[@]}"
    ) |& tee "${coverage_dir}/merge.log"
}


_clean_coverage_info() {
    local coverage_dir="$1"
    local dir
    for dir in CMakeFiles examples tests ; do
        local pattern="*/${dir}/*"
        (
            set -x
            lcov -q -o "${coverage_dir}/${coverage_info}" -r "${coverage_dir}/${coverage_info}" "${pattern}"
        ) |& tee "${coverage_dir}/clean.log"
    done
}


_render_html_report() {
    local coverage_dir="$1"
    (
        set -x
        genhtml -o "${coverage_dir}" "${coverage_dir}/${coverage_info}" &> "${coverage_dir}/render.log"
    )
}


_show_summary() {
    local coverage_dir="$1"
    (
        set -x
        lcov -q -l "${coverage_dir}/${coverage_info}"
    ) | grep -v '^\['
}


_main() {
    version="$(git describe --tags)"
    coverage_info=coverage.info

    local build_dirs=()
    local source_dir="$(_get_source_dir)"
    local coverage_dir="$(_get_coverage_dir)"

    _copy_to "${source_dir}"

    _build_case() {
        local build_dir="$(_get_build_dir)"

        echo "[${build_dir}]"
        _copy_to "${build_dir}"

        # Make sure we don't need to download xmlts.zip over and over again
        if [[ ${#build_dirs[*]} -gt 0 ]]; then
            ln -s "$PWD/${build_dirs[0]}/tests/xmlts.zip" "${build_dir}"/tests/
        fi

        _run "${source_dir}" "${build_dir}"

        build_dirs+=( "${build_dir}" )
    }

    # All combinations:
    with_unsigned_char=false
    with_libbsd=false
    with_m32=false
    for with_mingw in true false ; do
        for unicode_enabled in true false ; do
            if ${unicode_enabled} && ! ${with_mingw} ; then
                continue
            fi

            for xml_attr_info_enabled in true false ; do
                for xml_context in 0 1024 ; do
                    _build_case
                done
            done
        done
    done

    # Single cases:
    with_libbsd=true _build_case
    with_unsigned_char=true _build_case
    with_m32=true _build_case

    echo
    echo 'Merging coverage files...'
    _merge_coverage_info "${coverage_dir}" "${build_dirs[@]}"

    echo
    echo 'Cleaning coverage files...'
    _clean_coverage_info "${coverage_dir}"

    echo
    echo 'Rendering HTML report...'
    _render_html_report "${coverage_dir}"
    echo "--> ${coverage_dir}/index.html"

    echo
    echo 'Rendering ASCII report...'
    _show_summary "${coverage_dir}"
}


_main