#!@@SH@@
#
# 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
. @@LIBDIR@@/control
# 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=
# 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
oh_set_source_dir .
get_options "${@}"
shift $(($OPTIND - 1))
if [ "${#}" -ne 0 ]; then
oh_usage
exit 1
fi
test_uid0_cmd
find_sanity
oh_changelog_parse setup_package
if [ "${OPT_BUILD}" = 'source' -o "${OPT_BUILD}" = 'full' ]; then
build_source
fi
if [ "${OPT_BUILD}" != 'source' ]; then
setup_source_control
"${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_bbasf_mutex}"
fi
OPT_BUILD=binary
;;
B)
if [ -n "${OPT_BUILD}" ]; then
oh_error "${oh_str_bbasf_mutex}"
fi
OPT_BUILD=binary-arch
;;
A)
if [ -n "${OPT_BUILD}" ]; then
oh_error "${oh_str_bbasf_mutex}"
fi
OPT_BUILD=binary-indep
;;
S)
if [ -n "${OPT_BUILD}" ]; then
oh_error "${oh_str_bbasf_mutex}"
fi
OPT_BUILD=source
;;
F)
if [ -n "${OPT_BUILD}" ]; then
oh_error "${oh_str_bbasf_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
# 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_UID0_CMD}" id -u)
if [ "${?}" -ne 0 ]; then
oh_error "${oh_str_uid0_cmd_not_found}" "${OPT_UID0_CMD}"
fi
if [ "${test_uid}" -ne 0 ]; then
oh_error "${oh_str_uid0_cmd_bad_uid}" "${OPT_UID0_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}" != '2.0' ]; then
oh_error "${oh_str_unsupported_format}" "${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()
{
OH_SOURCE="${OH_CHANGELOG_SOURCE}"
OH_SOURCE_VERSION="${OH_CHANGELOG_VERSION}"
OH_SOURCE_VERSION_UPSTREAM="${OH_SOURCE_VERSION:%%-}"
OH_BINARY_VERSION="${OH_SOURCE_VERSION}"
return 1
}
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
oh_control_gen_source
# TODO: oh-buildopk
rm -Rf "src:${OH_SOURCE}.data" || oh_error "${oh_str_cant_rm_src_pkg_data}"
}
setup_source_control()
{
oh_control_parse_source
for field in ${OH_CONTROL_SOURCE_FIELDS_REQUIRED} \
${OH_CONTROL_SOURCE_FIELDS_OPTIONAL}; do
param="OH_CONTROL_SOURCE_FIELD_$(echo "${field}" | \
LC_CTYPE=C tr '[:lower:]-' '[:upper:]_')"
oh_pv_set 'src:.control' "${field}" "$(eval echo \$\{"${param}"\})"
done
}
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 <