blob: 5fc59b574a8cc97494c8bdb6250ca5e11b907b44 [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
42 * /Python
43 * /Resources/Python.app/Contents/MacOS/Python
44 *
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 *
53 * NOTE: virtualenv's are not an officially supported
54 * feature, support for that structure is provided as
55 * a convenience.
56 */
57static char* get_python_path(void)
58{
59 size_t len;
60 Dl_info info;
61 char* end;
62 char* g_path;
63
64 if (dladdr(Py_Initialize, &info) == 0) {
65 return NULL;
66 }
67
68 len = strlen(info.dli_fname);
69
70 g_path = malloc(len+60);
71 if (g_path == NULL) {
72 return NULL;
73 }
74
75 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[1] == '.') {
82 end++;
83 }
Ronald Oussorenb366cda2010-03-07 09:14:06 +000084 strcpy(end, "Resources/Python.app/Contents/MacOS/" PYTHONFRAMEWORK);
Ronald Oussoren6f6c5622009-12-24 14:03:19 +000085
86 return g_path;
87}
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{
93 size_t ocount;
94 size_t count;
95 cpu_type_t cpu_types[1];
96 short flags = 0;
97#ifdef __LP64__
98 int ch;
99#endif
100
101 if ((errno = posix_spawnattr_init(spawnattr)) != 0) {
102 err(2, "posix_spawnattr_int");
103 /* NOTREACHTED */
104 }
105
106 count = 1;
107
108 /* Run the real python executable using the same architure as this
109 * executable, this allows users to controle the architecture using
110 * "arch -ppc python"
111 */
112
113#if defined(__ppc64__)
114 cpu_types[0] = CPU_TYPE_POWERPC64;
115
116#elif defined(__x86_64__)
117 cpu_types[0] = CPU_TYPE_X86_64;
118
119#elif defined(__ppc__)
120 cpu_types[0] = CPU_TYPE_POWERPC;
121#elif defined(__i386__)
122 cpu_types[0] = CPU_TYPE_X86;
123#else
124# error "Unknown CPU"
125#endif
126
127 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 }
137
138
139 /*
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 }
147}
Ronald Oussoren755740f2010-02-07 19:56:39 +0000148#endif
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000149
150int
151main(int argc, char **argv) {
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000152 char* exec_path = get_python_path();
153
Ronald Oussoren755740f2010-02-07 19:56:39 +0000154#ifdef HAVE_SPAWN_H
155 /* We're weak-linking to posix-spawnv to ensure that
156 * an executable build on 10.5 can work on 10.4.
157 */
158 if (posix_spawn != NULL) {
159 posix_spawnattr_t spawnattr = NULL;
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000160
Ronald Oussoren755740f2010-02-07 19:56:39 +0000161 setup_spawnattr(&spawnattr);
162 posix_spawn(NULL, exec_path, NULL,
163 &spawnattr, argv, environ);
164 err(1, "posix_spawn: %s", argv[0]);
165 }
166#endif
167 execve(exec_path, argv, environ);
168 err(1, "execve: %s", argv[0]);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000169 /* NOTREACHED */
170}