blob: 30c82ac4760de0702c705c8dc465040fa64d4c0f [file] [log] [blame]
Thomas Wouters477c8d52006-05-27 19:21:47 +00001/*
2 * This wrapper program executes a python executable hidden inside an
3 * application bundle inside the Python framework. This is needed to run
4 * GUI code: some GUI API's don't work unless the program is inside an
5 * application bundle.
Ronald Oussoren6f6c5622009-12-24 14:03:19 +00006 *
7 * This program uses posix_spawn rather than plain execv because we need
8 * slightly more control over how the "real" interpreter is executed.
Ronald Oussoren755740f2010-02-07 19:56:39 +00009 *
10 * On OSX 10.4 (and earlier) this falls back to using exec because the
11 * posix_spawnv functions aren't available there.
Thomas Wouters477c8d52006-05-27 19:21:47 +000012 */
Ronald Oussoren755740f2010-02-07 19:56:39 +000013
14#pragma weak_import posix_spawnattr_init
15#pragma weak_import posix_spawnattr_setbinpref_np
16#pragma weak_import posix_spawnattr_setflags
17#pragma weak_import posix_spawn
18
19#include <Python.h>
Thomas Wouters477c8d52006-05-27 19:21:47 +000020#include <unistd.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +000021#ifdef HAVE_SPAWN_H
Ronald Oussoren6f6c5622009-12-24 14:03:19 +000022#include <spawn.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +000023#endif
Ronald Oussoren6f6c5622009-12-24 14:03:19 +000024#include <stdio.h>
25#include <string.h>
26#include <errno.h>
Thomas Wouters477c8d52006-05-27 19:21:47 +000027#include <err.h>
Ronald Oussoren6f6c5622009-12-24 14:03:19 +000028#include <dlfcn.h>
29#include <stdlib.h>
30#include <Python.h>
Thomas Wouters477c8d52006-05-27 19:21:47 +000031
Thomas Wouters477c8d52006-05-27 19:21:47 +000032
Ronald Oussoren6f6c5622009-12-24 14:03:19 +000033extern char** environ;
34
35/*
36 * Locate the python framework by looking for the
37 * library that contains Py_Initialize.
38 *
39 * In a regular framework the structure is:
40 *
41 * Python.framework/Versions/2.7
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 * /Python
43 * /Resources/Python.app/Contents/MacOS/Python
Ronald Oussoren6f6c5622009-12-24 14:03:19 +000044 *
45 * In a virtualenv style structure the expected
46 * structure is:
47 *
48 * ROOT
49 * /bin/pythonw
50 * /.Python <- the dylib
51 * /.Resources/Python.app/Contents/MacOS/Python
52 *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 * NOTE: virtualenv's are not an officially supported
Ronald Oussoren6f6c5622009-12-24 14:03:19 +000054 * feature, support for that structure is provided as
55 * a convenience.
56 */
57static char* get_python_path(void)
58{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 size_t len;
60 Dl_info info;
61 char* end;
62 char* g_path;
Ronald Oussoren6f6c5622009-12-24 14:03:19 +000063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 if (dladdr(Py_Initialize, &info) == 0) {
65 return NULL;
66 }
Ronald Oussoren6f6c5622009-12-24 14:03:19 +000067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 len = strlen(info.dli_fname);
Ronald Oussoren6f6c5622009-12-24 14:03:19 +000069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 g_path = malloc(len+60);
71 if (g_path == NULL) {
72 return NULL;
73 }
Ronald Oussoren6f6c5622009-12-24 14:03:19 +000074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 strcpy(g_path, info.dli_fname);
76 end = g_path + len - 1;
77 while (end != g_path && *end != '/') {
78 end --;
79 }
80 end++;
81 if (*end == '.') {
82 end++;
83 }
84 strcpy(end, "Resources/Python.app/Contents/MacOS/" PYTHONFRAMEWORK);
Ronald Oussoren6f6c5622009-12-24 14:03:19 +000085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 return g_path;
Ronald Oussoren6f6c5622009-12-24 14:03:19 +000087}
88
Ronald Oussoren755740f2010-02-07 19:56:39 +000089#ifdef HAVE_SPAWN_H
Ronald Oussoren6f6c5622009-12-24 14:03:19 +000090static void
91setup_spawnattr(posix_spawnattr_t* spawnattr)
92{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 size_t ocount;
94 size_t count;
95 cpu_type_t cpu_types[1];
96 short flags = 0;
Ronald Oussoren6f6c5622009-12-24 14:03:19 +000097#ifdef __LP64__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 int ch;
Ronald Oussoren6f6c5622009-12-24 14:03:19 +000099#endif
100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 if ((errno = posix_spawnattr_init(spawnattr)) != 0) {
102 err(2, "posix_spawnattr_int");
103 /* NOTREACHTED */
104 }
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 count = 1;
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000107
Ezio Melotti4969f702011-03-15 05:59:46 +0200108 /* Run the real python executable using the same architecture as this
109 * executable, this allows users to control the architecture using
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 * "arch -ppc python"
111 */
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000112
113#if defined(__ppc64__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 cpu_types[0] = CPU_TYPE_POWERPC64;
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000115
116#elif defined(__x86_64__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 cpu_types[0] = CPU_TYPE_X86_64;
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000118
119#elif defined(__ppc__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 cpu_types[0] = CPU_TYPE_POWERPC;
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000121#elif defined(__i386__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 cpu_types[0] = CPU_TYPE_X86;
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000123#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124# error "Unknown CPU"
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000125#endif
126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 if (posix_spawnattr_setbinpref_np(spawnattr, count,
128 cpu_types, &ocount) == -1) {
129 err(1, "posix_spawnattr_setbinpref");
130 /* NOTREACHTED */
131 }
132 if (count != ocount) {
133 fprintf(stderr, "posix_spawnattr_setbinpref failed to copy\n");
134 exit(1);
135 /* NOTREACHTED */
136 }
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000137
138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 /*
140 * Set flag that causes posix_spawn to behave like execv
141 */
142 flags |= POSIX_SPAWN_SETEXEC;
143 if ((errno = posix_spawnattr_setflags(spawnattr, flags)) != 0) {
144 err(1, "posix_spawnattr_setflags");
145 /* NOTREACHTED */
146 }
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000147}
Ronald Oussoren755740f2010-02-07 19:56:39 +0000148#endif
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150int
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000151main(int argc, char **argv) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 char* exec_path = get_python_path();
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000153
Ronald Oussoren82bfcc82010-06-02 03:50:56 +0000154 /*
155 * Let argv[0] refer to the new interpreter. This is needed to
156 * get the effect we want on OSX 10.5 or earlier. That is, without
157 * changing argv[0] the real interpreter won't have access to
158 * the Window Server.
159 */
160 argv[0] = exec_path;
161
Ronald Oussoren755740f2010-02-07 19:56:39 +0000162#ifdef HAVE_SPAWN_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 /* We're weak-linking to posix-spawnv to ensure that
164 * an executable build on 10.5 can work on 10.4.
165 */
166 if (posix_spawn != NULL) {
167 posix_spawnattr_t spawnattr = NULL;
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 setup_spawnattr(&spawnattr);
170 posix_spawn(NULL, exec_path, NULL,
171 &spawnattr, argv, environ);
172 err(1, "posix_spawn: %s", exec_path);
173 }
Ronald Oussoren755740f2010-02-07 19:56:39 +0000174#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 execve(exec_path, argv, environ);
176 err(1, "execve: %s", argv[0]);
177 /* NOTREACHED */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000178}