kernel 4.18 support
[cinitramfs.git] / make
blobd07a5ffe708de45e5ea36477a75417858adedea5
1 #!/bin/sh
3 # this script is brutal and verbose, has no tricks and is quite linear, then
4 # quite easy to deal with
5 # for the moment, it's hardcoded for a gcc toolchain... BAD! Since now
6 # gcc is a c++ piece of shit
8 # stolen from ffmpeg configure like a pig
9 set -e
11 # prevent locale nonsense from breaking basic text processing
12 LC_ALL=C
13 export LC_ALL
15 init_file_name=init
17 init_ulinux_src_files='
18 ulinux/utils/ascii/string/vsprintf.c
19 ulinux/utils/mem.c
20 ulinux/utils/ascii/string/conv/decimal/decimal.c
23 init_src_files="
24 uevents.c
25 modules.c
26 uevent.c
27 init.c
28 ramfs.c
29 $init_ulinux_src_files
32 sep_start()
34 printf '###############################################################################\n'
37 sep_end()
39 printf '###############################################################################\n\n'
42 subsep_start()
44 printf '*******************************************************************************\n'
47 subsep_end()
49 printf '*******************************************************************************\n'
52 ################################################################################
54 is_in()
56 value=$1
57 shift
58 for var in $*; do
59 [ $var = $value ] && return 0
60 done
61 return 1
64 die_unknown(){
65 echo "Unknown option \"$1\"."
66 echo "See $0 --help for available options."
67 exit 1
70 set_default(){
71 for opt; do
72 eval : \${$opt:=\$${opt}_default}
73 done
76 CMDLINE_SET='
77 linux_build_dir
78 kernel_modules_base_dir
79 init_cpp
80 init_cc
81 init_ccld
82 init_ulinux_arch
83 uevents_timeout
84 gen_init_cpio
85 kernel_release
86 extra_modules
87 pkg_config
88 elf_interpreter
90 # command line set defaults
91 # if the root is not around in less than 4s, something is really wrong
92 uevents_timeout_default=4000
93 gen_init_cpio_default='$linux_build_dir/usr/gen_init_cpio'
94 linux_build_dir_default=/usr/src/linux
95 #-------------------------------------------------------------------------------
96 # this defaults are for gcc, tested with version 4.7.3. You will need to
97 # override those for you compiler (tinycc/open64/pcc...). additionnally, source
98 # support for different toolchains is not done
99 # since we use standard C runtime libs, be nice with the C runtime
100 init_cpp_default='gcc -E -Wall -Wextra'
101 init_cc_default='gcc -Wall -Wextra -Wno-implicit-fallthrough -std=gnu99 -O2 -c'
102 init_ccld_default='gcc -Wl,-s -static'
103 #-------------------------------------------------------------------------------
104 kernel_modules_base_dir_default=/
105 kernel_release_default=$(uname -r)
106 init_ulinux_arch_default=$(uname -m | sed -e s/i.86/i386/ -e s/parisc64/parisc/ -e s/sun4u/sparc64/ -e s/arm.*/arm/ -e s/sa110/arm/ -e s/sh.*/sh/)
107 extra_modules_default=
108 pkg_config_default=pkg-config
109 elf_interpreter_default=
111 set_default $CMDLINE_SET
113 show_help()
115 cat <<EOF
116 Usage: make [options] [operations]
118 default is to build the the initramfs xz compressed cpio archive:
120 Options: [defaults in brackets after descriptions]
122 Help options:
123 --help print this message
125 Standard options:
126 --lib-path-list=LIB_PATH_LIST colon separated paths to look for target libraries
127 --uevents-timeout=UEVENTS_TIMEOUT uevents timeout in ms looking up for root block device to go online [$uevents_timeout]
128 --quiet init will be silenced (output code compiled out)
129 --linux-build-dir=DIR where to find the target linux build tree [$linux_build_dir_default]
130 --kernel-release=RELEASE the linux releases version [$kernel_release]
131 --kernel-modules-base-dir=DIR the base dir for linux modules and support files [$kernel_modules_base_dir]
132 --extra-modules=EXTRA_MODULES coma separated list of extra module to probe
134 Advanced options:
135 --gen-init-cpio=GEN_INIT_CPIO_PATH use this linux host tool to build the linux cpio initramfs [$gen_init_cpio_default]
136 --pkg-config=PKG_CONFIG use PKG_CONFIG pkg-config command for target libraries [$pkg_config_default]
137 --init-cpp=CPP use CPP compiler command line CPP for target init process [$init_cpp_default]
138 --init-cc=CC use C compiler command line CC for target init process objects [$init_cc_default]
139 --init-ccld=CCLD use compiler driver linker command line CCLD for target init process [$init_ccld_default]
140 --init-ulinux-arch=ARCH use ulinux ARCH for target init process [$init_ulinux_arch]
142 exit 0
145 ################################################################################
147 for opt do
148 optval="${opt#*=}"
149 case "$opt" in
150 --help|-h) show_help
152 --quiet) CPPFLAGS="$CPPFLAGS -DQUIET"
155 optname=${opt%%=*}
156 optname=${optname#--}
157 optname=$(echo "$optname" | sed 's/-/_/g')
158 if is_in $optname $CMDLINE_SET; then
159 eval $optname='$optval'
160 else
161 die_unknown $opt
164 esac
165 done
167 ################################################################################
169 sep_start;echo 'looking for source path:'
170 if test -f make; then
171 src_path=.
172 else
173 src_path=$(cd $(dirname "$0"); pwd)
174 echo "$src_path" | grep -q '[[:blank:]]' &&
175 die "out of tree builds are impossible with whitespace in source path."
177 echo "source path is $src_path";sep_end
179 ################################################################################
181 sep_start;echo 'checking libkmod and libblkid pkgconfig support:'
182 if $pkg_config --exists libkmod blkid; then
183 echo "found pkg-config files for libkmod and libblkid"
184 else
185 echo "missing pkg-config file for libkmod or libblkid"
186 exit 1
188 sep_end
190 ################################################################################
192 # define variable in source
193 CPPFLAGS="$CPPFLAGS -DUEVENTS_TIMEOUT=$uevents_timeout"
194 CPPFLAGS="$CPPFLAGS $($pkg_config --cflags-only-I libkmod blkid)"
195 CFLAGS="$CFLAGS $($pkg_config --cflags-only-other libkmod blkid)"
196 LIBS="$($pkg_config --libs --static libkmod blkid) $LIBS"
198 ################################################################################
200 sep_start;echo 'configure ulinux src tree for target arch:'
201 rm -f $src_path/ulinux/arch
202 ln -s archs/$init_ulinux_arch $src_path/ulinux/arch
203 echo "init ulinux arch is $init_ulinux_arch"
204 sep_end
206 ################################################################################
208 # generated some code/headers
210 sep_start
211 revision=$(echo "$kernel_release" | egrep -o '^[[:digit:]]+\.[[:digit:]]+')
212 . $src_path/$revision
213 printf "linux revision is $revision, $src_path/$revision was sourced:\n--------\n"
214 cat $src_path/$revision
215 echo '--------'
216 sep_end
218 # those modules are all loaded before anything else
219 sep_start;echo 'generate static module list:'
220 $src_path/script/static_modules_h.sh $extra_modules $DISK_MODULES \
221 >./static_modules.h
222 cat ./static_modules.h
223 sep_end
225 ################################################################################
227 sep_start;echo 'C preprocess init src files:'
228 for init_src_file in $init_src_files
230 init_pp_c_file=${init_src_file%.c}.pp.c
231 echo "INIT_CPP $init_src_file->$init_pp_c_file"
232 mkdir -p $(dirname $init_pp_c_file)
233 $init_cpp -o $init_pp_c_file \
234 $CPPFLAGS \
235 -I. \
236 -I$src_path \
237 $src_path/$init_src_file
238 init_pp_c_files="$init_pp_c_file $init_pp_c_files"
239 done
240 sep_end
242 ################################################################################
244 sep_start;echo 'compile init preprocessed src files:'
245 for init_pp_c_file in $init_pp_c_files
247 init_obj_file=${init_pp_c_file%.pp.c}.o
248 echo "INIT_CC $init_pp_c_file-->$init_obj_file"
249 $init_cc $CFLAGS -o $init_obj_file $init_pp_c_file
250 init_obj_files="$init_obj_file $init_obj_files"
251 done
252 sep_end
254 ################################################################################
256 sep_start;echo 'link the init objects to produce the init binary:'
257 echo "INIT_CCLD $init_file_name"
258 $init_ccld -o $init_file_name $init_obj_files $LIBS
259 sep_end
261 ################################################################################
263 sep_start;echo 'generate the cpio source file:'
264 sed -e "s:@INIT_PATH@:$(readlink -f $init_file_name):" "$src_path/cpio.in" >cpio
265 echo "dir /lib/modules/$kernel_release 0755 0 0">>cpio
267 modules=$HW_MODULES,$DISK_MODULES,$FS_MODULES
268 if test -n "$extra_modules";then
269 modules=$modules,$extra_modules
272 subsep_start
273 #add proper entries in the cpio definition file related to linux modules
274 $src_path/script/cpio_modules_add.sh $kernel_modules_base_dir $kernel_release \
275 $modules ./cpio
276 subsep_end
278 printf "cpio source file is:\n--------\n"
279 cat cpio
280 echo '--------'
281 sep_end
283 ################################################################################
285 sep_start;echo 'generating and compressing the cpio archive'
286 e_gen_init_cpio=$(eval echo "$gen_init_cpio")
287 $e_gen_init_cpio cpio >${kernel_release}.cpio
288 xz --force --check=crc32 --extreme --stdout ${kernel_release}.cpio >${kernel_release}.cpio.xz
289 sep_end