diff --git a/tools/Rules.mk b/tools/Rules.mk index 29639a341d..41a92a28bc 100644 --- a/tools/Rules.mk +++ b/tools/Rules.mk @@ -35,6 +35,11 @@ LDFLAGS += $(shell getconf LFS_LDFLAGS) CFLAGS-$(CONFIG_X86_32) += $(call cc-option,$(CC),-mno-tls-direct-seg-refs) CFLAGS += $(CFLAGS-y) +DEFAULT_PYTHON_PATH := $(shell $(XEN_ROOT)/tools/python/get-path) +PYTHON_PATH ?= $(DEFAULT_PYTHON_PATH) +INSTALL_PYTHON_PROG = $(XEN_ROOT)/tools/python/install-wrap \ +"$(PYTHON_PATH)" $(INSTALL_PROG) + # Require GCC v3.4+ (to avoid issues with alignment constraints in Xen headers) check-$(CONFIG_X86) = $(call cc-ver-check,CC,0x030400,\ "Xen requires at least gcc-3.4") diff --git a/tools/misc/Makefile b/tools/misc/Makefile index 42968d8f57..b8eec1605d 100644 --- a/tools/misc/Makefile +++ b/tools/misc/Makefile @@ -32,8 +32,8 @@ build: $(TARGETS) install: build $(INSTALL_DIR) $(DESTDIR)$(BINDIR) $(INSTALL_DIR) $(DESTDIR)$(SBINDIR) - $(INSTALL_PROG) $(INSTALL_BIN) $(DESTDIR)$(BINDIR) - $(INSTALL_PROG) $(INSTALL_SBIN) $(DESTDIR)$(SBINDIR) + $(INSTALL_PYTHON_PROG) $(INSTALL_BIN) $(DESTDIR)$(BINDIR) + $(INSTALL_PYTHON_PROG) $(INSTALL_SBIN) $(DESTDIR)$(SBINDIR) set -e; for d in $(SUBDIRS); do $(MAKE) -C $$d install-recurse; done .PHONY: clean diff --git a/tools/python/get-path b/tools/python/get-path new file mode 100755 index 0000000000..ae672793e2 --- /dev/null +++ b/tools/python/get-path @@ -0,0 +1,22 @@ +#! /usr/bin/env bash +set -e + +check () { + set +e + p=`type -p python$v` + r=$? + set -e + if [ $r = 0 ]; then + echo >&2 "${0##*/}: will use #!$p for python programs" + printf "%s\n" "$p" + exit 0 + fi +} + +v="$(python -V 2>&1)" +v="${v#* }" +check +v="${v%.*}" +check +echo >&2 'python version not determined, will use env to find python at runtime' +printf "/usr/bin/env python\n" diff --git a/tools/python/install-wrap b/tools/python/install-wrap new file mode 100755 index 0000000000..29db25d1a7 --- /dev/null +++ b/tools/python/install-wrap @@ -0,0 +1,44 @@ +#! /usr/bin/env bash +# usage: +# .../install-wrap $(PYTHON_PATH) install ... +# where +# PYTHON_PATH is what to put after #! and may be `/usr/bin/env python' +# +# Used via $(INSTALL_PYTHON_PROG) in Rules.mk; PYTHON_PATH comes from +# .../get-path alongside this script + +set -e +if [ $# -lt 2 ]; then echo >&2 "${0##*/}: too few arguments"; exit 1; fi +pythonpath="$1"; shift + +install=("$1"); shift +srcs=() + +while [ $# != 0 ]; do + case "$1" in + -|--) install=("${install[@]}" "$1"); shift; break ;; + -*) install=("${install[@]}" "$1"); shift ;; + *) break ;; + esac +done +while [ $# -gt 1 ]; do + srcs=("${srcs[@]}" "$1"); shift +done +dest="$1"; shift + +destf="$dest" +for srcf in "${srcs[@]}"; do + if test -d "$dest"; then + destf="$dest/${srcf%%*/}"; + fi + org="$(sed -n '2q; /^#! *\/usr\/bin\/env python *$/p' $srcf)" + if [ "x$org" = x ]; then + "${install[@]}" "$srcf" "$destf" + continue + fi + tmpf="$destf.tmp" + "${install[@]}" "$srcf" "$tmpf" + printf >"$tmpf" "#!%s\n" "$pythonpath" + sed -e 1d "$srcf" >>"$tmpf" + mv -f "$tmpf" "$destf" +done