make modules_install should use -j1 only
[genkernel.git] / gen_compile.sh
1 #!/bin/bash
2
3 compile_kernel_args()
4 {
5         local ARGS
6
7         ARGS=""
8         if [ "${KERNEL_CC}" != "" ]
9         then
10                 ARGS="CC=\"${KERNEL_CC}\""
11         fi
12         if [ "${KERNEL_LD}" != "" ]
13         then
14                 ARGS="${ARGS} LD=\"${KERNEL_LD}\""
15         fi
16
17         if [ "${KERNEL_AS}" != "" ]
18         then
19                 ARGS="${ARGS} AS=\"${KERNEL_AS}\""
20         fi
21
22         echo -n "${ARGS}"
23 }
24
25 compile_utils_args()
26 {
27         local ARGS
28
29         ARGS=""
30         if [ "${UTILS_CC}" != "" ]
31         then
32                 ARGS="CC=\"${UTILS_CC}\""
33         fi
34         if [ "${UTILS_LD}" != "" ]
35         then
36                 ARGS="${ARGS} LD=\"${UTILS_LD}\""
37         fi
38
39         if [ "${UTILS_AS}" != "" ]
40         then
41                 ARGS="${ARGS} AS=\"${UTILS_AS}\""
42         fi
43
44         echo -n "${ARGS}"
45 }
46
47 export_utils_args()
48 {
49         if [ "${UTILS_CC}" != "" ]
50         then
51                 export CC="${UTILS_CC}"
52         fi
53         if [ "${UTILS_LD}" != "" ]
54         then
55                 export LD="${UTILS_LD}"
56         fi
57         if [ "${UTILS_AS}" != "" ]
58         then
59                 export AS="${UTILS_AS}"
60         fi
61 }
62
63 unset_utils_args()
64 {
65         if [ "${UTILS_CC}" != "" ]
66         then
67                 unset CC
68         fi
69         if [ "${UTILS_LD}" != "" ]
70         then
71                 unset LD
72         fi
73         if [ "${UTILS_AS}" != "" ]
74         then
75                 unset AS
76         fi
77 }
78
79 export_kernel_args()
80 {
81         if [ "${KERNEL_CC}" != "" ]
82         then
83                 export CC="${KERNEL_CC}"
84         fi
85         if [ "${KERNEL_LD}" != "" ]
86         then
87                 export LD="${KERNEL_LD}"
88         fi
89         if [ "${KERNEL_AS}" != "" ]
90         then
91                 export AS="${KERNEL_AS}"
92         fi
93 }
94
95 unset_kernel_args()
96 {
97         if [ "${KERNEL_CC}" != "" ]
98         then
99                 unset CC
100         fi
101         if [ "${KERNEL_LD}" != "" ]
102         then
103                 unset LD
104         fi
105         if [ "${KERNEL_AS}" != "" ]
106         then
107                 unset AS
108         fi
109 }
110
111 compile_generic() {
112         local RET
113         if [ "$#" -lt "2" ]
114         then
115                 gen_die "compile_generic(): improper usage"
116         fi
117
118         if [ "${2}" = "kernel" ]
119         then
120                 export_kernel_args
121                 MAKE=${KERNEL_MAKE}
122         elif [ "${2}" = "utils" ]
123         then
124                 export_utils_args
125                 MAKE=${UTILS_MAKE}
126         fi
127
128         if [ "${DEBUGLEVEL}" -gt "1" ]
129         then
130                 # Output to stdout and debugfile
131                 print_info 2 "COMMAND: ${MAKE} ${MAKEOPTS} ${1}" 1 0 1
132                 ${MAKE} ${MAKEOPTS} ${1} 2>&1 | tee -a ${DEBUGFILE}
133                 RET=$?
134         else
135                 # Output to debugfile only
136                 print_info 2 "COMMAND: ${MAKE} ${MAKEOPTS} ${1}" 1 0 1
137                 ${MAKE} ${MAKEOPTS} ${1} >> ${DEBUGFILE} 2>&1
138                 RET=$?
139         fi
140         [ "${RET}" -ne "0" ] && gen_die "compile of ${1} failed"
141
142         unset MAKE
143         if [ "${2}" = "kernel" ]
144         then
145                 unset_kernel_args
146         elif [ "${2}" = "utils" ]
147         then
148                 unset_utils_args
149         fi
150
151 }
152
153 extract_dietlibc_bincache() {
154         print_info 1 "extracting dietlibc bincache"
155         CURR_DIR=`pwd`
156         cd "${TEMP}"
157         rm -rf "${TEMP}/diet" > /dev/null
158         tar -jxpf "${DIETLIBC_BINCACHE}" || gen_die "Could not extract dietlibc bincache"
159         [ ! -d "${TEMP}/diet" ] && gen_die "${TEMP}/diet directory not found"
160         cd "${CURR_DIR}"
161 }
162
163 clean_dietlibc_bincache() {
164         print_info 1 "cleaning up dietlibc bincache"
165         CURR_DIR=`pwd`
166         cd "${TEMP}"
167         rm -rf "${TEMP}/diet" > /dev/null
168         cd "${CURR_DIR}"
169 }
170
171
172 compile_dep() {
173         # Only make dep for 2.4 kernels
174         if [ "${PAT}" -gt "4" ]
175         then
176                 print_info 1 "kernel: skipping make dep for non 2.4 kernels"
177         else
178                 print_info 1 "kernel: Making dependencies for linux ${KV}"
179                 cd ${KERNEL_DIR}
180                 compile_generic "dep" kernel
181         fi
182 }
183
184 compile_modules() {
185         print_info 1 "kernel: Starting compile of linux ${KV} modules"
186         cd ${KERNEL_DIR}
187         compile_generic "modules" kernel
188         export UNAME_MACHINE="${ARCH}"
189         # On 2.4 kernels, if MAKEOPTS > -j1 it can cause failures
190         MAKEOPTS_SAVE="${MAKEOPTS}"
191         MAKEOPTS="-j1"
192         compile_generic "modules_install" kernel
193         MAKEOPTS="${MAKEOPTS_SAVE}"
194         export MAKEOPTS
195         unset UNAME_MACHINE
196 }
197
198 compile_kernel() {
199         [ "${KERNEL_MAKE}" = "" ] && gen_die "KERNEL_MAKE undefined. Don't know how to compile kernel for arch."
200         cd ${KERNEL_DIR}
201         print_info 1 "kernel: Starting compile of linux ${KV} ${KERNEL_MAKE_DIRECTIVE}"
202         compile_generic "${KERNEL_MAKE_DIRECTIVE}" kernel
203         if [ "${KERNEL_MAKE_DIRECTIVE_2}" != "" ]
204         then
205                 print_info 1 "kernel: Starting suppliment compile of linux ${KV} ${KERNEL_MAKE_DIRECTIVE_2}"
206                 compile_generic "${KERNEL_MAKE_DIRECTIVE_2}" kernel
207         fi
208         cp "${KERNEL_BINARY}" "/boot/kernel-${KV}" || gen_die "Could not copy kernel binary to boot"
209 }
210
211 compile_busybox() {
212         if [ ! -f "${BUSYBOX_BINCACHE}" ]
213         then
214                 [ ! -f "${BUSYBOX_SRCTAR}" ] && gen_die "Could not find busybox source tarball: ${BUSYBOX_SRCTAR}"
215                 [ ! -f "${BUSYBOX_CONFIG}" ] && gen_die "Cound not find busybox config file: ${BUSYBOX_CONFIG}"
216                 cd "${TEMP}"
217                 rm -rf ${BUSYBOX_DIR} > /dev/null
218                 tar -jxpf ${BUSYBOX_SRCTAR} || gen_die "Could not extract busybox source tarball"
219                 [ ! -d "${BUSYBOX_DIR}" ] && gen_die "Busybox directory ${BUSYBOX_DIR} invalid"
220                 cp "${BUSYBOX_CONFIG}" "${BUSYBOX_DIR}/.config"
221                 cd "${BUSYBOX_DIR}"
222 # Busybox and dietlibc don't play nice right now
223 #               if [ "${USE_DIETLIBC}" -eq "1" ]
224 #               then
225 #                       extract_dietlibc_bincache
226 #                       OLD_CC="${UTILS_CC}"
227 #                       UTILS_CC="${TEMP}/diet/bin/diet ${UTILS_CC}"
228 #               fi
229                 print_info 1 "Busybox: make oldconfig"
230                 yes "" | compile_generic "oldconfig" utils
231                 print_info 1 "Busybox: make all"
232                 compile_generic "all" utils
233 # Busybox and dietlibc don't play nice right now
234 #               if [ "${USE_DIETLIBC}" -eq "1" ]
235 #               then
236 #                       clean_dietlibc_bincache
237 #                       UTILS_CC="${OLD_CC}"
238 #               fi
239                 print_info 1 "Busybox: copying to bincache"
240                 [ ! -f "${TEMP}/${BUSYBOX_DIR}/busybox" ] && gen_die "busybox executable does not exist after compile, error"
241                 strip "${TEMP}/${BUSYBOX_DIR}/busybox" || gen_die "could not strip busybox"
242                 bzip2 "${TEMP}/${BUSYBOX_DIR}/busybox" || gen_die "bzip2 compression of busybox failed"
243                 [ ! -f "${TEMP}/${BUSYBOX_DIR}/busybox.bz2" ] && gen_die "could not find compressed busybox binary"
244                 mv "${TEMP}/${BUSYBOX_DIR}/busybox.bz2" "${BUSYBOX_BINCACHE}" || gen_die "could not copy busybox binary to arch package directory, does the directory exist?"
245
246                 print_info 1 "Busybox: cleaning up"
247                 cd "${TEMP}"
248                 rm -rf "${BUSYBOX_DIR}" > /dev/null
249         else
250                 print_info 1 "Busybox: Found bincache at ${BUSYBOX_BINCACHE}"
251         fi
252 }
253
254 compile_modutils() {
255         local ARGS
256         if [ ! -f "${MODUTILS_BINCACHE}" ]
257         then
258                 [ ! -f "${MODUTILS_SRCTAR}" ] && gen_die "Could not find modutils source tarball: ${MODUTILS_SRCTAR}"
259                 cd "${TEMP}"
260                 rm -rf "${MODUTILS_DIR}"
261                 tar -jxpf "${MODUTILS_SRCTAR}"
262                 [ ! -d "${MODUTILS_DIR}" ] && gen_die "Modutils directory ${MODUTILS_DIR} invalid"
263                 cd "${MODUTILS_DIR}"
264                 print_info 1 "modutils: configure"
265
266                 if [ "${USE_DIETLIBC}" -eq "1" ]
267                 then
268                         extract_dietlibc_bincache
269                         OLD_CC="${UTILS_CC}"
270                         UTILS_CC="${TEMP}/diet/bin/diet ${UTILS_CC}"
271                 fi
272
273                 export_utils_args
274                 ./configure --disable-combined --enable-insmod-static >> ${DEBUGFILE} 2>&1 || gen_die "Configure of modutils failed"
275                 unset_utils_args
276
277                 print_info 1 "modutils: make all"
278                 compile_generic "all" utils
279
280                 if [ "${USE_DIETLIBC}" -eq "1" ]
281                 then
282                         clean_dietlibc_bincache
283                         UTILS_CC="${OLD_CC}"
284                 fi
285
286                 print_info 1 "modutils: copying to bincache"
287                 [ ! -f "${TEMP}/${MODUTILS_DIR}/insmod/insmod.static" ] && gen_die "insmod.static does not exist after compilation of modutils"
288                 strip "${TEMP}/${MODUTILS_DIR}/insmod/insmod.static" || gen_die "could not strip insmod.static"
289                 bzip2 "${TEMP}/${MODUTILS_DIR}/insmod/insmod.static" || gen_die "compression of insmod.static failed"
290                 [ ! -f "${TEMP}/${MODUTILS_DIR}/insmod/insmod.static.bz2" ] && gen_die "could not find compressed insmod.static.bz2 binary"
291                 mv "${TEMP}/${MODUTILS_DIR}/insmod/insmod.static.bz2" "${MODUTILS_BINCACHE}" || gen_die "could not move compress binary to bincache"
292
293                 print_info 1 "modutils: cleaning up"
294                 cd "${TEMP}"
295                 rm -rf "${MODULE_INIT_TOOLS_DIR}" > /dev/null
296         else
297                 print_info 1 "modutils: Found bincache at ${MODUTILS_BINCACHE}"
298         fi
299 }
300
301 compile_module_init_tools() {
302         local ARGS
303         if [ ! -f "${MODULE_INIT_TOOLS_BINCACHE}" ]
304         then
305                 [ ! -f "${MODULE_INIT_TOOLS_SRCTAR}" ] && gen_die "Could not find module-init-tools source tarball: ${MODULE_INIT_TOOLS_SRCTAR}"
306                 cd "${TEMP}"
307                 rm -rf "${MODULE_INIT_TOOLS_DIR}"
308                 tar -jxpf "${MODULE_INIT_TOOLS_SRCTAR}"
309                 [ ! -d "${MODULE_INIT_TOOLS_DIR}" ] && gen_die "Module-init-tools directory ${MODULE_INIT_TOOLS_DIR} invalid"
310                 cd "${MODULE_INIT_TOOLS_DIR}"
311                 print_info 1 "module-init-tools: configure"
312
313                 if [ "${USE_DIETLIBC}" -eq "1" ]
314                 then
315                         extract_dietlibc_bincache
316                         OLD_CC="${UTILS_CC}"
317                         UTILS_CC="${TEMP}/diet/bin/diet ${UTILS_CC}"
318                 fi
319
320                 export_utils_args
321                 ./configure >> ${DEBUGFILE} 2>&1 || gen_die "Configure of module-init-tools failed"
322                 unset_utils_args
323                 print_info 1 "module-init-tools: make all"
324                 compile_generic "all" utils
325
326                 if [ "${USE_DIETLIBC}" -eq "1" ]
327                 then
328                         clean_dietlibc_bincache
329                         UTILS_CC="${OLD_CC}"
330                 fi
331
332                 print_info 1 "module-init-tools: copying to bincache"
333                 [ ! -f "${TEMP}/${MODULE_INIT_TOOLS_DIR}/insmod.static" ] && gen_die "insmod.static does not exist after compilation of module-init-tools"
334                 strip "${TEMP}/${MODULE_INIT_TOOLS_DIR}/insmod.static" || gen_die "could not strip insmod.static"
335                 bzip2 "${TEMP}/${MODULE_INIT_TOOLS_DIR}/insmod.static" || gen_die "compression of insmod.static failed"
336                 [ ! -f "${TEMP}/${MODULE_INIT_TOOLS_DIR}/insmod.static.bz2" ] && gen_die "could not find compressed insmod.static.bz2 binary"
337                 mv "${TEMP}/${MODULE_INIT_TOOLS_DIR}/insmod.static.bz2" "${MODULE_INIT_TOOLS_BINCACHE}" || gen_die "could not move compressed binary to bincache"
338
339                 print_info 1 "module-init-tools: cleaning up"
340                 cd "${TEMP}"
341                 rm -rf "${MODULE_INIT_TOOLS_DIR}" > /dev/null
342         else
343                 print_info 1 "module-init-tools: Found bincache at ${MODULE_INIT_TOOLS_BINCACHE}"
344         fi
345 }
346
347 compile_devfsd() {
348         local ARGS
349         if [ ! -f "${DEVFSD_BINCACHE}" -o ! -f "${DEVFSD_CONF_BINCACHE}" ]
350         then
351                 [ ! -f "${DEVFSD_SRCTAR}" ] && gen_die "Could not find devfsd source tarball: ${DEVFSD_SRCTAR}"
352                 cd "${TEMP}"
353                 rm -rf "${DEVFSD_DIR}"
354                 tar -jxpf "${DEVFSD_SRCTAR}"
355                 [ ! -d "${DEVFSD_DIR}" ] && gen_die "Devfsd directory ${DEVFSD_DIR} invalid"
356                 cd "${DEVFSD_DIR}"
357
358                 if [ "${USE_DIETLIBC}" -eq "1" ]
359                 then
360                         extract_dietlibc_bincache
361                         OLD_CC="${UTILS_CC}"
362                         UTILS_CC="${TEMP}/diet/bin/diet ${UTILS_CC}"
363                 fi
364
365                 print_info 1 "devfsd: make all"
366
367                 if [ "${USE_DIETLIBC}" -eq "1" ]
368                 then
369                         compile_generic "has_dlopen=0 has_rpcsvc=0" utils
370                 else
371                         compile_generic "LDFLAGS=-static" utils
372                 fi
373
374                 if [ "${USE_DIETLIBC}" -eq "1" ]
375                 then
376                         clean_dietlibc_bincache
377                         UTILS_CC="${OLD_CC}"
378                 fi
379
380                 print_info 1 "devfsd: copying to bincache"
381                 [ ! -f "${TEMP}/${DEVFSD_DIR}/devfsd" ] && gen_die "devfsd executable does not exist after compilation of devfsd"
382                 strip "${TEMP}/${DEVFSD_DIR}/devfsd" || gen_die "could not strip devfsd"
383                 bzip2 "${TEMP}/${DEVFSD_DIR}/devfsd" || gen_die "compression of devfsd failed"
384                 [ ! -f "${TEMP}/${DEVFSD_DIR}/devfsd.bz2" ] && gen_die "could not find compressed devfsd.bz2 binary"
385                 mv "${TEMP}/${DEVFSD_DIR}/devfsd.bz2" "${DEVFSD_BINCACHE}" || gen_die "could not move compressed binary to bincache"
386
387                 [ ! -f "${TEMP}/${DEVFSD_DIR}/devfsd.conf" ] && gen_die "devfsd.conf does not exist after compilation of devfsd"
388                 bzip2 "${TEMP}/${DEVFSD_DIR}/devfsd.conf" || gen_die "compression of devfsd.conf failed"
389                 [ ! -f "${TEMP}/${DEVFSD_DIR}/devfsd.conf.bz2" ] && gen_die "could not find compressed devfsd.conf.bz2 binary"
390                 mv "${TEMP}/${DEVFSD_DIR}/devfsd.conf.bz2" "${DEVFSD_CONF_BINCACHE}" || gen_die "could not move compressed binary to bincache"
391
392                 print_info 1 "devfsd: cleaning up"
393                 cd "${TEMP}"
394                 rm -rf "${DEVFSD_DIR}" > /dev/null
395         else
396                 print_info 1 "devfsd: Found bincache at ${DEVFSD_BINCACHE} and ${DEVFSD_CONF_BINCACHE}"
397         fi
398 }
399
400 compile_dietlibc() {
401         local BUILD_DIETLIBC
402         local ORIGTEMP
403
404         BUILD_DIETLIBC=0
405         [ ! -f "${DIETLIBC_BINCACHE}" ] && BUILD_DIETLIBC=1
406         [ ! -f "${DIETLIBC_BINCACHE_TEMP}" ] && BUILD_DIETLIBC=1
407         if [ "${BUILD_DIETLIBC}" -eq "0" ]
408         then
409                 ORIGTEMP=`cat "${DIETLIBC_BINCACHE_TEMP}"`
410                 if [ "${TEMP}" != "${ORIGTEMP}" ]
411                 then
412                         print_info 1 "Dietlibc: Bincache exists, but current temp directory is different than original. Rebuilding."
413                         BUILD_DIETLIBC=1
414                 fi
415         fi
416
417         if [ "${BUILD_DIETLIBC}" -eq "1" ]
418         then
419                 [ ! -f "${DIETLIBC_SRCTAR}" ] && gen_die "Could not find dietlibc source tarball: ${DIETLIBC_SRCTAR}"
420                 cd "${TEMP}"
421                 rm -rf "${DIETLIBC_DIR}" > /dev/null
422                 tar -jxpf ${DIETLIBC_SRCTAR} || gen_die "Could not extract dietlibc source tarball"
423                 [ ! -d "${DIETLIBC_DIR}" ] && gen_die "Dietlibc directory ${DIETLIBC_DIR} invalid"
424                 cd "${DIETLIBC_DIR}"
425                 print_info 1 "Dietlibc: make"
426                 compile_generic "prefix=${TEMP}/diet" utils
427                 print_info 1 "Dietlibc: installing"
428                 compile_generic "prefix=${TEMP}/diet install" utils
429                 print_info 1 "Dietlibc: copying to bincache"
430                 cd ${TEMP}
431                 tar -jcpf "${DIETLIBC_BINCACHE}" diet || gen_die "Could not tar up dietlibc bin"
432                 [ ! -f "${DIETLIBC_BINCACHE}" ] && gen_die "bincache not created"
433                 echo "${TEMP}" > "${DIETLIBC_BINCACHE_TEMP}"
434
435                 print_info 1 "Dietlibc: cleaning up"
436                 cd "${TEMP}"
437                 rm -rf "${DIETLIBC_DIR}" > /dev/null
438                 rm -rf "${TEMP}/diet" > /dev/null
439         else
440                 print_info 1 "Dietlibc: Found bincache at ${DIETLIBC_BINCACHE}"
441         fi
442 }
443