1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (guix discovery)
20 #:use-module (guix i18n)
21 #:use-module (guix modules)
22 #:use-module (guix combinators)
23 #:use-module (guix build syscalls)
24 #:use-module (srfi srfi-1)
25 #:use-module (ice-9 match)
26 #:use-module (ice-9 vlist)
27 #:use-module (ice-9 ftw)
28 #:export (scheme-files
33 fold-module-public-variables
34 fold-module-public-variables*))
38 ;;; This module provides tools to discover Guile modules and the variables
43 (define* (scheme-files directory)
44 "Return the list of Scheme files found under DIRECTORY, recursively. The
45 returned list is sorted in alphabetical order. Return the empty list if
46 DIRECTORY is not accessible."
47 (define (entry-type name properties)
48 (match (assoc-ref properties 'type)
50 (stat:type (lstat name)))
54 ;; Use 'scandir*' so we can avoid an extra 'lstat' for each entry, as
55 ;; opposed to Guile's 'scandir' or 'file-system-fold'.
56 (fold-right (lambda (entry result)
63 (let ((absolute (string-append directory "/" name)))
64 (case (entry-type absolute properties)
66 (append (scheme-files absolute) result))
68 (if (string-suffix? ".scm" name)
69 (cons absolute result)
72 (cond ((string-suffix? ".scm" name)
73 (cons absolute result))
78 ((= stat:type 'directory)
79 (append (scheme-files absolute)
89 (let ((errno (system-error-errno args)))
90 (unless (= errno ENOENT)
91 (format (current-error-port) ;XXX
92 (G_ "cannot access `~a': ~a~%")
93 directory (strerror errno)))
96 (define* (scheme-modules directory #:optional sub-directory
97 #:key (warn (const #f)))
98 "Return the list of Scheme modules available under DIRECTORY.
99 Optionally, narrow the search to SUB-DIRECTORY.
101 WARN is called when a module could not be loaded. It is passed the module
102 name and the exception key and arguments."
104 (string-length directory))
106 (filter-map (lambda (file)
107 (let* ((file (substring file prefix-len))
108 (module (file-name->module-name file)))
111 (resolve-interface module))
113 ;; Report the error, but keep going.
116 (scheme-files (if sub-directory
117 (string-append directory "/" sub-directory)
120 (define* (scheme-modules* directory #:optional sub-directory)
121 "Return the list of module names found under SUB-DIRECTORY in DIRECTORY.
122 This is a source-only variant that does not try to load files."
123 (let ((prefix (string-length directory)))
125 (file-name->module-name (string-drop file prefix)))
126 (scheme-files (if sub-directory
127 (string-append directory "/" sub-directory)
130 (define* (fold-modules proc init path #:key (warn (const #f)))
131 "Fold over all the Scheme modules present in PATH, a list of directories.
132 Call (PROC MODULE RESULT) for each module that is found."
133 (fold (lambda (spec result)
135 ((? string? directory)
136 (fold proc result (scheme-modules directory #:warn warn)))
137 ((directory . sub-directory)
139 (scheme-modules directory sub-directory
144 (define* (all-modules path #:key (warn (const #f)))
145 "Return the list of package modules found in PATH, a list of directories to
146 search. Entries in PATH can be directory names (strings) or (DIRECTORY
147 . SUB-DIRECTORY) pairs, in which case modules are searched for beneath
149 (fold-modules cons '() path #:warn warn))
151 (define (fold-module-public-variables* proc init modules)
152 "Call (PROC MODULE SYMBOL VARIABLE) for each variable exported by one of MODULES,
153 using INIT as the initial value of RESULT. It is guaranteed to never traverse
154 the same object twice."
155 ;; Here SEEN is populated by variables; if two different variables refer to
156 ;; the same object, we still let them through.
157 (identity ;discard second return value
158 (fold2 (lambda (module result seen)
159 (fold2 (lambda (sym+var result seen)
162 (if (not (vhash-assq var seen))
163 (values (proc module sym var result)
164 (vhash-consq var #t seen))
165 (values result seen)))))
168 (module-map cons module)))
173 (define (fold-module-public-variables proc init modules)
174 "Call (PROC OBJECT RESULT) for each variable exported by one of MODULES,
175 using INIT as the initial value of RESULT. It is guaranteed to never traverse
176 the same object twice."
177 ;; Note: here SEEN is populated by objects, not by variables.
178 (identity ; discard second return value
179 (fold2 (lambda (module result seen)
180 (fold2 (lambda (var result seen)
181 (if (not (vhash-assq var seen))
182 (values (proc var result)
183 (vhash-consq var #t seen))
184 (values result seen)))
187 (module-map (lambda (sym var)
188 (false-if-exception (variable-ref var)))
194 ;;; discovery.scm ends here