Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1 | /* |
| 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 Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 6 | * |
| 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 Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 9 | * |
| 10 | * On OSX 10.4 (and earlier) this falls back to using exec because the |
| 11 | * posix_spawnv functions aren't available there. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12 | */ |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 13 | |
| 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 Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 20 | #include <unistd.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 21 | #ifdef HAVE_SPAWN_H |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 22 | #include <spawn.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 23 | #endif |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <string.h> |
| 26 | #include <errno.h> |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 27 | #include <err.h> |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 28 | #include <dlfcn.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <Python.h> |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 31 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 32 | |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 33 | extern 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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 42 | * /Python |
| 43 | * /Resources/Python.app/Contents/MacOS/Python |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 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 | * |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 53 | * NOTE: virtualenv's are not an officially supported |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 54 | * feature, support for that structure is provided as |
| 55 | * a convenience. |
| 56 | */ |
| 57 | static char* get_python_path(void) |
| 58 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 59 | size_t len; |
| 60 | Dl_info info; |
| 61 | char* end; |
| 62 | char* g_path; |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 63 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 64 | if (dladdr(Py_Initialize, &info) == 0) { |
| 65 | return NULL; |
| 66 | } |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 67 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 68 | len = strlen(info.dli_fname); |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 69 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 70 | g_path = malloc(len+60); |
| 71 | if (g_path == NULL) { |
| 72 | return NULL; |
| 73 | } |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 74 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 == '.') { |
| 82 | end++; |
| 83 | } |
| 84 | strcpy(end, "Resources/Python.app/Contents/MacOS/" PYTHONFRAMEWORK); |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 85 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 86 | return g_path; |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 89 | #ifdef HAVE_SPAWN_H |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 90 | static void |
| 91 | setup_spawnattr(posix_spawnattr_t* spawnattr) |
| 92 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 93 | size_t ocount; |
| 94 | size_t count; |
| 95 | cpu_type_t cpu_types[1]; |
| 96 | short flags = 0; |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 97 | #ifdef __LP64__ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 98 | int ch; |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 99 | #endif |
| 100 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 101 | if ((errno = posix_spawnattr_init(spawnattr)) != 0) { |
| 102 | err(2, "posix_spawnattr_int"); |
| 103 | /* NOTREACHTED */ |
| 104 | } |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 105 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 106 | count = 1; |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 107 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 | */ |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 112 | |
| 113 | #if defined(__ppc64__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 114 | cpu_types[0] = CPU_TYPE_POWERPC64; |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 115 | |
| 116 | #elif defined(__x86_64__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 117 | cpu_types[0] = CPU_TYPE_X86_64; |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 118 | |
| 119 | #elif defined(__ppc__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 120 | cpu_types[0] = CPU_TYPE_POWERPC; |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 121 | #elif defined(__i386__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 122 | cpu_types[0] = CPU_TYPE_X86; |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 123 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 124 | # error "Unknown CPU" |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 125 | #endif |
| 126 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 | } |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 137 | |
| 138 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 | } |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 147 | } |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 148 | #endif |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 149 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 150 | int |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 151 | main(int argc, char **argv) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 152 | char* exec_path = get_python_path(); |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 153 | |
Ronald Oussoren | 82bfcc8 | 2010-06-02 03:50:56 +0000 | [diff] [blame^] | 154 | /* |
| 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 Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 162 | #ifdef HAVE_SPAWN_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 163 | /* 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 Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 168 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 169 | setup_spawnattr(&spawnattr); |
| 170 | posix_spawn(NULL, exec_path, NULL, |
| 171 | &spawnattr, argv, environ); |
| 172 | err(1, "posix_spawn: %s", exec_path); |
| 173 | } |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 174 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 175 | execve(exec_path, argv, environ); |
| 176 | err(1, "execve: %s", argv[0]); |
| 177 | /* NOTREACHED */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 178 | } |