summaryrefslogtreecommitdiff
path: root/Mac
diff options
context:
space:
mode:
authorRonald Oussoren <ronaldoussoren@mac.com>2009-12-24 14:03:19 +0000
committerRonald Oussoren <ronaldoussoren@mac.com>2009-12-24 14:03:19 +0000
commit55e3a95ae9fa26f11522523583838e781d91c128 (patch)
tree984718278b3b4f2805573c49beac29f422353141 /Mac
parentc52814539299178565113733ebd6f502c02a6472 (diff)
downloadcpython-55e3a95ae9fa26f11522523583838e781d91c128.tar.gz
Merged revisions 77031 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r77031 | ronald.oussoren | 2009-12-24 14:30:58 +0100 (Thu, 24 Dec 2009) | 15 lines Issue #6834: replace the implementation for the 'python' and 'pythonw' executables on OSX. The previous implementation used execv(2) to run the real interpreter, which means that you cannot use the arch(1) tool to select the architecture you want to use for a universal build because that only affects the python/pythonw wrapper and not the actual interpreter. The new version uses posix_spawnv with a number of OSX-specific options that ensure that the real interpreter is started using the same CPU architecture as the wrapper, and that means that 'arch -ppc python' now actually works. I've also changed the way that the wrapper looks for the framework: it is now linked to the framework rather than hardcoding the framework path. This should make it easier to provide pythonw support in tools like virtualenv. ........
Diffstat (limited to 'Mac')
-rw-r--r--Mac/Makefile.in65
-rw-r--r--Mac/README70
-rw-r--r--Mac/Tools/pythonw.c140
3 files changed, 213 insertions, 62 deletions
diff --git a/Mac/Makefile.in b/Mac/Makefile.in
index 15531d865f..b1d4456136 100644
--- a/Mac/Makefile.in
+++ b/Mac/Makefile.in
@@ -15,6 +15,7 @@ LDFLAGS=@LDFLAGS@
FRAMEWORKUNIXTOOLSPREFIX=@FRAMEWORKUNIXTOOLSPREFIX@
PYTHONFRAMEWORK=@PYTHONFRAMEWORK@
PYTHONFRAMEWORKIDENTIFIER=@PYTHONFRAMEWORKIDENTIFIER@
+LIPO_32BIT_FLAGS=@LIPO_32BIT_FLAGS@
# These are normally glimpsed from the previous set
@@ -41,11 +42,8 @@ APPTEMPLATE=$(srcdir)/Resources/app
APPSUBDIRS=MacOS Resources
compileall=$(srcdir)/../Lib/compileall.py
-installapps: install_Python install_PythonLauncher install_IDLE \
- checkapplepython install_pythonw install_versionedtools
-
-installapps4way: install_Python4way install_PythonLauncher \
- install_IDLE4way install_pythonw4way install_versionedtools
+installapps: install_Python install_pythonw install_PythonLauncher install_IDLE \
+ checkapplepython install_versionedtools
install_pythonw: pythonw
@@ -53,33 +51,12 @@ install_pythonw: pythonw
$(INSTALL_PROGRAM) $(STRIPFLAG) pythonw "$(DESTDIR)$(prefix)/bin/python$(VERSION)"
ln -sf python$(VERSION) "$(DESTDIR)$(prefix)/bin/python3"
ln -sf pythonw$(VERSION) "$(DESTDIR)$(prefix)/bin/pythonw3"
-
-
-# Install 3 variants of python/pythonw:
-# - 32-bit (i386 and ppc)
-# - 64-bit (x86_64 and ppc64)
-# - all (all four architectures)
-# - Make 'python' and 'pythonw' aliases for the 32-bit variant
-install_pythonw4way: pythonw-32 pythonw-64 pythonw
- $(INSTALL_PROGRAM) $(STRIPFLAG) pythonw-64 "$(DESTDIR)$(prefix)/bin/pythonw$(VERSION)-64"
- $(INSTALL_PROGRAM) $(STRIPFLAG) pythonw-64 "$(DESTDIR)$(prefix)/bin/python$(VERSION)-64"
- ln -sf python$(VERSION)-64 "$(DESTDIR)$(prefix)/bin/python3-64"
- ln -sf pythonw$(VERSION)-64 "$(DESTDIR)$(prefix)/bin/pythonw3-64"
-
- $(INSTALL_PROGRAM) $(STRIPFLAG) pythonw-32 "$(DESTDIR)$(prefix)/bin/pythonw$(VERSION)-32"
- $(INSTALL_PROGRAM) $(STRIPFLAG) pythonw-32 "$(DESTDIR)$(prefix)/bin/python$(VERSION)-32"
- ln -sf python$(VERSION)-32 "$(DESTDIR)$(prefix)/bin/python3-32"
- ln -sf pythonw$(VERSION)-32 "$(DESTDIR)$(prefix)/bin/pythonw3-32"
-
- $(INSTALL_PROGRAM) $(STRIPFLAG) pythonw "$(DESTDIR)$(prefix)/bin/pythonw$(VERSION)-all"
- $(INSTALL_PROGRAM) $(STRIPFLAG) pythonw "$(DESTDIR)$(prefix)/bin/python$(VERSION)-all"
- ln -sf python$(VERSION)-all "$(DESTDIR)$(prefix)/bin/python3-all"
- ln -sf pythonw$(VERSION)-all "$(DESTDIR)$(prefix)/bin/pythonw3-all"
-
+ifneq ($(LIPO_32BIT_FLAGS),)
+ lipo $(LIPO_32BIT_FLAGS) -output $(DESTDIR)$(prefix)/bin/python$(VERSION)-32 pythonw
+ lipo $(LIPO_32BIT_FLAGS) -output $(DESTDIR)$(prefix)/bin/pythonw$(VERSION)-32 pythonw
ln -sf pythonw$(VERSION)-32 "$(DESTDIR)$(prefix)/bin/pythonw$(VERSION)"
ln -sf python$(VERSION)-32 "$(DESTDIR)$(prefix)/bin/python$(VERSION)"
- ln -sf pythonw$(VERSION)-32 "$(DESTDIR)$(prefix)/bin/pythonw3"
- ln -sf python$(VERSION)-32 "$(DESTDIR)$(prefix)/bin/python3"
+endif
#
# Install unix tools in /usr/local/bin. These are just aliases for the
@@ -98,10 +75,6 @@ installunixtools:
done
-# TODO: install symlinks for -32, -64 and -all as well
-installunixtools4way: installunixtools
-
-
#
# Like installunixtools, but only install links to the versioned binaries.
#
@@ -115,9 +88,6 @@ altinstallunixtools:
ln -fs "$(prefix)/bin/$${fn}" "$(DESTDIR)$(FRAMEWORKUNIXTOOLSPREFIX)/bin/$${fn}" ;\
done
-# TODO: -32, -64 and -all variants
-altinstallunixtools4way: altinstallunixtools
-
# By default most tools are installed without a version in their basename, to
# make it easier to install (and use) several python versions side-by-side move
# the tools to a version-specific name and add the non-versioned name as an
@@ -140,16 +110,7 @@ install_versionedtools:
pythonw: $(srcdir)/Tools/pythonw.c Makefile
- $(CC) $(LDFLAGS) -o $@ $(srcdir)/Tools/pythonw.c \
- -DPYTHONWEXECUTABLE='"$(APPINSTALLDIR)/Contents/MacOS/$(PYTHONFRAMEWORK)"'
-
-pythonw-32: $(srcdir)/Tools/pythonw.c Makefile
- $(CC) $(LDFLAGS) -o $@ -arch i386 -arch ppc $(srcdir)/Tools/pythonw.c \
- -DPYTHONWEXECUTABLE='"$(APPINSTALLDIR)/Contents/MacOS/$(PYTHONFRAMEWORK)-32"'
-
-pythonw-64: $(srcdir)/Tools/pythonw.c Makefile
- $(CC) $(LDFLAGS) -o $@ -arch x86_64 -arch ppc64 $(srcdir)/Tools/pythonw.c \
- -DPYTHONWEXECUTABLE='"$(APPINSTALLDIR)/Contents/MacOS/$(PYTHONFRAMEWORK)-64"'
+ $(CC) $(LDFLAGS) -o $@ $(srcdir)/Tools/pythonw.c -I.. -I$(srcdir)/../Include ../$(PYTHONFRAMEWORK).framework/Versions/$(VERSION)/$(PYTHONFRAMEWORK)
install_PythonLauncher:
cd PythonLauncher && make install DESTDIR=$(DESTDIR)
@@ -205,11 +166,6 @@ install_Python:
> "$(DESTDIR)$(APPINSTALLDIR)/Contents/Info.plist"
rm "$(DESTDIR)$(APPINSTALLDIR)/Contents/Info.plist.in"
-install_Python4way: install_Python
- lipo -extract i386 -extract ppc7400 -output "$(DESTDIR)$(APPINSTALLDIR)/Contents/MacOS/$(PYTHONFRAMEWORK)-32" "$(DESTDIR)$(APPINSTALLDIR)/Contents/MacOS/$(PYTHONFRAMEWORK)"
- lipo -extract x86_64 -extract ppc64 -output "$(DESTDIR)$(APPINSTALLDIR)/Contents/MacOS/$(PYTHONFRAMEWORK)-64" "$(DESTDIR)$(APPINSTALLDIR)/Contents/MacOS/$(PYTHONFRAMEWORK)"
-
-
install_IDLE:
test -d "$(DESTDIR)$(PYTHONAPPSDIR)" || mkdir -p "$(DESTDIR)$(PYTHONAPPSDIR)"
@@ -237,11 +193,6 @@ install_IDLE:
fi
touch "$(DESTDIR)$(PYTHONAPPSDIR)/IDLE.app"
-install_IDLE4way: install_IDLE
- ln -sf "$(INSTALLED_PYTHONAPP)-32" "$(DESTDIR)$(PYTHONAPPSDIR)/IDLE.app/Contents/MacOS/Python"
- sed -e "s!%prefix%!$(prefix)!g" -e 's!%exe%!$(PYTHONFRAMEWORK)-32!g' < "$(srcdir)/IDLE/IDLE.app/Contents/MacOS/IDLE" > "$(DESTDIR)$(PYTHONAPPSDIR)/IDLE.app/Contents/MacOS/IDLE"
- touch "$(DESTDIR)$(PYTHONAPPSDIR)/IDLE.app"
-
$(INSTALLED_PYTHONAPP): install_Python
installextras: $(srcdir)/Extras.ReadMe.txt $(srcdir)/Extras.install.py
diff --git a/Mac/README b/Mac/README
index f0f41e9554..afd217315d 100644
--- a/Mac/README
+++ b/Mac/README
@@ -5,6 +5,35 @@ MacOSX Notes
This document provides a quick overview of some Mac OS X specific features in
the Python distribution.
+* ``--enable-framework``
+
+ If this argument is specified the build will create a Python.framework rather
+ than a traditional Unix install. See the section
+ _`Building and using a framework-based Python on Mac OS X` for more
+ information on frameworks.
+
+* ``--with-framework-name=NAME``
+
+ Specify the name for the python framework, defaults to ``Python``. This option
+ is only valid when ``--enable-framework`` is specified.
+
+* ``--enable-universalsdk[=PATH]``
+
+ Create a universal binary build of of Python. This can be used with both
+ regular and framework builds.
+
+ The optional argument specifies which OSX SDK should be used to perform the
+ build. This defaults to ``/Developer/SDKs/MacOSX.10.4u.sdk``, specify
+ ``/`` when building on a 10.5 system, especially when building 64-bit code.
+
+ See the section _`Building and using a universal binary of Python on Mac OS X`
+ for more information.
+
+* ``--with-univeral-archs=VALUE``
+
+ Specify the kind of universal binary that should be created. This option is
+ only valid when ``--enable-universalsdk`` is specified.
+
Building and using a universal binary of Python on Mac OS X
===========================================================
@@ -31,6 +60,47 @@ unix build. Either way you will have to build python on Mac OS X 10.4 (or later)
with Xcode 2.1 (or later). You also have to install the 10.4u SDK when
installing Xcode.
+2.1 Flavours of universal binaries
+..................................
+
+It is possible to build a number of flavours of the universal binary build,
+the default is a 32-bit only binary (i386 and ppc). The flavour can be
+specified using the option ``--with-universal-archs=VALUE``. The following
+values are available:
+
+ * ``32-bit``: ``ppc``, ``i386``
+
+ * ``64-bit``: ``ppc64``, ``x86_64``
+
+ * ``all``: ``ppc``, ``ppc64``, ``i386``, ``x86_64``
+
+ * ``3-way``: ``ppc``, ``i386`` and ``x86_64``
+
+ * ``intel``: ``i386``, ``x86_64``
+
+To build a universal binary that includes a 64-bit architecture, you must build
+on a system running OSX 10.5 or later. The ``all`` flavour can only be built on
+OSX 10.5.
+
+The makefile for a framework build will install ``python32`` and ``pythonw32``
+binaries when the universal architecures includes at least one 32-bit architecture
+(that is, for all flavours but ``64-bit``).
+
+Running a specific archicture
+.............................
+
+You can run code using a specific architecture using the ``arch`` command::
+
+ $ arch -i386 python
+
+Or to explicitly run in 32-bit mode, regardless of the machine hardware::
+
+ $ arch -i386 -ppc python
+
+NOTE: When you're using a framework install of Python this requires at least
+Python 2.7 or 3.2, in earlier versions the python (and pythonw) commands are
+wrapper tools that execute the real interpreter without ensuring that the
+real interpreter runs with the same architecture.
Building and using a framework-based Python on Mac OS X.
========================================================
diff --git a/Mac/Tools/pythonw.c b/Mac/Tools/pythonw.c
index e70a76f846..d7a86f2643 100644
--- a/Mac/Tools/pythonw.c
+++ b/Mac/Tools/pythonw.c
@@ -3,15 +3,145 @@
* application bundle inside the Python framework. This is needed to run
* GUI code: some GUI API's don't work unless the program is inside an
* application bundle.
+ *
+ * This program uses posix_spawn rather than plain execv because we need
+ * slightly more control over how the "real" interpreter is executed.
*/
#include <unistd.h>
+#include <spawn.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
#include <err.h>
+#include <dlfcn.h>
+#include <stdlib.h>
+#include <Python.h>
-static char Python[] = PYTHONWEXECUTABLE;
-int main(int argc, char **argv) {
- argv[0] = Python;
- execv(Python, argv);
- err(1, "execv: %s", Python);
+extern char** environ;
+
+/*
+ * Locate the python framework by looking for the
+ * library that contains Py_Initialize.
+ *
+ * In a regular framework the structure is:
+ *
+ * Python.framework/Versions/2.7
+ * /Python
+ * /Resources/Python.app/Contents/MacOS/Python
+ *
+ * In a virtualenv style structure the expected
+ * structure is:
+ *
+ * ROOT
+ * /bin/pythonw
+ * /.Python <- the dylib
+ * /.Resources/Python.app/Contents/MacOS/Python
+ *
+ * NOTE: virtualenv's are not an officially supported
+ * feature, support for that structure is provided as
+ * a convenience.
+ */
+static char* get_python_path(void)
+{
+ size_t len;
+ Dl_info info;
+ char* end;
+ char* g_path;
+
+ if (dladdr(Py_Initialize, &info) == 0) {
+ return NULL;
+ }
+
+ len = strlen(info.dli_fname);
+
+ g_path = malloc(len+60);
+ if (g_path == NULL) {
+ return NULL;
+ }
+
+ strcpy(g_path, info.dli_fname);
+ end = g_path + len - 1;
+ while (end != g_path && *end != '/') {
+ end --;
+ }
+ end++;
+ if (end[1] == '.') {
+ end++;
+ }
+ strcpy(end, "Resources/Python.app/Contents/MacOS/Python");
+
+ return g_path;
+}
+
+static void
+setup_spawnattr(posix_spawnattr_t* spawnattr)
+{
+ size_t ocount;
+ size_t count;
+ cpu_type_t cpu_types[1];
+ short flags = 0;
+#ifdef __LP64__
+ int ch;
+#endif
+
+ if ((errno = posix_spawnattr_init(spawnattr)) != 0) {
+ err(2, "posix_spawnattr_int");
+ /* NOTREACHTED */
+ }
+
+ count = 1;
+
+ /* Run the real python executable using the same architure as this
+ * executable, this allows users to controle the architecture using
+ * "arch -ppc python"
+ */
+
+#if defined(__ppc64__)
+ cpu_types[0] = CPU_TYPE_POWERPC64;
+
+#elif defined(__x86_64__)
+ cpu_types[0] = CPU_TYPE_X86_64;
+
+#elif defined(__ppc__)
+ cpu_types[0] = CPU_TYPE_POWERPC;
+#elif defined(__i386__)
+ cpu_types[0] = CPU_TYPE_X86;
+#else
+# error "Unknown CPU"
+#endif
+
+ if (posix_spawnattr_setbinpref_np(spawnattr, count,
+ cpu_types, &ocount) == -1) {
+ err(1, "posix_spawnattr_setbinpref");
+ /* NOTREACHTED */
+ }
+ if (count != ocount) {
+ fprintf(stderr, "posix_spawnattr_setbinpref failed to copy\n");
+ exit(1);
+ /* NOTREACHTED */
+ }
+
+
+ /*
+ * Set flag that causes posix_spawn to behave like execv
+ */
+ flags |= POSIX_SPAWN_SETEXEC;
+ if ((errno = posix_spawnattr_setflags(spawnattr, flags)) != 0) {
+ err(1, "posix_spawnattr_setflags");
+ /* NOTREACHTED */
+ }
+}
+
+int
+main(int argc, char **argv) {
+ posix_spawnattr_t spawnattr = NULL;
+ char* exec_path = get_python_path();
+
+
+ setup_spawnattr(&spawnattr);
+ posix_spawn(NULL, exec_path, NULL,
+ &spawnattr, argv, environ);
+ err(1, "posix_spawn: %s", argv[0]);
/* NOTREACHED */
}