#!@@SHELL@@
#
# opkhelper
# src/opkbuild
# Build opkg packages.
#
# Copyright (C) 2012 Patrick "P. J." McDermott
#
# This program 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.
#
# This program 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 this program. If not, see .
. @@LIBDIR@@/messages
. @@LIBDIR@@/locale
. @@LIBDIR@@/changelog
# Environment variables:
export OH_SOURCE=
export OH_SOURCE_VERSION=
export OH_SOURCE_VERSION_UPSTREAM=
export OH_BINARY_VERSION=
export OH_BUILD_ARCH=
export OH_BUILD_ARCH_CPU=
export OH_BUILD_ARCH_KERNEL=
export OH_BUILD_ARCH_LIBS=
export OH_BUILD_PLATFORM=
export OH_BUILD_ARCH_GNU=
export OH_HOST_ARCH=
export OH_HOST_ARCH_CPU=
export OH_HOST_ARCH_KERNEL=
export OH_HOST_ARCH_LIBS=
export OH_HOST_PLATFORM=
export OH_HOST_ARCH_GNU=
export OH_TOOL_PREFIX=
export AR=
export AS=
export CC=
export CPP=
export CXXLD=
export NM=
export OBJCOPY=
export OBJDUMP=
export RANLIB=
export READELF=
export SIZE=
export STRINGS=
export STRIP=
# Constant global parameters:
SOURCE_VERSION_ID_RE='^'
SOURCE_VERSION_ID_RE="${SOURCE_VERSION_ID_RE}"'[0-9a-z.~]+'
SOURCE_VERSION_ID_RE="${SOURCE_VERSION_ID_RE}"'(\+sip[1-9][0-9]*)?'
SOURCE_VERSION_ID_RE="${SOURCE_VERSION_ID_RE}"'(-[1-9][0-9]*)?'
SOURCE_VERSION_ID_RE="${SOURCE_VERSION_ID_RE}"'(\+[a-z0-9]+-[1-9][0-9]*)?'
SOURCE_VERSION_ID_RE="${SOURCE_VERSION_ID_RE}"'$'
# Global parameters:
OPT_BUILD=
OPT_TARGET=
OPT_HOST_ARCH=
OPT_HOST_PLAT=
OPT_CHECK_BUILD_DEPS=
OPT_CLEAN=
OPT_UID0_CMD=
main()
{
oh_locale_set
get_options
if [ "${#}" -ne 0 ]; then
oh_usage
exit 1
fi
test_uid0_cmd
find_sanity
oh_changelog_parse changelog setup_package
if [ "${OPT_BUILD}" = 'source' -o "${OPT_BUILD}" = 'full' ]; then
build_source
fi
if [ "${OPT_BUILD}" != 'source' ]; then
"${OPT_CHECK_BUILD_DEPS}" && oh-checkbuilddeps
setup_build
print_arch_stats
if [ -n "${OH_CV_STEP}" ]; then
"step_${OH_CV_STEP}"
else
make_work_area
fi
fi
}
get_options()
{
# Parse and handle command-line options.
while getopts 'bBASFT:a:P:Ddr:hV' opt; do
case "${opt}" in
b)
if [ -n "${OPT_BUILD}" ]; then
oh_error "${oh_str_basf_mutex}"
fi
OPT_BUILD=binary
;;
B)
if [ -n "${OPT_BUILD}" ]; then
oh_error "${oh_str_basf_mutex}"
fi
OPT_BUILD=binary-arch
;;
A)
if [ -n "${OPT_BUILD}" ]; then
oh_error "${oh_str_basf_mutex}"
fi
OPT_BUILD=binary-indep
;;
S)
if [ -n "${OPT_BUILD}" ]; then
oh_error "${oh_str_basf_mutex}"
fi
OPT_BUILD=source
;;
F)
if [ -n "${OPT_BUILD}" ]; then
oh_error "${oh_str_basf_mutex}"
fi
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)
oh_help
exit 0
;;
V)
oh_version
exit 0
;;
?)
oh_error "${oh_str_bad_opt}" "${opt}"
exit 1
;;
esac
done
shift $(($OPTIND - 1))
# Set default option values.
[ -z "${OPT_BUILD}" ] && OPT_BUILD=full
[ -z "${OPT_CHECK_BUILD_DEPS}" ] && OPT_CHECK_BUILD_DEPS=true
[ -z "${OPT_UID0_CMD}" ] && OPT_UID0_CMD=fakeroot
# Set default target and cleaning behavior.
if [ -n "${OPT_TARGET}" ]; then
[ -z "${OPT_CLEAN}" ] && OPT_CLEAN=false
else
if [ "${OPT_BUILD}" = 'full' ]; then
OPT_TARGET=binary
else
OPT_TARGET="${OPT_BUILD}"
fi
[ -z "${OPT_CLEAN}" ] && OPT_CLEAN=true
fi
}
test_uid0_cmd()
{
# Verify that the UID 0 command works.
test_uid=$("${OPT_UID_CMD}" id -u)
if [ "${?}" -ne 0 ]; then
oh_error "${oh_str_uid0_cmd_not_found}" "${OPT_UID_CMD}"
fi
if [ "${test_uid}" -ne 0 ]; then
oh_error "${oh_str_uid0_cmd_bad_uid}" "${OPT_UID_CMD}"
fi
}
find_sanity()
{
oh_info "${oh_str_find_sanity}"
format="$(cat format 2>/dev/null)"
if [ "${?}" -ne 0 ]; then
oh_error "${oh_str_no_format}"
fi
if [ "${format}" -ne 0 ]; then
oh_error "${oh_str_unsupported_format}"
fi
# Check for all files immediately required by SPF 2.0.
[ ! -f 'control' ] && oh_error "${oh_str_no_control}"
[ ! -x 'config' ] && oh_error "${oh_str_no_config}"
[ ! -f 'copyright' ] && oh_error "${oh_str_no_copyright}"
[ ! -f 'changelog' ] && oh_error "${oh_str_no_changelog}"
}
setup_package()
{
[ -n "${OH_SOURCE}" ] && return
OH_SOURCE="${OH_CHANGELOG_SOURCE}"
OH_SOURCE_VERSION="${OH_CHANGELOG_VERSION}"
# Validate source package version identifier.
echo "${OH_SOURCE_VERSION}" | grep -E "${SOURCE_VERSION_ID_RE}" \
>/dev/null 2>&1
if [ "${?}" -ne 0 ]; then
oh_error "${oh_str_bad_source_version}" "${OH_SOURCE_VERSION}"
fi
OH_SOURCE_VERSION_UPSTREAM="${OH_SOURCE_VERSION:%%-}"
OH_BINARY_VERSION="${OH_SOURCE_VERSION}"
}
build_source()
{
oh_info "${oh_str_installing_src_pkg_files}"
src_pkg_data_base="src:${OH_SOURCE}.data/usr/src"
src_pkg_data_base="${src_pkg_data_base}/${OH_SOURCE}_${OH_SOURCE_VERSION}"
"${OPT_UID0_CMD}" mkdir -p \
"${src_pkg_data_base}" || \
oh_error "${oh_str_cant_make_src_pkg_dir}"
for file in ../*; do
case "${file}" in
../tmp)
;;
../*)
"${OPT_UID0_CMD}" cp -R "${file}" "${src_pkg_data_base}" || \
oh_error "${oh_str_cant_install_src_pkg_file}"
;;
esac
done
# TODO: oh-gencontrol
# TODO: oh-buildopk
rm -Rf "src:${OH_SOURCE}.data" || oh_error "${oh_str_cant_rm_src_pkg_data}"
}
setup_build()
{
setup_build_arch
setup_build_flags
setup_build_platform
setup_host_arch
setup_host_platform
setup_toolchain
}
setup_build_arch()
{
# TODO: Get build arch from opkg.
# TODO: Get GNU system type from table.
IFS=- read OH_BUILD_ARCH_CPU OH_BUILD_ARCH_KERNEL OH_BUILD_ARCH_LIBS <