From 1889c0e175eb960ecaae7d0abf4a944d2069ae9e Mon Sep 17 00:00:00 2001 From: apb Date: Fri, 29 Sep 2006 19:53:54 +0000 Subject: [PATCH] Use ${HOST_SH} instead of the host system's /bin/sh wherever possible: * Try to guess a suitable value for HOST_SH, if it was not set in the environment. First try host-specific heuristics (Solaris has /usr/xpg4/bin/sh); then try to find the name of the shell that was used to run build.sh itself (by parsing the output from ps -p $$ -o comm); then fall back to "sh". * Having found a value for HOST_SH, copy it to BSHELL and CONFIG_SHELL. * Use ${HOST_SH} instead of /bin/sh when creating the make wrapper. * Use ${HOST_SH} instead of unqualified sh when running shell scripts. --- build.sh | 113 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 101 insertions(+), 12 deletions(-) diff --git a/build.sh b/build.sh index d0fc69007c16..ae49c1d6cf76 100755 --- a/build.sh +++ b/build.sh @@ -1,5 +1,5 @@ #! /usr/bin/env sh -# $NetBSD: build.sh,v 1.152 2006/09/01 21:52:55 uwe Exp $ +# $NetBSD: build.sh,v 1.153 2006/09/29 19:53:54 apb Exp $ # # Copyright (c) 2001-2005 The NetBSD Foundation, Inc. # All rights reserved. @@ -38,12 +38,13 @@ # # Top level build wrapper, for a system containing no tools. # -# This script should run on any POSIX-compliant shell. For systems -# with a strange /bin/sh, "ksh" or "bash" may be an ample alternative. -# -# Note, however, that due to the way the interpreter is invoked above, -# if a POSIX-compliant shell is the first in the PATH, you won't have -# to take any further action. +# This script should run on any POSIX-compliant shell. If the +# first "sh" found in the PATH is a POSIX-compliant shell, then +# you should not need to take any special action. Otherwise, you +# should set the environment variable HOST_SH to a POSIX-compliant +# shell, and invoke build.sh with that shell. (Depending on your +# system, one of /bin/ksh, /usr/local/bin/bash, or /usr/xpg4/bin/sh +# might be a suitable shell.) # progname=${0##*/} @@ -68,6 +69,83 @@ statusmsg() ${runcmd} echo "===> $@" | tee -a "${results}" } +# Find a program in the PATH +find_in_PATH() +{ + local prog="$1" + local oldIFS="${IFS}" + local dir + IFS=":" + for dir in ${PATH}; do + if [ -x "${dir}/${prog}" ]; then + prog="${dir}/${prog}" + break + fi + done + IFS="${oldIFS}" + echo "${prog}" +} + +# Try to find a working POSIX shell, and set HOST_SH to refer to it. +# Assumes that uname_s, uname_m, and PWD have been set. +set_HOST_SH() +{ + # Even if ${HOST_SH} is already defined, we still do the + # sanity checks at the end. + + # Solaris has /usr/xpg4/bin/sh. + # + [ -z "${HOST_SH}" ] && [ x"${uname_s}" = x"SunOS" ] && \ + [ -x /usr/xpg4/bin/sh ] && HOST_SH="/usr/xpg4/bin/sh" + + # Try to get the name of the shell that's running this script, + # by parsing the output from "ps". We assume that, if the host + # system's ps command supports -o comm at all, it will do so + # in the usual way: a one-line header followed by a one-line + # result, possibly including trailing white space. And if the + # host system's ps command doesn't support -o comm, we assume + # that we'll get an error message on stderr and nothing on + # stdout. (We don't try to use ps -o 'comm=' to suppress the + # header line, because that is less widely supported.) + # + # If we get the wrong result here, the user can override it by + # specifying HOST_SH in the environment. + # + [ -z "${HOST_SH}" ] && HOST_SH="$( + (ps -p $$ -o comm | sed -ne '2s/[ \t]*$//p') 2>/dev/null )" + + # If nothing above worked, use "sh". We will later find the + # first directory in the PATH that has a "sh" program. + # + [ -z "${HOST_SH}" ] && HOST_SH="sh" + + # If the result so far is not an absolute path, try to prepend + # PWD or search the PATH. + # + case "${HOST_SH}" in + /*) : + ;; + */*) HOST_SH="${PWD}/${HOST_SH}" + ;; + *) HOST_SH="$(find_in_PATH "${HOST_SH}")" + ;; + esac + + # If we don't have an absolute path by now, bomb. + # + case "${HOST_SH}" in + /*) : + ;; + *) bomb "HOST_SH=\"${HOST_SH}\" is not an absolute path." + ;; + esac + + # If HOST_SH is not executable, bomb. + # + [ -x "${HOST_SH}" ] || + bomb "HOST_SH=\"${HOST_SH}\" is not executable." +} + initdefaults() { cd "$(dirname $0)" @@ -89,6 +167,15 @@ initdefaults() unset PWD TOP=$(/bin/pwd -P 2>/dev/null || /bin/pwd 2>/dev/null) + # The user can set HOST_SH in the environment, or we try to + # guess an appropriate value. Then we set several other + # variables from HOST_SH. + # + set_HOST_SH + setmakeenv HOST_SH "${HOST_SH}" + setmakeenv BSHELL "${HOST_SH}" + setmakeenv CONFIG_SHELL "${HOST_SH}" + # Set defaults. # toolprefix=nb @@ -711,9 +798,9 @@ rebuildmake() ${runcmd} cd "${tmpdir}" ${runcmd} env CC="${HOST_CC-cc}" CPPFLAGS="${HOST_CPPFLAGS}" \ CFLAGS="${HOST_CFLAGS--O}" LDFLAGS="${HOST_LDFLAGS}" \ - sh "${TOP}/tools/make/configure" || + ${HOST_SH} "${TOP}/tools/make/configure" || bomb "Configure of ${toolprefix}make failed" - ${runcmd} sh buildmake.sh || + ${runcmd} ${HOST_SH} buildmake.sh || bomb "Build of ${toolprefix}make failed" make="${tmpdir}/${toolprefix}make" ${runcmd} cd "${TOP}" @@ -819,7 +906,7 @@ validatemakeparams() fi if ${do_build} || ${do_distribution} || ${do_release}; then if ! ${do_expertmode} && \ - [ $(id -u 2>/dev/null) -ne 0 ] && \ + [ "$(id -u 2>/dev/null)" -ne 0 ] && \ [ "${MKUNPRIVED}" = "no" ] ; then bomb "-U or -E must be set for build as an unprivileged user." fi @@ -879,9 +966,9 @@ createmakewrapper() esac eval cat <