File: sysFunctions

package info (click to toggle)
openfoam 1912.200626-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 238,940 kB
  • sloc: cpp: 1,159,638; sh: 15,902; ansic: 5,195; lex: 660; xml: 387; python: 282; awk: 212; makefile: 103; sed: 88; csh: 3
file content (393 lines) | stat: -rw-r--r-- 10,249 bytes parent folder | download | duplicates (2)
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#----------------------------------*-sh-*--------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     |
#   \\  /    A nd           | www.openfoam.com
#    \\/     M anipulation  |
#------------------------------------------------------------------------------
#     Copyright (C) 2018-2020 OpenCFD Ltd.
#------------------------------------------------------------------------------
# License
#     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
#
# Script
#     sysFunctions
#
# Description
#     General system helper functions
#
# Functions provided
#     isNone, isSystem, isAbsdir, hasAbsdir
#     isDarwin, isWindows
#     findFirstFile
#     findSystemInclude
#     findLibrary
#     findExtLib
#     versionCompare
#
# Internal variables used
#     extLibraries
#
# External variables used
#     WM_OSTYPE  (is set for Windows)
#     WM_COMPILER_LIB_ARCH
#     DEB_TARGET_MULTIARCH
#
#------------------------------------------------------------------------------

if [ -z "$WMAKE_SCRIPTS_SYSFUNCTIONS" ]
then
    # Load once, but do not rely on this variable elsewhere
    WMAKE_SCRIPTS_SYSFUNCTIONS=loaded

    # Debian multi-arch, ignore missing/bad dpkg-architecture.
    if [ -z "$DEB_TARGET_MULTIARCH" ]
    then
        DEB_TARGET_MULTIARCH=$(dpkg-architecture -qDEB_TARGET_MULTIARCH 2>/dev/null || true)
    fi

    # True if OS is Darwin.
    isDarwin()
    {
        test Darwin = "$(uname -s 2>/dev/null || true)"
    }

    # True if target OS is Windows
    isWindows()
    {
        test MSwindows = "$WM_OSTYPE"
    }

    # Static, dynamic library extensions
    extLibraries=".a .so"

    if isDarwin
    then
        extLibraries=".a .dylib"
    elif isWindows
    then
        extLibraries=".a .dll .dll.a"  # including cross-compiling
    fi

    # True if '$1' begins with '/'
    isAbsdir()
    {
        test "$1" = "/${1#/}"
    }


    # True if '$1' begins with '/' and also exists as a directory
    hasAbsdir()
    {
        test "$1" = "/${1#/}" -a -d "$1"
    }


    # True if '$1' is an empty string, "none" or ends in "-none"
    # Eg,
    #    if isNone "$BOOST_ARCH_PATH" ...
    isNone()
    {
        test -z "$1" -o "${1##*-}" = none
    }


    # True if '$1' is "system" or ends in "-system"
    # Eg,
    #    if isSystem "$BOOST_ARCH_PATH"
    isSystem()
    {
        test "${1##*-}" = system
    }


    # True if '$1' and '$2' have the same directory basename
    # Eg,
    #    equalBaseName "/usr/include/scotch-int32" "scotch-int32"
    equalBaseName()
    {
        test "${1##*/}" = "${2##*/}"
    }


    # Simple output for -query
    # $1 = software
    # $2 = setting
    _process_query()
    {
        if isNone "$2"
        then
            echo "$1=none"
        elif isAbsdir "$2"    ## not hasAbsdir
        then
            echo "$1=${2##*/}"
        elif isSystem "$2"
        then
            echo "$1=system"
        else
            echo "$1=unknown"
        fi
    }


    # Return system prefix (/usr, /usr/local, ...) based on hint provided
    # Eg,
    #    sysPrefix "/usr/local/include/fftw3.h"  -> "/usr/local"
    #
    # Without a hint, echoes "/usr"
    sysPrefix()
    {
        case "$1" in
        /usr/local/*)
            echo "/usr/local"
            ;;
        *)
            echo "/usr"
            ;;
        esac
    }


    # Check existence of any of the files
    # On success, echoes the file found and returns 0, otherwise returns 2
    findFirstFile()
    {
        local file

        for file
        do
            if [ -f "$file" ] && [ -r "$file" ]
            then
                echo "$file"  # Found
                return 0
            fi
        done
        return 2
    }

    # Check system /usr/local/include /usr/include paths
    #
    # On success, echoes the resolved file and returns 0, otherwise returns 2
    #
    # Specify -name=incName to search for
    #
    findSystemInclude()
    {
        local searchName

        case "$1" in
        -name=*)
            searchName="${1#*=}"
            ;;
        esac

        if [ -z "$searchName" ]
        then
            return 1
        fi

        findFirstFile \
            "/usr/local/include/$searchName" \
            "/usr/include/$searchName" \
            ;
    }

    # Check existence of library with ending '.a', '.so' ...
    #
    # On success, echoes the resolved file and returns 0, otherwise returns 2
    #
    # This function has two modes of operation.
    #
    # 1) Automated search.
    #    Specify -prefix=dirName -name=libName, optionally -local=subdirName
    #    and search for (lib, lib64, lib/x86_64..) etc.
    #
    # 2) Directed search.
    #    specify the fully qualified names to search on the parameter list
    #
    findLibrary()
    {
        local prefixDir localDir searchDir searchName
        local file ext

        searchDir=true

        while [ "$searchDir" = true ] && [ "$#" -gt 0 ]
        do
            case "$1" in
            -prefix=*)
                prefixDir="${1#*=}"
                shift
                ;;

            -local=*)
                # Prefix with directory separator
                localDir="/${1#*=}"
                shift
                ;;

            -name=*)
                searchName="${1#*=}"
                shift
                ;;

            (*)
                unset searchDir
                ;;
            esac
        done

        if [ -n "$searchName" ]
        then
            # Automated search (eg, lib/ lib64/, lib/x86_64-linux-gnu)
            # but also handle possible local versions (eg, lib/scotch-int32)

            : "${prefixDir:=/usr}"  # A reasonable default
            [ -d "$prefixDir" ] || return 2

            # Local and regular search paths
            set -- \
                "lib${localDir}" \
                "${WM_COMPILER_LIB_ARCH:+lib${WM_COMPILER_LIB_ARCH}${localDir}}" \
                "${DEB_TARGET_MULTIARCH:+lib/${DEB_TARGET_MULTIARCH}${localDir}}" \
                "lib" \
                "${WM_COMPILER_LIB_ARCH:+lib${WM_COMPILER_LIB_ARCH}}" \
                "${DEB_TARGET_MULTIARCH:+lib/${DEB_TARGET_MULTIARCH}}" \
                ;

            # Ignore empty local search path ("/")
            [ "${#localDir}" -gt 1 ] || shift 3

            ## echo "search: $# $@" 1>&2

            for searchDir in "$@"
            do
                [ -n "$searchDir" ] || continue
                for ext in '' $extLibraries
                do
                    file="$prefixDir/$searchDir/$searchName$ext"
                    if [ -f "$file" ] && [ -r "$file" ]
                    then
                        echo "$file"  # Found
                        return 0
                    fi
                done
            done

        else
            # Directed search

            for file
            do
                [ -n "$file" ] || continue
                for ext in '' $extLibraries
                do
                    if [ -f "$file$ext" ] && [ -r "$file$ext" ]
                    then
                        echo "$file$ext"  # Found
                        return 0
                    fi
                done
            done
        fi

        return 2
    }


    # Check existence of library in FOAM_EXT_LIBBIN, but conditional
    # on FOAM_EXT_LIBBIN being located in the ThirdParty directory
    #
    # On success, echoes the resolved file and returns 0, otherwise returns 2
    findExtLib()
    {
        local file

        if [ -n "$FOAM_EXT_LIBBIN" ] && \
           [ -n "$WM_THIRD_PARTY_DIR" ] && \
           [ "${FOAM_EXT_LIBBIN#$WM_THIRD_PARTY_DIR}" != "$FOAM_EXT_LIBBIN" ]
        then
            for file
            do
                if file="$(findLibrary "$FOAM_EXT_LIBBIN/$file")"
                then
                    echo "$file"
                    return 0
                fi
            done
        fi

        return 2
    }


    # Compare version tuples with syntax similar to POSIX shell,
    # but respecting dot separators.
    #
    # arg1 OP arg2
    #   OP is one of -eq, -ne, -lt, -le, -gt, or -ge.
    #   Returns true for a successful comparison.
    #   Arg1 and arg2 normally comprise positive integers, but leading content
    #   before a '-' is stripped.
    #   Missing digits are treated as '0'.
    #
    # Eg,
    #    versionCompare "software-1.2.3" -gt 1.1 && echo True
    #
    # Ad hoc handling of "git" version as always newest.
    #    "git" -gt "1.2.3" : True
    #    "1.2.3" -lt "git" : True
    versionCompare()
    {
        [ "$#" -eq 3 ] || {
            echo "Compare needs 3 arguments (was given $#)" 1>&2
            return 2
        }

        local arg1="${1#*-}"    # Strip leading prefix-
        local op="${2}"
        local arg2="${3#*-}"    # Strip leading prefix-
        local result=''         # Empty represents 'equal'

        arg1="${arg1:-0}."
        arg2="${arg2:-0}."

        if   [ "$arg1" = "$arg2" ];        then unset arg1 arg2 # Identical
        elif [ "${arg1#git}" != "$arg1" ]; then result='more'   # (git > arg2)
        elif [ "${arg2#git}" != "$arg2" ]; then result='less'   # (arg1 < git)
        fi

        while [ -z "$result" ] && [ -n "${arg1}${arg2}" ]
        do
            local digits1="${arg1%%.*}"
            local digits2="${arg2%%.*}"

            arg1="${arg1#*.}"
            arg2="${arg2#*.}"

            : "${digits1:=0}"
            : "${digits2:=0}"

            # Other handling of non-integer values?
            if   [ "$digits1" -lt "$digits2" ]; then result='less'
            elif [ "$digits1" -gt "$digits2" ]; then result='more'
            fi
        done

        case "$op" in
        (-eq | eq)  [ -z "$result" ] ;;
        (-ne | ne)  [ -n "$result" ] ;;
        (-lt | lt)  [ 'less' = "$result" ] ;;
        (-gt | gt)  [ 'more' = "$result" ] ;;
        (-le | le)  [ 'less' = "${result:-less}" ] ;;
        (-ge | ge)  [ 'more' = "${result:-more}" ] ;;
        (*)
            echo "Unknown operator: '$op'" 1>&2
            return 2
            ;;
        esac
    }
fi


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