c9f1861c2c110d97ea50daf5daa392e151695483
[sbcl.git] / src / code / module.lisp
1 ;;;; REQUIRE, PROVIDE, and friends
2 ;;;;
3 ;;;; Officially these are deprecated, but in practice they're probably
4 ;;;; even less likely to actually go away than there is to ever be
5 ;;;; another revision of the standard.
6
7 ;;;; This software is part of the SBCL system. See the README file for
8 ;;;; more information.
9 ;;;;
10 ;;;; This software is derived from the CMU CL system, which was
11 ;;;; written at Carnegie Mellon University and released into the
12 ;;;; public domain. The software is in the public domain and is
13 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
14 ;;;; files for more information.
15
16 (in-package "SB!IMPL")
17 \f
18 ;;;; exported specials
19
20 (defvar *modules* ()
21   #!+sb-doc
22   "This is a list of module names that have been loaded into Lisp so far.
23    It is used by PROVIDE and REQUIRE.")
24
25 (defvar *module-provider-functions* '(module-provide-contrib)
26   "See function documentation for REQUIRE")
27
28 \f
29 ;;;; PROVIDE and REQUIRE
30
31 (defun provide (module-name)
32   #!+sb-doc
33   "Adds a new module name to *MODULES* indicating that it has been loaded.
34    Module-name is a string designator"
35   (pushnew (string module-name) *modules* :test #'string=)
36   t)
37
38 (defun require (module-name &optional pathnames)
39   #!+sb-doc
40   "Loads a module, unless it already has been loaded. PATHNAMES, if supplied,
41    is a designator for a list of pathnames to be loaded if the module
42    needs to be. If PATHNAMES is not supplied, functions from the list
43    *MODULE-PROVIDER-FUNCTIONS* are called in order with MODULE-NAME
44    as an argument, until one of them returns non-NIL.  User code is
45    responsible for calling PROVIDE to indicate a successful load of the
46    module."
47   (let ((saved-modules (copy-list *modules*)))
48     (unless (member (string module-name) *modules* :test #'string=)
49       (cond (pathnames
50              (unless (listp pathnames) (setf pathnames (list pathnames)))
51              ;; ambiguity in standard: should we try all pathnames in the
52              ;; list, or should we stop as soon as one of them calls PROVIDE?
53              (dolist (ele pathnames t)
54                (load ele)))
55             (t
56              (unless (some (lambda (p) (funcall p module-name))
57                            *module-provider-functions*)
58                (error "Don't know how to load ~A" module-name)))))
59     (set-difference *modules* saved-modules)))
60 \f
61 ;;;; miscellany
62
63 (defun module-provide-contrib (name)
64   "Stringify and downcase NAME, then attempt to load the file
65    $SBCL_HOME/name/name"
66   (let* ((filesys-name (string-downcase (string name)))
67          (unadorned-path
68           (merge-pathnames
69            (make-pathname :directory (list :relative filesys-name)
70                           :name filesys-name)
71            (truename (posix-getenv "SBCL_HOME"))))
72          (fasl-path (merge-pathnames
73                      (make-pathname :type *fasl-file-type*)
74                      unadorned-path))
75          (lisp-path (merge-pathnames (make-pathname :type "lisp")
76                                      unadorned-path)))
77     ;; KLUDGE: there's a race condition here; the file we probe could
78     ;; be removed by the time we get round to trying to load it.
79     ;; Maybe factor out the logic in the LOAD guesser as to which file
80     ;; was meant, so that we can use it here on open streams instead?
81     (when (or (probe-file unadorned-path)
82               (probe-file fasl-path)
83               (probe-file lisp-path))
84       (load unadorned-path)
85       t)))