-
Notifications
You must be signed in to change notification settings - Fork 3
/
compiler-shadow.eselect
203 lines (165 loc) · 4.36 KB
/
compiler-shadow.eselect
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
# Copyright (c) 2017 Michał Górny
# Distributed under the terms of the 2-clause BSD license
inherit package-manager
DESCRIPTION="Manage compiler shadow link directories (distcc, ccache)"
MAINTAINER="mgorny@gentoo.org"
VERSION=4
# {{{ substitution variables
MASQ_MODULEDIR=$(dirname $(realpath "${BASH_SOURCE}"))/modules
# }}}
# {{{ configuration loaders
load_compilers() {
cd "${MASQ_MODULEDIR}"/compilers || die "Unable to enter ${MASQ_MODULEDIR}/compilers"
shopt -s nullglob
local files=( * )
shopt -u nullglob
MASQ_COMPILER_WILDCARDS=()
shopt -o -s noglob
local f
for f in "${files[@]}"; do
local wildcards
{
# 1st line has wildcards
read -r wildcards
# we ignore remaining lines for future extensions
} <"${f}"
declare -g -a "MASQ_COMPILER_WILDCARDS+=(${wildcards})"
done
shopt -o -u noglob
}
load_tools() {
cd "${MASQ_MODULEDIR}"/tools || die "Unable to enter ${MASQ_MODULEDIR}/tools"
shopt -s nullglob
local files=( * )
shopt -u nullglob
MASQ_TOOLS=()
local f
for f in "${files[@]}"; do
{
# 1st line has masq dir path
read -r "MASQ_DIR_${f}"
# we ignore remaining lines for future extensions
} <"${f}"
MASQ_TOOLS+=( "${f}" )
done
}
load_compilers
load_tools
# }}}
# {{{ helper functions
# print list of tools that are found installed on the system
# (via PATH lookup)
get_installed_tools() {
local t
for t in "${MASQ_TOOLS[@]}"; do
type -P "${t}" &>/dev/null &&
echo "${t}"
done
}
# }}}
## {{{ update masquerade
describe_update() {
echo "Update the shadow link directory"
}
describe_update_parameters() {
echo "[{<tool>|all} [<wildcard>]]"
}
describe_update_options() {
echo "<tool> : Tool(s) to update or 'all' (the default) to update all (${MASQ_TOOLS[*]})"
echo "<wildcard> : Link all executables matching the wildcard (as found in PATH), otherwise default list"
}
do_update() {
local tools=( ${1:-all} )
local wildcards=( "${2}" )
shopt -s nullglob
[[ ${tools} == all ]] && tools=( $(get_installed_tools) )
[[ ${wildcards} ]] || wildcards=(
"${MASQ_COMPILER_WILDCARDS[@]}"
)
local t
for t in "${tools[@]}"; do
has "${t}" "${MASQ_TOOLS[@]}" || die -q "Unknown tool: ${t}"
echo "Updating masquerade for ${t} ..."
local out_dir_var=MASQ_DIR_${t}
local out_dir=${!out_dir_var}
local tool_exec=$(type -P "${t}")
[[ ${tool_exec} ]] || die -q "Tool not found in PATH: ${t}"
mkdir -p "${out_dir}" || die
cd "${out_dir}" || die
local p
for p in "${wildcards[@]}"; do
# remove existing links matching the wildcard
rm -f ${p} || die
# create new links
local exe
while read -r exe; do
ln -v -s "${tool_exec}" "${exe}" || die
done < <(compgen -c -X "!${p}" | sort -u)
# note: compgen can return a command twice if it is included
# in multiple directories in PATH
done
done
}
## }}}
## {{{ clean stale symlinks
describe_clean() {
echo "Clean stale symlinks from the shadow directory"
}
describe_clean_parameters() {
echo "[<tool>|all]"
}
describe_clean_options() {
echo "<tool> : Tool(s) to clean or 'all' (the default) to update all (${MASQ_TOOLS[*]})"
}
do_clean() {
local tools=( ${1:-all} )
shopt -s nullglob
[[ ${tools} == all ]] && tools=( $(get_installed_tools) )
local t
for t in "${tools[@]}"; do
has "${t}" "${MASQ_TOOLS[@]}" || die -q "Unknown tool: ${t}"
echo "Cleaning up masquerade for ${t} ..."
local out_dir_var=MASQ_DIR_${t}
local out_dir=${!out_dir_var}
[[ -d ${out_dir} ]] || continue
cd "${out_dir}" || die
local p
for p in *; do
if [[ -d ${p} ]]; then
# Avoid removing distcc compat symlink
# https://bugs.gentoo.org/942759
continue
fi
if ! type -P "${p}" &>/dev/null; then
rm -v "${p}"
fi
done
done
}
## }}}
## {{{ remove masquerade dir
describe_remove() {
echo "Remove the shadow directory completely"
}
describe_remove_parameters() {
echo "<tool>"
}
describe_remove_options() {
echo "<tool> : Tool(s) to remove (${MASQ_TOOLS[*]})"
}
do_remove() {
[[ ${1} ]] || die -q "No tool specified for removal"
local tools=( ${1} )
shopt -s nullglob
[[ ${tools} == all ]] && die -q "Implicitly removing all tools is not allowed"
local t
for t in "${tools[@]}"; do
has "${t}" "${MASQ_TOOLS[@]}" || die -q "Unknown tool: ${t}"
echo "Removing masquerade for ${t} ..."
local out_dir_var=MASQ_DIR_${t}
local out_dir=${!out_dir_var}
rm -v -r -f "${out_dir}" || die
done
}
## }}}
# vim: ts=4 sw=4 noet fdm=marker