#!/bin/bash -e # SPDX-FileCopyrightText: 2021 Andrius Štikonas # SPDX-FileCopyrightText: 2021 fosslinux # SPDX-FileCopyrightText: 2021 Paul Dersey # # SPDX-License-Identifier: GPL-3.0-or-later export PATH=/after/bin mkdir() { printf '%s\n' "START: mkdir $*" command mkdir "$@" MYRC=$? sync || true printf '%s\n' "READY: RC: $MYRC" } list_files() { local dir="$1" # find or findutils is build later... ls -R "$dir" | awk ' /:$/&&f{s=$0;f=0} /:$/&&!f{sub(/:$/,"");s=$0;f=1;next} NF&&f{ print s"/"$0 }' } dump_all() { printf '%s\n' "make failed with rc:$?" list_files /after | while read -r LINE; do T="$( date +%s )" echo "# FILE: $LINE | $( ls -l "$LINE" )" echo "# sed -n /MARKER-$T-START\$/,/MARKER-$T-READY\$/p log.txt | sed '1d;\$d' | cut -b13- | base64 -d >my.bin" echo "# MARKER-$T-START" /after/bin/base64 "$LINE" echo "# MARKER-$T-READY" echo "# sed -n /MARKER-$T-START\$/,/MARKER-$T-READY\$/p log.txt | sed '1d;\$d' | cut -b13- | base64 -d >my.bin" echo "#" done printf '%s\n' "make failed ready" false } cat >/after/bin/base64 <); sub encode { my \$s = shift; my \$r = ''; while( \$s =~ /(.{1,45})/gs ){ chop( \$r .= substr(pack("u",\$1),1) ); } my \$pad=(3-length(\$s)%3)%3; \$r=~tr|\` -_|AA-Za-z0-9+/|; \$r=~s/.{\$pad}$/"="x\$pad/e if \$pad; \$r=~s/(.{1,76})/\$1\n/g; \$r; } EOF chmod +x /after/bin/base64 || true # Common build steps # Build function provides a few common stages with default implementation # that can be overridden on per package basis in the build script. # build takes three arguments: # 1) name-version of the package # 2) optionally specify build script. Default is name-version.sh # 3) optionally specify name of checksum file. Default is checksums build () { pkg=$1 script_name=${2:-${pkg}.sh} checksum_f=${3:-checksums} cd "$pkg" || (echo "Cannot cd into ${pkg}!"; kill $$) echo "${pkg}: beginning build using script ${script_name}" base_dir="${PWD}" patch_dir="${base_dir}/${4:-patches}" mk_dir="${base_dir}/mk" files_dir="${base_dir}/files" if test -f /after/bin/date; then grep -q "date_real" /after/bin/date || { rm /after/bin/date_real || true } if test -f /after/bin/date_real; then true else cp /after/bin/date /after/bin/date_real || true cat >/after/bin/date <&2 printf '%s\n' "called date binary with: '\$*' in '\$PWD'" || true /after/bin/date_real "\$@" || true OUT="\$( /after/bin/date_real "\$@" )" >&2 printf '%s\n' "called date binary result '\$OUT'" || true EOF chmod +x /after/bin/date_real || true printf '%s\n' "OK - new date binary generated:" cat /after/bin/date || true fi else true fi rm -rf "build" mkdir -p "build" cd "build" build_script="${base_dir}/${script_name}" if test -e "${build_script}"; then # shellcheck source=/dev/null . "${build_script}" fi echo "${pkg}: unpacking source." call src_unpack cd "${pkg}" || (echo "Cannot cd into build/${pkg}!"; kill $$) echo "${pkg}: preparing source." call src_prepare echo "${pkg}: configuring source." call src_configure FOORC= echo "${pkg}: compiling source." call src_compile || { FOORC=$? echo "# end of call src_compile:$FOORC" dump_all /after exit } echo "# end of call src_compile:$FOORC" echo "${pkg}: installing." call src_install cd ../.. sha256sum /after/bin/* /after/lib/* /after/include/* /after/share/* || true echo "${pkg}: checksumming installed files." test -e "${checksum_f}" && { sha256sum -c "${checksum_f}" || { sha256sum "${checksum_f}" || true ls -l "${checksum_f}" || true ls -lt /after/bin || true echo "executing: gcc -dM -E -xc /dev/null" gcc -dM -E -xc /dev/null || true echo "executing: /after/bin/base64" cat /after/bin/base64 || true echo "executing: sha256sum /after/bin/bash" sha256sum /after/bin/bash || true echo "MARKER1START" base64 <"${checksum_f}" || true echo "MARKER2READY" echo "MARKER3START" base64 my.bin" true } } # cat "builtins/pipesize.h" || true echo "${pkg}: build successful" echo "executing: gcc -dM -E -xc /dev/null" gcc -dM -E -xc /dev/null || true cd .. unset -f src_unpack src_prepare src_configure src_compile src_install } # Default unpacking function that unpacks a single source tarball. # Default function to prepare source code. # It applies all patches from patch_dir (at the moment only -p0 patches are supported). # Then it copies our custom makefile and any other custom files from files directory. # Default unpacking function that unpacks all source tarballs. default_src_unpack() { src_dir="${base_dir}/src" for i in "${src_dir}"/*.tar.gz; do [ -e "${i}" ] || continue tar -xzf "${i}" done for i in "${src_dir}"/*.tar.bz2; do [ -e "${i}" ] || continue tar -xf "${i}" --use-compress-program=bzip2 done for i in "${src_dir}"/*.tar.xz; do [ -e "${i}" ] || continue tar -xf "${i}" --use-compress-program=xz done for i in "${src_dir}"/*.tar; do [ -e "${i}" ] || continue tar -xf "${i}" done } default_src_prepare() { if test -d "${patch_dir}"; then for p in "${patch_dir}"/*.patch; do echo "Applying patch: ${p}" patch -Np0 < "${p}" done fi makefile="${mk_dir}/main.mk" if test -e "${makefile}"; then cp "${makefile}" Makefile fi if test -d "${files_dir}"; then cp "${files_dir}"/* "${PWD}/" fi } # Default function for configuring source. default_src_configure() { : } # Default function for compiling source. It simply runs make without any parameters. default_src_compile() { cat Makefile || true make --debug -f Makefile PREFIX="${PREFIX}" || { dump_all exit 1 } printf '%s\n' "src_compile" } # Default installing function. PREFIX should be set by run.sh script. # Note that upstream makefiles might ignore PREFIX and have to be configured in configure stage. default_src_install() { cat Makefile || true make --debug -f Makefile install PREFIX="${PREFIX}" DESTDIR="${DESTDIR}" || { printf '%s\n' "make failed with RC:$?" exit 1 } printf '%s\n' "src_install" } # Check if bash function exists fn_exists() { test "$(type -t "$1")" == 'function' } # Call package specific function or default implementation. call() { if fn_exists "$1"; then $1 else default_"${1}" fi }