File: utils.sh

package info (click to toggle)
liburcu 0.15.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,356 kB
  • sloc: ansic: 23,361; xml: 23,227; sh: 6,480; makefile: 1,045; cpp: 15
file content (112 lines) | stat: -rwxr-xr-x 2,669 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
#!/usr/bin/env bash
#
# SPDX-License-Identifier: GPL-2.0-only
#
# SPDX-FileCopyrightText: 2022 EfficiOS Inc.
#

# This file is meant to be sourced at the start of shell script-based tests.


# Error out when encountering an undefined variable
set -u

# If "readlink -f" is available, get a resolved absolute path to the
# tests source dir, otherwise make do with a relative path.
scriptdir="$(dirname "${BASH_SOURCE[0]}")"
if readlink -f "." >/dev/null 2>&1; then
	testsdir=$(readlink -f "$scriptdir/..")
else
	testsdir="$scriptdir/.."
fi

# The OS on which we are running. See [1] for possible values of 'uname -s'.
# We do a bit of translation to ease our life down the road for comparison.
# Export it so that called executables can use it.
# [1] https://en.wikipedia.org/wiki/Uname#Examples
if [ "x${URCU_TESTS_OS_TYPE:-}" = "x" ]; then
	URCU_TESTS_OS_TYPE="$(uname -s)"
	case "$URCU_TESTS_OS_TYPE" in
	MINGW*)
		URCU_TESTS_OS_TYPE="mingw"
		;;
	Darwin)
		URCU_TESTS_OS_TYPE="darwin"
		;;
	Linux)
		URCU_TESTS_OS_TYPE="linux"
		;;
	CYGWIN*)
		URCU_TESTS_OS_TYPE="cygwin"
		;;
	*)
		URCU_TESTS_OS_TYPE="unsupported"
		;;
	esac
fi
export URCU_TESTS_OS_TYPE

# Allow overriding the source and build directories
if [ "x${URCU_TESTS_SRCDIR:-}" = "x" ]; then
	URCU_TESTS_SRCDIR="$testsdir"
fi
export URCU_TESTS_SRCDIR

if [ "x${URCU_TESTS_BUILDDIR:-}" = "x" ]; then
	URCU_TESTS_BUILDDIR="$testsdir"
fi
export URCU_TESTS_BUILDDIR

# Source the generated environment file if it's present.
if [ -f "${URCU_TESTS_BUILDDIR}/utils/env.sh" ]; then
	# shellcheck source=./env.sh
	. "${URCU_TESTS_BUILDDIR}/utils/env.sh"
fi


### External Tools ###

if [ "x${URCU_TESTS_NPROC_BIN:-}" = "x" ]; then
	URCU_TESTS_NPROC_BIN="nproc"
fi
export URCU_TESTS_NPROC_BIN

if [ "x${URCU_TESTS_GETCONF_BIN:-}" = "x" ]; then
	URCU_TESTS_GETCONF_BIN="getconf"
fi
export URCU_TESTS_GETCONF_BIN


# By default, it will not source tap.sh.  If you to tap output directly from
# the test script, define the 'SH_TAP' variable to '1' before sourcing this
# script.
if [ "x${SH_TAP:-}" = x1 ]; then
	# shellcheck source=./tap.sh
	. "${URCU_TESTS_SRCDIR}/utils/tap.sh"
fi


### Functions ###

# Print the number of online CPUs, fallback to '1' if no tools to count CPUs
# are found in the environment.
urcu_nproc() {
	if command -v "${URCU_TESTS_NPROC_BIN}" >/dev/null 2>&1; then
		"${URCU_TESTS_NPROC_BIN}"
	elif command -v "${URCU_TESTS_GETCONF_BIN}" >/dev/null 2>&1; then
		"${URCU_TESTS_GETCONF_BIN}" _NPROCESSORS_ONLN
	else
		# Fallback to '1'
		echo 1
	fi
}

# Shell implementation of the 'seq' command.
urcu_xseq() {
	local i=$1

	while [[ "$i" -le "$2" ]]; do
		echo "$i"
		i=$(( i + 1 ))
	done
}