]> git.ipfire.org Git - ipfire-3.x.git/blob - kernel/scripts/configure
kernel: configure: Allow passing --arch= to edit a certain configuration
[ipfire-3.x.git] / kernel / scripts / configure
1 #!/bin/bash
2 ###############################################################################
3 # IPFire.org - An Open Source Firewall Solution #
4 # Copyright (C) - IPFire Development Team <info@ipfire.org> #
5 ###############################################################################
6
7 BASEDIR=$(dirname ${0})
8 SCRIPTS_DIR=${BASEDIR}
9
10 # x86_64
11 CONFIGS="x86_64:default"
12
13 # aarch64
14 CONFIGS="${CONFIGS} aarch64:default"
15
16 # armv7hl
17 CONFIGS="${CONFIGS} armv7hl:default"
18
19 PLATFORMS="x86 arm"
20 declare -A SUBPLATFORMS
21 SUBPLATFORMS=(
22 [arm]="arm64 arm32"
23 )
24
25 search_kernel_dir() {
26 local dir
27 for dir in /builddir/source/linux-*; do
28 if [ -d "${dir}" ]; then
29 echo "${dir}"
30 break
31 fi
32 done
33 }
34
35 function get_platform() {
36 local arch="${1}"
37
38 case "${arch}" in
39 aarch64|arm*)
40 echo "arm"
41 ;;
42 x86_64|i?86)
43 echo "x86"
44 ;;
45 *)
46 return 1
47 ;;
48 esac
49 }
50
51 function get_subplatform() {
52 local arch="${1}"
53
54 case "${arch}" in
55 aarch64)
56 echo "arm64"
57 ;;
58 arm*)
59 echo "arm32"
60 ;;
61 *)
62 return 1
63 ;;
64 esac
65 }
66
67 function get_kernel_arch() {
68 local arch="${1}"
69
70 case "${arch}" in
71 aarch64)
72 echo "arm64"
73 ;;
74 arm*)
75 echo "arm"
76 ;;
77 x86_64)
78 echo "x86"
79 ;;
80 esac
81 }
82
83 function merge_config() {
84 local arch=${1}
85 local flavour=${2}
86 local output=${3}
87 shift 3
88
89 local arg
90 for arg in arch flavour output; do
91 if [ -z "${!arg}" ]; then
92 echo >&2 "merge usage: <arch> <flavour> <output filename>"
93 exit 2
94 fi
95 done
96
97 local config_mode="olddefconfig"
98 local extra_configs
99 while [ $# -gt 0 ]; do
100 case "${1}" in
101 --mode=*)
102 config_mode=${1#--mode=}
103 shift
104 ;;
105 -*)
106 echo >&2 "Unknown option: ${1}"
107 ;;
108 *)
109 extra_configs="${extra_configs} ${1}"
110 ;;
111 esac
112 shift
113 done
114
115 local configs="${extra_configs} config-generic"
116
117 case "${arch}:${flavour}" in
118 # x86
119 x86_64:default)
120 configs="${configs} config-x86-generic"
121 ;;
122
123 # ARM64
124 aarch64:default)
125 configs="${configs} config-arm-generic config-arm64-generic"
126 ;;
127
128 # ARM
129 armv7hl:default)
130 configs="${configs} config-arm-generic config-arm32-generic"
131 ;;
132 *)
133 echo >&2 "ERROR: Invalid parameters given: $@"
134 return 1
135 ;;
136 esac
137
138 # Merge the configuration files from its elementary configuration
139 # files.
140 local tmp_out=$(mktemp)
141 local tmp_in=$(mktemp)
142
143 local config
144 for config in ${configs}; do
145 cat ${tmp_out} > ${tmp_in}
146 perl ${SCRIPTS_DIR}/merge.pl \
147 ${tmp_in} ${config} > ${tmp_out}
148 done
149
150 if [ "${config_mode}" != "none" ]; then
151 echo "Running 'make olddefconfig' for ${arch} (${flavour})..."
152 local kernel_arch="$(get_kernel_arch "${arch}")"
153 (
154 cd ${KERNEL_DIR}
155 cat ${tmp_out} > .config
156 make ARCH="${kernel_arch}" ${config_mode}
157 cat .config > ${tmp_out}
158 )
159 fi
160
161 cat ${tmp_out} > ${output}
162 rm -f ${tmp_in} ${tmp_out}
163 }
164
165 # This function runs an interactive "make oldconfig".
166 function make_config() {
167 local arch="${1}"
168 shift
169
170 # Detect kernel arch
171 local kernel_arch="$(get_kernel_arch "${arch}")"
172 local flavour="default"
173
174 local config_in=$(mktemp)
175 local config_out=$(mktemp)
176 local diff_out=$(mktemp)
177
178 merge_config ${arch} ${flavour} ${config_in} --mode=none
179
180 (
181 pushd ${KERNEL_DIR}
182 cat ${config_in} > .config
183
184 echo "You may now edit the configuration..."
185 make ARCH=${kernel_arch} "$@"
186
187 cat .config > ${config_out}
188 popd
189 )
190
191 ${SCRIPTS_DIR}/configdiff.py ${config_in} ${config_out} > ${diff_out}
192
193 # Update the rest of the configurations.
194 diff_configs ${diff_out} --mode=oldconfig
195
196 rm -f ${config_in} ${config_out} ${diff_out}
197 }
198
199 # config-generic
200 # Intersection of all files.
201 # config-x86-x86_64
202 # Diff against merge of (config-generic and config-x86-generic).
203
204 function diff_configs() {
205 local extra_configs="$@"
206
207 local filename
208 local platform
209 local subplatform
210
211 declare -A platform_configs
212 declare -A subplatform_configs
213
214 tmpdir=$(mktemp -d)
215
216 for config in ${CONFIGS}; do
217 arch=${config%:*}
218 flavour=${config#*:}
219
220 filename=${tmpdir}/config-${arch}-${flavour}
221
222 merge_config ${arch} ${flavour} ${filename} ${extra_configs}
223
224 platform="$(get_platform "${arch}")"
225 subplatform="$(get_subplatform "${arch}")"
226
227 if [ -n "${subplatform}" ]; then
228 subplatform_configs[${subplatform}]="${subplatform_configs[${subplatform}]} ${filename}"
229 else
230 platform_configs[${platform}]="${platform_configs[${platform}]} ${filename}"
231 fi
232 done
233
234 local common_configs
235 for platform in ${PLATFORMS}; do
236 for subplatform in ${SUBPLATFORMS[${platform}]}; do
237 filename="${tmpdir}/config-${subplatform}-common"
238 ${SCRIPTS_DIR}/configcommon.py ${subplatform_configs[${subplatform}]} \
239 > ${filename}
240
241 platform_configs[${platform}]="${platform_configs[${platform}]} ${filename}"
242 done
243
244 filename="${tmpdir}/config-${platform}-common"
245 ${SCRIPTS_DIR}/configcommon.py ${platform_configs[${platform}]} \
246 > ${filename}
247
248 common_configs="${common_configs} ${filename}"
249 done
250
251 ${SCRIPTS_DIR}/configcommon.py ${common_configs} > ${tmpdir}/config-generic
252
253 for platform in ${PLATFORMS}; do
254 for subplatform in ${SUBPLATFORMS[${platform}]}; do
255 ${SCRIPTS_DIR}/configdiff.py \
256 ${tmpdir}/config-${platform}-common \
257 ${tmpdir}/config-${subplatform}-common \
258 > ${tmpdir}/config-${subplatform}-generic
259 done
260
261 ${SCRIPTS_DIR}/configdiff.py \
262 ${tmpdir}/config-generic \
263 ${tmpdir}/config-${platform}-common \
264 > ${tmpdir}/config-${platform}-generic
265 done
266
267 for config in ${CONFIGS}; do
268 arch=${config%:*}
269 flavour=${config#*:}
270
271 filename=${tmpdir}/config-${arch}-${flavour}
272
273 case "${config}" in
274 aarch64:default|armv7hl:default|x86_64:default)
275 # Virtual configuration
276 rm -f ${filename}
277 continue
278 ;;
279 *)
280 platform="$(get_subplatform "${arch}" || get_platform "${arch}")"
281 ${SCRIPTS_DIR}/configdiff.py ${tmpdir}/config-${platform}-common \
282 ${filename} > ${filename}.tmp
283 ;;
284 esac
285 mv ${filename}{.tmp,}
286 done
287 rm -f ${tmpdir}/config-*-common
288
289 for config in ${tmpdir}/*; do
290 if ! cmp $(basename ${config}) ${config} &>/dev/null; then
291 echo "$(basename ${config}) has changed."
292 fi
293 cat ${config} > $(basename ${config})
294 done
295
296 rm -rf ${tmpdir}
297 }
298
299 ARCH="x86_64"
300 KERNEL_DIR="$(search_kernel_dir)"
301
302 # Parse commandline.
303 while [ $# -gt 0 ]; do
304 arg=${1}; shift
305 case "${arg}" in
306 help|"")
307 echo "${0} - available commands:"
308 echo " * merge <arch> <flavour> <output filename>"
309 echo " * menuconfig"
310 echo " * oldconfig"
311 echo " * olddefconfig"
312 exit 0
313 ;;
314 listnewconfig|menuconfig|merge|oldconfig|olddefconfig)
315 action=${arg}
316 ;;
317
318 # Parse switches
319 --arch=*)
320 ARCH="${arg#*=}"
321
322 if ! get_platform "${ARCH}" &>/dev/null; then
323 echo "Unknown architecture: ${ARCH}" >&2
324 exit 2
325 fi
326 ;;
327 esac
328 done
329
330 if [ -z "${KERNEL_DIR}" ]; then
331 echo >&2 "KERNEL_DIR was not set!"
332 exit 2
333 fi
334
335 if [ -z "${action}" ]; then
336 echo >&2 "No action given... Try ${0} help."
337 exit 2
338 fi
339
340 case "${action}" in
341 merge)
342 merge_config $@
343 exit $?
344 ;;
345 listnewconfig|menuconfig|oldconfig|olddefconfig)
346 make_config "${ARCH}" "${action}"
347 exit $?
348 ;;
349 esac
350
351 exit 1