| Guido van Rossum | 582646a | 1996-05-28 22:30:17 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
| Guido van Rossum | 582646a | 1996-05-28 22:30:17 +0000 | [diff] [blame] | 6 | |
| Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| Guido van Rossum | 582646a | 1996-05-28 22:30:17 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 10 | |
| 11 | /* Return the initial module search path. */ |
| 12 | |
| Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 13 | #include "Python.h" |
| 14 | #include "osdefs.h" |
| 15 | |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 16 | #include <sys/types.h> |
| 17 | #include <sys/stat.h> |
| Guido van Rossum | 21f8497 | 1997-06-02 22:18:31 +0000 | [diff] [blame] | 18 | #include <string.h> |
| Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 19 | |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 20 | #if HAVE_UNISTD_H |
| 21 | #include <unistd.h> |
| 22 | #endif /* HAVE_UNISTD_H */ |
| 23 | |
| Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 24 | #ifdef WITH_NEXT_FRAMEWORK |
| 25 | #include <mach-o/dyld.h> |
| 26 | #endif |
| 27 | |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 28 | /* Search in some common locations for the associated Python libraries. |
| 29 | * |
| 30 | * Two directories must be found, the platform independent directory |
| Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 31 | * (prefix), containing the common .py and .pyc files, and the platform |
| 32 | * dependent directory (exec_prefix), containing the shared library |
| 33 | * modules. Note that prefix and exec_prefix can be the same directory, |
| 34 | * but for some installations, they are different. |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 35 | * |
| Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 36 | * Py_GetPath() carries out separate searches for prefix and exec_prefix. |
| 37 | * Each search tries a number of different locations until a ``landmark'' |
| 38 | * file or directory is found. If no prefix or exec_prefix is found, a |
| 39 | * warning message is issued and the preprocessor defined PREFIX and |
| 40 | * EXEC_PREFIX are used (even though they will not work); python carries on |
| 41 | * as best as is possible, but most imports will fail. |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 42 | * |
| 43 | * Before any searches are done, the location of the executable is |
| Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 44 | * determined. If argv[0] has one or more slashs in it, it is used |
| 45 | * unchanged. Otherwise, it must have been invoked from the shell's path, |
| 46 | * so we search $PATH for the named executable and use that. If the |
| 47 | * executable was not found on $PATH (or there was no $PATH environment |
| 48 | * variable), the original argv[0] string is used. |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 49 | * |
| Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 50 | * Next, the executable location is examined to see if it is a symbolic |
| 51 | * link. If so, the link is chased (correctly interpreting a relative |
| 52 | * pathname if one is found) and the directory of the link target is used. |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 53 | * |
| Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 54 | * Finally, argv0_path is set to the directory containing the executable |
| 55 | * (i.e. the last component is stripped). |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 56 | * |
| Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 57 | * With argv0_path in hand, we perform a number of steps. The same steps |
| 58 | * are performed for prefix and for exec_prefix, but with a different |
| 59 | * landmark. |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 60 | * |
| 61 | * Step 1. Are we running python out of the build directory? This is |
| 62 | * checked by looking for a different kind of landmark relative to |
| Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 63 | * argv0_path. For prefix, the landmark's path is derived from the VPATH |
| 64 | * preprocessor variable (taking into account that its value is almost, but |
| 65 | * not quite, what we need). For exec_prefix, the landmark is |
| 66 | * Modules/Setup. If the landmark is found, we're done. |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 67 | * |
| 68 | * For the remaining steps, the prefix landmark will always be |
| Jeremy Hylton | 847a996 | 2000-05-26 21:49:07 +0000 | [diff] [blame] | 69 | * lib/python$VERSION/os.py and the exec_prefix will always be |
| Guido van Rossum | 266033e | 1997-10-20 23:20:32 +0000 | [diff] [blame] | 70 | * lib/python$VERSION/lib-dynload, where $VERSION is Python's version |
| Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 71 | * number as supplied by the Makefile. Note that this means that no more |
| 72 | * build directory checking is performed; if the first step did not find |
| 73 | * the landmarks, the assumption is that python is running from an |
| 74 | * installed setup. |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 75 | * |
| 76 | * Step 2. See if the $PYTHONHOME environment variable points to the |
| Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 77 | * installed location of the Python libraries. If $PYTHONHOME is set, then |
| 78 | * it points to prefix and exec_prefix. $PYTHONHOME can be a single |
| 79 | * directory, which is used for both, or the prefix and exec_prefix |
| 80 | * directories separated by a colon. |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 81 | * |
| 82 | * Step 3. Try to find prefix and exec_prefix relative to argv0_path, |
| Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 83 | * backtracking up the path until it is exhausted. This is the most common |
| 84 | * step to succeed. Note that if prefix and exec_prefix are different, |
| 85 | * exec_prefix is more likely to be found; however if exec_prefix is a |
| 86 | * subdirectory of prefix, both will be found. |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 87 | * |
| Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 88 | * Step 4. Search the directories pointed to by the preprocessor variables |
| 89 | * PREFIX and EXEC_PREFIX. These are supplied by the Makefile but can be |
| 90 | * passed in as options to the configure script. |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 91 | * |
| Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 92 | * That's it! |
| 93 | * |
| 94 | * Well, almost. Once we have determined prefix and exec_prefix, the |
| 95 | * preprocesor variable PYTHONPATH is used to construct a path. Each |
| 96 | * relative path on PYTHONPATH is prefixed with prefix. Then the directory |
| 97 | * containing the shared library modules is appended. The environment |
| 98 | * variable $PYTHONPATH is inserted in front of it all. Finally, the |
| 99 | * prefix and exec_prefix globals are tweaked so they reflect the values |
| 100 | * expected by other code, by stripping the "lib/python$VERSION/..." stuff |
| 101 | * off. If either points to the build directory, the globals are reset to |
| 102 | * the corresponding preprocessor variables (so sys.prefix will reflect the |
| 103 | * installation location, even though sys.path points into the build |
| 104 | * directory). This seems to make more sense given that currently the only |
| 105 | * known use of sys.prefix and sys.exec_prefix is for the ILU installation |
| 106 | * process to find the installed Python tree. |
| 107 | */ |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 108 | |
| 109 | #ifndef VERSION |
| 110 | #define VERSION "1.5" |
| 111 | #endif |
| 112 | |
| 113 | #ifndef VPATH |
| 114 | #define VPATH "." |
| Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 115 | #endif |
| 116 | |
| Guido van Rossum | c34c9a5 | 1996-06-12 04:20:27 +0000 | [diff] [blame] | 117 | #ifndef PREFIX |
| 118 | #define PREFIX "/usr/local" |
| 119 | #endif |
| 120 | |
| 121 | #ifndef EXEC_PREFIX |
| Guido van Rossum | 6e12d56 | 1996-07-30 20:36:12 +0000 | [diff] [blame] | 122 | #define EXEC_PREFIX PREFIX |
| Guido van Rossum | c34c9a5 | 1996-06-12 04:20:27 +0000 | [diff] [blame] | 123 | #endif |
| 124 | |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 125 | #ifndef PYTHONPATH |
| 126 | /* I know this isn't K&R C, but the Makefile specifies it anyway */ |
| 127 | #define PYTHONPATH PREFIX "/lib/python" VERSION ":" \ |
| Guido van Rossum | 266033e | 1997-10-20 23:20:32 +0000 | [diff] [blame] | 128 | EXEC_PREFIX "/lib/python" VERSION "/lib-dynload" |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 129 | #endif |
| Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 130 | |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 131 | #ifndef LANDMARK |
| Jeremy Hylton | 847a996 | 2000-05-26 21:49:07 +0000 | [diff] [blame] | 132 | #define LANDMARK "os.py" |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 133 | #endif |
| 134 | |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 135 | static char prefix[MAXPATHLEN+1]; |
| 136 | static char exec_prefix[MAXPATHLEN+1]; |
| Guido van Rossum | 7929c6f | 1997-05-20 22:38:21 +0000 | [diff] [blame] | 137 | static char progpath[MAXPATHLEN+1]; |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 138 | static char *module_search_path = NULL; |
| 139 | static char lib_python[20]; /* Dynamically set to "lib/python" VERSION */ |
| 140 | |
| 141 | static void |
| 142 | reduce(dir) |
| 143 | char *dir; |
| 144 | { |
| Guido van Rossum | b6f657c | 2000-06-28 21:29:03 +0000 | [diff] [blame] | 145 | size_t i = strlen(dir); |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 146 | while (i > 0 && dir[i] != SEP) |
| 147 | --i; |
| 148 | dir[i] = '\0'; |
| 149 | } |
| Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 150 | |
| 151 | |
| 152 | #ifndef S_ISREG |
| 153 | #define S_ISREG(x) (((x) & S_IFMT) == S_IFREG) |
| 154 | #endif |
| 155 | |
| 156 | #ifndef S_ISDIR |
| 157 | #define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR) |
| 158 | #endif |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 159 | |
| 160 | static int |
| Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 161 | isfile(filename) /* Is file, not directory */ |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 162 | char *filename; |
| 163 | { |
| 164 | struct stat buf; |
| Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 165 | if (stat(filename, &buf) != 0) |
| 166 | return 0; |
| 167 | if (!S_ISREG(buf.st_mode)) |
| 168 | return 0; |
| 169 | return 1; |
| 170 | } |
| 171 | |
| 172 | |
| 173 | static int |
| 174 | ismodule(filename) /* Is module -- check for .pyc/.pyo too */ |
| 175 | char *filename; |
| 176 | { |
| 177 | if (isfile(filename)) |
| 178 | return 1; |
| 179 | |
| 180 | /* Check for the compiled version of prefix. */ |
| 181 | if (strlen(filename) < MAXPATHLEN) { |
| 182 | strcat(filename, Py_OptimizeFlag ? "o" : "c"); |
| 183 | if (isfile(filename)) |
| 184 | return 1; |
| 185 | } |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | |
| 190 | static int |
| 191 | isxfile(filename) /* Is executable file */ |
| 192 | char *filename; |
| 193 | { |
| 194 | struct stat buf; |
| 195 | if (stat(filename, &buf) != 0) |
| 196 | return 0; |
| 197 | if (!S_ISREG(buf.st_mode)) |
| 198 | return 0; |
| 199 | if ((buf.st_mode & 0111) == 0) |
| 200 | return 0; |
| 201 | return 1; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | static int |
| 206 | isdir(filename) /* Is directory */ |
| 207 | char *filename; |
| 208 | { |
| 209 | struct stat buf; |
| 210 | if (stat(filename, &buf) != 0) |
| 211 | return 0; |
| 212 | if (!S_ISDIR(buf.st_mode)) |
| 213 | return 0; |
| 214 | return 1; |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | |
| 218 | static void |
| Guido van Rossum | 6b9fdf5 | 1997-08-20 23:48:16 +0000 | [diff] [blame] | 219 | joinpath(buffer, stuff) |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 220 | char *buffer; |
| 221 | char *stuff; |
| 222 | { |
| Guido van Rossum | b6f657c | 2000-06-28 21:29:03 +0000 | [diff] [blame] | 223 | size_t n, k; |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 224 | if (stuff[0] == SEP) |
| 225 | n = 0; |
| 226 | else { |
| 227 | n = strlen(buffer); |
| 228 | if (n > 0 && buffer[n-1] != SEP && n < MAXPATHLEN) |
| 229 | buffer[n++] = SEP; |
| 230 | } |
| 231 | k = strlen(stuff); |
| 232 | if (n + k > MAXPATHLEN) |
| 233 | k = MAXPATHLEN - n; |
| 234 | strncpy(buffer+n, stuff, k); |
| 235 | buffer[n+k] = '\0'; |
| 236 | } |
| 237 | |
| 238 | |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 239 | static int |
| 240 | search_for_prefix(argv0_path, home) |
| 241 | char *argv0_path; |
| 242 | char *home; |
| 243 | { |
| Guido van Rossum | b6f657c | 2000-06-28 21:29:03 +0000 | [diff] [blame] | 244 | size_t n; |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 245 | char *vpath; |
| 246 | |
| Jeremy Hylton | 847a996 | 2000-05-26 21:49:07 +0000 | [diff] [blame] | 247 | /* If PYTHONHOME is set, we believe it unconditionally */ |
| 248 | if (home) { |
| 249 | char *delim; |
| 250 | strcpy(prefix, home); |
| 251 | delim = strchr(prefix, DELIM); |
| 252 | if (delim) |
| 253 | *delim = '\0'; |
| 254 | joinpath(prefix, lib_python); |
| 255 | joinpath(prefix, LANDMARK); |
| 256 | return 1; |
| 257 | } |
| 258 | |
| Guido van Rossum | 573a24a | 1997-05-12 20:49:39 +0000 | [diff] [blame] | 259 | /* Check to see if argv[0] is in the build directory */ |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 260 | strcpy(prefix, argv0_path); |
| Guido van Rossum | 6b9fdf5 | 1997-08-20 23:48:16 +0000 | [diff] [blame] | 261 | joinpath(prefix, "Modules/Setup"); |
| Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 262 | if (isfile(prefix)) { |
| Guido van Rossum | 573a24a | 1997-05-12 20:49:39 +0000 | [diff] [blame] | 263 | /* Check VPATH to see if argv0_path is in the build directory. |
| 264 | * Complication: the VPATH passed in is relative to the |
| 265 | * Modules build directory and points to the Modules source |
| 266 | * directory; we need it relative to the build tree and |
| 267 | * pointing to the source tree. Solution: chop off a leading |
| 268 | * ".." (but only if it's there -- it could be an absolute |
| 269 | * path) and chop off the final component (assuming it's |
| 270 | * "Modules"). |
| 271 | */ |
| 272 | vpath = VPATH; |
| 273 | if (vpath[0] == '.' && vpath[1] == '.' && vpath[2] == '/') |
| 274 | vpath += 3; |
| 275 | strcpy(prefix, argv0_path); |
| Guido van Rossum | 6b9fdf5 | 1997-08-20 23:48:16 +0000 | [diff] [blame] | 276 | joinpath(prefix, vpath); |
| Guido van Rossum | 573a24a | 1997-05-12 20:49:39 +0000 | [diff] [blame] | 277 | reduce(prefix); |
| Guido van Rossum | 6b9fdf5 | 1997-08-20 23:48:16 +0000 | [diff] [blame] | 278 | joinpath(prefix, "Lib"); |
| 279 | joinpath(prefix, LANDMARK); |
| Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 280 | if (ismodule(prefix)) |
| Guido van Rossum | 573a24a | 1997-05-12 20:49:39 +0000 | [diff] [blame] | 281 | return -1; |
| 282 | } |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 283 | |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 284 | /* Search from argv0_path, until root is found */ |
| 285 | strcpy(prefix, argv0_path); |
| 286 | do { |
| 287 | n = strlen(prefix); |
| Guido van Rossum | 6b9fdf5 | 1997-08-20 23:48:16 +0000 | [diff] [blame] | 288 | joinpath(prefix, lib_python); |
| 289 | joinpath(prefix, LANDMARK); |
| Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 290 | if (ismodule(prefix)) |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 291 | return 1; |
| 292 | prefix[n] = '\0'; |
| 293 | reduce(prefix); |
| 294 | } while (prefix[0]); |
| 295 | |
| 296 | /* Look at configure's PREFIX */ |
| 297 | strcpy(prefix, PREFIX); |
| Guido van Rossum | 6b9fdf5 | 1997-08-20 23:48:16 +0000 | [diff] [blame] | 298 | joinpath(prefix, lib_python); |
| 299 | joinpath(prefix, LANDMARK); |
| Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 300 | if (ismodule(prefix)) |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 301 | return 1; |
| 302 | |
| Guido van Rossum | d776362 | 1997-05-12 20:53:23 +0000 | [diff] [blame] | 303 | /* Fail */ |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | |
| 308 | static int |
| 309 | search_for_exec_prefix(argv0_path, home) |
| 310 | char *argv0_path; |
| 311 | char *home; |
| 312 | { |
| Guido van Rossum | b6f657c | 2000-06-28 21:29:03 +0000 | [diff] [blame] | 313 | size_t n; |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 314 | |
| Jeremy Hylton | 847a996 | 2000-05-26 21:49:07 +0000 | [diff] [blame] | 315 | /* If PYTHONHOME is set, we believe it unconditionally */ |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 316 | if (home) { |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 317 | char *delim; |
| 318 | delim = strchr(home, DELIM); |
| 319 | if (delim) |
| 320 | strcpy(exec_prefix, delim+1); |
| 321 | else |
| 322 | strcpy(exec_prefix, home); |
| Guido van Rossum | 6b9fdf5 | 1997-08-20 23:48:16 +0000 | [diff] [blame] | 323 | joinpath(exec_prefix, lib_python); |
| Guido van Rossum | 266033e | 1997-10-20 23:20:32 +0000 | [diff] [blame] | 324 | joinpath(exec_prefix, "lib-dynload"); |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 325 | return 1; |
| 326 | } |
| 327 | |
| Jeremy Hylton | 847a996 | 2000-05-26 21:49:07 +0000 | [diff] [blame] | 328 | /* Check to see if argv[0] is in the build directory */ |
| 329 | strcpy(exec_prefix, argv0_path); |
| 330 | joinpath(exec_prefix, "Modules/Setup"); |
| 331 | if (isfile(exec_prefix)) { |
| 332 | reduce(exec_prefix); |
| 333 | return -1; |
| 334 | } |
| 335 | |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 336 | /* Search from argv0_path, until root is found */ |
| 337 | strcpy(exec_prefix, argv0_path); |
| 338 | do { |
| 339 | n = strlen(exec_prefix); |
| Guido van Rossum | 6b9fdf5 | 1997-08-20 23:48:16 +0000 | [diff] [blame] | 340 | joinpath(exec_prefix, lib_python); |
| Guido van Rossum | 266033e | 1997-10-20 23:20:32 +0000 | [diff] [blame] | 341 | joinpath(exec_prefix, "lib-dynload"); |
| Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 342 | if (isdir(exec_prefix)) |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 343 | return 1; |
| 344 | exec_prefix[n] = '\0'; |
| 345 | reduce(exec_prefix); |
| 346 | } while (exec_prefix[0]); |
| 347 | |
| 348 | /* Look at configure's EXEC_PREFIX */ |
| 349 | strcpy(exec_prefix, EXEC_PREFIX); |
| Guido van Rossum | 6b9fdf5 | 1997-08-20 23:48:16 +0000 | [diff] [blame] | 350 | joinpath(exec_prefix, lib_python); |
| Guido van Rossum | 266033e | 1997-10-20 23:20:32 +0000 | [diff] [blame] | 351 | joinpath(exec_prefix, "lib-dynload"); |
| Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 352 | if (isdir(exec_prefix)) |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 353 | return 1; |
| 354 | |
| Guido van Rossum | d776362 | 1997-05-12 20:53:23 +0000 | [diff] [blame] | 355 | /* Fail */ |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | |
| 360 | static void |
| 361 | calculate_path() |
| 362 | { |
| 363 | extern char *Py_GetProgramName(); |
| 364 | |
| Guido van Rossum | 7929c6f | 1997-05-20 22:38:21 +0000 | [diff] [blame] | 365 | static char delimiter[2] = {DELIM, '\0'}; |
| 366 | static char separator[2] = {SEP, '\0'}; |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 367 | char *pythonpath = PYTHONPATH; |
| 368 | char *rtpypath = getenv("PYTHONPATH"); |
| Guido van Rossum | 131c92c | 1998-02-06 22:29:30 +0000 | [diff] [blame] | 369 | char *home = Py_GetPythonHome(); |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 370 | char *path = getenv("PATH"); |
| 371 | char *prog = Py_GetProgramName(); |
| 372 | char argv0_path[MAXPATHLEN+1]; |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 373 | int pfound, efound; /* 1 if found; -1 if found build directory */ |
| 374 | char *buf; |
| Guido van Rossum | b6f657c | 2000-06-28 21:29:03 +0000 | [diff] [blame] | 375 | size_t bufsz; |
| 376 | size_t prefixsz; |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 377 | char *defpath = pythonpath; |
| Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 378 | #ifdef WITH_NEXT_FRAMEWORK |
| 379 | NSModule pythonModule; |
| 380 | #endif |
| 381 | |
| 382 | #ifdef WITH_NEXT_FRAMEWORK |
| 383 | pythonModule = NSModuleForSymbol(NSLookupAndBindSymbol("_Py_Initialize")); |
| 384 | /* Use dylib functions to find out where the framework was loaded from */ |
| 385 | buf = NSLibraryNameForModule(pythonModule); |
| 386 | if (buf != NULL) { |
| 387 | /* We're in a framework. */ |
| 388 | strcpy(progpath, buf); |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 389 | |
| Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 390 | /* Frameworks have support for versioning */ |
| 391 | strcpy(lib_python, "lib"); |
| 392 | } else { |
| 393 | /* If we're not in a framework, fall back to the old way (even though NSNameOfModule() probably does the same thing.) */ |
| 394 | #endif |
| 395 | |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 396 | /* Initialize this dynamically for K&R C */ |
| 397 | sprintf(lib_python, "lib/python%s", VERSION); |
| 398 | |
| 399 | /* If there is no slash in the argv0 path, then we have to |
| 400 | * assume python is on the user's $PATH, since there's no |
| 401 | * other way to find a directory to start the search from. If |
| 402 | * $PATH isn't exported, you lose. |
| 403 | */ |
| 404 | if (strchr(prog, SEP)) |
| 405 | strcpy(progpath, prog); |
| 406 | else if (path) { |
| 407 | while (1) { |
| 408 | char *delim = strchr(path, DELIM); |
| 409 | |
| 410 | if (delim) { |
| Guido van Rossum | b6f657c | 2000-06-28 21:29:03 +0000 | [diff] [blame] | 411 | size_t len = delim - path; |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 412 | strncpy(progpath, path, len); |
| 413 | *(progpath + len) = '\0'; |
| 414 | } |
| 415 | else |
| 416 | strcpy(progpath, path); |
| 417 | |
| Guido van Rossum | 6b9fdf5 | 1997-08-20 23:48:16 +0000 | [diff] [blame] | 418 | joinpath(progpath, prog); |
| Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 419 | if (isxfile(progpath)) |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 420 | break; |
| 421 | |
| 422 | if (!delim) { |
| 423 | progpath[0] = '\0'; |
| 424 | break; |
| 425 | } |
| 426 | path = delim + 1; |
| 427 | } |
| 428 | } |
| 429 | else |
| 430 | progpath[0] = '\0'; |
| Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 431 | #ifdef WITH_NEXT_FRAMEWORK |
| 432 | } |
| 433 | #endif |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 434 | |
| 435 | strcpy(argv0_path, progpath); |
| 436 | |
| 437 | #if HAVE_READLINK |
| 438 | { |
| 439 | char tmpbuffer[MAXPATHLEN+1]; |
| 440 | int linklen = readlink(progpath, tmpbuffer, MAXPATHLEN); |
| Guido van Rossum | 302be44 | 1998-04-29 21:07:06 +0000 | [diff] [blame] | 441 | while (linklen != -1) { |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 442 | /* It's not null terminated! */ |
| 443 | tmpbuffer[linklen] = '\0'; |
| 444 | if (tmpbuffer[0] == SEP) |
| 445 | strcpy(argv0_path, tmpbuffer); |
| 446 | else { |
| 447 | /* Interpret relative to progpath */ |
| 448 | reduce(argv0_path); |
| Guido van Rossum | 6b9fdf5 | 1997-08-20 23:48:16 +0000 | [diff] [blame] | 449 | joinpath(argv0_path, tmpbuffer); |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 450 | } |
| Guido van Rossum | 302be44 | 1998-04-29 21:07:06 +0000 | [diff] [blame] | 451 | linklen = readlink(argv0_path, tmpbuffer, MAXPATHLEN); |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | #endif /* HAVE_READLINK */ |
| 455 | |
| 456 | reduce(argv0_path); |
| 457 | |
| 458 | if (!(pfound = search_for_prefix(argv0_path, home))) { |
| Guido van Rossum | 131c92c | 1998-02-06 22:29:30 +0000 | [diff] [blame] | 459 | if (!Py_FrozenFlag) |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 460 | fprintf(stderr, |
| 461 | "Could not find platform independent libraries <prefix>\n"); |
| 462 | strcpy(prefix, PREFIX); |
| Guido van Rossum | 6b9fdf5 | 1997-08-20 23:48:16 +0000 | [diff] [blame] | 463 | joinpath(prefix, lib_python); |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 464 | } |
| 465 | else |
| 466 | reduce(prefix); |
| 467 | |
| 468 | if (!(efound = search_for_exec_prefix(argv0_path, home))) { |
| Guido van Rossum | 131c92c | 1998-02-06 22:29:30 +0000 | [diff] [blame] | 469 | if (!Py_FrozenFlag) |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 470 | fprintf(stderr, |
| 471 | "Could not find platform dependent libraries <exec_prefix>\n"); |
| 472 | strcpy(exec_prefix, EXEC_PREFIX); |
| Guido van Rossum | 266033e | 1997-10-20 23:20:32 +0000 | [diff] [blame] | 473 | joinpath(exec_prefix, "lib/lib-dynload"); |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 474 | } |
| 475 | /* If we found EXEC_PREFIX do *not* reduce it! (Yet.) */ |
| 476 | |
| Guido van Rossum | 131c92c | 1998-02-06 22:29:30 +0000 | [diff] [blame] | 477 | if ((!pfound || !efound) && !Py_FrozenFlag) |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 478 | fprintf(stderr, |
| 479 | "Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]\n"); |
| 480 | |
| 481 | /* Calculate size of return buffer. |
| 482 | */ |
| 483 | bufsz = 0; |
| 484 | |
| 485 | if (rtpypath) |
| 486 | bufsz += strlen(rtpypath) + 1; |
| 487 | |
| 488 | prefixsz = strlen(prefix) + 1; |
| 489 | |
| 490 | while (1) { |
| 491 | char *delim = strchr(defpath, DELIM); |
| 492 | |
| 493 | if (defpath[0] != SEP) |
| 494 | /* Paths are relative to prefix */ |
| 495 | bufsz += prefixsz; |
| 496 | |
| 497 | if (delim) |
| 498 | bufsz += delim - defpath + 1; |
| 499 | else { |
| 500 | bufsz += strlen(defpath) + 1; |
| 501 | break; |
| 502 | } |
| 503 | defpath = delim + 1; |
| 504 | } |
| 505 | |
| 506 | bufsz += strlen(exec_prefix) + 1; |
| 507 | |
| 508 | /* This is the only malloc call in this file */ |
| Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 509 | buf = PyMem_Malloc(bufsz); |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 510 | |
| 511 | if (buf == NULL) { |
| 512 | /* We can't exit, so print a warning and limp along */ |
| 513 | fprintf(stderr, "Not enough memory for dynamic PYTHONPATH.\n"); |
| 514 | fprintf(stderr, "Using default static PYTHONPATH.\n"); |
| 515 | module_search_path = PYTHONPATH; |
| 516 | } |
| 517 | else { |
| 518 | /* Run-time value of $PYTHONPATH goes first */ |
| 519 | if (rtpypath) { |
| 520 | strcpy(buf, rtpypath); |
| 521 | strcat(buf, delimiter); |
| 522 | } |
| Guido van Rossum | 573a24a | 1997-05-12 20:49:39 +0000 | [diff] [blame] | 523 | else |
| 524 | buf[0] = '\0'; |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 525 | |
| 526 | /* Next goes merge of compile-time $PYTHONPATH with |
| 527 | * dynamically located prefix. |
| 528 | */ |
| 529 | defpath = pythonpath; |
| 530 | while (1) { |
| 531 | char *delim = strchr(defpath, DELIM); |
| 532 | |
| 533 | if (defpath[0] != SEP) { |
| 534 | strcat(buf, prefix); |
| 535 | strcat(buf, separator); |
| 536 | } |
| 537 | |
| 538 | if (delim) { |
| Guido van Rossum | b6f657c | 2000-06-28 21:29:03 +0000 | [diff] [blame] | 539 | size_t len = delim - defpath + 1; |
| 540 | size_t end = strlen(buf) + len; |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 541 | strncat(buf, defpath, len); |
| 542 | *(buf + end) = '\0'; |
| 543 | } |
| 544 | else { |
| 545 | strcat(buf, defpath); |
| 546 | break; |
| 547 | } |
| 548 | defpath = delim + 1; |
| 549 | } |
| 550 | strcat(buf, delimiter); |
| 551 | |
| 552 | /* Finally, on goes the directory for dynamic-load modules */ |
| 553 | strcat(buf, exec_prefix); |
| 554 | |
| 555 | /* And publish the results */ |
| 556 | module_search_path = buf; |
| 557 | } |
| 558 | |
| 559 | /* Reduce prefix and exec_prefix to their essence, |
| 560 | * e.g. /usr/local/lib/python1.5 is reduced to /usr/local. |
| 561 | * If we're loading relative to the build directory, |
| 562 | * return the compiled-in defaults instead. |
| 563 | */ |
| 564 | if (pfound > 0) { |
| 565 | reduce(prefix); |
| 566 | reduce(prefix); |
| 567 | } |
| 568 | else |
| 569 | strcpy(prefix, PREFIX); |
| 570 | |
| 571 | if (efound > 0) { |
| 572 | reduce(exec_prefix); |
| 573 | reduce(exec_prefix); |
| 574 | reduce(exec_prefix); |
| 575 | } |
| 576 | else |
| 577 | strcpy(exec_prefix, EXEC_PREFIX); |
| 578 | } |
| 579 | |
| 580 | |
| 581 | /* External interface */ |
| Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 582 | |
| 583 | char * |
| Guido van Rossum | 582646a | 1996-05-28 22:30:17 +0000 | [diff] [blame] | 584 | Py_GetPath() |
| Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 585 | { |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 586 | if (!module_search_path) |
| 587 | calculate_path(); |
| 588 | return module_search_path; |
| Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 589 | } |
| Guido van Rossum | c34c9a5 | 1996-06-12 04:20:27 +0000 | [diff] [blame] | 590 | |
| Guido van Rossum | c34c9a5 | 1996-06-12 04:20:27 +0000 | [diff] [blame] | 591 | char * |
| 592 | Py_GetPrefix() |
| 593 | { |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 594 | if (!module_search_path) |
| 595 | calculate_path(); |
| 596 | return prefix; |
| Guido van Rossum | c34c9a5 | 1996-06-12 04:20:27 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | char * |
| 600 | Py_GetExecPrefix() |
| 601 | { |
| Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 602 | if (!module_search_path) |
| 603 | calculate_path(); |
| 604 | return exec_prefix; |
| Guido van Rossum | c34c9a5 | 1996-06-12 04:20:27 +0000 | [diff] [blame] | 605 | } |
| Guido van Rossum | 7929c6f | 1997-05-20 22:38:21 +0000 | [diff] [blame] | 606 | |
| 607 | char * |
| 608 | Py_GetProgramFullPath() |
| 609 | { |
| 610 | if (!module_search_path) |
| 611 | calculate_path(); |
| 612 | return progpath; |
| 613 | } |