2016-09-04 23:41:53 +02:00
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
2018-03-27 14:00:48 +02:00
|
|
|
;;; Copyright © 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
|
2016-09-04 23:41:53 +02:00
|
|
|
;;;
|
|
|
|
;;; This file is part of GNU Guix.
|
|
|
|
;;;
|
|
|
|
;;; GNU Guix is free software; you can redistribute it and/or modify it
|
|
|
|
;;; under the terms of the GNU General Public License as published by
|
|
|
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
;;; your option) any later version.
|
|
|
|
;;;
|
|
|
|
;;; GNU Guix is distributed in the hope that it will be useful, but
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
(define-module (guix modules)
|
Add (guix memoization).
* guix/combinators.scm (memoize): Remove.
* guix/memoization.scm: New file.
* Makefile.am (MODULES): Add it.
* gnu/packages.scm, gnu/packages/bootstrap.scm,
guix/build-system/gnu.scm, guix/build-system/python.scm,
guix/derivations.scm, guix/gnu-maintenance.scm,
guix/import/cran.scm, guix/import/elpa.scm,
guix/modules.scm, guix/scripts/build.scm,
guix/scripts/graph.scm, guix/scripts/lint.scm,
guix/store.scm, guix/utils.scm: Adjust imports accordingly.
2017-01-28 16:33:57 +01:00
|
|
|
#:use-module (guix memoization)
|
2016-09-04 23:41:53 +02:00
|
|
|
#:use-module (guix sets)
|
|
|
|
#:use-module (srfi srfi-26)
|
2017-05-25 14:19:51 +02:00
|
|
|
#:use-module (srfi srfi-34)
|
|
|
|
#:use-module (srfi srfi-35)
|
2016-09-04 23:41:53 +02:00
|
|
|
#:use-module (ice-9 match)
|
2017-05-25 14:19:51 +02:00
|
|
|
#:export (missing-dependency-error?
|
|
|
|
missing-dependency-module
|
2018-04-03 23:33:39 +02:00
|
|
|
missing-dependency-search-path
|
2017-05-25 14:19:51 +02:00
|
|
|
|
2017-10-13 22:16:46 +02:00
|
|
|
file-name->module-name
|
|
|
|
module-name->file-name
|
|
|
|
|
2018-03-27 14:00:48 +02:00
|
|
|
source-module-dependencies
|
2017-05-25 14:19:51 +02:00
|
|
|
source-module-closure
|
2016-09-04 23:41:53 +02:00
|
|
|
live-module-closure
|
|
|
|
guix-module-name?))
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;;;
|
|
|
|
;;; This module provides introspection tools for Guile modules at the source
|
|
|
|
;;; level. Namely, it allows you to determine the closure of a module; it
|
|
|
|
;;; does so just by reading the 'define-module' clause of the module and its
|
|
|
|
;;; dependencies. This is primarily useful as an argument to
|
|
|
|
;;; 'with-imported-modules'.
|
|
|
|
;;;
|
|
|
|
;;; Code:
|
|
|
|
|
2017-05-25 14:19:51 +02:00
|
|
|
;; The error corresponding to a missing module.
|
|
|
|
(define-condition-type &missing-dependency-error &error
|
|
|
|
missing-dependency-error?
|
2018-04-03 23:33:39 +02:00
|
|
|
(module missing-dependency-module)
|
|
|
|
(search-path missing-dependency-search-path))
|
2017-05-25 14:19:51 +02:00
|
|
|
|
2016-09-04 23:41:53 +02:00
|
|
|
(define (colon-symbol? obj)
|
|
|
|
"Return true if OBJ is a symbol that starts with a colon."
|
|
|
|
(and (symbol? obj)
|
|
|
|
(string-prefix? ":" (symbol->string obj))))
|
|
|
|
|
|
|
|
(define (colon-symbol->keyword symbol)
|
|
|
|
"Convert SYMBOL to a keyword after stripping its initial ':'."
|
|
|
|
(symbol->keyword
|
|
|
|
(string->symbol (string-drop (symbol->string symbol) 1))))
|
|
|
|
|
|
|
|
(define (extract-dependencies clauses)
|
|
|
|
"Return the list of modules imported according to the given 'define-module'
|
|
|
|
CLAUSES."
|
|
|
|
(let loop ((clauses clauses)
|
|
|
|
(result '()))
|
|
|
|
(match clauses
|
|
|
|
(()
|
|
|
|
(reverse result))
|
|
|
|
((#:use-module (module (or #:select #:hide #:prefix #:renamer) _)
|
|
|
|
rest ...)
|
|
|
|
(loop rest (cons module result)))
|
|
|
|
((#:use-module module rest ...)
|
|
|
|
(loop rest (cons module result)))
|
|
|
|
((#:autoload module _ rest ...)
|
|
|
|
(loop rest (cons module result)))
|
|
|
|
(((or #:export #:re-export #:export-syntax #:re-export-syntax
|
|
|
|
#:replace #:version)
|
|
|
|
_ rest ...)
|
|
|
|
(loop rest result))
|
|
|
|
(((or #:pure #:no-backtrace) rest ...)
|
|
|
|
(loop rest result))
|
|
|
|
(((? colon-symbol? symbol) rest ...)
|
|
|
|
(loop (cons (colon-symbol->keyword symbol) rest)
|
|
|
|
result)))))
|
|
|
|
|
|
|
|
(define module-file-dependencies
|
2017-01-28 17:09:34 +01:00
|
|
|
(mlambda (file)
|
|
|
|
"Return the list of the names of modules that the Guile module in FILE
|
2016-09-04 23:41:53 +02:00
|
|
|
depends on."
|
2017-01-28 17:09:34 +01:00
|
|
|
(call-with-input-file file
|
|
|
|
(lambda (port)
|
|
|
|
(match (read port)
|
|
|
|
(('define-module name clauses ...)
|
|
|
|
(extract-dependencies clauses))
|
|
|
|
;; XXX: R6RS 'library' form is ignored.
|
|
|
|
(_
|
|
|
|
'()))))))
|
2016-09-04 23:41:53 +02:00
|
|
|
|
2017-10-13 22:16:46 +02:00
|
|
|
(define file-name->module-name
|
|
|
|
(let ((not-slash (char-set-complement (char-set #\/))))
|
|
|
|
(lambda (file)
|
|
|
|
"Return the module name (a list of symbols) corresponding to FILE."
|
|
|
|
(map string->symbol
|
|
|
|
(string-tokenize (string-drop-right file 4) not-slash)))))
|
|
|
|
|
2016-09-04 23:41:53 +02:00
|
|
|
(define (module-name->file-name module)
|
|
|
|
"Return the file name for MODULE."
|
|
|
|
(string-append (string-join (map symbol->string module) "/")
|
|
|
|
".scm"))
|
|
|
|
|
|
|
|
(define (guix-module-name? name)
|
|
|
|
"Return true if NAME (a list of symbols) denotes a Guix or GuixSD module."
|
|
|
|
(match name
|
|
|
|
(('guix _ ...) #t)
|
|
|
|
(('gnu _ ...) #t)
|
|
|
|
(_ #f)))
|
|
|
|
|
2017-05-19 10:45:12 +02:00
|
|
|
(define %source-less-modules
|
|
|
|
;; These are modules that have no corresponding source files or a source
|
|
|
|
;; file different from what you'd expect.
|
|
|
|
'((system syntax) ;2.0, defined in boot-9
|
|
|
|
(ice-9 ports internal) ;2.2, defined in (ice-9 ports)
|
|
|
|
(system syntax internal))) ;2.2, defined in boot-9
|
|
|
|
|
2016-09-04 23:41:53 +02:00
|
|
|
(define* (source-module-dependencies module #:optional (load-path %load-path))
|
|
|
|
"Return the modules used by MODULE by looking at its source code."
|
2017-05-19 10:45:12 +02:00
|
|
|
(if (member module %source-less-modules)
|
2016-09-04 23:41:53 +02:00
|
|
|
'()
|
2017-05-25 14:19:51 +02:00
|
|
|
(match (search-path load-path (module-name->file-name module))
|
|
|
|
((? string? file)
|
|
|
|
(module-file-dependencies file))
|
|
|
|
(#f
|
|
|
|
(raise (condition (&missing-dependency-error
|
2018-04-03 23:33:39 +02:00
|
|
|
(module module)
|
|
|
|
(search-path load-path))))))))
|
2016-09-04 23:41:53 +02:00
|
|
|
|
|
|
|
(define* (module-closure modules
|
|
|
|
#:key
|
|
|
|
(select? guix-module-name?)
|
|
|
|
(dependencies source-module-dependencies))
|
|
|
|
"Return the closure of MODULES, calling DEPENDENCIES to determine the list
|
|
|
|
of modules used by a given module. MODULES and the result are a list of Guile
|
|
|
|
module names. Only modules that match SELECT? are considered."
|
|
|
|
(let loop ((modules modules)
|
|
|
|
(result '())
|
|
|
|
(visited (set)))
|
|
|
|
(match modules
|
|
|
|
(()
|
|
|
|
(reverse result))
|
|
|
|
((module rest ...)
|
|
|
|
(cond ((set-contains? visited module)
|
|
|
|
(loop rest result visited))
|
|
|
|
((select? module)
|
|
|
|
(loop (append (dependencies module) rest)
|
|
|
|
(cons module result)
|
|
|
|
(set-insert module visited)))
|
|
|
|
(else
|
|
|
|
(loop rest result visited)))))))
|
|
|
|
|
|
|
|
(define* (source-module-closure modules
|
|
|
|
#:optional (load-path %load-path)
|
|
|
|
#:key (select? guix-module-name?))
|
|
|
|
"Return the closure of MODULES by reading 'define-module' forms in their
|
|
|
|
source code. MODULES and the result are a list of Guile module names. Only
|
|
|
|
modules that match SELECT? are considered."
|
|
|
|
(module-closure modules
|
|
|
|
#:dependencies (cut source-module-dependencies <> load-path)
|
|
|
|
#:select? select?))
|
|
|
|
|
|
|
|
(define* (live-module-closure modules
|
|
|
|
#:key (select? guix-module-name?))
|
|
|
|
"Return the closure of MODULES, determined by looking at live (loaded)
|
|
|
|
module information. MODULES and the result are a list of Guile module names.
|
|
|
|
Only modules that match SELECT? are considered."
|
|
|
|
(define (dependencies module)
|
|
|
|
(map module-name
|
|
|
|
(delq the-scm-module (module-uses (resolve-module module)))))
|
|
|
|
|
|
|
|
(module-closure modules
|
|
|
|
#:dependencies dependencies
|
|
|
|
#:select? select?))
|
|
|
|
|
|
|
|
;;; modules.scm ends here
|