Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-ports-kde
Path: blob/main/Mk/Scripts/functions.sh
16461 views
1
#!/bin/sh
2
# This file for common functions used for port scripts.
3
#
4
# MAINTAINER: [email protected]
5
6
# Strip (owner,group,perm) from keywords
7
_strip_perms() {
8
sed -Ee 's/^@\([^)]*\)[[:space:]]+//' \
9
-e 's/^(@[[:alpha:]]+)\([^)]*\)[[:space:]]+/\1 /' \
10
-e 's/^@@[[:alnum:]]+@@//'
11
}
12
13
# Expand TMPPLIST to absolute paths, splitting files and dirs into separate
14
# descriptors.
15
# Input:
16
# fd:0 - plist to parse
17
# Required params:
18
# PREFIX
19
# parse_comments: Whether to parse and include commented files.
20
# Output:
21
# fd:1 - list of files
22
# fd:2 - stderr
23
# fd:3 - list of directories
24
parse_plist() {
25
local cwd cwd_save commented_cwd comment line newcwd parse_comments \
26
PREFIX
27
28
PREFIX="${1}"
29
parse_comments="${2:-1}"
30
31
cwd=${PREFIX}
32
cwd_save=
33
commented_cwd=
34
_strip_perms | while read -r line; do
35
# Handle deactivated OPTIONS. Treat "@comment file" as being in
36
# the plist so it does not show up as an orphan. PLIST_SUB uses
37
# a @comment to deactive files. XXX: It would be better to
38
# make all ports use @ignore instead of @comment.
39
if [ ${parse_comments} -eq 1 -a -z "${line%%@comment *}" ]; then
40
line="${line##*@comment }"
41
# Strip (owner,group,perm) from keywords
42
# Need to do this again after stripping away @comment.
43
line="$(printf %s "$line" | _strip_perms)"
44
# Remove @comment so it can be parsed as a file,
45
# but later prepend it again to create a list of
46
# all files commented and uncommented.
47
comment="@comment "
48
# Only consider comment @cwd for commented lines
49
if [ -n "${commented_cwd}" ]; then
50
[ -z "${cwd_save}" ] && cwd_save=${cwd}
51
cwd=${commented_cwd}
52
fi
53
else
54
comment=
55
# On first uncommented line, forget about commented
56
# @cwd
57
if [ -n "${cwd_save}" ]; then
58
cwd=${cwd_save}
59
cwd_save=
60
commented_cwd=
61
fi
62
fi
63
64
case $line in
65
@dir*|'@unexec rmdir'*|'@unexec /bin/rmdir'*)
66
line="$(printf %s "$line" \
67
| sed -Ee 's/\|\|.*//;s|[[:space:]]+[0-9]*[[:space:]]*>[&]?[[:space:]]*[^[:space:]]+||g' \
68
-e "/^@unexec[[:space:]]+(\/bin\/)?rmdir( -p)?/s|([^%])%D([^%])|\1${cwd}\2|g" \
69
-e '/^@unexec[[:space:]]+(\/bin\/)?rmdir( -p)?/s|"(.*)"[[:space:]]*|\1|g' \
70
-e 's/@unexec[[:space:]]+(\/bin\/)?rmdir( -p)?[[:space:]]+//' \
71
-e 's/@dir(rm|rmtry)?[[:space:]]+//' \
72
-e 's/[[:space:]]+$//')"
73
case "$line" in
74
/*) echo >&3 "${comment}${line%/}" ;;
75
*) echo >&3 "${comment}${cwd}/${line%/}" ;;
76
esac
77
;;
78
# Handle [file] Keywords
79
@info\ *|@shell\ *|@xmlcatmgr\ *)
80
set -- $line
81
shift
82
case "$*" in
83
/*) echo "${comment}$*" ;;
84
*) echo "${comment}${cwd}/$*" ;;
85
esac
86
;;
87
@sample\ *)
88
set -- $line
89
shift
90
sample_file=$1
91
target_file=${1%.sample}
92
if [ $# -eq 2 ]; then
93
target_file=$2
94
fi
95
case "${sample_file}" in
96
/*) ;;
97
*) sample_file=${cwd}/${sample_file} ;;
98
esac
99
case "${target_file}" in
100
/*) ;;
101
*) target_file=${cwd}/${target_file} ;;
102
esac
103
# Ignore the actual file if it is in stagedir
104
echo "@comment ${target_file}"
105
echo "${comment}${sample_file}"
106
;;
107
# Handle [dir] Keywords
108
@fc\ *|@fcfontsdir\ *|@fontsdir\ *)
109
set -- $line
110
shift
111
case "$*" in
112
/*)
113
echo >&3 "${comment}$*"
114
;;
115
*)
116
echo >&3 "${comment}${cwd}/$*"
117
;;
118
esac
119
;;
120
121
# order matters here - we must check @cwd first because
122
# otherwise the @cwd* would also match it first, shadowing the
123
# @cwd) line.
124
@cwd|@cd)
125
# Don't actually reset cwd for commented @cwd
126
if [ -n "${comment}" ]; then
127
commented_cwd=${PREFIX}
128
else
129
cwd=${PREFIX}
130
fi
131
;;
132
@cwd*|@cd*)
133
set -- $line
134
newcwd=$2
135
# Don't set cwd=/ as it causes // in plist and
136
# won't match later.
137
[ "${newcwd}" = "/" ] && newcwd=
138
# Don't actually reset cwd for commented @cwd
139
if [ -n "${comment}" ]; then
140
commented_cwd=${newcwd}
141
else
142
cwd=${newcwd}
143
fi
144
unset newcwd
145
;;
146
@*) ;;
147
/*) echo "${comment}${line}" ;;
148
*) echo "${comment}${cwd}/${line}" ;;
149
esac
150
done
151
}
152
153
validate_env() {
154
local envfault
155
for i ; do
156
set -f
157
if ! (eval ": \${${i}?}" ) >/dev/null; then
158
envfault="${envfault}${envfault:+" "}${i}"
159
fi
160
set +f
161
done
162
if [ -n "${envfault}" ]; then
163
echo "Environment variable ${envfault} undefined. Aborting." \
164
| fmt >&2
165
exit 1
166
fi
167
}
168
169
export_ports_env() {
170
local export_vars make_cmd make_env var value uses
171
172
if [ -n "${HAVE_PORTS_ENV:-}" ]; then
173
return 0
174
fi
175
176
validate_env MAKE PORTSDIR
177
178
uses="python compiler:features objc"
179
180
make_env="\
181
_PORTS_ENV_CHECK=1 \
182
PACKAGE_BUILDING=1 \
183
GNU_CONFIGURE=1 \
184
USE_JAVA=1 \
185
USE_LINUX=1 \
186
"
187
188
make_cmd="${make_env}"
189
190
export_vars="$(${MAKE} -f ${PORTSDIR}/Mk/bsd.port.mk \
191
-V PORTS_ENV_VARS ${make_env} USES="${uses}")"
192
193
for var in ${export_vars}; do
194
make_cmd="${make_cmd}${make_cmd:+ }-V ${var}=\${${var}:Q}"
195
done
196
197
# Bring in all the vars, but not empty ones.
198
eval "$(${MAKE} -f ${PORTSDIR}/Mk/bsd.port.mk ${make_cmd} \
199
USES="${uses}" | grep -v '=$' | sed -e 's,\\ $,,')"
200
for var in ${export_vars}; do
201
# Export and display non-empty ones. This is not redundant
202
# with above since we're looping on all vars here; do not
203
# export a var we didn't eval in.
204
value="$(eval echo \$${var})"
205
206
if [ -n "${value}" ]; then
207
# shellcheck disable=SC2163
208
# We want to export the variable which name is in var.
209
export ${var}
210
echo "export ${var}=\"${value}\""
211
fi
212
done
213
export HAVE_PORTS_ENV=1
214
echo "export HAVE_PORTS_ENV=1"
215
}
216
217
distinfo_data() {
218
local alg file
219
220
alg=$1
221
file=$2
222
223
if [ \( -n "${dp_DISABLE_SIZE}" -a -n "${dp_NO_CHECKSUM}" \) -o ! -f "${dp_DISTINFO_FILE}" ]; then
224
exit
225
fi
226
awk -v alg="$alg" -v file="${file}" \
227
'$1 == alg && $2 == "(" file ")" {print $4}' "${dp_DISTINFO_FILE}"
228
}
229
230
check_checksum_algorithms() {
231
for alg in ${dp_CHECKSUM_ALGORITHMS}; do
232
eval "alg_executable=\$dp_$alg"
233
if [ -z "$alg_executable" ]; then
234
${dp_ECHO_MSG} "Checksum algorithm $alg: Couldn't find the executable."
235
${dp_ECHO_MSG} "Set $alg=/path/to/$alg in /etc/make.conf and try again."
236
exit 1
237
elif [ ! -x "$alg_executable" ]; then
238
${dp_ECHO_MSG} "Checksum algorithm $alg: $alg_executable is not executable."
239
${dp_ECHO_MSG} "Fix modes, or change $alg=$alg_executable in /etc/make.conf and try again."
240
exit 1
241
fi
242
done
243
}
244
escape() {
245
echo "$1" | sed -e 's/[&;()!#]/\\&/g'
246
}
247
unescape() {
248
echo "$1" | sed -e 's/\\//g'
249
}
250
251
# Fetch vars from the Makefile and set them locally.
252
# port_var_fetch ports-mgmt/pkg "" PKGNAME pkgname PKGBASE pkgbase ...
253
# the 2nd variable is for passing any wanted make arguments, such as
254
# DEPENDS_ARGS.
255
port_var_fetch() {
256
local origin="$1"
257
local make_args="$2"
258
local _makeflags _vars
259
local _portvar _var _line
260
261
_makeflags=
262
_vars=
263
shift 2
264
while [ $# -ge 2 ]; do
265
_portvar="$1"
266
_var="$2"
267
_makeflags="${_makeflags}${_makeflags:+ }-V${_portvar}"
268
_vars="${_vars}${_vars:+ }${_var}"
269
shift 2
270
done
271
set -- ${_vars}
272
while read -r _line; do
273
setvar "$1" "${_line}"
274
shift
275
done <<-EOF
276
$(${dp_MAKE} -C "${origin}" ${make_args} ${_makeflags} || echo)
277
EOF
278
}
279
280