# Build opkg packages # # Copyright (C) 2012, 2019 Patrick McDermott # # This file is part of opkbuild. # # opkbuild is free software: you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # opkbuild is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with opkbuild. If not, see . set -eu export OPK_SOURCE= export OPK_SOURCE_VERSION= export OPK_SOURCE_VERSION_UPSTREAM= export OPK_BINARY_VERSION= export OPK_PACKAGES= export OPK_PACKAGES_REDUCED= export OPK_BUILD_ARCH= export OPK_BUILD_ARCH_CPU= export OPK_BUILD_ARCH_KERNEL= export OPK_BUILD_ARCH_LIBS= export OPK_BUILD_PLAT= export OPK_HOST_ARCH= export OPK_HOST_ARCH_CPU= export OPK_HOST_ARCH_KERNEL= export OPK_HOST_ARCH_LIBS= export OPK_HOST_PLAT= export SOURCE_DATE_EPOCH= bindir= opt_build= opt_arch_dep= opt_plat_dep= opt_target= opt_host_arch= opt_host_plat= opt_check_build_deps= opt_clean= opt_uid0_cmd= usage() { printf "$(ob_get_msg 'usage')\n" 'opkbuild' } help() { usage printf '%s\n' "$(ob_get_msg 'help')" } version() { printf "$(ob_get_msg 'version')\n" \ 'opkbuild' "${PACKAGE_NAME}" "${PACKAGE_VERSION}" } get_options() { local opt= # Set default option values. opt_build='full' opt_check_build_deps=true opt_uid0_cmd="${FAKEROOT}" opt_arch_dep=false opt_plat_dep=false # Parse and handle command-line options. # Remember to update the optstring file when changing this. while getopts 'bBPSFT:a:p:DdCcr:hV' opt; do case "${opt}" in b) case "${opt_build}" in 'full'|'binary');; *) ob_error "$(ob_get_msg 'bsf_mutex')" return 1 esac opt_build='binary' ;; B) case "${opt_build}" in 'full'|'binary');; *) ob_error "$(ob_get_msg 'bsf_mutex')" return 1 esac opt_build='binary' opt_arch_dep=true ;; P) case "${opt_build}" in 'full'|'binary');; *) ob_error "$(ob_get_msg 'bsf_mutex')" return 1 esac opt_build='binary' opt_plat_dep=true ;; S) case "${opt_build}" in 'full'|'source');; *) ob_error "$(ob_get_msg 'bsf_mutex')" return 1 esac opt_build='source' ;; F) case "${opt_build}" in ''|'full');; *) ob_error "$(ob_get_msg 'bsf_mutex')" return 1 esac opt_build='full' ;; T) opt_target="${OPTARG}" ;; a) opt_host_arch="${OPTARG}" ;; p) opt_host_plat="${OPTARG}" ;; D) opt_check_build_deps='true' ;; d) opt_check_build_deps='false' ;; C) opt_clean='true' ;; c) opt_clean='false' ;; r) opt_uid0_cmd="${OPTARG}" ;; h) help exit 0 ;; V) version exit 0 ;; ?) ob_error "$(ob_get_msg 'bad_opt')" "${opt}" return 1 ;; esac done # Set cleaning behavior. if [ -n "${opt_target}" ]; then [ -z "${opt_clean}" ] && opt_clean='false' else [ -z "${opt_clean}" ] && opt_clean='true' fi return 0 } test_uid0_cmd() { local test_uid= # Verify that the UID 0 command works. if ! test_uid=$(${opt_uid0_cmd} -- id -u); then ob_error "$(ob_get_msg 'uid0_cmd_not_found')" "${opt_uid0_cmd}" return 1 fi if [ x"${test_uid}" != x'0' ]; then ob_error "$(ob_get_msg 'uid0_cmd_bad_uid')" "${opt_uid0_cmd}" return 1 fi return 0 } setup_arch_plat() { ob_info "$(ob_get_msg 'setup_arch_plat')" OPK_BUILD_ARCH="$(ob_get_system_arch)" OPK_BUILD_PLAT="$(ob_get_system_plat)" if [ -n "${opt_host_arch}" ]; then OPK_HOST_ARCH="${opt_host_arch}" else OPK_HOST_ARCH="${OPK_BUILD_ARCH}" fi if [ -n "${opt_host_plat}" ]; then OPK_HOST_PLAT="${opt_host_plat}" else OPK_HOST_PLAT="${OPK_BUILD_PLAT}" fi IFS='-' read OPK_BUILD_ARCH_CPU OPK_BUILD_ARCH_KERNEL \ OPK_BUILD_ARCH_LIBS <<-EOF ${OPK_BUILD_ARCH} EOF IFS='-' read OPK_HOST_ARCH_CPU OPK_HOST_ARCH_KERNEL \ OPK_HOST_ARCH_LIBS <<-EOF ${OPK_HOST_ARCH} EOF return 0 } make_work_area() { ob_info "$(ob_get_msg 'make_work_area')" mkdir -p 'tmp' || return 1 cd 'tmp' || return 1 return 0 } setup_package() { ob_info "$(ob_get_msg 'parse_package_metadata')" ob_init_package '..' || return 1 OPK_SOURCE="$(ob_get_source_parameter 'Source')" OPK_SOURCE_VERSION="$(ob_get_source_parameter 'Version')" ob_parse_version -u 'OPK_SOURCE_VERSION_UPSTREAM' -- \ "${OPK_SOURCE_VERSION}" OPK_BINARY_VERSION="$(ob_get_source_parameter 'Version')" SOURCE_DATE_EPOCH="$(ob_rfc822_mktime \ "$(ob_get_source_parameter 'Date')")" return 0 } build_source() { local src= local ver= local src_pkg_data_base= local file= ob_info "$(ob_get_msg 'build_source')" src="$(ob_get_source_parameter 'Source')" ver="$(ob_get_source_parameter 'Version')" src_pkg_data_base="src:${src}.data$(ob_get_system_path \ 'package-source' "${src}" "${ver}")" if ! mkdir -p -- "${src_pkg_data_base}"; then ob_error "$(ob_get_msg 'cant_make_src_pkg_dir')" return 1 fi for file in ../*; do case "${file}" in ../tmp) ;; ../*) if ! cp -Rp -- "${file}" "${src_pkg_data_base}" then ob_error "$(ob_get_msg \ 'cant_install_src_pkg_file')" return 1 fi ;; esac done if ! OB_DO_SOURCE='true' "${bindir}/ob-gencontrol"; then return 1 fi if ! OB_DO_SOURCE='true' ${opt_uid0_cmd} -- "${bindir}/ob-buildopk" then return 1 fi if ! OB_DO_SOURCE='true' "${bindir}/ob-genchanges"; then return 1 fi if ! rm -Rf "src:${src}.data"; then ob_error "$(ob_get_msg 'cant_rm_src_pkg_data')" return 1 fi return 0 } print_arch_stats() { ob_info "$(ob_get_msg 'build_arch_stat_header')" ob_info "$(ob_get_msg 'arch_stat_arch')" "${OPK_BUILD_ARCH}" ob_info "$(ob_get_msg 'arch_stat_plat')" "${OPK_BUILD_PLAT}" ob_info "$(ob_get_msg 'host_arch_stat_header')" ob_info "$(ob_get_msg 'arch_stat_arch')" "${OPK_HOST_ARCH}" ob_info "$(ob_get_msg 'arch_stat_plat')" "${OPK_HOST_PLAT}" } setup_build() { OPK_PACKAGES="$(\ ob_get_binary_packages -a "${OPK_HOST_ARCH}" \ -p "${OPK_HOST_PLAT}"; \ ob_get_binary_packages -a "${OPK_HOST_ARCH}" -p 'all'; \ ob_get_binary_packages -a 'all' -p "${OPK_HOST_PLAT}"; \ ob_get_binary_packages -a 'all' -p 'all')" # This is an internal non-SPF variable. The definition of OPK_PACKAGES # may change in future SPF versions. # TODO: ??? OPK_PACKAGES_REDUCED="$(\ ob_get_binary_packages -a "${OPK_HOST_ARCH}" \ -p "${OPK_HOST_PLAT}")" if ! ${opt_plat_dep}; then OPK_PACKAGES_REDUCED="${OPK_PACKAGES_REDUCED} $(\ ob_get_binary_packages -a "${OPK_HOST_ARCH}" -p 'all')" fi if ! ${opt_arch_dep}; then OPK_PACKAGES_REDUCED="${OPK_PACKAGES_REDUCED} $(\ ob_get_binary_packages -a 'all' -p "${OPK_HOST_PLAT}")" if ! ${opt_plat_dep}; then OPK_PACKAGES_REDUCED="${OPK_PACKAGES_REDUCED} $(\ ob_get_binary_packages -a 'all' -p 'all')" fi fi eval "$("${bindir}/ob-buildenv" | sed 's/^/export /')" if ! "${bindir}/ob-unpacksource"; then return 1 fi if ! "${bindir}/ob-applypatches"; then return 1 fi if ! "${bindir}/ob-installplatconf"; then return 1 fi return 0 } build() { case "${opt_target}" in '') if ! ../build build || ! ${opt_uid0_cmd} -- ${SH} <<-EOF ../build install && "${bindir}/ob-installdocs" && "${bindir}/ob-gencontrol" && "${bindir}/ob-buildopk" && "${bindir}/ob-genchanges" EOF then return 1 fi ;; 'install'|'install-'*) if ! ${opt_uid0_cmd} -- ../build -- "${opt_target}" then return 1 fi ;; *) if ! ../build -- "${opt_target}"; then return 1 fi ;; esac return 0 } clean() { if "${opt_clean}"; then cd .. || return 1 rm -Rf 'tmp' || return 1 fi return 0 } main() { if ! ob_set_text_domain 'opkbuild'; then printf '%s: Error: Failed to load locale messages\n' \ "${0##*/}" >&2 return 1 fi get_options "${@}" || return 1 shift $((${OPTIND} - 1)) if [ ${#} -ne 0 ]; then usage return 1 fi if [ x"${OB_BINDIR:+set}" = x'set' ]; then bindir="${OB_BINDIR}" else bindir="${BINDIR}" fi test_uid0_cmd || return 1 setup_arch_plat || return 1 make_work_area || return 1 setup_package || return 1 if [ x"${opt_build}" = x'source' ] || [ x"${opt_build}" = x'full' ] then build_source || return 1 fi if [ x"${opt_build}" != x'source' ]; then print_arch_stats if ! "${bindir}/ob-checkbuilddeps"; then if "${opt_check_build_deps}"; then return 1 fi fi setup_build || return 1 build || return 1 clean || return 1 fi return 0 }