#!/usr/bin/env bash # # SPDX-License-Identifier: GPL-2.0 # # Copyright (c) 2013-2023 Igor Pecovnik, igor@armbian.com # # This file is a part of the Armbian Build Framework # https://github.com/armbian/build/ # for RAW deb building. does a bunch of magic to "DEBIAN" directory. Argument is the open package directory. Target is always artifact_base_dir function fakeroot_dpkg_deb_build() { # check artifact_name and artifact_version is set otherwise exit_with_error [[ -z "${artifact_name}" ]] && exit_with_error "fakeroot_dpkg_deb_build: artifact_name is not set" [[ -z "${artifact_version}" ]] && exit_with_error "fakeroot_dpkg_deb_build: artifact_version is not set" [[ -z "${artifact_base_dir}" ]] && exit_with_error "fakeroot_dpkg_deb_build: artifact_base_dir is not set" display_alert "Building .deb package" "${artifact_name}: $*" "debug" declare first_arg="${1}" if [[ ! -d "${first_arg}" ]]; then exit_with_error "fakeroot_dpkg_deb_build: can't find source package directory: ${first_arg}" fi # Get the basename of the dir declare pkg_name pkg_name=$(basename "${first_arg}") # Show the total human size of the source package directory. display_alert "Source package size" "${first_arg}: $(du -sh "${first_arg}" | cut -f1)" "debug" # Lets fix all packages with Installed-Size: # get the size of the package in bytes declare -i pkg_size_bytes pkg_size_bytes=$(du -s -b "${first_arg}" | cut -f1) # edit DEBIAN/control, removed any Installed-Size: line sed -i '/^Installed-Size:/d' "${first_arg}/DEBIAN/control" # add the new Installed-Size: line. The disk space is given as the integer value of the estimated installed size in bytes, divided by 1024 and rounded up. declare -i installed_size installed_size=$(((pkg_size_bytes + 1023) / 1024)) echo "Installed-Size: ${installed_size}" >> "${first_arg}/DEBIAN/control" # Lets create DEBIAN/md5sums, for all the files in ${first_arg}. Do not include the paths in the md5sums file. Don't include the DEBIAN/* files. find "${first_arg}" -type f -not -path "${first_arg}/DEBIAN/*" -print0 | xargs -0 md5sum | sed "s|${first_arg}/||g" > "${first_arg}/DEBIAN/md5sums" # Parse the DEBIAN/control and get the real package name... declare control_package_name control_package_name=$(grep -E "^Package:" "${first_arg}/DEBIAN/control" | cut -d' ' -f2) # generate minimal DEBIAN/changelog cat <<- EOF > "${first_arg}"/DEBIAN/changelog ${control_package_name} (${artifact_version}) armbian-repo-name; urgency=low * Initial changelog entry for ${control_package_name} package hash ${artifact_version} -- $MAINTAINER <$MAINTAINERMAIL> $(date -R) EOF # Also a usr/share/doc/${control_package_name}/changelog.gz mkdir -p "${first_arg}/usr/share/doc/${control_package_name}" gzip -9 -c "${first_arg}/DEBIAN/changelog" > "${first_arg}/usr/share/doc/${control_package_name}/changelog.gz" # find the DEBIAN scripts (postinst, prerm, etc) and run shellcheck on them. dpkg_deb_run_shellcheck_on_scripts "${first_arg}" # Debug, dump the generated postrm/preinst/postinst if [[ "${SHOW_DEBUG}" == "yes" || "${SHOW_DEBIAN}" == "yes" ]]; then # Dump the CONTROL file to the log run_tool_batcat --file-name "${artifact_name}/DEBIAN/control" "${first_arg}/DEBIAN/control" if [[ -f "${first_arg}/DEBIAN/changelog" ]]; then run_tool_batcat --file-name "${artifact_name}/DEBIAN/changelog" "${first_arg}/DEBIAN/changelog" fi if [[ -f "${first_arg}/DEBIAN/postrm" ]]; then run_tool_batcat --file-name "${artifact_name}/DEBIAN/postrm.sh" "${first_arg}/DEBIAN/postrm" fi if [[ -f "${first_arg}/DEBIAN/preinst" ]]; then run_tool_batcat --file-name "${artifact_name}/DEBIAN/preinst.sh" "${first_arg}/DEBIAN/preinst" fi if [[ -f "${first_arg}/DEBIAN/postinst" ]]; then run_tool_batcat --file-name "${artifact_name}/DEBIAN/postinst.sh" "${first_arg}/DEBIAN/postinst" fi run_tool_batcat --file-name "${artifact_name}/DEBIAN/md5sums" "${first_arg}/DEBIAN/md5sums" fi mkdir -p "${artifact_base_dir}" run_host_command_logged_raw fakeroot dpkg-deb -b "-Z${DEB_COMPRESS}" "${first_arg}" "${artifact_base_dir}/" } function dpkg_deb_run_shellcheck_on_scripts() { declare pkg_dir="$1" [[ -z "${pkg_dir}" ]] && exit_with_error "dpkg_deb_run_shellcheck_on_scripts: no package directory specified" [[ ! -d "${pkg_dir}" ]] && exit_with_error "dpkg_deb_run_shellcheck_on_scripts: pkg dir '${pkg_dir}' doesn't exist" [[ ! -d "${pkg_dir}/DEBIAN" ]] && exit_with_error "dpkg_deb_run_shellcheck_on_scripts: pkg dir '${pkg_dir}'/DEBIAN doesn't exist" # parse the DEBIAN/control script to find the name of the package declare pkg_name pkg_name=$(grep -E "^Package:" "${pkg_dir}/DEBIAN/control" | cut -d: -f2 | tr -d '[:space:]') [[ -z "${pkg_name}" ]] && exit_with_error "dpkg_deb_run_shellcheck_on_scripts: can't find package name in ${pkg_dir}/DEBIAN/control" # use "find" to find executable files in the package directory declare -a executables=($(find "${pkg_dir}/DEBIAN" -type f -executable || true)) # @TODO: also include other, executable, shells scripts found in the dir; e.g. /usr/bin, /usr/sbin, etc. # if more than zero items in array... if [[ ${#executables[@]} -gt 0 ]]; then display_alert "Running shellcheck on package scripts" "${executables[*]}" "debug" if shellcheck_debian_control_scripts "${executables[@]}"; then display_alert "shellcheck found no problems in package scripts" "shellchecked ${#executables[@]} scripts in '${pkg_name}'" "info" else display_alert "shellcheck found problems in package scripts; see above" "shellcheck failed for '${pkg_name}'" "wrn" fi else display_alert "shellcheck found no problems in package scripts" "no scripts found for '${pkg_name}'" "info" fi } function artifact_package_hook_helper_board_side_functions() { declare script="${1}" shift # each remaining arg is a function name; for each, run 'declare -f', remove the first, second and last line (function declaration, open brace, close brac, and add it to contents. declare -a functions=("$@") declare contents="" declare newline=$'\n' for function in "${functions[@]}"; do contents+="${newline}## begin contents of '${function}'${newline}" contents+="$(declare -f "${function}" | sed -e '2d' -e '1d' -e '$d')" contents+="${newline}## end contents of '${function}'${newline}" done generic_artifact_package_hook_helper "${script}" "${contents}" } function generic_artifact_package_hook_helper() { # check '$destination' is set [[ -z "${destination}" ]] && exit_with_error "generic_artifact_package_hook_helper: destination not set" declare script="${1}" declare contents="${2}" declare package_DEBIAN_dir="${destination}"/DEBIAN [[ ! -d "${package_DEBIAN_dir}" ]] && exit_with_error "generic_artifact_package_hook_helper: package DEBIAN dir '${package_DEBIAN_dir}' doesn't exist" cat >> "${package_DEBIAN_dir}/${script}" <<- EOT #!/bin/bash echo "Armbian '${artifact_name:?}' for '${artifact_version:?}': '${script}' starting." set +e # NO ERROR CONTROL, for compatibility with legacy Armbian scripts. #set -e # Debugging $(echo "${contents}") set +x # Disable debugging echo "Armbian '${artifact_name:?}' for '${artifact_version:?}': '${script}' finishing." true EOT chmod 755 "${package_DEBIAN_dir}/${script}" # produce log asset for script (@TODO: batcat?) LOG_ASSET="deb-${artifact_name:?}-${script}.sh" do_with_log_asset run_host_command_logged cat "${package_DEBIAN_dir}/${script}" }