/*1* This wrapper program executes a python executable hidden inside an2* application bundle inside the Python framework. This is needed to run3* GUI code: some GUI API's don't work unless the program is inside an4* application bundle.5*6* This program uses posix_spawn rather than plain execv because we need7* slightly more control over how the "real" interpreter is executed.8*9* On OSX 10.4 (and earlier) this falls back to using exec because the10* posix_spawnv functions aren't available there.11*/1213#pragma weak_import posix_spawnattr_init14#pragma weak_import posix_spawnattr_setbinpref_np15#pragma weak_import posix_spawnattr_setflags16#pragma weak_import posix_spawn1718#include <Python.h>19#include <unistd.h>20#ifdef HAVE_SPAWN_H21#include <spawn.h>22#endif23#include <stdio.h>24#include <string.h>25#include <errno.h>26#include <err.h>27#include <dlfcn.h>28#include <stdlib.h>29#include <mach-o/dyld.h>303132extern char** environ;3334/*35* Locate the python framework by looking for the36* library that contains Py_Initialize.37*38* In a regular framework the structure is:39*40* Python.framework/Versions/2.741* /Python42* /Resources/Python.app/Contents/MacOS/Python43*44* In a virtualenv style structure the expected45* structure is:46*47* ROOT48* /bin/pythonw49* /.Python <- the dylib50* /.Resources/Python.app/Contents/MacOS/Python51*52* NOTE: virtualenv's are not an officially supported53* feature, support for that structure is provided as54* a convenience.55*/56static char* get_python_path(void)57{58size_t len;59Dl_info info;60char* end;61char* g_path;6263if (dladdr(Py_Initialize, &info) == 0) {64return NULL;65}6667len = strlen(info.dli_fname);6869g_path = malloc(len+60);70if (g_path == NULL) {71return NULL;72}7374strcpy(g_path, info.dli_fname);75end = g_path + len - 1;76while (end != g_path && *end != '/') {77end --;78}79end++;80if (*end == '.') {81end++;82}83strcpy(end, "Resources/Python.app/Contents/MacOS/" PYTHONFRAMEWORK);8485return g_path;86}8788#ifdef HAVE_SPAWN_H89static void90setup_spawnattr(posix_spawnattr_t* spawnattr)91{92size_t ocount;93size_t count;94cpu_type_t cpu_types[1];95short flags = 0;9697if ((errno = posix_spawnattr_init(spawnattr)) != 0) {98err(2, "posix_spawnattr_int");99/* NOTREACHTED */100}101102count = 1;103104/* Run the real python executable using the same architecture as this105* executable, this allows users to control the architecture using106* "arch -ppc python"107*/108109#if defined(__ppc64__)110cpu_types[0] = CPU_TYPE_POWERPC64;111112#elif defined(__x86_64__)113cpu_types[0] = CPU_TYPE_X86_64;114115#elif defined(__ppc__)116cpu_types[0] = CPU_TYPE_POWERPC;117118#elif defined(__i386__)119cpu_types[0] = CPU_TYPE_X86;120121#elif defined(__arm64__)122cpu_types[0] = CPU_TYPE_ARM64;123124#else125# error "Unknown CPU"126127#endif128129if (posix_spawnattr_setbinpref_np(spawnattr, count,130cpu_types, &ocount) == -1) {131err(1, "posix_spawnattr_setbinpref");132/* NOTREACHTED */133}134if (count != ocount) {135fprintf(stderr, "posix_spawnattr_setbinpref failed to copy\n");136exit(1);137/* NOTREACHTED */138}139140141/*142* Set flag that causes posix_spawn to behave like execv143*/144flags |= POSIX_SPAWN_SETEXEC;145if ((errno = posix_spawnattr_setflags(spawnattr, flags)) != 0) {146err(1, "posix_spawnattr_setflags");147/* NOTREACHTED */148}149}150#endif151152int153main(int argc, char **argv) {154char* exec_path = get_python_path();155static char path[PATH_MAX * 2];156static char real_path[PATH_MAX * 2];157int status;158uint32_t size = PATH_MAX * 2;159160/* Set the original executable path in the environment. */161status = _NSGetExecutablePath(path, &size);162if (status == 0) {163/*164* Note: don't call 'realpath', that will165* erase symlink information, and that166* breaks "pyvenv --symlink"167*168* It is nice to have the directory name169* as a cleaned up absolute path though,170* therefore call realpath on dirname(path)171*/172char* slash = strrchr(path, '/');173if (slash) {174char replaced;175replaced = slash[1];176slash[1] = 0;177if (realpath(path, real_path) == NULL) {178err(1, "realpath: %s", path);179}180slash[1] = replaced;181if (strlcat(real_path, slash, sizeof(real_path)) > sizeof(real_path)) {182errno = EINVAL;183err(1, "realpath: %s", path);184}185186} else {187if (realpath(".", real_path) == NULL) {188err(1, "realpath: %s", path);189}190if (strlcat(real_path, "/", sizeof(real_path)) > sizeof(real_path)) {191errno = EINVAL;192err(1, "realpath: %s", path);193}194if (strlcat(real_path, path, sizeof(real_path)) > sizeof(real_path)) {195errno = EINVAL;196err(1, "realpath: %s", path);197}198}199200/*201* The environment variable is used to pass the value of real_path202* to the actual python interpreter, and is read by code in203* Python/coreconfig.c.204*205* This way the real interpreter knows how the user invoked the206* interpreter and can behave as if this launcher is the real207* interpreter (looking for pyvenv configuration, ...)208*/209setenv("__PYVENV_LAUNCHER__", real_path, 1);210}211212/*213* Let argv[0] refer to the new interpreter. This is needed to214* get the effect we want on OSX 10.5 or earlier. That is, without215* changing argv[0] the real interpreter won't have access to216* the Window Server.217*/218argv[0] = exec_path;219220#ifdef HAVE_SPAWN_H221/* We're weak-linking to posix-spawnv to ensure that222* an executable build on 10.5 can work on 10.4.223*/224225if (&posix_spawn != NULL) {226posix_spawnattr_t spawnattr = NULL;227228setup_spawnattr(&spawnattr);229posix_spawn(NULL, exec_path, NULL,230&spawnattr, argv, environ);231err(1, "posix_spawn: %s", exec_path);232}233#endif234execve(exec_path, argv, environ);235err(1, "execve: %s", argv[0]);236/* NOTREACHED */237}238239240