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