Minor cleanup in how we run make:

o Move SRCCONF and __MAKE_CONF into the environment to cope with
  file paths with spaces in them better.
o Move the rest of the variable setting command line args into
  __MAKE_CONF files.
o Trace the commands that we're using to build so they appear at the
  top of the log.
o Be more consistent about quoting paths for cd and similar commands
  to better cope with paths with spaces in them, though some more
  work is likely needed.
o Add some comments about all this.
o Minor formatting tweaks in a couple places

Sponsored by:	Netflix, Inc
This commit is contained in:
Warner Losh 2015-12-05 00:15:04 +00:00
parent 7dfcfa5cf1
commit 1934c7aa0a
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=291828

View File

@ -188,6 +188,47 @@ SRCCONF=${SRCCONF:=/dev/null}
# #
####################################################################### #######################################################################
# Export values into the shell. Must use { } instead of ( ) like
# other functions to avoid a subshell.
# We set __MAKE_CONF as a global since it is easier to get quoting
# right for paths with spaces in them.
make_export ( ) {
# Similar to export_var, except puts the data out to stdout
var=$1
eval val=\$$var
echo "Setting variable: $var=\"$val\""
export $1
}
nano_make_build_env ( ) {
__MAKE_CONF="${NANO_MAKE_CONF_BUILD}"
make_export __MAKE_CONF
}
nano_make_install_env ( ) {
__MAKE_CONF="${NANO_MAKE_CONF_INSTALL}"
make_export __MAKE_CONF
}
# Extra environment variables for kernel builds
nano_make_kernel_env ( ) {
if [ -f ${NANO_KERNEL} ] ; then
KERNCONFDIR="$(realpath $(dirname ${NANO_KERNEL}))"
KERNCONF="$(basename ${NANO_KERNEL})"
make_export KERNCONFDIR
make_export KERNCONF
else
export KERNCONF="${NANO_KERNEL}"
make_export KERNCONF
fi
}
nano_global_make_env ( ) (
[ ! -z "${NANO_ARCH}" ] && echo TARGET_ARCH="${NANO_ARCH}"
[ ! -z "${NANO_CPUTYPE}" ] && echo TARGET_CPUTYPE="${NANO_CPUTYPE}"
echo SRCCONF=${SRCCONF}
)
# rm doesn't know -x prior to FreeBSD 10, so cope with a variety of build # rm doesn't know -x prior to FreeBSD 10, so cope with a variety of build
# hosts for now. # hosts for now.
nano_rm ( ) { nano_rm ( ) {
@ -198,15 +239,13 @@ nano_rm ( ) {
} }
# run in the world chroot, errors fatal # run in the world chroot, errors fatal
CR() CR ( ) {
{ chroot "${NANO_WORLDDIR}" /bin/sh -exc "$*"
chroot ${NANO_WORLDDIR} /bin/sh -exc "$*"
} }
# run in the world chroot, errors not fatal # run in the world chroot, errors not fatal
CR0() CR0 ( ) {
{ chroot "${NANO_WORLDDIR}" /bin/sh -c "$*" || true
chroot ${NANO_WORLDDIR} /bin/sh -c "$*" || true
} }
nano_cleanup ( ) ( nano_cleanup ( ) (
@ -231,19 +270,25 @@ make_conf_build ( ) (
mkdir -p ${MAKEOBJDIRPREFIX} mkdir -p ${MAKEOBJDIRPREFIX}
printenv > ${MAKEOBJDIRPREFIX}/_.env printenv > ${MAKEOBJDIRPREFIX}/_.env
echo "${CONF_WORLD}" > ${NANO_MAKE_CONF_BUILD} # Make sure we get all the global settings that NanoBSD wants
echo "${CONF_BUILD}" >> ${NANO_MAKE_CONF_BUILD} # in addition to the user's global settings
(
nano_global_make_env
echo "${CONF_WORLD}"
echo "${CONF_BUILD}"
) > ${NANO_MAKE_CONF_BUILD}
) )
build_world ( ) ( build_world ( ) (
pprint 2 "run buildworld" pprint 2 "run buildworld"
pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bw" pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bw"
cd ${NANO_SRC} (
env TARGET_ARCH=${NANO_ARCH} TARGET_CPUTYPE=${NANO_CPUTYPE} ${NANO_PMAKE} \ nano_make_build_env
SRCCONF=${SRCCONF} \ set -o xtrace
__MAKE_CONF=${NANO_MAKE_CONF_BUILD} buildworld \ cd "${NANO_SRC}"
> ${MAKEOBJDIRPREFIX}/_.bw 2>&1 ${NANO_PMAKE} buildworld
) > ${MAKEOBJDIRPREFIX}/_.bw 2>&1
) )
build_kernel ( ) ( build_kernel ( ) (
@ -253,20 +298,16 @@ build_kernel ( ) (
pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bk" pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bk"
( (
if [ -f ${NANO_KERNEL} ] ; then nano_make_build_env
kernconfdir_arg="KERNCONFDIR='$(realpath $(dirname ${NANO_KERNEL}))'" nano_make_kernel_env
kernconf=$(basename ${NANO_KERNEL})
else
kernconf=${NANO_KERNEL}
fi
cd ${NANO_SRC};
# Note: We intentionally build all modules, not only the ones in # Note: We intentionally build all modules, not only the ones in
# NANO_MODULES so the built world can be reused by multiple images. # NANO_MODULES so the built world can be reused by multiple images.
eval "TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} buildkernel \ # Although MODULES_OVERRIDE can be defined in the kenrel config
SRCCONF='${SRCCONF}' \ # file to override this behavior. Just set NANO_MODULES=default.
__MAKE_CONF='${NANO_MAKE_CONF_BUILD}' \ set -o xtrace
${kernconfdir_arg} KERNCONF=${kernconf}" cd "${NANO_SRC}"
${NANO_PMAKE} buildkernel
) > ${MAKEOBJDIRPREFIX}/_.bk 2>&1 ) > ${MAKEOBJDIRPREFIX}/_.bk 2>&1
) )
@ -277,36 +318,41 @@ clean_world ( ) (
chflags -R noschg ${NANO_OBJ} chflags -R noschg ${NANO_OBJ}
nano_rm -r ${NANO_OBJ}/ nano_rm -r ${NANO_OBJ}/
fi fi
mkdir -p ${NANO_OBJ} ${NANO_WORLDDIR} mkdir -p "${NANO_OBJ}" "${NANO_WORLDDIR}"
printenv > ${NANO_OBJ}/_.env printenv > ${NANO_OBJ}/_.env
else else
pprint 2 "Clean and create world directory (${NANO_WORLDDIR})" pprint 2 "Clean and create world directory (${NANO_WORLDDIR})"
if ! nano_rm -rf ${NANO_WORLDDIR}/ > /dev/null 2>&1 ; then if ! nano_rm -rf "${NANO_WORLDDIR}/" > /dev/null 2>&1 ; then
chflags -R noschg ${NANO_WORLDDIR} chflags -R noschg "${NANO_WORLDDIR}"
nano_rm -rf ${NANO_WORLDDIR}/ nano_rm -rf "${NANO_WORLDDIR}/"
fi fi
mkdir -p ${NANO_WORLDDIR} mkdir -p "${NANO_WORLDDIR}"
fi fi
) )
make_conf_install ( ) ( make_conf_install ( ) (
pprint 2 "Construct install make.conf ($NANO_MAKE_CONF_INSTALL)" pprint 2 "Construct install make.conf ($NANO_MAKE_CONF_INSTALL)"
echo "${CONF_WORLD}" > ${NANO_MAKE_CONF_INSTALL} # Make sure we get all the global settings that NanoBSD wants
echo "${CONF_INSTALL}" >> ${NANO_MAKE_CONF_INSTALL} # in addition to the user's global settings
(
nano_global_make_env
echo "${CONF_WORLD}"
echo "${CONF_INSTALL}"
) > ${NANO_MAKE_CONF_INSTALL}
) )
install_world ( ) ( install_world ( ) (
pprint 2 "installworld" pprint 2 "installworld"
pprint 3 "log: ${NANO_OBJ}/_.iw" pprint 3 "log: ${NANO_OBJ}/_.iw"
cd ${NANO_SRC} (
env TARGET_ARCH=${NANO_ARCH} TARGET_CPUTYPE=${NANO_CPUTYPE} \ nano_make_install_env
${NANO_MAKE} SRCCONF=${SRCCONF} \ set -o xtrace
__MAKE_CONF=${NANO_MAKE_CONF_INSTALL} installworld \ cd "${NANO_SRC}"
DESTDIR=${NANO_WORLDDIR} \ ${NANO_MAKE} installworld DESTDIR="${NANO_WORLDDIR}"
> ${NANO_OBJ}/_.iw 2>&1 chflags -R noschg "${NANO_WORLDDIR}"
chflags -R noschg ${NANO_WORLDDIR} ) > ${NANO_OBJ}/_.iw 2>&1
) )
install_etc ( ) ( install_etc ( ) (
@ -314,15 +360,15 @@ install_etc ( ) (
pprint 2 "install /etc" pprint 2 "install /etc"
pprint 3 "log: ${NANO_OBJ}/_.etc" pprint 3 "log: ${NANO_OBJ}/_.etc"
cd ${NANO_SRC} (
env TARGET_ARCH=${NANO_ARCH} TARGET_CPUTYPE=${NANO_CPUTYPE} \ nano_make_install_env
${NANO_MAKE} SRCCONF=${SRCCONF} \ set -o xtrace
__MAKE_CONF=${NANO_MAKE_CONF_INSTALL} distribution \ cd "${NANO_SRC}"
DESTDIR=${NANO_WORLDDIR} \ ${NANO_MAKE} distribution DESTDIR="${NANO_WORLDDIR}"
> ${NANO_OBJ}/_.etc 2>&1
# make.conf doesn't get created by default, but some ports need it # make.conf doesn't get created by default, but some ports need it
# so they can spam it. # so they can spam it.
cp /dev/null ${NANO_WORLDDIR}/etc/make.conf cp /dev/null "${NANO_WORLDDIR}"/etc/make.conf
) > ${NANO_OBJ}/_.etc 2>&1
) )
install_kernel ( ) ( install_kernel ( ) (
@ -332,27 +378,19 @@ install_kernel ( ) (
pprint 3 "log: ${NANO_OBJ}/_.ik" pprint 3 "log: ${NANO_OBJ}/_.ik"
( (
if [ -f ${NANO_KERNEL} ] ; then
kernconfdir_arg="KERNCONFDIR='$(realpath $(dirname ${NANO_KERNEL}))'"
kernconf=$(basename ${NANO_KERNEL})
else
kernconf=${NANO_KERNEL}
fi
# Install all built modules if NANO_MODULES=default, nano_make_install_env
# else install only listed modules (none if NANO_MODULES is empty). nano_make_kernel_env
if [ "${NANO_MODULES}" != "default" ]; then if [ "${NANO_MODULES}" != "default" ]; then
modules_override_arg="MODULES_OVERRIDE='${NANO_MODULES}'" MODULES_OVERRIDE="${NANO_MODULES}"
make_export MODULES_OVERRIDE
fi fi
cd ${NANO_SRC} set -o xtrace
eval "TARGET_ARCH=${NANO_ARCH} TARGET_CPUTYPE=${NANO_CPUTYPE} \ cd "${NANO_SRC}"
${NANO_MAKE} installkernel \ ${NANO_MAKE} installkernel DESTDIR="${NANO_WORLDDIR}"
DESTDIR='${NANO_WORLDDIR}' \
SRCCONF='${SRCCONF}' \
__MAKE_CONF='${NANO_MAKE_CONF_INSTALL}' \
${kernconfdir_arg} KERNCONF=${kernconf} \
${modules_override_arg}"
) > ${NANO_OBJ}/_.ik 2>&1 ) > ${NANO_OBJ}/_.ik 2>&1
) )
@ -360,15 +398,17 @@ native_xtools ( ) (
print 2 "Installing the optimized native build tools for cross env" print 2 "Installing the optimized native build tools for cross env"
pprint 3 "log: ${NANO_OBJ}/_.native_xtools" pprint 3 "log: ${NANO_OBJ}/_.native_xtools"
cd ${NANO_SRC} (
env TARGET_ARCH=${NANO_ARCH} TARGET_CPUTYPE=${NANO_CPUTYPE} \
${NANO_MAKE} SRCCONF=${SRCCONF} \ nano_make_install_env
__MAKE_CONF=${NANO_MAKE_CONF_INSTALL} native-xtools \ set -o xtrace
DESTDIR=${NANO_WORLDDIR} \ cd "${NANO_SRC}"
> ${NANO_OBJ}/_.native_xtools 2>&1 ${NANO_MAKE} native-xtools DESTDIR="${NANO_WORLDDIR}"
) > ${NANO_OBJ}/_.native_xtools 2>&1
) )
run_customize() ( run_customize ( ) (
pprint 2 "run customize scripts" pprint 2 "run customize scripts"
for c in $NANO_CUSTOMIZE for c in $NANO_CUSTOMIZE
@ -380,7 +420,7 @@ run_customize() (
done done
) )
run_late_customize() ( run_late_customize ( ) (
pprint 2 "run late customize scripts" pprint 2 "run late customize scripts"
for c in $NANO_LATE_CUSTOMIZE for c in $NANO_LATE_CUSTOMIZE
@ -397,7 +437,7 @@ setup_nanobsd ( ) (
pprint 3 "log: ${NANO_OBJ}/_.dl" pprint 3 "log: ${NANO_OBJ}/_.dl"
( (
cd ${NANO_WORLDDIR} cd "${NANO_WORLDDIR}"
# Move /usr/local/etc to /etc/local so that the /cfg stuff # Move /usr/local/etc to /etc/local so that the /cfg stuff
# can stomp on it. Otherwise packages like ipsec-tools which # can stomp on it. Otherwise packages like ipsec-tools which
@ -439,7 +479,7 @@ setup_nanobsd_etc ( ) (
pprint 2 "configure nanobsd /etc" pprint 2 "configure nanobsd /etc"
( (
cd ${NANO_WORLDDIR} cd "${NANO_WORLDDIR}"
# create diskless marker file # create diskless marker file
touch etc/diskless touch etc/diskless
@ -456,10 +496,10 @@ setup_nanobsd_etc ( ) (
) )
) )
prune_usr() ( prune_usr ( ) (
# Remove all empty directories in /usr # Remove all empty directories in /usr
find ${NANO_WORLDDIR}/usr -type d -depth -print | find "${NANO_WORLDDIR}"/usr -type d -depth -print |
while read d while read d
do do
rmdir $d > /dev/null 2>&1 || true rmdir $d > /dev/null 2>&1 || true
@ -478,7 +518,7 @@ newfs_part ( ) (
# Convenient spot to work around any umount issues that your build environment # Convenient spot to work around any umount issues that your build environment
# hits by overriding this method. # hits by overriding this method.
nano_umount () ( nano_umount ( ) (
umount ${1} umount ${1}
) )
@ -492,7 +532,7 @@ populate_slice ( ) (
newfs_part ${dev} ${mnt} ${lbl} newfs_part ${dev} ${mnt} ${lbl}
if [ -n "${dir}" -a -d "${dir}" ]; then if [ -n "${dir}" -a -d "${dir}" ]; then
echo "Populating ${lbl} from ${dir}" echo "Populating ${lbl} from ${dir}"
cd ${dir} cd "${dir}"
find . -print | grep -Ev '/(CVS|\.svn|\.hg|\.git)' | cpio -dumpv ${mnt} find . -print | grep -Ev '/(CVS|\.svn|\.hg|\.git)' | cpio -dumpv ${mnt}
fi fi
df -i ${mnt} df -i ${mnt}
@ -615,9 +655,9 @@ create_diskimage ( ) (
populate_slice /dev/${MD}${NANO_SLICE_ROOT}a ${NANO_WORLDDIR} ${MNT} "${NANO_SLICE_ROOT}a" populate_slice /dev/${MD}${NANO_SLICE_ROOT}a ${NANO_WORLDDIR} ${MNT} "${NANO_SLICE_ROOT}a"
mount /dev/${MD}${NANO_SLICE_ROOT}a ${MNT} mount /dev/${MD}${NANO_SLICE_ROOT}a ${MNT}
echo "Generating mtree..." echo "Generating mtree..."
( cd ${MNT} && mtree -c ) > ${NANO_OBJ}/_.mtree ( cd "${MNT}" && mtree -c ) > ${NANO_OBJ}/_.mtree
( cd ${MNT} && du -k ) > ${NANO_OBJ}/_.du ( cd "${MNT}" && du -k ) > ${NANO_OBJ}/_.du
nano_umount ${MNT} nano_umount "${MNT}"
if [ $NANO_IMAGES -gt 1 -a $NANO_INIT_IMG2 -gt 0 ] ; then if [ $NANO_IMAGES -gt 1 -a $NANO_INIT_IMG2 -gt 0 ] ; then
# Duplicate to second image (if present) # Duplicate to second image (if present)
@ -674,7 +714,7 @@ create_diskimage ( ) (
) > ${NANO_OBJ}/_.di 2>&1 ) > ${NANO_OBJ}/_.di 2>&1
) )
last_orders () ( last_orders ( ) (
# Redefine this function with any last orders you may have # Redefine this function with any last orders you may have
# after the build completed, for instance to copy the finished # after the build completed, for instance to copy the finished
# image to a more convenient place: # image to a more convenient place:
@ -692,7 +732,7 @@ last_orders () (
# Common Flash device geometries # Common Flash device geometries
# #
FlashDevice () { FlashDevice ( ) {
if [ -d ${NANO_TOOLS} ] ; then if [ -d ${NANO_TOOLS} ] ; then
. ${NANO_TOOLS}/FlashDevice.sub . ${NANO_TOOLS}/FlashDevice.sub
else else
@ -721,7 +761,7 @@ FlashDevice () {
# The generic-hdd device is preferred for flash devices larger than 1GB. # The generic-hdd device is preferred for flash devices larger than 1GB.
# #
UsbDevice () { UsbDevice ( ) {
a1=`echo $1 | tr '[:upper:]' '[:lower:]'` a1=`echo $1 | tr '[:upper:]' '[:lower:]'`
case $a1 in case $a1 in
generic-fdd) generic-fdd)
@ -744,7 +784,7 @@ UsbDevice () {
####################################################################### #######################################################################
# Setup serial console # Setup serial console
cust_comconsole () ( cust_comconsole ( ) (
# Enable getty on console # Enable getty on console
sed -i "" -e /tty[du]0/s/off/on/ ${NANO_WORLDDIR}/etc/ttys sed -i "" -e /tty[du]0/s/off/on/ ${NANO_WORLDDIR}/etc/ttys
@ -758,7 +798,7 @@ cust_comconsole () (
####################################################################### #######################################################################
# Allow root login via ssh # Allow root login via ssh
cust_allow_ssh_root () ( cust_allow_ssh_root ( ) (
sed -i "" -e '/PermitRootLogin/s/.*/PermitRootLogin yes/' \ sed -i "" -e '/PermitRootLogin/s/.*/PermitRootLogin yes/' \
${NANO_WORLDDIR}/etc/ssh/sshd_config ${NANO_WORLDDIR}/etc/ssh/sshd_config
) )
@ -766,15 +806,15 @@ cust_allow_ssh_root () (
####################################################################### #######################################################################
# Install the stuff under ./Files # Install the stuff under ./Files
cust_install_files () ( cust_install_files ( ) (
cd ${NANO_TOOLS}/Files cd "${NANO_TOOLS}/Files"
find . -print | grep -Ev '/(CVS|\.svn|\.hg|\.git)' | cpio -Ldumpv ${NANO_WORLDDIR} find . -print | grep -Ev '/(CVS|\.svn|\.hg|\.git)' | cpio -Ldumpv ${NANO_WORLDDIR}
) )
####################################################################### #######################################################################
# Install packages from ${NANO_PACKAGE_DIR} # Install packages from ${NANO_PACKAGE_DIR}
cust_pkgng () ( cust_pkgng ( ) (
# If the package directory doesn't exist, we're done. # If the package directory doesn't exist, we're done.
if [ ! -d ${NANO_PACKAGE_DIR} ]; then if [ ! -d ${NANO_PACKAGE_DIR} ]; then
@ -794,8 +834,8 @@ cust_pkgng () (
# Copy packages into chroot # Copy packages into chroot
mkdir -p ${NANO_WORLDDIR}/Pkg mkdir -p ${NANO_WORLDDIR}/Pkg
( (
cd ${NANO_PACKAGE_DIR} cd "${NANO_PACKAGE_DIR}"
find ${NANO_PACKAGE_LIST} -print | find "${NANO_PACKAGE_LIST}" -print |
cpio -Ldumpv ${NANO_WORLDDIR}/Pkg cpio -Ldumpv ${NANO_WORLDDIR}/Pkg
) )
@ -842,7 +882,7 @@ cust_pkgng () (
# Convenience function: # Convenience function:
# Register all args as customize function. # Register all args as customize function.
customize_cmd () { customize_cmd ( ) {
NANO_CUSTOMIZE="$NANO_CUSTOMIZE $*" NANO_CUSTOMIZE="$NANO_CUSTOMIZE $*"
} }
@ -851,7 +891,7 @@ customize_cmd () {
# Register all args as late customize function to run just before # Register all args as late customize function to run just before
# image creation. # image creation.
late_customize_cmd () { late_customize_cmd ( ) {
NANO_LATE_CUSTOMIZE="$NANO_LATE_CUSTOMIZE $*" NANO_LATE_CUSTOMIZE="$NANO_LATE_CUSTOMIZE $*"
} }
@ -863,14 +903,14 @@ late_customize_cmd () {
# Progress Print # Progress Print
# Print $2 at level $1. # Print $2 at level $1.
pprint() ( pprint ( ) (
if [ "$1" -le $PPLEVEL ]; then if [ "$1" -le $PPLEVEL ]; then
runtime=$(( `date +%s` - $NANO_STARTTIME )) runtime=$(( `date +%s` - $NANO_STARTTIME ))
printf "%s %.${1}s %s\n" "`date -u -r $runtime +%H:%M:%S`" "#####" "$2" 1>&3 printf "%s %.${1}s %s\n" "`date -u -r $runtime +%H:%M:%S`" "#####" "$2" 1>&3
fi fi
) )
usage () { usage ( ) {
( (
echo "Usage: $0 [-bfiKknqvw] [-c config_file]" echo "Usage: $0 [-bfiKknqvw] [-c config_file]"
echo " -b suppress builds (both kernel and world)" echo " -b suppress builds (both kernel and world)"
@ -891,7 +931,7 @@ usage () {
# Setup and Export Internal variables # Setup and Export Internal variables
# #
export_var() { export_var ( ) { # Don't wawnt a subshell
var=$1 var=$1
# Lookup value of the variable. # Lookup value of the variable.
eval val=\$$var eval val=\$$var
@ -900,7 +940,8 @@ export_var() {
} }
# Call this function to set defaults _after_ parsing options. # Call this function to set defaults _after_ parsing options.
set_defaults_and_export() { # dont want a subshell otherwise variable setting is thrown away.
set_defaults_and_export ( ) {
test -n "${NANO_OBJ}" || NANO_OBJ=/usr/obj/nanobsd.${NANO_NAME} test -n "${NANO_OBJ}" || NANO_OBJ=/usr/obj/nanobsd.${NANO_NAME}
test -n "${MAKEOBJDIRPREFIX}" || MAKEOBJDIRPREFIX=${NANO_OBJ} test -n "${MAKEOBJDIRPREFIX}" || MAKEOBJDIRPREFIX=${NANO_OBJ}
test -n "${NANO_DISKIMGDIR}" || NANO_DISKIMGDIR=${NANO_OBJ} test -n "${NANO_DISKIMGDIR}" || NANO_DISKIMGDIR=${NANO_OBJ}
@ -943,4 +984,5 @@ set_defaults_and_export() {
export_var NANO_BOOTLOADER export_var NANO_BOOTLOADER
export_var NANO_LABEL export_var NANO_LABEL
export_var NANO_MODULES export_var NANO_MODULES
export_var SRCCONF
} }