ia64/xen-unstable

changeset 17830:b8fa4a11f715

Have `make install' write the Python version number in the #!

The Xen tools contain a number of Python extensions written in C. The
C API to Python, used by these extensions, is not stable from one
version of Python to the next.

Our build system uses whatever version of Python is the default on the
build system at that time to build these extensions. However, the
actual scripts such as `xm' use #!/usr/bin/env python which uses
whichever version of Python is the default on the deployment system at
the time of invocation.

If for any reason these two versions of Python are not the same (eg,
because the system is built on one computer and executed on another,
or because the system's gains a more recent Python installation
alongside the original which changes the previous default), warnings
and failures occur.

In this patch I arrange for Python scripts to be installed via a
special wrapper around `install', which determines the build-time
Python version and path and then writes that into the #! line at the
point of `make install' (or `make dist'). (It can also be overridden
by setting PYTHON_PATH.)

Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Wed Jun 11 09:36:23 2008 +0100 (2008-06-11)
parents cc4e471bbc08
children f292689f48ba
files tools/Rules.mk tools/misc/Makefile tools/python/get-path tools/python/install-wrap
line diff
     1.1 --- a/tools/Rules.mk	Tue Jun 10 16:59:24 2008 +0100
     1.2 +++ b/tools/Rules.mk	Wed Jun 11 09:36:23 2008 +0100
     1.3 @@ -35,6 +35,11 @@ LDFLAGS += $(shell getconf LFS_LDFLAGS)
     1.4  CFLAGS-$(CONFIG_X86_32) += $(call cc-option,$(CC),-mno-tls-direct-seg-refs)
     1.5  CFLAGS += $(CFLAGS-y)
     1.6  
     1.7 +DEFAULT_PYTHON_PATH := $(shell $(XEN_ROOT)/tools/python/get-path)
     1.8 +PYTHON_PATH ?= $(DEFAULT_PYTHON_PATH)
     1.9 +INSTALL_PYTHON_PROG = $(XEN_ROOT)/tools/python/install-wrap \
    1.10 +"$(PYTHON_PATH)" $(INSTALL_PROG)
    1.11 +
    1.12  # Require GCC v3.4+ (to avoid issues with alignment constraints in Xen headers)
    1.13  check-$(CONFIG_X86) = $(call cc-ver-check,CC,0x030400,\
    1.14                          "Xen requires at least gcc-3.4")
     2.1 --- a/tools/misc/Makefile	Tue Jun 10 16:59:24 2008 +0100
     2.2 +++ b/tools/misc/Makefile	Wed Jun 11 09:36:23 2008 +0100
     2.3 @@ -32,8 +32,8 @@ build: $(TARGETS)
     2.4  install: build
     2.5  	$(INSTALL_DIR) $(DESTDIR)$(BINDIR)
     2.6  	$(INSTALL_DIR) $(DESTDIR)$(SBINDIR)
     2.7 -	$(INSTALL_PROG) $(INSTALL_BIN) $(DESTDIR)$(BINDIR)
     2.8 -	$(INSTALL_PROG) $(INSTALL_SBIN) $(DESTDIR)$(SBINDIR)
     2.9 +	$(INSTALL_PYTHON_PROG) $(INSTALL_BIN) $(DESTDIR)$(BINDIR)
    2.10 +	$(INSTALL_PYTHON_PROG) $(INSTALL_SBIN) $(DESTDIR)$(SBINDIR)
    2.11  	set -e; for d in $(SUBDIRS); do $(MAKE) -C $$d install-recurse; done
    2.12  
    2.13  .PHONY: clean
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/tools/python/get-path	Wed Jun 11 09:36:23 2008 +0100
     3.3 @@ -0,0 +1,22 @@
     3.4 +#! /usr/bin/env bash
     3.5 +set -e
     3.6 +
     3.7 +check () {
     3.8 +	set +e
     3.9 +	p=`type -p python$v`
    3.10 +	r=$?
    3.11 +	set -e
    3.12 +	if [ $r = 0 ]; then
    3.13 +		echo >&2 "${0##*/}: will use #!$p for python programs"
    3.14 +		printf "%s\n" "$p"
    3.15 +		exit 0
    3.16 +	fi
    3.17 +}
    3.18 +
    3.19 +v="$(python -V 2>&1)"
    3.20 +v="${v#* }"
    3.21 +check
    3.22 +v="${v%.*}"
    3.23 +check
    3.24 +echo >&2 'python version not determined, will use env to find python at runtime'
    3.25 +printf "/usr/bin/env python\n"
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/tools/python/install-wrap	Wed Jun 11 09:36:23 2008 +0100
     4.3 @@ -0,0 +1,44 @@
     4.4 +#! /usr/bin/env bash
     4.5 +# usage:
     4.6 +#  .../install-wrap $(PYTHON_PATH) install <options-to-install> <src>... <dest>
     4.7 +# where
     4.8 +#  PYTHON_PATH is what to put after #! and may be `/usr/bin/env python'
     4.9 +#
    4.10 +# Used via $(INSTALL_PYTHON_PROG) in Rules.mk; PYTHON_PATH comes from
    4.11 +# .../get-path alongside this script
    4.12 +
    4.13 +set -e
    4.14 +if [ $# -lt 2 ]; then echo >&2 "${0##*/}: too few arguments"; exit 1; fi
    4.15 +pythonpath="$1"; shift
    4.16 +
    4.17 +install=("$1"); shift
    4.18 +srcs=()
    4.19 +
    4.20 +while [ $# != 0 ]; do
    4.21 +	case "$1" in
    4.22 +	-|--)	install=("${install[@]}" "$1"); shift; break ;;
    4.23 +	-*)	install=("${install[@]}" "$1"); shift ;;
    4.24 +	*)	break ;;
    4.25 +	esac
    4.26 +done
    4.27 +while [ $# -gt 1 ]; do
    4.28 +	srcs=("${srcs[@]}" "$1"); shift
    4.29 +done
    4.30 +dest="$1"; shift
    4.31 +
    4.32 +destf="$dest"
    4.33 +for srcf in "${srcs[@]}"; do
    4.34 +	if test -d "$dest"; then
    4.35 +		destf="$dest/${srcf%%*/}";
    4.36 +	fi
    4.37 +	org="$(sed -n '2q; /^#! *\/usr\/bin\/env python *$/p' $srcf)"
    4.38 +	if [ "x$org" = x ]; then
    4.39 +		"${install[@]}" "$srcf" "$destf"
    4.40 +		continue
    4.41 +	fi
    4.42 +	tmpf="$destf.tmp"
    4.43 +	"${install[@]}" "$srcf" "$tmpf"
    4.44 +	printf >"$tmpf" "#!%s\n" "$pythonpath"
    4.45 +	sed -e 1d "$srcf" >>"$tmpf"
    4.46 +	mv -f "$tmpf" "$destf"
    4.47 +done