autocompile -> auto-compile
[bpt/guile.git] / libguile / load.c
1 /* Copyright (C) 1995,1996,1998,1999,2000,2001, 2004, 2006, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
17 */
18
19
20 \f
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <stdio.h>
28
29 #include "libguile/_scm.h"
30 #include "libguile/private-gc.h" /* scm_getenv_int */
31 #include "libguile/libpath.h"
32 #include "libguile/fports.h"
33 #include "libguile/read.h"
34 #include "libguile/eval.h"
35 #include "libguile/throw.h"
36 #include "libguile/alist.h"
37 #include "libguile/dynwind.h"
38 #include "libguile/root.h"
39 #include "libguile/strings.h"
40 #include "libguile/modules.h"
41 #include "libguile/chars.h"
42 #include "libguile/srfi-13.h"
43
44 #include "libguile/validate.h"
45 #include "libguile/load.h"
46 #include "libguile/fluids.h"
47
48 #include "libguile/vm.h" /* for load-compiled/vm */
49
50 #include <sys/types.h>
51 #include <sys/stat.h>
52
53 #ifdef HAVE_UNISTD_H
54 #include <unistd.h>
55 #endif /* HAVE_UNISTD_H */
56
57 #ifdef HAVE_PWD_H
58 #include <pwd.h>
59 #endif /* HAVE_PWD_H */
60
61 #ifndef R_OK
62 #define R_OK 4
63 #endif
64
65 #include <stat-time.h>
66
67 \f
68 /* Loading a file, given an absolute filename. */
69
70 /* Hook to run when we load a file, perhaps to announce the fact somewhere.
71 Applied to the full name of the file. */
72 static SCM *scm_loc_load_hook;
73
74 /* The current reader (a fluid). */
75 static SCM the_reader = SCM_BOOL_F;
76
77
78 SCM_DEFINE (scm_primitive_load, "primitive-load", 1, 0, 0,
79 (SCM filename),
80 "Load the file named @var{filename} and evaluate its contents in\n"
81 "the top-level environment. The load paths are not searched;\n"
82 "@var{filename} must either be a full pathname or be a pathname\n"
83 "relative to the current directory. If the variable\n"
84 "@code{%load-hook} is defined, it should be bound to a procedure\n"
85 "that will be called before any code is loaded. See the\n"
86 "documentation for @code{%load-hook} later in this section.")
87 #define FUNC_NAME s_scm_primitive_load
88 {
89 SCM hook = *scm_loc_load_hook;
90 char *encoding;
91 SCM_VALIDATE_STRING (1, filename);
92 if (scm_is_true (hook) && scm_is_false (scm_procedure_p (hook)))
93 SCM_MISC_ERROR ("value of %load-hook is neither a procedure nor #f",
94 SCM_EOL);
95
96 if (!scm_is_false (hook))
97 scm_call_1 (hook, filename);
98
99 { /* scope */
100 SCM port = scm_open_file (filename, scm_from_locale_string ("r"));
101 scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
102 scm_i_dynwind_current_load_port (port);
103
104 encoding = scm_i_scan_for_encoding (port);
105 if (encoding)
106 scm_i_set_port_encoding_x (port, encoding);
107 else
108 /* The file has no encoding declared. We'll presume Latin-1. */
109 scm_i_set_port_encoding_x (port, NULL);
110
111 while (1)
112 {
113 SCM reader, form;
114
115 /* Lookup and use the current reader to read the next
116 expression. */
117 reader = scm_fluid_ref (the_reader);
118 if (reader == SCM_BOOL_F)
119 form = scm_read (port);
120 else
121 form = scm_call_1 (reader, port);
122
123 if (SCM_EOF_OBJECT_P (form))
124 break;
125
126 scm_primitive_eval_x (form);
127 }
128
129 scm_dynwind_end ();
130 scm_close_port (port);
131 }
132 return SCM_UNSPECIFIED;
133 }
134 #undef FUNC_NAME
135
136 SCM
137 scm_c_primitive_load (const char *filename)
138 {
139 return scm_primitive_load (scm_from_locale_string (filename));
140 }
141
142 \f
143 /* Builtin path to scheme library files. */
144 #ifdef SCM_PKGDATA_DIR
145 SCM_DEFINE (scm_sys_package_data_dir, "%package-data-dir", 0, 0, 0,
146 (),
147 "Return the name of the directory where Scheme packages, modules and\n"
148 "libraries are kept. On most Unix systems, this will be\n"
149 "@samp{/usr/local/share/guile}.")
150 #define FUNC_NAME s_scm_sys_package_data_dir
151 {
152 return scm_from_locale_string (SCM_PKGDATA_DIR);
153 }
154 #undef FUNC_NAME
155 #endif /* SCM_PKGDATA_DIR */
156
157 #ifdef SCM_LIBRARY_DIR
158 SCM_DEFINE (scm_sys_library_dir, "%library-dir", 0,0,0,
159 (),
160 "Return the directory where the Guile Scheme library files are installed.\n"
161 "E.g., may return \"/usr/share/guile/1.3.5\".")
162 #define FUNC_NAME s_scm_sys_library_dir
163 {
164 return scm_from_locale_string (SCM_LIBRARY_DIR);
165 }
166 #undef FUNC_NAME
167 #endif /* SCM_LIBRARY_DIR */
168
169 #ifdef SCM_SITE_DIR
170 SCM_DEFINE (scm_sys_site_dir, "%site-dir", 0,0,0,
171 (),
172 "Return the directory where users should install Scheme code for use\n"
173 "with this version of Guile.\n\n"
174 "E.g., may return \"/usr/share/guile/site/" SCM_EFFECTIVE_VERSION "\".")
175 #define FUNC_NAME s_scm_sys_site_dir
176 {
177 return scm_from_locale_string (SCM_SITE_DIR);
178 }
179 #undef FUNC_NAME
180 #endif /* SCM_SITE_DIR */
181
182 #ifdef SCM_GLOBAL_SITE_DIR
183 SCM_DEFINE (scm_sys_global_site_dir, "%global-site-dir", 0,0,0,
184 (),
185 "Return the directory where users should install Scheme code for use\n"
186 "with all versions of Guile.\n\n"
187 "E.g., may return \"/usr/share/guile/site\".")
188 #define FUNC_NAME s_scm_sys_global_site_dir
189 {
190 return scm_from_locale_string (SCM_GLOBAL_SITE_DIR);
191 }
192 #undef FUNC_NAME
193 #endif /* SCM_GLOBAL_SITE_DIR */
194
195
196 \f
197 /* Initializing the load path, and searching it. */
198
199 /* List of names of directories we search for files to load. */
200 static SCM *scm_loc_load_path;
201
202 /* List of extensions we try adding to the filenames. */
203 static SCM *scm_loc_load_extensions;
204
205 /* Like %load-path and %load-extensions, but for compiled files. */
206 static SCM *scm_loc_load_compiled_path;
207 static SCM *scm_loc_load_compiled_extensions;
208
209 /* Whether we should try to auto-compile. */
210 static SCM *scm_loc_load_should_auto_compile;
211
212 /* The fallback path for auto-compilation */
213 static SCM *scm_loc_compile_fallback_path;
214
215 SCM_DEFINE (scm_parse_path, "parse-path", 1, 1, 0,
216 (SCM path, SCM tail),
217 "Parse @var{path}, which is expected to be a colon-separated\n"
218 "string, into a list and return the resulting list with\n"
219 "@var{tail} appended. If @var{path} is @code{#f}, @var{tail}\n"
220 "is returned.")
221 #define FUNC_NAME s_scm_parse_path
222 {
223 #ifdef __MINGW32__
224 SCM sep = SCM_MAKE_CHAR (';');
225 #else
226 SCM sep = SCM_MAKE_CHAR (':');
227 #endif
228
229 if (SCM_UNBNDP (tail))
230 tail = SCM_EOL;
231 return (scm_is_false (path)
232 ? tail
233 : scm_append_x (scm_list_2 (scm_string_split (path, sep), tail)));
234 }
235 #undef FUNC_NAME
236
237
238 /* Initialize the global variable %load-path, given the value of the
239 SCM_SITE_DIR and SCM_LIBRARY_DIR preprocessor symbols and the
240 GUILE_LOAD_PATH environment variable. */
241 void
242 scm_init_load_path ()
243 {
244 char *env;
245 SCM path = SCM_EOL;
246 SCM cpath = SCM_EOL;
247
248 #ifdef SCM_LIBRARY_DIR
249 env = getenv ("GUILE_SYSTEM_PATH");
250 if (env && strcmp (env, "") == 0)
251 /* special-case interpret system-path=="" as meaning no system path instead
252 of '("") */
253 ;
254 else if (env)
255 path = scm_parse_path (scm_from_locale_string (env), path);
256 else
257 path = scm_list_4 (scm_from_locale_string (SCM_LIBRARY_DIR),
258 scm_from_locale_string (SCM_SITE_DIR),
259 scm_from_locale_string (SCM_GLOBAL_SITE_DIR),
260 scm_from_locale_string (SCM_PKGDATA_DIR));
261
262 env = getenv ("GUILE_SYSTEM_COMPILED_PATH");
263 if (env && strcmp (env, "") == 0)
264 /* like above */
265 ;
266 else if (env)
267 cpath = scm_parse_path (scm_from_locale_string (env), cpath);
268 else
269 cpath = scm_cons (scm_from_locale_string (SCM_CCACHE_DIR), cpath);
270
271 #endif /* SCM_LIBRARY_DIR */
272
273 {
274 char cachedir[1024];
275 char *e;
276 #ifdef HAVE_GETPWENT
277 struct passwd *pwd;
278 #endif
279
280 #define FALLBACK_DIR \
281 "guile/ccache/" SCM_EFFECTIVE_VERSION "-" SCM_OBJCODE_MACHINE_VERSION_STRING
282
283 if ((e = getenv ("XDG_CACHE_HOME")))
284 snprintf (cachedir, sizeof(cachedir), "%s/" FALLBACK_DIR, e);
285 else if ((e = getenv ("HOME")))
286 snprintf (cachedir, sizeof(cachedir), "%s/.cache/" FALLBACK_DIR, e);
287 #ifdef HAVE_GETPWENT
288 else if ((pwd = getpwuid (getuid ())) && pwd->pw_dir)
289 snprintf (cachedir, sizeof(cachedir), "%s/.cache/" FALLBACK_DIR,
290 pwd->pw_dir);
291 #endif /* HAVE_GETPWENT */
292 else
293 cachedir[0] = 0;
294
295 if (cachedir[0])
296 *scm_loc_compile_fallback_path = scm_from_locale_string (cachedir);
297 }
298
299 env = getenv ("GUILE_LOAD_PATH");
300 if (env)
301 path = scm_parse_path (scm_from_locale_string (env), path);
302
303 env = getenv ("GUILE_LOAD_COMPILED_PATH");
304 if (env)
305 cpath = scm_parse_path (scm_from_locale_string (env), cpath);
306
307 *scm_loc_load_path = path;
308 *scm_loc_load_compiled_path = cpath;
309 }
310
311 SCM scm_listofnullstr;
312
313 /* Utility functions for assembling C strings in a buffer.
314 */
315
316 struct stringbuf {
317 char *buf, *ptr;
318 size_t buf_len;
319 };
320
321 static void
322 stringbuf_free (void *data)
323 {
324 struct stringbuf *buf = (struct stringbuf *)data;
325 free (buf->buf);
326 }
327
328 static void
329 stringbuf_grow (struct stringbuf *buf)
330 {
331 size_t ptroff = buf->ptr - buf->buf;
332 buf->buf_len *= 2;
333 buf->buf = scm_realloc (buf->buf, buf->buf_len);
334 buf->ptr = buf->buf + ptroff;
335 }
336
337 static void
338 stringbuf_cat_locale_string (struct stringbuf *buf, SCM str)
339 {
340 size_t max_len = buf->buf_len - (buf->ptr - buf->buf) - 1;
341 size_t len = scm_to_locale_stringbuf (str, buf->ptr, max_len);
342 if (len > max_len)
343 {
344 /* buffer is too small, double its size and try again.
345 */
346 stringbuf_grow (buf);
347 stringbuf_cat_locale_string (buf, str);
348 }
349 else
350 {
351 /* string fits, terminate it and check for embedded '\0'.
352 */
353 buf->ptr[len] = '\0';
354 if (strlen (buf->ptr) != len)
355 scm_misc_error (NULL,
356 "string contains #\\nul character: ~S",
357 scm_list_1 (str));
358 buf->ptr += len;
359 }
360 }
361
362 static void
363 stringbuf_cat (struct stringbuf *buf, char *str)
364 {
365 size_t max_len = buf->buf_len - (buf->ptr - buf->buf) - 1;
366 size_t len = strlen (str);
367 if (len > max_len)
368 {
369 /* buffer is too small, double its size and try again.
370 */
371 stringbuf_grow (buf);
372 stringbuf_cat (buf, str);
373 }
374 else
375 {
376 /* string fits, copy it into buffer.
377 */
378 strcpy (buf->ptr, str);
379 buf->ptr += len;
380 }
381 }
382
383
384 static int
385 scm_c_string_has_an_ext (char *str, size_t len, SCM extensions)
386 {
387 for (; !scm_is_null (extensions); extensions = SCM_CDR (extensions))
388 {
389 char *ext;
390 size_t extlen;
391 int match;
392 ext = scm_to_locale_string (SCM_CAR (extensions));
393 extlen = strlen (ext);
394 match = (len > extlen && str[len - extlen - 1] == '.'
395 && strncmp (str + (len - extlen), ext, extlen) == 0);
396 free (ext);
397 if (match)
398 return 1;
399 }
400 return 0;
401 }
402
403 /* Search PATH for a directory containing a file named FILENAME.
404 The file must be readable, and not a directory.
405 If we find one, return its full filename; otherwise, return #f.
406 If FILENAME is absolute, return it unchanged.
407 If given, EXTENSIONS is a list of strings; for each directory
408 in PATH, we search for FILENAME concatenated with each EXTENSION. */
409 SCM_DEFINE (scm_search_path, "search-path", 2, 0, 1,
410 (SCM path, SCM filename, SCM rest),
411 "Search @var{path} for a directory containing a file named\n"
412 "@var{filename}. The file must be readable, and not a directory.\n"
413 "If we find one, return its full filename; otherwise, return\n"
414 "@code{#f}. If @var{filename} is absolute, return it unchanged.\n"
415 "If given, @var{extensions} is a list of strings; for each\n"
416 "directory in @var{path}, we search for @var{filename}\n"
417 "concatenated with each @var{extension}.")
418 #define FUNC_NAME s_scm_search_path
419 {
420 struct stringbuf buf;
421 char *filename_chars;
422 size_t filename_len;
423 SCM extensions, require_exts;
424 SCM result = SCM_BOOL_F;
425
426 if (SCM_UNBNDP (rest) || scm_is_null (rest))
427 {
428 /* Called either by Scheme code that didn't provide the optional
429 arguments, or C code that used the Guile 1.8 signature (2 required,
430 1 optional arg) and passed '() or nothing as the EXTENSIONS
431 argument. */
432 extensions = SCM_EOL;
433 require_exts = SCM_UNDEFINED;
434 }
435 else
436 {
437 if (scm_is_null (SCM_CAR (rest)) || scm_is_pair (SCM_CAR (rest)))
438 {
439 /* Called by Scheme code written for 1.9. */
440 extensions = SCM_CAR (rest);
441 if (scm_is_null (SCM_CDR (rest)))
442 require_exts = SCM_UNDEFINED;
443 else
444 {
445 require_exts = SCM_CADR (rest);
446 if (SCM_UNLIKELY (!scm_is_null (SCM_CDDR (rest))))
447 scm_wrong_num_args (scm_from_locale_string (FUNC_NAME));
448 }
449 }
450 else
451 {
452 /* Called by C code that uses the 1.8 signature, i.e., which
453 expects the 3rd argument to be EXTENSIONS. */
454 extensions = rest;
455 require_exts = SCM_UNDEFINED;
456 }
457 }
458
459 if (SCM_UNBNDP (extensions))
460 extensions = SCM_EOL;
461
462 SCM_VALIDATE_LIST (3, extensions);
463
464 if (SCM_UNBNDP (require_exts))
465 require_exts = SCM_BOOL_F;
466
467 scm_dynwind_begin (0);
468
469 filename_chars = scm_to_locale_string (filename);
470 filename_len = strlen (filename_chars);
471 scm_dynwind_free (filename_chars);
472
473 /* If FILENAME is absolute, return it unchanged. */
474 #ifdef __MINGW32__
475 if (((filename_len >= 1) &&
476 (filename_chars[0] == '/' || filename_chars[0] == '\\')) ||
477 ((filename_len >= 3) && filename_chars[1] == ':' &&
478 ((filename_chars[0] >= 'a' && filename_chars[0] <= 'z') ||
479 (filename_chars[0] >= 'A' && filename_chars[0] <= 'Z')) &&
480 (filename_chars[2] == '/' || filename_chars[2] == '\\')))
481 #else
482 if (filename_len >= 1 && filename_chars[0] == '/')
483 #endif
484 {
485 SCM res = filename;
486 if (scm_is_true (require_exts) &&
487 !scm_c_string_has_an_ext (filename_chars, filename_len,
488 extensions))
489 res = SCM_BOOL_F;
490
491 scm_dynwind_end ();
492 return res;
493 }
494
495 /* If FILENAME has an extension, don't try to add EXTENSIONS to it. */
496 {
497 char *endp;
498
499 for (endp = filename_chars + filename_len - 1;
500 endp >= filename_chars;
501 endp--)
502 {
503 if (*endp == '.')
504 {
505 if (scm_is_true (require_exts) &&
506 !scm_c_string_has_an_ext (filename_chars, filename_len,
507 extensions))
508 {
509 /* This filename has an extension, but not one of the right
510 ones... */
511 scm_dynwind_end ();
512 return SCM_BOOL_F;
513 }
514 /* This filename already has an extension, so cancel the
515 list of extensions. */
516 extensions = SCM_EOL;
517 break;
518 }
519 #ifdef __MINGW32__
520 else if (*endp == '/' || *endp == '\\')
521 #else
522 else if (*endp == '/')
523 #endif
524 /* This filename has no extension, so keep the current list
525 of extensions. */
526 break;
527 }
528 }
529
530 /* This simplifies the loop below a bit.
531 */
532 if (scm_is_null (extensions))
533 extensions = scm_listofnullstr;
534
535 buf.buf_len = 512;
536 buf.buf = scm_malloc (buf.buf_len);
537 scm_dynwind_unwind_handler (stringbuf_free, &buf, SCM_F_WIND_EXPLICITLY);
538
539 /* Try every path element.
540 */
541 for (; scm_is_pair (path); path = SCM_CDR (path))
542 {
543 SCM dir = SCM_CAR (path);
544 SCM exts;
545 size_t sans_ext_len;
546
547 buf.ptr = buf.buf;
548 stringbuf_cat_locale_string (&buf, dir);
549
550 /* Concatenate the path name and the filename. */
551
552 #ifdef __MINGW32__
553 if ((buf.ptr > buf.buf) && (buf.ptr[-1] != '/') && (buf.ptr[-1] != '\\'))
554 #else
555 if ((buf.ptr > buf.buf) && (buf.ptr[-1] != '/'))
556 #endif
557 stringbuf_cat (&buf, "/");
558
559 stringbuf_cat (&buf, filename_chars);
560 sans_ext_len = buf.ptr - buf.buf;
561
562 /* Try every extension. */
563 for (exts = extensions; scm_is_pair (exts); exts = SCM_CDR (exts))
564 {
565 SCM ext = SCM_CAR (exts);
566 struct stat mode;
567
568 buf.ptr = buf.buf + sans_ext_len;
569 stringbuf_cat_locale_string (&buf, ext);
570
571 /* If the file exists at all, we should return it. If the
572 file is inaccessible, then that's an error. */
573
574 if (stat (buf.buf, &mode) == 0
575 && ! (mode.st_mode & S_IFDIR))
576 {
577 result = scm_from_locale_string (buf.buf);
578 goto end;
579 }
580 }
581
582 if (!SCM_NULL_OR_NIL_P (exts))
583 scm_wrong_type_arg_msg (NULL, 0, extensions, "proper list");
584 }
585
586 if (!SCM_NULL_OR_NIL_P (path))
587 scm_wrong_type_arg_msg (NULL, 0, path, "proper list");
588
589 end:
590 scm_dynwind_end ();
591 return result;
592 }
593 #undef FUNC_NAME
594
595
596 /* Search %load-path for a directory containing a file named FILENAME.
597 The file must be readable, and not a directory.
598 If we find one, return its full filename; otherwise, return #f.
599 If FILENAME is absolute, return it unchanged. */
600 SCM_DEFINE (scm_sys_search_load_path, "%search-load-path", 1, 0, 0,
601 (SCM filename),
602 "Search @var{%load-path} for the file named @var{filename},\n"
603 "which must be readable by the current user. If @var{filename}\n"
604 "is found in the list of paths to search or is an absolute\n"
605 "pathname, return its full pathname. Otherwise, return\n"
606 "@code{#f}. Filenames may have any of the optional extensions\n"
607 "in the @code{%load-extensions} list; @code{%search-load-path}\n"
608 "will try each extension automatically.")
609 #define FUNC_NAME s_scm_sys_search_load_path
610 {
611 SCM path = *scm_loc_load_path;
612 SCM exts = *scm_loc_load_extensions;
613 SCM_VALIDATE_STRING (1, filename);
614
615 if (scm_ilength (path) < 0)
616 SCM_MISC_ERROR ("%load-path is not a proper list", SCM_EOL);
617 if (scm_ilength (exts) < 0)
618 SCM_MISC_ERROR ("%load-extension list is not a proper list", SCM_EOL);
619 return scm_search_path (path, filename, exts);
620 }
621 #undef FUNC_NAME
622
623
624 /* Return true if COMPILED_FILENAME is newer than source file
625 FULL_FILENAME, false otherwise. Also return false if one of the
626 files cannot be stat'd. */
627 static int
628 compiled_is_fresh (SCM full_filename, SCM compiled_filename)
629 {
630 char *source, *compiled;
631 struct stat stat_source, stat_compiled;
632 int compiled_is_newer;
633
634 source = scm_to_locale_string (full_filename);
635 compiled = scm_to_locale_string (compiled_filename);
636
637 if (stat (source, &stat_source) == 0
638 && stat (compiled, &stat_compiled) == 0)
639 {
640 struct timespec source_mtime, compiled_mtime;
641
642 source_mtime = get_stat_mtime (&stat_source);
643 compiled_mtime = get_stat_mtime (&stat_compiled);
644
645 if (source_mtime.tv_sec < compiled_mtime.tv_sec
646 || (source_mtime.tv_sec == compiled_mtime.tv_sec
647 && source_mtime.tv_nsec <= compiled_mtime.tv_nsec))
648 compiled_is_newer = 1;
649 else
650 {
651 compiled_is_newer = 0;
652 scm_puts (";;; note: source file ", scm_current_error_port ());
653 scm_puts (source, scm_current_error_port ());
654 scm_puts ("\n;;; newer than compiled ", scm_current_error_port ());
655 scm_puts (compiled, scm_current_error_port ());
656 scm_puts ("\n", scm_current_error_port ());
657 }
658 }
659 else
660 /* At least one of the files isn't accessible. */
661 compiled_is_newer = 0;
662
663 free (source);
664 free (compiled);
665
666 return compiled_is_newer;
667 }
668
669 SCM_KEYWORD (kw_env, "env");
670
671 static SCM
672 do_try_auto_compile (void *data)
673 {
674 SCM source = PTR2SCM (data);
675 SCM comp_mod, compile_file;
676
677 scm_puts (";;; compiling ", scm_current_error_port ());
678 scm_display (source, scm_current_error_port ());
679 scm_newline (scm_current_error_port ());
680
681 comp_mod = scm_c_resolve_module ("system base compile");
682 compile_file = scm_module_variable
683 (comp_mod, scm_from_latin1_symbol ("compile-file"));
684
685 if (scm_is_true (compile_file))
686 {
687 /* Auto-compile in the context of the current module. */
688 SCM res = scm_call_3 (scm_variable_ref (compile_file), source,
689 kw_env, scm_current_module ());
690 scm_puts (";;; compiled ", scm_current_error_port ());
691 scm_display (res, scm_current_error_port ());
692 scm_newline (scm_current_error_port ());
693 return res;
694 }
695 else
696 {
697 scm_puts (";;; it seems ", scm_current_error_port ());
698 scm_display (source, scm_current_error_port ());
699 scm_puts ("\n;;; is part of the compiler; skipping auto-compilation\n",
700 scm_current_error_port ());
701 return SCM_BOOL_F;
702 }
703 }
704
705 static SCM
706 auto_compile_catch_handler (void *data, SCM tag, SCM throw_args)
707 {
708 SCM source = PTR2SCM (data);
709 scm_puts (";;; WARNING: compilation of ", scm_current_error_port ());
710 scm_display (source, scm_current_error_port ());
711 scm_puts (" failed:\n", scm_current_error_port ());
712 scm_puts (";;; key ", scm_current_error_port ());
713 scm_write (tag, scm_current_error_port ());
714 scm_puts (", throw args ", scm_current_error_port ());
715 scm_write (throw_args, scm_current_error_port ());
716 scm_newline (scm_current_error_port ());
717 return SCM_BOOL_F;
718 }
719
720 SCM_DEFINE (scm_sys_warn_auto_compilation_enabled, "%warn-auto-compilation-enabled", 0, 0, 0,
721 (void), "")
722 #define FUNC_NAME s_scm_sys_warn_auto_compilation_enabled
723 {
724 static int message_shown = 0;
725
726 if (!message_shown)
727 {
728 scm_puts (";;; note: auto-compilation is enabled, set GUILE_AUTO_COMPILE=0\n"
729 ";;; or pass the --no-auto-compile argument to disable.\n",
730 scm_current_error_port ());
731 message_shown = 1;
732 }
733
734 return SCM_UNSPECIFIED;
735 }
736 #undef FUNC_NAME
737
738 static SCM
739 scm_try_auto_compile (SCM source)
740 {
741 if (scm_is_false (*scm_loc_load_should_auto_compile))
742 return SCM_BOOL_F;
743
744 scm_sys_warn_auto_compilation_enabled ();
745 return scm_c_catch (SCM_BOOL_T,
746 do_try_auto_compile,
747 SCM2PTR (source),
748 auto_compile_catch_handler,
749 SCM2PTR (source),
750 NULL, NULL);
751 }
752
753 SCM_DEFINE (scm_primitive_load_path, "primitive-load-path", 0, 0, 1,
754 (SCM args),
755 "Search @var{%load-path} for the file named @var{filename} and\n"
756 "load it into the top-level environment. If @var{filename} is a\n"
757 "relative pathname and is not found in the list of search paths,\n"
758 "an error is signalled, unless the optional argument\n"
759 "@var{exception_on_not_found} is @code{#f}, in which case\n"
760 "@code{#f} is returned instead.")
761 #define FUNC_NAME s_scm_primitive_load_path
762 {
763 SCM filename, exception_on_not_found;
764 SCM full_filename, compiled_filename;
765 int compiled_is_fallback = 0;
766
767 if (scm_is_string (args))
768 {
769 /* C code written for 1.8 and earlier expects this function to take a
770 single argument (the file name). */
771 filename = args;
772 exception_on_not_found = SCM_UNDEFINED;
773 }
774 else
775 {
776 /* Starting from 1.9, this function takes 1 required and 1 optional
777 argument. */
778 long len;
779
780 SCM_VALIDATE_LIST_COPYLEN (SCM_ARG1, args, len);
781 if (len < 1 || len > 2)
782 scm_error_num_args_subr (FUNC_NAME);
783
784 filename = SCM_CAR (args);
785 SCM_VALIDATE_STRING (SCM_ARG1, filename);
786
787 exception_on_not_found = len > 1 ? SCM_CADR (args) : SCM_UNDEFINED;
788 }
789
790 if (SCM_UNBNDP (exception_on_not_found))
791 exception_on_not_found = SCM_BOOL_T;
792
793 full_filename = scm_sys_search_load_path (filename);
794 if (scm_is_string (full_filename))
795 full_filename = scm_canonicalize_path (full_filename);
796
797 compiled_filename =
798 scm_search_path (*scm_loc_load_compiled_path,
799 filename,
800 scm_list_2 (*scm_loc_load_compiled_extensions,
801 SCM_BOOL_T));
802
803 if (scm_is_false (compiled_filename)
804 && scm_is_true (full_filename)
805 && scm_is_true (*scm_loc_compile_fallback_path)
806 && scm_is_pair (*scm_loc_load_compiled_extensions)
807 && scm_is_string (scm_car (*scm_loc_load_compiled_extensions)))
808 {
809 SCM fallback = scm_string_append
810 (scm_list_3 (*scm_loc_compile_fallback_path,
811 full_filename,
812 scm_car (*scm_loc_load_compiled_extensions)));
813 if (scm_is_true (scm_stat (fallback, SCM_BOOL_F)))
814 {
815 compiled_filename = fallback;
816 compiled_is_fallback = 1;
817 }
818 }
819
820 if (scm_is_false (full_filename) && scm_is_false (compiled_filename))
821 {
822 if (scm_is_true (exception_on_not_found))
823 SCM_MISC_ERROR ("Unable to find file ~S in load path",
824 scm_list_1 (filename));
825 else
826 return SCM_BOOL_F;
827 }
828
829 if (scm_is_false (full_filename)
830 || (scm_is_true (compiled_filename)
831 && compiled_is_fresh (full_filename, compiled_filename)))
832 return scm_load_compiled_with_vm (compiled_filename);
833
834 /* Perhaps there was the installed .go that was stale, but our fallback is
835 fresh. Let's try that. Duplicating code, but perhaps that's OK. */
836
837 if (!compiled_is_fallback
838 && scm_is_true (*scm_loc_compile_fallback_path)
839 && scm_is_pair (*scm_loc_load_compiled_extensions)
840 && scm_is_string (scm_car (*scm_loc_load_compiled_extensions)))
841 {
842 SCM fallback = scm_string_append
843 (scm_list_3 (*scm_loc_compile_fallback_path,
844 full_filename,
845 scm_car (*scm_loc_load_compiled_extensions)));
846 if (scm_is_true (scm_stat (fallback, SCM_BOOL_F))
847 && compiled_is_fresh (full_filename, fallback))
848 {
849 scm_puts (";;; found fresh local cache at ", scm_current_error_port ());
850 scm_display (fallback, scm_current_error_port ());
851 scm_newline (scm_current_error_port ());
852 return scm_load_compiled_with_vm (fallback);
853 }
854 }
855
856 /* Otherwise, we bottom out here. */
857 {
858 SCM freshly_compiled = scm_try_auto_compile (full_filename);
859
860 if (scm_is_true (freshly_compiled))
861 return scm_load_compiled_with_vm (freshly_compiled);
862 else
863 return scm_primitive_load (full_filename);
864 }
865 }
866 #undef FUNC_NAME
867
868 SCM
869 scm_c_primitive_load_path (const char *filename)
870 {
871 return scm_primitive_load_path (scm_from_locale_string (filename));
872 }
873
874 void
875 scm_init_eval_in_scheme (void)
876 {
877 SCM eval_scm, eval_go;
878 eval_scm = scm_search_path (*scm_loc_load_path,
879 scm_from_locale_string ("ice-9/eval.scm"),
880 SCM_EOL);
881 eval_go = scm_search_path (*scm_loc_load_compiled_path,
882 scm_from_locale_string ("ice-9/eval.go"),
883 SCM_EOL);
884
885 if (scm_is_true (eval_scm) && scm_is_true (eval_go)
886 && compiled_is_fresh (eval_scm, eval_go))
887 scm_load_compiled_with_vm (eval_go);
888 else
889 /* if we have no eval.go, we shouldn't load any compiled code at all */
890 *scm_loc_load_compiled_path = SCM_EOL;
891 }
892
893 \f
894 /* Information about the build environment. */
895
896 SCM_VARIABLE_INIT (sys_host_type, "%host-type",
897 scm_from_locale_string (HOST_TYPE));
898
899
900 /* Initialize the scheme variable %guile-build-info, based on data
901 provided by the Makefile, via libpath.h. */
902 static void
903 init_build_info ()
904 {
905 static struct { char *name; char *value; } info[] = SCM_BUILD_INFO;
906 SCM *loc = SCM_VARIABLE_LOC (scm_c_define ("%guile-build-info", SCM_EOL));
907 unsigned long i;
908
909 for (i = 0; i < (sizeof (info) / sizeof (info[0])); i++)
910 {
911 SCM key = scm_from_locale_symbol (info[i].name);
912 SCM val = scm_from_locale_string (info[i].value);
913 *loc = scm_acons (key, val, *loc);
914 }
915 }
916
917 \f
918 void
919 scm_init_load ()
920 {
921 scm_listofnullstr = scm_list_1 (scm_nullstr);
922 scm_loc_load_path = SCM_VARIABLE_LOC (scm_c_define ("%load-path", SCM_EOL));
923 scm_loc_load_extensions
924 = SCM_VARIABLE_LOC (scm_c_define ("%load-extensions",
925 scm_list_2 (scm_from_locale_string (".scm"),
926 scm_nullstr)));
927 scm_loc_load_compiled_path
928 = SCM_VARIABLE_LOC (scm_c_define ("%load-compiled-path", SCM_EOL));
929 scm_loc_load_compiled_extensions
930 = SCM_VARIABLE_LOC (scm_c_define ("%load-compiled-extensions",
931 scm_list_1 (scm_from_locale_string (".go"))));
932 scm_loc_load_hook = SCM_VARIABLE_LOC (scm_c_define ("%load-hook", SCM_BOOL_F));
933
934 scm_loc_compile_fallback_path
935 = SCM_VARIABLE_LOC (scm_c_define ("%compile-fallback-path", SCM_BOOL_F));
936 scm_loc_load_should_auto_compile
937 = SCM_VARIABLE_LOC (scm_c_define ("%load-should-auto-compile", SCM_BOOL_F));
938
939 the_reader = scm_make_fluid ();
940 scm_fluid_set_x (the_reader, SCM_BOOL_F);
941 scm_c_define("current-reader", the_reader);
942
943 scm_c_define ("load-compiled",
944 scm_c_make_gsubr ("load-compiled/vm", 1, 0, 0,
945 scm_load_compiled_with_vm));
946
947 init_build_info ();
948
949 #include "libguile/load.x"
950 }
951
952 void
953 scm_init_load_should_auto_compile ()
954 {
955 *scm_loc_load_should_auto_compile =
956 scm_from_bool (scm_getenv_int ("GUILE_AUTO_COMPILE", 1));
957 }
958
959
960
961 /*
962 Local Variables:
963 c-file-style: "gnu"
964 End:
965 */