blob: 8330cbfe6f559de7c9fd0364e794b0f7fc7990ce [file] [log] [blame]
Guido van Rossum582646a1996-05-28 22:30:17 +00001/***********************************************************
2Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
4
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossum582646a1996-05-28 22:30:17 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossum582646a1996-05-28 22:30:17 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossum582646a1996-05-28 22:30:17 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum582646a1996-05-28 22:30:17 +000029
30******************************************************************/
31
32/* Return the initial module search path. */
33
Guido van Rossum667d7041995-08-04 04:20:48 +000034#include "Python.h"
35#include "osdefs.h"
36
Guido van Rossum305e5d01997-04-11 17:18:45 +000037#include <sys/types.h>
38#include <sys/stat.h>
39#include <strings.h>
Guido van Rossum667d7041995-08-04 04:20:48 +000040
Guido van Rossum305e5d01997-04-11 17:18:45 +000041#if HAVE_UNISTD_H
42#include <unistd.h>
43#endif /* HAVE_UNISTD_H */
44
45/* Search in some common locations for the associated Python libraries.
46 *
47 * Two directories must be found, the platform independent directory
Barry Warsaw90126031997-04-11 20:27:03 +000048 * (prefix), containing the common .py and .pyc files, and the platform
49 * dependent directory (exec_prefix), containing the shared library
50 * modules. Note that prefix and exec_prefix can be the same directory,
51 * but for some installations, they are different.
Guido van Rossum305e5d01997-04-11 17:18:45 +000052 *
Barry Warsaw90126031997-04-11 20:27:03 +000053 * Py_GetPath() carries out separate searches for prefix and exec_prefix.
54 * Each search tries a number of different locations until a ``landmark''
55 * file or directory is found. If no prefix or exec_prefix is found, a
56 * warning message is issued and the preprocessor defined PREFIX and
57 * EXEC_PREFIX are used (even though they will not work); python carries on
58 * as best as is possible, but most imports will fail.
Guido van Rossum305e5d01997-04-11 17:18:45 +000059 *
60 * Before any searches are done, the location of the executable is
Barry Warsaw90126031997-04-11 20:27:03 +000061 * determined. If argv[0] has one or more slashs in it, it is used
62 * unchanged. Otherwise, it must have been invoked from the shell's path,
63 * so we search $PATH for the named executable and use that. If the
64 * executable was not found on $PATH (or there was no $PATH environment
65 * variable), the original argv[0] string is used.
Guido van Rossum305e5d01997-04-11 17:18:45 +000066 *
Barry Warsaw90126031997-04-11 20:27:03 +000067 * Next, the executable location is examined to see if it is a symbolic
68 * link. If so, the link is chased (correctly interpreting a relative
69 * pathname if one is found) and the directory of the link target is used.
Guido van Rossum305e5d01997-04-11 17:18:45 +000070 *
Barry Warsaw90126031997-04-11 20:27:03 +000071 * Finally, argv0_path is set to the directory containing the executable
72 * (i.e. the last component is stripped).
Guido van Rossum305e5d01997-04-11 17:18:45 +000073 *
Barry Warsaw90126031997-04-11 20:27:03 +000074 * With argv0_path in hand, we perform a number of steps. The same steps
75 * are performed for prefix and for exec_prefix, but with a different
76 * landmark.
Guido van Rossum305e5d01997-04-11 17:18:45 +000077 *
78 * Step 1. Are we running python out of the build directory? This is
79 * checked by looking for a different kind of landmark relative to
Barry Warsaw90126031997-04-11 20:27:03 +000080 * argv0_path. For prefix, the landmark's path is derived from the VPATH
81 * preprocessor variable (taking into account that its value is almost, but
82 * not quite, what we need). For exec_prefix, the landmark is
83 * Modules/Setup. If the landmark is found, we're done.
Guido van Rossum305e5d01997-04-11 17:18:45 +000084 *
85 * For the remaining steps, the prefix landmark will always be
86 * lib/python$VERSION/string.py and the exec_prefix will always be
Barry Warsaw90126031997-04-11 20:27:03 +000087 * lib/python$VERSION/sharedmodules, where $VERSION is Python's version
88 * number as supplied by the Makefile. Note that this means that no more
89 * build directory checking is performed; if the first step did not find
90 * the landmarks, the assumption is that python is running from an
91 * installed setup.
Guido van Rossum305e5d01997-04-11 17:18:45 +000092 *
93 * Step 2. See if the $PYTHONHOME environment variable points to the
Barry Warsaw90126031997-04-11 20:27:03 +000094 * installed location of the Python libraries. If $PYTHONHOME is set, then
95 * it points to prefix and exec_prefix. $PYTHONHOME can be a single
96 * directory, which is used for both, or the prefix and exec_prefix
97 * directories separated by a colon.
Guido van Rossum305e5d01997-04-11 17:18:45 +000098 *
99 * Step 3. Try to find prefix and exec_prefix relative to argv0_path,
Barry Warsaw90126031997-04-11 20:27:03 +0000100 * backtracking up the path until it is exhausted. This is the most common
101 * step to succeed. Note that if prefix and exec_prefix are different,
102 * exec_prefix is more likely to be found; however if exec_prefix is a
103 * subdirectory of prefix, both will be found.
Guido van Rossum305e5d01997-04-11 17:18:45 +0000104 *
Barry Warsaw90126031997-04-11 20:27:03 +0000105 * Step 4. Search the directories pointed to by the preprocessor variables
106 * PREFIX and EXEC_PREFIX. These are supplied by the Makefile but can be
107 * passed in as options to the configure script.
Guido van Rossum305e5d01997-04-11 17:18:45 +0000108 *
Barry Warsaw90126031997-04-11 20:27:03 +0000109 * That's it!
110 *
111 * Well, almost. Once we have determined prefix and exec_prefix, the
112 * preprocesor variable PYTHONPATH is used to construct a path. Each
113 * relative path on PYTHONPATH is prefixed with prefix. Then the directory
114 * containing the shared library modules is appended. The environment
115 * variable $PYTHONPATH is inserted in front of it all. Finally, the
116 * prefix and exec_prefix globals are tweaked so they reflect the values
117 * expected by other code, by stripping the "lib/python$VERSION/..." stuff
118 * off. If either points to the build directory, the globals are reset to
119 * the corresponding preprocessor variables (so sys.prefix will reflect the
120 * installation location, even though sys.path points into the build
121 * directory). This seems to make more sense given that currently the only
122 * known use of sys.prefix and sys.exec_prefix is for the ILU installation
123 * process to find the installed Python tree.
124 */
Guido van Rossum305e5d01997-04-11 17:18:45 +0000125
126#ifndef VERSION
127#define VERSION "1.5"
128#endif
129
130#ifndef VPATH
131#define VPATH "."
Guido van Rossum667d7041995-08-04 04:20:48 +0000132#endif
133
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000134#ifndef PREFIX
135#define PREFIX "/usr/local"
136#endif
137
138#ifndef EXEC_PREFIX
Guido van Rossum6e12d561996-07-30 20:36:12 +0000139#define EXEC_PREFIX PREFIX
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000140#endif
141
Guido van Rossum305e5d01997-04-11 17:18:45 +0000142#ifndef PYTHONPATH
143/* I know this isn't K&R C, but the Makefile specifies it anyway */
144#define PYTHONPATH PREFIX "/lib/python" VERSION ":" \
145 PREFIX "/lib/python" VERSION "/test" ":" \
146 EXEC_PREFIX "/lib/python" VERSION "/sharedmodules"
147#endif
Guido van Rossum667d7041995-08-04 04:20:48 +0000148
Guido van Rossum305e5d01997-04-11 17:18:45 +0000149#ifndef LANDMARK
150#define LANDMARK "string.py"
151#endif
152
Guido van Rossum305e5d01997-04-11 17:18:45 +0000153static char prefix[MAXPATHLEN+1];
154static char exec_prefix[MAXPATHLEN+1];
Guido van Rossum7929c6f1997-05-20 22:38:21 +0000155static char progpath[MAXPATHLEN+1];
Guido van Rossum305e5d01997-04-11 17:18:45 +0000156static char *module_search_path = NULL;
157static char lib_python[20]; /* Dynamically set to "lib/python" VERSION */
158
159static void
160reduce(dir)
161 char *dir;
162{
163 int i = strlen(dir);
164 while (i > 0 && dir[i] != SEP)
165 --i;
166 dir[i] = '\0';
167}
168
169
170static int
171exists(filename)
172 char *filename;
173{
174 struct stat buf;
175 return stat(filename, &buf) == 0;
176}
177
178
179static void
180join(buffer, stuff)
181 char *buffer;
182 char *stuff;
183{
184 int n, k;
185 if (stuff[0] == SEP)
186 n = 0;
187 else {
188 n = strlen(buffer);
189 if (n > 0 && buffer[n-1] != SEP && n < MAXPATHLEN)
190 buffer[n++] = SEP;
191 }
192 k = strlen(stuff);
193 if (n + k > MAXPATHLEN)
194 k = MAXPATHLEN - n;
195 strncpy(buffer+n, stuff, k);
196 buffer[n+k] = '\0';
197}
198
199
Guido van Rossum305e5d01997-04-11 17:18:45 +0000200static int
201search_for_prefix(argv0_path, home)
202 char *argv0_path;
203 char *home;
204{
Guido van Rossum7d3246d1997-05-13 19:19:41 +0000205 int n;
Guido van Rossum305e5d01997-04-11 17:18:45 +0000206 char *vpath;
207
Guido van Rossum573a24a1997-05-12 20:49:39 +0000208 /* Check to see if argv[0] is in the build directory */
Guido van Rossum305e5d01997-04-11 17:18:45 +0000209 strcpy(prefix, argv0_path);
Guido van Rossum573a24a1997-05-12 20:49:39 +0000210 join(prefix, "Modules/Setup");
211 if (exists(prefix)) {
212 /* Check VPATH to see if argv0_path is in the build directory.
213 * Complication: the VPATH passed in is relative to the
214 * Modules build directory and points to the Modules source
215 * directory; we need it relative to the build tree and
216 * pointing to the source tree. Solution: chop off a leading
217 * ".." (but only if it's there -- it could be an absolute
218 * path) and chop off the final component (assuming it's
219 * "Modules").
220 */
221 vpath = VPATH;
222 if (vpath[0] == '.' && vpath[1] == '.' && vpath[2] == '/')
223 vpath += 3;
224 strcpy(prefix, argv0_path);
225 join(prefix, vpath);
226 reduce(prefix);
227 join(prefix, "Lib");
228 join(prefix, LANDMARK);
229 if (exists(prefix))
230 return -1;
231 }
Guido van Rossum305e5d01997-04-11 17:18:45 +0000232
233 if (home) {
234 /* Check $PYTHONHOME */
235 char *delim;
236 strcpy(prefix, home);
237 delim = strchr(prefix, DELIM);
238 if (delim)
239 *delim = '\0';
240 join(prefix, lib_python);
241 join(prefix, LANDMARK);
242 if (exists(prefix))
243 return 1;
244 }
245
246 /* Search from argv0_path, until root is found */
247 strcpy(prefix, argv0_path);
248 do {
249 n = strlen(prefix);
250 join(prefix, lib_python);
251 join(prefix, LANDMARK);
252 if (exists(prefix))
253 return 1;
254 prefix[n] = '\0';
255 reduce(prefix);
256 } while (prefix[0]);
257
258 /* Look at configure's PREFIX */
259 strcpy(prefix, PREFIX);
260 join(prefix, lib_python);
261 join(prefix, LANDMARK);
262 if (exists(prefix))
263 return 1;
264
Guido van Rossumd7763621997-05-12 20:53:23 +0000265 /* Fail */
Guido van Rossum305e5d01997-04-11 17:18:45 +0000266 return 0;
267}
268
269
270static int
271search_for_exec_prefix(argv0_path, home)
272 char *argv0_path;
273 char *home;
274{
Guido van Rossum7d3246d1997-05-13 19:19:41 +0000275 int n;
Guido van Rossum305e5d01997-04-11 17:18:45 +0000276
277 /* Check to see if argv[0] is in the build directory */
278 strcpy(exec_prefix, argv0_path);
279 join(exec_prefix, "Modules/Setup");
280 if (exists(exec_prefix)) {
281 reduce(exec_prefix);
282 return -1;
283 }
284
285 if (home) {
286 /* Check $PYTHONHOME */
287 char *delim;
288 delim = strchr(home, DELIM);
289 if (delim)
290 strcpy(exec_prefix, delim+1);
291 else
292 strcpy(exec_prefix, home);
293 join(exec_prefix, lib_python);
294 join(exec_prefix, "sharedmodules");
295 if (exists(exec_prefix))
296 return 1;
297 }
298
299 /* Search from argv0_path, until root is found */
300 strcpy(exec_prefix, argv0_path);
301 do {
302 n = strlen(exec_prefix);
303 join(exec_prefix, lib_python);
304 join(exec_prefix, "sharedmodules");
305 if (exists(exec_prefix))
306 return 1;
307 exec_prefix[n] = '\0';
308 reduce(exec_prefix);
309 } while (exec_prefix[0]);
310
311 /* Look at configure's EXEC_PREFIX */
312 strcpy(exec_prefix, EXEC_PREFIX);
313 join(exec_prefix, lib_python);
314 join(exec_prefix, "sharedmodules");
315 if (exists(exec_prefix))
316 return 1;
317
Guido van Rossumd7763621997-05-12 20:53:23 +0000318 /* Fail */
Guido van Rossum305e5d01997-04-11 17:18:45 +0000319 return 0;
320}
321
322
323static void
324calculate_path()
325{
326 extern char *Py_GetProgramName();
327
Guido van Rossum7929c6f1997-05-20 22:38:21 +0000328 static char delimiter[2] = {DELIM, '\0'};
329 static char separator[2] = {SEP, '\0'};
Guido van Rossum305e5d01997-04-11 17:18:45 +0000330 char *pythonpath = PYTHONPATH;
331 char *rtpypath = getenv("PYTHONPATH");
332 char *home = getenv("PYTHONHOME");
333 char *path = getenv("PATH");
334 char *prog = Py_GetProgramName();
335 char argv0_path[MAXPATHLEN+1];
Guido van Rossum305e5d01997-04-11 17:18:45 +0000336 int pfound, efound; /* 1 if found; -1 if found build directory */
337 char *buf;
338 int bufsz;
339 int prefixsz;
340 char *defpath = pythonpath;
341
342 /* Initialize this dynamically for K&R C */
343 sprintf(lib_python, "lib/python%s", VERSION);
344
345 /* If there is no slash in the argv0 path, then we have to
346 * assume python is on the user's $PATH, since there's no
347 * other way to find a directory to start the search from. If
348 * $PATH isn't exported, you lose.
349 */
350 if (strchr(prog, SEP))
351 strcpy(progpath, prog);
352 else if (path) {
353 while (1) {
354 char *delim = strchr(path, DELIM);
355
356 if (delim) {
357 int len = delim - path;
358 strncpy(progpath, path, len);
359 *(progpath + len) = '\0';
360 }
361 else
362 strcpy(progpath, path);
363
364 join(progpath, prog);
365 if (exists(progpath))
366 break;
367
368 if (!delim) {
369 progpath[0] = '\0';
370 break;
371 }
372 path = delim + 1;
373 }
374 }
375 else
376 progpath[0] = '\0';
377
378 strcpy(argv0_path, progpath);
379
380#if HAVE_READLINK
381 {
382 char tmpbuffer[MAXPATHLEN+1];
383 int linklen = readlink(progpath, tmpbuffer, MAXPATHLEN);
384 if (linklen != -1) {
385 /* It's not null terminated! */
386 tmpbuffer[linklen] = '\0';
387 if (tmpbuffer[0] == SEP)
388 strcpy(argv0_path, tmpbuffer);
389 else {
390 /* Interpret relative to progpath */
391 reduce(argv0_path);
392 join(argv0_path, tmpbuffer);
393 }
394 }
395 }
396#endif /* HAVE_READLINK */
397
398 reduce(argv0_path);
399
400 if (!(pfound = search_for_prefix(argv0_path, home))) {
401 fprintf(stderr,
402 "Could not find platform independent libraries <prefix>\n");
403 strcpy(prefix, PREFIX);
404 join(prefix, lib_python);
405 }
406 else
407 reduce(prefix);
408
409 if (!(efound = search_for_exec_prefix(argv0_path, home))) {
410 fprintf(stderr,
411 "Could not find platform dependent libraries <exec_prefix>\n");
412 strcpy(exec_prefix, EXEC_PREFIX);
413 join(exec_prefix, "lib/sharedmodules");
414 }
415 /* If we found EXEC_PREFIX do *not* reduce it! (Yet.) */
416
417 if (!pfound || !efound)
418 fprintf(stderr,
419 "Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]\n");
420
421 /* Calculate size of return buffer.
422 */
423 bufsz = 0;
424
425 if (rtpypath)
426 bufsz += strlen(rtpypath) + 1;
427
428 prefixsz = strlen(prefix) + 1;
429
430 while (1) {
431 char *delim = strchr(defpath, DELIM);
432
433 if (defpath[0] != SEP)
434 /* Paths are relative to prefix */
435 bufsz += prefixsz;
436
437 if (delim)
438 bufsz += delim - defpath + 1;
439 else {
440 bufsz += strlen(defpath) + 1;
441 break;
442 }
443 defpath = delim + 1;
444 }
445
446 bufsz += strlen(exec_prefix) + 1;
447
448 /* This is the only malloc call in this file */
449 buf = malloc(bufsz);
450
451 if (buf == NULL) {
452 /* We can't exit, so print a warning and limp along */
453 fprintf(stderr, "Not enough memory for dynamic PYTHONPATH.\n");
454 fprintf(stderr, "Using default static PYTHONPATH.\n");
455 module_search_path = PYTHONPATH;
456 }
457 else {
458 /* Run-time value of $PYTHONPATH goes first */
459 if (rtpypath) {
460 strcpy(buf, rtpypath);
461 strcat(buf, delimiter);
462 }
Guido van Rossum573a24a1997-05-12 20:49:39 +0000463 else
464 buf[0] = '\0';
Guido van Rossum305e5d01997-04-11 17:18:45 +0000465
466 /* Next goes merge of compile-time $PYTHONPATH with
467 * dynamically located prefix.
468 */
469 defpath = pythonpath;
470 while (1) {
471 char *delim = strchr(defpath, DELIM);
472
473 if (defpath[0] != SEP) {
474 strcat(buf, prefix);
475 strcat(buf, separator);
476 }
477
478 if (delim) {
479 int len = delim - defpath + 1;
480 int end = strlen(buf) + len;
481 strncat(buf, defpath, len);
482 *(buf + end) = '\0';
483 }
484 else {
485 strcat(buf, defpath);
486 break;
487 }
488 defpath = delim + 1;
489 }
490 strcat(buf, delimiter);
491
492 /* Finally, on goes the directory for dynamic-load modules */
493 strcat(buf, exec_prefix);
494
495 /* And publish the results */
496 module_search_path = buf;
497 }
498
499 /* Reduce prefix and exec_prefix to their essence,
500 * e.g. /usr/local/lib/python1.5 is reduced to /usr/local.
501 * If we're loading relative to the build directory,
502 * return the compiled-in defaults instead.
503 */
504 if (pfound > 0) {
505 reduce(prefix);
506 reduce(prefix);
507 }
508 else
509 strcpy(prefix, PREFIX);
510
511 if (efound > 0) {
512 reduce(exec_prefix);
513 reduce(exec_prefix);
514 reduce(exec_prefix);
515 }
516 else
517 strcpy(exec_prefix, EXEC_PREFIX);
518}
519
520
521/* External interface */
Guido van Rossum667d7041995-08-04 04:20:48 +0000522
523char *
Guido van Rossum582646a1996-05-28 22:30:17 +0000524Py_GetPath()
Guido van Rossum667d7041995-08-04 04:20:48 +0000525{
Guido van Rossum305e5d01997-04-11 17:18:45 +0000526 if (!module_search_path)
527 calculate_path();
528 return module_search_path;
Guido van Rossum667d7041995-08-04 04:20:48 +0000529}
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000530
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000531char *
532Py_GetPrefix()
533{
Guido van Rossum305e5d01997-04-11 17:18:45 +0000534 if (!module_search_path)
535 calculate_path();
536 return prefix;
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000537}
538
539char *
540Py_GetExecPrefix()
541{
Guido van Rossum305e5d01997-04-11 17:18:45 +0000542 if (!module_search_path)
543 calculate_path();
544 return exec_prefix;
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000545}
Guido van Rossum7929c6f1997-05-20 22:38:21 +0000546
547char *
548Py_GetProgramFullPath()
549{
550 if (!module_search_path)
551 calculate_path();
552 return progpath;
553}