/make (9f4bb09cd1e95ca807848a2445e8580f0a8b0aa6) (12172 bytes) (mode 100755) (type blob)

#!/bin/sh

#this script is brutal and verbose, has no tricks and is quite linear, then
#quite easy to deal with
#for the moment, it's hardcoded for a gcc toolchain... BAD! Since now
#gcc is a c++ piece of shit

# stolen from ffmpeg configure like a pig
set -e

# Prevent locale nonsense from breaking basic text processing.
LC_ALL=C
export LC_ALL

init_file_name=init

init_ulinux_src_files='
ulinux/utils/ascii/string/vsprintf.c
ulinux/utils/mem.c
ulinux/utils/ascii/string/conv/decimal/decimal.c
'

init_src_files="
uevents.c
modules.c
uevent.c
init.c
ramfs.c
$init_ulinux_src_files
"

clean_do()
{
    rm -f static_modules.h
    rm -f *.cpio.xz
    rm -f *.cpio
    rm -f cpio
    rm -f $init_file_name
    for init_src_file in $init_src_files
    do
      rm -f ${init_src_file/\.c/.pp.c}
      rm -f ${init_src_file/\.c/.o}
      #clean directories, but keep root of build tree
      tgt_dir=$(dirname $init_src_file)
      if test -d $tgt_dir -a "$tgt_dir" != "."; then
        rmdir --ignore-fail-on-non-empty -p $tgt_dir
      fi
    done
    exit 0
}

sep_start()
{
  echo '###############################################################################'
}

sep_end()
{
  echo -e '###############################################################################\n'
}

subsep_start()
{
  echo -e '*******************************************************************************'
}

subsep_end()
{
  echo -e '*******************************************************************************'
}
################################################################################

is_in(){
    value=$1
    shift
    for var in $*; do
        [ $var = $value ] && return 0
    done
    return 1
}

die_unknown(){
    echo "Unknown option \"$1\"."
    echo "See $0 --help for available options."
    exit 1
}

set_default(){
    for opt; do
        eval : \${$opt:=\$${opt}_default}
    done
}

CMDLINE_SET='
    linux_src_dir
    kernel_modules_base_dir
    init_cpp
    init_cc
    init_ld
    init_ulinux_arch
    uevents_timeout
    root_uuid
    gen_init_cpio
    kernel_release
    extra_modules
    pkg_config
    readelf
    elf_interpreter
'
#command line set defaults
#if the root is not around in less than 4s, something is really wrong
uevents_timeout_default=4000
root_uuid_default=00000000-0000-0000-0000-000000000000
gen_init_cpio_default='$linux_src_dir/usr/gen_init_cpio'
linux_src_dir_default=/usr/src/linux
#-------------------------------------------------------------------------------
#This defaults are for gcc, tested with version 4.7.3. You will need to
#override those for you compiler (tinycc/open64/pcc...). Additionnally, source
#support for different toolchains is not done.
#Since we use standard C runtime libs, be nice with the C runtime.
#The right way to do it is to have a toolchain abstraction layer since there are
#no standards for some
init_cpp_default='gcc -E -Wall -Wextra'
init_cc_default="gcc -Wall -Wextra -std=gnu99 -O0 \
-Wl,--dynamic-linker=/lib/ld.so -c"
init_ld_default='gcc -Wl,-O10,-s'
#-------------------------------------------------------------------------------
kernel_modules_base_dir_default=/
kernel_release_default=$(uname -r)
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/)
extra_modules_default=
pkg_config_default=pkg-config
readelf_default=readelf
elf_interpreter_default=

set_default $CMDLINE_SET

show_help(){
    cat <<EOF
Usage: make [options] [operations]

Operations: [default is to build the the initramfs xz compressed cpio archive]:
  clean                                clean build products

Options: [defaults in brackets after descriptions]

Help options:
  --help                               print this message

Standard options:
  --root-uuid=ROOT_UUID                the uuid or the root filesystem to mount [$root_uuid_default]
  --lib-path-list=LIB_PATH_LIST        colon separated paths to look for target libraries
  --uevents-timeout=UEVENTS_TIMEOUT    uevents timeout in ms looking up for root block device to go online [$uevents_timeout]
  --quiet                              init will be silenced (output code compiled out)
  --linux-src-dir=DIR                  where to find the target linux source tree [$linux_src_dir_default]
  --kernel-release=RELEASE             the linux releases version [$kernel_release]
  --kernel-modules-base-dir=DIR        the base dir for linux modules and support files [$kernel_modules_base_dir]
  --extra-modules=EXTRA_MODULES        coma separated list of extra module to probe

Advanced options:
  --gen-init-cpio=GEN_INIT_CPIO_PATH   use this linux host tool to build the linux cpio initramfs [$gen_init_cpio_default]
  --pkg-config=PKG_CONFIG              use PKG_CONFIG pkg-config command for target libraries [$pkg_config_default]
  --readelf=READELF                    use READELF readelf command for target readelf [$readelf_default]
  --elf-interpreter=ELF_INTERPRETER    copy target ELF_INTERPRETER in cpio archive [will use the interpreter from the generated init elf binary]
  --init-cpp=CPP                       use CPP compiler command line CPP for target init process [$init_cpp_default]
  --init-cc=CC                         use C compiler command line CC for target init process objects [$init_cc_default]
  --init-ld=LD                         use linker command line LD for target init process [$init_ld_default]
  --init-ulinux-arch=ARCH              use ulinux ARCH for target init process [$init_ulinux_arch]
EOF
  exit 0
}

################################################################################

for opt do
    optval="${opt#*=}"
    case "$opt" in
        clean) clean_do
        ;;
        --help|-h) show_help
        ;;
        --quiet) CPPFLAGS="$CPPFLAGS -DQUIET"
        ;;
        *)
            optname=${opt%%=*}
            optname=${optname#--}
            optname=$(echo "$optname" | sed 's/-/_/g')
            if is_in $optname $CMDLINE_SET; then
                eval $optname='$optval'
            else
                die_unknown $opt
            fi
        ;;
    esac
done

################################################################################

sep_start;echo 'looking for source path:'
if test -f make; then
    src_path=.
else
    src_path=$(cd $(dirname "$0"); pwd)
    echo "$src_path" | grep -q '[[:blank:]]' &&
        die "out of tree builds are impossible with whitespace in source path."
    test -e "$src_path/config.h" &&
        die "out of tree builds are impossible with config.h in source dir."
fi
echo "source path is $src_path";sep_end

################################################################################

sep_start;echo 'checking libkmod and libblkid pkgconfig support:'
if $pkg_config --exists libkmod blkid; then
    echo "found pkg-config files for libkmod and libblkid"
else
    echo "missing pkg-config file for libkmod or libblkid"
    exit 1
fi
sep_end

################################################################################

#define variable in source
CPPFLAGS="$CPPFLAGS -DUEVENTS_TIMEOUT=$uevents_timeout"
CPPFLAGS="$CPPFLAGS -DROOT_UUID="$root_uuid""
CPPFLAGS="$CPPFLAGS $($pkg_config --cflags-only-I libkmod blkid)"
CFLAGS="$CFLAGS $($pkg_config --cflags-only-other libkmod blkid)"
LDFLAGS="$LDFLAGS $($pkg_config --libs libkmod blkid)"

################################################################################

sep_start;echo 'configure ulinux src tree for target arch:'
rm -f $src_path/ulinux/arch
ln -s archs/$init_ulinux_arch $src_path/ulinux/arch
echo "init ulinux arch is $init_ulinux_arch"
sep_end

################################################################################

#generated some code/headers

sep_start
revision=$(uname -r | egrep -o '^[[:digit:]]+\.[[:digit:]]+')
source $src_path/$revision
echo -e "linux revision is $revision, $src_path/$revision was sourced:\n--------"
cat $src_path/$revision
echo '--------'
sep_end

sep_start;echo 'generate static module list:'
$src_path/script/static_modules_h.sh $extra_modules $DISK_MODULES \
                                                             >./static_modules.h
cat ./static_modules.h
sep_end

################################################################################

sep_start;echo 'C preprocess init src files:'
for init_src_file in $init_src_files
do
    init_pp_c_file=${init_src_file/\.c/.pp.c}
    echo "INIT_CPP $init_src_file->$init_pp_c_file"
    mkdir -p $(dirname $init_pp_c_file)
    $init_cpp $CPPFLAGS -I. -I$src_path -o $init_pp_c_file \
                                                        $src_path/$init_src_file
    init_pp_c_files="$init_pp_c_file $init_pp_c_files"
done
sep_end

################################################################################

sep_start;echo 'compile init preprocessed src files:'
for init_pp_c_file in $init_pp_c_files
do
    init_obj_file=${init_pp_c_file/\.pp.c/.o}
    echo "INIT_CC $init_pp_c_file-->$init_obj_file"
    $init_cc $CFLAGS -o $init_obj_file $init_pp_c_file
    init_obj_files="$init_obj_file $init_obj_files"
done
sep_end

################################################################################

sep_start;echo 'link the init objects to produce the init binary:'
echo "INIT_LD $init_file_name"
$init_ld -o $init_file_name $init_obj_files $LDFLAGS
sep_end

################################################################################

#build the library path used by the gcc toolchain. We will need it to lookup
#for the dynamic shared libraries and copy them into the cpio
sep_start;echo "computing gcc library path:"
#in gcc search paths, the sysroot uses '='
lib_path_nosysroot=$($init_ld $LDFLAGS -print-search-dirs | egrep '^libraries' \
                                    | sed -r 's/libraries:[[:space:]]+(.+)/\1/')
echo "gcc library path is $lib_path_nosysroot"
gcc_sysroot=$($init_ld -print-sysroot)
echo "gcc_sysroot (can be empty) is $gcc_sysroot"

IFS=:
#replace the sysroot marker, '=', in gcc library path
if test -n "$gcc_sysroot"; then
  for p in $lib_path_nosysroot; do
    lib_path_norealpath="$lib_path_norealpath:$(echo $p | sed -r "s:^=:$gcc_sysroot:")"
  done
else
  for p in $lib_path_nosysroot; do
    lib_path_norealpath="$lib_path_norealpath:$(echo $p | sed -r 's/^=//')"
  done
fi
for p in $lib_path_norealpath; do
  if realpath -q "$p" &>/dev/null; then
    lib_path="$lib_path:$(realpath "$p")"
  fi
done
lib_path=${lib_path#:}
echo "final LIB_PATH=$lib_path"
sep_end

################################################################################

#locate the target elf interpreter which will be copied in the cpio archive
sep_start
if test -z "$elf_interpreter"; then
  elf_interpreter=$($readelf -l ./init | egrep 'Requesting program interpreter' | sed -r 's/^.+interpreter:[[:space:]]*(.+)\]/\1/')
fi
echo "will copy $elf_interpreter elf interpreter in cpio archive"
sep_end

################################################################################

sep_start;echo 'generate the cpio source file:'
sed -e "s:@INIT_PATH@:$(realpath $init_file_name):" "$src_path/cpio.in" >cpio
echo "dir /lib/modules/$kernel_release 0755 0 0">>cpio

modules=$HW_MODULES,$DISK_MODULES,$FS_MODULES
if test -n $"extra_modules";then
    modules=$modules,$extra_modules
fi

subsep_start
#add proper entries in the cpio definition file related to linux modules
$src_path/script/cpio_modules_add.sh $kernel_modules_base_dir $kernel_release \
                                                                 $modules ./cpio
subsep_end

subsep_start
#add proper entries in the cpio definition file related to shared elf libs
$src_path/script/cpio_libs_add.sh "$readelf" "$lib_path" ./init \
                                                       "$elf_interpreter" ./cpio
subsep_end

echo -e 'cpio source file is:\n--------'
cat cpio
echo '--------'
sep_end

################################################################################

e_gen_init_cpio=$(eval echo "$gen_init_cpio")
$e_gen_init_cpio cpio >${kernel_release}.cpio
xz --force --check=crc32 --extreme --stdout ${kernel_release}.cpio >${kernel_release}.cpio.xz


Mode Type Size Ref File
100644 blob 444 49d5064b5a60c1d457aaf871c71275eb962d83f2 3.16
100644 blob 558 3b1926aa2a9e5e4a8fa5c8a3d178a0ed02a01346 DEPENDENCIES
100644 blob 35147 94a9ed024d3859793618152ea559a168bbcbb5e2 LICENSE.md
100644 blob 768 32cae14d717baa6c88aab11a82663e0a2aefe652 README
100644 blob 644 598bf35b742147b3cc2bc1da0fd6d41aa282e77e TODO
100644 blob 432 2516d063ca695699c1ec3d755ef782bad84f203f cpio.in
100644 blob 587 1890391057120a83a6714310987be3ca82de46cc globals.h
100644 blob 5197 6caef253d4902e1c038135a156d42a847c89dddf init.c
100755 blob 12172 9f4bb09cd1e95ca807848a2445e8580f0a8b0aa6 make
100644 blob 5877 e849ba428752ea61144fe879792c8c68a65e3160 modules.c
100644 blob 503 f478bdeefa4ae3259dedfe0b4f78036492295d27 modules.h
100644 blob 674 7f272e25a25b4da12ad81f13b6eeff1bba4187af out.h
100644 blob 3180 6d142befdbc559aa3b6089443a2988b054bc71de ramfs.c
100644 blob 341 de9218a39777b8dd8feb5ee76cdf41af215ef473 ramfs.h
040000 tree - 87b8a0b9717f8367a1f8427da787968136f8aac9 script
100644 blob 3592 e6e7af84e5bd621843bfee6c9907ad85f789dccb uevent.c
100644 blob 348 cf07be71685703316f9522de37d466034d08e635 uevent.h
100644 blob 3970 cfe47059c2c26697fd02d2f18b0afe68dc7627fd uevents.c
100644 blob 413 896725abf8b5477eb7013e55429aa89a2bf97ed3 uevents.h
040000 tree - 62d63dbff565849898f1b5138d50a03774874906 ulinux
100644 blob 4060 feb12fd4fc27f1442198ea878911269a2b2e1215 ulinux_namespace.h
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/sylware/cinitramfs

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/sylware/cinitramfs

Clone this repository using git:
git clone git://git.rocketgit.com/user/sylware/cinitramfs

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main