]> git.ipfire.org Git - ipfire-3.x.git/blob - kernel/scripts/configure
kernel: configure: Correctly save changes
[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="x86_64"
168 local kernel_arch="x86"
169 local flavour="default"
170
171 local config_in=$(mktemp)
172 local config_out=$(mktemp)
173 local diff_out=$(mktemp)
174
175 merge_config ${arch} ${flavour} ${config_in} --mode=none
176
177 (
178 pushd ${KERNEL_DIR}
179 cat ${config_in} > .config
180
181 echo "You may now edit the configuration..."
182 make ARCH=${kernel_arch} "$@"
183
184 cat .config > ${config_out}
185 popd
186 )
187
188 ${SCRIPTS_DIR}/configdiff.py ${config_in} ${config_out} > ${diff_out}
189
190 # Update the rest of the configurations.
191 diff_configs ${diff_out} --mode=oldconfig
192
193 rm -f ${config_in} ${config_out} ${diff_out}
194 }
195
196 # config-generic
197 # Intersection of all files.
198 # config-x86-x86_64
199 # Diff against merge of (config-generic and config-x86-generic).
200
201 function diff_configs() {
202 local extra_configs="$@"
203
204 local filename
205 local platform
206 local subplatform
207
208 declare -A platform_configs
209 declare -A subplatform_configs
210
211 tmpdir=$(mktemp -d)
212
213 for config in ${CONFIGS}; do
214 arch=${config%:*}
215 flavour=${config#*:}
216
217 filename=${tmpdir}/config-${arch}-${flavour}
218
219 merge_config ${arch} ${flavour} ${filename} ${extra_configs}
220
221 platform="$(get_platform "${arch}")"
222 subplatform="$(get_subplatform "${arch}")"
223
224 if [ -n "${subplatform}" ]; then
225 subplatform_configs[${subplatform}]="${subplatform_configs[${subplatform}]} ${filename}"
226 else
227 platform_configs[${platform}]="${platform_configs[${platform}]} ${filename}"
228 fi
229 done
230
231 local common_configs
232 for platform in ${PLATFORMS}; do
233 for subplatform in ${SUBPLATFORMS[${platform}]}; do
234 filename="${tmpdir}/config-${subplatform}-common"
235 ${SCRIPTS_DIR}/configcommon.py ${subplatform_configs[${subplatform}]} \
236 > ${filename}
237
238 platform_configs[${platform}]="${platform_configs[${platform}]} ${filename}"
239 done
240
241 filename="${tmpdir}/config-${platform}-common"
242 ${SCRIPTS_DIR}/configcommon.py ${platform_configs[${platform}]} \
243 > ${filename}
244
245 common_configs="${common_configs} ${filename}"
246 done
247
248 ${SCRIPTS_DIR}/configcommon.py ${common_configs} > ${tmpdir}/config-generic
249
250 for platform in ${PLATFORMS}; do
251 for subplatform in ${SUBPLATFORMS[${platform}]}; do
252 ${SCRIPTS_DIR}/configdiff.py \
253 ${tmpdir}/config-${platform}-common \
254 ${tmpdir}/config-${subplatform}-common \
255 > ${tmpdir}/config-${subplatform}-generic
256 done
257
258 ${SCRIPTS_DIR}/configdiff.py \
259 ${tmpdir}/config-generic \
260 ${tmpdir}/config-${platform}-common \
261 > ${tmpdir}/config-${platform}-generic
262 done
263
264 for config in ${CONFIGS}; do
265 arch=${config%:*}
266 flavour=${config#*:}
267
268 filename=${tmpdir}/config-${arch}-${flavour}
269
270 case "${config}" in
271 aarch64:default|armv7hl:default|x86_64:default)
272 # Virtual configuration
273 rm -f ${filename}
274 continue
275 ;;
276 *)
277 platform="$(get_subplatform "${arch}" || get_platform "${arch}")"
278 ${SCRIPTS_DIR}/configdiff.py ${tmpdir}/config-${platform}-common \
279 ${filename} > ${filename}.tmp
280 ;;
281 esac
282 mv ${filename}{.tmp,}
283 done
284 rm -f ${tmpdir}/config-*-common
285
286 for config in ${tmpdir}/*; do
287 if ! cmp $(basename ${config}) ${config} &>/dev/null; then
288 echo "$(basename ${config}) has changed."
289 fi
290 cat ${config} > $(basename ${config})
291 done
292
293 rm -rf ${tmpdir}
294 }
295
296 KERNEL_DIR="$(search_kernel_dir)"
297
298 # Parse commandline.
299 while [ $# -gt 0 ]; do
300 arg=${1}; shift
301 case "${arg}" in
302 help|"")
303 echo "${0} - available commands:"
304 echo " * merge <arch> <flavour> <output filename>"
305 echo " * menuconfig"
306 echo " * oldconfig"
307 echo " * olddefconfig"
308 exit 0
309 ;;
310 menuconfig|merge|oldconfig|olddefconfig)
311 action=${arg}
312 break
313 ;;
314 esac
315 done
316
317 if [ -z "${KERNEL_DIR}" ]; then
318 echo >&2 "KERNEL_DIR was not set!"
319 exit 2
320 fi
321
322 if [ -z "${action}" ]; then
323 echo >&2 "No action given... Try ${0} help."
324 exit 2
325 fi
326
327 case "${action}" in
328 merge)
329 merge_config $@
330 exit $?
331 ;;
332 menuconfig|oldconfig|olddefconfig)
333 make_config "${action}"
334 exit $?
335 ;;
336 esac
337
338 exit 1