wmake-build-info 15.2 KB
Newer Older
1
#!/bin/sh
2 3 4 5
#------------------------------------------------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     |
OpenFOAM bot's avatar
OpenFOAM bot committed
6
#   \\  /    A nd           | www.openfoam.com
7
#    \\/     M anipulation  |
OpenFOAM bot's avatar
OpenFOAM bot committed
8
#------------------------------------------------------------------------------
9
#     Copyright (C) 2018-2020 OpenCFD Ltd.
OpenFOAM bot's avatar
OpenFOAM bot committed
10
#------------------------------------------------------------------------------
11
# License
12
#     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
13 14
#
# Script
15 16
#     wmake/scripts/wmake-build-info
#     Backend for "wmake -build-info"
17 18
#
# Description
19
#     Print the api/version and other build information for the project.
20 21 22
#
# Environment
#     - WM_PROJECT_DIR
23
#     - WM_PROJECT_VERSION
24 25
#     - WM_DIR              (unset defaults to WM_PROJECT_DIR/wmake)
#
26 27 28 29 30
# Files
#     - META-INFO/{api-info,build-info}
#     - wmake/rules/General/general
#     - debian : implicitly disables git queries
#
31
# Note
32 33 34 35 36 37 38 39 40 41 42 43 44
#     There is a corner case when obtaining build information from git.
#     It is possible that OpenFOAM has been imported into a debian form,
#     in which case the '.git/' directory is most from debian and cannot
#     be used to obtain an OpenFOAM sha1 value.
#
#     Ad hoc logic (slightly messy):
#     1) Have an 'openfoam.debian/' directory.
#        Assume that debian control has been added to OpenFOAM.
#        Can use git to obtain build information.
#     2) Have a 'debian/' directory only.
#        Assume OpenFOAM has been added into a debian project.
#        Treat as '-no-git', which which has no effect
#        when building from pristine sources.
45 46 47
#
# SeeAlso
#     META-INFO/README.md for other routines that also use META-INFO.
48 49 50 51
#
#------------------------------------------------------------------------------
# Locations
rulesFile="${WM_DIR:-$WM_PROJECT_DIR/wmake}/rules/General/general"
52 53
META_INFO="$WM_PROJECT_DIR/META-INFO"
FOAM_GIT_DIR="$WM_PROJECT_DIR/.git"
54

55
printHelp() {
56
    cat<<USAGE
57

58 59 60
Usage: ${0##*/} [OPTION]
       ${0##*/} [-update] -filter FILE
options:
61
  -cmp, -check  Compare make and meta information (exit 0 for no changes)
62 63 64
  -diff         Display differences between make and meta information
                (exit code 0 for no changes)
  -dry-run      In combination with -update
65
  -filter FILE  Filter @API@, @BUILD@ tags in file with make information
66
  -no-git       Disable use of git for obtaining information
67
  -remove       Remove meta-info build information and exit
68 69 70 71 72 73 74 75
  -update       Update meta-info from make information
  -query        Report make-info and meta-info
  -query-make   Report make-info values (api, branch, build)
  -query-meta   Report meta-info values (api, branch, build)
  -show-api     Print api value from wmake/rules, or meta-info and exit
  -show-patch   Print patch value from meta-info and exit
  -help         Print the usage

76
Query/manage status of {api,branch,build} information.
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
Default without any arguments is the same as '-query-make'.

USAGE
    exit 1
}

# Report error and exit
die()
{
    exec 1>&2
    echo
    echo "Error encountered:"
    while [ "$#" -ge 1 ]; do echo "    $1"; shift; done
    echo
    echo "See '${0##*/} -help' for usage"
    echo
    exit 1
}

#------------------------------------------------------------------------------
# Parse arguments and options
#------------------------------------------------------------------------------
unset optCheck optDryRun optUpdate optQuery optFilter

while [ "$#" -gt 0 ]
do
    case "$1" in
    -h | -help*)
105
        printHelp
106
        ;;
107
    -cmp | -check)
108 109 110 111 112 113 114 115
        optCheck=true
        ;;
    -diff)
        optCheck=verbose
        ;;
    -dry-run)
        optDryRun=true
        ;;
116 117 118 119
    -remove)
        optUpdate=remove
        break # Stop now
        ;;
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    -update)
        optUpdate=true
        ;;
    -query)
        optQuery="make:meta"
        ;;
    -query-make | -query-meta)
        optQuery="$optQuery:${1##*-}"
        ;;
    -show-api)
        optQuery="api"
        ;;
    -show-patch)
        optQuery="patch"
        ;;
    -filter)
        optFilter=true
        shift # Stop here, a file name follows
        break
        ;;
140 141 142
    -no-git)
        unset FOAM_GIT_DIR
        ;;
143
    *)
144
        die "Unknown option/argument: '$1'"
145 146 147 148 149
        ;;
    esac
    shift
done

150 151 152 153
# Check environment variables
[ -d "$WM_PROJECT_DIR" ] || \
    die "Bad or unset environment variable: \$WM_PROJECT_DIR"

154 155
[ -d "$META_INFO" ] || \
    die "No ${META_INFO##*/}/ directory for project"
156

157 158 159 160
# Debian handling (see notes above)
# - disable git queries if debian project and not internal "debianization"
if [ -d "$WM_PROJECT_DIR/debian" ] \
&& [ ! -e "$WM_PROJECT_DIR/openfoam.debian" ]
161 162 163
then
    unset FOAM_GIT_DIR
fi
164

165 166
#------------------------------------------------------------------------------

167
if [ "$optUpdate" = remove ]
168
then
169

170
    if [ -f "$META_INFO/build-info" ]
171
    then
172
        echo "Removing project ${META_INFO##*/}/build-info" 1>&2
173
        rm -f "$META_INFO/build-info"
174
    else
175
        echo "Already removed project ${META_INFO##*/}/build-info" 1>&2
176 177 178 179 180 181
    fi
    exit 0

elif [ "$optFilter" = true ]
then

182 183 184 185 186 187 188
    [ -f "$1" ] || {
        echo "Error in ${0##*/}: file not found '$1'" 1>&2
        exit 2
    }

    # Disable other methods that generate output to stdout
    unset optCheck optQuery
189

190
else
191

192
    [ "$#" -eq 0 ] || die "Unexpected option/arguments $*"
193 194 195 196 197 198

    # Nothing specified? Default to -query-make
    if [ -z "$optCheck$optUpdate$optQuery" ]
    then
        optQuery="make"
    fi
199

200 201 202 203 204
fi


#------------------------------------------------------------------------------

205 206
# Variables - for portability, avoiding bash associative arrays
unset make_info meta_info
207

208 209

# Populate make_* variables
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
#
#  - api    : from rules/General/general
#  - patch  : cached value from previous make
#  - branch : from git
#  - build  : from git
#
# Failure modes:
# - No api information (can't find file etc).
#   -> FATAL: should never happen.
#
# - No git installed or no git repo
#   -> branch and build are populated as empty strings
#
# - Working on detached head.
#   -> branch has value "HEAD" instead of something more readable.
getMakeInfo()
{
227
    if [ -n "$make_info" ]
228 229 230 231 232 233
    then
        ##echo "use cached value for make info" 1>&2
        return 0
    fi
    ##echo "get make info" 1>&2

234
    local api patch build branch
235
    unset make_api make_patch make_branch make_build
236 237 238 239

    # (api) from WM_DIR/rules/General/general
    # - extract WM_VERSION = OPENFOAM=<digits>

240
    api="$(sed -ne '/^ *#/!{ /WM_VERSION.*OPENFOAM=/{ s@^.*OPENFOAM= *\([0-9][0-9]*\).*@\1@p; q }}' "$rulesFile" 2>/dev/null)"
241

242
    if [ -d "$META_INFO" ]
243 244
    then
        # (patch) from build-info - not from api-info
245
        patch="$(sed -ne 's@^patch *= *\([0-9][0-9]*\).*@\1@p' "$META_INFO/build-info" 2>/dev/null)"
246 247
    fi

248 249
    # Build info from git. Use short date format (YYYY-MM-DD) and sed instead
    # of the newer --date='format:%y%m%d'
250
    if [ -d "$FOAM_GIT_DIR" ]
251
    then
252 253 254 255 256 257 258
        build="$(git --git-dir="$FOAM_GIT_DIR" log -1 --date=short --format='%h=%ad' 2>/dev/null|sed 's/-//g;s/=/-/')"

        # Branch info from git
        if [ -n "$build" ]
        then
            branch="$(git --git-dir="$FOAM_GIT_DIR" rev-parse --abbrev-ref HEAD 2>/dev/null)"
        fi
259 260
    fi

261 262 263 264 265
    make_api="$api"
    make_patch="${patch:-0}"  # Default is 0 (unpatched)
    make_branch="$branch"
    make_build="$build"
    make_info=true
266 267 268
}


269
# Populate meta_* variables
270 271 272 273 274 275 276 277 278 279 280
#
#  - api    : from META-INFO/api-info
#  - patch  : from META-INFO/api-info
#  - branch : from META-INFO/build-info
#  - build  : from META-INFO/build-info
#
# Failure modes:
# - Directory, file or entry not found.
#   -> corresponding entries are empty strings
getMetaInfo()
{
281
    if [ -n "$meta_info" ]
282 283 284 285 286 287
    then
        ##echo "use cached value for meta info" 1>&2
        return 0
    fi
    ##echo "get meta info" 1>&2

288
    local api patch build branch
289
    unset meta_api meta_patch meta_branch meta_build
290

291
    if [ -d "$META_INFO" ]
292 293 294 295
    then
        # (api, patch) from api-info
        # (branch, build) from build-info

296 297 298 299
        api="$(sed -ne 's@^api *= *\([0-9][0-9]*\).*@\1@p' "$META_INFO/api-info" 2>/dev/null)"
        patch="$(sed -ne 's@^patch *= *\([0-9][0-9]*\).*@\1@p' "$META_INFO/api-info" 2>/dev/null)"
        branch="$(sed -ne 's@^branch *= *\([^ ]*\).*@\1@p' "$META_INFO/build-info" 2>/dev/null)"
        build="$(sed -ne 's@^build *= *\([^ ]*\).*@\1@p' "$META_INFO/build-info" 2>/dev/null)"
300 301
    fi

302 303 304 305 306
    meta_api="$api"
    meta_patch="${patch:-0}"  # Default is 0 (unpatched)
    meta_branch="$branch"
    meta_build="$build"
    meta_info=true
307 308 309 310 311 312 313 314 315 316 317 318
}


# Get api from rules/General/general
#
# Failure modes:
# - No api information (can't find file etc).
#   -> Fatal for building, but could be OK for a stripped down version
#
# Fallback. Get from api-info
getApi()
{
319
    getMakeInfo
320 321

    # Local copy
322
    local api="${make_api}"
323 324 325

    if [ -z "$api" ]
    then
326
        getMetaInfo
327
        api="${meta_api}"
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
    fi

    if [ -n "$api" ]
    then
        echo "$api"
    else
        return 1
    fi
}


# Get patch from meta-info / api-info
#
# Failure modes:
# - No patch information (can't find file etc).
getPatchLevel()
{
345
    getMetaInfo
346 347

    # Local copy
348
    local patch="${meta_patch}"
349

350
    if [ -n "$patch" ]
351
    then
352
        echo "$patch"
353 354 355 356 357 358
    else
        return 1
    fi
}


359
#
360
# Report make info
361
#
362 363
reportMakeInfo()
{
364 365
    getMakeInfo
    getMetaInfo
366 367

    echo "make"
368 369 370 371
    echo "    api = ${make_api}"
    echo "    patch = ${meta_patch:-0}"  # <- From meta-info only
    echo "    branch = ${make_branch}"
    echo "    build = ${make_build}"
372 373 374
}


375
#
376
# Report meta info
377
#
378 379
reportMetaInfo()
{
380
    getMetaInfo
381 382

    echo "meta"
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
    echo "    api = ${meta_api}"
    echo "    patch = ${meta_patch:-0}"  # <- From meta-info only
    echo "    branch = ${meta_branch}"
    echo "    build = ${meta_build}"
}


# Report diff between make and meta info (single key).
# Set diff_header prior to the first call.
# $1 == key
# $2 == make value
# $3 == meta value
unset diff_header
_reportDiff()
{
    if [ -n "$diff_header" ]
    then
        echo "$diff_header"
        unset diff_header
    fi
    echo "$1:"
    echo "    make $2"
    echo "    meta $3"
406 407 408 409 410 411 412 413
}


# Test make vs meta info.
# Return 0 for no differences, 1 otherwise
# $1 == verbose, print as diff. Silent otherwise
checkDiff()
{
414 415 416 417 418 419 420
    local diff verbose

    if [ "$1" = "verbose" ]
    then
        diff_header="Differences"
        verbose=true
    fi
421

422 423
    getMakeInfo
    getMetaInfo
424

425 426 427 428 429 430
    # api
    if [ "${make_api}" != "${meta_api}" ]
    then
        diff=true

        if [ -n "$verbose" ]
431
        then
432
            _reportDiff "api" "${make_api}" "${meta_api}"
433
        fi
434
    fi
435

436 437
    # patch
    if [ "${make_patch}" != "${meta_patch}" ]
438
    then
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
        diff=true

        if [ -n "$verbose" ]
        then
            _reportDiff "patch" "${make_patch}" "${meta_patch}"
        fi
    fi

    # branch - only test when make info is non-empty
    if [ -n "${make_branch}" ] && [ "${make_branch}" != "${meta_branch}" ]
    then
        diff=true

        if [ -n "$verbose" ]
        then
            _reportDiff "branch" "${make_branch}" "${meta_branch}"
        fi
    fi

    # build - only test when make info is non-empty
    if [ -n "${make_build}" ] && [ "${make_build}" != "${meta_build}" ]
    then
        diff=true

        if [ -n "$verbose" ]
        then
            _reportDiff "build" "${make_build}" "${meta_build}"
        fi
467 468
    fi

469
    # No diffs, but never permit entirely empty values for build.
470
    test -z "$diff" || test -z "${make_build}${meta_build}"
471 472 473 474
}


#
475
# Update meta info (on disk) based on the make info
476 477 478
#
performUpdate()
{
479 480
    getMakeInfo
    getMetaInfo
481 482

    # Local copies of the make info
483 484 485 486
    local api="${make_api}"
    local branch="${make_branch}"
    local build="${make_build}"
    local patch="${make_patch}"
487 488 489

    # If any of the make-info are empty (bad),
    # use the meta-info to avoid spurious changes
490 491 492
    [ -n "$api" ] || api="${meta_api}"
    [ -n "$branch" ] || branch="${meta_branch}"
    [ -n "$build" ] || build="${meta_build}"
493

494 495 496
    # Fallback to WM_PROJECT_VERSION alone
    [ -n "$build" ] || build="${WM_PROJECT_VERSION:-unknown}"

497 498 499
    local outputFile

    # build-info
500
    outputFile="$META_INFO/build-info"
501 502 503
    if [ "$branch" != "${meta_branch}" ] || \
       [ "$build" != "${meta_build}" ] || \
       [ "$patch" != "${meta_patch}" ]
504
    then
505
        patch="${meta_patch:-0}"        # <- From meta-info only
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520

        if [ -n "$optDryRun" ]
        then
            echo "dry-run (update) ${outputFile##*/} branch=${branch}" 1>&2
            echo "dry-run (update) ${outputFile##*/} build=${build}" 1>&2
            echo "dry-run (update) ${outputFile##*/} patch=${patch}" 1>&2
        else
            echo "branch=${branch}" >| "$outputFile"
            echo "build=${build}"   >> "$outputFile"
            echo "patch=${patch}"   >> "$outputFile"
        fi
    fi


    # api-info
521
    outputFile="$META_INFO/api-info"
522
    if [ "$api" != "${meta_api}" ]
523
    then
524
        patch="${meta_patch:-0}"        # <- From meta-info only
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540

        if [ -n "$optDryRun" ]
        then
            echo "dry-run (update) ${outputFile##*/} api=${api}" 1>&2
            echo "dry-run (update) ${outputFile##*/} patch=${patch}" 1>&2
        else
            echo "api=${api}"     >| "$outputFile"
            echo "patch=${patch}" >> "$outputFile"
        fi
    fi

    return 0
}


#
541
# Update meta info (on disk) based on the make info
542 543 544 545 546 547 548 549 550 551
#
performFiltering()
{
    local input="$1"

    [ -f "$input" ] || {
        echo "Error in ${0##*/}: file not found '$1'" 1>&2
        exit 2
    }

552 553
    getMakeInfo
    getMetaInfo
554 555

    # Local copies of the make info
556 557 558 559
    local api="${make_api}"
    local branch="${make_branch}"
    local build="${make_build}"
    local patch="${meta_patch:-0}"  # <- From meta-info only
560 561 562 563 564 565 566 567 568 569


    # If any of the make-info are empty (bad),
    # conjure up something from the meta-info

    # api is not normally needed (available directly from -Ddefine)
    # but we may wish to filter other types of files

    if [ -z "$api" ]
    then
570
        api="${meta_api}"
571 572 573 574 575 576
        api="${api:-0}"  # integer value
    fi

    # branch/build could be missing for non-git
    if [ -z "$branch" ]
    then
577
        branch="${meta_branch}"
578 579
        branch="${branch:-unknown}"
    fi
580

581 582
    if [ -z "$build" ]
    then
583
        build="${meta_build}"
584 585
        # Fallback to WM_PROJECT_VERSION
        build="${build:-${WM_PROJECT_VERSION:-unknown}}"
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
    fi

    sed \
        -e 's!@API@!'"${api}"'!g' \
        -e 's!@PATCH@!'"${patch:-0}"'!g' \
        -e 's!@BRANCH@!'"${branch}"'!g' \
        -e 's!@BUILD@!'"${build}"'!g' \
        -e 's!@VERSION@!'"${WM_PROJECT_VERSION}"'!g' \
        "$input"

    return 0
}


#------------------------------------------------------------------------------

602
# Dispatch
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634

if [ -n "$optCheck" ]
then
    checkDiff $optCheck
    exit $?
elif [ "$optQuery" = api ]
then
    # Show API and exit
    getApi
    exit $?
elif [ "$optQuery" = patch ]
then
    # Show patch level and exit
    getPatchLevel
    exit $?
else
    # Other queries
    case "$optQuery" in (*make*) reportMakeInfo ;; esac
    case "$optQuery" in (*meta*) reportMetaInfo ;; esac
fi

[ -n "$optUpdate" ] && performUpdate

if [ -n "$optFilter" ]
then
    # Perform filter on file
    performFiltering "$1"
fi

exit 0  # clean exit

#------------------------------------------------------------------------------