1.1--- a/tools/asdf.lisp Thu Jun 15 22:01:40 2023 -0400
1.2+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3@@ -1,13987 +0,0 @@
1.4-;;; -*- mode: Lisp; Base: 10 ; Syntax: ANSI-Common-Lisp ; Package: CL-USER ; buffer-read-only: t; -*-
1.5-;;; This is ASDF 3.3.6: Another System Definition Facility.
1.6-;;;
1.7-;;; Feedback, bug reports, and patches are all welcome:
1.8-;;; please mail to <asdf-devel@common-lisp.net>.
1.9-;;; Note first that the canonical source for ASDF is presently
1.10-;;; <URL:http://common-lisp.net/project/asdf/>.
1.11-;;;
1.12-;;; If you obtained this copy from anywhere else, and you experience
1.13-;;; trouble using it, or find bugs, you may want to check at the
1.14-;;; location above for a more recent version (and for documentation
1.15-;;; and test files, if your copy came without them) before reporting
1.16-;;; bugs. There are usually two "supported" revisions - the git master
1.17-;;; branch is the latest development version, whereas the git release
1.18-;;; branch may be slightly older but is considered `stable'
1.19-
1.20-;;; -- LICENSE START
1.21-;;; (This is the MIT / X Consortium license as taken from
1.22-;;; http://www.opensource.org/licenses/mit-license.html on or about
1.23-;;; Monday; July 13, 2009)
1.24-;;;
1.25-;;; Copyright (c) 2001-2019 Daniel Barlow and contributors
1.26-;;;
1.27-;;; Permission is hereby granted, free of charge, to any person obtaining
1.28-;;; a copy of this software and associated documentation files (the
1.29-;;; "Software"), to deal in the Software without restriction, including
1.30-;;; without limitation the rights to use, copy, modify, merge, publish,
1.31-;;; distribute, sublicense, and/or sell copies of the Software, and to
1.32-;;; permit persons to whom the Software is furnished to do so, subject to
1.33-;;; the following conditions:
1.34-;;;
1.35-;;; The above copyright notice and this permission notice shall be
1.36-;;; included in all copies or substantial portions of the Software.
1.37-;;;
1.38-;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1.39-;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1.40-;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1.41-;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
1.42-;;; LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
1.43-;;; OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
1.44-;;; WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1.45-;;;
1.46-;;; -- LICENSE END
1.47-
1.48-;;; The problem with writing a defsystem replacement is bootstrapping:
1.49-;;; we can't use defsystem to compile it. Hence, all in one file.
1.50-
1.51-#+genera
1.52-(eval-when (:compile-toplevel :load-toplevel :execute)
1.53- (multiple-value-bind (system-major system-minor)
1.54- (sct:get-system-version)
1.55- (multiple-value-bind (is-major is-minor)
1.56- (sct:get-system-version "Intel-Support")
1.57- (unless (or (> system-major 452)
1.58- (and is-major
1.59- (or (> is-major 3)
1.60- (and (= is-major 3) (> is-minor 86)))))
1.61- (error "ASDF requires either System 453 or later or Intel Support 3.87 or later")))))
1.62-;;;; ---------------------------------------------------------------------------
1.63-;;;; ASDF package upgrade, including implementation-dependent magic.
1.64-;;
1.65-;; See https://bugs.launchpad.net/asdf/+bug/485687
1.66-;;
1.67-
1.68-;; CAUTION: The definition of the UIOP/PACKAGE package MUST NOT CHANGE,
1.69-;; NOT NOW, NOT EVER, NOT UNDER ANY CIRCUMSTANCE. NEVER.
1.70-;; ... and the same goes for UIOP/PACKAGE-LOCAL-NICKNAMES.
1.71-;;
1.72-;; The entire point of UIOP/PACKAGE is to address the fact that the CL standard
1.73-;; *leaves it unspecified what happens when a package is redefined incompatibly*.
1.74-;; For instance, SBCL 1.4.2 will signal a full WARNING when this happens,
1.75-;; throwing a wrench in upgrading code with ASDF itself, while continuing to
1.76-;; export old symbols it now shouldn't as it also exports new ones,
1.77-;; causing problems with code that relies on the new/current exports.
1.78-;; CLISP and CCL also exports both sets of symbols, though without any WARNING.
1.79-;; ABCL 1.6.1 will plainly ignore the new definition.
1.80-;; Other implementations may do whatever they want and change their behavior at any time.
1.81-;; ***Using DEFPACKAGE twice with different definitions is nasal-demon territory.***
1.82-;;
1.83-;; Thus we define UIOP/PACKAGE:DEFINE-PACKAGE with which packages can be defined
1.84-;; in an upgrade-friendly way: the new definition is authoritative, and
1.85-;; the package will define and export exactly those symbols in the new definition,
1.86-;; no more and no fewer, whereas it is well-defined what happens to previous symbols.
1.87-;; However, for obvious bootstrap reasons, we cannot use DEFINE-PACKAGE
1.88-;; to define UIOP/PACKAGE itself, only DEFPACKAGE.
1.89-;; Therefore, unlike the other packages in ASDF, UIOP/PACKAGE is immutable,
1.90-;; now and forever. It is frozen for the aeons to come, like the CL package itself,
1.91-;; to the same exact state it was defined at its inception, in ASDF 2.27 in 2013.
1.92-;; The same goes for UIOP/PACKAGE-LOCAL-NICKNAMES, that we use internally.
1.93-;;
1.94-;; If you ever must define new symbols in this file, you can and must
1.95-;; export them from a different package, possibly defined in the same file,
1.96-;; say a package UIOP/PACKAGE* defined at the end of this file with DEFINE-PACKAGE,
1.97-;; that might use :import-from to import the symbols from UIOP/PACKAGE,
1.98-;; if you must somehow define them in UIOP/PACKAGE.
1.99-
1.100-(defpackage :uiop/package ;;; THOU SHALT NOT modify this definition, EVER. See explanations above.
1.101- (:use :common-lisp)
1.102- (:export
1.103- #:find-package* #:find-symbol* #:symbol-call
1.104- #:intern* #:export* #:import* #:shadowing-import* #:shadow* #:make-symbol* #:unintern*
1.105- #:symbol-shadowing-p #:home-package-p
1.106- #:symbol-package-name #:standard-common-lisp-symbol-p
1.107- #:reify-package #:unreify-package #:reify-symbol #:unreify-symbol
1.108- #:nuke-symbol-in-package #:nuke-symbol #:rehome-symbol
1.109- #:ensure-package-unused #:delete-package*
1.110- #:package-names #:packages-from-names #:fresh-package-name #:rename-package-away
1.111- #:package-definition-form #:parse-define-package-form
1.112- #:ensure-package #:define-package
1.113- ))
1.114-
1.115-(in-package :uiop/package)
1.116-
1.117-;;; package local nicknames feature.
1.118-;;; This can't be deferred until common-lisp.lisp, where most such features are set.
1.119-;;; ABCL and CCL already define this feature appropriately.
1.120-;;; Seems to be unconditionally present for SBCL, ACL, and CLASP
1.121-;;; Don't know about ECL, or others
1.122-(eval-when (:load-toplevel :compile-toplevel :execute)
1.123- ;; ABCL pushes :package-local-nicknames without UIOP interfering,
1.124- ;; and Lispworks will do so
1.125- #+(or sbcl clasp)
1.126- (pushnew :package-local-nicknames *features*)
1.127- #+allegro
1.128- (let ((fname (find-symbol (symbol-name '#:add-package-local-nickname) '#:excl)))
1.129- (when (and fname (fboundp fname))
1.130- (pushnew :package-local-nicknames *features*))))
1.131-
1.132-;;; THOU SHALT NOT modify this definition, EVER, *EXCEPT* to add a new implementation.
1.133-;; If you somehow need to modify the API in any way,
1.134-;; you will need to create another, differently named, and just as immutable package.
1.135-#+package-local-nicknames
1.136-(defpackage :uiop/package-local-nicknames
1.137- (:use :cl)
1.138- (:import-from
1.139- #+allegro #:excl
1.140- #+sbcl #:sb-ext
1.141- #+(or clasp abcl ecl) #:ext
1.142- #+ccl #:ccl
1.143- #+lispworks #:hcl
1.144- #-(or allegro sbcl clasp abcl ccl lispworks ecl)
1.145- (error "Don't know from which package this lisp supplies the local-package-nicknames API.")
1.146- #:remove-package-local-nickname #:package-local-nicknames #:add-package-local-nickname)
1.147- (:export
1.148- #:add-package-local-nickname #:remove-package-local-nickname #:package-local-nicknames))
1.149-
1.150-;;;; General purpose package utilities
1.151-
1.152-(eval-when (:load-toplevel :compile-toplevel :execute)
1.153- (deftype package-designator () '(and (or package character string symbol) (satisfies find-package)))
1.154- (define-condition no-such-package-error (type-error)
1.155- ()
1.156- (:default-initargs :expected-type 'package-designator)
1.157- (:report (lambda (c s)
1.158- (format s "No package named ~a" (string (type-error-datum c))))))
1.159-
1.160- (defmethod package-designator ((c no-such-package-error))
1.161- (type-error-datum c))
1.162-
1.163- (defun find-package* (package-designator &optional (errorp t))
1.164- "Like CL:FIND-PACKAGE, but by default raises a UIOP:NO-SUCH-PACKAGE-ERROR if the
1.165- package is not found."
1.166- (let ((package (find-package package-designator)))
1.167- (cond
1.168- (package package)
1.169- (errorp (error 'no-such-package-error :datum package-designator))
1.170- (t nil))))
1.171-
1.172- (defun find-symbol* (name package-designator &optional (error t))
1.173- "Find a symbol in a package of given string'ified NAME;
1.174-unlike CL:FIND-SYMBOL, work well with 'modern' case sensitive syntax
1.175-by letting you supply a symbol or keyword for the name;
1.176-also works well when the package is not present.
1.177-If optional ERROR argument is NIL, return NIL instead of an error
1.178-when the symbol is not found."
1.179- (block nil
1.180- (let ((package (find-package* package-designator error)))
1.181- (when package ;; package error handled by find-package* already
1.182- (multiple-value-bind (symbol status) (find-symbol (string name) package)
1.183- (cond
1.184- (status (return (values symbol status)))
1.185- (error (error "There is no symbol ~S in package ~S" name (package-name package))))))
1.186- (values nil nil))))
1.187- (defun symbol-call (package name &rest args)
1.188- "Call a function associated with symbol of given name in given package,
1.189-with given ARGS. Useful when the call is read before the package is loaded,
1.190-or when loading the package is optional."
1.191- (apply (find-symbol* name package) args))
1.192- (defun intern* (name package-designator &optional (error t))
1.193- (intern (string name) (find-package* package-designator error)))
1.194- (defun export* (name package-designator)
1.195- (let* ((package (find-package* package-designator))
1.196- (symbol (intern* name package)))
1.197- (export (or symbol (list symbol)) package)))
1.198- (defun import* (symbol package-designator)
1.199- (import (or symbol (list symbol)) (find-package* package-designator)))
1.200- (defun shadowing-import* (symbol package-designator)
1.201- (shadowing-import (or symbol (list symbol)) (find-package* package-designator)))
1.202- (defun shadow* (name package-designator)
1.203- (shadow (list (string name)) (find-package* package-designator)))
1.204- (defun make-symbol* (name)
1.205- (etypecase name
1.206- (string (make-symbol name))
1.207- (symbol (copy-symbol name))))
1.208- (defun unintern* (name package-designator &optional (error t))
1.209- (block nil
1.210- (let ((package (find-package* package-designator error)))
1.211- (when package
1.212- (multiple-value-bind (symbol status) (find-symbol* name package error)
1.213- (cond
1.214- (status (unintern symbol package)
1.215- (return (values symbol status)))
1.216- (error (error "symbol ~A not present in package ~A"
1.217- (string symbol) (package-name package))))))
1.218- (values nil nil))))
1.219- (defun symbol-shadowing-p (symbol package)
1.220- (and (member symbol (package-shadowing-symbols package)) t))
1.221- (defun home-package-p (symbol package)
1.222- (and package (let ((sp (symbol-package symbol)))
1.223- (and sp (let ((pp (find-package* package)))
1.224- (and pp (eq sp pp))))))))
1.225-
1.226-
1.227-(eval-when (:load-toplevel :compile-toplevel :execute)
1.228- (defun symbol-package-name (symbol)
1.229- (let ((package (symbol-package symbol)))
1.230- (and package (package-name package))))
1.231- (defun standard-common-lisp-symbol-p (symbol)
1.232- (multiple-value-bind (sym status) (find-symbol* symbol :common-lisp nil)
1.233- (and (eq sym symbol) (eq status :external))))
1.234- (defun reify-package (package &optional package-context)
1.235- (if (eq package package-context) t
1.236- (etypecase package
1.237- (null nil)
1.238- ((eql (find-package :cl)) :cl)
1.239- (package (package-name package)))))
1.240- (defun unreify-package (package &optional package-context)
1.241- (etypecase package
1.242- (null nil)
1.243- ((eql t) package-context)
1.244- ((or symbol string) (find-package package))))
1.245- (defun reify-symbol (symbol &optional package-context)
1.246- (etypecase symbol
1.247- ((or keyword (satisfies standard-common-lisp-symbol-p)) symbol)
1.248- (symbol (vector (symbol-name symbol)
1.249- (reify-package (symbol-package symbol) package-context)))))
1.250- (defun unreify-symbol (symbol &optional package-context)
1.251- (etypecase symbol
1.252- (symbol symbol)
1.253- ((simple-vector 2)
1.254- (let* ((symbol-name (svref symbol 0))
1.255- (package-foo (svref symbol 1))
1.256- (package (unreify-package package-foo package-context)))
1.257- (if package (intern* symbol-name package)
1.258- (make-symbol* symbol-name)))))))
1.259-
1.260-(eval-when (:load-toplevel :compile-toplevel :execute)
1.261- (defvar *all-package-happiness* '())
1.262- (defvar *all-package-fishiness* (list t))
1.263- (defun record-fishy (info)
1.264- ;;(format t "~&FISHY: ~S~%" info)
1.265- (push info *all-package-fishiness*))
1.266- (defmacro when-package-fishiness (&body body)
1.267- `(when *all-package-fishiness* ,@body))
1.268- (defmacro note-package-fishiness (&rest info)
1.269- `(when-package-fishiness (record-fishy (list ,@info)))))
1.270-
1.271-(eval-when (:load-toplevel :compile-toplevel :execute)
1.272- #+(or clisp clozure)
1.273- (defun get-setf-function-symbol (symbol)
1.274- #+clisp (let ((sym (get symbol 'system::setf-function)))
1.275- (if sym (values sym :setf-function)
1.276- (let ((sym (get symbol 'system::setf-expander)))
1.277- (if sym (values sym :setf-expander)
1.278- (values nil nil)))))
1.279- #+clozure (gethash symbol ccl::%setf-function-names%))
1.280- #+(or clisp clozure)
1.281- (defun set-setf-function-symbol (new-setf-symbol symbol &optional kind)
1.282- #+clisp (assert (member kind '(:setf-function :setf-expander)))
1.283- #+clozure (assert (eq kind t))
1.284- #+clisp
1.285- (cond
1.286- ((null new-setf-symbol)
1.287- (remprop symbol 'system::setf-function)
1.288- (remprop symbol 'system::setf-expander))
1.289- ((eq kind :setf-function)
1.290- (setf (get symbol 'system::setf-function) new-setf-symbol))
1.291- ((eq kind :setf-expander)
1.292- (setf (get symbol 'system::setf-expander) new-setf-symbol))
1.293- (t (error "invalid kind of setf-function ~S for ~S to be set to ~S"
1.294- kind symbol new-setf-symbol)))
1.295- #+clozure
1.296- (progn
1.297- (gethash symbol ccl::%setf-function-names%) new-setf-symbol
1.298- (gethash new-setf-symbol ccl::%setf-function-name-inverses%) symbol))
1.299- #+(or clisp clozure)
1.300- (defun create-setf-function-symbol (symbol)
1.301- #+clisp (system::setf-symbol symbol)
1.302- #+clozure (ccl::construct-setf-function-name symbol))
1.303- (defun set-dummy-symbol (symbol reason other-symbol)
1.304- (setf (get symbol 'dummy-symbol) (cons reason other-symbol)))
1.305- (defun make-dummy-symbol (symbol)
1.306- (let ((dummy (copy-symbol symbol)))
1.307- (set-dummy-symbol dummy 'replacing symbol)
1.308- (set-dummy-symbol symbol 'replaced-by dummy)
1.309- dummy))
1.310- (defun dummy-symbol (symbol)
1.311- (get symbol 'dummy-symbol))
1.312- (defun get-dummy-symbol (symbol)
1.313- (let ((existing (dummy-symbol symbol)))
1.314- (if existing (values (cdr existing) (car existing))
1.315- (make-dummy-symbol symbol))))
1.316- (defun nuke-symbol-in-package (symbol package-designator)
1.317- (let ((package (find-package* package-designator))
1.318- (name (symbol-name symbol)))
1.319- (multiple-value-bind (sym stat) (find-symbol name package)
1.320- (when (and (member stat '(:internal :external)) (eq symbol sym))
1.321- (if (symbol-shadowing-p symbol package)
1.322- (shadowing-import* (get-dummy-symbol symbol) package)
1.323- (unintern* symbol package))))))
1.324- (defun nuke-symbol (symbol &optional (packages (list-all-packages)))
1.325- #+(or clisp clozure)
1.326- (multiple-value-bind (setf-symbol kind)
1.327- (get-setf-function-symbol symbol)
1.328- (when kind (nuke-symbol setf-symbol)))
1.329- (loop :for p :in packages :do (nuke-symbol-in-package symbol p)))
1.330- (defun rehome-symbol (symbol package-designator)
1.331- "Changes the home package of a symbol, also leaving it present in its old home if any"
1.332- (let* ((name (symbol-name symbol))
1.333- (package (find-package* package-designator))
1.334- (old-package (symbol-package symbol))
1.335- (old-status (and old-package (nth-value 1 (find-symbol name old-package))))
1.336- (shadowing (and old-package (symbol-shadowing-p symbol old-package) (make-symbol name))))
1.337- (multiple-value-bind (overwritten-symbol overwritten-symbol-status) (find-symbol name package)
1.338- (unless (eq package old-package)
1.339- (let ((overwritten-symbol-shadowing-p
1.340- (and overwritten-symbol-status
1.341- (symbol-shadowing-p overwritten-symbol package))))
1.342- (note-package-fishiness
1.343- :rehome-symbol name
1.344- (when old-package (package-name old-package)) old-status (and shadowing t)
1.345- (package-name package) overwritten-symbol-status overwritten-symbol-shadowing-p)
1.346- (when old-package
1.347- (if shadowing
1.348- (shadowing-import* shadowing old-package))
1.349- (unintern* symbol old-package))
1.350- (cond
1.351- (overwritten-symbol-shadowing-p
1.352- (shadowing-import* symbol package))
1.353- (t
1.354- (when overwritten-symbol-status
1.355- (unintern* overwritten-symbol package))
1.356- (import* symbol package)))
1.357- (if shadowing
1.358- (shadowing-import* symbol old-package)
1.359- (import* symbol old-package))
1.360- #+(or clisp clozure)
1.361- (multiple-value-bind (setf-symbol kind)
1.362- (get-setf-function-symbol symbol)
1.363- (when kind
1.364- (let* ((setf-function (fdefinition setf-symbol))
1.365- (new-setf-symbol (create-setf-function-symbol symbol)))
1.366- (note-package-fishiness
1.367- :setf-function
1.368- name (package-name package)
1.369- (symbol-name setf-symbol) (symbol-package-name setf-symbol)
1.370- (symbol-name new-setf-symbol) (symbol-package-name new-setf-symbol))
1.371- (when (symbol-package setf-symbol)
1.372- (unintern* setf-symbol (symbol-package setf-symbol)))
1.373- (setf (fdefinition new-setf-symbol) setf-function)
1.374- (set-setf-function-symbol new-setf-symbol symbol kind))))
1.375- #+(or clisp clozure)
1.376- (multiple-value-bind (overwritten-setf foundp)
1.377- (get-setf-function-symbol overwritten-symbol)
1.378- (when foundp
1.379- (unintern overwritten-setf)))
1.380- (when (eq old-status :external)
1.381- (export* symbol old-package))
1.382- (when (eq overwritten-symbol-status :external)
1.383- (export* symbol package))))
1.384- (values overwritten-symbol overwritten-symbol-status))))
1.385- (defun ensure-package-unused (package)
1.386- (loop :for p :in (package-used-by-list package) :do
1.387- (unuse-package package p)))
1.388- (defun delete-package* (package &key nuke)
1.389- (let ((p (find-package package)))
1.390- (when p
1.391- (when nuke (do-symbols (s p) (when (home-package-p s p) (nuke-symbol s))))
1.392- (ensure-package-unused p)
1.393- (delete-package package))))
1.394- (defun package-names (package)
1.395- (cons (package-name package) (package-nicknames package)))
1.396- (defun packages-from-names (names)
1.397- (remove-duplicates (remove nil (mapcar #'find-package names)) :from-end t))
1.398- (defun fresh-package-name (&key (prefix :%TO-BE-DELETED)
1.399- separator
1.400- (index (random most-positive-fixnum)))
1.401- (loop :for i :from index
1.402- :for n = (format nil "~A~@[~A~D~]" prefix (and (plusp i) (or separator "")) i)
1.403- :thereis (and (not (find-package n)) n)))
1.404- (defun rename-package-away (p &rest keys &key prefix &allow-other-keys)
1.405- (let ((new-name
1.406- (apply 'fresh-package-name
1.407- :prefix (or prefix (format nil "__~A__" (package-name p))) keys)))
1.408- (record-fishy (list :rename-away (package-names p) new-name))
1.409- (rename-package p new-name))))
1.410-
1.411-
1.412-;;; Communicable representation of symbol and package information
1.413-
1.414-(eval-when (:load-toplevel :compile-toplevel :execute)
1.415- (defun package-definition-form (package-designator
1.416- &key (nicknamesp t) (usep t)
1.417- (shadowp t) (shadowing-import-p t)
1.418- (exportp t) (importp t) internp (error t))
1.419- (let* ((package (or (find-package* package-designator error)
1.420- (return-from package-definition-form nil)))
1.421- (name (package-name package))
1.422- (nicknames (package-nicknames package))
1.423- (use (mapcar #'package-name (package-use-list package)))
1.424- (shadow ())
1.425- (shadowing-import (make-hash-table :test 'equal))
1.426- (import (make-hash-table :test 'equal))
1.427- (export ())
1.428- (intern ()))
1.429- (when package
1.430- (loop :for sym :being :the :symbols :in package
1.431- :for status = (nth-value 1 (find-symbol* sym package)) :do
1.432- (ecase status
1.433- ((nil :inherited))
1.434- ((:internal :external)
1.435- (let* ((name (symbol-name sym))
1.436- (external (eq status :external))
1.437- (home (symbol-package sym))
1.438- (home-name (package-name home))
1.439- (imported (not (eq home package)))
1.440- (shadowing (symbol-shadowing-p sym package)))
1.441- (cond
1.442- ((and shadowing imported)
1.443- (push name (gethash home-name shadowing-import)))
1.444- (shadowing
1.445- (push name shadow))
1.446- (imported
1.447- (push name (gethash home-name import))))
1.448- (cond
1.449- (external
1.450- (push name export))
1.451- (imported)
1.452- (t (push name intern)))))))
1.453- (labels ((sort-names (names)
1.454- (sort (copy-list names) #'string<))
1.455- (table-keys (table)
1.456- (loop :for k :being :the :hash-keys :of table :collect k))
1.457- (when-relevant (key value)
1.458- (when value (list (cons key value))))
1.459- (import-options (key table)
1.460- (loop :for i :in (sort-names (table-keys table))
1.461- :collect `(,key ,i ,@(sort-names (gethash i table))))))
1.462- `(defpackage ,name
1.463- ,@(when-relevant :nicknames (and nicknamesp (sort-names nicknames)))
1.464- (:use ,@(and usep (sort-names use)))
1.465- ,@(when-relevant :shadow (and shadowp (sort-names shadow)))
1.466- ,@(import-options :shadowing-import-from (and shadowing-import-p shadowing-import))
1.467- ,@(import-options :import-from (and importp import))
1.468- ,@(when-relevant :export (and exportp (sort-names export)))
1.469- ,@(when-relevant :intern (and internp (sort-names intern)))))))))
1.470-
1.471-
1.472-;;; ensure-package, define-package
1.473-(eval-when (:load-toplevel :compile-toplevel :execute)
1.474- ;; We already have UIOP:SIMPLE-STYLE-WARNING, but it comes from a later
1.475- ;; package.
1.476- (define-condition define-package-style-warning
1.477- #+sbcl (sb-int:simple-style-warning) #-sbcl (simple-condition style-warning)
1.478- ())
1.479- (defun ensure-shadowing-import (name to-package from-package shadowed imported)
1.480- (check-type name string)
1.481- (check-type to-package package)
1.482- (check-type from-package package)
1.483- (check-type shadowed hash-table)
1.484- (check-type imported hash-table)
1.485- (let ((import-me (find-symbol* name from-package)))
1.486- (multiple-value-bind (existing status) (find-symbol name to-package)
1.487- (cond
1.488- ((gethash name shadowed)
1.489- (unless (eq import-me existing)
1.490- (error "Conflicting shadowings for ~A" name)))
1.491- (t
1.492- (setf (gethash name shadowed) t)
1.493- (setf (gethash name imported) t)
1.494- (unless (or (null status)
1.495- (and (member status '(:internal :external))
1.496- (eq existing import-me)
1.497- (symbol-shadowing-p existing to-package)))
1.498- (note-package-fishiness
1.499- :shadowing-import name
1.500- (package-name from-package)
1.501- (or (home-package-p import-me from-package) (symbol-package-name import-me))
1.502- (package-name to-package) status
1.503- (and status (or (home-package-p existing to-package) (symbol-package-name existing)))))
1.504- (shadowing-import* import-me to-package))))))
1.505- (defun ensure-imported (import-me into-package &optional from-package)
1.506- (check-type import-me symbol)
1.507- (check-type into-package package)
1.508- (check-type from-package (or null package))
1.509- (let ((name (symbol-name import-me)))
1.510- (multiple-value-bind (existing status) (find-symbol name into-package)
1.511- (cond
1.512- ((not status)
1.513- (import* import-me into-package))
1.514- ((eq import-me existing))
1.515- (t
1.516- (let ((shadowing-p (symbol-shadowing-p existing into-package)))
1.517- (note-package-fishiness
1.518- :ensure-imported name
1.519- (and from-package (package-name from-package))
1.520- (or (home-package-p import-me from-package) (symbol-package-name import-me))
1.521- (package-name into-package)
1.522- status
1.523- (and status (or (home-package-p existing into-package) (symbol-package-name existing)))
1.524- shadowing-p)
1.525- (cond
1.526- ((or shadowing-p (eq status :inherited))
1.527- (shadowing-import* import-me into-package))
1.528- (t
1.529- (unintern* existing into-package)
1.530- (import* import-me into-package))))))))
1.531- (values))
1.532- (defun ensure-import (name to-package from-package shadowed imported)
1.533- (check-type name string)
1.534- (check-type to-package package)
1.535- (check-type from-package package)
1.536- (check-type shadowed hash-table)
1.537- (check-type imported hash-table)
1.538- (multiple-value-bind (import-me import-status) (find-symbol name from-package)
1.539- (when (null import-status)
1.540- (note-package-fishiness
1.541- :import-uninterned name (package-name from-package) (package-name to-package))
1.542- (setf import-me (intern* name from-package)))
1.543- (multiple-value-bind (existing status) (find-symbol name to-package)
1.544- (cond
1.545- ((and imported (gethash name imported))
1.546- (unless (and status (eq import-me existing))
1.547- (error "Can't import ~S from both ~S and ~S"
1.548- name (package-name (symbol-package existing)) (package-name from-package))))
1.549- ((gethash name shadowed)
1.550- (error "Can't both shadow ~S and import it from ~S" name (package-name from-package)))
1.551- (t
1.552- (setf (gethash name imported) t))))
1.553- (ensure-imported import-me to-package from-package)))
1.554- (defun ensure-inherited (name symbol to-package from-package mixp shadowed imported inherited)
1.555- (check-type name string)
1.556- (check-type symbol symbol)
1.557- (check-type to-package package)
1.558- (check-type from-package package)
1.559- (check-type mixp (member nil t)) ; no cl:boolean on Genera
1.560- (check-type shadowed hash-table)
1.561- (check-type imported hash-table)
1.562- (check-type inherited hash-table)
1.563- (multiple-value-bind (existing status) (find-symbol name to-package)
1.564- (let* ((sp (symbol-package symbol))
1.565- (in (gethash name inherited))
1.566- (xp (and status (symbol-package existing))))
1.567- (when (null sp)
1.568- (note-package-fishiness
1.569- :import-uninterned name
1.570- (package-name from-package) (package-name to-package) mixp)
1.571- (import* symbol from-package)
1.572- (setf sp (package-name from-package)))
1.573- (cond
1.574- ((gethash name shadowed))
1.575- (in
1.576- (unless (equal sp (first in))
1.577- (if mixp
1.578- (ensure-shadowing-import name to-package (second in) shadowed imported)
1.579- (error "Can't inherit ~S from ~S, it is inherited from ~S"
1.580- name (package-name sp) (package-name (first in))))))
1.581- ((gethash name imported)
1.582- (unless (eq symbol existing)
1.583- (error "Can't inherit ~S from ~S, it is imported from ~S"
1.584- name (package-name sp) (package-name xp))))
1.585- (t
1.586- (setf (gethash name inherited) (list sp from-package))
1.587- (when (and status (not (eq sp xp)))
1.588- (let ((shadowing (symbol-shadowing-p existing to-package)))
1.589- (note-package-fishiness
1.590- :inherited name
1.591- (package-name from-package)
1.592- (or (home-package-p symbol from-package) (symbol-package-name symbol))
1.593- (package-name to-package)
1.594- (or (home-package-p existing to-package) (symbol-package-name existing)))
1.595- (if shadowing (ensure-shadowing-import name to-package from-package shadowed imported)
1.596- (unintern* existing to-package)))))))))
1.597- (defun ensure-mix (name symbol to-package from-package shadowed imported inherited)
1.598- (check-type name string)
1.599- (check-type symbol symbol)
1.600- (check-type to-package package)
1.601- (check-type from-package package)
1.602- (check-type shadowed hash-table)
1.603- (check-type imported hash-table)
1.604- (check-type inherited hash-table)
1.605- (unless (gethash name shadowed)
1.606- (multiple-value-bind (existing status) (find-symbol name to-package)
1.607- (let* ((sp (symbol-package symbol))
1.608- (im (gethash name imported))
1.609- (in (gethash name inherited)))
1.610- (cond
1.611- ((or (null status)
1.612- (and status (eq symbol existing))
1.613- (and in (eq sp (first in))))
1.614- (ensure-inherited name symbol to-package from-package t shadowed imported inherited))
1.615- (in
1.616- (remhash name inherited)
1.617- (ensure-shadowing-import name to-package (second in) shadowed imported))
1.618- (im
1.619- (error "Symbol ~S import from ~S~:[~; actually ~:[uninterned~;~:*from ~S~]~] conflicts with existing symbol in ~S~:[~; actually ~:[uninterned~;from ~:*~S~]~]"
1.620- name (package-name from-package)
1.621- (home-package-p symbol from-package) (symbol-package-name symbol)
1.622- (package-name to-package)
1.623- (home-package-p existing to-package) (symbol-package-name existing)))
1.624- (t
1.625- (ensure-inherited name symbol to-package from-package t shadowed imported inherited)))))))
1.626-
1.627- (defun recycle-symbol (name recycle exported)
1.628- ;; Takes a symbol NAME (a string), a list of package designators for RECYCLE
1.629- ;; packages, and a hash-table of names (strings) of symbols scheduled to be
1.630- ;; EXPORTED from the package being defined. It returns two values, the
1.631- ;; symbol found (if any, or else NIL), and a boolean flag indicating whether
1.632- ;; a symbol was found. The caller (DEFINE-PACKAGE) will then do the
1.633- ;; re-homing of the symbol, etc.
1.634- (check-type name string)
1.635- (check-type recycle list)
1.636- (check-type exported hash-table)
1.637- (when (gethash name exported) ;; don't bother recycling private symbols
1.638- (let (recycled foundp)
1.639- (dolist (r recycle (values recycled foundp))
1.640- (multiple-value-bind (symbol status) (find-symbol name r)
1.641- (when (and status (home-package-p symbol r))
1.642- (cond
1.643- (foundp
1.644- ;; (nuke-symbol symbol)) -- even simple variable names like O or C will do that.
1.645- (note-package-fishiness :recycled-duplicate name (package-name foundp) (package-name r)))
1.646- (t
1.647- (setf recycled symbol foundp r)))))))))
1.648- (defun symbol-recycled-p (sym recycle)
1.649- (check-type sym symbol)
1.650- (check-type recycle list)
1.651- (and (member (symbol-package sym) recycle) t))
1.652- (defun ensure-symbol (name package intern recycle shadowed imported inherited exported)
1.653- (check-type name string)
1.654- (check-type package package)
1.655- (check-type intern (member nil t)) ; no cl:boolean on Genera
1.656- (check-type shadowed hash-table)
1.657- (check-type imported hash-table)
1.658- (check-type inherited hash-table)
1.659- (unless (or (gethash name shadowed)
1.660- (gethash name imported)
1.661- (gethash name inherited))
1.662- (multiple-value-bind (existing status)
1.663- (find-symbol name package)
1.664- (multiple-value-bind (recycled previous) (recycle-symbol name recycle exported)
1.665- (cond
1.666- ((and status (eq existing recycled) (eq previous package)))
1.667- (previous
1.668- (rehome-symbol recycled package))
1.669- ((and status (eq package (symbol-package existing))))
1.670- (t
1.671- (when status
1.672- (note-package-fishiness
1.673- :ensure-symbol name
1.674- (reify-package (symbol-package existing) package)
1.675- status intern)
1.676- (unintern existing))
1.677- (when intern
1.678- (intern* name package))))))))
1.679- (declaim (ftype (function (t t t &optional t) t) ensure-exported))
1.680- (defun ensure-exported-to-user (name symbol to-package &optional recycle)
1.681- (check-type name string)
1.682- (check-type symbol symbol)
1.683- (check-type to-package package)
1.684- (check-type recycle list)
1.685- (assert (equal name (symbol-name symbol)))
1.686- (multiple-value-bind (existing status) (find-symbol name to-package)
1.687- (unless (and status (eq symbol existing))
1.688- (let ((accessible
1.689- (or (null status)
1.690- (let ((shadowing (symbol-shadowing-p existing to-package))
1.691- (recycled (symbol-recycled-p existing recycle)))
1.692- (unless (and shadowing (not recycled))
1.693- (note-package-fishiness
1.694- :ensure-export name (symbol-package-name symbol)
1.695- (package-name to-package)
1.696- (or (home-package-p existing to-package) (symbol-package-name existing))
1.697- status shadowing)
1.698- (if (or (eq status :inherited) shadowing)
1.699- (shadowing-import* symbol to-package)
1.700- (unintern existing to-package))
1.701- t)))))
1.702- (when (and accessible (eq status :external))
1.703- (ensure-exported name symbol to-package recycle))))))
1.704- (defun ensure-exported (name symbol from-package &optional recycle)
1.705- (dolist (to-package (package-used-by-list from-package))
1.706- (ensure-exported-to-user name symbol to-package recycle))
1.707- (unless (eq from-package (symbol-package symbol))
1.708- (ensure-imported symbol from-package))
1.709- (export* name from-package))
1.710- (defun ensure-export (name from-package &optional recycle)
1.711- (multiple-value-bind (symbol status) (find-symbol* name from-package)
1.712- (unless (eq status :external)
1.713- (ensure-exported name symbol from-package recycle))))
1.714-
1.715- #+package-local-nicknames
1.716- (defun install-package-local-nicknames (destination-package new-nicknames)
1.717- ;; First, remove all package-local nicknames. (We'll reinstall any desired ones later.)
1.718- (dolist (pair-to-remove (uiop/package-local-nicknames:package-local-nicknames destination-package))
1.719- (uiop/package-local-nicknames:remove-package-local-nickname
1.720- (string (car pair-to-remove)) destination-package))
1.721- ;; Then, install all desired nicknames.
1.722- (loop :for (nickname package) :in new-nicknames
1.723- :do (uiop/package-local-nicknames:add-package-local-nickname
1.724- (string nickname)
1.725- (find-package package)
1.726- destination-package)))
1.727-
1.728- (defun ensure-package (name &key
1.729- nicknames documentation use
1.730- shadow shadowing-import-from
1.731- import-from export intern
1.732- recycle mix reexport
1.733- unintern local-nicknames)
1.734- #+genera (declare (ignore documentation))
1.735- (let* ((package-name (string name))
1.736- (nicknames (mapcar #'string nicknames))
1.737- (names (cons package-name nicknames))
1.738- (previous (packages-from-names names))
1.739- (discarded (cdr previous))
1.740- (to-delete ())
1.741- (package (or (first previous) (make-package package-name :nicknames nicknames)))
1.742- (recycle (packages-from-names recycle))
1.743- (use (mapcar 'find-package* use))
1.744- (mix (mapcar 'find-package* mix))
1.745- (reexport (mapcar 'find-package* reexport))
1.746- (shadow (mapcar 'string shadow))
1.747- (export (mapcar 'string export))
1.748- (intern (mapcar 'string intern))
1.749- (unintern (mapcar 'string unintern))
1.750- (local-nicknames (mapcar #'(lambda (pair) (mapcar 'string pair)) local-nicknames))
1.751- (shadowed (make-hash-table :test 'equal)) ; string to bool
1.752- (imported (make-hash-table :test 'equal)) ; string to bool
1.753- (exported (make-hash-table :test 'equal)) ; string to bool
1.754- ;; string to list home package and use package:
1.755- (inherited (make-hash-table :test 'equal)))
1.756- #-package-local-nicknames
1.757- (declare (ignore local-nicknames)) ; if not supported
1.758- (when-package-fishiness (record-fishy package-name))
1.759- ;; if supported, put package documentation
1.760- #-genera
1.761- (when documentation (setf (documentation package t) documentation))
1.762- ;; remove unwanted packages from use list
1.763- (loop :for p :in (set-difference (package-use-list package) (append mix use))
1.764- :do (note-package-fishiness :over-use name (package-names p))
1.765- (unuse-package p package))
1.766- ;; mark unwanted packages for deletion
1.767- (loop :for p :in discarded
1.768- :for n = (remove-if #'(lambda (x) (member x names :test 'equal))
1.769- (package-names p))
1.770- :do (note-package-fishiness :nickname name (package-names p))
1.771- (cond (n (rename-package p (first n) (rest n)))
1.772- (t (rename-package-away p)
1.773- (push p to-delete))))
1.774- ;; give package its desired name
1.775- (rename-package package package-name nicknames)
1.776- ;; Handle local nicknames
1.777- #+package-local-nicknames
1.778- (install-package-local-nicknames package local-nicknames)
1.779- (dolist (name unintern)
1.780- (multiple-value-bind (existing status) (find-symbol name package)
1.781- (when status
1.782- (unless (eq status :inherited)
1.783- (note-package-fishiness
1.784- :unintern (package-name package) name (symbol-package-name existing) status)
1.785- (unintern* name package nil)))))
1.786- ;; handle exports
1.787- (dolist (name export)
1.788- (setf (gethash name exported) t))
1.789- ;; handle reexportss
1.790- (dolist (p reexport)
1.791- (do-external-symbols (sym p)
1.792- (setf (gethash (string sym) exported) t)))
1.793- ;; unexport symbols not listed in (re)export
1.794- (do-external-symbols (sym package)
1.795- (let ((name (symbol-name sym)))
1.796- (unless (gethash name exported)
1.797- (note-package-fishiness
1.798- :over-export (package-name package) name
1.799- (or (home-package-p sym package) (symbol-package-name sym)))
1.800- (unexport sym package))))
1.801- ;; handle explicitly listed shadowed ssymbols
1.802- (dolist (name shadow)
1.803- (setf (gethash name shadowed) t)
1.804- (multiple-value-bind (existing status) (find-symbol name package)
1.805- (multiple-value-bind (recycled previous) (recycle-symbol name recycle exported)
1.806- (let ((shadowing (and status (symbol-shadowing-p existing package))))
1.807- (cond
1.808- ((eq previous package))
1.809- (previous
1.810- (rehome-symbol recycled package))
1.811- ((or (member status '(nil :inherited))
1.812- (home-package-p existing package)))
1.813- (t
1.814- (let ((dummy (make-symbol name)))
1.815- (note-package-fishiness
1.816- :shadow-imported (package-name package) name
1.817- (symbol-package-name existing) status shadowing)
1.818- (shadowing-import* dummy package)
1.819- (import* dummy package)))))))
1.820- (shadow* name package))
1.821- ;; handle shadowing imports
1.822- (loop :for (p . syms) :in shadowing-import-from
1.823- :for pp = (find-package* p) :do
1.824- (dolist (sym syms) (ensure-shadowing-import (string sym) package pp shadowed imported)))
1.825- ;; handle mixed packages
1.826- (loop :for p :in mix
1.827- :for pp = (find-package* p) :do
1.828- (do-external-symbols (sym pp) (ensure-mix (symbol-name sym) sym package pp shadowed imported inherited)))
1.829- ;; handle import-from packages
1.830- (loop :for (p . syms) :in import-from
1.831- ;; FOR NOW suppress errors in the case where the :import-from
1.832- ;; symbol list is empty (used only to establish a dependency by
1.833- ;; package-inferred-system users).
1.834- :for pp = (find-package* p syms) :do
1.835- (when (null pp)
1.836- ;; TODO: ASDF 3.4 Change to a full warning.
1.837- (warn 'define-package-style-warning
1.838- :format-control "When defining package ~a, attempting to import-from non-existent package ~a. This is deprecated behavior and will be removed from UIOP in the future."
1.839- :format-arguments (list name p)))
1.840- (dolist (sym syms) (ensure-import (symbol-name sym) package pp shadowed imported)))
1.841- ;; handle use-list and mix
1.842- (dolist (p (append use mix))
1.843- (do-external-symbols (sym p) (ensure-inherited (string sym) sym package p nil shadowed imported inherited))
1.844- (use-package p package))
1.845- (loop :for name :being :the :hash-keys :of exported :do
1.846- (ensure-symbol name package t recycle shadowed imported inherited exported)
1.847- (ensure-export name package recycle))
1.848- ;; intern dessired symbols
1.849- (dolist (name intern)
1.850- (ensure-symbol name package t recycle shadowed imported inherited exported))
1.851- (do-symbols (sym package)
1.852- (ensure-symbol (symbol-name sym) package nil recycle shadowed imported inherited exported))
1.853- ;; delete now-deceased packages
1.854- (map () 'delete-package* to-delete)
1.855- package)))
1.856-
1.857-
1.858-(eval-when (:load-toplevel :compile-toplevel :execute)
1.859- (defun parse-define-package-form (package clauses)
1.860- (loop
1.861- :with use-p = nil :with recycle-p = nil
1.862- :with documentation = nil
1.863- :for (kw . args) :in clauses
1.864- :when (eq kw :nicknames) :append args :into nicknames :else
1.865- :when (eq kw :documentation)
1.866- :do (cond
1.867- (documentation (error "define-package: can't define documentation twice"))
1.868- ((or (atom args) (cdr args)) (error "define-package: bad documentation"))
1.869- (t (setf documentation (car args)))) :else
1.870- :when (eq kw :use) :append args :into use :and :do (setf use-p t) :else
1.871- :when (eq kw :shadow) :append args :into shadow :else
1.872- :when (eq kw :shadowing-import-from) :collect args :into shadowing-import-from :else
1.873- :when (eq kw :import-from) :collect args :into import-from :else
1.874- :when (eq kw :export) :append args :into export :else
1.875- :when (eq kw :intern) :append args :into intern :else
1.876- :when (eq kw :recycle) :append args :into recycle :and :do (setf recycle-p t) :else
1.877- :when (eq kw :mix) :append args :into mix :else
1.878- :when (eq kw :reexport) :append args :into reexport :else
1.879- :when (eq kw :use-reexport) :append args :into use :and :append args :into reexport
1.880- :and :do (setf use-p t) :else
1.881- :when (eq kw :mix-reexport) :append args :into mix :and :append args :into reexport
1.882- :and :do (setf use-p t) :else
1.883- :when (eq kw :unintern) :append args :into unintern :else
1.884- :when (eq kw :local-nicknames)
1.885- :if (symbol-call '#:uiop '#:featurep :package-local-nicknames)
1.886- :append args :into local-nicknames
1.887- :else
1.888- :do (error ":LOCAL-NICKAMES option is not supported on this lisp implementation.")
1.889- :end
1.890- :else
1.891- :do (error "unrecognized define-package keyword ~S" kw)
1.892- :finally (return `(',package
1.893- :nicknames ',nicknames :documentation ',documentation
1.894- :use ',(if use-p use '(:common-lisp))
1.895- :shadow ',shadow :shadowing-import-from ',shadowing-import-from
1.896- :import-from ',import-from :export ',export :intern ',intern
1.897- :recycle ',(if recycle-p recycle (cons package nicknames))
1.898- :mix ',mix :reexport ',reexport :unintern ',unintern
1.899- ,@(when local-nicknames
1.900- `(:local-nicknames ',local-nicknames)))))))
1.901-
1.902-(defmacro define-package (package &rest clauses)
1.903- "DEFINE-PACKAGE takes a PACKAGE and a number of CLAUSES, of the form
1.904-\(KEYWORD . ARGS\).
1.905-DEFINE-PACKAGE supports the following keywords:
1.906-SHADOW, SHADOWING-IMPORT-FROM, IMPORT-FROM, EXPORT, INTERN, NICKNAMES,
1.907-DOCUMENTATION -- as per CL:DEFPACKAGE.
1.908-USE -- as per CL:DEFPACKAGE, but if neither USE, USE-REEXPORT, MIX,
1.909-nor MIX-REEXPORT is supplied, then it is equivalent to specifying
1.910-(:USE :COMMON-LISP). This is unlike CL:DEFPACKAGE for which the
1.911-behavior of a form without USE is implementation-dependent.
1.912-RECYCLE -- Recycle the package's exported symbols from the specified packages,
1.913-in order. For every symbol scheduled to be exported by the DEFINE-PACKAGE,
1.914-either through an :EXPORT option or a :REEXPORT option, if the symbol exists in
1.915-one of the :RECYCLE packages, the first such symbol is re-homed to the package
1.916-being defined.
1.917-For the sake of idempotence, it is important that the package being defined
1.918-should appear in first position if it already exists, and even if it doesn't,
1.919-ahead of any package that is not going to be deleted afterwards and never
1.920-created again. In short, except for special cases, always make it the first
1.921-package on the list if the list is not empty.
1.922-MIX -- Takes a list of package designators. MIX behaves like
1.923-\(:USE PKG1 PKG2 ... PKGn\) but additionally uses :SHADOWING-IMPORT-FROM to
1.924-resolve conflicts in favor of the first found symbol. It may still yield
1.925-an error if there is a conflict with an explicitly :IMPORT-FROM symbol.
1.926-REEXPORT -- Takes a list of package designators. For each package, p, in the list,
1.927-export symbols with the same name as those exported from p. Note that in the case
1.928-of shadowing, etc. the symbols with the same name may not be the same symbols.
1.929-UNINTERN -- Remove symbols here from PACKAGE. Note that this is primarily useful
1.930-when *redefining* a previously-existing package in the current image (e.g., when
1.931-upgrading ASDF). Most programmers will have no use for this option.
1.932-LOCAL-NICKNAMES -- If the host implementation supports package local nicknames
1.933-\(check for the :PACKAGE-LOCAL-NICKNAMES feature\), then this should be a list of
1.934-nickname and package name pairs. Using this option will cause an error if the
1.935-host CL implementation does not support it.
1.936-USE-REEXPORT, MIX-REEXPORT -- Use or mix the specified packages as per the USE or
1.937-MIX directives, and reexport their contents as per the REEXPORT directive."
1.938- (let ((ensure-form
1.939- `(prog1
1.940- (funcall 'ensure-package ,@(parse-define-package-form package clauses))
1.941- #+sbcl (setf (sb-impl::package-source-location (find-package ',package))
1.942- (sb-c:source-location)))))
1.943- `(progn
1.944- #+(or clasp ecl gcl mkcl) (defpackage ,package (:use))
1.945- (eval-when (:compile-toplevel :load-toplevel :execute)
1.946- ,ensure-form))))
1.947-
1.948-;; This package, unlike UIOP/PACKAGE, is allowed to evolve and acquire new symbols or drop old ones.
1.949-(define-package :uiop/package*
1.950- (:use-reexport :uiop/package
1.951- #+package-local-nicknames :uiop/package-local-nicknames)
1.952- (:import-from :uiop/package
1.953- #:define-package-style-warning
1.954- #:no-such-package-error
1.955- #:package-designator)
1.956- (:export #:define-package-style-warning
1.957- #:no-such-package-error
1.958- #:package-designator))
1.959-;;;; -------------------------------------------------------------------------
1.960-;;;; Handle compatibility with multiple implementations.
1.961-;;; This file is for papering over the deficiencies and peculiarities
1.962-;;; of various Common Lisp implementations.
1.963-;;; For implementation-specific access to the system, see os.lisp instead.
1.964-;;; A few functions are defined here, but actually exported from utility;
1.965-;;; from this package only common-lisp symbols are exported.
1.966-
1.967-(uiop/package:define-package :uiop/common-lisp
1.968- (:nicknames :uiop/cl)
1.969- (:use :uiop/package)
1.970- (:use-reexport #-genera :common-lisp #+genera :future-common-lisp)
1.971- #+allegro (:intern #:*acl-warn-save*)
1.972- #+cormanlisp (:shadow #:user-homedir-pathname)
1.973- #+cormanlisp
1.974- (:export
1.975- #:logical-pathname #:translate-logical-pathname
1.976- #:make-broadcast-stream #:file-namestring)
1.977- #+genera (:shadowing-import-from :scl #:boolean)
1.978- #+genera (:export #:boolean #:ensure-directories-exist #:read-sequence #:write-sequence)
1.979- #+(or mcl cmucl) (:shadow #:user-homedir-pathname))
1.980-(in-package :uiop/common-lisp)
1.981-
1.982-#-(or abcl allegro clasp clisp clozure cmucl cormanlisp ecl gcl genera lispworks mcl mezzano mkcl sbcl scl xcl)
1.983-(error "ASDF is not supported on your implementation. Please help us port it.")
1.984-
1.985-;; (declaim (optimize (speed 1) (debug 3) (safety 3))) ; DON'T: trust implementation defaults.
1.986-
1.987-
1.988-;;;; Early meta-level tweaks
1.989-
1.990-#+(or allegro clasp clisp clozure cmucl ecl lispworks mezzano mkcl sbcl abcl)
1.991-(eval-when (:load-toplevel :compile-toplevel :execute)
1.992- (when (and #+allegro (member :ics *features*)
1.993- #+(or clasp clisp cmucl ecl lispworks mkcl) (member :unicode *features*)
1.994- #+clozure (member :openmcl-unicode-strings *features*)
1.995- #+sbcl (member :sb-unicode *features*)
1.996- #+abcl t)
1.997- ;; Check for unicode at runtime, so that a hypothetical FASL compiled with unicode
1.998- ;; but loaded in a non-unicode setting (e.g. on Allegro) won't tell a lie.
1.999- (pushnew :asdf-unicode *features*)))
1.1000-
1.1001-#+allegro
1.1002-(eval-when (:load-toplevel :compile-toplevel :execute)
1.1003- ;; We need to disable autoloading BEFORE any mention of package ASDF.
1.1004- ;; In particular, there must NOT be a mention of package ASDF in the defpackage of this file
1.1005- ;; or any previous file.
1.1006- (setf excl::*autoload-package-name-alist*
1.1007- (remove "asdf" excl::*autoload-package-name-alist*
1.1008- :test 'equalp :key 'car))
1.1009- (defparameter *acl-warn-save*
1.1010- (when (boundp 'excl:*warn-on-nested-reader-conditionals*)
1.1011- excl:*warn-on-nested-reader-conditionals*))
1.1012- (when (boundp 'excl:*warn-on-nested-reader-conditionals*)
1.1013- (setf excl:*warn-on-nested-reader-conditionals* nil))
1.1014- (setf *print-readably* nil))
1.1015-
1.1016-#+clasp
1.1017-(eval-when (:load-toplevel :compile-toplevel :execute)
1.1018- (setf *load-verbose* nil)
1.1019- (defun use-ecl-byte-compiler-p () nil))
1.1020-
1.1021-#+clozure (in-package :ccl)
1.1022-#+(and clozure windows-target) ;; See http://trac.clozure.com/ccl/ticket/1117
1.1023-(eval-when (:load-toplevel :compile-toplevel :execute)
1.1024- (unless (fboundp 'external-process-wait)
1.1025- (in-development-mode
1.1026- (defun external-process-wait (proc)
1.1027- (when (and (external-process-pid proc) (eq (external-process-%status proc) :running))
1.1028- (with-interrupts-enabled
1.1029- (wait-on-semaphore (external-process-completed proc))))
1.1030- (values (external-process-%exit-code proc)
1.1031- (external-process-%status proc))))))
1.1032-#+clozure (in-package :uiop/common-lisp) ;; back in this package.
1.1033-
1.1034-#+cmucl
1.1035-(eval-when (:load-toplevel :compile-toplevel :execute)
1.1036- (setf ext:*gc-verbose* nil)
1.1037- (defun user-homedir-pathname ()
1.1038- (first (ext:search-list (cl:user-homedir-pathname)))))
1.1039-
1.1040-#+cormanlisp
1.1041-(eval-when (:load-toplevel :compile-toplevel :execute)
1.1042- (deftype logical-pathname () nil)
1.1043- (defun make-broadcast-stream () *error-output*)
1.1044- (defun translate-logical-pathname (x) x)
1.1045- (defun user-homedir-pathname (&optional host)
1.1046- (declare (ignore host))
1.1047- (parse-namestring (format nil "~A\\" (cl:user-homedir-pathname))))
1.1048- (defun file-namestring (p)
1.1049- (setf p (pathname p))
1.1050- (format nil "~@[~A~]~@[.~A~]" (pathname-name p) (pathname-type p))))
1.1051-
1.1052-#+ecl
1.1053-(eval-when (:load-toplevel :compile-toplevel :execute)
1.1054- (setf *load-verbose* nil)
1.1055- (defun use-ecl-byte-compiler-p () (and (member :ecl-bytecmp *features*) t))
1.1056- (unless (use-ecl-byte-compiler-p) (require :cmp)))
1.1057-
1.1058-#+gcl
1.1059-(eval-when (:load-toplevel :compile-toplevel :execute)
1.1060- (unless (member :ansi-cl *features*)
1.1061- (error "ASDF only supports GCL in ANSI mode. Aborting.~%"))
1.1062- (setf compiler::*compiler-default-type* (pathname "")
1.1063- compiler::*lsp-ext* "")
1.1064- #.(let ((code ;; Only support very recent GCL 2.7.0 from November 2013 or later.
1.1065- (cond
1.1066- #+gcl
1.1067- ((or (< system::*gcl-major-version* 2)
1.1068- (and (= system::*gcl-major-version* 2)
1.1069- (< system::*gcl-minor-version* 7)))
1.1070- '(error "GCL 2.7 or later required to use ASDF")))))
1.1071- (eval code)
1.1072- code))
1.1073-
1.1074-#+genera
1.1075-(eval-when (:load-toplevel :compile-toplevel :execute)
1.1076- (unless (fboundp 'lambda)
1.1077- (defmacro lambda (&whole form &rest bvl-decls-and-body)
1.1078- (declare (ignore bvl-decls-and-body)(zwei::indentation 1 1))
1.1079- `#',(cons 'lisp::lambda (cdr form))))
1.1080- (unless (fboundp 'ensure-directories-exist)
1.1081- (defun ensure-directories-exist (path)
1.1082- (fs:create-directories-recursively (pathname path))))
1.1083- (unless (fboundp 'read-sequence)
1.1084- (defun read-sequence (sequence stream &key (start 0) end)
1.1085- (scl:send stream :string-in nil sequence start end)))
1.1086- (unless (fboundp 'write-sequence)
1.1087- (defun write-sequence (sequence stream &key (start 0) end)
1.1088- (scl:send stream :string-out sequence start end)
1.1089- sequence)))
1.1090-
1.1091-#+lispworks
1.1092-(eval-when (:load-toplevel :compile-toplevel :execute)
1.1093- ;; lispworks 3 and earlier cannot be checked for so we always assume
1.1094- ;; at least version 4
1.1095- (unless (member :lispworks4 *features*)
1.1096- (pushnew :lispworks5+ *features*)
1.1097- (unless (member :lispworks5 *features*)
1.1098- (pushnew :lispworks6+ *features*)
1.1099- (unless (member :lispworks6 *features*)
1.1100- (pushnew :lispworks7+ *features*)))))
1.1101-
1.1102-
1.1103-#.(or #+mcl ;; the #$ doesn't work on other lisps, even protected by #+mcl, so we use this trick
1.1104- (read-from-string
1.1105- "(eval-when (:load-toplevel :compile-toplevel :execute)
1.1106- (ccl:define-entry-point (_getenv \"getenv\") ((name :string)) :string)
1.1107- (ccl:define-entry-point (_system \"system\") ((name :string)) :int)
1.1108- ;; Note: ASDF may expect user-homedir-pathname to provide
1.1109- ;; the pathname of the current user's home directory, whereas
1.1110- ;; MCL by default provides the directory from which MCL was started.
1.1111- ;; See http://code.google.com/p/mcl/wiki/Portability
1.1112- (defun user-homedir-pathname ()
1.1113- (ccl::findfolder #$kuserdomain #$kCurrentUserFolderType))
1.1114- (defun probe-posix (posix-namestring)
1.1115- \"If a file exists for the posix namestring, return the pathname\"
1.1116- (ccl::with-cstrs ((cpath posix-namestring))
1.1117- (ccl::rlet ((is-dir :boolean)
1.1118- (fsref :fsref))
1.1119- (when (eq #$noerr (#_fspathmakeref cpath fsref is-dir))
1.1120- (ccl::%path-from-fsref fsref is-dir))))))"))
1.1121-
1.1122-#+mkcl
1.1123-(eval-when (:load-toplevel :compile-toplevel :execute)
1.1124- (require :cmp)
1.1125- (setq clos::*redefine-class-in-place* t)) ;; Make sure we have strict ANSI class redefinition semantics
1.1126-
1.1127-
1.1128-;;;; compatfmt: avoid fancy format directives when unsupported
1.1129-(eval-when (:load-toplevel :compile-toplevel :execute)
1.1130- (defun frob-substrings (string substrings &optional frob)
1.1131- "for each substring in SUBSTRINGS, find occurrences of it within STRING
1.1132-that don't use parts of matched occurrences of previous strings, and
1.1133-FROB them, that is to say, remove them if FROB is NIL,
1.1134-replace by FROB if FROB is a STRING, or if FROB is a FUNCTION,
1.1135-call FROB with the match and a function that emits a string in the output.
1.1136-Return a string made of the parts not omitted or emitted by FROB."
1.1137- (declare (optimize (speed 0) (safety #-gcl 3 #+gcl 0) (debug 3)))
1.1138- (let ((length (length string)) (stream nil))
1.1139- (labels ((emit-string (x &optional (start 0) (end (length x)))
1.1140- (when (< start end)
1.1141- (unless stream (setf stream (make-string-output-stream)))
1.1142- (write-string x stream :start start :end end)))
1.1143- (emit-substring (start end)
1.1144- (when (and (zerop start) (= end length))
1.1145- (return-from frob-substrings string))
1.1146- (emit-string string start end))
1.1147- (recurse (substrings start end)
1.1148- (cond
1.1149- ((>= start end))
1.1150- ((null substrings) (emit-substring start end))
1.1151- (t (let* ((sub-spec (first substrings))
1.1152- (sub (if (consp sub-spec) (car sub-spec) sub-spec))
1.1153- (fun (if (consp sub-spec) (cdr sub-spec) frob))
1.1154- (found (search sub string :start2 start :end2 end))
1.1155- (more (rest substrings)))
1.1156- (cond
1.1157- (found
1.1158- (recurse more start found)
1.1159- (etypecase fun
1.1160- (null)
1.1161- (string (emit-string fun))
1.1162- (function (funcall fun sub #'emit-string)))
1.1163- (recurse substrings (+ found (length sub)) end))
1.1164- (t
1.1165- (recurse more start end))))))))
1.1166- (recurse substrings 0 length))
1.1167- (if stream (get-output-stream-string stream) "")))
1.1168-
1.1169- (defmacro compatfmt (format)
1.1170- #+(or gcl genera)
1.1171- (frob-substrings format `("~3i~_" #+genera ,@'("~@<" "~@;" "~@:>" "~:>")))
1.1172- #-(or gcl genera) format))
1.1173-;;;; -------------------------------------------------------------------------
1.1174-;;;; General Purpose Utilities for ASDF
1.1175-
1.1176-(uiop/package:define-package :uiop/utility
1.1177- (:use :uiop/common-lisp :uiop/package)
1.1178- ;; import and reexport a few things defined in :uiop/common-lisp
1.1179- (:import-from :uiop/common-lisp #:compatfmt #:frob-substrings
1.1180- #+(or clasp ecl) #:use-ecl-byte-compiler-p #+mcl #:probe-posix)
1.1181- (:export #:compatfmt #:frob-substrings #:compatfmt
1.1182- #+(or clasp ecl) #:use-ecl-byte-compiler-p #+mcl #:probe-posix)
1.1183- (:export
1.1184- ;; magic helper to define debugging functions:
1.1185- #:uiop-debug #:load-uiop-debug-utility #:*uiop-debug-utility*
1.1186- #:with-upgradability ;; (un)defining functions in an upgrade-friendly way
1.1187- #:nest #:if-let ;; basic flow control
1.1188- #:parse-body ;; macro definition helper
1.1189- #:while-collecting #:appendf #:length=n-p #:ensure-list ;; lists
1.1190- #:remove-plist-keys #:remove-plist-key ;; plists
1.1191- #:emptyp ;; sequences
1.1192- #:+non-base-chars-exist-p+ ;; characters
1.1193- #:+max-character-type-index+ #:character-type-index #:+character-types+
1.1194- #:base-string-p #:strings-common-element-type #:reduce/strcat #:strcat ;; strings
1.1195- #:first-char #:last-char #:split-string #:stripln #:+cr+ #:+lf+ #:+crlf+
1.1196- #:string-prefix-p #:string-enclosed-p #:string-suffix-p
1.1197- #:standard-case-symbol-name #:find-standard-case-symbol ;; symbols
1.1198- #:coerce-class ;; CLOS
1.1199- #:timestamp< #:timestamps< #:timestamp*< #:timestamp<= ;; timestamps
1.1200- #:earlier-timestamp #:timestamps-earliest #:earliest-timestamp
1.1201- #:later-timestamp #:timestamps-latest #:latest-timestamp #:latest-timestamp-f
1.1202- #:list-to-hash-set #:ensure-gethash ;; hash-table
1.1203- #:ensure-function #:access-at #:access-at-count ;; functions
1.1204- #:call-function #:call-functions #:register-hook-function
1.1205- #:lexicographic< #:lexicographic<= ;; version
1.1206- #:simple-style-warning #:style-warn ;; simple style warnings
1.1207- #:match-condition-p #:match-any-condition-p ;; conditions
1.1208- #:call-with-muffled-conditions #:with-muffled-conditions
1.1209- #:not-implemented-error #:parameter-error
1.1210- #:symbol-test-to-feature-expression
1.1211- #:boolean-to-feature-expression))
1.1212-(in-package :uiop/utility)
1.1213-
1.1214-;;;; Defining functions in a way compatible with hot-upgrade:
1.1215-;; - The WTIH-UPGRADABILITY infrastructure below ensures that functions are declared NOTINLINE,
1.1216-;; so that new definitions are always seen by all callers, even those up the stack.
1.1217-;; - WITH-UPGRADABILITY also uses EVAL-WHEN so that definitions used by ASDF are in a limbo state
1.1218-;; (especially for gf's) in between the COMPILE-OP and LOAD-OP operations on the defining file.
1.1219-;; - THOU SHALT NOT redefine a function with a backward-incompatible semantics without renaming it,
1.1220-;; at least if that function is used by ASDF while performing the plan to load ASDF.
1.1221-;; - THOU SHALT change the name of a function whenever thou makest an incompatible change.
1.1222-;; - For instance, when the meanings of NIL and T for timestamps was inverted,
1.1223-;; functions in the STAMP<, STAMP<=, etc. family had to be renamed to TIMESTAMP<, TIMESTAMP<=, etc.,
1.1224-;; because the change other caused a huge incompatibility during upgrade.
1.1225-;; - Whenever a function goes from a DEFUN to a DEFGENERIC, or the DEFGENERIC signature changes, etc.,
1.1226-;; even in a backward-compatible way, you MUST precede the definition by FMAKUNBOUND.
1.1227-;; - Since FMAKUNBOUND will remove all the methods on the generic function, make sure that
1.1228-;; all the methods required for ASDF to successfully continue compiling itself
1.1229-;; shall be defined in the same file as the one with the FMAKUNBOUND, *after* the DEFGENERIC.
1.1230-;; - When a function goes from DEFGENERIC to DEFUN, you may omit to use FMAKUNBOUND.
1.1231-;; - For safety, you shall put the FMAKUNBOUND just before the DEFUN or DEFGENERIC,
1.1232-;; in the same WITH-UPGRADABILITY form (and its implicit EVAL-WHEN).
1.1233-;; - Any time you change a signature, please keep a comment specifying the first release after the change;
1.1234-;; put that comment on the same line as FMAKUNBOUND, it you use FMAKUNBOUND.
1.1235-(eval-when (:load-toplevel :compile-toplevel :execute)
1.1236- (defun ensure-function-notinline (definition &aux (name (second definition)))
1.1237- (assert (member (first definition) '(defun defgeneric)))
1.1238- `(progn
1.1239- ,(when (and #+(or clasp ecl) (symbolp name)) ; NB: fails for (SETF functions) on ECL
1.1240- `(declaim (notinline ,name)))
1.1241- ,definition))
1.1242- (defmacro with-upgradability ((&optional) &body body)
1.1243- "Evaluate BODY at compile- load- and run- times, with DEFUN and DEFGENERIC modified
1.1244-to also declare the functions NOTINLINE and to accept a wrapping the function name
1.1245-specification into a list with keyword argument SUPERSEDE (which defaults to T if the name
1.1246-is not wrapped, and NIL if it is wrapped). If SUPERSEDE is true, call UNDEFINE-FUNCTION
1.1247-to supersede any previous definition."
1.1248- `(eval-when (:compile-toplevel :load-toplevel :execute)
1.1249- ,@(loop :for form :in body :collect
1.1250- (if (consp form)
1.1251- (case (first form)
1.1252- ((defun defgeneric) (ensure-function-notinline form))
1.1253- (otherwise form))
1.1254- form)))))
1.1255-
1.1256-;;; Magic debugging help. See contrib/debug.lisp
1.1257-(with-upgradability ()
1.1258- (defvar *uiop-debug-utility*
1.1259- '(symbol-call :uiop :subpathname (symbol-call :uiop :uiop-directory) "contrib/debug.lisp")
1.1260- "form that evaluates to the pathname to your favorite debugging utilities")
1.1261-
1.1262- (defmacro uiop-debug (&rest keys)
1.1263- "Load the UIOP debug utility at compile-time as well as runtime"
1.1264- `(eval-when (:compile-toplevel :load-toplevel :execute)
1.1265- (load-uiop-debug-utility ,@keys)))
1.1266-
1.1267- (defun load-uiop-debug-utility (&key package utility-file)
1.1268- "Load the UIOP debug utility in given PACKAGE (default *PACKAGE*).
1.1269-Beware: The utility is located by EVAL'uating the UTILITY-FILE form (default *UIOP-DEBUG-UTILITY*)."
1.1270- (let* ((*package* (if package (find-package package) *package*))
1.1271- (keyword (read-from-string
1.1272- (format nil ":DBG-~:@(~A~)" (package-name *package*)))))
1.1273- (unless (member keyword *features*)
1.1274- (let* ((utility-file (or utility-file *uiop-debug-utility*))
1.1275- (file (ignore-errors (probe-file (eval utility-file)))))
1.1276- (if file (load file)
1.1277- (error "Failed to locate debug utility file: ~S" utility-file)))))))
1.1278-
1.1279-;;; Flow control
1.1280-(with-upgradability ()
1.1281- (defmacro nest (&rest things)
1.1282- "Macro to keep code nesting and indentation under control." ;; Thanks to mbaringer
1.1283- (reduce #'(lambda (outer inner) `(,@outer ,inner))
1.1284- things :from-end t))
1.1285-
1.1286- (defmacro if-let (bindings &body (then-form &optional else-form)) ;; from alexandria
1.1287- ;; bindings can be (var form) or ((var1 form1) ...)
1.1288- (let* ((binding-list (if (and (consp bindings) (symbolp (car bindings)))
1.1289- (list bindings)
1.1290- bindings))
1.1291- (variables (mapcar #'car binding-list)))
1.1292- `(let ,binding-list
1.1293- (if (and ,@variables)
1.1294- ,then-form
1.1295- ,else-form)))))
1.1296-
1.1297-;;; Macro definition helper
1.1298-(with-upgradability ()
1.1299- (defun parse-body (body &key documentation whole) ;; from alexandria
1.1300- "Parses BODY into (values remaining-forms declarations doc-string).
1.1301-Documentation strings are recognized only if DOCUMENTATION is true.
1.1302-Syntax errors in body are signalled and WHOLE is used in the signal
1.1303-arguments when given."
1.1304- (let ((doc nil)
1.1305- (decls nil)
1.1306- (current nil))
1.1307- (tagbody
1.1308- :declarations
1.1309- (setf current (car body))
1.1310- (when (and documentation (stringp current) (cdr body))
1.1311- (if doc
1.1312- (error "Too many documentation strings in ~S." (or whole body))
1.1313- (setf doc (pop body)))
1.1314- (go :declarations))
1.1315- (when (and (listp current) (eql (first current) 'declare))
1.1316- (push (pop body) decls)
1.1317- (go :declarations)))
1.1318- (values body (nreverse decls) doc))))
1.1319-
1.1320-
1.1321-;;; List manipulation
1.1322-(with-upgradability ()
1.1323- (defmacro while-collecting ((&rest collectors) &body body)
1.1324- "COLLECTORS should be a list of names for collections. A collector
1.1325-defines a function that, when applied to an argument inside BODY, will
1.1326-add its argument to the corresponding collection. Returns multiple values,
1.1327-a list for each collection, in order.
1.1328- E.g.,
1.1329-\(while-collecting \(foo bar\)
1.1330- \(dolist \(x '\(\(a 1\) \(b 2\) \(c 3\)\)\)
1.1331- \(foo \(first x\)\)
1.1332- \(bar \(second x\)\)\)\)
1.1333-Returns two values: \(A B C\) and \(1 2 3\)."
1.1334- (let ((vars (mapcar #'(lambda (x) (gensym (symbol-name x))) collectors))
1.1335- (initial-values (mapcar (constantly nil) collectors)))
1.1336- `(let ,(mapcar #'list vars initial-values)
1.1337- (flet ,(mapcar #'(lambda (c v) `(,c (x) (push x ,v) (values))) collectors vars)
1.1338- ,@body
1.1339- (values ,@(mapcar #'(lambda (v) `(reverse ,v)) vars))))))
1.1340-
1.1341- (define-modify-macro appendf (&rest args)
1.1342- append "Append onto list") ;; only to be used on short lists.
1.1343-
1.1344- (defun length=n-p (x n) ;is it that (= (length x) n) ?
1.1345- (check-type n (integer 0 *))
1.1346- (loop
1.1347- :for l = x :then (cdr l)
1.1348- :for i :downfrom n :do
1.1349- (cond
1.1350- ((zerop i) (return (null l)))
1.1351- ((not (consp l)) (return nil)))))
1.1352-
1.1353- (defun ensure-list (x)
1.1354- (if (listp x) x (list x))))
1.1355-
1.1356-
1.1357-;;; Remove a key from a plist, i.e. for keyword argument cleanup
1.1358-(with-upgradability ()
1.1359- (defun remove-plist-key (key plist)
1.1360- "Remove a single key from a plist"
1.1361- (loop :for (k v) :on plist :by #'cddr
1.1362- :unless (eq k key)
1.1363- :append (list k v)))
1.1364-
1.1365- (defun remove-plist-keys (keys plist)
1.1366- "Remove a list of keys from a plist"
1.1367- (loop :for (k v) :on plist :by #'cddr
1.1368- :unless (member k keys)
1.1369- :append (list k v))))
1.1370-
1.1371-
1.1372-;;; Sequences
1.1373-(with-upgradability ()
1.1374- (defun emptyp (x)
1.1375- "Predicate that is true for an empty sequence"
1.1376- (or (null x) (and (vectorp x) (zerop (length x))))))
1.1377-
1.1378-
1.1379-;;; Characters
1.1380-(with-upgradability ()
1.1381- ;; base-char != character on ECL, LW, SBCL, Genera.
1.1382- ;; NB: We assume a total order on character types.
1.1383- ;; If that's not true... this code will need to be updated.
1.1384- (defparameter +character-types+ ;; assuming a simple hierarchy
1.1385- #.(coerce (loop :for (type next) :on
1.1386- '(;; In SCL, all characters seem to be 16-bit base-char
1.1387- ;; Yet somehow character fails to be a subtype of base-char
1.1388- #-scl base-char
1.1389- ;; LW6 has BASE-CHAR < SIMPLE-CHAR < CHARACTER
1.1390- ;; LW7 has BASE-CHAR < BMP-CHAR < SIMPLE-CHAR = CHARACTER
1.1391- #+lispworks7+ lw:bmp-char
1.1392- #+lispworks lw:simple-char
1.1393- character)
1.1394- :unless (and next (subtypep next type))
1.1395- :collect type) 'vector))
1.1396- (defparameter +max-character-type-index+ (1- (length +character-types+)))
1.1397- (defconstant +non-base-chars-exist-p+ (plusp +max-character-type-index+))
1.1398- (when +non-base-chars-exist-p+ (pushnew :non-base-chars-exist-p *features*)))
1.1399-
1.1400-(with-upgradability ()
1.1401- (defun character-type-index (x)
1.1402- (declare (ignorable x))
1.1403- #.(case +max-character-type-index+
1.1404- (0 0)
1.1405- (1 '(etypecase x
1.1406- (character (if (typep x 'base-char) 0 1))
1.1407- (symbol (if (subtypep x 'base-char) 0 1))))
1.1408- (otherwise
1.1409- '(or (position-if (etypecase x
1.1410- (character #'(lambda (type) (typep x type)))
1.1411- (symbol #'(lambda (type) (subtypep x type))))
1.1412- +character-types+)
1.1413- (error "Not a character or character type: ~S" x))))))
1.1414-
1.1415-
1.1416-;;; Strings
1.1417-(with-upgradability ()
1.1418- (defun base-string-p (string)
1.1419- "Does the STRING only contain BASE-CHARs?"
1.1420- (declare (ignorable string))
1.1421- (and #+non-base-chars-exist-p (eq 'base-char (array-element-type string))))
1.1422-
1.1423- (defun strings-common-element-type (strings)
1.1424- "What least subtype of CHARACTER can contain all the elements of all the STRINGS?"
1.1425- (declare (ignorable strings))
1.1426- #.(if +non-base-chars-exist-p+
1.1427- `(aref +character-types+
1.1428- (loop :with index = 0 :for s :in strings :do
1.1429- (flet ((consider (i)
1.1430- (cond ((= i ,+max-character-type-index+) (return i))
1.1431- ,@(when (> +max-character-type-index+ 1) `(((> i index) (setf index i)))))))
1.1432- (cond
1.1433- ((emptyp s)) ;; NIL or empty string
1.1434- ((characterp s) (consider (character-type-index s)))
1.1435- ((stringp s) (let ((string-type-index
1.1436- (character-type-index (array-element-type s))))
1.1437- (unless (>= index string-type-index)
1.1438- (loop :for c :across s :for i = (character-type-index c)
1.1439- :do (consider i)
1.1440- ,@(when (> +max-character-type-index+ 1)
1.1441- `((when (= i string-type-index) (return))))))))
1.1442- (t (error "Invalid string designator ~S for ~S" s 'strings-common-element-type))))
1.1443- :finally (return index)))
1.1444- ''character))
1.1445-
1.1446- (defun reduce/strcat (strings &key key start end)
1.1447- "Reduce a list as if by STRCAT, accepting KEY START and END keywords like REDUCE.
1.1448-NIL is interpreted as an empty string. A character is interpreted as a string of length one."
1.1449- (when (or start end) (setf strings (subseq strings start end)))
1.1450- (when key (setf strings (mapcar key strings)))
1.1451- (loop :with output = (make-string (loop :for s :in strings
1.1452- :sum (if (characterp s) 1 (length s)))
1.1453- :element-type (strings-common-element-type strings))
1.1454- :with pos = 0
1.1455- :for input :in strings
1.1456- :do (etypecase input
1.1457- (null)
1.1458- (character (setf (char output pos) input) (incf pos))
1.1459- (string (replace output input :start1 pos) (incf pos (length input))))
1.1460- :finally (return output)))
1.1461-
1.1462- (defun strcat (&rest strings)
1.1463- "Concatenate strings.
1.1464-NIL is interpreted as an empty string, a character as a string of length one."
1.1465- (reduce/strcat strings))
1.1466-
1.1467- (defun first-char (s)
1.1468- "Return the first character of a non-empty string S, or NIL"
1.1469- (and (stringp s) (plusp (length s)) (char s 0)))
1.1470-
1.1471- (defun last-char (s)
1.1472- "Return the last character of a non-empty string S, or NIL"
1.1473- (and (stringp s) (plusp (length s)) (char s (1- (length s)))))
1.1474-
1.1475- (defun split-string (string &key max (separator '(#\Space #\Tab)))
1.1476- "Split STRING into a list of components separated by
1.1477-any of the characters in the sequence SEPARATOR.
1.1478-If MAX is specified, then no more than max(1,MAX) components will be returned,
1.1479-starting the separation from the end, e.g. when called with arguments
1.1480- \"a.b.c.d.e\" :max 3 :separator \".\" it will return (\"a.b.c\" \"d\" \"e\")."
1.1481- (block ()
1.1482- (let ((list nil) (words 0) (end (length string)))
1.1483- (when (zerop end) (return nil))
1.1484- (flet ((separatorp (char) (find char separator))
1.1485- (done () (return (cons (subseq string 0 end) list))))
1.1486- (loop
1.1487- :for start = (if (and max (>= words (1- max)))
1.1488- (done)
1.1489- (position-if #'separatorp string :end end :from-end t))
1.1490- :do (when (null start) (done))
1.1491- (push (subseq string (1+ start) end) list)
1.1492- (incf words)
1.1493- (setf end start))))))
1.1494-
1.1495- (defun string-prefix-p (prefix string)
1.1496- "Does STRING begin with PREFIX?"
1.1497- (let* ((x (string prefix))
1.1498- (y (string string))
1.1499- (lx (length x))
1.1500- (ly (length y)))
1.1501- (and (<= lx ly) (string= x y :end2 lx))))
1.1502-
1.1503- (defun string-suffix-p (string suffix)
1.1504- "Does STRING end with SUFFIX?"
1.1505- (let* ((x (string string))
1.1506- (y (string suffix))
1.1507- (lx (length x))
1.1508- (ly (length y)))
1.1509- (and (<= ly lx) (string= x y :start1 (- lx ly)))))
1.1510-
1.1511- (defun string-enclosed-p (prefix string suffix)
1.1512- "Does STRING begin with PREFIX and end with SUFFIX?"
1.1513- (and (string-prefix-p prefix string)
1.1514- (string-suffix-p string suffix)))
1.1515-
1.1516- (defvar +cr+ (coerce #(#\Return) 'string))
1.1517- (defvar +lf+ (coerce #(#\Linefeed) 'string))
1.1518- (defvar +crlf+ (coerce #(#\Return #\Linefeed) 'string))
1.1519-
1.1520- (defun stripln (x)
1.1521- "Strip a string X from any ending CR, LF or CRLF.
1.1522-Return two values, the stripped string and the ending that was stripped,
1.1523-or the original value and NIL if no stripping took place.
1.1524-Since our STRCAT accepts NIL as empty string designator,
1.1525-the two results passed to STRCAT always reconstitute the original string"
1.1526- (check-type x string)
1.1527- (block nil
1.1528- (flet ((c (end) (when (string-suffix-p x end)
1.1529- (return (values (subseq x 0 (- (length x) (length end))) end)))))
1.1530- (when x (c +crlf+) (c +lf+) (c +cr+) (values x nil)))))
1.1531-
1.1532- (defun standard-case-symbol-name (name-designator)
1.1533- "Given a NAME-DESIGNATOR for a symbol, if it is a symbol, convert it to a string using STRING;
1.1534-if it is a string, use STRING-UPCASE on an ANSI CL platform, or STRING on a so-called \"modern\"
1.1535-platform such as Allegro with modern syntax."
1.1536- (check-type name-designator (or string symbol))
1.1537- (cond
1.1538- ((or (symbolp name-designator) #+allegro (eq excl:*current-case-mode* :case-sensitive-lower))
1.1539- (string name-designator))
1.1540- ;; Should we be doing something on CLISP?
1.1541- (t (string-upcase name-designator))))
1.1542-
1.1543- (defun find-standard-case-symbol (name-designator package-designator &optional (error t))
1.1544- "Find a symbol designated by NAME-DESIGNATOR in a package designated by PACKAGE-DESIGNATOR,
1.1545-where STANDARD-CASE-SYMBOL-NAME is used to transform them if these designators are strings.
1.1546-If optional ERROR argument is NIL, return NIL instead of an error when the symbol is not found."
1.1547- (find-symbol* (standard-case-symbol-name name-designator)
1.1548- (etypecase package-designator
1.1549- ((or package symbol) package-designator)
1.1550- (string (standard-case-symbol-name package-designator)))
1.1551- error)))
1.1552-
1.1553-;;; timestamps: a REAL or a boolean where T=-infinity, NIL=+infinity
1.1554-(eval-when (#-lispworks :compile-toplevel :load-toplevel :execute)
1.1555- (deftype timestamp () '(or real boolean)))
1.1556-(with-upgradability ()
1.1557- (defun timestamp< (x y)
1.1558- (etypecase x
1.1559- ((eql t) (not (eql y t)))
1.1560- (real (etypecase y
1.1561- ((eql t) nil)
1.1562- (real (< x y))
1.1563- (null t)))
1.1564- (null nil)))
1.1565- (defun timestamps< (list) (loop :for y :in list :for x = nil :then y :always (timestamp< x y)))
1.1566- (defun timestamp*< (&rest list) (timestamps< list))
1.1567- (defun timestamp<= (x y) (not (timestamp< y x)))
1.1568- (defun earlier-timestamp (x y) (if (timestamp< x y) x y))
1.1569- (defun timestamps-earliest (list) (reduce 'earlier-timestamp list :initial-value nil))
1.1570- (defun earliest-timestamp (&rest list) (timestamps-earliest list))
1.1571- (defun later-timestamp (x y) (if (timestamp< x y) y x))
1.1572- (defun timestamps-latest (list) (reduce 'later-timestamp list :initial-value t))
1.1573- (defun latest-timestamp (&rest list) (timestamps-latest list))
1.1574- (define-modify-macro latest-timestamp-f (&rest timestamps) latest-timestamp))
1.1575-
1.1576-
1.1577-;;; Function designators
1.1578-(with-upgradability ()
1.1579- (defun ensure-function (fun &key (package :cl))
1.1580- "Coerce the object FUN into a function.
1.1581-
1.1582-If FUN is a FUNCTION, return it.
1.1583-If the FUN is a non-sequence literal constant, return constantly that,
1.1584-i.e. for a boolean keyword character number or pathname.
1.1585-Otherwise if FUN is a non-literally constant symbol, return its FDEFINITION.
1.1586-If FUN is a CONS, return the function that applies its CAR
1.1587-to the appended list of the rest of its CDR and the arguments,
1.1588-unless the CAR is LAMBDA, in which case the expression is evaluated.
1.1589-If FUN is a string, READ a form from it in the specified PACKAGE (default: CL)
1.1590-and EVAL that in a (FUNCTION ...) context."
1.1591- (etypecase fun
1.1592- (function fun)
1.1593- ((or boolean keyword character number pathname) (constantly fun))
1.1594- (hash-table #'(lambda (x) (gethash x fun)))
1.1595- (symbol (fdefinition fun))
1.1596- (cons (if (eq 'lambda (car fun))
1.1597- (eval fun)
1.1598- #'(lambda (&rest args) (apply (car fun) (append (cdr fun) args)))))
1.1599- (string (eval `(function ,(with-standard-io-syntax
1.1600- (let ((*package* (find-package package)))
1.1601- (read-from-string fun))))))))
1.1602-
1.1603- (defun access-at (object at)
1.1604- "Given an OBJECT and an AT specifier, list of successive accessors,
1.1605-call each accessor on the result of the previous calls.
1.1606-An accessor may be an integer, meaning a call to ELT,
1.1607-a keyword, meaning a call to GETF,
1.1608-NIL, meaning identity,
1.1609-a function or other symbol, meaning itself,
1.1610-or a list of a function designator and arguments, interpreted as per ENSURE-FUNCTION.
1.1611-As a degenerate case, the AT specifier may be an atom of a single such accessor
1.1612-instead of a list."
1.1613- (flet ((access (object accessor)
1.1614- (etypecase accessor
1.1615- (function (funcall accessor object))
1.1616- (integer (elt object accessor))
1.1617- (keyword (getf object accessor))
1.1618- (null object)
1.1619- (symbol (funcall accessor object))
1.1620- (cons (funcall (ensure-function accessor) object)))))
1.1621- (if (listp at)
1.1622- (dolist (accessor at object)
1.1623- (setf object (access object accessor)))
1.1624- (access object at))))
1.1625-
1.1626- (defun access-at-count (at)
1.1627- "From an AT specification, extract a COUNT of maximum number
1.1628-of sub-objects to read as per ACCESS-AT"
1.1629- (cond
1.1630- ((integerp at)
1.1631- (1+ at))
1.1632- ((and (consp at) (integerp (first at)))
1.1633- (1+ (first at)))))
1.1634-
1.1635- (defun call-function (function-spec &rest arguments)
1.1636- "Call the function designated by FUNCTION-SPEC as per ENSURE-FUNCTION,
1.1637-with the given ARGUMENTS"
1.1638- (apply (ensure-function function-spec) arguments))
1.1639-
1.1640- (defun call-functions (function-specs)
1.1641- "For each function in the list FUNCTION-SPECS, in order, call the function as per CALL-FUNCTION"
1.1642- (map () 'call-function function-specs))
1.1643-
1.1644- (defun register-hook-function (variable hook &optional call-now-p)
1.1645- "Push the HOOK function (a designator as per ENSURE-FUNCTION) onto the hook VARIABLE.
1.1646-When CALL-NOW-P is true, also call the function immediately."
1.1647- (pushnew hook (symbol-value variable) :test 'equal)
1.1648- (when call-now-p (call-function hook))))
1.1649-
1.1650-
1.1651-;;; CLOS
1.1652-(with-upgradability ()
1.1653- (defun coerce-class (class &key (package :cl) (super t) (error 'error))
1.1654- "Coerce CLASS to a class that is subclass of SUPER if specified,
1.1655-or invoke ERROR handler as per CALL-FUNCTION.
1.1656-
1.1657-A keyword designates the name a symbol, which when found in either PACKAGE, designates a class.
1.1658--- for backward compatibility, *PACKAGE* is also accepted for now, but this may go in the future.
1.1659-A string is read as a symbol while in PACKAGE, the symbol designates a class.
1.1660-
1.1661-A class object designates itself.
1.1662-NIL designates itself (no class).
1.1663-A symbol otherwise designates a class by name."
1.1664- (let* ((normalized
1.1665- (typecase class
1.1666- (keyword (or (find-symbol* class package nil)
1.1667- (find-symbol* class *package* nil)))
1.1668- (string (symbol-call :uiop :safe-read-from-string class :package package))
1.1669- (t class)))
1.1670- (found
1.1671- (etypecase normalized
1.1672- ((or standard-class built-in-class) normalized)
1.1673- ((or null keyword) nil)
1.1674- (symbol (find-class normalized nil nil))))
1.1675- (super-class
1.1676- (etypecase super
1.1677- ((or standard-class built-in-class) super)
1.1678- ((or null keyword) nil)
1.1679- (symbol (find-class super nil nil)))))
1.1680- #+allegro (when found (mop:finalize-inheritance found))
1.1681- (or (and found
1.1682- (or (eq super t) (#-cormanlisp subtypep #+cormanlisp cl::subclassp found super-class))
1.1683- found)
1.1684- (call-function error "Can't coerce ~S to a ~:[class~;subclass of ~:*~S~]" class super)))))
1.1685-
1.1686-
1.1687-;;; Hash-tables
1.1688-(with-upgradability ()
1.1689- (defun ensure-gethash (key table default)
1.1690- "Lookup the TABLE for a KEY as by GETHASH, but if not present,
1.1691-call the (possibly constant) function designated by DEFAULT as per CALL-FUNCTION,
1.1692-set the corresponding entry to the result in the table.
1.1693-Return two values: the entry after its optional computation, and whether it was found"
1.1694- (multiple-value-bind (value foundp) (gethash key table)
1.1695- (values
1.1696- (if foundp
1.1697- value
1.1698- (setf (gethash key table) (call-function default)))
1.1699- foundp)))
1.1700-
1.1701- (defun list-to-hash-set (list &aux (h (make-hash-table :test 'equal)))
1.1702- "Convert a LIST into hash-table that has the same elements when viewed as a set,
1.1703-up to the given equality TEST"
1.1704- (dolist (x list h) (setf (gethash x h) t))))
1.1705-
1.1706-
1.1707-;;; Lexicographic comparison of lists of numbers
1.1708-(with-upgradability ()
1.1709- (defun lexicographic< (element< x y)
1.1710- "Lexicographically compare two lists of using the function element< to compare elements.
1.1711-element< is a strict total order; the resulting order on X and Y will also be strict."
1.1712- (cond ((null y) nil)
1.1713- ((null x) t)
1.1714- ((funcall element< (car x) (car y)) t)
1.1715- ((funcall element< (car y) (car x)) nil)
1.1716- (t (lexicographic< element< (cdr x) (cdr y)))))
1.1717-
1.1718- (defun lexicographic<= (element< x y)
1.1719- "Lexicographically compare two lists of using the function element< to compare elements.
1.1720-element< is a strict total order; the resulting order on X and Y will be a non-strict total order."
1.1721- (not (lexicographic< element< y x))))
1.1722-
1.1723-
1.1724-;;; Simple style warnings
1.1725-(with-upgradability ()
1.1726- (define-condition simple-style-warning
1.1727- #+sbcl (sb-int:simple-style-warning) #-sbcl (simple-condition style-warning)
1.1728- ())
1.1729-
1.1730- (defun style-warn (datum &rest arguments)
1.1731- (etypecase datum
1.1732- (string (warn (make-condition 'simple-style-warning :format-control datum :format-arguments arguments)))
1.1733- (symbol (assert (subtypep datum 'style-warning)) (apply 'warn datum arguments))
1.1734- (style-warning (apply 'warn datum arguments)))))
1.1735-
1.1736-
1.1737-;;; Condition control
1.1738-
1.1739-(with-upgradability ()
1.1740- (defparameter +simple-condition-format-control-slot+
1.1741- #+abcl 'system::format-control
1.1742- #+allegro 'excl::format-control
1.1743- #+(or clasp ecl mkcl) 'si::format-control
1.1744- #+clisp 'system::$format-control
1.1745- #+clozure 'ccl::format-control
1.1746- #+(or cmucl scl) 'conditions::format-control
1.1747- #+(or gcl lispworks) 'conditions::format-string
1.1748- #+sbcl 'sb-kernel:format-control
1.1749- #-(or abcl allegro clasp clisp clozure cmucl ecl gcl lispworks mkcl sbcl scl) nil
1.1750- "Name of the slot for FORMAT-CONTROL in simple-condition")
1.1751-
1.1752- (defun match-condition-p (x condition)
1.1753- "Compare received CONDITION to some pattern X:
1.1754-a symbol naming a condition class,
1.1755-a simple vector of length 2, arguments to find-symbol* with result as above,
1.1756-or a string describing the format-control of a simple-condition."
1.1757- (etypecase x
1.1758- (symbol (typep condition x))
1.1759- ((simple-vector 2)
1.1760- (ignore-errors (typep condition (find-symbol* (svref x 0) (svref x 1) nil))))
1.1761- (function (funcall x condition))
1.1762- (string (and (typep condition 'simple-condition)
1.1763- ;; On SBCL, it's always set and the check triggers a warning
1.1764- #+(or allegro clozure cmucl lispworks scl)
1.1765- (slot-boundp condition +simple-condition-format-control-slot+)
1.1766- (ignore-errors (equal (simple-condition-format-control condition) x))))))
1.1767-
1.1768- (defun match-any-condition-p (condition conditions)
1.1769- "match CONDITION against any of the patterns of CONDITIONS supplied"
1.1770- (loop :for x :in conditions :thereis (match-condition-p x condition)))
1.1771-
1.1772- (defun call-with-muffled-conditions (thunk conditions)
1.1773- "calls the THUNK in a context where the CONDITIONS are muffled"
1.1774- (handler-bind ((t #'(lambda (c) (when (match-any-condition-p c conditions)
1.1775- (muffle-warning c)))))
1.1776- (funcall thunk)))
1.1777-
1.1778- (defmacro with-muffled-conditions ((conditions) &body body)
1.1779- "Shorthand syntax for CALL-WITH-MUFFLED-CONDITIONS"
1.1780- `(call-with-muffled-conditions #'(lambda () ,@body) ,conditions)))
1.1781-
1.1782-;;; Conditions
1.1783-
1.1784-(with-upgradability ()
1.1785- (define-condition not-implemented-error (error)
1.1786- ((functionality :initarg :functionality)
1.1787- (format-control :initarg :format-control)
1.1788- (format-arguments :initarg :format-arguments))
1.1789- (:report (lambda (condition stream)
1.1790- (format stream "Not (currently) implemented on ~A: ~S~@[ ~?~]"
1.1791- (nth-value 1 (symbol-call :uiop :implementation-type))
1.1792- (slot-value condition 'functionality)
1.1793- (slot-value condition 'format-control)
1.1794- (slot-value condition 'format-arguments)))))
1.1795-
1.1796- (defun not-implemented-error (functionality &optional format-control &rest format-arguments)
1.1797- "Signal an error because some FUNCTIONALITY is not implemented in the current version
1.1798-of the software on the current platform; it may or may not be implemented in different combinations
1.1799-of version of the software and of the underlying platform. Optionally, report a formatted error
1.1800-message."
1.1801- (error 'not-implemented-error
1.1802- :functionality functionality
1.1803- :format-control format-control
1.1804- :format-arguments format-arguments))
1.1805-
1.1806- (define-condition parameter-error (error)
1.1807- ((functionality :initarg :functionality)
1.1808- (format-control :initarg :format-control)
1.1809- (format-arguments :initarg :format-arguments))
1.1810- (:report (lambda (condition stream)
1.1811- (apply 'format stream
1.1812- (slot-value condition 'format-control)
1.1813- (slot-value condition 'functionality)
1.1814- (slot-value condition 'format-arguments)))))
1.1815-
1.1816- ;; Note that functionality MUST be passed as the second argument to parameter-error, just after
1.1817- ;; the format-control. If you want it to not appear in first position in actual message, use
1.1818- ;; ~* and ~:* to adjust parameter order.
1.1819- (defun parameter-error (format-control functionality &rest format-arguments)
1.1820- "Signal an error because some FUNCTIONALITY or its specific implementation on a given underlying
1.1821-platform does not accept a given parameter or combination of parameters. Report a formatted error
1.1822-message, that takes the functionality as its first argument (that can be skipped with ~*)."
1.1823- (error 'parameter-error
1.1824- :functionality functionality
1.1825- :format-control format-control
1.1826- :format-arguments format-arguments)))
1.1827-
1.1828-(with-upgradability ()
1.1829- (defun boolean-to-feature-expression (value)
1.1830- "Converts a boolean VALUE to a form suitable for testing with #+."
1.1831- (if value
1.1832- '(:and)
1.1833- '(:or)))
1.1834-
1.1835- (defun symbol-test-to-feature-expression (name package)
1.1836- "Check if a symbol with a given NAME exists in PACKAGE and returns a
1.1837-form suitable for testing with #+."
1.1838- (boolean-to-feature-expression
1.1839- (find-symbol* name package nil))))
1.1840-(uiop/package:define-package :uiop/version
1.1841- (:recycle :uiop/version :uiop/utility :asdf)
1.1842- (:use :uiop/common-lisp :uiop/package :uiop/utility)
1.1843- (:export
1.1844- #:*uiop-version*
1.1845- #:parse-version #:unparse-version #:version< #:version<= #:version= ;; version support, moved from uiop/utility
1.1846- #:next-version
1.1847- #:deprecated-function-condition #:deprecated-function-name ;; deprecation control
1.1848- #:deprecated-function-style-warning #:deprecated-function-warning
1.1849- #:deprecated-function-error #:deprecated-function-should-be-deleted
1.1850- #:version-deprecation #:with-deprecation))
1.1851-(in-package :uiop/version)
1.1852-
1.1853-(with-upgradability ()
1.1854- (defparameter *uiop-version* "3.3.6")
1.1855-
1.1856- (defun unparse-version (version-list)
1.1857- "From a parsed version (a list of natural numbers), compute the version string"
1.1858- (format nil "~{~D~^.~}" version-list))
1.1859-
1.1860- (defun parse-version (version-string &optional on-error)
1.1861- "Parse a VERSION-STRING as a series of natural numbers separated by dots.
1.1862-Return a (non-null) list of integers if the string is valid;
1.1863-otherwise return NIL.
1.1864-
1.1865-When invalid, ON-ERROR is called as per CALL-FUNCTION before to return NIL,
1.1866-with format arguments explaining why the version is invalid.
1.1867-ON-ERROR is also called if the version is not canonical
1.1868-in that it doesn't print back to itself, but the list is returned anyway."
1.1869- (block nil
1.1870- (unless (stringp version-string)
1.1871- (call-function on-error "~S: ~S is not a string" 'parse-version version-string)
1.1872- (return))
1.1873- (unless (loop :for prev = nil :then c :for c :across version-string
1.1874- :always (or (digit-char-p c)
1.1875- (and (eql c #\.) prev (not (eql prev #\.))))
1.1876- :finally (return (and c (digit-char-p c))))
1.1877- (call-function on-error "~S: ~S doesn't follow asdf version numbering convention"
1.1878- 'parse-version version-string)
1.1879- (return))
1.1880- (let* ((version-list
1.1881- (mapcar #'parse-integer (split-string version-string :separator ".")))
1.1882- (normalized-version (unparse-version version-list)))
1.1883- (unless (equal version-string normalized-version)
1.1884- (call-function on-error "~S: ~S contains leading zeros" 'parse-version version-string))
1.1885- version-list)))
1.1886-
1.1887- (defun next-version (version)
1.1888- "When VERSION is not nil, it is a string, then parse it as a version, compute the next version
1.1889-and return it as a string."
1.1890- (when version
1.1891- (let ((version-list (parse-version version)))
1.1892- (incf (car (last version-list)))
1.1893- (unparse-version version-list))))
1.1894-
1.1895- (defun version< (version1 version2)
1.1896- "Given two version strings, return T if the second is strictly newer"
1.1897- (let ((v1 (parse-version version1 nil))
1.1898- (v2 (parse-version version2 nil)))
1.1899- (lexicographic< '< v1 v2)))
1.1900-
1.1901- (defun version<= (version1 version2)
1.1902- "Given two version strings, return T if the second is newer or the same"
1.1903- (not (version< version2 version1))))
1.1904-
1.1905- (defun version= (version1 version2)
1.1906- "Given two version strings, return T if the first is newer or the same and
1.1907-the second is also newer or the same."
1.1908- (and (version<= version1 version2)
1.1909- (version<= version2 version1)))
1.1910-
1.1911-
1.1912-(with-upgradability ()
1.1913- (define-condition deprecated-function-condition (condition)
1.1914- ((name :initarg :name :reader deprecated-function-name)))
1.1915- (define-condition deprecated-function-style-warning (deprecated-function-condition style-warning) ())
1.1916- (define-condition deprecated-function-warning (deprecated-function-condition warning) ())
1.1917- (define-condition deprecated-function-error (deprecated-function-condition error) ())
1.1918- (define-condition deprecated-function-should-be-deleted (deprecated-function-condition error) ())
1.1919-
1.1920- (defun deprecated-function-condition-kind (type)
1.1921- (ecase type
1.1922- ((deprecated-function-style-warning) :style-warning)
1.1923- ((deprecated-function-warning) :warning)
1.1924- ((deprecated-function-error) :error)
1.1925- ((deprecated-function-should-be-deleted) :delete)))
1.1926-
1.1927- (defmethod print-object ((c deprecated-function-condition) stream)
1.1928- (let ((name (deprecated-function-name c)))
1.1929- (cond
1.1930- (*print-readably*
1.1931- (let ((fmt "#.(make-condition '~S :name ~S)")
1.1932- (args (list (type-of c) name)))
1.1933- (if *read-eval*
1.1934- (apply 'format stream fmt args)
1.1935- (error "Can't print ~?" fmt args))))
1.1936- (*print-escape*
1.1937- (print-unreadable-object (c stream :type t) (format stream ":name ~S" name)))
1.1938- (t
1.1939- (let ((*package* (find-package :cl))
1.1940- (type (type-of c)))
1.1941- (format stream
1.1942- (if (eq type 'deprecated-function-should-be-deleted)
1.1943- "~A: Still defining deprecated function~:P ~{~S~^ ~} that promised to delete"
1.1944- "~A: Using deprecated function ~S -- please update your code to use a newer API.~
1.1945-~@[~%The docstring for this function says:~%~A~%~]")
1.1946- type name (when (symbolp name) (documentation name 'function))))))))
1.1947-
1.1948- (defun notify-deprecated-function (status name)
1.1949- (ecase status
1.1950- ((nil) nil)
1.1951- ((:style-warning) (style-warn 'deprecated-function-style-warning :name name))
1.1952- ((:warning) (warn 'deprecated-function-warning :name name))
1.1953- ((:error) (cerror "USE FUNCTION ANYWAY" 'deprecated-function-error :name name))))
1.1954-
1.1955- (defun version-deprecation (version &key (style-warning nil)
1.1956- (warning (next-version style-warning))
1.1957- (error (next-version warning))
1.1958- (delete (next-version error)))
1.1959- "Given a VERSION string, and the starting versions for notifying the programmer of
1.1960-various levels of deprecation, return the current level of deprecation as per WITH-DEPRECATION
1.1961-that is the highest level that has a declared version older than the specified version.
1.1962-Each start version for a level of deprecation can be specified by a keyword argument, or
1.1963-if left unspecified, will be the NEXT-VERSION of the immediate lower level of deprecation."
1.1964- (cond
1.1965- ((and delete (version<= delete version)) :delete)
1.1966- ((and error (version<= error version)) :error)
1.1967- ((and warning (version<= warning version)) :warning)
1.1968- ((and style-warning (version<= style-warning version)) :style-warning)))
1.1969-
1.1970- (defmacro with-deprecation ((level) &body definitions)
1.1971- "Given a deprecation LEVEL (a form to be EVAL'ed at macro-expansion time), instrument the
1.1972-DEFUN and DEFMETHOD forms in DEFINITIONS to notify the programmer of the deprecation of the function
1.1973-when it is compiled or called.
1.1974-
1.1975-Increasing levels (as result from evaluating LEVEL) are: NIL (not deprecated yet),
1.1976-:STYLE-WARNING (a style warning is issued when used), :WARNING (a full warning is issued when used),
1.1977-:ERROR (a continuable error instead), and :DELETE (it's an error if the code is still there while
1.1978-at that level).
1.1979-
1.1980-Forms other than DEFUN and DEFMETHOD are not instrumented, and you can protect a DEFUN or DEFMETHOD
1.1981-from instrumentation by enclosing it in a PROGN."
1.1982- (let ((level (eval level)))
1.1983- (check-type level (member nil :style-warning :warning :error :delete))
1.1984- (when (eq level :delete)
1.1985- (error 'deprecated-function-should-be-deleted :name
1.1986- (mapcar 'second
1.1987- (remove-if-not #'(lambda (x) (member x '(defun defmethod)))
1.1988- definitions :key 'first))))
1.1989- (labels ((instrument (name head body whole)
1.1990- (if level
1.1991- (let ((notifiedp
1.1992- (intern (format nil "*~A-~A-~A-~A*"
1.1993- :deprecated-function level name :notified-p))))
1.1994- (multiple-value-bind (remaining-forms declarations doc-string)
1.1995- (parse-body body :documentation t :whole whole)
1.1996- `(progn
1.1997- (defparameter ,notifiedp nil)
1.1998- ;; tell some implementations to use the compiler-macro
1.1999- (declaim (inline ,name))
1.2000- (define-compiler-macro ,name (&whole form &rest args)
1.2001- (declare (ignore args))
1.2002- (notify-deprecated-function ,level ',name)
1.2003- form)
1.2004- (,@head ,@(when doc-string (list doc-string)) ,@declarations
1.2005- (unless ,notifiedp
1.2006- (setf ,notifiedp t)
1.2007- (notify-deprecated-function ,level ',name))
1.2008- ,@remaining-forms))))
1.2009- `(progn
1.2010- (eval-when (:compile-toplevel :load-toplevel :execute)
1.2011- (setf (compiler-macro-function ',name) nil))
1.2012- (declaim (notinline ,name))
1.2013- (,@head ,@body)))))
1.2014- `(progn
1.2015- ,@(loop :for form :in definitions :collect
1.2016- (cond
1.2017- ((and (consp form) (eq (car form) 'defun))
1.2018- (instrument (second form) (subseq form 0 3) (subseq form 3) form))
1.2019- ((and (consp form) (eq (car form) 'defmethod))
1.2020- (let ((body-start (if (listp (third form)) 3 4)))
1.2021- (instrument (second form)
1.2022- (subseq form 0 body-start)
1.2023- (subseq form body-start)
1.2024- form)))
1.2025- (t
1.2026- form))))))))
1.2027-;;;; ---------------------------------------------------------------------------
1.2028-;;;; Access to the Operating System
1.2029-
1.2030-(uiop/package:define-package :uiop/os
1.2031- (:use :uiop/common-lisp :uiop/package :uiop/utility)
1.2032- (:export
1.2033- #:featurep #:os-unix-p #:os-macosx-p #:os-windows-p #:os-genera-p #:detect-os ;; features
1.2034- #:os-cond
1.2035- #:getenv #:getenvp ;; environment variables
1.2036- #:implementation-identifier ;; implementation identifier
1.2037- #:implementation-type #:*implementation-type*
1.2038- #:operating-system #:architecture #:lisp-version-string
1.2039- #:hostname #:getcwd #:chdir
1.2040- ;; Windows shortcut support
1.2041- #:read-null-terminated-string #:read-little-endian
1.2042- #:parse-file-location-info #:parse-windows-shortcut))
1.2043-(in-package :uiop/os)
1.2044-
1.2045-;;; Features
1.2046-(with-upgradability ()
1.2047- (defun featurep (x &optional (*features* *features*))
1.2048- "Checks whether a feature expression X is true with respect to the *FEATURES* set,
1.2049-as per the CLHS standard for #+ and #-. Beware that just like the CLHS,
1.2050-we assume symbols from the KEYWORD package are used, but that unless you're using #+/#-
1.2051-your reader will not have magically used the KEYWORD package, so you need specify
1.2052-keywords explicitly."
1.2053- (cond
1.2054- ((atom x) (and (member x *features*) t))
1.2055- ((eq :not (car x)) (assert (null (cddr x))) (not (featurep (cadr x))))
1.2056- ((eq :or (car x)) (some #'featurep (cdr x)))
1.2057- ((eq :and (car x)) (every #'featurep (cdr x)))
1.2058- (t (parameter-error "~S: malformed feature specification ~S" 'featurep x))))
1.2059-
1.2060- ;; Starting with UIOP 3.1.5, these are runtime tests.
1.2061- ;; You may bind *features* with a copy of what your target system offers to test its properties.
1.2062- (defun os-macosx-p ()
1.2063- "Is the underlying operating system MacOS X?"
1.2064- ;; OS-MACOSX is not mutually exclusive with OS-UNIX,
1.2065- ;; in fact the former implies the latter.
1.2066- (featurep '(:or :darwin (:and :allegro :macosx) (:and :clisp :macos))))
1.2067-
1.2068- (defun os-unix-p ()
1.2069- "Is the underlying operating system some Unix variant?"
1.2070- (or (featurep '(:or :unix :cygwin :haiku)) (os-macosx-p)))
1.2071-
1.2072- (defun os-windows-p ()
1.2073- "Is the underlying operating system Microsoft Windows?"
1.2074- (and (not (os-unix-p)) (featurep '(:or :win32 :windows :mswindows :mingw32 :mingw64))))
1.2075-
1.2076- (defun os-genera-p ()
1.2077- "Is the underlying operating system Genera (running on a Symbolics Lisp Machine)?"
1.2078- (featurep :genera))
1.2079-
1.2080- (defun os-oldmac-p ()
1.2081- "Is the underlying operating system an (emulated?) MacOS 9 or earlier?"
1.2082- (featurep :mcl))
1.2083-
1.2084- (defun os-haiku-p ()
1.2085- "Is the underlying operating system Haiku?"
1.2086- (featurep :haiku))
1.2087-
1.2088- (defun os-mezzano-p ()
1.2089- "Is the underlying operating system Mezzano?"
1.2090- (featurep :mezzano))
1.2091-
1.2092- (defun detect-os ()
1.2093- "Detects the current operating system. Only needs be run at compile-time,
1.2094-except on ABCL where it might change between FASL compilation and runtime."
1.2095- (loop :with o
1.2096- :for (feature . detect) :in '((:os-unix . os-unix-p) (:os-macosx . os-macosx-p)
1.2097- (:os-windows . os-windows-p)
1.2098- (:os-genera . os-genera-p) (:os-oldmac . os-oldmac-p)
1.2099- (:os-haiku . os-haiku-p)
1.2100- (:os-mezzano . os-mezzano-p))
1.2101- :when (and (or (not o) (eq feature :os-macosx) (eq feature :os-haiku)) (funcall detect))
1.2102- :do (setf o feature) (pushnew feature *features*)
1.2103- :else :do (setf *features* (remove feature *features*))
1.2104- :finally
1.2105- (return (or o (error "Congratulations for trying ASDF on an operating system~%~
1.2106-that is neither Unix, nor Windows, nor Genera, nor even old MacOS.~%Now you port it.")))))
1.2107-
1.2108- (defmacro os-cond (&rest clauses)
1.2109- #+abcl `(cond ,@clauses)
1.2110- #-abcl (loop :for (test . body) :in clauses :when (eval test) :return `(progn ,@body)))
1.2111-
1.2112- (detect-os))
1.2113-
1.2114-;;;; Environment variables: getting them, and parsing them.
1.2115-(with-upgradability ()
1.2116- (defun getenv (x)
1.2117- "Query the environment, as in C getenv.
1.2118-Beware: may return empty string if a variable is present but empty;
1.2119-use getenvp to return NIL in such a case."
1.2120- (declare (ignorable x))
1.2121- #+(or abcl clasp clisp ecl xcl) (ext:getenv x)
1.2122- #+allegro (sys:getenv x)
1.2123- #+clozure (ccl:getenv x)
1.2124- #+cmucl (unix:unix-getenv x)
1.2125- #+scl (cdr (assoc x ext:*environment-list* :test #'string=))
1.2126- #+cormanlisp
1.2127- (let* ((buffer (ct:malloc 1))
1.2128- (cname (ct:lisp-string-to-c-string x))
1.2129- (needed-size (win:getenvironmentvariable cname buffer 0))
1.2130- (buffer1 (ct:malloc (1+ needed-size))))
1.2131- (prog1 (if (zerop (win:getenvironmentvariable cname buffer1 needed-size))
1.2132- nil
1.2133- (ct:c-string-to-lisp-string buffer1))
1.2134- (ct:free buffer)
1.2135- (ct:free buffer1)))
1.2136- #+gcl (system:getenv x)
1.2137- #+(or genera mezzano) nil
1.2138- #+lispworks (lispworks:environment-variable x)
1.2139- #+mcl (ccl:with-cstrs ((name x))
1.2140- (let ((value (_getenv name)))
1.2141- (unless (ccl:%null-ptr-p value)
1.2142- (ccl:%get-cstring value))))
1.2143- #+mkcl (#.(or (find-symbol* 'getenv :si nil) (find-symbol* 'getenv :mk-ext nil)) x)
1.2144- #+sbcl (sb-ext:posix-getenv x)
1.2145- #-(or abcl allegro clasp clisp clozure cmucl cormanlisp ecl gcl genera lispworks mcl mezzano mkcl sbcl scl xcl)
1.2146- (not-implemented-error 'getenv))
1.2147-
1.2148- (defsetf getenv (x) (val)
1.2149- "Set an environment variable."
1.2150- (declare (ignorable x val))
1.2151- #+allegro `(setf (sys:getenv ,x) ,val)
1.2152- #+clasp `(ext:setenv ,x ,val)
1.2153- #+clisp `(system::setenv ,x ,val)
1.2154- #+clozure `(ccl:setenv ,x ,val)
1.2155- #+cmucl `(unix:unix-setenv ,x ,val 1)
1.2156- #+(or ecl clasp) `(ext:setenv ,x ,val)
1.2157- #+lispworks `(setf (lispworks:environment-variable ,x) ,val)
1.2158- #+mkcl `(mkcl:setenv ,x ,val)
1.2159- #+sbcl `(progn (require :sb-posix) (symbol-call :sb-posix :setenv ,x ,val 1))
1.2160- #-(or allegro clasp clisp clozure cmucl ecl lispworks mkcl sbcl)
1.2161- '(not-implemented-error '(setf getenv)))
1.2162-
1.2163- (defun getenvp (x)
1.2164- "Predicate that is true if the named variable is present in the libc environment,
1.2165-then returning the non-empty string value of the variable"
1.2166- (let ((g (getenv x))) (and (not (emptyp g)) g))))
1.2167-
1.2168-
1.2169-;;;; implementation-identifier
1.2170-;;
1.2171-;; produce a string to identify current implementation.
1.2172-;; Initially stolen from SLIME's SWANK, completely rewritten since.
1.2173-;; We're back to runtime checking, for the sake of e.g. ABCL.
1.2174-
1.2175-(with-upgradability ()
1.2176- (defun first-feature (feature-sets)
1.2177- "A helper for various feature detection functions"
1.2178- (dolist (x feature-sets)
1.2179- (multiple-value-bind (short long feature-expr)
1.2180- (if (consp x)
1.2181- (values (first x) (second x) (cons :or (rest x)))
1.2182- (values x x x))
1.2183- (when (featurep feature-expr)
1.2184- (return (values short long))))))
1.2185-
1.2186- (defun implementation-type ()
1.2187- "The type of Lisp implementation used, as a short UIOP-standardized keyword"
1.2188- (first-feature
1.2189- '(:abcl (:acl :allegro) (:ccl :clozure) :clisp (:corman :cormanlisp)
1.2190- (:cmu :cmucl :cmu) :clasp :ecl :gcl
1.2191- (:lwpe :lispworks-personal-edition) (:lw :lispworks)
1.2192- :mcl :mezzano :mkcl :sbcl :scl (:smbx :symbolics) :xcl)))
1.2193-
1.2194- (defvar *implementation-type* (implementation-type)
1.2195- "The type of Lisp implementation used, as a short UIOP-standardized keyword")
1.2196-
1.2197- (defun operating-system ()
1.2198- "The operating system of the current host"
1.2199- (first-feature
1.2200- '(:cygwin
1.2201- (:win :windows :mswindows :win32 :mingw32) ;; try cygwin first!
1.2202- (:linux :linux :linux-target) ;; for GCL at least, must appear before :bsd
1.2203- (:macosx :macosx :darwin :darwin-target :apple) ; also before :bsd
1.2204- (:solaris :solaris :sunos)
1.2205- (:bsd :bsd :freebsd :netbsd :openbsd :dragonfly)
1.2206- :unix
1.2207- :genera
1.2208- :mezzano)))
1.2209-
1.2210- (defun architecture ()
1.2211- "The CPU architecture of the current host"
1.2212- (first-feature
1.2213- '((:x64 :x86-64 :x86_64 :x8664-target :amd64 (:and :word-size=64 :pc386))
1.2214- (:x86 :x86 :i386 :i486 :i586 :i686 :pentium3 :pentium4 :pc386 :iapx386 :x8632-target)
1.2215- (:ppc64 :ppc64 :ppc64-target) (:ppc32 :ppc32 :ppc32-target :ppc :powerpc)
1.2216- :hppa64 :hppa :sparc64 (:sparc32 :sparc32 :sparc)
1.2217- :mipsel :mipseb :mips :alpha
1.2218- (:arm64 :arm64 :aarch64 :armv8l :armv8b :aarch64_be :|aarch64|)
1.2219- (:arm :arm :arm-target) :vlm :imach
1.2220- ;; Java comes last: if someone uses C via CFFI or otherwise JNA or JNI,
1.2221- ;; we may have to segregate the code still by architecture.
1.2222- (:java :java :java-1.4 :java-1.5 :java-1.6 :java-1.7))))
1.2223-
1.2224- #+clozure
1.2225- (defun ccl-fasl-version ()
1.2226- ;; the fasl version is target-dependent from CCL 1.8 on.
1.2227- (or (let ((s 'ccl::target-fasl-version))
1.2228- (and (fboundp s) (funcall s)))
1.2229- (and (boundp 'ccl::fasl-version)
1.2230- (symbol-value 'ccl::fasl-version))
1.2231- (error "Can't determine fasl version.")))
1.2232-
1.2233- (defun lisp-version-string ()
1.2234- "return a string that identifies the current Lisp implementation version"
1.2235- (let ((s (lisp-implementation-version)))
1.2236- (car ; as opposed to OR, this idiom prevents some unreachable code warning
1.2237- (list
1.2238- #+allegro
1.2239- (format nil "~A~@[~A~]~@[~A~]~@[~A~]"
1.2240- excl::*common-lisp-version-number*
1.2241- ;; M means "modern", as opposed to ANSI-compatible mode (which I consider default)
1.2242- (and (eq excl:*current-case-mode* :case-sensitive-lower) "M")
1.2243- ;; Note if not using International ACL
1.2244- ;; see http://www.franz.com/support/documentation/8.1/doc/operators/excl/ics-target-case.htm
1.2245- (excl:ics-target-case (:-ics "8"))
1.2246- (and (member :smp *features*) "S"))
1.2247- #+armedbear (format nil "~a-fasl~a" s system::*fasl-version*)
1.2248- #+clisp
1.2249- (subseq s 0 (position #\space s)) ; strip build information (date, etc.)
1.2250- #+clozure
1.2251- (format nil "~d.~d-f~d" ; shorten for windows
1.2252- ccl::*openmcl-major-version*
1.2253- ccl::*openmcl-minor-version*
1.2254- (logand (ccl-fasl-version) #xFF))
1.2255- #+cmucl (substitute #\- #\/ s)
1.2256- #+scl (format nil "~A~A" s
1.2257- ;; ANSI upper case vs lower case.
1.2258- (ecase ext:*case-mode* (:upper "") (:lower "l")))
1.2259- #+ecl (format nil "~A~@[-~A~]" s
1.2260- (let ((vcs-id (ext:lisp-implementation-vcs-id)))
1.2261- (unless (equal vcs-id "UNKNOWN")
1.2262- (subseq vcs-id 0 (min (length vcs-id) 8)))))
1.2263- #+gcl (subseq s (1+ (position #\space s)))
1.2264- #+genera
1.2265- (multiple-value-bind (major minor) (sct:get-system-version "System")
1.2266- (format nil "~D.~D" major minor))
1.2267- #+mcl (subseq s 8) ; strip the leading "Version "
1.2268- #+mezzano (format nil "~A-~D"
1.2269- (subseq s 0 (position #\space s)) ; strip commit hash
1.2270- sys.int::*llf-version*)
1.2271- ;; seems like there should be a shorter way to do this, like ACALL.
1.2272- #+mkcl (or
1.2273- (let ((fname (find-symbol* '#:git-describe-this-mkcl :mkcl nil)))
1.2274- (when (and fname (fboundp fname))
1.2275- (funcall fname)))
1.2276- s)
1.2277- s))))
1.2278-
1.2279- (defun implementation-identifier ()
1.2280- "Return a string that identifies the ABI of the current implementation,
1.2281-suitable for use as a directory name to segregate Lisp FASLs, C dynamic libraries, etc."
1.2282- (substitute-if
1.2283- #\_ #'(lambda (x) (find x " /:;&^\\|?<>(){}[]$#`'\""))
1.2284- (format nil "~(~a~@{~@[-~a~]~}~)"
1.2285- (or (implementation-type) (lisp-implementation-type))
1.2286- (lisp-version-string)
1.2287- (or (operating-system) (software-type))
1.2288- (or (architecture) (machine-type))))))
1.2289-
1.2290-
1.2291-;;;; Other system information
1.2292-
1.2293-(with-upgradability ()
1.2294- (defun hostname ()
1.2295- "return the hostname of the current host"
1.2296- #+(or abcl clasp clozure cmucl ecl genera lispworks mcl mezzano mkcl sbcl scl xcl) (machine-instance)
1.2297- #+cormanlisp "localhost" ;; is there a better way? Does it matter?
1.2298- #+allegro (symbol-call :excl.osi :gethostname)
1.2299- #+clisp (first (split-string (machine-instance) :separator " "))
1.2300- #+gcl (system:gethostname)))
1.2301-
1.2302-
1.2303-;;; Current directory
1.2304-(with-upgradability ()
1.2305-
1.2306- #+cmucl
1.2307- (defun parse-unix-namestring* (unix-namestring)
1.2308- "variant of LISP::PARSE-UNIX-NAMESTRING that returns a pathname object"
1.2309- (multiple-value-bind (host device directory name type version)
1.2310- (lisp::parse-unix-namestring unix-namestring 0 (length unix-namestring))
1.2311- (make-pathname :host (or host lisp::*unix-host*) :device device
1.2312- :directory directory :name name :type type :version version)))
1.2313-
1.2314- (defun getcwd ()
1.2315- "Get the current working directory as per POSIX getcwd(3), as a pathname object"
1.2316- (or #+(or abcl genera mezzano xcl) (truename *default-pathname-defaults*) ;; d-p-d is canonical!
1.2317- #+allegro (excl::current-directory)
1.2318- #+clisp (ext:default-directory)
1.2319- #+clozure (ccl:current-directory)
1.2320- #+(or cmucl scl) (#+cmucl parse-unix-namestring* #+scl lisp::parse-unix-namestring
1.2321- (strcat (nth-value 1 (unix:unix-current-directory)) "/"))
1.2322- #+cormanlisp (pathname (pl::get-current-directory)) ;; Q: what type does it return?
1.2323- #+(or clasp ecl) (ext:getcwd)
1.2324- #+gcl (let ((*default-pathname-defaults* #p"")) (truename #p""))
1.2325- #+lispworks (hcl:get-working-directory)
1.2326- #+mkcl (mk-ext:getcwd)
1.2327- #+sbcl (sb-ext:parse-native-namestring (sb-unix:posix-getcwd/))
1.2328- #+xcl (extensions:current-directory)
1.2329- (not-implemented-error 'getcwd)))
1.2330-
1.2331- (defun chdir (x)
1.2332- "Change current directory, as per POSIX chdir(2), to a given pathname object"
1.2333- (if-let (x (pathname x))
1.2334- #+(or abcl genera mezzano xcl) (setf *default-pathname-defaults* (truename x)) ;; d-p-d is canonical!
1.2335- #+allegro (excl:chdir x)
1.2336- #+clisp (ext:cd x)
1.2337- #+clozure (setf (ccl:current-directory) x)
1.2338- #+(or cmucl scl) (unix:unix-chdir (ext:unix-namestring x))
1.2339- #+cormanlisp (unless (zerop (win32::_chdir (namestring x)))
1.2340- (error "Could not set current directory to ~A" x))
1.2341- #+ecl (ext:chdir x)
1.2342- #+clasp (ext:chdir x t)
1.2343- #+gcl (system:chdir x)
1.2344- #+lispworks (hcl:change-directory x)
1.2345- #+mkcl (mk-ext:chdir x)
1.2346- #+sbcl (progn (require :sb-posix) (symbol-call :sb-posix :chdir (sb-ext:native-namestring x)))
1.2347- #-(or abcl allegro clasp clisp clozure cmucl cormanlisp ecl gcl genera lispworks mkcl sbcl scl xcl)
1.2348- (not-implemented-error 'chdir))))
1.2349-
1.2350-
1.2351-;;;; -----------------------------------------------------------------
1.2352-;;;; Windows shortcut support. Based on:
1.2353-;;;;
1.2354-;;;; Jesse Hager: The Windows Shortcut File Format.
1.2355-;;;; http://www.wotsit.org/list.asp?fc=13
1.2356-
1.2357-#-(or clisp genera) ; CLISP doesn't need it, and READ-SEQUENCE annoys old Genera that doesn't need it
1.2358-(with-upgradability ()
1.2359- (defparameter *link-initial-dword* 76)
1.2360- (defparameter *link-guid* #(1 20 2 0 0 0 0 0 192 0 0 0 0 0 0 70))
1.2361-
1.2362- (defun read-null-terminated-string (s)
1.2363- "Read a null-terminated string from an octet stream S"
1.2364- ;; note: doesn't play well with UNICODE
1.2365- (with-output-to-string (out)
1.2366- (loop :for code = (read-byte s)
1.2367- :until (zerop code)
1.2368- :do (write-char (code-char code) out))))
1.2369-
1.2370- (defun read-little-endian (s &optional (bytes 4))
1.2371- "Read a number in little-endian format from an byte (octet) stream S,
1.2372-the number having BYTES octets (defaulting to 4)."
1.2373- (loop :for i :from 0 :below bytes
1.2374- :sum (ash (read-byte s) (* 8 i))))
1.2375-
1.2376- (defun parse-file-location-info (s)
1.2377- "helper to parse-windows-shortcut"
1.2378- (let ((start (file-position s))
1.2379- (total-length (read-little-endian s))
1.2380- (end-of-header (read-little-endian s))
1.2381- (fli-flags (read-little-endian s))
1.2382- (local-volume-offset (read-little-endian s))
1.2383- (local-offset (read-little-endian s))
1.2384- (network-volume-offset (read-little-endian s))
1.2385- (remaining-offset (read-little-endian s)))
1.2386- (declare (ignore total-length end-of-header local-volume-offset))
1.2387- (unless (zerop fli-flags)
1.2388- (cond
1.2389- ((logbitp 0 fli-flags)
1.2390- (file-position s (+ start local-offset)))
1.2391- ((logbitp 1 fli-flags)
1.2392- (file-position s (+ start
1.2393- network-volume-offset
1.2394- #x14))))
1.2395- (strcat (read-null-terminated-string s)
1.2396- (progn
1.2397- (file-position s (+ start remaining-offset))
1.2398- (read-null-terminated-string s))))))
1.2399-
1.2400- (defun parse-windows-shortcut (pathname)
1.2401- "From a .lnk windows shortcut, extract the pathname linked to"
1.2402- ;; NB: doesn't do much checking & doesn't look like it will work well with UNICODE.
1.2403- (with-open-file (s pathname :element-type '(unsigned-byte 8))
1.2404- (handler-case
1.2405- (when (and (= (read-little-endian s) *link-initial-dword*)
1.2406- (let ((header (make-array (length *link-guid*))))
1.2407- (read-sequence header s)
1.2408- (equalp header *link-guid*)))
1.2409- (let ((flags (read-little-endian s)))
1.2410- (file-position s 76) ;skip rest of header
1.2411- (when (logbitp 0 flags)
1.2412- ;; skip shell item id list
1.2413- (let ((length (read-little-endian s 2)))
1.2414- (file-position s (+ length (file-position s)))))
1.2415- (cond
1.2416- ((logbitp 1 flags)
1.2417- (parse-file-location-info s))
1.2418- (t
1.2419- (when (logbitp 2 flags)
1.2420- ;; skip description string
1.2421- (let ((length (read-little-endian s 2)))
1.2422- (file-position s (+ length (file-position s)))))
1.2423- (when (logbitp 3 flags)
1.2424- ;; finally, our pathname
1.2425- (let* ((length (read-little-endian s 2))
1.2426- (buffer (make-array length)))
1.2427- (read-sequence buffer s)
1.2428- (map 'string #'code-char buffer)))))))
1.2429- (end-of-file (c)
1.2430- (declare (ignore c))
1.2431- nil)))))
1.2432-
1.2433-
1.2434-;;;; -------------------------------------------------------------------------
1.2435-;;;; Portability layer around Common Lisp pathnames
1.2436-;; This layer allows for portable manipulation of pathname objects themselves,
1.2437-;; which all is necessary prior to any access the filesystem or environment.
1.2438-
1.2439-(uiop/package:define-package :uiop/pathname
1.2440- (:nicknames :asdf/pathname) ;; deprecated. Used by ceramic
1.2441- (:use :uiop/common-lisp :uiop/package :uiop/utility :uiop/os)
1.2442- (:export
1.2443- ;; Making and merging pathnames, portably
1.2444- #:normalize-pathname-directory-component #:denormalize-pathname-directory-component
1.2445- #:merge-pathname-directory-components #:*unspecific-pathname-type* #:make-pathname*
1.2446- #:make-pathname-component-logical #:make-pathname-logical
1.2447- #:merge-pathnames*
1.2448- #:nil-pathname #:*nil-pathname* #:with-pathname-defaults
1.2449- ;; Predicates
1.2450- #:pathname-equal #:logical-pathname-p #:physical-pathname-p #:physicalize-pathname
1.2451- #:absolute-pathname-p #:relative-pathname-p #:hidden-pathname-p #:file-pathname-p
1.2452- ;; Directories
1.2453- #:pathname-directory-pathname #:pathname-parent-directory-pathname
1.2454- #:directory-pathname-p #:ensure-directory-pathname
1.2455- ;; Parsing filenames
1.2456- #:split-name-type #:parse-unix-namestring #:unix-namestring
1.2457- #:split-unix-namestring-directory-components
1.2458- ;; Absolute and relative pathnames
1.2459- #:subpathname #:subpathname*
1.2460- #:ensure-absolute-pathname
1.2461- #:pathname-root #:pathname-host-pathname
1.2462- #:subpathp #:enough-pathname #:with-enough-pathname #:call-with-enough-pathname
1.2463- ;; Checking constraints
1.2464- #:ensure-pathname ;; implemented in filesystem.lisp to accommodate for existence constraints
1.2465- ;; Wildcard pathnames
1.2466- #:*wild* #:*wild-file* #:*wild-file-for-directory* #:*wild-directory*
1.2467- #:*wild-inferiors* #:*wild-path* #:wilden
1.2468- ;; Translate a pathname
1.2469- #:relativize-directory-component #:relativize-pathname-directory
1.2470- #:directory-separator-for-host #:directorize-pathname-host-device
1.2471- #:translate-pathname*
1.2472- #:*output-translation-function*))
1.2473-(in-package :uiop/pathname)
1.2474-
1.2475-;;; Normalizing pathnames across implementations
1.2476-
1.2477-(with-upgradability ()
1.2478- (defun normalize-pathname-directory-component (directory)
1.2479- "Convert the DIRECTORY component from a format usable by the underlying
1.2480-implementation's MAKE-PATHNAME and other primitives to a CLHS-standard format
1.2481-that is a list and not a string."
1.2482- (cond
1.2483- #-(or cmucl sbcl scl) ;; these implementations already normalize directory components.
1.2484- ((stringp directory) `(:absolute ,directory))
1.2485- ((or (null directory)
1.2486- (and (consp directory) (member (first directory) '(:absolute :relative))))
1.2487- directory)
1.2488- #+gcl
1.2489- ((consp directory)
1.2490- (cons :relative directory))
1.2491- (t
1.2492- (parameter-error (compatfmt "~@<~S: Unrecognized pathname directory component ~S~@:>")
1.2493- 'normalize-pathname-directory-component directory))))
1.2494-
1.2495- (defun denormalize-pathname-directory-component (directory-component)
1.2496- "Convert the DIRECTORY-COMPONENT from a CLHS-standard format to a format usable
1.2497-by the underlying implementation's MAKE-PATHNAME and other primitives"
1.2498- directory-component)
1.2499-
1.2500- (defun merge-pathname-directory-components (specified defaults)
1.2501- "Helper for MERGE-PATHNAMES* that handles directory components"
1.2502- (let ((directory (normalize-pathname-directory-component specified)))
1.2503- (ecase (first directory)
1.2504- ((nil) defaults)
1.2505- (:absolute specified)
1.2506- (:relative
1.2507- (let ((defdir (normalize-pathname-directory-component defaults))
1.2508- (reldir (cdr directory)))
1.2509- (cond
1.2510- ((null defdir)
1.2511- directory)
1.2512- ((not (eq :back (first reldir)))
1.2513- (append defdir reldir))
1.2514- (t
1.2515- (loop :with defabs = (first defdir)
1.2516- :with defrev = (reverse (rest defdir))
1.2517- :while (and (eq :back (car reldir))
1.2518- (or (and (eq :absolute defabs) (null defrev))
1.2519- (stringp (car defrev))))
1.2520- :do (pop reldir) (pop defrev)
1.2521- :finally (return (cons defabs (append (reverse defrev) reldir)))))))))))
1.2522-
1.2523- ;; Giving :unspecific as :type argument to make-pathname is not portable.
1.2524- ;; See CLHS make-pathname and 19.2.2.2.3.
1.2525- ;; This will be :unspecific if supported, or NIL if not.
1.2526- (defparameter *unspecific-pathname-type*
1.2527- #+(or abcl allegro clozure cmucl lispworks sbcl scl) :unspecific
1.2528- #+(or genera clasp clisp ecl mkcl gcl xcl #|These haven't been tested:|# cormanlisp mcl mezzano) nil
1.2529- "Unspecific type component to use with the underlying implementation's MAKE-PATHNAME")
1.2530-
1.2531- (defun make-pathname* (&rest keys &key directory host device name type version defaults
1.2532- #+scl &allow-other-keys)
1.2533- "Takes arguments like CL:MAKE-PATHNAME in the CLHS, and
1.2534- tries hard to make a pathname that will actually behave as documented,
1.2535- despite the peculiarities of each implementation. DEPRECATED: just use MAKE-PATHNAME."
1.2536- (declare (ignore host device directory name type version defaults))
1.2537- (apply 'make-pathname keys))
1.2538-
1.2539- (defun make-pathname-component-logical (x)
1.2540- "Make a pathname component suitable for use in a logical-pathname"
1.2541- (typecase x
1.2542- ((eql :unspecific) nil)
1.2543- #+clisp (string (string-upcase x))
1.2544- #+clisp (cons (mapcar 'make-pathname-component-logical x))
1.2545- (t x)))
1.2546-
1.2547- (defun make-pathname-logical (pathname host)
1.2548- "Take a PATHNAME's directory, name, type and version components,
1.2549-and make a new pathname with corresponding components and specified logical HOST"
1.2550- (make-pathname
1.2551- :host host
1.2552- :directory (make-pathname-component-logical (pathname-directory pathname))
1.2553- :name (make-pathname-component-logical (pathname-name pathname))
1.2554- :type (make-pathname-component-logical (pathname-type pathname))
1.2555- :version (make-pathname-component-logical (pathname-version pathname))))
1.2556-
1.2557- (defun merge-pathnames* (specified &optional (defaults *default-pathname-defaults*))
1.2558- "MERGE-PATHNAMES* is like MERGE-PATHNAMES except that
1.2559-if the SPECIFIED pathname does not have an absolute directory,
1.2560-then the HOST and DEVICE both come from the DEFAULTS, whereas
1.2561-if the SPECIFIED pathname does have an absolute directory,
1.2562-then the HOST and DEVICE both come from the SPECIFIED pathname.
1.2563-This is what users want on a modern Unix or Windows operating system,
1.2564-unlike the MERGE-PATHNAMES behavior.
1.2565-Also, if either argument is NIL, then the other argument is returned unmodified;
1.2566-this is unlike MERGE-PATHNAMES which always merges with a pathname,
1.2567-by default *DEFAULT-PATHNAME-DEFAULTS*, which cannot be NIL."
1.2568- (when (null specified) (return-from merge-pathnames* defaults))
1.2569- (when (null defaults) (return-from merge-pathnames* specified))
1.2570- #+scl
1.2571- (ext:resolve-pathname specified defaults)
1.2572- #-scl
1.2573- (let* ((specified (pathname specified))
1.2574- (defaults (pathname defaults))
1.2575- (directory (normalize-pathname-directory-component (pathname-directory specified)))
1.2576- (name (or (pathname-name specified) (pathname-name defaults)))
1.2577- (type (or (pathname-type specified) (pathname-type defaults)))
1.2578- (version (or (pathname-version specified) (pathname-version defaults))))
1.2579- (labels ((unspecific-handler (p)
1.2580- (if (typep p 'logical-pathname) #'make-pathname-component-logical #'identity)))
1.2581- (multiple-value-bind (host device directory unspecific-handler)
1.2582- (ecase (first directory)
1.2583- ((:absolute)
1.2584- (values (pathname-host specified)
1.2585- (pathname-device specified)
1.2586- directory
1.2587- (unspecific-handler specified)))
1.2588- ((nil :relative)
1.2589- (values (pathname-host defaults)
1.2590- (pathname-device defaults)
1.2591- (merge-pathname-directory-components directory (pathname-directory defaults))
1.2592- (unspecific-handler defaults))))
1.2593- (make-pathname :host host :device device :directory directory
1.2594- :name (funcall unspecific-handler name)
1.2595- :type (funcall unspecific-handler type)
1.2596- :version (funcall unspecific-handler version))))))
1.2597-
1.2598- (defun logical-pathname-p (x)
1.2599- "is X a logical-pathname?"
1.2600- (typep x 'logical-pathname))
1.2601-
1.2602- (defun physical-pathname-p (x)
1.2603- "is X a pathname that is not a logical-pathname?"
1.2604- (and (pathnamep x) (not (logical-pathname-p x))))
1.2605-
1.2606- (defun physicalize-pathname (x)
1.2607- "if X is a logical pathname, use translate-logical-pathname on it."
1.2608- ;; Ought to be the same as translate-logical-pathname, except the latter borks on CLISP
1.2609- (let ((p (when x (pathname x))))
1.2610- (if (logical-pathname-p p) (translate-logical-pathname p) p)))
1.2611-
1.2612- (defun nil-pathname (&optional (defaults *default-pathname-defaults*))
1.2613- "A pathname that is as neutral as possible for use as defaults
1.2614-when merging, making or parsing pathnames"
1.2615- ;; 19.2.2.2.1 says a NIL host can mean a default host;
1.2616- ;; see also "valid physical pathname host" in the CLHS glossary, that suggests
1.2617- ;; strings and lists of strings or :unspecific
1.2618- ;; But CMUCL decides to die on NIL.
1.2619- ;; MCL has issues with make-pathname, nil and defaulting
1.2620- (declare (ignorable defaults))
1.2621- #.`(make-pathname :directory nil :name nil :type nil :version nil
1.2622- :device (or #+(and mkcl os-unix) :unspecific)
1.2623- :host (or #+cmucl lisp::*unix-host* #+(and mkcl os-unix) "localhost")
1.2624- #+scl ,@'(:scheme nil :scheme-specific-part nil
1.2625- :username nil :password nil :parameters nil :query nil :fragment nil)
1.2626- ;; the default shouldn't matter, but we really want something physical
1.2627- #-mcl ,@'(:defaults defaults)))
1.2628-
1.2629- (defvar *nil-pathname* (nil-pathname (physicalize-pathname (user-homedir-pathname)))
1.2630- "A pathname that is as neutral as possible for use as defaults
1.2631-when merging, making or parsing pathnames")
1.2632-
1.2633- (defmacro with-pathname-defaults ((&optional defaults) &body body)
1.2634- "Execute BODY in a context where the *DEFAULT-PATHNAME-DEFAULTS* is as specified,
1.2635-where leaving the defaults NIL or unspecified means a (NIL-PATHNAME), except
1.2636-on ABCL, Genera and XCL, where it remains unchanged for it doubles as current-directory."
1.2637- `(let ((*default-pathname-defaults*
1.2638- ,(or defaults
1.2639- #-(or abcl genera xcl) '*nil-pathname*
1.2640- #+(or abcl genera xcl) '*default-pathname-defaults*)))
1.2641- ,@body)))
1.2642-
1.2643-
1.2644-;;; Some pathname predicates
1.2645-(with-upgradability ()
1.2646- (defun pathname-equal (p1 p2)
1.2647- "Are the two pathnames P1 and P2 reasonably equal in the paths they denote?"
1.2648- (when (stringp p1) (setf p1 (pathname p1)))
1.2649- (when (stringp p2) (setf p2 (pathname p2)))
1.2650- (flet ((normalize-component (x)
1.2651- (unless (member x '(nil :unspecific :newest (:relative)) :test 'equal)
1.2652- x)))
1.2653- (macrolet ((=? (&rest accessors)
1.2654- (flet ((frob (x)
1.2655- (reduce 'list (cons 'normalize-component accessors)
1.2656- :initial-value x :from-end t)))
1.2657- `(equal ,(frob 'p1) ,(frob 'p2)))))
1.2658- (or (and (null p1) (null p2))
1.2659- (and (pathnamep p1) (pathnamep p2)
1.2660- (and (=? pathname-host)
1.2661- #-(and mkcl os-unix) (=? pathname-device)
1.2662- (=? normalize-pathname-directory-component pathname-directory)
1.2663- (=? pathname-name)
1.2664- (=? pathname-type)
1.2665- #-mkcl (=? pathname-version)))))))
1.2666-
1.2667- (defun absolute-pathname-p (pathspec)
1.2668- "If PATHSPEC is a pathname or namestring object that parses as a pathname
1.2669-possessing an :ABSOLUTE directory component, return the (parsed) pathname.
1.2670-Otherwise return NIL"
1.2671- (and pathspec
1.2672- (typep pathspec '(or null pathname string))
1.2673- (let ((pathname (pathname pathspec)))
1.2674- (and (eq :absolute (car (normalize-pathname-directory-component
1.2675- (pathname-directory pathname))))
1.2676- pathname))))
1.2677-
1.2678- (defun relative-pathname-p (pathspec)
1.2679- "If PATHSPEC is a pathname or namestring object that parses as a pathname
1.2680-possessing a :RELATIVE or NIL directory component, return the (parsed) pathname.
1.2681-Otherwise return NIL"
1.2682- (and pathspec
1.2683- (typep pathspec '(or null pathname string))
1.2684- (let* ((pathname (pathname pathspec))
1.2685- (directory (normalize-pathname-directory-component
1.2686- (pathname-directory pathname))))
1.2687- (when (or (null directory) (eq :relative (car directory)))
1.2688- pathname))))
1.2689-
1.2690- (defun hidden-pathname-p (pathname)
1.2691- "Return a boolean that is true if the pathname is hidden as per Unix style,
1.2692-i.e. its name starts with a dot."
1.2693- (and pathname (equal (first-char (pathname-name pathname)) #\.)))
1.2694-
1.2695- (defun file-pathname-p (pathname)
1.2696- "Does PATHNAME represent a file, i.e. has a non-null NAME component?
1.2697-
1.2698-Accepts NIL, a string (converted through PARSE-NAMESTRING) or a PATHNAME.
1.2699-
1.2700-Note that this does _not_ check to see that PATHNAME points to an
1.2701-actually-existing file.
1.2702-
1.2703-Returns the (parsed) PATHNAME when true"
1.2704- (when pathname
1.2705- (let ((pathname (pathname pathname)))
1.2706- (unless (and (member (pathname-name pathname) '(nil :unspecific "") :test 'equal)
1.2707- (member (pathname-type pathname) '(nil :unspecific "") :test 'equal))
1.2708- pathname)))))
1.2709-
1.2710-
1.2711-;;; Directory pathnames
1.2712-(with-upgradability ()
1.2713- (defun pathname-directory-pathname (pathname)
1.2714- "Returns a new pathname with same HOST, DEVICE, DIRECTORY as PATHNAME,
1.2715-and NIL NAME, TYPE and VERSION components"
1.2716- (when pathname
1.2717- (make-pathname :name nil :type nil :version nil :defaults pathname)))
1.2718-
1.2719- (defun pathname-parent-directory-pathname (pathname)
1.2720- "Returns a new pathname that corresponds to the parent of the current pathname's directory,
1.2721-i.e. removing one level of depth in the DIRECTORY component. e.g. if pathname is
1.2722-Unix pathname /foo/bar/baz/file.type then return /foo/bar/"
1.2723- (when pathname
1.2724- (make-pathname :name nil :type nil :version nil
1.2725- :directory (merge-pathname-directory-components
1.2726- '(:relative :back) (pathname-directory pathname))
1.2727- :defaults pathname)))
1.2728-
1.2729- (defun directory-pathname-p (pathname)
1.2730- "Does PATHNAME represent a directory?
1.2731-
1.2732-A directory-pathname is a pathname _without_ a filename. The three
1.2733-ways that the filename components can be missing are for it to be NIL,
1.2734-:UNSPECIFIC or the empty string.
1.2735-
1.2736-Note that this does _not_ check to see that PATHNAME points to an
1.2737-actually-existing directory."
1.2738- (when pathname
1.2739- ;; I tried using Allegro's excl:file-directory-p, but this cannot be done,
1.2740- ;; because it rejects apparently legal pathnames as
1.2741- ;; ill-formed. [2014/02/10:rpg]
1.2742- (let ((pathname (pathname pathname)))
1.2743- (flet ((check-one (x)
1.2744- (member x '(nil :unspecific) :test 'equal)))
1.2745- (and (not (wild-pathname-p pathname))
1.2746- (check-one (pathname-name pathname))
1.2747- (check-one (pathname-type pathname))
1.2748- t)))))
1.2749-
1.2750- (defun ensure-directory-pathname (pathspec &optional (on-error 'error))
1.2751- "Converts the non-wild pathname designator PATHSPEC to directory form."
1.2752- (cond
1.2753- ((stringp pathspec)
1.2754- (ensure-directory-pathname (pathname pathspec)))
1.2755- ((not (pathnamep pathspec))
1.2756- (call-function on-error (compatfmt "~@<Invalid pathname designator ~S~@:>") pathspec))
1.2757- ((wild-pathname-p pathspec)
1.2758- (call-function on-error (compatfmt "~@<Can't reliably convert wild pathname ~3i~_~S~@:>") pathspec))
1.2759- ((directory-pathname-p pathspec)
1.2760- pathspec)
1.2761- (t
1.2762- (handler-case
1.2763- (make-pathname :directory (append (or (normalize-pathname-directory-component
1.2764- (pathname-directory pathspec))
1.2765- (list :relative))
1.2766- (list #-genera (file-namestring pathspec)
1.2767- ;; On Genera's native filesystem (LMFS),
1.2768- ;; directories have a type and version
1.2769- ;; which must be ignored when converting
1.2770- ;; to a directory pathname
1.2771- #+genera (if (typep pathspec 'fs:lmfs-pathname)
1.2772- (pathname-name pathspec)
1.2773- (file-namestring pathspec))))
1.2774- :name nil :type nil :version nil :defaults pathspec)
1.2775- (error (c) (call-function on-error (compatfmt "~@<error while trying to create a directory pathname for ~S: ~A~@:>") pathspec c)))))))
1.2776-
1.2777-
1.2778-;;; Parsing filenames
1.2779-(with-upgradability ()
1.2780- (declaim (ftype function ensure-pathname)) ; forward reference
1.2781-
1.2782- (defun split-unix-namestring-directory-components
1.2783- (unix-namestring &key ensure-directory dot-dot)
1.2784- "Splits the path string UNIX-NAMESTRING, returning four values:
1.2785-A flag that is either :absolute or :relative, indicating
1.2786- how the rest of the values are to be interpreted.
1.2787-A directory path --- a list of strings and keywords, suitable for
1.2788- use with MAKE-PATHNAME when prepended with the flag value.
1.2789- Directory components with an empty name or the name . are removed.
1.2790- Any directory named .. is read as DOT-DOT, or :BACK if it's NIL (not :UP).
1.2791-A last-component, either a file-namestring including type extension,
1.2792- or NIL in the case of a directory pathname.
1.2793-A flag that is true iff the unix-style-pathname was just
1.2794- a file-namestring without / path specification.
1.2795-ENSURE-DIRECTORY forces the namestring to be interpreted as a directory pathname:
1.2796-the third return value will be NIL, and final component of the namestring
1.2797-will be treated as part of the directory path.
1.2798-
1.2799-An empty string is thus read as meaning a pathname object with all fields nil.
1.2800-
1.2801-Note that colon characters #\: will NOT be interpreted as host specification.
1.2802-Absolute pathnames are only appropriate on Unix-style systems.
1.2803-
1.2804-The intention of this function is to support structured component names,
1.2805-e.g., \(:file \"foo/bar\"\), which will be unpacked to relative pathnames."
1.2806- (check-type unix-namestring string)
1.2807- (check-type dot-dot (member nil :back :up))
1.2808- (if (and (not (find #\/ unix-namestring)) (not ensure-directory)
1.2809- (plusp (length unix-namestring)))
1.2810- (values :relative () unix-namestring t)
1.2811- (let* ((components (split-string unix-namestring :separator "/"))
1.2812- (last-comp (car (last components))))
1.2813- (multiple-value-bind (relative components)
1.2814- (if (equal (first components) "")
1.2815- (if (equal (first-char unix-namestring) #\/)
1.2816- (values :absolute (cdr components))
1.2817- (values :relative nil))
1.2818- (values :relative components))
1.2819- (setf components (remove-if #'(lambda (x) (member x '("" ".") :test #'equal))
1.2820- components))
1.2821- (setf components (substitute (or dot-dot :back) ".." components :test #'equal))
1.2822- (cond
1.2823- ((equal last-comp "")
1.2824- (values relative components nil nil)) ; "" already removed from components
1.2825- (ensure-directory
1.2826- (values relative components nil nil))
1.2827- (t
1.2828- (values relative (butlast components) last-comp nil)))))))
1.2829-
1.2830- (defun split-name-type (filename)
1.2831- "Split a filename into two values NAME and TYPE that are returned.
1.2832-We assume filename has no directory component.
1.2833-The last . if any separates name and type from from type,
1.2834-except that if there is only one . and it is in first position,
1.2835-the whole filename is the NAME with an empty type.
1.2836-NAME is always a string.
1.2837-For an empty type, *UNSPECIFIC-PATHNAME-TYPE* is returned."
1.2838- (check-type filename string)
1.2839- (assert (plusp (length filename)))
1.2840- (destructuring-bind (name &optional (type *unspecific-pathname-type*))
1.2841- (split-string filename :max 2 :separator ".")
1.2842- (if (equal name "")
1.2843- (values filename *unspecific-pathname-type*)
1.2844- (values name type))))
1.2845-
1.2846- (defun parse-unix-namestring (name &rest keys &key type defaults dot-dot ensure-directory
1.2847- &allow-other-keys)
1.2848- "Coerce NAME into a PATHNAME using standard Unix syntax.
1.2849-
1.2850-Unix syntax is used whether or not the underlying system is Unix;
1.2851-on such non-Unix systems it is reliably usable only for relative pathnames.
1.2852-This function is especially useful to manipulate relative pathnames portably,
1.2853-where it is crucial to possess a portable pathname syntax independent of the underlying OS.
1.2854-This is what PARSE-UNIX-NAMESTRING provides, and why we use it in ASDF.
1.2855-
1.2856-When given a PATHNAME object, just return it untouched.
1.2857-When given NIL, just return NIL.
1.2858-When given a non-null SYMBOL, first downcase its name and treat it as a string.
1.2859-When given a STRING, portably decompose it into a pathname as below.
1.2860-
1.2861-#\\/ separates directory components.
1.2862-
1.2863-The last #\\/-separated substring is interpreted as follows:
1.2864-1- If TYPE is :DIRECTORY or ENSURE-DIRECTORY is true,
1.2865- the string is made the last directory component, and NAME and TYPE are NIL.
1.2866- if the string is empty, it's the empty pathname with all slots NIL.
1.2867-2- If TYPE is NIL, the substring is a file-namestring, and its NAME and TYPE
1.2868- are separated by SPLIT-NAME-TYPE.
1.2869-3- If TYPE is a string, it is the given TYPE, and the whole string is the NAME.
1.2870-
1.2871-Directory components with an empty name or the name \".\" are removed.
1.2872-Any directory named \"..\" is read as DOT-DOT,
1.2873-which must be one of :BACK or :UP and defaults to :BACK.
1.2874-
1.2875-HOST, DEVICE and VERSION components are taken from DEFAULTS,
1.2876-which itself defaults to *NIL-PATHNAME*, also used if DEFAULTS is NIL.
1.2877-No host or device can be specified in the string itself,
1.2878-which makes it unsuitable for absolute pathnames outside Unix.
1.2879-
1.2880-For relative pathnames, these components (and hence the defaults) won't matter
1.2881-if you use MERGE-PATHNAMES* but will matter if you use MERGE-PATHNAMES,
1.2882-which is an important reason to always use MERGE-PATHNAMES*.
1.2883-
1.2884-Arbitrary keys are accepted, and the parse result is passed to ENSURE-PATHNAME
1.2885-with those keys, removing TYPE DEFAULTS and DOT-DOT.
1.2886-When you're manipulating pathnames that are supposed to make sense portably
1.2887-even though the OS may not be Unixish, we recommend you use :WANT-RELATIVE T
1.2888-to throw an error if the pathname is absolute"
1.2889- (block nil
1.2890- (check-type type (or null string (eql :directory)))
1.2891- (when ensure-directory
1.2892- (setf type :directory))
1.2893- (etypecase name
1.2894- ((or null pathname) (return name))
1.2895- (symbol
1.2896- (setf name (string-downcase name)))
1.2897- (string))
1.2898- (multiple-value-bind (relative path filename file-only)
1.2899- (split-unix-namestring-directory-components
1.2900- name :dot-dot dot-dot :ensure-directory (eq type :directory))
1.2901- (multiple-value-bind (name type)
1.2902- (cond
1.2903- ((or (eq type :directory) (null filename))
1.2904- (values nil nil))
1.2905- (type
1.2906- (values filename type))
1.2907- (t
1.2908- (split-name-type filename)))
1.2909- (let* ((directory
1.2910- (unless file-only (cons relative path)))
1.2911- (pathname
1.2912- #-abcl
1.2913- (make-pathname
1.2914- :directory directory
1.2915- :name name :type type
1.2916- :defaults (or #-mcl defaults *nil-pathname*))
1.2917- #+abcl
1.2918- (if (and defaults
1.2919- (ext:pathname-jar-p defaults)
1.2920- (null directory))
1.2921- ;; When DEFAULTS is a jar, it will have the directory we want
1.2922- (make-pathname :name name :type type
1.2923- :defaults (or defaults *nil-pathname*))
1.2924- (make-pathname :name name :type type
1.2925- :defaults (or defaults *nil-pathname*)
1.2926- :directory directory))))
1.2927- (apply 'ensure-pathname
1.2928- pathname
1.2929- (remove-plist-keys '(:type :dot-dot :defaults) keys)))))))
1.2930-
1.2931- (defun unix-namestring (pathname)
1.2932- "Given a non-wild PATHNAME, return a Unix-style namestring for it.
1.2933-If the PATHNAME is NIL or a STRING, return it unchanged.
1.2934-
1.2935-This only considers the DIRECTORY, NAME and TYPE components of the pathname.
1.2936-This is a portable solution for representing relative pathnames,
1.2937-But unless you are running on a Unix system, it is not a general solution
1.2938-to representing native pathnames.
1.2939-
1.2940-An error is signaled if the argument is not NULL, a STRING or a PATHNAME,
1.2941-or if it is a PATHNAME but some of its components are not recognized."
1.2942- (etypecase pathname
1.2943- ((or null string) pathname)
1.2944- (pathname
1.2945- (with-output-to-string (s)
1.2946- (flet ((err () (parameter-error "~S: invalid unix-namestring ~S"
1.2947- 'unix-namestring pathname)))
1.2948- (let* ((dir (normalize-pathname-directory-component (pathname-directory pathname)))
1.2949- (name (pathname-name pathname))
1.2950- (name (and (not (eq name :unspecific)) name))
1.2951- (type (pathname-type pathname))
1.2952- (type (and (not (eq type :unspecific)) type)))
1.2953- (cond
1.2954- ((member dir '(nil :unspecific)))
1.2955- ((eq dir '(:relative)) (princ "./" s))
1.2956- ((consp dir)
1.2957- (destructuring-bind (relabs &rest dirs) dir
1.2958- (or (member relabs '(:relative :absolute)) (err))
1.2959- (when (eq relabs :absolute) (princ #\/ s))
1.2960- (loop :for x :in dirs :do
1.2961- (cond
1.2962- ((member x '(:back :up)) (princ "../" s))
1.2963- ((equal x "") (err))
1.2964- ;;((member x '("." "..") :test 'equal) (err))
1.2965- ((stringp x) (format s "~A/" x))
1.2966- (t (err))))))
1.2967- (t (err)))
1.2968- (cond
1.2969- (name
1.2970- (unless (and (stringp name) (or (null type) (stringp type))) (err))
1.2971- (format s "~A~@[.~A~]" name type))
1.2972- (t
1.2973- (or (null type) (err)))))))))))
1.2974-
1.2975-;;; Absolute and relative pathnames
1.2976-(with-upgradability ()
1.2977- (defun subpathname (pathname subpath &key type)
1.2978- "This function takes a PATHNAME and a SUBPATH and a TYPE.
1.2979-If SUBPATH is already a PATHNAME object (not namestring),
1.2980-and is an absolute pathname at that, it is returned unchanged;
1.2981-otherwise, SUBPATH is turned into a relative pathname with given TYPE
1.2982-as per PARSE-UNIX-NAMESTRING with :WANT-RELATIVE T :TYPE TYPE,
1.2983-then it is merged with the PATHNAME-DIRECTORY-PATHNAME of PATHNAME."
1.2984- (or (and (pathnamep subpath) (absolute-pathname-p subpath))
1.2985- (merge-pathnames* (parse-unix-namestring subpath :type type :want-relative t)
1.2986- (pathname-directory-pathname pathname))))
1.2987-
1.2988- (defun subpathname* (pathname subpath &key type)
1.2989- "returns NIL if the base pathname is NIL, otherwise like SUBPATHNAME."
1.2990- (and pathname
1.2991- (subpathname (ensure-directory-pathname pathname) subpath :type type)))
1.2992-
1.2993- (defun pathname-root (pathname)
1.2994- "return the root directory for the host and device of given PATHNAME"
1.2995- (make-pathname :directory '(:absolute)
1.2996- :name nil :type nil :version nil
1.2997- :defaults pathname ;; host device, and on scl, *some*
1.2998- ;; scheme-specific parts: port username password, not others:
1.2999- . #.(or #+scl '(:parameters nil :query nil :fragment nil))))
1.3000-
1.3001- (defun pathname-host-pathname (pathname)
1.3002- "return a pathname with the same host as given PATHNAME, and all other fields NIL"
1.3003- (make-pathname :directory nil
1.3004- :name nil :type nil :version nil :device nil
1.3005- :defaults pathname ;; host device, and on scl, *some*
1.3006- ;; scheme-specific parts: port username password, not others:
1.3007- . #.(or #+scl '(:parameters nil :query nil :fragment nil))))
1.3008-
1.3009- (defun ensure-absolute-pathname (path &optional defaults (on-error 'error))
1.3010- "Given a pathname designator PATH, return an absolute pathname as specified by PATH
1.3011-considering the DEFAULTS, or, if not possible, use CALL-FUNCTION on the specified ON-ERROR behavior,
1.3012-with a format control-string and other arguments as arguments"
1.3013- (cond
1.3014- ((absolute-pathname-p path))
1.3015- ((stringp path) (ensure-absolute-pathname (pathname path) defaults on-error))
1.3016- ((not (pathnamep path)) (call-function on-error "not a valid pathname designator ~S" path))
1.3017- ((let ((default-pathname (if (pathnamep defaults) defaults (call-function defaults))))
1.3018- (or (if (absolute-pathname-p default-pathname)
1.3019- (absolute-pathname-p (merge-pathnames* path default-pathname))
1.3020- (call-function on-error "Default pathname ~S is not an absolute pathname"
1.3021- default-pathname))
1.3022- (call-function on-error "Failed to merge ~S with ~S into an absolute pathname"
1.3023- path default-pathname))))
1.3024- (t (call-function on-error
1.3025- "Cannot ensure ~S is evaluated as an absolute pathname with defaults ~S"
1.3026- path defaults))))
1.3027-
1.3028- (defun subpathp (maybe-subpath base-pathname)
1.3029- "if MAYBE-SUBPATH is a pathname that is under BASE-PATHNAME, return a pathname object that
1.3030-when used with MERGE-PATHNAMES* with defaults BASE-PATHNAME, returns MAYBE-SUBPATH."
1.3031- (and (pathnamep maybe-subpath) (pathnamep base-pathname)
1.3032- (absolute-pathname-p maybe-subpath) (absolute-pathname-p base-pathname)
1.3033- (directory-pathname-p base-pathname) (not (wild-pathname-p base-pathname))
1.3034- (pathname-equal (pathname-root maybe-subpath) (pathname-root base-pathname))
1.3035- (with-pathname-defaults (*nil-pathname*)
1.3036- (let ((enough (enough-namestring maybe-subpath base-pathname)))
1.3037- (and (relative-pathname-p enough) (pathname enough))))))
1.3038-
1.3039- (defun enough-pathname (maybe-subpath base-pathname)
1.3040- "if MAYBE-SUBPATH is a pathname that is under BASE-PATHNAME, return a pathname object that
1.3041-when used with MERGE-PATHNAMES* with defaults BASE-PATHNAME, returns MAYBE-SUBPATH."
1.3042- (let ((sub (when maybe-subpath (pathname maybe-subpath)))
1.3043- (base (when base-pathname (ensure-absolute-pathname (pathname base-pathname)))))
1.3044- (or (and base (subpathp sub base)) sub)))
1.3045-
1.3046- (defun call-with-enough-pathname (maybe-subpath defaults-pathname thunk)
1.3047- "In a context where *DEFAULT-PATHNAME-DEFAULTS* is bound to DEFAULTS-PATHNAME (if not null,
1.3048-or else to its current value), call THUNK with ENOUGH-PATHNAME for MAYBE-SUBPATH
1.3049-given DEFAULTS-PATHNAME as a base pathname."
1.3050- (let ((enough (enough-pathname maybe-subpath defaults-pathname))
1.3051- (*default-pathname-defaults* (or defaults-pathname *default-pathname-defaults*)))
1.3052- (funcall thunk enough)))
1.3053-
1.3054- (defmacro with-enough-pathname ((pathname-var &key (pathname pathname-var)
1.3055- (defaults *default-pathname-defaults*))
1.3056- &body body)
1.3057- "Shorthand syntax for CALL-WITH-ENOUGH-PATHNAME"
1.3058- `(call-with-enough-pathname ,pathname ,defaults #'(lambda (,pathname-var) ,@body))))
1.3059-
1.3060-
1.3061-;;; Wildcard pathnames
1.3062-(with-upgradability ()
1.3063- (defparameter *wild* (or #+cormanlisp "*" :wild)
1.3064- "Wild component for use with MAKE-PATHNAME")
1.3065- (defparameter *wild-directory-component* (or :wild)
1.3066- "Wild directory component for use with MAKE-PATHNAME")
1.3067- (defparameter *wild-inferiors-component* (or :wild-inferiors)
1.3068- "Wild-inferiors directory component for use with MAKE-PATHNAME")
1.3069- (defparameter *wild-file*
1.3070- (make-pathname :directory nil :name *wild* :type *wild*
1.3071- :version (or #-(or allegro abcl xcl) *wild*))
1.3072- "A pathname object with wildcards for matching any file with TRANSLATE-PATHNAME")
1.3073- (defparameter *wild-file-for-directory*
1.3074- (make-pathname :directory nil :name *wild* :type (or #-(or clisp gcl) *wild*)
1.3075- :version (or #-(or allegro abcl clisp gcl xcl) *wild*))
1.3076- "A pathname object with wildcards for matching any file with DIRECTORY")
1.3077- (defparameter *wild-directory*
1.3078- (make-pathname :directory `(:relative ,*wild-directory-component*)
1.3079- :name nil :type nil :version nil)
1.3080- "A pathname object with wildcards for matching any subdirectory")
1.3081- (defparameter *wild-inferiors*
1.3082- (make-pathname :directory `(:relative ,*wild-inferiors-component*)
1.3083- :name nil :type nil :version nil)
1.3084- "A pathname object with wildcards for matching any recursive subdirectory")
1.3085- (defparameter *wild-path*
1.3086- (merge-pathnames* *wild-file* *wild-inferiors*)
1.3087- "A pathname object with wildcards for matching any file in any recursive subdirectory")
1.3088-
1.3089- (defun wilden (path)
1.3090- "From a pathname, return a wildcard pathname matching any file in any subdirectory of given pathname's directory"
1.3091- (merge-pathnames* *wild-path* path)))
1.3092-
1.3093-
1.3094-;;; Translate a pathname
1.3095-(with-upgradability ()
1.3096- (defun relativize-directory-component (directory-component)
1.3097- "Given the DIRECTORY-COMPONENT of a pathname, return an otherwise similar relative directory component"
1.3098- (let ((directory (normalize-pathname-directory-component directory-component)))
1.3099- (cond
1.3100- ((stringp directory)
1.3101- (list :relative directory))
1.3102- ((eq (car directory) :absolute)
1.3103- (cons :relative (cdr directory)))
1.3104- (t
1.3105- directory))))
1.3106-
1.3107- (defun relativize-pathname-directory (pathspec)
1.3108- "Given a PATHNAME, return a relative pathname with otherwise the same components"
1.3109- (let ((p (pathname pathspec)))
1.3110- (make-pathname
1.3111- :directory (relativize-directory-component (pathname-directory p))
1.3112- :defaults p)))
1.3113-
1.3114- (defun directory-separator-for-host (&optional (pathname *default-pathname-defaults*))
1.3115- "Given a PATHNAME, return the character used to delimit directory names on this host and device."
1.3116- (let ((foo (make-pathname :directory '(:absolute "FOO") :defaults pathname)))
1.3117- (last-char (namestring foo))))
1.3118-
1.3119- #-scl
1.3120- (defun directorize-pathname-host-device (pathname)
1.3121- "Given a PATHNAME, return a pathname that has representations of its HOST and DEVICE components
1.3122-added to its DIRECTORY component. This is useful for output translations."
1.3123- (os-cond
1.3124- ((os-unix-p)
1.3125- (when (physical-pathname-p pathname)
1.3126- (return-from directorize-pathname-host-device pathname))))
1.3127- (let* ((root (pathname-root pathname))
1.3128- (wild-root (wilden root))
1.3129- (absolute-pathname (merge-pathnames* pathname root))
1.3130- (separator (directory-separator-for-host root))
1.3131- (root-namestring (namestring root))
1.3132- (root-string
1.3133- (substitute-if #\/
1.3134- #'(lambda (x) (or (eql x #\:)
1.3135- (eql x separator)))
1.3136- root-namestring)))
1.3137- (multiple-value-bind (relative path filename)
1.3138- (split-unix-namestring-directory-components root-string :ensure-directory t)
1.3139- (declare (ignore relative filename))
1.3140- (let ((new-base (make-pathname :defaults root :directory `(:absolute ,@path))))
1.3141- (translate-pathname absolute-pathname wild-root (wilden new-base))))))
1.3142-
1.3143- #+scl
1.3144- (defun directorize-pathname-host-device (pathname)
1.3145- (let ((scheme (ext:pathname-scheme pathname))
1.3146- (host (pathname-host pathname))
1.3147- (port (ext:pathname-port pathname))
1.3148- (directory (pathname-directory pathname)))
1.3149- (flet ((specificp (x) (and x (not (eq x :unspecific)))))
1.3150- (if (or (specificp port)
1.3151- (and (specificp host) (plusp (length host)))
1.3152- (specificp scheme))
1.3153- (let ((prefix ""))
1.3154- (when (specificp port)
1.3155- (setf prefix (format nil ":~D" port)))
1.3156- (when (and (specificp host) (plusp (length host)))
1.3157- (setf prefix (strcat host prefix)))
1.3158- (setf prefix (strcat ":" prefix))
1.3159- (when (specificp scheme)
1.3160- (setf prefix (strcat scheme prefix)))
1.3161- (assert (and directory (eq (first directory) :absolute)))
1.3162- (make-pathname :directory `(:absolute ,prefix ,@(rest directory))
1.3163- :defaults pathname)))
1.3164- pathname)))
1.3165-
1.3166- (defun translate-pathname* (path absolute-source destination &optional root source)
1.3167- "A wrapper around TRANSLATE-PATHNAME to be used by the ASDF output-translations facility.
1.3168-PATH is the pathname to be translated.
1.3169-ABSOLUTE-SOURCE is an absolute pathname to use as source for translate-pathname,
1.3170-DESTINATION is either a function, to be called with PATH and ABSOLUTE-SOURCE,
1.3171-or a relative pathname, to be merged with ROOT and used as destination for translate-pathname
1.3172-or an absolute pathname, to be used as destination for translate-pathname.
1.3173-In that last case, if ROOT is non-NIL, PATH is first transformated by DIRECTORIZE-PATHNAME-HOST-DEVICE."
1.3174- (declare (ignore source))
1.3175- (cond
1.3176- ((functionp destination)
1.3177- (funcall destination path absolute-source))
1.3178- ((eq destination t)
1.3179- path)
1.3180- ((not (pathnamep destination))
1.3181- (parameter-error "~S: Invalid destination" 'translate-pathname*))
1.3182- ((not (absolute-pathname-p destination))
1.3183- (translate-pathname path absolute-source (merge-pathnames* destination root)))
1.3184- (root
1.3185- (translate-pathname (directorize-pathname-host-device path) absolute-source destination))
1.3186- (t
1.3187- (translate-pathname path absolute-source destination))))
1.3188-
1.3189- (defvar *output-translation-function* 'identity
1.3190- "Hook for output translations.
1.3191-
1.3192-This function needs to be idempotent, so that actions can work
1.3193-whether their inputs were translated or not,
1.3194-which they will be if we are composing operations. e.g. if some
1.3195-create-lisp-op creates a lisp file from some higher-level input,
1.3196-you need to still be able to use compile-op on that lisp file."))
1.3197-;;;; -------------------------------------------------------------------------
1.3198-;;;; Portability layer around Common Lisp filesystem access
1.3199-
1.3200-(uiop/package:define-package :uiop/filesystem
1.3201- (:use :uiop/common-lisp :uiop/package :uiop/utility :uiop/os :uiop/pathname)
1.3202- (:export
1.3203- ;; Native namestrings
1.3204- #:native-namestring #:parse-native-namestring
1.3205- ;; Probing the filesystem
1.3206- #:truename* #:safe-file-write-date #:probe-file* #:directory-exists-p #:file-exists-p
1.3207- #:directory* #:filter-logical-directory-results #:directory-files #:subdirectories
1.3208- #:collect-sub*directories
1.3209- ;; Resolving symlinks somewhat
1.3210- #:truenamize #:resolve-symlinks #:*resolve-symlinks* #:resolve-symlinks*
1.3211- ;; merging with cwd
1.3212- #:get-pathname-defaults #:call-with-current-directory #:with-current-directory
1.3213- ;; Environment pathnames
1.3214- #:inter-directory-separator #:split-native-pathnames-string
1.3215- #:getenv-pathname #:getenv-pathnames
1.3216- #:getenv-absolute-directory #:getenv-absolute-directories
1.3217- #:lisp-implementation-directory #:lisp-implementation-pathname-p
1.3218- ;; Simple filesystem operations
1.3219- #:ensure-all-directories-exist
1.3220- #:rename-file-overwriting-target
1.3221- #:delete-file-if-exists #:delete-empty-directory #:delete-directory-tree))
1.3222-(in-package :uiop/filesystem)
1.3223-
1.3224-;;; Native namestrings, as seen by the operating system calls rather than Lisp
1.3225-(with-upgradability ()
1.3226- (defun native-namestring (x)
1.3227- "From a non-wildcard CL pathname, a return namestring suitable for passing to the operating system"
1.3228- (when x
1.3229- (let ((p (pathname x)))
1.3230- #+clozure (with-pathname-defaults () (ccl:native-translated-namestring p)) ; see ccl bug 978
1.3231- #+(or cmucl scl) (ext:unix-namestring p nil)
1.3232- #+sbcl (sb-ext:native-namestring p)
1.3233- #-(or clozure cmucl sbcl scl)
1.3234- (os-cond
1.3235- ((os-unix-p) (unix-namestring p))
1.3236- (t (namestring p))))))
1.3237-
1.3238- (defun parse-native-namestring (string &rest constraints &key ensure-directory &allow-other-keys)
1.3239- "From a native namestring suitable for use by the operating system, return
1.3240-a CL pathname satisfying all the specified constraints as per ENSURE-PATHNAME"
1.3241- (check-type string (or string null))
1.3242- (let* ((pathname
1.3243- (when string
1.3244- (with-pathname-defaults ()
1.3245- #+clozure (ccl:native-to-pathname string)
1.3246- #+cmucl (uiop/os::parse-unix-namestring* string)
1.3247- #+sbcl (sb-ext:parse-native-namestring string)
1.3248- #+scl (lisp::parse-unix-namestring string)
1.3249- #-(or clozure cmucl sbcl scl)
1.3250- (os-cond
1.3251- ((os-unix-p) (parse-unix-namestring string :ensure-directory ensure-directory))
1.3252- (t (parse-namestring string))))))
1.3253- (pathname
1.3254- (if ensure-directory
1.3255- (and pathname (ensure-directory-pathname pathname))
1.3256- pathname)))
1.3257- (apply 'ensure-pathname pathname constraints))))
1.3258-
1.3259-
1.3260-;;; Probing the filesystem
1.3261-(with-upgradability ()
1.3262- (defun truename* (p)
1.3263- "Nicer variant of TRUENAME that plays well with NIL, avoids logical pathname contexts, and tries both files and directories"
1.3264- (when p
1.3265- (when (stringp p) (setf p (with-pathname-defaults () (parse-namestring p))))
1.3266- (values
1.3267- (or (ignore-errors (truename p))
1.3268- ;; this is here because trying to find the truename of a directory pathname WITHOUT supplying
1.3269- ;; a trailing directory separator, causes an error on some lisps.
1.3270- #+(or clisp gcl) (if-let (d (ensure-directory-pathname p nil)) (ignore-errors (truename d)))
1.3271- ;; On Genera, truename of a directory pathname will probably fail as Genera
1.3272- ;; will merge in a filename/type/version from *default-pathname-defaults* and
1.3273- ;; will try to get the truename of a file that probably doesn't exist.
1.3274- #+genera (when (directory-pathname-p p)
1.3275- (let ((d (scl:send p :directory-pathname-as-file)))
1.3276- (ensure-directory-pathname (ignore-errors (truename d)) nil)))))))
1.3277-
1.3278- (defun safe-file-write-date (pathname)
1.3279- "Safe variant of FILE-WRITE-DATE that may return NIL rather than raise an error."
1.3280- ;; If FILE-WRITE-DATE returns NIL, it's possible that
1.3281- ;; the user or some other agent has deleted an input file.
1.3282- ;; Also, generated files will not exist at the time planning is done
1.3283- ;; and calls compute-action-stamp which calls safe-file-write-date.
1.3284- ;; So it is very possible that we can't get a valid file-write-date,
1.3285- ;; and we can survive and we will continue the planning
1.3286- ;; as if the file were very old.
1.3287- ;; (or should we treat the case in a different, special way?)
1.3288- (and pathname
1.3289- (handler-case (file-write-date (physicalize-pathname pathname))
1.3290- (file-error () nil))))
1.3291-
1.3292- (defun probe-file* (p &key truename)
1.3293- "when given a pathname P (designated by a string as per PARSE-NAMESTRING),
1.3294-probes the filesystem for a file or directory with given pathname.
1.3295-If it exists, return its truename if TRUENAME is true,
1.3296-or the original (parsed) pathname if it is false (the default)."
1.3297- (values
1.3298- (ignore-errors
1.3299- (setf p (funcall 'ensure-pathname p
1.3300- :namestring :lisp
1.3301- :ensure-physical t
1.3302- :ensure-absolute t :defaults 'get-pathname-defaults
1.3303- :want-non-wild t
1.3304- :on-error nil))
1.3305- (when p
1.3306- #+allegro
1.3307- (probe-file p :follow-symlinks truename)
1.3308- #+gcl
1.3309- (if truename
1.3310- (truename* p)
1.3311- (let ((kind (car (si::stat p))))
1.3312- (when (eq kind :link)
1.3313- (setf kind (ignore-errors (car (si::stat (truename* p))))))
1.3314- (ecase kind
1.3315- ((nil) nil)
1.3316- ((:file :link)
1.3317- (cond
1.3318- ((file-pathname-p p) p)
1.3319- ((directory-pathname-p p)
1.3320- (subpathname p (car (last (pathname-directory p)))))))
1.3321- (:directory (ensure-directory-pathname p)))))
1.3322- #+clisp
1.3323- #.(let* ((fs (or #-os-windows (find-symbol* '#:file-stat :posix nil)))
1.3324- (pp (find-symbol* '#:probe-pathname :ext nil)))
1.3325- `(if truename
1.3326- ,(if pp
1.3327- `(values (,pp p))
1.3328- '(or (truename* p)
1.3329- (truename* (ignore-errors (ensure-directory-pathname p)))))
1.3330- ,(cond
1.3331- (fs `(and (,fs p) p))
1.3332- (pp `(nth-value 1 (,pp p)))
1.3333- (t '(or (and (truename* p) p)
1.3334- (if-let (d (ensure-directory-pathname p))
1.3335- (and (truename* d) d)))))))
1.3336- #-(or allegro clisp gcl)
1.3337- (if truename
1.3338- (probe-file p)
1.3339- (and
1.3340- #+(or cmucl scl) (unix:unix-stat (ext:unix-namestring p))
1.3341- #+(and lispworks os-unix) (system:get-file-stat p)
1.3342- #+sbcl (sb-unix:unix-stat (sb-ext:native-namestring p))
1.3343- #-(or cmucl (and lispworks os-unix) sbcl scl) (file-write-date p)
1.3344- p))))))
1.3345-
1.3346- (defun directory-exists-p (x)
1.3347- "Is X the name of a directory that exists on the filesystem?"
1.3348- #+allegro
1.3349- (excl:probe-directory x)
1.3350- #+clisp
1.3351- (handler-case (ext:probe-directory x)
1.3352- (sys::simple-file-error ()
1.3353- nil))
1.3354- #-(or allegro clisp)
1.3355- (let ((p (probe-file* x :truename t)))
1.3356- (and (directory-pathname-p p) p)))
1.3357-
1.3358- (defun file-exists-p (x)
1.3359- "Is X the name of a file that exists on the filesystem?"
1.3360- (let ((p (probe-file* x :truename t)))
1.3361- (and (file-pathname-p p) p)))
1.3362-
1.3363- (defun directory* (pathname-spec &rest keys &key &allow-other-keys)
1.3364- "Return a list of the entries in a directory by calling DIRECTORY.
1.3365-Try to override the defaults to not resolving symlinks, if implementation allows."
1.3366- (apply 'directory pathname-spec
1.3367- (append keys '#.(or #+allegro '(:directories-are-files nil :follow-symbolic-links nil)
1.3368- #+(or clozure digitool) '(:follow-links nil)
1.3369- #+clisp '(:circle t :if-does-not-exist :ignore)
1.3370- #+(or cmucl scl) '(:follow-links nil :truenamep nil)
1.3371- #+lispworks '(:link-transparency nil)
1.3372- #+sbcl (when (find-symbol* :resolve-symlinks '#:sb-impl nil)
1.3373- '(:resolve-symlinks nil))))))
1.3374-
1.3375- (defun filter-logical-directory-results (directory entries merger)
1.3376- "If DIRECTORY isn't a logical pathname, return ENTRIES. If it is,
1.3377-given ENTRIES in the DIRECTORY, remove the entries which are physical yet
1.3378-when transformed by MERGER have a different TRUENAME.
1.3379-Also remove duplicates as may appear with some translation rules.
1.3380-This function is used as a helper to DIRECTORY-FILES to avoid invalid entries
1.3381-when using logical-pathnames."
1.3382- (if (logical-pathname-p directory)
1.3383- (remove-duplicates ;; on CLISP, querying ~/ will return duplicates
1.3384- ;; Try hard to not resolve logical-pathname into physical pathnames;
1.3385- ;; otherwise logical-pathname users/lovers will be disappointed.
1.3386- ;; If directory* could use some implementation-dependent magic,
1.3387- ;; we will have logical pathnames already; otherwise,
1.3388- ;; we only keep pathnames for which specifying the name and
1.3389- ;; translating the LPN commute.
1.3390- (loop :for f :in entries
1.3391- :for p = (or (and (logical-pathname-p f) f)
1.3392- (let* ((u (ignore-errors (call-function merger f))))
1.3393- ;; The first u avoids a cumbersome (truename u) error.
1.3394- ;; At this point f should already be a truename,
1.3395- ;; but isn't quite in CLISP, for it doesn't have :version :newest
1.3396- (and u (equal (truename* u) (truename* f)) u)))
1.3397- :when p :collect p)
1.3398- :test 'pathname-equal)
1.3399- entries))
1.3400-
1.3401- (defun directory-files (directory &optional (pattern *wild-file-for-directory*))
1.3402- "Return a list of the files in a directory according to the PATTERN.
1.3403-Subdirectories should NOT be returned.
1.3404- PATTERN defaults to a pattern carefully chosen based on the implementation;
1.3405-override the default at your own risk.
1.3406- DIRECTORY-FILES tries NOT to resolve symlinks if the implementation permits this,
1.3407-but the behavior in presence of symlinks is not portable. Use IOlib to handle such situations."
1.3408- (let ((dir (ensure-directory-pathname directory)))
1.3409- (when (logical-pathname-p dir)
1.3410- ;; Because of the filtering we do below,
1.3411- ;; logical pathnames have restrictions on wild patterns.
1.3412- ;; Not that the results are very portable when you use these patterns on physical pathnames.
1.3413- (when (wild-pathname-p dir)
1.3414- (parameter-error "~S: Invalid wild pattern in logical directory ~S"
1.3415- 'directory-files directory))
1.3416- (unless (member (pathname-directory pattern) '(() (:relative)) :test 'equal)
1.3417- (parameter-error "~S: Invalid file pattern ~S for logical directory ~S" 'directory-files pattern directory))
1.3418- (setf pattern (make-pathname-logical pattern (pathname-host dir))))
1.3419- (let* ((pat (merge-pathnames* pattern dir))
1.3420- (entries (ignore-errors (directory* pat))))
1.3421- (remove-if 'directory-pathname-p
1.3422- (filter-logical-directory-results
1.3423- directory entries
1.3424- #'(lambda (f)
1.3425- (make-pathname :defaults dir
1.3426- :name (make-pathname-component-logical (pathname-name f))
1.3427- :type (make-pathname-component-logical (pathname-type f))
1.3428- :version (make-pathname-component-logical (pathname-version f)))))))))
1.3429-
1.3430- (defun subdirectories (directory)
1.3431- "Given a DIRECTORY pathname designator, return a list of the subdirectories under it.
1.3432-The behavior in presence of symlinks is not portable. Use IOlib to handle such situations."
1.3433- (let* ((directory (ensure-directory-pathname directory))
1.3434- #-(or abcl cormanlisp genera xcl)
1.3435- (wild (merge-pathnames*
1.3436- #-(or abcl allegro cmucl lispworks sbcl scl xcl)
1.3437- *wild-directory*
1.3438- #+(or abcl allegro cmucl lispworks sbcl scl xcl) "*.*"
1.3439- directory))
1.3440- (dirs
1.3441- #-(or abcl cormanlisp genera xcl)
1.3442- (ignore-errors
1.3443- (directory* wild . #.(or #+clozure '(:directories t :files nil)
1.3444- #+mcl '(:directories t))))
1.3445- #+(or abcl xcl) (system:list-directory directory)
1.3446- #+cormanlisp (cl::directory-subdirs directory)
1.3447- #+genera (handler-case (fs:directory-list directory) (fs:directory-not-found () nil)))
1.3448- #+(or abcl allegro cmucl genera lispworks sbcl scl xcl)
1.3449- (dirs (loop :for x :in dirs
1.3450- :for d = #+(or abcl xcl) (extensions:probe-directory x)
1.3451- #+allegro (excl:probe-directory x)
1.3452- #+(or cmucl sbcl scl) (directory-pathname-p x)
1.3453- #+genera (getf (cdr x) :directory)
1.3454- #+lispworks (lw:file-directory-p x)
1.3455- :when d :collect #+(or abcl allegro xcl) (ensure-directory-pathname d)
1.3456- #+genera (ensure-directory-pathname (first x))
1.3457- #+(or cmucl lispworks sbcl scl) x)))
1.3458- (filter-logical-directory-results
1.3459- directory dirs
1.3460- (let ((prefix (or (normalize-pathname-directory-component (pathname-directory directory))
1.3461- '(:absolute)))) ; because allegro returns NIL for #p"FOO:"
1.3462- #'(lambda (d)
1.3463- (let ((dir (normalize-pathname-directory-component (pathname-directory d))))
1.3464- (and (consp dir) (consp (cdr dir))
1.3465- (make-pathname
1.3466- :defaults directory :name nil :type nil :version nil
1.3467- :directory (append prefix (make-pathname-component-logical (last dir)))))))))))
1.3468-
1.3469- (defun collect-sub*directories (directory collectp recursep collector)
1.3470- "Given a DIRECTORY, when COLLECTP returns true when CALL-FUNCTION'ed with the directory,
1.3471-call-function the COLLECTOR function designator on the directory,
1.3472-and recurse each of its subdirectories on which the RECURSEP returns true when CALL-FUNCTION'ed with them.
1.3473-This function will thus let you traverse a filesystem hierarchy,
1.3474-superseding the functionality of CL-FAD:WALK-DIRECTORY.
1.3475-The behavior in presence of symlinks is not portable. Use IOlib to handle such situations."
1.3476- (when (call-function collectp directory)
1.3477- (call-function collector directory)
1.3478- (dolist (subdir (subdirectories directory))
1.3479- (when (call-function recursep subdir)
1.3480- (collect-sub*directories subdir collectp recursep collector))))))
1.3481-
1.3482-;;; Resolving symlinks somewhat
1.3483-(with-upgradability ()
1.3484- (defun truenamize (pathname)
1.3485- "Resolve as much of a pathname as possible"
1.3486- (block nil
1.3487- (when (typep pathname '(or null logical-pathname)) (return pathname))
1.3488- (let ((p pathname))
1.3489- (unless (absolute-pathname-p p)
1.3490- (setf p (or (absolute-pathname-p (ensure-absolute-pathname p 'get-pathname-defaults nil))
1.3491- (return p))))
1.3492- (when (logical-pathname-p p) (return p))
1.3493- (let ((found (probe-file* p :truename t)))
1.3494- (when found (return found)))
1.3495- (let* ((directory (normalize-pathname-directory-component (pathname-directory p)))
1.3496- (up-components (reverse (rest directory)))
1.3497- (down-components ()))
1.3498- (assert (eq :absolute (first directory)))
1.3499- (loop :while up-components :do
1.3500- (if-let (parent
1.3501- (ignore-errors
1.3502- (probe-file* (make-pathname :directory `(:absolute ,@(reverse up-components))
1.3503- :name nil :type nil :version nil :defaults p))))
1.3504- (if-let (simplified
1.3505- (ignore-errors
1.3506- (merge-pathnames*
1.3507- (make-pathname :directory `(:relative ,@down-components)
1.3508- :defaults p)
1.3509- (ensure-directory-pathname parent))))
1.3510- (return simplified)))
1.3511- (push (pop up-components) down-components)
1.3512- :finally (return p))))))
1.3513-
1.3514- (defun resolve-symlinks (path)
1.3515- "Do a best effort at resolving symlinks in PATH, returning a partially or totally resolved PATH."
1.3516- #-allegro (truenamize path)
1.3517- #+allegro
1.3518- (if (physical-pathname-p path)
1.3519- (or (ignore-errors (excl:pathname-resolve-symbolic-links path)) path)
1.3520- path))
1.3521-
1.3522- (defvar *resolve-symlinks* t
1.3523- "Determine whether or not ASDF resolves symlinks when defining systems.
1.3524-Defaults to T.")
1.3525-
1.3526- (defun resolve-symlinks* (path)
1.3527- "RESOLVE-SYMLINKS in PATH iff *RESOLVE-SYMLINKS* is T (the default)."
1.3528- (if *resolve-symlinks*
1.3529- (and path (resolve-symlinks path))
1.3530- path)))
1.3531-
1.3532-
1.3533-;;; Check pathname constraints
1.3534-(with-upgradability ()
1.3535- (defun ensure-pathname
1.3536- (pathname &key
1.3537- on-error
1.3538- defaults type dot-dot namestring
1.3539- empty-is-nil
1.3540- want-pathname
1.3541- want-logical want-physical ensure-physical
1.3542- want-relative want-absolute ensure-absolute ensure-subpath
1.3543- want-non-wild want-wild wilden
1.3544- want-file want-directory ensure-directory
1.3545- want-existing ensure-directories-exist
1.3546- truename resolve-symlinks truenamize
1.3547- &aux (p pathname)) ;; mutable working copy, preserve original
1.3548- "Coerces its argument into a PATHNAME,
1.3549-optionally doing some transformations and checking specified constraints.
1.3550-
1.3551-If the argument is NIL, then NIL is returned unless the WANT-PATHNAME constraint is specified.
1.3552-
1.3553-If the argument is a STRING, it is first converted to a pathname via
1.3554-PARSE-UNIX-NAMESTRING, PARSE-NAMESTRING or PARSE-NATIVE-NAMESTRING respectively
1.3555-depending on the NAMESTRING argument being :UNIX, :LISP or :NATIVE respectively,
1.3556-or else by using CALL-FUNCTION on the NAMESTRING argument;
1.3557-if :UNIX is specified (or NIL, the default, which specifies the same thing),
1.3558-then PARSE-UNIX-NAMESTRING it is called with the keywords
1.3559-DEFAULTS TYPE DOT-DOT ENSURE-DIRECTORY WANT-RELATIVE, and
1.3560-the result is optionally merged into the DEFAULTS if ENSURE-ABSOLUTE is true.
1.3561-
1.3562-The pathname passed or resulting from parsing the string
1.3563-is then subjected to all the checks and transformations below are run.
1.3564-
1.3565-Each non-nil constraint argument can be one of the symbols T, ERROR, CERROR or IGNORE.
1.3566-The boolean T is an alias for ERROR.
1.3567-ERROR means that an error will be raised if the constraint is not satisfied.
1.3568-CERROR means that an continuable error will be raised if the constraint is not satisfied.
1.3569-IGNORE means just return NIL instead of the pathname.
1.3570-
1.3571-The ON-ERROR argument, if not NIL, is a function designator (as per CALL-FUNCTION)
1.3572-that will be called with the the following arguments:
1.3573-a generic format string for ensure pathname, the pathname,
1.3574-the keyword argument corresponding to the failed check or transformation,
1.3575-a format string for the reason ENSURE-PATHNAME failed,
1.3576-and a list with arguments to that format string.
1.3577-If ON-ERROR is NIL, ERROR is used instead, which does the right thing.
1.3578-You could also pass (CERROR \"CONTINUE DESPITE FAILED CHECK\").
1.3579-
1.3580-The transformations and constraint checks are done in this order,
1.3581-which is also the order in the lambda-list:
1.3582-
1.3583-EMPTY-IS-NIL returns NIL if the argument is an empty string.
1.3584-WANT-PATHNAME checks that pathname (after parsing if needed) is not null.
1.3585-Otherwise, if the pathname is NIL, ensure-pathname returns NIL.
1.3586-WANT-LOGICAL checks that pathname is a LOGICAL-PATHNAME
1.3587-WANT-PHYSICAL checks that pathname is not a LOGICAL-PATHNAME
1.3588-ENSURE-PHYSICAL ensures that pathname is physical via TRANSLATE-LOGICAL-PATHNAME
1.3589-WANT-RELATIVE checks that pathname has a relative directory component
1.3590-WANT-ABSOLUTE checks that pathname does have an absolute directory component
1.3591-ENSURE-ABSOLUTE merges with the DEFAULTS, then checks again
1.3592-that the result absolute is an absolute pathname indeed.
1.3593-ENSURE-SUBPATH checks that the pathname is a subpath of the DEFAULTS.
1.3594-WANT-FILE checks that pathname has a non-nil FILE component
1.3595-WANT-DIRECTORY checks that pathname has nil FILE and TYPE components
1.3596-ENSURE-DIRECTORY uses ENSURE-DIRECTORY-PATHNAME to interpret
1.3597-any file and type components as being actually a last directory component.
1.3598-WANT-NON-WILD checks that pathname is not a wild pathname
1.3599-WANT-WILD checks that pathname is a wild pathname
1.3600-WILDEN merges the pathname with **/*.*.* if it is not wild
1.3601-WANT-EXISTING checks that a file (or directory) exists with that pathname.
1.3602-ENSURE-DIRECTORIES-EXIST creates any parent directory with ENSURE-DIRECTORIES-EXIST.
1.3603-TRUENAME replaces the pathname by its truename, or errors if not possible.
1.3604-RESOLVE-SYMLINKS replaces the pathname by a variant with symlinks resolved by RESOLVE-SYMLINKS.
1.3605-TRUENAMIZE uses TRUENAMIZE to resolve as many symlinks as possible."
1.3606- (block nil
1.3607- (flet ((report-error (keyword description &rest arguments)
1.3608- (call-function (or on-error 'error)
1.3609- "Invalid pathname ~S: ~*~?"
1.3610- pathname keyword description arguments)))
1.3611- (macrolet ((err (constraint &rest arguments)
1.3612- `(report-error ',(intern* constraint :keyword) ,@arguments))
1.3613- (check (constraint condition &rest arguments)
1.3614- `(when ,constraint
1.3615- (unless ,condition (err ,constraint ,@arguments))))
1.3616- (transform (transform condition expr)
1.3617- `(when ,transform
1.3618- (,@(if condition `(when ,condition) '(progn))
1.3619- (setf p ,expr)))))
1.3620- (etypecase p
1.3621- ((or null pathname))
1.3622- (string
1.3623- (when (and (emptyp p) empty-is-nil)
1.3624- (return-from ensure-pathname nil))
1.3625- (setf p (case namestring
1.3626- ((:unix nil)
1.3627- (parse-unix-namestring
1.3628- p :defaults defaults :type type :dot-dot dot-dot
1.3629- :ensure-directory ensure-directory :want-relative want-relative))
1.3630- ((:native)
1.3631- (parse-native-namestring p))
1.3632- ((:lisp)
1.3633- (parse-namestring p))
1.3634- (t
1.3635- (call-function namestring p))))))
1.3636- (etypecase p
1.3637- (pathname)
1.3638- (null
1.3639- (check want-pathname (pathnamep p) "Expected a pathname, not NIL")
1.3640- (return nil)))
1.3641- (check want-logical (logical-pathname-p p) "Expected a logical pathname")
1.3642- (check want-physical (physical-pathname-p p) "Expected a physical pathname")
1.3643- (transform ensure-physical () (physicalize-pathname p))
1.3644- (check ensure-physical (physical-pathname-p p) "Could not translate to a physical pathname")
1.3645- (check want-relative (relative-pathname-p p) "Expected a relative pathname")
1.3646- (check want-absolute (absolute-pathname-p p) "Expected an absolute pathname")
1.3647- (transform ensure-absolute (not (absolute-pathname-p p))
1.3648- (ensure-absolute-pathname p defaults (list #'report-error :ensure-absolute "~@?")))
1.3649- (check ensure-absolute (absolute-pathname-p p)
1.3650- "Could not make into an absolute pathname even after merging with ~S" defaults)
1.3651- (check ensure-subpath (absolute-pathname-p defaults)
1.3652- "cannot be checked to be a subpath of non-absolute pathname ~S" defaults)
1.3653- (check ensure-subpath (subpathp p defaults) "is not a sub pathname of ~S" defaults)
1.3654- (check want-file (file-pathname-p p) "Expected a file pathname")
1.3655- (check want-directory (directory-pathname-p p) "Expected a directory pathname")
1.3656- (transform ensure-directory (not (directory-pathname-p p)) (ensure-directory-pathname p))
1.3657- (check want-non-wild (not (wild-pathname-p p)) "Expected a non-wildcard pathname")
1.3658- (check want-wild (wild-pathname-p p) "Expected a wildcard pathname")
1.3659- (transform wilden (not (wild-pathname-p p)) (wilden p))
1.3660- (when want-existing
1.3661- (let ((existing (probe-file* p :truename truename)))
1.3662- (if existing
1.3663- (when truename
1.3664- (return existing))
1.3665- (err want-existing "Expected an existing pathname"))))
1.3666- (when ensure-directories-exist (ensure-directories-exist p))
1.3667- (when truename
1.3668- (let ((truename (truename* p)))
1.3669- (if truename
1.3670- (return truename)
1.3671- (err truename "Can't get a truename for pathname"))))
1.3672- (transform resolve-symlinks () (resolve-symlinks p))
1.3673- (transform truenamize () (truenamize p))
1.3674- p)))))
1.3675-
1.3676-
1.3677-;;; Pathname defaults
1.3678-(with-upgradability ()
1.3679- (defun get-pathname-defaults (&optional (defaults *default-pathname-defaults*))
1.3680- "Find the actual DEFAULTS to use for pathnames, including
1.3681-resolving them with respect to GETCWD if the DEFAULTS were relative"
1.3682- (or (absolute-pathname-p defaults)
1.3683- (merge-pathnames* defaults (getcwd))))
1.3684-
1.3685- (defun call-with-current-directory (dir thunk)
1.3686- "call the THUNK in a context where the current directory was changed to DIR, if not NIL.
1.3687-Note that this operation is usually NOT thread-safe."
1.3688- (if dir
1.3689- (let* ((dir (resolve-symlinks*
1.3690- (get-pathname-defaults
1.3691- (ensure-directory-pathname
1.3692- dir))))
1.3693- (cwd (getcwd))
1.3694- (*default-pathname-defaults* dir))
1.3695- (chdir dir)
1.3696- (unwind-protect
1.3697- (funcall thunk)
1.3698- (chdir cwd)))
1.3699- (funcall thunk)))
1.3700-
1.3701- (defmacro with-current-directory ((&optional dir) &body body)
1.3702- "Call BODY while the POSIX current working directory is set to DIR"
1.3703- `(call-with-current-directory ,dir #'(lambda () ,@body))))
1.3704-
1.3705-
1.3706-;;; Environment pathnames
1.3707-(with-upgradability ()
1.3708- (defun inter-directory-separator ()
1.3709- "What character does the current OS conventionally uses to separate directories?"
1.3710- (os-cond ((os-unix-p) #\:) (t #\;)))
1.3711-
1.3712- (defun split-native-pathnames-string (string &rest constraints &key &allow-other-keys)
1.3713- "Given a string of pathnames specified in native OS syntax, separate them in a list,
1.3714-check constraints and normalize each one as per ENSURE-PATHNAME,
1.3715-where an empty string denotes NIL."
1.3716- (loop :for namestring :in (split-string string :separator (string (inter-directory-separator)))
1.3717- :collect (unless (emptyp namestring) (apply 'parse-native-namestring namestring constraints))))
1.3718-
1.3719- (defun getenv-pathname (x &rest constraints &key ensure-directory want-directory on-error &allow-other-keys)
1.3720- "Extract a pathname from a user-configured environment variable, as per native OS,
1.3721-check constraints and normalize as per ENSURE-PATHNAME."
1.3722- ;; For backward compatibility with ASDF 2, want-directory implies ensure-directory
1.3723- (apply 'parse-native-namestring (getenvp x)
1.3724- :ensure-directory (or ensure-directory want-directory)
1.3725- :on-error (or on-error
1.3726- `(error "In (~S ~S), invalid pathname ~*~S: ~*~?" getenv-pathname ,x))
1.3727- constraints))
1.3728- (defun getenv-pathnames (x &rest constraints &key on-error &allow-other-keys)
1.3729- "Extract a list of pathname from a user-configured environment variable, as per native OS,
1.3730-check constraints and normalize each one as per ENSURE-PATHNAME.
1.3731- Any empty entries in the environment variable X will be returned as NILs."
1.3732- (unless (getf constraints :empty-is-nil t)
1.3733- (parameter-error "Cannot have EMPTY-IS-NIL false for ~S" 'getenv-pathnames))
1.3734- (apply 'split-native-pathnames-string (getenvp x)
1.3735- :on-error (or on-error
1.3736- `(error "In (~S ~S), invalid pathname ~*~S: ~*~?" getenv-pathnames ,x))
1.3737- :empty-is-nil t
1.3738- constraints))
1.3739- (defun getenv-absolute-directory (x)
1.3740- "Extract an absolute directory pathname from a user-configured environment variable,
1.3741-as per native OS"
1.3742- (getenv-pathname x :want-absolute t :ensure-directory t))
1.3743- (defun getenv-absolute-directories (x)
1.3744- "Extract a list of absolute directories from a user-configured environment variable,
1.3745-as per native OS. Any empty entries in the environment variable X will be returned as
1.3746-NILs."
1.3747- (getenv-pathnames x :want-absolute t :ensure-directory t))
1.3748-
1.3749- (defun lisp-implementation-directory (&key truename)
1.3750- "Where are the system files of the current installation of the CL implementation?"
1.3751- (declare (ignorable truename))
1.3752- (let ((dir
1.3753- #+abcl extensions:*lisp-home*
1.3754- #+(or allegro clasp ecl mkcl) #p"SYS:"
1.3755- #+clisp custom:*lib-directory*
1.3756- #+clozure #p"ccl:"
1.3757- #+cmucl (ignore-errors (pathname-parent-directory-pathname (truename #p"modules:")))
1.3758- #+gcl system::*system-directory*
1.3759- #+lispworks lispworks:*lispworks-directory*
1.3760- #+sbcl (if-let (it (find-symbol* :sbcl-homedir-pathname :sb-int nil))
1.3761- (funcall it)
1.3762- (getenv-pathname "SBCL_HOME" :ensure-directory t))
1.3763- #+scl (ignore-errors (pathname-parent-directory-pathname (truename #p"file://modules/")))
1.3764- #+xcl ext:*xcl-home*))
1.3765- (if (and dir truename)
1.3766- (truename* dir)
1.3767- dir)))
1.3768-
1.3769- (defun lisp-implementation-pathname-p (pathname)
1.3770- "Is the PATHNAME under the current installation of the CL implementation?"
1.3771- ;; Other builtin systems are those under the implementation directory
1.3772- (and (when pathname
1.3773- (if-let (impdir (lisp-implementation-directory))
1.3774- (or (subpathp pathname impdir)
1.3775- (when *resolve-symlinks*
1.3776- (if-let (truename (truename* pathname))
1.3777- (if-let (trueimpdir (truename* impdir))
1.3778- (subpathp truename trueimpdir)))))))
1.3779- t)))
1.3780-
1.3781-
1.3782-;;; Simple filesystem operations
1.3783-(with-upgradability ()
1.3784- (defun ensure-all-directories-exist (pathnames)
1.3785- "Ensure that for every pathname in PATHNAMES, we ensure its directories exist"
1.3786- (dolist (pathname pathnames)
1.3787- (when pathname
1.3788- (ensure-directories-exist (physicalize-pathname pathname)))))
1.3789-
1.3790- (defun delete-file-if-exists (x)
1.3791- "Delete a file X if it already exists"
1.3792- (when x (handler-case (delete-file x) (file-error () nil))))
1.3793-
1.3794- (defun rename-file-overwriting-target (source target)
1.3795- "Rename a file, overwriting any previous file with the TARGET name,
1.3796-in an atomic way if the implementation allows."
1.3797- (let ((source (ensure-pathname source :namestring :lisp :ensure-physical t :want-file t))
1.3798- (target (ensure-pathname target :namestring :lisp :ensure-physical t :want-file t)))
1.3799- #+clisp ;; in recent enough versions of CLISP, :if-exists :overwrite would make it atomic
1.3800- (progn (funcall 'require "syscalls")
1.3801- (symbol-call :posix :copy-file source target :method :rename))
1.3802- #+(and sbcl os-windows) (delete-file-if-exists target) ;; not atomic
1.3803- #-clisp
1.3804- (rename-file source target
1.3805- #+(or clasp clozure ecl) :if-exists
1.3806- #+clozure :rename-and-delete #+(or clasp ecl) t)))
1.3807-
1.3808- (defun delete-empty-directory (directory-pathname)
1.3809- "Delete an empty directory"
1.3810- #+(or abcl digitool gcl) (delete-file directory-pathname)
1.3811- #+allegro (excl:delete-directory directory-pathname)
1.3812- #+clisp (ext:delete-directory directory-pathname)
1.3813- #+clozure (ccl::delete-empty-directory directory-pathname)
1.3814- #+(or cmucl scl) (multiple-value-bind (ok errno)
1.3815- (unix:unix-rmdir (native-namestring directory-pathname))
1.3816- (unless ok
1.3817- #+cmucl (error "Error number ~A when trying to delete directory ~A"
1.3818- errno directory-pathname)
1.3819- #+scl (error "~@<Error deleting ~S: ~A~@:>"
1.3820- directory-pathname (unix:get-unix-error-msg errno))))
1.3821- #+cormanlisp (win32:delete-directory directory-pathname)
1.3822- #+(or clasp ecl) (si:rmdir directory-pathname)
1.3823- #+genera (fs:delete-directory directory-pathname)
1.3824- #+lispworks (lw:delete-directory directory-pathname)
1.3825- #+mkcl (mkcl:rmdir directory-pathname)
1.3826- #+sbcl #.(if-let (dd (find-symbol* :delete-directory :sb-ext nil))
1.3827- `(,dd directory-pathname) ;; requires SBCL 1.0.44 or later
1.3828- `(progn (require :sb-posix) (symbol-call :sb-posix :rmdir directory-pathname)))
1.3829- #+xcl (symbol-call :uiop :run-program `("rmdir" ,(native-namestring directory-pathname)))
1.3830- #-(or abcl allegro clasp clisp clozure cmucl cormanlisp digitool ecl gcl genera lispworks mkcl sbcl scl xcl)
1.3831- (not-implemented-error 'delete-empty-directory "(on your platform)")) ; genera
1.3832-
1.3833- (defun delete-directory-tree (directory-pathname &key (validate nil validatep) (if-does-not-exist :error))
1.3834- "Delete a directory including all its recursive contents, aka rm -rf.
1.3835-
1.3836-To reduce the risk of infortunate mistakes, DIRECTORY-PATHNAME must be
1.3837-a physical non-wildcard directory pathname (not namestring).
1.3838-
1.3839-If the directory does not exist, the IF-DOES-NOT-EXIST argument specifies what happens:
1.3840-if it is :ERROR (the default), an error is signaled, whereas if it is :IGNORE, nothing is done.
1.3841-
1.3842-Furthermore, before any deletion is attempted, the DIRECTORY-PATHNAME must pass
1.3843-the validation function designated (as per ENSURE-FUNCTION) by the VALIDATE keyword argument
1.3844-which in practice is thus compulsory, and validates by returning a non-NIL result.
1.3845-If you're suicidal or extremely confident, just use :VALIDATE T."
1.3846- (check-type if-does-not-exist (member :error :ignore))
1.3847- (setf directory-pathname (ensure-pathname directory-pathname
1.3848- :want-pathname t :want-non-wild t
1.3849- :want-physical t :want-directory t))
1.3850- (cond
1.3851- ((not validatep)
1.3852- (parameter-error "~S was asked to delete ~S but was not provided a validation predicate"
1.3853- 'delete-directory-tree directory-pathname))
1.3854- ((not (call-function validate directory-pathname))
1.3855- (parameter-error "~S was asked to delete ~S but it is not valid ~@[according to ~S~]"
1.3856- 'delete-directory-tree directory-pathname validate))
1.3857- ((not (directory-exists-p directory-pathname))
1.3858- (ecase if-does-not-exist
1.3859- (:error
1.3860- (error "~S was asked to delete ~S but the directory does not exist"
1.3861- 'delete-directory-tree directory-pathname))
1.3862- (:ignore nil)))
1.3863- #-(or allegro cmucl clozure genera sbcl scl)
1.3864- ((os-unix-p) ;; On Unix, don't recursively walk the directory and delete everything in Lisp,
1.3865- ;; except on implementations where we can prevent DIRECTORY from following symlinks;
1.3866- ;; instead spawn a standard external program to do the dirty work.
1.3867- (symbol-call :uiop :run-program `("rm" "-rf" ,(native-namestring directory-pathname))))
1.3868- (t
1.3869- ;; On supported implementation, call supported system functions
1.3870- #+allegro (symbol-call :excl.osi :delete-directory-and-files
1.3871- directory-pathname :if-does-not-exist if-does-not-exist)
1.3872- #+clozure (ccl:delete-directory directory-pathname)
1.3873- #+genera (fs:delete-directory directory-pathname :confirm nil)
1.3874- #+sbcl #.(if-let (dd (find-symbol* :delete-directory :sb-ext nil))
1.3875- `(,dd directory-pathname :recursive t) ;; requires SBCL 1.0.44 or later
1.3876- '(error "~S requires SBCL 1.0.44 or later" 'delete-directory-tree))
1.3877- ;; Outside Unix or on CMUCL and SCL that can avoid following symlinks,
1.3878- ;; do things the hard way.
1.3879- #-(or allegro clozure genera sbcl)
1.3880- (let ((sub*directories
1.3881- (while-collecting (c)
1.3882- (collect-sub*directories directory-pathname t t #'c))))
1.3883- (dolist (d (nreverse sub*directories))
1.3884- (map () 'delete-file (directory-files d))
1.3885- (delete-empty-directory d)))))))
1.3886-;;;; ---------------------------------------------------------------------------
1.3887-;;;; Utilities related to streams
1.3888-
1.3889-(uiop/package:define-package :uiop/stream
1.3890- (:use :uiop/common-lisp :uiop/package :uiop/utility :uiop/os :uiop/pathname :uiop/filesystem)
1.3891- (:export
1.3892- #:*default-stream-element-type*
1.3893- #:*stdin* #:setup-stdin #:*stdout* #:setup-stdout #:*stderr* #:setup-stderr
1.3894- #:detect-encoding #:*encoding-detection-hook* #:always-default-encoding
1.3895- #:encoding-external-format #:*encoding-external-format-hook* #:default-encoding-external-format
1.3896- #:*default-encoding* #:*utf-8-external-format*
1.3897- #:with-safe-io-syntax #:call-with-safe-io-syntax #:safe-read-from-string
1.3898- #:with-output #:output-string #:with-input #:input-string
1.3899- #:with-input-file #:call-with-input-file #:with-output-file #:call-with-output-file
1.3900- #:null-device-pathname #:call-with-null-input #:with-null-input
1.3901- #:call-with-null-output #:with-null-output
1.3902- #:finish-outputs #:format! #:safe-format!
1.3903- #:copy-stream-to-stream #:concatenate-files #:copy-file
1.3904- #:slurp-stream-string #:slurp-stream-lines #:slurp-stream-line
1.3905- #:slurp-stream-forms #:slurp-stream-form
1.3906- #:read-file-string #:read-file-line #:read-file-lines #:safe-read-file-line
1.3907- #:read-file-forms #:read-file-form #:safe-read-file-form
1.3908- #:eval-input #:eval-thunk #:standard-eval-thunk
1.3909- #:println #:writeln
1.3910- #:file-stream-p #:file-or-synonym-stream-p
1.3911- ;; Temporary files
1.3912- #:*temporary-directory* #:temporary-directory #:default-temporary-directory
1.3913- #:setup-temporary-directory
1.3914- #:call-with-temporary-file #:with-temporary-file
1.3915- #:add-pathname-suffix #:tmpize-pathname
1.3916- #:call-with-staging-pathname #:with-staging-pathname))
1.3917-(in-package :uiop/stream)
1.3918-
1.3919-(with-upgradability ()
1.3920- (defvar *default-stream-element-type*
1.3921- (or #+(or abcl cmucl cormanlisp scl xcl) 'character
1.3922- #+lispworks 'lw:simple-char
1.3923- :default)
1.3924- "default element-type for open (depends on the current CL implementation)")
1.3925-
1.3926- (defvar *stdin* *standard-input*
1.3927- "the original standard input stream at startup")
1.3928-
1.3929- (defun setup-stdin ()
1.3930- (setf *stdin*
1.3931- #.(or #+clozure 'ccl::*stdin*
1.3932- #+(or cmucl scl) 'system:*stdin*
1.3933- #+(or clasp ecl) 'ext::+process-standard-input+
1.3934- #+sbcl 'sb-sys:*stdin*
1.3935- '*standard-input*)))
1.3936-
1.3937- (defvar *stdout* *standard-output*
1.3938- "the original standard output stream at startup")
1.3939-
1.3940- (defun setup-stdout ()
1.3941- (setf *stdout*
1.3942- #.(or #+clozure 'ccl::*stdout*
1.3943- #+(or cmucl scl) 'system:*stdout*
1.3944- #+(or clasp ecl) 'ext::+process-standard-output+
1.3945- #+sbcl 'sb-sys:*stdout*
1.3946- '*standard-output*)))
1.3947-
1.3948- (defvar *stderr* *error-output*
1.3949- "the original error output stream at startup")
1.3950-
1.3951- (defun setup-stderr ()
1.3952- (setf *stderr*
1.3953- #.(or #+allegro 'excl::*stderr*
1.3954- #+clozure 'ccl::*stderr*
1.3955- #+(or cmucl scl) 'system:*stderr*
1.3956- #+(or clasp ecl) 'ext::+process-error-output+
1.3957- #+sbcl 'sb-sys:*stderr*
1.3958- '*error-output*)))
1.3959-
1.3960- ;; Run them now. In image.lisp, we'll register them to be run at image restart.
1.3961- (setup-stdin) (setup-stdout) (setup-stderr))
1.3962-
1.3963-
1.3964-;;; Encodings (mostly hooks only; full support requires asdf-encodings)
1.3965-(with-upgradability ()
1.3966- (defparameter *default-encoding*
1.3967- ;; preserve explicit user changes to something other than the legacy default :default
1.3968- (or (if-let (previous (and (boundp '*default-encoding*) (symbol-value '*default-encoding*)))
1.3969- (unless (eq previous :default) previous))
1.3970- :utf-8)
1.3971- "Default encoding for source files.
1.3972-The default value :utf-8 is the portable thing.
1.3973-The legacy behavior was :default.
1.3974-If you (asdf:load-system :asdf-encodings) then
1.3975-you will have autodetection via *encoding-detection-hook* below,
1.3976-reading emacs-style -*- coding: utf-8 -*- specifications,
1.3977-and falling back to utf-8 or latin1 if nothing is specified.")
1.3978-
1.3979- (defparameter *utf-8-external-format*
1.3980- (if (featurep :asdf-unicode)
1.3981- (or #+clisp charset:utf-8 :utf-8)
1.3982- :default)
1.3983- "Default :external-format argument to pass to CL:OPEN and also
1.3984-CL:LOAD or CL:COMPILE-FILE to best process a UTF-8 encoded file.
1.3985-On modern implementations, this will decode UTF-8 code points as CL characters.
1.3986-On legacy implementations, it may fall back on some 8-bit encoding,
1.3987-with non-ASCII code points being read as several CL characters;
1.3988-hopefully, if done consistently, that won't affect program behavior too much.")
1.3989-
1.3990- (defun always-default-encoding (pathname)
1.3991- "Trivial function to use as *encoding-detection-hook*,
1.3992-always 'detects' the *default-encoding*"
1.3993- (declare (ignore pathname))
1.3994- *default-encoding*)
1.3995-
1.3996- (defvar *encoding-detection-hook* #'always-default-encoding
1.3997- "Hook for an extension to define a function to automatically detect a file's encoding")
1.3998-
1.3999- (defun detect-encoding (pathname)
1.4000- "Detects the encoding of a specified file, going through user-configurable hooks"
1.4001- (if (and pathname (not (directory-pathname-p pathname)) (probe-file* pathname))
1.4002- (funcall *encoding-detection-hook* pathname)
1.4003- *default-encoding*))
1.4004-
1.4005- (defun default-encoding-external-format (encoding)
1.4006- "Default, ignorant, function to transform a character ENCODING as a
1.4007-portable keyword to an implementation-dependent EXTERNAL-FORMAT specification.
1.4008-Load system ASDF-ENCODINGS to hook in a better one."
1.4009- (case encoding
1.4010- (:default :default) ;; for backward-compatibility only. Explicit usage discouraged.
1.4011- (:utf-8 *utf-8-external-format*)
1.4012- (otherwise
1.4013- (cerror "Continue using :external-format :default" (compatfmt "~@<Your ASDF component is using encoding ~S but it isn't recognized. Your system should :defsystem-depends-on (:asdf-encodings).~:>") encoding)
1.4014- :default)))
1.4015-
1.4016- (defvar *encoding-external-format-hook*
1.4017- #'default-encoding-external-format
1.4018- "Hook for an extension (e.g. ASDF-ENCODINGS) to define a better mapping
1.4019-from non-default encodings to and implementation-defined external-format's")
1.4020-
1.4021- (defun encoding-external-format (encoding)
1.4022- "Transform a portable ENCODING keyword to an implementation-dependent EXTERNAL-FORMAT,
1.4023-going through all the proper hooks."
1.4024- (funcall *encoding-external-format-hook* (or encoding *default-encoding*))))
1.4025-
1.4026-
1.4027-;;; Safe syntax
1.4028-(with-upgradability ()
1.4029- (defvar *standard-readtable* (with-standard-io-syntax *readtable*)
1.4030- "The standard readtable, implementing the syntax specified by the CLHS.
1.4031-It must never be modified, though only good implementations will even enforce that.")
1.4032-
1.4033- (defmacro with-safe-io-syntax ((&key (package :cl)) &body body)
1.4034- "Establish safe CL reader options around the evaluation of BODY"
1.4035- `(call-with-safe-io-syntax #'(lambda () (let ((*package* (find-package ,package))) ,@body))))
1.4036-
1.4037- (defun call-with-safe-io-syntax (thunk &key (package :cl))
1.4038- (with-standard-io-syntax
1.4039- (let ((*package* (find-package package))
1.4040- (*read-default-float-format* 'double-float)
1.4041- (*print-readably* nil)
1.4042- (*read-eval* nil))
1.4043- (funcall thunk))))
1.4044-
1.4045- (defun safe-read-from-string (string &key (package :cl) (eof-error-p t) eof-value (start 0) end preserve-whitespace)
1.4046- "Read from STRING using a safe syntax, as per WITH-SAFE-IO-SYNTAX"
1.4047- (with-safe-io-syntax (:package package)
1.4048- (read-from-string string eof-error-p eof-value :start start :end end :preserve-whitespace preserve-whitespace))))
1.4049-
1.4050-;;; Output helpers
1.4051- (with-upgradability ()
1.4052- (defun call-with-output-file (pathname thunk
1.4053- &key
1.4054- (element-type *default-stream-element-type*)
1.4055- (external-format *utf-8-external-format*)
1.4056- (if-exists :error)
1.4057- (if-does-not-exist :create))
1.4058- "Open FILE for input with given recognizes options, call THUNK with the resulting stream.
1.4059-Other keys are accepted but discarded."
1.4060- (with-open-file (s pathname :direction :output
1.4061- :element-type element-type
1.4062- :external-format external-format
1.4063- :if-exists if-exists
1.4064- :if-does-not-exist if-does-not-exist)
1.4065- (funcall thunk s)))
1.4066-
1.4067- (defmacro with-output-file ((var pathname &rest keys
1.4068- &key element-type external-format if-exists if-does-not-exist)
1.4069- &body body)
1.4070- (declare (ignore element-type external-format if-exists if-does-not-exist))
1.4071- `(call-with-output-file ,pathname #'(lambda (,var) ,@body) ,@keys))
1.4072-
1.4073- (defun call-with-output (output function &key (element-type 'character))
1.4074- "Calls FUNCTION with an actual stream argument,
1.4075-behaving like FORMAT with respect to how stream designators are interpreted:
1.4076-If OUTPUT is a STREAM, use it as the stream.
1.4077-If OUTPUT is NIL, use a STRING-OUTPUT-STREAM of given ELEMENT-TYPE as the stream, and
1.4078-return the resulting string.
1.4079-If OUTPUT is T, use *STANDARD-OUTPUT* as the stream.
1.4080-If OUTPUT is a STRING with a fill-pointer, use it as a STRING-OUTPUT-STREAM of given ELEMENT-TYPE.
1.4081-If OUTPUT is a PATHNAME, open the file and write to it, passing ELEMENT-TYPE to WITH-OUTPUT-FILE
1.4082--- this latter as an extension since ASDF 3.1.
1.4083-\(Proper ELEMENT-TYPE treatment since ASDF 3.3.4 only.\)
1.4084-Otherwise, signal an error."
1.4085- (etypecase output
1.4086- (null
1.4087- (with-output-to-string (stream nil :element-type element-type) (funcall function stream)))
1.4088- ((eql t)
1.4089- (funcall function *standard-output*))
1.4090- (stream
1.4091- (funcall function output))
1.4092- (string
1.4093- (assert (fill-pointer output))
1.4094- (with-output-to-string (stream output :element-type element-type) (funcall function stream)))
1.4095- (pathname
1.4096- (call-with-output-file output function :element-type element-type)))))
1.4097-
1.4098-(with-upgradability ()
1.4099- (locally (declare #+sbcl (sb-ext:muffle-conditions style-warning))
1.4100- (handler-bind (#+sbcl (style-warning #'muffle-warning))
1.4101- (defmacro with-output ((output-var &optional (value output-var) &key element-type) &body body)
1.4102- "Bind OUTPUT-VAR to an output stream obtained from VALUE (default: previous binding
1.4103-of OUTPUT-VAR) treated as a stream designator per CALL-WITH-OUTPUT. Evaluate BODY in
1.4104-the scope of this binding."
1.4105- `(call-with-output ,value #'(lambda (,output-var) ,@body)
1.4106- ,@(when element-type `(:element-type ,element-type)))))))
1.4107-
1.4108-(defun output-string (string &optional output)
1.4109- "If the desired OUTPUT is not NIL, print the string to the output; otherwise return the string"
1.4110- (if output
1.4111- (with-output (output) (princ string output))
1.4112- string))
1.4113-
1.4114-
1.4115-;;; Input helpers
1.4116-(with-upgradability ()
1.4117- (defun call-with-input-file (pathname thunk
1.4118- &key
1.4119- (element-type *default-stream-element-type*)
1.4120- (external-format *utf-8-external-format*)
1.4121- (if-does-not-exist :error))
1.4122- "Open FILE for input with given recognizes options, call THUNK with the resulting stream.
1.4123-Other keys are accepted but discarded."
1.4124- (with-open-file (s pathname :direction :input
1.4125- :element-type element-type
1.4126- :external-format external-format
1.4127- :if-does-not-exist if-does-not-exist)
1.4128- (funcall thunk s)))
1.4129-
1.4130- (defmacro with-input-file ((var pathname &rest keys
1.4131- &key element-type external-format if-does-not-exist)
1.4132- &body body)
1.4133- (declare (ignore element-type external-format if-does-not-exist))
1.4134- `(call-with-input-file ,pathname #'(lambda (,var) ,@body) ,@keys))
1.4135-
1.4136- (defun call-with-input (input function &key keys)
1.4137- "Calls FUNCTION with an actual stream argument, interpreting
1.4138-stream designators like READ, but also coercing strings to STRING-INPUT-STREAM,
1.4139-and PATHNAME to FILE-STREAM.
1.4140-If INPUT is a STREAM, use it as the stream.
1.4141-If INPUT is NIL, use a *STANDARD-INPUT* as the stream.
1.4142-If INPUT is T, use *TERMINAL-IO* as the stream.
1.4143-If INPUT is a STRING, use it as a string-input-stream.
1.4144-If INPUT is a PATHNAME, open it, passing KEYS to WITH-INPUT-FILE
1.4145--- the latter is an extension since ASDF 3.1.
1.4146-Otherwise, signal an error."
1.4147- (etypecase input
1.4148- (null (funcall function *standard-input*))
1.4149- ((eql t) (funcall function *terminal-io*))
1.4150- (stream (funcall function input))
1.4151- (string (with-input-from-string (stream input) (funcall function stream)))
1.4152- (pathname (apply 'call-with-input-file input function keys))))
1.4153-
1.4154- (defmacro with-input ((input-var &optional (value input-var)) &body body)
1.4155- "Bind INPUT-VAR to an input stream, coercing VALUE (default: previous binding of INPUT-VAR)
1.4156-as per CALL-WITH-INPUT, and evaluate BODY within the scope of this binding."
1.4157- `(call-with-input ,value #'(lambda (,input-var) ,@body)))
1.4158-
1.4159- (defun input-string (&optional input)
1.4160- "If the desired INPUT is a string, return that string; otherwise slurp the INPUT into a string
1.4161-and return that"
1.4162- (if (stringp input)
1.4163- input
1.4164- (with-input (input) (funcall 'slurp-stream-string input)))))
1.4165-
1.4166-;;; Null device
1.4167-(with-upgradability ()
1.4168- (defun null-device-pathname ()
1.4169- "Pathname to a bit bucket device that discards any information written to it
1.4170-and always returns EOF when read from"
1.4171- (os-cond
1.4172- ((os-unix-p) #p"/dev/null")
1.4173- ((os-windows-p) #p"NUL") ;; Q: how many Lisps accept the #p"NUL:" syntax?
1.4174- (t (error "No /dev/null on your OS"))))
1.4175- (defun call-with-null-input (fun &key element-type external-format if-does-not-exist)
1.4176- "Call FUN with an input stream that always returns end of file.
1.4177-The keyword arguments are allowed for backward compatibility, but are ignored."
1.4178- (declare (ignore element-type external-format if-does-not-exist))
1.4179- (with-open-stream (input (make-concatenated-stream))
1.4180- (funcall fun input)))
1.4181- (defmacro with-null-input ((var &rest keys
1.4182- &key element-type external-format if-does-not-exist)
1.4183- &body body)
1.4184- (declare (ignore element-type external-format if-does-not-exist))
1.4185- "Evaluate BODY in a context when VAR is bound to an input stream that always returns end of file.
1.4186-The keyword arguments are allowed for backward compatibility, but are ignored."
1.4187- `(call-with-null-input #'(lambda (,var) ,@body) ,@keys))
1.4188- (defun call-with-null-output (fun
1.4189- &key (element-type *default-stream-element-type*)
1.4190- (external-format *utf-8-external-format*)
1.4191- (if-exists :overwrite)
1.4192- (if-does-not-exist :error))
1.4193- (declare (ignore element-type external-format if-exists if-does-not-exist))
1.4194- "Call FUN with an output stream that discards all output.
1.4195-The keyword arguments are allowed for backward compatibility, but are ignored."
1.4196- (with-open-stream (output (make-broadcast-stream))
1.4197- (funcall fun output)))
1.4198- (defmacro with-null-output ((var &rest keys
1.4199- &key element-type external-format if-does-not-exist if-exists)
1.4200- &body body)
1.4201- "Evaluate BODY in a context when VAR is bound to an output stream that discards all output.
1.4202-The keyword arguments are allowed for backward compatibility, but are ignored."
1.4203- (declare (ignore element-type external-format if-exists if-does-not-exist))
1.4204- `(call-with-null-output #'(lambda (,var) ,@body) ,@keys)))
1.4205-
1.4206-;;; Ensure output buffers are flushed
1.4207-(with-upgradability ()
1.4208- (defun finish-outputs (&rest streams)
1.4209- "Finish output on the main output streams as well as any specified one.
1.4210-Useful for portably flushing I/O before user input or program exit."
1.4211- ;; CCL notably buffers its stream output by default.
1.4212- (dolist (s (append streams
1.4213- (list *stdout* *stderr* *error-output* *standard-output* *trace-output*
1.4214- *debug-io* *terminal-io* *query-io*)))
1.4215- (ignore-errors (finish-output s)))
1.4216- (values))
1.4217-
1.4218- (defun format! (stream format &rest args)
1.4219- "Just like format, but call finish-outputs before and after the output."
1.4220- (finish-outputs stream)
1.4221- (apply 'format stream format args)
1.4222- (finish-outputs stream))
1.4223-
1.4224- (defun safe-format! (stream format &rest args)
1.4225- "Variant of FORMAT that is safe against both
1.4226-dangerous syntax configuration and errors while printing."
1.4227- (with-safe-io-syntax ()
1.4228- (ignore-errors (apply 'format! stream format args))
1.4229- (finish-outputs stream)))) ; just in case format failed
1.4230-
1.4231-
1.4232-;;; Simple Whole-Stream processing
1.4233-(with-upgradability ()
1.4234- (defun copy-stream-to-stream (input output &key element-type buffer-size linewise prefix)
1.4235- "Copy the contents of the INPUT stream into the OUTPUT stream.
1.4236-If LINEWISE is true, then read and copy the stream line by line, with an optional PREFIX.
1.4237-Otherwise, using WRITE-SEQUENCE using a buffer of size BUFFER-SIZE."
1.4238- (with-open-stream (input input)
1.4239- (if linewise
1.4240- (loop :for (line eof) = (multiple-value-list (read-line input nil nil))
1.4241- :while line :do
1.4242- (when prefix (princ prefix output))
1.4243- (princ line output)
1.4244- (unless eof (terpri output))
1.4245- (finish-output output)
1.4246- (when eof (return)))
1.4247- (loop
1.4248- :with buffer-size = (or buffer-size 8192)
1.4249- :with buffer = (make-array (list buffer-size) :element-type (or element-type 'character))
1.4250- :for end = (read-sequence buffer input)
1.4251- :until (zerop end)
1.4252- :do (write-sequence buffer output :end end)
1.4253- (when (< end buffer-size) (return))))))
1.4254-
1.4255- (defun concatenate-files (inputs output)
1.4256- "create a new OUTPUT file the contents of which a the concatenate of the INPUTS files."
1.4257- (with-open-file (o output :element-type '(unsigned-byte 8)
1.4258- :direction :output :if-exists :rename-and-delete)
1.4259- (dolist (input inputs)
1.4260- (with-open-file (i input :element-type '(unsigned-byte 8)
1.4261- :direction :input :if-does-not-exist :error)
1.4262- (copy-stream-to-stream i o :element-type '(unsigned-byte 8))))))
1.4263-
1.4264- (defun copy-file (input output)
1.4265- "Copy contents of the INPUT file to the OUTPUT file"
1.4266- ;; Not available on LW personal edition or LW 6.0 on Mac: (lispworks:copy-file i f)
1.4267- #+allegro
1.4268- (excl.osi:copy-file input output)
1.4269- #+ecl
1.4270- (ext:copy-file input output)
1.4271- #-(or allegro ecl)
1.4272- (concatenate-files (list input) output))
1.4273-
1.4274- (defun slurp-stream-string (input &key (element-type 'character) stripped)
1.4275- "Read the contents of the INPUT stream as a string"
1.4276- (let ((string
1.4277- (with-open-stream (input input)
1.4278- (with-output-to-string (output nil :element-type element-type)
1.4279- (copy-stream-to-stream input output :element-type element-type)))))
1.4280- (if stripped (stripln string) string)))
1.4281-
1.4282- (defun slurp-stream-lines (input &key count)
1.4283- "Read the contents of the INPUT stream as a list of lines, return those lines.
1.4284-
1.4285-Note: relies on the Lisp's READ-LINE, but additionally removes any remaining CR
1.4286-from the line-ending if the file or stream had CR+LF but Lisp only removed LF.
1.4287-
1.4288-Read no more than COUNT lines."
1.4289- (check-type count (or null integer))
1.4290- (with-open-stream (input input)
1.4291- (loop :for n :from 0
1.4292- :for l = (and (or (not count) (< n count))
1.4293- (read-line input nil nil))
1.4294- ;; stripln: to remove CR when the OS sends CRLF and Lisp only remove LF
1.4295- :while l :collect (stripln l))))
1.4296-
1.4297- (defun slurp-stream-line (input &key (at 0))
1.4298- "Read the contents of the INPUT stream as a list of lines,
1.4299-then return the ACCESS-AT of that list of lines using the AT specifier.
1.4300-PATH defaults to 0, i.e. return the first line.
1.4301-PATH is typically an integer, or a list of an integer and a function.
1.4302-If PATH is NIL, it will return all the lines in the file.
1.4303-
1.4304-The stream will not be read beyond the Nth lines,
1.4305-where N is the index specified by path
1.4306-if path is either an integer or a list that starts with an integer."
1.4307- (access-at (slurp-stream-lines input :count (access-at-count at)) at))
1.4308-
1.4309- (defun slurp-stream-forms (input &key count)
1.4310- "Read the contents of the INPUT stream as a list of forms,
1.4311-and return those forms.
1.4312-
1.4313-If COUNT is null, read to the end of the stream;
1.4314-if COUNT is an integer, stop after COUNT forms were read.
1.4315-
1.4316-BEWARE: be sure to use WITH-SAFE-IO-SYNTAX, or some variant thereof"
1.4317- (check-type count (or null integer))
1.4318- (loop :with eof = '#:eof
1.4319- :for n :from 0
1.4320- :for form = (if (and count (>= n count))
1.4321- eof
1.4322- (read-preserving-whitespace input nil eof))
1.4323- :until (eq form eof) :collect form))
1.4324-
1.4325- (defun slurp-stream-form (input &key (at 0))
1.4326- "Read the contents of the INPUT stream as a list of forms,
1.4327-then return the ACCESS-AT of these forms following the AT.
1.4328-AT defaults to 0, i.e. return the first form.
1.4329-AT is typically a list of integers.
1.4330-If AT is NIL, it will return all the forms in the file.
1.4331-
1.4332-The stream will not be read beyond the Nth form,
1.4333-where N is the index specified by path,
1.4334-if path is either an integer or a list that starts with an integer.
1.4335-
1.4336-BEWARE: be sure to use WITH-SAFE-IO-SYNTAX, or some variant thereof"
1.4337- (access-at (slurp-stream-forms input :count (access-at-count at)) at))
1.4338-
1.4339- (defun read-file-string (file &rest keys)
1.4340- "Open FILE with option KEYS, read its contents as a string"
1.4341- (apply 'call-with-input-file file 'slurp-stream-string keys))
1.4342-
1.4343- (defun read-file-lines (file &rest keys)
1.4344- "Open FILE with option KEYS, read its contents as a list of lines
1.4345-BEWARE: be sure to use WITH-SAFE-IO-SYNTAX, or some variant thereof"
1.4346- (apply 'call-with-input-file file 'slurp-stream-lines keys))
1.4347-
1.4348- (defun read-file-line (file &rest keys &key (at 0) &allow-other-keys)
1.4349- "Open input FILE with option KEYS (except AT),
1.4350-and read its contents as per SLURP-STREAM-LINE with given AT specifier.
1.4351-BEWARE: be sure to use WITH-SAFE-IO-SYNTAX, or some variant thereof"
1.4352- (apply 'call-with-input-file file
1.4353- #'(lambda (input) (slurp-stream-line input :at at))
1.4354- (remove-plist-key :at keys)))
1.4355-
1.4356- (defun read-file-forms (file &rest keys &key count &allow-other-keys)
1.4357- "Open input FILE with option KEYS (except COUNT),
1.4358-and read its contents as per SLURP-STREAM-FORMS with given COUNT.
1.4359-If COUNT is null, read to the end of the stream;
1.4360-if COUNT is an integer, stop after COUNT forms were read.
1.4361-BEWARE: be sure to use WITH-SAFE-IO-SYNTAX, or some variant thereof"
1.4362- (apply 'call-with-input-file file
1.4363- #'(lambda (input) (slurp-stream-forms input :count count))
1.4364- (remove-plist-key :count keys)))
1.4365-
1.4366- (defun read-file-form (file &rest keys &key (at 0) &allow-other-keys)
1.4367- "Open input FILE with option KEYS (except AT),
1.4368-and read its contents as per SLURP-STREAM-FORM with given AT specifier.
1.4369-BEWARE: be sure to use WITH-SAFE-IO-SYNTAX, or some variant thereof"
1.4370- (apply 'call-with-input-file file
1.4371- #'(lambda (input) (slurp-stream-form input :at at))
1.4372- (remove-plist-key :at keys)))
1.4373-
1.4374- (defun safe-read-file-line (pathname &rest keys &key (package :cl) &allow-other-keys)
1.4375- "Reads the specified line from the top of a file using a safe standardized syntax.
1.4376-Extracts the line using READ-FILE-LINE,
1.4377-within an WITH-SAFE-IO-SYNTAX using the specified PACKAGE."
1.4378- (with-safe-io-syntax (:package package)
1.4379- (apply 'read-file-line pathname (remove-plist-key :package keys))))
1.4380-
1.4381- (defun safe-read-file-form (pathname &rest keys &key (package :cl) &allow-other-keys)
1.4382- "Reads the specified form from the top of a file using a safe standardized syntax.
1.4383-Extracts the form using READ-FILE-FORM,
1.4384-within an WITH-SAFE-IO-SYNTAX using the specified PACKAGE."
1.4385- (with-safe-io-syntax (:package package)
1.4386- (apply 'read-file-form pathname (remove-plist-key :package keys))))
1.4387-
1.4388- (defun eval-input (input)
1.4389- "Portably read and evaluate forms from INPUT, return the last values."
1.4390- (with-input (input)
1.4391- (loop :with results :with eof ='#:eof
1.4392- :for form = (read input nil eof)
1.4393- :until (eq form eof)
1.4394- :do (setf results (multiple-value-list (eval form)))
1.4395- :finally (return (values-list results)))))
1.4396-
1.4397- (defun eval-thunk (thunk)
1.4398- "Evaluate a THUNK of code:
1.4399-If a function, FUNCALL it without arguments.
1.4400-If a constant literal and not a sequence, return it.
1.4401-If a cons or a symbol, EVAL it.
1.4402-If a string, repeatedly read and evaluate from it, returning the last values."
1.4403- (etypecase thunk
1.4404- ((or boolean keyword number character pathname) thunk)
1.4405- ((or cons symbol) (eval thunk))
1.4406- (function (funcall thunk))
1.4407- (string (eval-input thunk))))
1.4408-
1.4409- (defun standard-eval-thunk (thunk &key (package :cl))
1.4410- "Like EVAL-THUNK, but in a more standardized evaluation context."
1.4411- ;; Note: it's "standard-" not "safe-", because evaluation is never safe.
1.4412- (when thunk
1.4413- (with-safe-io-syntax (:package package)
1.4414- (let ((*read-eval* t))
1.4415- (eval-thunk thunk))))))
1.4416-
1.4417-(with-upgradability ()
1.4418- (defun println (x &optional (stream *standard-output*))
1.4419- "Variant of PRINC that also calls TERPRI afterwards"
1.4420- (princ x stream) (terpri stream) (finish-output stream) (values))
1.4421-
1.4422- (defun writeln (x &rest keys &key (stream *standard-output*) &allow-other-keys)
1.4423- "Variant of WRITE that also calls TERPRI afterwards"
1.4424- (apply 'write x keys) (terpri stream) (finish-output stream) (values)))
1.4425-
1.4426-
1.4427-;;; Using temporary files
1.4428-(with-upgradability ()
1.4429- (defun default-temporary-directory ()
1.4430- "Return a default directory to use for temporary files"
1.4431- (os-cond
1.4432- ((os-unix-p)
1.4433- (or (getenv-pathname "TMPDIR" :ensure-directory t)
1.4434- (parse-native-namestring "/tmp/")))
1.4435- ((os-windows-p)
1.4436- (getenv-pathname "TEMP" :ensure-directory t))
1.4437- (t (subpathname (user-homedir-pathname) "tmp/"))))
1.4438-
1.4439- (defvar *temporary-directory* nil "User-configurable location for temporary files")
1.4440-
1.4441- (defun temporary-directory ()
1.4442- "Return a directory to use for temporary files"
1.4443- (or *temporary-directory* (default-temporary-directory)))
1.4444-
1.4445- (defun setup-temporary-directory ()
1.4446- "Configure a default temporary directory to use."
1.4447- (setf *temporary-directory* (default-temporary-directory))
1.4448- #+gcl (setf system::*tmp-dir* *temporary-directory*))
1.4449-
1.4450- (defun call-with-temporary-file
1.4451- (thunk &key
1.4452- (want-stream-p t) (want-pathname-p t) (direction :io) keep after
1.4453- directory (type "tmp" typep) prefix (suffix (when typep "-tmp"))
1.4454- (element-type *default-stream-element-type*)
1.4455- (external-format *utf-8-external-format*))
1.4456- "Call a THUNK with stream and/or pathname arguments identifying a temporary file.
1.4457-
1.4458-The temporary file's pathname will be based on concatenating
1.4459-PREFIX (or \"tmp\" if it's NIL), a random alphanumeric string,
1.4460-and optional SUFFIX (defaults to \"-tmp\" if a type was provided)
1.4461-and TYPE (defaults to \"tmp\", using a dot as separator if not NIL),
1.4462-within DIRECTORY (defaulting to the TEMPORARY-DIRECTORY) if the PREFIX isn't absolute.
1.4463-
1.4464-The file will be open with specified DIRECTION (defaults to :IO),
1.4465-ELEMENT-TYPE (defaults to *DEFAULT-STREAM-ELEMENT-TYPE*) and
1.4466-EXTERNAL-FORMAT (defaults to *UTF-8-EXTERNAL-FORMAT*).
1.4467-If WANT-STREAM-P is true (the defaults to T), then THUNK will then be CALL-FUNCTION'ed
1.4468-with the stream and the pathname (if WANT-PATHNAME-P is true, defaults to T),
1.4469-and stream will be closed after the THUNK exits (either normally or abnormally).
1.4470-If WANT-STREAM-P is false, then WANT-PATHAME-P must be true, and then
1.4471-THUNK is only CALL-FUNCTION'ed after the stream is closed, with the pathname as argument.
1.4472-Upon exit of THUNK, the AFTER thunk if defined is CALL-FUNCTION'ed with the pathname as argument.
1.4473-If AFTER is defined, its results are returned, otherwise, the results of THUNK are returned.
1.4474-Finally, the file will be deleted, unless the KEEP argument when CALL-FUNCTION'ed returns true."
1.4475- #+xcl (declare (ignorable typep))
1.4476- (check-type direction (member :output :io))
1.4477- (assert (or want-stream-p want-pathname-p))
1.4478- (loop
1.4479- :with prefix-pn = (ensure-absolute-pathname
1.4480- (or prefix "tmp")
1.4481- (or (ensure-pathname
1.4482- directory
1.4483- :namestring :native
1.4484- :ensure-directory t
1.4485- :ensure-physical t)
1.4486- #'temporary-directory))
1.4487- :with prefix-nns = (native-namestring prefix-pn)
1.4488- :with results = (progn (ensure-directories-exist prefix-pn)
1.4489- ())
1.4490- :for counter :from (random (expt 36 #-gcl 8 #+gcl 5))
1.4491- :for pathname = (parse-native-namestring
1.4492- (format nil "~A~36R~@[~A~]~@[.~A~]"
1.4493- prefix-nns counter suffix (unless (eq type :unspecific) type)))
1.4494- :for okp = nil :do
1.4495- ;; TODO: on Unix, do something about umask
1.4496- ;; TODO: on Unix, audit the code so we make sure it uses O_CREAT|O_EXCL
1.4497- ;; TODO: on Unix, use CFFI and mkstemp --
1.4498- ;; except UIOP is precisely meant to not depend on CFFI or on anything! Grrrr.
1.4499- ;; Can we at least design some hook?
1.4500- (unwind-protect
1.4501- (progn
1.4502- (ensure-directories-exist pathname)
1.4503- (with-open-file (stream pathname
1.4504- :direction direction
1.4505- :element-type element-type
1.4506- :external-format external-format
1.4507- :if-exists nil :if-does-not-exist :create)
1.4508- (when stream
1.4509- (setf okp pathname)
1.4510- (when want-stream-p
1.4511- ;; Note: can't return directly from within with-open-file
1.4512- ;; or the non-local return causes the file creation to be undone.
1.4513- (setf results (multiple-value-list
1.4514- (if want-pathname-p
1.4515- (call-function thunk stream pathname)
1.4516- (call-function thunk stream)))))))
1.4517- ;; if we don't want a stream, then we must call the thunk *after*
1.4518- ;; the stream is closed, but only if it was successfully opened.
1.4519- (when okp
1.4520- (when (and want-pathname-p (not want-stream-p))
1.4521- (setf results (multiple-value-list (call-function thunk okp))))
1.4522- ;; if the stream was successfully opened, then return a value,
1.4523- ;; either one computed already, or one from AFTER, if that exists.
1.4524- (if after
1.4525- (return (call-function after pathname))
1.4526- (return (values-list results)))))
1.4527- (when (and okp (not (call-function keep)))
1.4528- (ignore-errors (delete-file-if-exists okp))))))
1.4529-
1.4530- (defmacro with-temporary-file ((&key (stream (gensym "STREAM") streamp)
1.4531- (pathname (gensym "PATHNAME") pathnamep)
1.4532- directory prefix suffix type
1.4533- keep direction element-type external-format)
1.4534- &body body)
1.4535- "Evaluate BODY where the symbols specified by keyword arguments
1.4536-STREAM and PATHNAME (if respectively specified) are bound corresponding
1.4537-to a newly created temporary file ready for I/O, as per CALL-WITH-TEMPORARY-FILE.
1.4538-At least one of STREAM or PATHNAME must be specified.
1.4539-If the STREAM is not specified, it will be closed before the BODY is evaluated.
1.4540-If STREAM is specified, then the :CLOSE-STREAM label if it appears in the BODY,
1.4541-separates forms run before and after the stream is closed.
1.4542-The values of the last form of the BODY (not counting the separating :CLOSE-STREAM) are returned.
1.4543-Upon success, the KEEP form is evaluated and the file is is deleted unless it evaluates to TRUE."
1.4544- (check-type stream symbol)
1.4545- (check-type pathname symbol)
1.4546- (assert (or streamp pathnamep))
1.4547- (let* ((afterp (position :close-stream body))
1.4548- (before (if afterp (subseq body 0 afterp) body))
1.4549- (after (when afterp (subseq body (1+ afterp))))
1.4550- (beforef (gensym "BEFORE"))
1.4551- (afterf (gensym "AFTER")))
1.4552- (when (eql afterp 0)
1.4553- (style-warn ":CLOSE-STREAM should not be the first form of BODY in WITH-TEMPORARY-FILE. Instead, do not provide a STREAM."))
1.4554- `(flet (,@(when before
1.4555- `((,beforef (,@(when streamp `(,stream)) ,@(when pathnamep `(,pathname)))
1.4556- ,@(when after `((declare (ignorable ,pathname))))
1.4557- ,@before)))
1.4558- ,@(when after
1.4559- (assert pathnamep)
1.4560- `((,afterf (,pathname) ,@after))))
1.4561- #-gcl (declare (dynamic-extent ,@(when before `(#',beforef)) ,@(when after `(#',afterf))))
1.4562- (call-with-temporary-file
1.4563- ,(when before `#',beforef)
1.4564- :want-stream-p ,streamp
1.4565- :want-pathname-p ,pathnamep
1.4566- ,@(when direction `(:direction ,direction))
1.4567- ,@(when directory `(:directory ,directory))
1.4568- ,@(when prefix `(:prefix ,prefix))
1.4569- ,@(when suffix `(:suffix ,suffix))
1.4570- ,@(when type `(:type ,type))
1.4571- ,@(when keep `(:keep ,keep))
1.4572- ,@(when after `(:after #',afterf))
1.4573- ,@(when element-type `(:element-type ,element-type))
1.4574- ,@(when external-format `(:external-format ,external-format))))))
1.4575-
1.4576- (defun get-temporary-file (&key directory prefix suffix type (keep t))
1.4577- (with-temporary-file (:pathname pn :keep keep
1.4578- :directory directory :prefix prefix :suffix suffix :type type)
1.4579- pn))
1.4580-
1.4581- ;; Temporary pathnames in simple cases where no contention is assumed
1.4582- (defun add-pathname-suffix (pathname suffix &rest keys)
1.4583- "Add a SUFFIX to the name of a PATHNAME, return a new pathname.
1.4584-Further KEYS can be passed to MAKE-PATHNAME."
1.4585- (apply 'make-pathname :name (strcat (pathname-name pathname) suffix)
1.4586- :defaults pathname keys))
1.4587-
1.4588- (defun tmpize-pathname (x)
1.4589- "Return a new pathname modified from X by adding a trivial random suffix.
1.4590-A new empty file with said temporary pathname is created, to ensure there is no
1.4591-clash with any concurrent process attempting the same thing."
1.4592- (let* ((px (ensure-pathname x :ensure-physical t))
1.4593- (prefix (if-let (n (pathname-name px)) (strcat n "-tmp") "tmp"))
1.4594- (directory (pathname-directory-pathname px)))
1.4595- ;; Genera uses versioned pathnames -- If we leave the empty file in place,
1.4596- ;; the system will create a new version of the file when the caller opens
1.4597- ;; it for output. That empty file will remain after the operation is completed.
1.4598- ;; As Genera is a single core processor, the possibility of a name conflict is
1.4599- ;; minimal if not nil. (And, in the event of a collision, the two processes
1.4600- ;; would be writing to different versions of the file.)
1.4601- (get-temporary-file :directory directory :prefix prefix :type (pathname-type px)
1.4602- #+genera :keep #+genera nil)))
1.4603-
1.4604- (defun call-with-staging-pathname (pathname fun)
1.4605- "Calls FUN with a staging pathname, and atomically
1.4606-renames the staging pathname to the PATHNAME in the end.
1.4607-NB: this protects only against failure of the program, not against concurrent attempts.
1.4608-For the latter case, we ought pick a random suffix and atomically open it."
1.4609- (let* ((pathname (pathname pathname))
1.4610- (staging (tmpize-pathname pathname)))
1.4611- (unwind-protect
1.4612- (multiple-value-prog1
1.4613- (funcall fun staging)
1.4614- (rename-file-overwriting-target staging pathname))
1.4615- (delete-file-if-exists staging))))
1.4616-
1.4617- (defmacro with-staging-pathname ((pathname-var &optional (pathname-value pathname-var)) &body body)
1.4618- "Trivial syntax wrapper for CALL-WITH-STAGING-PATHNAME"
1.4619- `(call-with-staging-pathname ,pathname-value #'(lambda (,pathname-var) ,@body))))
1.4620-
1.4621-(with-upgradability ()
1.4622- (defun file-stream-p (stream)
1.4623- (typep stream 'file-stream))
1.4624- (defun file-or-synonym-stream-p (stream)
1.4625- (or (file-stream-p stream)
1.4626- (and (typep stream 'synonym-stream)
1.4627- (file-or-synonym-stream-p
1.4628- (symbol-value (synonym-stream-symbol stream)))))))
1.4629-;;;; -------------------------------------------------------------------------
1.4630-;;;; Starting, Stopping, Dumping a Lisp image
1.4631-
1.4632-(uiop/package:define-package :uiop/image
1.4633- (:use :uiop/common-lisp :uiop/package :uiop/utility :uiop/pathname :uiop/stream :uiop/os)
1.4634- (:export
1.4635- #:*image-dumped-p* #:raw-command-line-arguments #:*command-line-arguments*
1.4636- #:command-line-arguments #:raw-command-line-arguments #:setup-command-line-arguments #:argv0
1.4637- #:*lisp-interaction*
1.4638- #:fatal-condition #:fatal-condition-p
1.4639- #:handle-fatal-condition
1.4640- #:call-with-fatal-condition-handler #:with-fatal-condition-handler
1.4641- #:*image-restore-hook* #:*image-prelude* #:*image-entry-point*
1.4642- #:*image-postlude* #:*image-dump-hook*
1.4643- #:quit #:die #:raw-print-backtrace #:print-backtrace #:print-condition-backtrace
1.4644- #:shell-boolean-exit
1.4645- #:register-image-restore-hook #:register-image-dump-hook
1.4646- #:call-image-restore-hook #:call-image-dump-hook
1.4647- #:restore-image #:dump-image #:create-image
1.4648-))
1.4649-(in-package :uiop/image)
1.4650-
1.4651-(with-upgradability ()
1.4652- (defvar *lisp-interaction* t
1.4653- "Is this an interactive Lisp environment, or is it batch processing?")
1.4654-
1.4655- (defvar *command-line-arguments* nil
1.4656- "Command-line arguments")
1.4657-
1.4658- (defvar *image-dumped-p* nil ; may matter as to how to get to command-line-arguments
1.4659- "Is this a dumped image? As a standalone executable?")
1.4660-
1.4661- (defvar *image-restore-hook* nil
1.4662- "Functions to call (in reverse order) when the image is restored")
1.4663-
1.4664- (defvar *image-restored-p* nil
1.4665- "Has the image been restored? A boolean, or :in-progress while restoring, :in-regress while dumping")
1.4666-
1.4667- (defvar *image-prelude* nil
1.4668- "a form to evaluate, or string containing forms to read and evaluate
1.4669-when the image is restarted, but before the entry point is called.")
1.4670-
1.4671- (defvar *image-entry-point* nil
1.4672- "a function with which to restart the dumped image when execution is restored from it.")
1.4673-
1.4674- (defvar *image-postlude* nil
1.4675- "a form to evaluate, or string containing forms to read and evaluate
1.4676-before the image dump hooks are called and before the image is dumped.")
1.4677-
1.4678- (defvar *image-dump-hook* nil
1.4679- "Functions to call (in order) when before an image is dumped"))
1.4680-
1.4681-(eval-when (#-lispworks :compile-toplevel :load-toplevel :execute)
1.4682- (deftype fatal-condition ()
1.4683- `(and serious-condition #+clozure (not ccl:process-reset))))
1.4684-
1.4685-;;; Exiting properly or im-
1.4686-(with-upgradability ()
1.4687- (defun quit (&optional (code 0) (finish-output t))
1.4688- "Quits from the Lisp world, with the given exit status if provided.
1.4689-This is designed to abstract away the implementation specific quit forms."
1.4690- (when finish-output ;; essential, for ClozureCL, and for standard compliance.
1.4691- (finish-outputs))
1.4692- #+(or abcl xcl) (ext:quit :status code)
1.4693- #+allegro (excl:exit code :quiet t)
1.4694- #+(or clasp ecl) (si:quit code)
1.4695- #+clisp (ext:quit code)
1.4696- #+clozure (ccl:quit code)
1.4697- #+cormanlisp (win32:exitprocess code)
1.4698- #+(or cmucl scl) (unix:unix-exit code)
1.4699- #+gcl (system:quit code)
1.4700- #+genera (error "~S: You probably don't want to Halt Genera. (code: ~S)" 'quit code)
1.4701- #+lispworks (lispworks:quit :status code :confirm nil :return nil :ignore-errors-p t)
1.4702- #+mcl (progn code (ccl:quit)) ;; or should we use FFI to call libc's exit(3) ?
1.4703- #+mkcl (mk-ext:quit :exit-code code)
1.4704- #+sbcl #.(let ((exit (find-symbol* :exit :sb-ext nil))
1.4705- (quit (find-symbol* :quit :sb-ext nil)))
1.4706- (cond
1.4707- (exit `(,exit :code code :abort (not finish-output)))
1.4708- (quit `(,quit :unix-status code :recklessly-p (not finish-output)))))
1.4709- #-(or abcl allegro clasp clisp clozure cmucl ecl gcl genera lispworks mcl mkcl sbcl scl xcl)
1.4710- (not-implemented-error 'quit "(called with exit code ~S)" code))
1.4711-
1.4712- (defun die (code format &rest arguments)
1.4713- "Die in error with some error message"
1.4714- (with-safe-io-syntax ()
1.4715- (ignore-errors
1.4716- (format! *stderr* "~&~?~&" format arguments)))
1.4717- (quit code))
1.4718-
1.4719- (defun raw-print-backtrace (&key (stream *debug-io*) count condition)
1.4720- "Print a backtrace, directly accessing the implementation"
1.4721- (declare (ignorable stream count condition))
1.4722- #+abcl
1.4723- (loop :for i :from 0
1.4724- :for frame :in (sys:backtrace (or count most-positive-fixnum)) :do
1.4725- (safe-format! stream "~&~D: ~A~%" i frame))
1.4726- #+allegro
1.4727- (let ((*terminal-io* stream)
1.4728- (*standard-output* stream)
1.4729- (tpl:*zoom-print-circle* *print-circle*)
1.4730- (tpl:*zoom-print-level* *print-level*)
1.4731- (tpl:*zoom-print-length* *print-length*))
1.4732- (tpl:do-command "zoom"
1.4733- :from-read-eval-print-loop nil
1.4734- :count (or count t)
1.4735- :all t))
1.4736- #+clasp
1.4737- (clasp-debug:print-backtrace :stream stream :count count)
1.4738- #+(or ecl mkcl)
1.4739- (let* ((top (si:ihs-top))
1.4740- (repeats (if count (min top count) top))
1.4741- (backtrace (loop :for ihs :from 0 :below top
1.4742- :collect (list (si::ihs-fun ihs)
1.4743- (si::ihs-env ihs)))))
1.4744- (loop :for i :from 0 :below repeats
1.4745- :for frame :in (nreverse backtrace) :do
1.4746- (safe-format! stream "~&~D: ~S~%" i frame)))
1.4747- #+clisp
1.4748- (system::print-backtrace :out stream :limit count)
1.4749- #+(or clozure mcl)
1.4750- (let ((*debug-io* stream))
1.4751- #+clozure (ccl:print-call-history :count count :start-frame-number 1)
1.4752- #+mcl (ccl:print-call-history :detailed-p nil)
1.4753- (finish-output stream))
1.4754- #+(or cmucl scl)
1.4755- (let ((debug:*debug-print-level* *print-level*)
1.4756- (debug:*debug-print-length* *print-length*))
1.4757- (debug:backtrace (or count most-positive-fixnum) stream))
1.4758- #+gcl
1.4759- (let ((*debug-io* stream))
1.4760- (ignore-errors
1.4761- (with-safe-io-syntax ()
1.4762- (if condition
1.4763- (conditions::condition-backtrace condition)
1.4764- (system::simple-backtrace)))))
1.4765- #+lispworks
1.4766- (let ((dbg::*debugger-stack*
1.4767- (dbg::grab-stack nil :how-many (or count most-positive-fixnum)))
1.4768- (*debug-io* stream)
1.4769- (dbg:*debug-print-level* *print-level*)
1.4770- (dbg:*debug-print-length* *print-length*))
1.4771- (dbg:bug-backtrace nil))
1.4772- #+mezzano
1.4773- (let ((*standard-output* stream))
1.4774- (sys.int::backtrace count))
1.4775- #+sbcl
1.4776- (sb-debug:print-backtrace :stream stream :count (or count most-positive-fixnum))
1.4777- #+xcl
1.4778- (loop :for i :from 0 :below (or count most-positive-fixnum)
1.4779- :for frame :in (extensions:backtrace-as-list) :do
1.4780- (safe-format! stream "~&~D: ~S~%" i frame)))
1.4781-
1.4782- (defun print-backtrace (&rest keys &key stream count condition)
1.4783- "Print a backtrace"
1.4784- (declare (ignore stream count condition))
1.4785- (with-safe-io-syntax (:package :cl)
1.4786- (let ((*print-readably* nil)
1.4787- (*print-circle* t)
1.4788- (*print-miser-width* 75)
1.4789- (*print-length* nil)
1.4790- (*print-level* nil)
1.4791- (*print-pretty* t))
1.4792- (ignore-errors (apply 'raw-print-backtrace keys)))))
1.4793-
1.4794- (defun print-condition-backtrace (condition &key (stream *stderr*) count)
1.4795- "Print a condition after a backtrace triggered by that condition"
1.4796- ;; We print the condition *after* the backtrace,
1.4797- ;; for the sake of who sees the backtrace at a terminal.
1.4798- ;; It is up to the caller to print the condition *before*, with some context.
1.4799- (print-backtrace :stream stream :count count :condition condition)
1.4800- (when condition
1.4801- (safe-format! stream "~&Above backtrace due to this condition:~%~A~&"
1.4802- condition)))
1.4803-
1.4804- (defun fatal-condition-p (condition)
1.4805- "Is the CONDITION fatal?"
1.4806- (typep condition 'fatal-condition))
1.4807-
1.4808- (defun handle-fatal-condition (condition)
1.4809- "Handle a fatal CONDITION:
1.4810-depending on whether *LISP-INTERACTION* is set, enter debugger or die"
1.4811- (cond
1.4812- (*lisp-interaction*
1.4813- (invoke-debugger condition))
1.4814- (t
1.4815- (safe-format! *stderr* "~&Fatal condition:~%~A~%" condition)
1.4816- (print-condition-backtrace condition :stream *stderr*)
1.4817- (die 99 "~A" condition))))
1.4818-
1.4819- (defun call-with-fatal-condition-handler (thunk)
1.4820- "Call THUNK in a context where fatal conditions are appropriately handled"
1.4821- (handler-bind ((fatal-condition #'handle-fatal-condition))
1.4822- (funcall thunk)))
1.4823-
1.4824- (defmacro with-fatal-condition-handler ((&optional) &body body)
1.4825- "Execute BODY in a context where fatal conditions are appropriately handled"
1.4826- `(call-with-fatal-condition-handler #'(lambda () ,@body)))
1.4827-
1.4828- (defun shell-boolean-exit (x)
1.4829- "Quit with a return code that is 0 iff argument X is true"
1.4830- (quit (if x 0 1))))
1.4831-
1.4832-
1.4833-;;; Using image hooks
1.4834-(with-upgradability ()
1.4835- (defun register-image-restore-hook (hook &optional (call-now-p t))
1.4836- "Regiter a hook function to be run when restoring a dumped image"
1.4837- (register-hook-function '*image-restore-hook* hook call-now-p))
1.4838-
1.4839- (defun register-image-dump-hook (hook &optional (call-now-p nil))
1.4840- "Register a the hook function to be run before to dump an image"
1.4841- (register-hook-function '*image-dump-hook* hook call-now-p))
1.4842-
1.4843- (defun call-image-restore-hook ()
1.4844- "Call the hook functions registered to be run when restoring a dumped image"
1.4845- (call-functions (reverse *image-restore-hook*)))
1.4846-
1.4847- (defun call-image-dump-hook ()
1.4848- "Call the hook functions registered to be run before to dump an image"
1.4849- (call-functions *image-dump-hook*)))
1.4850-
1.4851-
1.4852-;;; Proper command-line arguments
1.4853-(with-upgradability ()
1.4854- (defun raw-command-line-arguments ()
1.4855- "Find what the actual command line for this process was."
1.4856- #+abcl ext:*command-line-argument-list* ; Use 1.0.0 or later!
1.4857- #+allegro (sys:command-line-arguments) ; default: :application t
1.4858- #+(or clasp ecl) (loop :for i :from 0 :below (si:argc) :collect (si:argv i))
1.4859- #+clisp (coerce (ext:argv) 'list)
1.4860- #+clozure ccl:*command-line-argument-list*
1.4861- #+(or cmucl scl) extensions:*command-line-strings*
1.4862- #+gcl si:*command-args*
1.4863- #+(or genera mcl mezzano) nil
1.4864- #+lispworks sys:*line-arguments-list*
1.4865- #+mkcl (loop :for i :from 0 :below (mkcl:argc) :collect (mkcl:argv i))
1.4866- #+sbcl sb-ext:*posix-argv*
1.4867- #+xcl system:*argv*
1.4868- #-(or abcl allegro clasp clisp clozure cmucl ecl gcl genera lispworks mcl mezzano mkcl sbcl scl xcl)
1.4869- (not-implemented-error 'raw-command-line-arguments))
1.4870-
1.4871- (defun command-line-arguments (&optional (arguments (raw-command-line-arguments)))
1.4872- "Extract user arguments from command-line invocation of current process.
1.4873-Assume the calling conventions of a generated script that uses --
1.4874-if we are not called from a directly executable image."
1.4875- (block nil
1.4876- #+abcl (return arguments)
1.4877- ;; SBCL and Allegro already separate user arguments from implementation arguments.
1.4878- #-(or sbcl allegro)
1.4879- (unless (eq *image-dumped-p* :executable)
1.4880- ;; LispWorks command-line processing isn't transparent to the user
1.4881- ;; unless you create a standalone executable; in that case,
1.4882- ;; we rely on cl-launch or some other script to set the arguments for us.
1.4883- #+lispworks (return *command-line-arguments*)
1.4884- ;; On other implementations, on non-standalone executables,
1.4885- ;; we trust cl-launch or whichever script starts the program
1.4886- ;; to use -- as a delimiter between implementation arguments and user arguments.
1.4887- #-lispworks (setf arguments (member "--" arguments :test 'string-equal)))
1.4888- (rest arguments)))
1.4889-
1.4890- (defun argv0 ()
1.4891- "On supported implementations (most that matter), or when invoked by a proper wrapper script,
1.4892-return a string that for the name with which the program was invoked, i.e. argv[0] in C.
1.4893-Otherwise, return NIL."
1.4894- (cond
1.4895- ((eq *image-dumped-p* :executable) ; yes, this ARGV0 is our argv0 !
1.4896- ;; NB: not currently available on ABCL, Corman, Genera, MCL
1.4897- (or #+(or allegro clisp clozure cmucl gcl lispworks sbcl scl xcl)
1.4898- (first (raw-command-line-arguments))
1.4899- #+(or clasp ecl) (si:argv 0) #+mkcl (mkcl:argv 0)))
1.4900- (t ;; argv[0] is the name of the interpreter.
1.4901- ;; The wrapper script can export __CL_ARGV0. cl-launch does as of 4.0.1.8.
1.4902- (getenvp "__CL_ARGV0"))))
1.4903-
1.4904- (defun setup-command-line-arguments ()
1.4905- (setf *command-line-arguments* (command-line-arguments)))
1.4906-
1.4907- (defun restore-image (&key
1.4908- (lisp-interaction *lisp-interaction*)
1.4909- (restore-hook *image-restore-hook*)
1.4910- (prelude *image-prelude*)
1.4911- (entry-point *image-entry-point*)
1.4912- (if-already-restored '(cerror "RUN RESTORE-IMAGE ANYWAY")))
1.4913- "From a freshly restarted Lisp image, restore the saved Lisp environment
1.4914-by setting appropriate variables, running various hooks, and calling any specified entry point.
1.4915-
1.4916-If the image has already been restored or is already being restored, as per *IMAGE-RESTORED-P*,
1.4917-call the IF-ALREADY-RESTORED error handler (by default, a continuable error), and do return
1.4918-immediately to the surrounding restore process if allowed to continue.
1.4919-
1.4920-Then, comes the restore process itself:
1.4921-First, call each function in the RESTORE-HOOK,
1.4922-in the order they were registered with REGISTER-IMAGE-RESTORE-HOOK.
1.4923-Second, evaluate the prelude, which is often Lisp text that is read,
1.4924-as per EVAL-INPUT.
1.4925-Third, call the ENTRY-POINT function, if any is specified, with no argument.
1.4926-
1.4927-The restore process happens in a WITH-FATAL-CONDITION-HANDLER, so that if LISP-INTERACTION is NIL,
1.4928-any unhandled error leads to a backtrace and an exit with an error status.
1.4929-If LISP-INTERACTION is NIL, the process also exits when no error occurs:
1.4930-if neither restart nor entry function is provided, the program will exit with status 0 (success);
1.4931-if a function was provided, the program will exit after the function returns (if it returns),
1.4932-with status 0 if and only if the primary return value of result is generalized boolean true,
1.4933-and with status 1 if this value is NIL.
1.4934-
1.4935-If LISP-INTERACTION is true, unhandled errors will take you to the debugger, and the result
1.4936-of the function will be returned rather than interpreted as a boolean designating an exit code."
1.4937- (when *image-restored-p*
1.4938- (if if-already-restored
1.4939- (call-function if-already-restored "Image already ~:[being ~;~]restored"
1.4940- (eq *image-restored-p* t))
1.4941- (return-from restore-image)))
1.4942- (with-fatal-condition-handler ()
1.4943- (setf *lisp-interaction* lisp-interaction)
1.4944- (setf *image-restore-hook* restore-hook)
1.4945- (setf *image-prelude* prelude)
1.4946- (setf *image-restored-p* :in-progress)
1.4947- (call-image-restore-hook)
1.4948- (standard-eval-thunk prelude)
1.4949- (setf *image-restored-p* t)
1.4950- (let ((results (multiple-value-list
1.4951- (if entry-point
1.4952- (call-function entry-point)
1.4953- t))))
1.4954- (if lisp-interaction
1.4955- (values-list results)
1.4956- (shell-boolean-exit (first results)))))))
1.4957-
1.4958-
1.4959-;;; Dumping an image
1.4960-
1.4961-(with-upgradability ()
1.4962- (defun dump-image (filename &key output-name executable
1.4963- (postlude *image-postlude*)
1.4964- (dump-hook *image-dump-hook*)
1.4965- #+clozure prepend-symbols #+clozure (purify t)
1.4966- #+sbcl compression
1.4967- #+(and sbcl os-windows) application-type)
1.4968- "Dump an image of the current Lisp environment at pathname FILENAME, with various options.
1.4969-
1.4970-First, finalize the image, by evaluating the POSTLUDE as per EVAL-INPUT, then calling each of
1.4971- the functions in DUMP-HOOK, in reverse order of registration by REGISTER-IMAGE-DUMP-HOOK.
1.4972-
1.4973-If EXECUTABLE is true, create an standalone executable program that calls RESTORE-IMAGE on startup.
1.4974-
1.4975-Pass various implementation-defined options, such as PREPEND-SYMBOLS and PURITY on CCL,
1.4976-or COMPRESSION on SBCL, and APPLICATION-TYPE on SBCL/Windows."
1.4977- ;; Note: at least SBCL saves only global values of variables in the heap image,
1.4978- ;; so make sure things you want to dump are NOT just local bindings shadowing the global values.
1.4979- (declare (ignorable filename output-name executable))
1.4980- (setf *image-dumped-p* (if executable :executable t))
1.4981- (setf *image-restored-p* :in-regress)
1.4982- (setf *image-postlude* postlude)
1.4983- (standard-eval-thunk *image-postlude*)
1.4984- (setf *image-dump-hook* dump-hook)
1.4985- (call-image-dump-hook)
1.4986- (setf *image-restored-p* nil)
1.4987- #-(or clisp clozure (and cmucl executable) lispworks sbcl scl)
1.4988- (when executable
1.4989- (not-implemented-error 'dump-image "dumping an executable"))
1.4990- #+allegro
1.4991- (progn
1.4992- (sys:resize-areas :global-gc t :pack-heap t :sift-old-areas t :tenure t) ; :new 5000000
1.4993- (excl:dumplisp :name filename :suppress-allegro-cl-banner t))
1.4994- #+clisp
1.4995- (apply #'ext:saveinitmem filename
1.4996- :quiet t
1.4997- :start-package *package*
1.4998- :keep-global-handlers nil
1.4999- ;; Faré explains the odd executable value (slightly paraphrased):
1.5000- ;; 0 is very different from t in clisp and there for a good reason:
1.5001- ;; 0 turns the executable into one that has its own command-line handling, so hackers can't
1.5002- ;; use the underlying -i or -x to turn your would-be restricted binary into an unrestricted evaluator.
1.5003- :executable (if executable 0 t) ;--- requires clisp 2.48 or later, still catches --clisp-x
1.5004- (when executable
1.5005- (list
1.5006- ;; :parse-options nil ;--- requires a non-standard patch to clisp.
1.5007- :norc t :script nil :init-function #'restore-image)))
1.5008- #+clozure
1.5009- (flet ((dump (prepend-kernel)
1.5010- (ccl:save-application filename :prepend-kernel prepend-kernel :purify purify
1.5011- :toplevel-function (when executable #'restore-image))))
1.5012- ;;(setf ccl::*application* (make-instance 'ccl::lisp-development-system))
1.5013- (if prepend-symbols
1.5014- (with-temporary-file (:prefix "ccl-symbols-" :direction :output :pathname path)
1.5015- (require 'elf)
1.5016- (funcall (fdefinition 'ccl::write-elf-symbols-to-file) path)
1.5017- (dump path))
1.5018- (dump t)))
1.5019- #+(or cmucl scl)
1.5020- (progn
1.5021- (ext:gc :full t)
1.5022- (setf ext:*batch-mode* nil)
1.5023- (setf ext::*gc-run-time* 0)
1.5024- (apply 'ext:save-lisp filename
1.5025- :allow-other-keys t ;; hush SCL and old versions of CMUCL
1.5026- #+(and cmucl executable) :executable #+(and cmucl executable) t
1.5027- (when executable '(:init-function restore-image :process-command-line nil
1.5028- :quiet t :load-init-file nil :site-init nil))))
1.5029- #+gcl
1.5030- (progn
1.5031- (si::set-hole-size 500) (si::gbc nil) (si::sgc-on t)
1.5032- (si::save-system filename))
1.5033- #+lispworks
1.5034- (if executable
1.5035- (lispworks:deliver 'restore-image filename 0 :interface nil)
1.5036- (hcl:save-image filename :environment nil))
1.5037- #+sbcl
1.5038- (progn
1.5039- ;;(sb-pcl::precompile-random-code-segments) ;--- it is ugly slow at compile-time (!) when the initial core is a big CLOS program. If you want it, do it yourself
1.5040- (setf sb-ext::*gc-run-time* 0)
1.5041- (apply 'sb-ext:save-lisp-and-die filename
1.5042- :executable t ;--- always include the runtime that goes with the core
1.5043- (append
1.5044- (when compression (list :compression compression))
1.5045- ;;--- only save runtime-options for standalone executables
1.5046- (when executable (list :toplevel #'restore-image :save-runtime-options t))
1.5047- #+(and sbcl os-windows) ;; passing :application-type :gui will disable the console window.
1.5048- ;; the default is :console - only works with SBCL 1.1.15 or later.
1.5049- (when application-type (list :application-type application-type)))))
1.5050- #-(or allegro clisp clozure cmucl gcl lispworks sbcl scl)
1.5051- (not-implemented-error 'dump-image))
1.5052-
1.5053- (defun create-image (destination lisp-object-files
1.5054- &key kind output-name prologue-code epilogue-code extra-object-files
1.5055- (prelude () preludep) (postlude () postludep)
1.5056- (entry-point () entry-point-p) build-args no-uiop)
1.5057- (declare (ignorable destination lisp-object-files extra-object-files kind output-name
1.5058- prologue-code epilogue-code prelude preludep postlude postludep
1.5059- entry-point entry-point-p build-args no-uiop))
1.5060- "On ECL, create an executable at pathname DESTINATION from the specified OBJECT-FILES and options"
1.5061- ;; Is it meaningful to run these in the current environment?
1.5062- ;; only if we also track the object files that constitute the "current" image,
1.5063- ;; and otherwise simulate dump-image, including quitting at the end.
1.5064- #-(or clasp ecl mkcl) (not-implemented-error 'create-image)
1.5065- #+(or clasp ecl mkcl)
1.5066- (let ((epilogue-code
1.5067- (if no-uiop
1.5068- epilogue-code
1.5069- (let ((forms
1.5070- (append
1.5071- (when epilogue-code `(,epilogue-code))
1.5072- (when postludep `((setf *image-postlude* ',postlude)))
1.5073- (when preludep `((setf *image-prelude* ',prelude)))
1.5074- (when entry-point-p `((setf *image-entry-point* ',entry-point)))
1.5075- (case kind
1.5076- ((:image)
1.5077- (setf kind :program) ;; to ECL, it's just another program.
1.5078- `((setf *image-dumped-p* t)
1.5079- (si::top-level #+(or clasp ecl) t) (quit)))
1.5080- ((:program)
1.5081- `((setf *image-dumped-p* :executable)
1.5082- (shell-boolean-exit
1.5083- (restore-image))))))))
1.5084- (when forms `(progn ,@forms))))))
1.5085- (check-type kind (member :dll :shared-library :lib :static-library
1.5086- :fasl :fasb :program))
1.5087- (apply #+clasp 'cmp:builder #+clasp kind
1.5088- #+(or ecl mkcl)
1.5089- (ecase kind
1.5090- ((:dll :shared-library)
1.5091- #+ecl 'c::build-shared-library #+mkcl 'compiler:build-shared-library)
1.5092- ((:lib :static-library)
1.5093- #+ecl 'c::build-static-library #+mkcl 'compiler:build-static-library)
1.5094- ((:fasl #+ecl :fasb)
1.5095- #+ecl 'c::build-fasl #+mkcl 'compiler:build-fasl)
1.5096- #+mkcl ((:fasb) 'compiler:build-bundle)
1.5097- ((:program)
1.5098- #+ecl 'c::build-program #+mkcl 'compiler:build-program))
1.5099- (pathname destination)
1.5100- #+(or clasp ecl) :lisp-files #+mkcl :lisp-object-files
1.5101- (append lisp-object-files #+(or clasp ecl) extra-object-files)
1.5102- #+ecl :init-name
1.5103- #+ecl (getf build-args :init-name)
1.5104- (append
1.5105- (when prologue-code `(:prologue-code ,prologue-code))
1.5106- (when epilogue-code `(:epilogue-code ,epilogue-code))
1.5107- #+mkcl (when extra-object-files `(:object-files ,extra-object-files))
1.5108- build-args)))))
1.5109-
1.5110-
1.5111-;;; Some universal image restore hooks
1.5112-(with-upgradability ()
1.5113- (map () 'register-image-restore-hook
1.5114- '(setup-stdin setup-stdout setup-stderr
1.5115- setup-command-line-arguments setup-temporary-directory
1.5116- #+abcl detect-os)))
1.5117-;;;; -------------------------------------------------------------------------
1.5118-;;;; Support to build (compile and load) Lisp files
1.5119-
1.5120-(uiop/package:define-package :uiop/lisp-build
1.5121- (:nicknames :asdf/lisp-build) ;; OBSOLETE, used by slime/contrib/swank-asdf.lisp
1.5122- (:use :uiop/common-lisp :uiop/package :uiop/utility
1.5123- :uiop/os :uiop/pathname :uiop/filesystem :uiop/stream :uiop/image)
1.5124- (:export
1.5125- ;; Variables
1.5126- #:*compile-file-warnings-behaviour* #:*compile-file-failure-behaviour*
1.5127- #:*output-translation-function*
1.5128- #:*optimization-settings* #:*previous-optimization-settings*
1.5129- #:*base-build-directory*
1.5130- #:compile-condition #:compile-file-error #:compile-warned-error #:compile-failed-error
1.5131- #:compile-warned-warning #:compile-failed-warning
1.5132- #:check-lisp-compile-results #:check-lisp-compile-warnings
1.5133- #:*uninteresting-conditions* #:*usual-uninteresting-conditions*
1.5134- #:*uninteresting-compiler-conditions* #:*uninteresting-loader-conditions*
1.5135- ;; Types
1.5136- #+sbcl #:sb-grovel-unknown-constant-condition
1.5137- ;; Functions & Macros
1.5138- #:get-optimization-settings #:proclaim-optimization-settings #:with-optimization-settings
1.5139- #:call-with-muffled-compiler-conditions #:with-muffled-compiler-conditions
1.5140- #:call-with-muffled-loader-conditions #:with-muffled-loader-conditions
1.5141- #:reify-simple-sexp #:unreify-simple-sexp
1.5142- #:reify-deferred-warnings #:unreify-deferred-warnings
1.5143- #:reset-deferred-warnings #:save-deferred-warnings #:check-deferred-warnings
1.5144- #:with-saved-deferred-warnings #:warnings-file-p #:warnings-file-type #:*warnings-file-type*
1.5145- #:enable-deferred-warnings-check #:disable-deferred-warnings-check
1.5146- #:current-lisp-file-pathname #:load-pathname
1.5147- #:lispize-pathname #:compile-file-type #:call-around-hook
1.5148- #:compile-file* #:compile-file-pathname* #:*compile-check*
1.5149- #:load* #:load-from-string #:combine-fasls)
1.5150- (:intern #:defaults #:failure-p #:warnings-p #:s #:y #:body))
1.5151-(in-package :uiop/lisp-build)
1.5152-
1.5153-(with-upgradability ()
1.5154- (defvar *compile-file-warnings-behaviour*
1.5155- (or #+clisp :ignore :warn)
1.5156- "How should ASDF react if it encounters a warning when compiling a file?
1.5157-Valid values are :error, :warn, and :ignore.")
1.5158-
1.5159- (defvar *compile-file-failure-behaviour*
1.5160- (or #+(or mkcl sbcl) :error #+clisp :ignore :warn)
1.5161- "How should ASDF react if it encounters a failure (per the ANSI spec of COMPILE-FILE)
1.5162-when compiling a file, which includes any non-style-warning warning.
1.5163-Valid values are :error, :warn, and :ignore.
1.5164-Note that ASDF ALWAYS raises an error if it fails to create an output file when compiling.")
1.5165-
1.5166- (defvar *base-build-directory* nil
1.5167- "When set to a non-null value, it should be an absolute directory pathname,
1.5168-which will serve as the *DEFAULT-PATHNAME-DEFAULTS* around a COMPILE-FILE,
1.5169-what more while the input-file is shortened if possible to ENOUGH-PATHNAME relative to it.
1.5170-This can help you produce more deterministic output for FASLs."))
1.5171-
1.5172-;;; Optimization settings
1.5173-(with-upgradability ()
1.5174- (defvar *optimization-settings* nil
1.5175- "Optimization settings to be used by PROCLAIM-OPTIMIZATION-SETTINGS")
1.5176- (defvar *previous-optimization-settings* nil
1.5177- "Optimization settings saved by PROCLAIM-OPTIMIZATION-SETTINGS")
1.5178- (defparameter +optimization-variables+
1.5179- ;; TODO: allegro genera corman mcl
1.5180- (or #+(or abcl xcl) '(system::*speed* system::*space* system::*safety* system::*debug*)
1.5181- #+clisp '() ;; system::*optimize* is a constant hash-table! (with non-constant contents)
1.5182- #+clozure '(ccl::*nx-speed* ccl::*nx-space* ccl::*nx-safety*
1.5183- ccl::*nx-debug* ccl::*nx-cspeed*)
1.5184- #+(or cmucl scl) '(c::*default-cookie*)
1.5185- #+clasp nil
1.5186- #+ecl (unless (use-ecl-byte-compiler-p) '(c::*speed* c::*space* c::*safety* c::*debug*))
1.5187- #+gcl '(compiler::*speed* compiler::*space* compiler::*compiler-new-safety* compiler::*debug*)
1.5188- #+lispworks '(compiler::*optimization-level*)
1.5189- #+mkcl '(si::*speed* si::*space* si::*safety* si::*debug*)
1.5190- #+sbcl '(sb-c::*policy*)))
1.5191- (defun get-optimization-settings ()
1.5192- "Get current compiler optimization settings, ready to PROCLAIM again"
1.5193- #-(or abcl allegro clasp clisp clozure cmucl ecl lispworks mkcl sbcl scl xcl)
1.5194- (warn "~S does not support ~S. Please help me fix that."
1.5195- 'get-optimization-settings (implementation-type))
1.5196- #+clasp (cleavir-env:optimize (cleavir-env:optimize-info CLASP-CLEAVIR:*CLASP-ENV*))
1.5197- #+(or abcl allegro clisp clozure cmucl ecl lispworks mkcl sbcl scl xcl)
1.5198- (let ((settings '(speed space safety debug compilation-speed #+(or cmucl scl) c::brevity)))
1.5199- #.`(loop #+(or allegro clozure)
1.5200- ,@'(:with info = #+allegro (sys:declaration-information 'optimize)
1.5201- #+clozure (ccl:declaration-information 'optimize nil))
1.5202- :for x :in settings
1.5203- ,@(or #+(or abcl clasp ecl gcl mkcl xcl) '(:for v :in +optimization-variables+))
1.5204- :for y = (or #+(or allegro clozure) (second (assoc x info)) ; normalize order
1.5205- #+clisp (gethash x system::*optimize* 1)
1.5206- #+(or abcl ecl mkcl xcl) (symbol-value v)
1.5207- #+(or cmucl scl) (slot-value c::*default-cookie*
1.5208- (case x (compilation-speed 'c::cspeed)
1.5209- (otherwise x)))
1.5210- #+lispworks (slot-value compiler::*optimization-level* x)
1.5211- #+sbcl (sb-c::policy-quality sb-c::*policy* x))
1.5212- :when y :collect (list x y))))
1.5213- (defun proclaim-optimization-settings ()
1.5214- "Proclaim the optimization settings in *OPTIMIZATION-SETTINGS*"
1.5215- (proclaim `(optimize ,@*optimization-settings*))
1.5216- (let ((settings (get-optimization-settings)))
1.5217- (unless (equal *previous-optimization-settings* settings)
1.5218- (setf *previous-optimization-settings* settings))))
1.5219- (defmacro with-optimization-settings ((&optional (settings *optimization-settings*)) &body body)
1.5220- #+(or allegro clasp clisp)
1.5221- (let ((previous-settings (gensym "PREVIOUS-SETTINGS"))
1.5222- (reset-settings (gensym "RESET-SETTINGS")))
1.5223- `(let* ((,previous-settings (get-optimization-settings))
1.5224- (,reset-settings #+clasp (reverse ,previous-settings) #-clasp ,previous-settings))
1.5225- ,@(when settings `((proclaim `(optimize ,@,settings))))
1.5226- (unwind-protect (progn ,@body)
1.5227- (proclaim `(optimize ,@,reset-settings)))))
1.5228- #-(or allegro clasp clisp)
1.5229- `(let ,(loop :for v :in +optimization-variables+ :collect `(,v ,v))
1.5230- ,@(when settings `((proclaim `(optimize ,@,settings))))
1.5231- ,@body)))
1.5232-
1.5233-
1.5234-;;; Condition control
1.5235-(with-upgradability ()
1.5236- #+sbcl
1.5237- (progn
1.5238- (defun sb-grovel-unknown-constant-condition-p (c)
1.5239- "Detect SB-GROVEL unknown-constant conditions on older versions of SBCL"
1.5240- (ignore-errors
1.5241- (and (typep c 'sb-int:simple-style-warning)
1.5242- (string-enclosed-p
1.5243- "Couldn't grovel for "
1.5244- (simple-condition-format-control c)
1.5245- " (unknown to the C compiler)."))))
1.5246- (deftype sb-grovel-unknown-constant-condition ()
1.5247- '(and style-warning (satisfies sb-grovel-unknown-constant-condition-p))))
1.5248-
1.5249- (defvar *usual-uninteresting-conditions*
1.5250- (append
1.5251- ;;#+clozure '(ccl:compiler-warning)
1.5252- #+cmucl '("Deleting unreachable code.")
1.5253- #+lispworks '("~S being redefined in ~A (previously in ~A)."
1.5254- "~S defined more than once in ~A.") ;; lispworks gets confused by eval-when.
1.5255- #+sbcl
1.5256- '(sb-c::simple-compiler-note
1.5257- "&OPTIONAL and &KEY found in the same lambda list: ~S"
1.5258- sb-kernel:undefined-alien-style-warning
1.5259- sb-grovel-unknown-constant-condition ; defined above.
1.5260- sb-ext:implicit-generic-function-warning ;; Controversial.
1.5261- sb-int:package-at-variance
1.5262- sb-kernel:uninteresting-redefinition
1.5263- ;; BEWARE: the below four are controversial to include here.
1.5264- sb-kernel:redefinition-with-defun
1.5265- sb-kernel:redefinition-with-defgeneric
1.5266- sb-kernel:redefinition-with-defmethod
1.5267- sb-kernel::redefinition-with-defmacro) ; not exported by old SBCLs
1.5268- #+sbcl
1.5269- (let ((condition (find-symbol* '#:lexical-environment-too-complex :sb-kernel nil)))
1.5270- (when condition
1.5271- (list condition)))
1.5272- '("No generic function ~S present when encountering macroexpansion of defmethod. Assuming it will be an instance of standard-generic-function.")) ;; from closer2mop
1.5273- "A suggested value to which to set or bind *uninteresting-conditions*.")
1.5274-
1.5275- (defvar *uninteresting-conditions* '()
1.5276- "Conditions that may be skipped while compiling or loading Lisp code.")
1.5277- (defvar *uninteresting-compiler-conditions* '()
1.5278- "Additional conditions that may be skipped while compiling Lisp code.")
1.5279- (defvar *uninteresting-loader-conditions*
1.5280- (append
1.5281- '("Overwriting already existing readtable ~S." ;; from named-readtables
1.5282- #(#:finalizers-off-warning :asdf-finalizers)) ;; from asdf-finalizers
1.5283- #+clisp '(clos::simple-gf-replacing-method-warning))
1.5284- "Additional conditions that may be skipped while loading Lisp code."))
1.5285-
1.5286-;;;; ----- Filtering conditions while building -----
1.5287-(with-upgradability ()
1.5288- (defun call-with-muffled-compiler-conditions (thunk)
1.5289- "Call given THUNK in a context where uninteresting conditions and compiler conditions are muffled"
1.5290- (call-with-muffled-conditions
1.5291- thunk (append *uninteresting-conditions* *uninteresting-compiler-conditions*)))
1.5292- (defmacro with-muffled-compiler-conditions ((&optional) &body body)
1.5293- "Trivial syntax for CALL-WITH-MUFFLED-COMPILER-CONDITIONS"
1.5294- `(call-with-muffled-compiler-conditions #'(lambda () ,@body)))
1.5295- (defun call-with-muffled-loader-conditions (thunk)
1.5296- "Call given THUNK in a context where uninteresting conditions and loader conditions are muffled"
1.5297- (call-with-muffled-conditions
1.5298- thunk (append *uninteresting-conditions* *uninteresting-loader-conditions*)))
1.5299- (defmacro with-muffled-loader-conditions ((&optional) &body body)
1.5300- "Trivial syntax for CALL-WITH-MUFFLED-LOADER-CONDITIONS"
1.5301- `(call-with-muffled-loader-conditions #'(lambda () ,@body))))
1.5302-
1.5303-
1.5304-;;;; Handle warnings and failures
1.5305-(with-upgradability ()
1.5306- (define-condition compile-condition (condition)
1.5307- ((context-format
1.5308- :initform nil :reader compile-condition-context-format :initarg :context-format)
1.5309- (context-arguments
1.5310- :initform nil :reader compile-condition-context-arguments :initarg :context-arguments)
1.5311- (description
1.5312- :initform nil :reader compile-condition-description :initarg :description))
1.5313- (:report (lambda (c s)
1.5314- (format s (compatfmt "~@<~A~@[ while ~?~]~@:>")
1.5315- (or (compile-condition-description c) (type-of c))
1.5316- (compile-condition-context-format c)
1.5317- (compile-condition-context-arguments c)))))
1.5318- (define-condition compile-file-error (compile-condition error) ())
1.5319- (define-condition compile-warned-warning (compile-condition warning) ())
1.5320- (define-condition compile-warned-error (compile-condition error) ())
1.5321- (define-condition compile-failed-warning (compile-condition warning) ())
1.5322- (define-condition compile-failed-error (compile-condition error) ())
1.5323-
1.5324- (defun check-lisp-compile-warnings (warnings-p failure-p
1.5325- &optional context-format context-arguments)
1.5326- "Given the warnings or failures as resulted from COMPILE-FILE or checking deferred warnings,
1.5327-raise an error or warning as appropriate"
1.5328- (when failure-p
1.5329- (case *compile-file-failure-behaviour*
1.5330- (:warn (warn 'compile-failed-warning
1.5331- :description "Lisp compilation failed"
1.5332- :context-format context-format
1.5333- :context-arguments context-arguments))
1.5334- (:error (error 'compile-failed-error
1.5335- :description "Lisp compilation failed"
1.5336- :context-format context-format
1.5337- :context-arguments context-arguments))
1.5338- (:ignore nil)))
1.5339- (when warnings-p
1.5340- (case *compile-file-warnings-behaviour*
1.5341- (:warn (warn 'compile-warned-warning
1.5342- :description "Lisp compilation had style-warnings"
1.5343- :context-format context-format
1.5344- :context-arguments context-arguments))
1.5345- (:error (error 'compile-warned-error
1.5346- :description "Lisp compilation had style-warnings"
1.5347- :context-format context-format
1.5348- :context-arguments context-arguments))
1.5349- (:ignore nil))))
1.5350-
1.5351- (defun check-lisp-compile-results (output warnings-p failure-p
1.5352- &optional context-format context-arguments)
1.5353- "Given the results of COMPILE-FILE, raise an error or warning as appropriate"
1.5354- (unless output
1.5355- (error 'compile-file-error :context-format context-format :context-arguments context-arguments))
1.5356- (check-lisp-compile-warnings warnings-p failure-p context-format context-arguments)))
1.5357-
1.5358-
1.5359-;;;; Deferred-warnings treatment, originally implemented by Douglas Katzman.
1.5360-;;;
1.5361-;;; To support an implementation, three functions must be implemented:
1.5362-;;; reify-deferred-warnings unreify-deferred-warnings reset-deferred-warnings
1.5363-;;; See their respective docstrings.
1.5364-(with-upgradability ()
1.5365- (defun reify-simple-sexp (sexp)
1.5366- "Given a simple SEXP, return a representation of it as a portable SEXP.
1.5367-Simple means made of symbols, numbers, characters, simple-strings, pathnames, cons cells."
1.5368- (etypecase sexp
1.5369- (symbol (reify-symbol sexp))
1.5370- ((or number character simple-string pathname) sexp)
1.5371- (cons (cons (reify-simple-sexp (car sexp)) (reify-simple-sexp (cdr sexp))))
1.5372- (simple-vector (vector (mapcar 'reify-simple-sexp (coerce sexp 'list))))))
1.5373-
1.5374- (defun unreify-simple-sexp (sexp)
1.5375- "Given the portable output of REIFY-SIMPLE-SEXP, return the simple SEXP it represents"
1.5376- (etypecase sexp
1.5377- ((or symbol number character simple-string pathname) sexp)
1.5378- (cons (cons (unreify-simple-sexp (car sexp)) (unreify-simple-sexp (cdr sexp))))
1.5379- ((simple-vector 2) (unreify-symbol sexp))
1.5380- ((simple-vector 1) (coerce (mapcar 'unreify-simple-sexp (aref sexp 0)) 'vector))))
1.5381-
1.5382- #+clozure
1.5383- (progn
1.5384- (defun reify-source-note (source-note)
1.5385- (when source-note
1.5386- (with-accessors ((source ccl::source-note-source) (filename ccl:source-note-filename)
1.5387- (start-pos ccl:source-note-start-pos) (end-pos ccl:source-note-end-pos)) source-note
1.5388- (declare (ignorable source))
1.5389- (list :filename filename :start-pos start-pos :end-pos end-pos
1.5390- #|:source (reify-source-note source)|#))))
1.5391- (defun unreify-source-note (source-note)
1.5392- (when source-note
1.5393- (destructuring-bind (&key filename start-pos end-pos source) source-note
1.5394- (ccl::make-source-note :filename filename :start-pos start-pos :end-pos end-pos
1.5395- :source (unreify-source-note source)))))
1.5396- (defun unsymbolify-function-name (name)
1.5397- (if-let (setfed (gethash name ccl::%setf-function-name-inverses%))
1.5398- `(setf ,setfed)
1.5399- name))
1.5400- (defun symbolify-function-name (name)
1.5401- (if (and (consp name) (eq (first name) 'setf))
1.5402- (let ((setfed (second name)))
1.5403- (gethash setfed ccl::%setf-function-names%))
1.5404- name))
1.5405- (defun reify-function-name (function-name)
1.5406- (let ((name (or (first function-name) ;; defun: extract the name
1.5407- (let ((sec (second function-name)))
1.5408- (or (and (atom sec) sec) ; scoped method: drop scope
1.5409- (first sec)))))) ; method: keep gf name, drop method specializers
1.5410- (list name)))
1.5411- (defun unreify-function-name (function-name)
1.5412- function-name)
1.5413- (defun nullify-non-literals (sexp)
1.5414- (typecase sexp
1.5415- ((or number character simple-string symbol pathname) sexp)
1.5416- (cons (cons (nullify-non-literals (car sexp))
1.5417- (nullify-non-literals (cdr sexp))))
1.5418- (t nil)))
1.5419- (defun reify-deferred-warning (deferred-warning)
1.5420- (with-accessors ((warning-type ccl::compiler-warning-warning-type)
1.5421- (args ccl::compiler-warning-args)
1.5422- (source-note ccl:compiler-warning-source-note)
1.5423- (function-name ccl:compiler-warning-function-name)) deferred-warning
1.5424- (list :warning-type warning-type :function-name (reify-function-name function-name)
1.5425- :source-note (reify-source-note source-note)
1.5426- :args (destructuring-bind (fun &rest more)
1.5427- args
1.5428- (cons (unsymbolify-function-name fun)
1.5429- (nullify-non-literals more))))))
1.5430- (defun unreify-deferred-warning (reified-deferred-warning)
1.5431- (destructuring-bind (&key warning-type function-name source-note args)
1.5432- reified-deferred-warning
1.5433- (make-condition (or (cdr (ccl::assq warning-type ccl::*compiler-whining-conditions*))
1.5434- 'ccl::compiler-warning)
1.5435- :function-name (unreify-function-name function-name)
1.5436- :source-note (unreify-source-note source-note)
1.5437- :warning-type warning-type
1.5438- :args (destructuring-bind (fun . more) args
1.5439- (cons (symbolify-function-name fun) more))))))
1.5440- #+(or cmucl scl)
1.5441- (defun reify-undefined-warning (warning)
1.5442- ;; Extracting undefined-warnings from the compilation-unit
1.5443- ;; To be passed through the above reify/unreify link, it must be a "simple-sexp"
1.5444- (list*
1.5445- (c::undefined-warning-kind warning)
1.5446- (c::undefined-warning-name warning)
1.5447- (c::undefined-warning-count warning)
1.5448- (mapcar
1.5449- #'(lambda (frob)
1.5450- ;; the lexenv slot can be ignored for reporting purposes
1.5451- `(:enclosing-source ,(c::compiler-error-context-enclosing-source frob)
1.5452- :source ,(c::compiler-error-context-source frob)
1.5453- :original-source ,(c::compiler-error-context-original-source frob)
1.5454- :context ,(c::compiler-error-context-context frob)
1.5455- :file-name ,(c::compiler-error-context-file-name frob) ; a pathname
1.5456- :file-position ,(c::compiler-error-context-file-position frob) ; an integer
1.5457- :original-source-path ,(c::compiler-error-context-original-source-path frob)))
1.5458- (c::undefined-warning-warnings warning))))
1.5459-
1.5460- #+sbcl
1.5461- (defun reify-undefined-warning (warning)
1.5462- ;; Extracting undefined-warnings from the compilation-unit
1.5463- ;; To be passed through the above reify/unreify link, it must be a "simple-sexp"
1.5464- (list*
1.5465- (sb-c::undefined-warning-kind warning)
1.5466- (sb-c::undefined-warning-name warning)
1.5467- (sb-c::undefined-warning-count warning)
1.5468- ;; the COMPILER-ERROR-CONTEXT struct has changed in SBCL, which means how we
1.5469- ;; handle deferred warnings must change... TODO: when enough time has
1.5470- ;; gone by, just assume all versions of SBCL are adequately
1.5471- ;; up-to-date, and cut this material.[2018/05/30:rpg]
1.5472- (mapcar
1.5473- #'(lambda (frob)
1.5474- ;; the lexenv slot can be ignored for reporting purposes
1.5475- `(
1.5476- #- #.(uiop/utility:symbol-test-to-feature-expression '#:compiler-error-context-%source '#:sb-c)
1.5477- ,@`(:enclosing-source
1.5478- ,(sb-c::compiler-error-context-enclosing-source frob)
1.5479- :source
1.5480- ,(sb-c::compiler-error-context-source frob)
1.5481- :original-source
1.5482- ,(sb-c::compiler-error-context-original-source frob))
1.5483- #+ #.(uiop/utility:symbol-test-to-feature-expression '#:compiler-error-context-%source '#:sb-c)
1.5484- ,@ `(:%enclosing-source
1.5485- ,(sb-c::compiler-error-context-enclosing-source frob)
1.5486- :%source
1.5487- ,(sb-c::compiler-error-context-source frob)
1.5488- :original-form
1.5489- ,(sb-c::compiler-error-context-original-form frob))
1.5490- :context ,(sb-c::compiler-error-context-context frob)
1.5491- :file-name ,(sb-c::compiler-error-context-file-name frob) ; a pathname
1.5492- :file-position ,(sb-c::compiler-error-context-file-position frob) ; an integer
1.5493- :original-source-path ,(sb-c::compiler-error-context-original-source-path frob)))
1.5494- (sb-c::undefined-warning-warnings warning))))
1.5495-
1.5496- (defun reify-deferred-warnings ()
1.5497- "return a portable S-expression, portably readable and writeable in any Common Lisp implementation
1.5498-using READ within a WITH-SAFE-IO-SYNTAX, that represents the warnings currently deferred by
1.5499-WITH-COMPILATION-UNIT. One of three functions required for deferred-warnings support in ASDF."
1.5500- #+allegro
1.5501- (list :functions-defined excl::.functions-defined.
1.5502- :functions-called excl::.functions-called.)
1.5503- #+clozure
1.5504- (mapcar 'reify-deferred-warning
1.5505- (if-let (dw ccl::*outstanding-deferred-warnings*)
1.5506- (let ((mdw (ccl::ensure-merged-deferred-warnings dw)))
1.5507- (ccl::deferred-warnings.warnings mdw))))
1.5508- #+(or cmucl scl)
1.5509- (when lisp::*in-compilation-unit*
1.5510- ;; Try to send nothing through the pipe if nothing needs to be accumulated
1.5511- `(,@(when c::*undefined-warnings*
1.5512- `((c::*undefined-warnings*
1.5513- ,@(mapcar #'reify-undefined-warning c::*undefined-warnings*))))
1.5514- ,@(loop :for what :in '(c::*compiler-error-count*
1.5515- c::*compiler-warning-count*
1.5516- c::*compiler-note-count*)
1.5517- :for value = (symbol-value what)
1.5518- :when (plusp value)
1.5519- :collect `(,what . ,value))))
1.5520- #+sbcl
1.5521- (when sb-c::*in-compilation-unit*
1.5522- ;; Try to send nothing through the pipe if nothing needs to be accumulated
1.5523- `(,@(when sb-c::*undefined-warnings*
1.5524- `((sb-c::*undefined-warnings*
1.5525- ,@(mapcar #'reify-undefined-warning sb-c::*undefined-warnings*))))
1.5526- ,@(loop :for what :in '(sb-c::*aborted-compilation-unit-count*
1.5527- sb-c::*compiler-error-count*
1.5528- sb-c::*compiler-warning-count*
1.5529- sb-c::*compiler-style-warning-count*
1.5530- sb-c::*compiler-note-count*)
1.5531- :for value = (symbol-value what)
1.5532- :when (plusp value)
1.5533- :collect `(,what . ,value)))))
1.5534-
1.5535- (defun unreify-deferred-warnings (reified-deferred-warnings)
1.5536- "given a S-expression created by REIFY-DEFERRED-WARNINGS, reinstantiate the corresponding
1.5537-deferred warnings as to be handled at the end of the current WITH-COMPILATION-UNIT.
1.5538-Handle any warning that has been resolved already,
1.5539-such as an undefined function that has been defined since.
1.5540-One of three functions required for deferred-warnings support in ASDF."
1.5541- (declare (ignorable reified-deferred-warnings))
1.5542- #+allegro
1.5543- (destructuring-bind (&key functions-defined functions-called)
1.5544- reified-deferred-warnings
1.5545- (setf excl::.functions-defined.
1.5546- (append functions-defined excl::.functions-defined.)
1.5547- excl::.functions-called.
1.5548- (append functions-called excl::.functions-called.)))
1.5549- #+clozure
1.5550- (let ((dw (or ccl::*outstanding-deferred-warnings*
1.5551- (setf ccl::*outstanding-deferred-warnings* (ccl::%defer-warnings t)))))
1.5552- (appendf (ccl::deferred-warnings.warnings dw)
1.5553- (mapcar 'unreify-deferred-warning reified-deferred-warnings)))
1.5554- #+(or cmucl scl)
1.5555- (dolist (item reified-deferred-warnings)
1.5556- ;; Each item is (symbol . adjustment) where the adjustment depends on the symbol.
1.5557- ;; For *undefined-warnings*, the adjustment is a list of initargs.
1.5558- ;; For everything else, it's an integer.
1.5559- (destructuring-bind (symbol . adjustment) item
1.5560- (case symbol
1.5561- ((c::*undefined-warnings*)
1.5562- (setf c::*undefined-warnings*
1.5563- (nconc (mapcan
1.5564- #'(lambda (stuff)
1.5565- (destructuring-bind (kind name count . rest) stuff
1.5566- (unless (case kind (:function (fboundp name)))
1.5567- (list
1.5568- (c::make-undefined-warning
1.5569- :name name
1.5570- :kind kind
1.5571- :count count
1.5572- :warnings
1.5573- (mapcar #'(lambda (x)
1.5574- (apply #'c::make-compiler-error-context x))
1.5575- rest))))))
1.5576- adjustment)
1.5577- c::*undefined-warnings*)))
1.5578- (otherwise
1.5579- (set symbol (+ (symbol-value symbol) adjustment))))))
1.5580- #+sbcl
1.5581- (dolist (item reified-deferred-warnings)
1.5582- ;; Each item is (symbol . adjustment) where the adjustment depends on the symbol.
1.5583- ;; For *undefined-warnings*, the adjustment is a list of initargs.
1.5584- ;; For everything else, it's an integer.
1.5585- (destructuring-bind (symbol . adjustment) item
1.5586- (case symbol
1.5587- ((sb-c::*undefined-warnings*)
1.5588- (setf sb-c::*undefined-warnings*
1.5589- (nconc (mapcan
1.5590- #'(lambda (stuff)
1.5591- (destructuring-bind (kind name count . rest) stuff
1.5592- (unless (case kind (:function (fboundp name)))
1.5593- (list
1.5594- (sb-c::make-undefined-warning
1.5595- :name name
1.5596- :kind kind
1.5597- :count count
1.5598- :warnings
1.5599- (mapcar #'(lambda (x)
1.5600- (apply #'sb-c::make-compiler-error-context x))
1.5601- rest))))))
1.5602- adjustment)
1.5603- sb-c::*undefined-warnings*)))
1.5604- (otherwise
1.5605- (set symbol (+ (symbol-value symbol) adjustment)))))))
1.5606-
1.5607- (defun reset-deferred-warnings ()
1.5608- "Reset the set of deferred warnings to be handled at the end of the current WITH-COMPILATION-UNIT.
1.5609-One of three functions required for deferred-warnings support in ASDF."
1.5610- #+allegro
1.5611- (setf excl::.functions-defined. nil
1.5612- excl::.functions-called. nil)
1.5613- #+clozure
1.5614- (if-let (dw ccl::*outstanding-deferred-warnings*)
1.5615- (let ((mdw (ccl::ensure-merged-deferred-warnings dw)))
1.5616- (setf (ccl::deferred-warnings.warnings mdw) nil)))
1.5617- #+(or cmucl scl)
1.5618- (when lisp::*in-compilation-unit*
1.5619- (setf c::*undefined-warnings* nil
1.5620- c::*compiler-error-count* 0
1.5621- c::*compiler-warning-count* 0
1.5622- c::*compiler-note-count* 0))
1.5623- #+sbcl
1.5624- (when sb-c::*in-compilation-unit*
1.5625- (setf sb-c::*undefined-warnings* nil
1.5626- sb-c::*aborted-compilation-unit-count* 0
1.5627- sb-c::*compiler-error-count* 0
1.5628- sb-c::*compiler-warning-count* 0
1.5629- sb-c::*compiler-style-warning-count* 0
1.5630- sb-c::*compiler-note-count* 0)))
1.5631-
1.5632- (defun save-deferred-warnings (warnings-file)
1.5633- "Save forward reference conditions so they may be issued at a latter time,
1.5634-possibly in a different process."
1.5635- (with-open-file (s warnings-file :direction :output :if-exists :supersede
1.5636- :element-type *default-stream-element-type*
1.5637- :external-format *utf-8-external-format*)
1.5638- (with-safe-io-syntax ()
1.5639- (let ((*read-eval* t))
1.5640- (write (reify-deferred-warnings) :stream s :pretty t :readably t))
1.5641- (terpri s))))
1.5642-
1.5643- (defun warnings-file-type (&optional implementation-type)
1.5644- "The pathname type for warnings files on given IMPLEMENTATION-TYPE,
1.5645-where NIL designates the current one"
1.5646- (case (or implementation-type *implementation-type*)
1.5647- ((:acl :allegro) "allegro-warnings")
1.5648- ;;((:clisp) "clisp-warnings")
1.5649- ((:cmu :cmucl) "cmucl-warnings")
1.5650- ((:sbcl) "sbcl-warnings")
1.5651- ((:clozure :ccl) "ccl-warnings")
1.5652- ((:scl) "scl-warnings")))
1.5653-
1.5654- (defvar *warnings-file-type* nil
1.5655- "Pathname type for warnings files, or NIL if disabled")
1.5656-
1.5657- (defun enable-deferred-warnings-check ()
1.5658- "Enable the saving of deferred warnings"
1.5659- (setf *warnings-file-type* (warnings-file-type)))
1.5660-
1.5661- (defun disable-deferred-warnings-check ()
1.5662- "Disable the saving of deferred warnings"
1.5663- (setf *warnings-file-type* nil))
1.5664-
1.5665- (defun warnings-file-p (file &optional implementation-type)
1.5666- "Is FILE a saved warnings file for the given IMPLEMENTATION-TYPE?
1.5667-If that given type is NIL, use the currently configured *WARNINGS-FILE-TYPE* instead."
1.5668- (if-let (type (if implementation-type
1.5669- (warnings-file-type implementation-type)
1.5670- *warnings-file-type*))
1.5671- (equal (pathname-type file) type)))
1.5672-
1.5673- (defun check-deferred-warnings (files &optional context-format context-arguments)
1.5674- "Given a list of FILES containing deferred warnings saved by CALL-WITH-SAVED-DEFERRED-WARNINGS,
1.5675-re-intern and raise any warnings that are still meaningful."
1.5676- (let ((file-errors nil)
1.5677- (failure-p nil)
1.5678- (warnings-p nil))
1.5679- (handler-bind
1.5680- ((warning #'(lambda (c)
1.5681- (setf warnings-p t)
1.5682- (unless (typep c 'style-warning)
1.5683- (setf failure-p t)))))
1.5684- (with-compilation-unit (:override t)
1.5685- (reset-deferred-warnings)
1.5686- (dolist (file files)
1.5687- (unreify-deferred-warnings
1.5688- (handler-case
1.5689- (with-safe-io-syntax ()
1.5690- (let ((*read-eval* t))
1.5691- (read-file-form file)))
1.5692- (error (c)
1.5693- ;;(delete-file-if-exists file) ;; deleting forces rebuild but prevents debugging
1.5694- (push c file-errors)
1.5695- nil))))))
1.5696- (dolist (error file-errors) (error error))
1.5697- (check-lisp-compile-warnings
1.5698- (or failure-p warnings-p) failure-p context-format context-arguments)))
1.5699-
1.5700- #|
1.5701- Mini-guide to adding support for deferred warnings on an implementation.
1.5702-
1.5703- First, look at what such a warning looks like:
1.5704-
1.5705- (describe
1.5706- (handler-case
1.5707- (and (eval '(lambda () (some-undefined-function))) nil)
1.5708- (t (c) c)))
1.5709-
1.5710- Then you can grep for the condition type in your compiler sources
1.5711- and see how to catch those that have been deferred,
1.5712- and/or read, clear and restore the deferred list.
1.5713-
1.5714- Also look at
1.5715- (macroexpand-1 '(with-compilation-unit () foo))
1.5716- |#
1.5717-
1.5718- (defun call-with-saved-deferred-warnings (thunk warnings-file &key source-namestring)
1.5719- "If WARNINGS-FILE is not nil, record the deferred-warnings around a call to THUNK
1.5720-and save those warnings to the given file for latter use,
1.5721-possibly in a different process. Otherwise just call THUNK."
1.5722- (declare (ignorable source-namestring))
1.5723- (if warnings-file
1.5724- (with-compilation-unit (:override t #+sbcl :source-namestring #+sbcl source-namestring)
1.5725- (unwind-protect
1.5726- (let (#+sbcl (sb-c::*undefined-warnings* nil))
1.5727- (multiple-value-prog1
1.5728- (funcall thunk)
1.5729- (save-deferred-warnings warnings-file)))
1.5730- (reset-deferred-warnings)))
1.5731- (funcall thunk)))
1.5732-
1.5733- (defmacro with-saved-deferred-warnings ((warnings-file &key source-namestring) &body body)
1.5734- "Trivial syntax for CALL-WITH-SAVED-DEFERRED-WARNINGS"
1.5735- `(call-with-saved-deferred-warnings
1.5736- #'(lambda () ,@body) ,warnings-file :source-namestring ,source-namestring)))
1.5737-
1.5738-
1.5739-;;; from ASDF
1.5740-(with-upgradability ()
1.5741- (defun current-lisp-file-pathname ()
1.5742- "Portably return the PATHNAME of the current Lisp source file being compiled or loaded"
1.5743- (or *compile-file-pathname* *load-pathname*))
1.5744-
1.5745- (defun load-pathname ()
1.5746- "Portably return the LOAD-PATHNAME of the current source file or fasl.
1.5747- May return a relative pathname."
1.5748- *load-pathname*) ;; magic no longer needed for GCL.
1.5749-
1.5750- (defun lispize-pathname (input-file)
1.5751- "From a INPUT-FILE pathname, return a corresponding .lisp source pathname"
1.5752- (make-pathname :type "lisp" :defaults input-file))
1.5753-
1.5754- (defun compile-file-type (&rest keys)
1.5755- "pathname TYPE for lisp FASt Loading files"
1.5756- (declare (ignorable keys))
1.5757- #-(or clasp ecl mkcl) (load-time-value (pathname-type (compile-file-pathname "foo.lisp")))
1.5758- #+(or clasp ecl mkcl) (pathname-type (apply 'compile-file-pathname "foo" keys)))
1.5759-
1.5760- (defun call-around-hook (hook function)
1.5761- "Call a HOOK around the execution of FUNCTION"
1.5762- (call-function (or hook 'funcall) function))
1.5763-
1.5764- (defun compile-file-pathname* (input-file &rest keys &key output-file &allow-other-keys)
1.5765- "Variant of COMPILE-FILE-PATHNAME that works well with COMPILE-FILE*"
1.5766- (let* ((keys
1.5767- (remove-plist-keys `(#+(or (and allegro (not (version>= 8 2)))) :external-format
1.5768- ,@(unless output-file '(:output-file))) keys)))
1.5769- (if (absolute-pathname-p output-file)
1.5770- ;; what cfp should be doing, w/ mp* instead of mp
1.5771- (let* ((type (pathname-type (apply 'compile-file-type keys)))
1.5772- (defaults (make-pathname
1.5773- :type type :defaults (merge-pathnames* input-file))))
1.5774- (merge-pathnames* output-file defaults))
1.5775- (funcall *output-translation-function*
1.5776- (apply 'compile-file-pathname input-file keys)))))
1.5777-
1.5778- (defvar *compile-check* nil
1.5779- "A hook for user-defined compile-time invariants")
1.5780-
1.5781- (defun compile-file* (input-file &rest keys
1.5782- &key (compile-check *compile-check*) output-file warnings-file
1.5783- #+clisp lib-file #+(or clasp ecl mkcl) object-file #+sbcl emit-cfasl
1.5784- &allow-other-keys)
1.5785- "This function provides a portable wrapper around COMPILE-FILE.
1.5786-It ensures that the OUTPUT-FILE value is only returned and
1.5787-the file only actually created if the compilation was successful,
1.5788-even though your implementation may not do that. It also checks an optional
1.5789-user-provided consistency function COMPILE-CHECK to determine success;
1.5790-it will call this function if not NIL at the end of the compilation
1.5791-with the arguments sent to COMPILE-FILE*, except with :OUTPUT-FILE TMP-FILE
1.5792-where TMP-FILE is the name of a temporary output-file.
1.5793-It also checks two flags (with legacy british spelling from ASDF1),
1.5794-*COMPILE-FILE-FAILURE-BEHAVIOUR* and *COMPILE-FILE-WARNINGS-BEHAVIOUR*
1.5795-with appropriate implementation-dependent defaults,
1.5796-and if a failure (respectively warnings) are reported by COMPILE-FILE,
1.5797-it will consider that an error unless the respective behaviour flag
1.5798-is one of :SUCCESS :WARN :IGNORE.
1.5799-If WARNINGS-FILE is defined, deferred warnings are saved to that file.
1.5800-On ECL or MKCL, it creates both the linkable object and loadable fasl files.
1.5801-On implementations that erroneously do not recognize standard keyword arguments,
1.5802-it will filter them appropriately."
1.5803- #+(or clasp ecl)
1.5804- (when (and object-file (equal (compile-file-type) (pathname object-file)))
1.5805- (format t "Whoa, some funky ASDF upgrade switched ~S calling convention for ~S and ~S~%"
1.5806- 'compile-file* output-file object-file)
1.5807- (rotatef output-file object-file))
1.5808- (let* ((keywords (remove-plist-keys
1.5809- `(:output-file :compile-check :warnings-file
1.5810- #+clisp :lib-file #+(or clasp ecl mkcl) :object-file) keys))
1.5811- (output-file
1.5812- (or output-file
1.5813- (apply 'compile-file-pathname* input-file :output-file output-file keywords)))
1.5814- (physical-output-file (physicalize-pathname output-file))
1.5815- #+(or clasp ecl)
1.5816- (object-file
1.5817- (unless (use-ecl-byte-compiler-p)
1.5818- (or object-file
1.5819- #+ecl (compile-file-pathname output-file :type :object)
1.5820- #+clasp (compile-file-pathname output-file :output-type :object))))
1.5821- #+mkcl
1.5822- (object-file
1.5823- (or object-file
1.5824- (compile-file-pathname output-file :fasl-p nil)))
1.5825- (tmp-file (tmpize-pathname physical-output-file))
1.5826- #+clasp
1.5827- (tmp-object-file (compile-file-pathname tmp-file :output-type :object))
1.5828- #+sbcl
1.5829- (cfasl-file (etypecase emit-cfasl
1.5830- (null nil)
1.5831- ((eql t) (make-pathname :type "cfasl" :defaults physical-output-file))
1.5832- (string (parse-namestring emit-cfasl))
1.5833- (pathname emit-cfasl)))
1.5834- #+sbcl
1.5835- (tmp-cfasl (when cfasl-file (make-pathname :type "cfasl" :defaults tmp-file)))
1.5836- #+clisp
1.5837- (tmp-lib (make-pathname :type "lib" :defaults tmp-file)))
1.5838- (multiple-value-bind (output-truename warnings-p failure-p)
1.5839- (with-enough-pathname (input-file :defaults *base-build-directory*)
1.5840- (with-saved-deferred-warnings (warnings-file :source-namestring (namestring input-file))
1.5841- (with-muffled-compiler-conditions ()
1.5842- (or #-(or clasp ecl mkcl)
1.5843- (let (#+genera (si:*common-lisp-syntax-is-ansi-common-lisp* t))
1.5844- (apply 'compile-file input-file :output-file tmp-file
1.5845- #+sbcl (if emit-cfasl (list* :emit-cfasl tmp-cfasl keywords) keywords)
1.5846- #-sbcl keywords))
1.5847- #+ecl (apply 'compile-file input-file :output-file
1.5848- (if object-file
1.5849- (list* object-file :system-p t keywords)
1.5850- (list* tmp-file keywords)))
1.5851- #+clasp (apply 'compile-file input-file :output-file
1.5852- (if object-file
1.5853- (list* tmp-object-file :output-type :object #|:system-p t|# keywords)
1.5854- (list* tmp-file keywords)))
1.5855- #+mkcl (apply 'compile-file input-file
1.5856- :output-file object-file :fasl-p nil keywords)))))
1.5857- (cond
1.5858- ((and output-truename
1.5859- (flet ((check-flag (flag behaviour)
1.5860- (or (not flag) (member behaviour '(:success :warn :ignore)))))
1.5861- (and (check-flag failure-p *compile-file-failure-behaviour*)
1.5862- (check-flag warnings-p *compile-file-warnings-behaviour*)))
1.5863- (progn
1.5864- #+(or clasp ecl mkcl)
1.5865- (when (and #+(or clasp ecl) object-file)
1.5866- (setf output-truename
1.5867- (compiler::build-fasl tmp-file
1.5868- #+(or clasp ecl) :lisp-files #+mkcl :lisp-object-files (list #+clasp tmp-object-file #-clasp object-file))))
1.5869- (or (not compile-check)
1.5870- (apply compile-check input-file
1.5871- :output-file output-truename
1.5872- keywords))))
1.5873- (delete-file-if-exists physical-output-file)
1.5874- (when output-truename
1.5875- ;; see CLISP bug 677
1.5876- #+clisp
1.5877- (progn
1.5878- (setf tmp-lib (make-pathname :type "lib" :defaults output-truename))
1.5879- (unless lib-file (setf lib-file (make-pathname :type "lib" :defaults physical-output-file)))
1.5880- (rename-file-overwriting-target tmp-lib lib-file))
1.5881- #+sbcl (when cfasl-file (rename-file-overwriting-target tmp-cfasl cfasl-file))
1.5882- #+clasp
1.5883- (progn
1.5884- ;;; the following 4 rename-file-overwriting-target better be atomic, but we can't implement this right now
1.5885- #+:target-os-darwin
1.5886- (let ((temp-dwarf (pathname (strcat (namestring output-truename) ".dwarf")))
1.5887- (target-dwarf (pathname (strcat (namestring physical-output-file) ".dwarf"))))
1.5888- (when (probe-file temp-dwarf)
1.5889- (rename-file-overwriting-target temp-dwarf target-dwarf)))
1.5890- ;;; need to rename the bc or ll file as well or test-bundle.script fails
1.5891- ;;; They might not exist with parallel compilation
1.5892- (let ((bitcode-src (compile-file-pathname tmp-file :output-type :bitcode))
1.5893- (bitcode-target (compile-file-pathname physical-output-file :output-type :bitcode)))
1.5894- (when (probe-file bitcode-src)
1.5895- (rename-file-overwriting-target bitcode-src bitcode-target)))
1.5896- (rename-file-overwriting-target tmp-object-file object-file))
1.5897- (rename-file-overwriting-target output-truename physical-output-file)
1.5898- (setf output-truename (truename physical-output-file)))
1.5899- #+clasp (delete-file-if-exists tmp-file)
1.5900- #+clisp (progn (delete-file-if-exists tmp-file) ;; this one works around clisp BUG 677
1.5901- (delete-file-if-exists tmp-lib))) ;; this one is "normal" defensive cleanup
1.5902- (t ;; error or failed check
1.5903- (delete-file-if-exists output-truename)
1.5904- #+clisp (delete-file-if-exists tmp-lib)
1.5905- #+sbcl (delete-file-if-exists tmp-cfasl)
1.5906- (setf output-truename nil)))
1.5907- (values output-truename warnings-p failure-p))))
1.5908-
1.5909- (defun load* (x &rest keys &key &allow-other-keys)
1.5910- "Portable wrapper around LOAD that properly handles loading from a stream."
1.5911- (with-muffled-loader-conditions ()
1.5912- (let (#+genera (si:*common-lisp-syntax-is-ansi-common-lisp* t))
1.5913- (etypecase x
1.5914- ((or pathname string #-(or allegro clozure genera) stream #+clozure file-stream)
1.5915- (apply 'load x keys))
1.5916- ;; Genera can't load from a string-input-stream
1.5917- ;; ClozureCL 1.6 can only load from file input stream
1.5918- ;; Allegro 5, I don't remember but it must have been broken when I tested.
1.5919- #+(or allegro clozure genera)
1.5920- (stream ;; make do this way
1.5921- (let ((*package* *package*)
1.5922- (*readtable* *readtable*)
1.5923- (*load-pathname* nil)
1.5924- (*load-truename* nil))
1.5925- (eval-input x)))))))
1.5926-
1.5927- (defun load-from-string (string)
1.5928- "Portably read and evaluate forms from a STRING."
1.5929- (with-input-from-string (s string) (load* s))))
1.5930-
1.5931-;;; Links FASLs together
1.5932-(with-upgradability ()
1.5933- (defun combine-fasls (inputs output)
1.5934- "Combine a list of FASLs INPUTS into a single FASL OUTPUT"
1.5935- #-(or abcl allegro clisp clozure cmucl lispworks sbcl scl xcl)
1.5936- (not-implemented-error 'combine-fasls "~%inputs: ~S~%output: ~S" inputs output)
1.5937- #+abcl (funcall 'sys::concatenate-fasls inputs output) ; requires ABCL 1.2.0
1.5938- #+(or allegro clisp cmucl sbcl scl xcl) (concatenate-files inputs output)
1.5939- #+clozure (ccl:fasl-concatenate output inputs :if-exists :supersede)
1.5940- #+lispworks
1.5941- (let (fasls)
1.5942- (unwind-protect
1.5943- (progn
1.5944- (loop :for i :in inputs
1.5945- :for n :from 1
1.5946- :for f = (add-pathname-suffix
1.5947- output (format nil "-FASL~D" n))
1.5948- :do (copy-file i f)
1.5949- (push f fasls))
1.5950- (ignore-errors (lispworks:delete-system :fasls-to-concatenate))
1.5951- (eval `(scm:defsystem :fasls-to-concatenate
1.5952- (:default-pathname ,(pathname-directory-pathname output))
1.5953- :members
1.5954- ,(loop :for f :in (reverse fasls)
1.5955- :collect `(,(namestring f) :load-only t))))
1.5956- (scm:concatenate-system output :fasls-to-concatenate :force t))
1.5957- (loop :for f :in fasls :do (ignore-errors (delete-file f)))
1.5958- (ignore-errors (lispworks:delete-system :fasls-to-concatenate))))))
1.5959-;;;; -------------------------------------------------------------------------
1.5960-;;;; launch-program - semi-portably spawn asynchronous subprocesses
1.5961-
1.5962-(uiop/package:define-package :uiop/launch-program
1.5963- (:use :uiop/common-lisp :uiop/package :uiop/utility
1.5964- :uiop/pathname :uiop/os :uiop/filesystem :uiop/stream
1.5965- :uiop/version)
1.5966- (:export
1.5967- ;;; Escaping the command invocation madness
1.5968- #:easy-sh-character-p #:escape-sh-token #:escape-sh-command
1.5969- #:escape-windows-token #:escape-windows-command
1.5970- #:escape-shell-token #:escape-shell-command
1.5971- #:escape-token #:escape-command
1.5972-
1.5973- ;;; launch-program
1.5974- #:launch-program
1.5975- #:close-streams #:process-alive-p #:terminate-process #:wait-process
1.5976- #:process-info
1.5977- #:process-info-error-output #:process-info-input #:process-info-output #:process-info-pid))
1.5978-(in-package :uiop/launch-program)
1.5979-
1.5980-;;;; ----- Escaping strings for the shell -----
1.5981-(with-upgradability ()
1.5982- (defun requires-escaping-p (token &key good-chars bad-chars)
1.5983- "Does this token require escaping, given the specification of
1.5984-either good chars that don't need escaping or bad chars that do need escaping,
1.5985-as either a recognizing function or a sequence of characters."
1.5986- (some
1.5987- (cond
1.5988- ((and good-chars bad-chars)
1.5989- (parameter-error "~S: only one of good-chars and bad-chars can be provided"
1.5990- 'requires-escaping-p))
1.5991- ((typep good-chars 'function)
1.5992- (complement good-chars))
1.5993- ((typep bad-chars 'function)
1.5994- bad-chars)
1.5995- ((and good-chars (typep good-chars 'sequence))
1.5996- #'(lambda (c) (not (find c good-chars))))
1.5997- ((and bad-chars (typep bad-chars 'sequence))
1.5998- #'(lambda (c) (find c bad-chars)))
1.5999- (t (parameter-error "~S: no good-char criterion" 'requires-escaping-p)))
1.6000- token))
1.6001-
1.6002- (defun escape-token (token &key stream quote good-chars bad-chars escaper)
1.6003- "Call the ESCAPER function on TOKEN string if it needs escaping as per
1.6004-REQUIRES-ESCAPING-P using GOOD-CHARS and BAD-CHARS, otherwise output TOKEN,
1.6005-using STREAM as output (or returning result as a string if NIL)"
1.6006- (if (requires-escaping-p token :good-chars good-chars :bad-chars bad-chars)
1.6007- (with-output (stream)
1.6008- (apply escaper token stream (when quote `(:quote ,quote))))
1.6009- (output-string token stream)))
1.6010-
1.6011- (defun escape-windows-token-within-double-quotes (x &optional s)
1.6012- "Escape a string token X within double-quotes
1.6013-for use within a MS Windows command-line, outputing to S."
1.6014- (labels ((issue (c) (princ c s))
1.6015- (issue-backslash (n) (loop :repeat n :do (issue #\\))))
1.6016- (loop
1.6017- :initially (issue #\") :finally (issue #\")
1.6018- :with l = (length x) :with i = 0
1.6019- :for i+1 = (1+ i) :while (< i l) :do
1.6020- (case (char x i)
1.6021- ((#\") (issue-backslash 1) (issue #\") (setf i i+1))
1.6022- ((#\\)
1.6023- (let* ((j (and (< i+1 l) (position-if-not
1.6024- #'(lambda (c) (eql c #\\)) x :start i+1)))
1.6025- (n (- (or j l) i)))
1.6026- (cond
1.6027- ((null j)
1.6028- (issue-backslash (* 2 n)) (setf i l))
1.6029- ((and (< j l) (eql (char x j) #\"))
1.6030- (issue-backslash (1+ (* 2 n))) (issue #\") (setf i (1+ j)))
1.6031- (t
1.6032- (issue-backslash n) (setf i j)))))
1.6033- (otherwise
1.6034- (issue (char x i)) (setf i i+1))))))
1.6035-
1.6036- (defun easy-windows-character-p (x)
1.6037- "Is X an \"easy\" character that does not require quoting by the shell?"
1.6038- (or (alphanumericp x) (find x "+-_.,@:/=")))
1.6039-
1.6040- (defun escape-windows-token (token &optional s)
1.6041- "Escape a string TOKEN within double-quotes if needed
1.6042-for use within a MS Windows command-line, outputing to S."
1.6043- (escape-token token :stream s :good-chars #'easy-windows-character-p :quote nil
1.6044- :escaper 'escape-windows-token-within-double-quotes))
1.6045-
1.6046- (defun escape-sh-token-within-double-quotes (x s &key (quote t))
1.6047- "Escape a string TOKEN within double-quotes
1.6048-for use within a POSIX Bourne shell, outputing to S;
1.6049-omit the outer double-quotes if key argument :QUOTE is NIL"
1.6050- (when quote (princ #\" s))
1.6051- (loop :for c :across x :do
1.6052- (when (find c "$`\\\"") (princ #\\ s))
1.6053- (princ c s))
1.6054- (when quote (princ #\" s)))
1.6055-
1.6056- (defun easy-sh-character-p (x)
1.6057- "Is X an \"easy\" character that does not require quoting by the shell?"
1.6058- (or (alphanumericp x) (find x "+-_.,%@:/=")))
1.6059-
1.6060- (defun escape-sh-token (token &optional s)
1.6061- "Escape a string TOKEN within double-quotes if needed
1.6062-for use within a POSIX Bourne shell, outputing to S."
1.6063- (escape-token token :stream s :quote #\" :good-chars #'easy-sh-character-p
1.6064- :escaper 'escape-sh-token-within-double-quotes))
1.6065-
1.6066- (defun escape-shell-token (token &optional s)
1.6067- "Escape a token for the current operating system shell"
1.6068- (os-cond
1.6069- ((os-unix-p) (escape-sh-token token s))
1.6070- ((os-windows-p) (escape-windows-token token s))))
1.6071-
1.6072- (defun escape-command (command &optional s
1.6073- (escaper 'escape-shell-token))
1.6074- "Given a COMMAND as a list of tokens, return a string of the
1.6075-spaced, escaped tokens, using ESCAPER to escape."
1.6076- (etypecase command
1.6077- (string (output-string command s))
1.6078- (list (with-output (s)
1.6079- (loop :for first = t :then nil :for token :in command :do
1.6080- (unless first (princ #\space s))
1.6081- (funcall escaper token s))))))
1.6082-
1.6083- (defun escape-windows-command (command &optional s)
1.6084- "Escape a list of command-line arguments into a string suitable for parsing
1.6085-by CommandLineToArgv in MS Windows"
1.6086- ;; http://msdn.microsoft.com/en-us/library/bb776391(v=vs.85).aspx
1.6087- ;; http://msdn.microsoft.com/en-us/library/17w5ykft(v=vs.85).aspx
1.6088- (escape-command command s 'escape-windows-token))
1.6089-
1.6090- (defun escape-sh-command (command &optional s)
1.6091- "Escape a list of command-line arguments into a string suitable for parsing
1.6092-by /bin/sh in POSIX"
1.6093- (escape-command command s 'escape-sh-token))
1.6094-
1.6095- (defun escape-shell-command (command &optional stream)
1.6096- "Escape a command for the current operating system's shell"
1.6097- (escape-command command stream 'escape-shell-token)))
1.6098-
1.6099-
1.6100-(with-upgradability ()
1.6101- ;;; Internal helpers for run-program
1.6102- (defun %normalize-io-specifier (specifier &optional role)
1.6103- "Normalizes a portable I/O specifier for LAUNCH-PROGRAM into an implementation-dependent
1.6104-argument to pass to the internal RUN-PROGRAM"
1.6105- (declare (ignorable role))
1.6106- (typecase specifier
1.6107- (null (or #+(or allegro lispworks) (null-device-pathname)))
1.6108- (string (parse-native-namestring specifier))
1.6109- (pathname specifier)
1.6110- (stream specifier)
1.6111- ((eql :stream) :stream)
1.6112- ((eql :interactive)
1.6113- #+(or allegro lispworks) nil
1.6114- #+clisp :terminal
1.6115- #+(or abcl clasp clozure cmucl ecl mkcl sbcl scl) t
1.6116- #-(or abcl clasp clozure cmucl ecl mkcl sbcl scl allegro lispworks clisp)
1.6117- (not-implemented-error :interactive-output
1.6118- "On this lisp implementation, cannot interpret ~a value of ~a"
1.6119- specifier role))
1.6120- ((eql :output)
1.6121- (cond ((eq role :error-output)
1.6122- #+(or abcl allegro clasp clozure cmucl ecl lispworks mkcl sbcl scl)
1.6123- :output
1.6124- #-(or abcl allegro clasp clozure cmucl ecl lispworks mkcl sbcl scl)
1.6125- (not-implemented-error :error-output-redirect
1.6126- "Can't send ~a to ~a on this lisp implementation."
1.6127- role specifier))
1.6128- (t (parameter-error "~S IO specifier invalid for ~S" specifier role))))
1.6129- ((eql t)
1.6130- #+ (or lispworks abcl)
1.6131- (not-implemented-error :interactive-output
1.6132- "On this lisp implementation, cannot interpret ~a value of ~a"
1.6133- specifier role)
1.6134- #- (or lispworks abcl)
1.6135- (cond ((eq role :error-output) *error-output*)
1.6136- ((eq role :output) #+lispworks *terminal-io* #-lispworks *standard-output*)
1.6137- ((eq role :input) *standard-input*)))
1.6138- (otherwise
1.6139- (parameter-error "Incorrect I/O specifier ~S for ~S"
1.6140- specifier role))))
1.6141-
1.6142- (defun %interactivep (input output error-output)
1.6143- (member :interactive (list input output error-output)))
1.6144-
1.6145- (defun %signal-to-exit-code (signum)
1.6146- (+ 128 signum))
1.6147-
1.6148- (defun %code-to-status (exit-code signal-code)
1.6149- (cond ((null exit-code) :running)
1.6150- ((null signal-code) (values :exited exit-code))
1.6151- (t (values :signaled signal-code))))
1.6152-
1.6153- #+mkcl
1.6154- (defun %mkcl-signal-to-number (signal)
1.6155- (require :mk-unix)
1.6156- (symbol-value (find-symbol signal :mk-unix)))
1.6157-
1.6158- (defclass process-info ()
1.6159- (;; The process field is highly platform-, implementation-, and
1.6160- ;; even version-dependent.
1.6161- ;; Prior to LispWorks 7, the only information that
1.6162- ;; `sys:run-shell-command` with `:wait nil` was certain to return
1.6163- ;; is a PID (e.g. when all streams are nil), hence we stored it
1.6164- ;; and used `sys:pid-exit-status` to obtain an exit status
1.6165- ;; later. That is still what we do.
1.6166- ;; From LispWorks 7 on, if `sys:run-shell-command` does not
1.6167- ;; return a proper stream, we are instead given a dummy stream.
1.6168- ;; We can thus always store a stream and use
1.6169- ;; `sys:pipe-exit-status` to obtain an exit status later.
1.6170- ;; The advantage of dealing with streams instead of PID is the
1.6171- ;; availability of functions like `sys:pipe-kill-process`.
1.6172- (process :initform nil)
1.6173- (input-stream :initform nil)
1.6174- (output-stream :initform nil)
1.6175- (bidir-stream :initform nil)
1.6176- (error-output-stream :initform nil)
1.6177- ;; For backward-compatibility, to maintain the property (zerop
1.6178- ;; exit-code) <-> success, an exit in response to a signal is
1.6179- ;; encoded as 128+signum.
1.6180- (exit-code :initform nil)
1.6181- ;; If the platform allows it, distinguish exiting with a code
1.6182- ;; >128 from exiting in response to a signal by setting this code
1.6183- (signal-code :initform nil))
1.6184- (:documentation "This class should be treated as opaque by programmers, except for the
1.6185-exported PROCESS-INFO-* functions. It should never be directly instantiated by
1.6186-MAKE-INSTANCE. Primarily, it is being made available to enable type-checking."))
1.6187-
1.6188-;;;---------------------------------------------------------------------------
1.6189-;;; The following two helper functions take care of handling the IF-EXISTS and
1.6190-;;; IF-DOES-NOT-EXIST arguments for RUN-PROGRAM. In particular, they process the
1.6191-;;; :ERROR, :APPEND, and :SUPERSEDE arguments *here*, allowing the master
1.6192-;;; function to treat input and output files unconditionally for reading and
1.6193-;;; writing.
1.6194-;;;---------------------------------------------------------------------------
1.6195-
1.6196- (defun %handle-if-exists (file if-exists)
1.6197- (when (or (stringp file) (pathnamep file))
1.6198- (ecase if-exists
1.6199- ((:append :supersede :error)
1.6200- (with-open-file (dummy file :direction :output :if-exists if-exists)
1.6201- (declare (ignorable dummy)))))))
1.6202-
1.6203- (defun %handle-if-does-not-exist (file if-does-not-exist)
1.6204- (when (or (stringp file) (pathnamep file))
1.6205- (ecase if-does-not-exist
1.6206- ((:create :error)
1.6207- (with-open-file (dummy file :direction :probe
1.6208- :if-does-not-exist if-does-not-exist)
1.6209- (declare (ignorable dummy)))))))
1.6210-
1.6211- (defun process-info-error-output (process-info)
1.6212- (slot-value process-info 'error-output-stream))
1.6213- (defun process-info-input (process-info)
1.6214- (or (slot-value process-info 'bidir-stream)
1.6215- (slot-value process-info 'input-stream)))
1.6216- (defun process-info-output (process-info)
1.6217- (or (slot-value process-info 'bidir-stream)
1.6218- (slot-value process-info 'output-stream)))
1.6219-
1.6220- (defun process-info-pid (process-info)
1.6221- (let ((process (slot-value process-info 'process)))
1.6222- (declare (ignorable process))
1.6223- #+abcl (symbol-call :sys :process-pid process)
1.6224- #+allegro process
1.6225- #+clasp (if (find-symbol* '#:external-process-pid :ext nil)
1.6226- (symbol-call :ext '#:external-process-pid process)
1.6227- (not-implemented-error 'process-info-pid))
1.6228- #+clozure (ccl:external-process-id process)
1.6229- #+ecl (ext:external-process-pid process)
1.6230- #+(or cmucl scl) (ext:process-pid process)
1.6231- #+lispworks7+ (sys:pipe-pid process)
1.6232- #+(and lispworks (not lispworks7+)) process
1.6233- #+mkcl (mkcl:process-id process)
1.6234- #+sbcl (sb-ext:process-pid process)
1.6235- #-(or abcl allegro clasp clozure cmucl ecl mkcl lispworks sbcl scl)
1.6236- (not-implemented-error 'process-info-pid)))
1.6237-
1.6238- (defun %process-status (process-info)
1.6239- (if-let (exit-code (slot-value process-info 'exit-code))
1.6240- (return-from %process-status
1.6241- (if-let (signal-code (slot-value process-info 'signal-code))
1.6242- (values :signaled signal-code)
1.6243- (values :exited exit-code))))
1.6244- #-(or allegro clasp clozure cmucl ecl lispworks mkcl sbcl scl)
1.6245- (not-implemented-error '%process-status)
1.6246- (if-let (process (slot-value process-info 'process))
1.6247- (multiple-value-bind (status code)
1.6248- (progn
1.6249- #+allegro (multiple-value-bind (exit-code pid signal-code)
1.6250- (sys:reap-os-subprocess :pid process :wait nil)
1.6251- (assert pid)
1.6252- (%code-to-status exit-code signal-code))
1.6253- #+clasp (if (find-symbol* '#:external-process-status :ext nil)
1.6254- (symbol-call :ext '#:external-process-status process)
1.6255- (not-implemented-error '%process-status))
1.6256- #+clozure (ccl:external-process-status process)
1.6257- #+(or cmucl scl) (let ((status (ext:process-status process)))
1.6258- (if (member status '(:exited :signaled))
1.6259- ;; Calling ext:process-exit-code on
1.6260- ;; processes that are still alive
1.6261- ;; yields an undefined result
1.6262- (values status (ext:process-exit-code process))
1.6263- status))
1.6264- #+ecl (ext:external-process-status process)
1.6265- #+lispworks
1.6266- ;; a signal is only returned on LispWorks 7+
1.6267- (multiple-value-bind (exit-code signal-code)
1.6268- (symbol-call :sys
1.6269- #+lispworks7+ :pipe-exit-status
1.6270- #-lispworks7+ :pid-exit-status
1.6271- process :wait nil)
1.6272- (%code-to-status exit-code signal-code))
1.6273- #+mkcl (let ((status (mk-ext:process-status process)))
1.6274- (if (eq status :exited)
1.6275- ;; Only call mk-ext:process-exit-code when
1.6276- ;; necessary since it leads to another waitpid()
1.6277- (let ((code (mk-ext:process-exit-code process)))
1.6278- (if (stringp code)
1.6279- (values :signaled (%mkcl-signal-to-number code))
1.6280- (values :exited code)))
1.6281- status))
1.6282- #+sbcl (let ((status (sb-ext:process-status process)))
1.6283- (if (eq status :running)
1.6284- :running
1.6285- ;; sb-ext:process-exit-code can also be
1.6286- ;; called for stopped processes to determine
1.6287- ;; the signal that stopped them
1.6288- (values status (sb-ext:process-exit-code process)))))
1.6289- (case status
1.6290- (:exited (setf (slot-value process-info 'exit-code) code))
1.6291- (:signaled (let ((%code (%signal-to-exit-code code)))
1.6292- (setf (slot-value process-info 'exit-code) %code
1.6293- (slot-value process-info 'signal-code) code))))
1.6294- (if code
1.6295- (values status code)
1.6296- status))))
1.6297-
1.6298- (defun process-alive-p (process-info)
1.6299- "Check if a process has yet to exit."
1.6300- (unless (slot-value process-info 'exit-code)
1.6301- #+abcl (sys:process-alive-p (slot-value process-info 'process))
1.6302- #+(or cmucl scl) (ext:process-alive-p (slot-value process-info 'process))
1.6303- #+sbcl (sb-ext:process-alive-p (slot-value process-info 'process))
1.6304- #-(or abcl cmucl sbcl scl) (find (%process-status process-info)
1.6305- '(:running :stopped :continued :resumed))))
1.6306-
1.6307- (defun wait-process (process-info)
1.6308- "Wait for the process to terminate, if it is still running.
1.6309-Otherwise, return immediately. An exit code (a number) will be
1.6310-returned, with 0 indicating success, and anything else indicating
1.6311-failure. If the process exits after receiving a signal, the exit code
1.6312-will be the sum of 128 and the (positive) numeric signal code. A second
1.6313-value may be returned in this case: the numeric signal code itself.
1.6314-Any asynchronously spawned process requires this function to be run
1.6315-before it is garbage-collected in order to free up resources that
1.6316-might otherwise be irrevocably lost."
1.6317- (if-let (exit-code (slot-value process-info 'exit-code))
1.6318- (if-let (signal-code (slot-value process-info 'signal-code))
1.6319- (values exit-code signal-code)
1.6320- exit-code)
1.6321- (let ((process (slot-value process-info 'process)))
1.6322- #-(or abcl allegro clasp clozure cmucl ecl lispworks mkcl sbcl scl)
1.6323- (not-implemented-error 'wait-process)
1.6324- (when process
1.6325- ;; 1- wait
1.6326- #+clozure (ccl::external-process-wait process)
1.6327- #+(or cmucl scl) (ext:process-wait process)
1.6328- #+sbcl (sb-ext:process-wait process)
1.6329- ;; 2- extract result
1.6330- (multiple-value-bind (exit-code signal-code)
1.6331- (progn
1.6332- #+abcl (sys:process-wait process)
1.6333- #+allegro (multiple-value-bind (exit-code pid signal)
1.6334- (sys:reap-os-subprocess :pid process :wait t)
1.6335- (assert pid)
1.6336- (values exit-code signal))
1.6337- #+clasp (if (find-symbol* '#:external-process-wait :ext nil)
1.6338- (multiple-value-bind (status code)
1.6339- (symbol-call :ext '#:external-process-wait process t)
1.6340- (if (eq status :signaled)
1.6341- (values nil code)
1.6342- code))
1.6343- (not-implemented-error 'wait-process))
1.6344- #+clozure (multiple-value-bind (status code)
1.6345- (ccl:external-process-status process)
1.6346- (if (eq status :signaled)
1.6347- (values nil code)
1.6348- code))
1.6349- #+(or cmucl scl) (let ((status (ext:process-status process))
1.6350- (code (ext:process-exit-code process)))
1.6351- (if (eq status :signaled)
1.6352- (values nil code)
1.6353- code))
1.6354- #+ecl (multiple-value-bind (status code)
1.6355- (ext:external-process-wait process t)
1.6356- (if (eq status :signaled)
1.6357- (values nil code)
1.6358- code))
1.6359- #+lispworks (symbol-call :sys
1.6360- #+lispworks7+ :pipe-exit-status
1.6361- #-lispworks7+ :pid-exit-status
1.6362- process :wait t)
1.6363- #+mkcl (let ((code (mkcl:join-process process)))
1.6364- (if (stringp code)
1.6365- (values nil (%mkcl-signal-to-number code))
1.6366- code))
1.6367- #+sbcl (let ((status (sb-ext:process-status process))
1.6368- (code (sb-ext:process-exit-code process)))
1.6369- (if (eq status :signaled)
1.6370- (values nil code)
1.6371- code)))
1.6372- (if signal-code
1.6373- (let ((%exit-code (%signal-to-exit-code signal-code)))
1.6374- (setf (slot-value process-info 'exit-code) %exit-code
1.6375- (slot-value process-info 'signal-code) signal-code)
1.6376- (values %exit-code signal-code))
1.6377- (progn (setf (slot-value process-info 'exit-code) exit-code)
1.6378- exit-code)))))))
1.6379-
1.6380- ;; WARNING: For signals other than SIGTERM and SIGKILL this may not
1.6381- ;; do what you expect it to. Sending SIGSTOP to a process spawned
1.6382- ;; via LAUNCH-PROGRAM, e.g., will stop the shell /bin/sh that is used
1.6383- ;; to run the command (via `sh -c command`) but not the actual
1.6384- ;; command.
1.6385- #+os-unix
1.6386- (defun %posix-send-signal (process-info signal)
1.6387- #+allegro (excl.osi:kill (slot-value process-info 'process) signal)
1.6388- #+clozure (ccl:signal-external-process (slot-value process-info 'process)
1.6389- signal :error-if-exited nil)
1.6390- #+(or cmucl scl) (ext:process-kill (slot-value process-info 'process) signal)
1.6391- #+sbcl (sb-ext:process-kill (slot-value process-info 'process) signal)
1.6392- #-(or allegro clozure cmucl sbcl scl)
1.6393- (if-let (pid (process-info-pid process-info))
1.6394- (symbol-call :uiop :run-program
1.6395- (format nil "kill -~a ~a" signal pid) :ignore-error-status t)))
1.6396-
1.6397- ;;; this function never gets called on Windows, but the compiler cannot tell
1.6398- ;;; that. [2016/09/25:rpg]
1.6399- #+os-windows
1.6400- (defun %posix-send-signal (process-info signal)
1.6401- (declare (ignore process-info signal))
1.6402- (values))
1.6403-
1.6404- (defun terminate-process (process-info &key urgent)
1.6405- "Cause the process to exit. To that end, the process may or may
1.6406-not be sent a signal, which it will find harder (or even impossible)
1.6407-to ignore if URGENT is T. On some platforms, it may also be subject to
1.6408-race conditions."
1.6409- (declare (ignorable urgent))
1.6410- #+abcl (sys:process-kill (slot-value process-info 'process))
1.6411- ;; On ECL, this will only work on versions later than 2016-09-06,
1.6412- ;; but we still want to compile on earlier versions, so we use symbol-call
1.6413- #+(or clasp ecl) (symbol-call :ext :terminate-process (slot-value process-info 'process) urgent)
1.6414- #+lispworks7+ (sys:pipe-kill-process (slot-value process-info 'process))
1.6415- #+mkcl (mk-ext:terminate-process (slot-value process-info 'process)
1.6416- :force urgent)
1.6417- #-(or abcl clasp ecl lispworks7+ mkcl)
1.6418- (os-cond
1.6419- ((os-unix-p) (%posix-send-signal process-info (if urgent 9 15)))
1.6420- ((os-windows-p) (if-let (pid (process-info-pid process-info))
1.6421- (symbol-call :uiop :run-program
1.6422- (format nil "taskkill ~:[~;/f ~]/pid ~a" urgent pid)
1.6423- :ignore-error-status t)))
1.6424- (t (not-implemented-error 'terminate-process))))
1.6425-
1.6426- (defun close-streams (process-info)
1.6427- "Close any stream that the process might own. Needs to be run
1.6428-whenever streams were requested by passing :stream to :input, :output,
1.6429-or :error-output."
1.6430- (dolist (stream
1.6431- (cons (slot-value process-info 'error-output-stream)
1.6432- (if-let (bidir-stream (slot-value process-info 'bidir-stream))
1.6433- (list bidir-stream)
1.6434- (list (slot-value process-info 'input-stream)
1.6435- (slot-value process-info 'output-stream)))))
1.6436- (when stream (close stream))))
1.6437-
1.6438- (defun launch-program (command &rest keys
1.6439- &key
1.6440- input (if-input-does-not-exist :error)
1.6441- output (if-output-exists :supersede)
1.6442- error-output (if-error-output-exists :supersede)
1.6443- (element-type #-clozure *default-stream-element-type*
1.6444- #+clozure 'character)
1.6445- (external-format *utf-8-external-format*)
1.6446- directory
1.6447- #+allegro separate-streams
1.6448- &allow-other-keys)
1.6449- "Launch program specified by COMMAND,
1.6450-either a list of strings specifying a program and list of arguments,
1.6451-or a string specifying a shell command (/bin/sh on Unix, CMD.EXE on
1.6452-Windows) _asynchronously_.
1.6453-
1.6454-If OUTPUT is a pathname, a string designating a pathname, or NIL (the
1.6455-default) designating the null device, the file at that path is used as
1.6456-output.
1.6457-If it's :INTERACTIVE, output is inherited from the current process;
1.6458-beware that this may be different from your *STANDARD-OUTPUT*, and
1.6459-under SLIME will be on your *inferior-lisp* buffer. If it's T, output
1.6460-goes to your current *STANDARD-OUTPUT* stream. If it's :STREAM, a new
1.6461-stream will be made available that can be accessed via
1.6462-PROCESS-INFO-OUTPUT and read from. Otherwise, OUTPUT should be a value
1.6463-that the underlying lisp implementation knows how to handle.
1.6464-
1.6465-IF-OUTPUT-EXISTS, which is only meaningful if OUTPUT is a string or a
1.6466-pathname, can take the values :ERROR, :APPEND, and :SUPERSEDE (the
1.6467-default). The meaning of these values and their effect on the case
1.6468-where OUTPUT does not exist, is analogous to the IF-EXISTS parameter
1.6469-to OPEN with :DIRECTION :OUTPUT.
1.6470-
1.6471-ERROR-OUTPUT is similar to OUTPUT. T designates the *ERROR-OUTPUT*,
1.6472-:OUTPUT means redirecting the error output to the output stream,
1.6473-and :STREAM causes a stream to be made available via
1.6474-PROCESS-INFO-ERROR-OUTPUT.
1.6475-
1.6476-IF-ERROR-OUTPUT-EXISTS is similar to IF-OUTPUT-EXIST, except that it
1.6477-affects ERROR-OUTPUT rather than OUTPUT.
1.6478-
1.6479-INPUT is similar to OUTPUT, except that T designates the
1.6480-*STANDARD-INPUT* and a stream requested through the :STREAM keyword
1.6481-would be available through PROCESS-INFO-INPUT.
1.6482-
1.6483-IF-INPUT-DOES-NOT-EXIST, which is only meaningful if INPUT is a string
1.6484-or a pathname, can take the values :CREATE and :ERROR (the
1.6485-default). The meaning of these values is analogous to the
1.6486-IF-DOES-NOT-EXIST parameter to OPEN with :DIRECTION :INPUT.
1.6487-
1.6488-ELEMENT-TYPE and EXTERNAL-FORMAT are passed on to your Lisp
1.6489-implementation, when applicable, for creation of the output stream.
1.6490-
1.6491-LAUNCH-PROGRAM returns a PROCESS-INFO object.
1.6492-
1.6493-LAUNCH-PROGRAM currently does not smooth over all the differences between
1.6494-implementations. Of particular note is when streams are provided for OUTPUT or
1.6495-ERROR-OUTPUT. Some implementations don't support this at all, some support only
1.6496-certain subclasses of streams, and some support any arbitrary
1.6497-stream. Additionally, the implementations that support streams may have
1.6498-differing behavior on how those streams are filled with data. If data is not
1.6499-periodically read from the child process and sent to the stream, the child
1.6500-could block because its output buffers are full."
1.6501- #-(or abcl allegro clasp clozure cmucl ecl (and lispworks os-unix) mkcl sbcl scl)
1.6502- (progn command keys input output error-output directory element-type external-format
1.6503- if-input-does-not-exist if-output-exists if-error-output-exists ;; ignore
1.6504- (not-implemented-error 'launch-program))
1.6505- #+allegro
1.6506- (when (some #'(lambda (stream)
1.6507- (and (streamp stream)
1.6508- (not (file-stream-p stream))))
1.6509- (list input output error-output))
1.6510- (parameter-error "~S: Streams passed as I/O parameters need to be file streams on this lisp"
1.6511- 'launch-program))
1.6512- #+(or abcl clisp lispworks)
1.6513- (when (some #'streamp (list input output error-output))
1.6514- (parameter-error "~S: I/O parameters cannot be foreign streams on this lisp"
1.6515- 'launch-program))
1.6516- #+clisp
1.6517- (unless (eq error-output :interactive)
1.6518- (parameter-error "~S: The only admissible value for ~S is ~S on this lisp"
1.6519- 'launch-program :error-output :interactive))
1.6520- #+(or clasp ecl)
1.6521- (when (and #+ecl (version< (lisp-implementation-version) "20.4.24")
1.6522- (some #'(lambda (stream)
1.6523- (and (streamp stream)
1.6524- (not (file-or-synonym-stream-p stream))))
1.6525- (list input output error-output)))
1.6526- (parameter-error "~S: Streams passed as I/O parameters need to be (synonymous with) file streams on this lisp"
1.6527- 'launch-program))
1.6528- #+(or abcl allegro clasp clozure cmucl ecl (and lispworks os-unix) mkcl sbcl scl)
1.6529- (nest
1.6530- (progn ;; see comments for these functions
1.6531- (%handle-if-does-not-exist input if-input-does-not-exist)
1.6532- (%handle-if-exists output if-output-exists)
1.6533- (%handle-if-exists error-output if-error-output-exists))
1.6534- #+(or clasp ecl) (let ((*standard-input* *stdin*)
1.6535- (*standard-output* *stdout*)
1.6536- (*error-output* *stderr*)))
1.6537- (let ((process-info (make-instance 'process-info))
1.6538- (input (%normalize-io-specifier input :input))
1.6539- (output (%normalize-io-specifier output :output))
1.6540- (error-output (%normalize-io-specifier error-output :error-output))
1.6541- #+(and allegro os-windows) (interactive (%interactivep input output error-output))
1.6542- (command
1.6543- (etypecase command
1.6544- #+os-unix (string `("/bin/sh" "-c" ,command))
1.6545- #+os-unix (list command)
1.6546- #+os-windows
1.6547- (string
1.6548- ;; NB: On other Windows implementations, this is utterly bogus
1.6549- ;; except in the most trivial cases where no quoting is needed.
1.6550- ;; Use at your own risk.
1.6551- #-(or allegro clasp clisp clozure ecl)
1.6552- (nest
1.6553- #+(or clasp ecl sbcl) (unless (find-symbol* :escape-arguments #+(or clasp ecl) :ext #+sbcl :sb-impl nil))
1.6554- (parameter-error "~S doesn't support string commands on Windows on this Lisp"
1.6555- 'launch-program command))
1.6556- ;; NB: We add cmd /c here. Behavior without going through cmd is not well specified
1.6557- ;; when the command contains spaces or special characters:
1.6558- ;; IIUC, the system will use space as a separator,
1.6559- ;; but the C++ argv-decoding libraries won't, and
1.6560- ;; you're supposed to use an extra argument to CreateProcess to bridge the gap,
1.6561- ;; yet neither allegro nor clisp provide access to that argument.
1.6562- #+(or allegro clisp) (strcat "cmd /c " command)
1.6563- ;; On ClozureCL for Windows, we assume you are using
1.6564- ;; r15398 or later in 1.9 or later,
1.6565- ;; so that bug 858 is fixed http://trac.clozure.com/ccl/ticket/858
1.6566- ;; On ECL, commit 2040629 https://gitlab.com/embeddable-common-lisp/ecl/issues/304
1.6567- ;; On SBCL, we assume the patch from fcae0fd (to be part of SBCL 1.3.13)
1.6568- #+(or clasp clozure ecl sbcl) (cons "cmd" (strcat "/c " command)))
1.6569- #+os-windows
1.6570- (list
1.6571- #+allegro (escape-windows-command command)
1.6572- #-allegro command)))))
1.6573- #+(or abcl (and allegro os-unix) clasp clozure cmucl ecl mkcl sbcl)
1.6574- (let ((program (car command))
1.6575- #-allegro (arguments (cdr command))))
1.6576- #+(and (or clasp ecl sbcl) os-windows)
1.6577- (multiple-value-bind (arguments escape-arguments)
1.6578- (if (listp arguments)
1.6579- (values arguments t)
1.6580- (values (list arguments) nil)))
1.6581- #-(or allegro mkcl sbcl) (with-current-directory (directory))
1.6582- (multiple-value-bind
1.6583- #+(or abcl clozure cmucl sbcl scl) (process)
1.6584- #+allegro (in-or-io out-or-err err-or-pid pid-or-nil)
1.6585- #+(or clasp ecl) (stream code process)
1.6586- #+lispworks (io-or-pid err-or-nil #-lispworks7+ pid-or-nil)
1.6587- #+mkcl (stream process code)
1.6588- #.`(apply
1.6589- #+abcl 'sys:run-program
1.6590- #+allegro ,@'('excl:run-shell-command
1.6591- #+os-unix (coerce (cons program command) 'vector)
1.6592- #+os-windows command)
1.6593- #+clasp (if (find-symbol* '#:run-program :ext nil)
1.6594- (find-symbol* '#:run-program :ext nil)
1.6595- (not-implemented-error 'launch-program))
1.6596- #+clozure 'ccl:run-program
1.6597- #+(or cmucl ecl scl) 'ext:run-program
1.6598-
1.6599- #+lispworks ,@'('system:run-shell-command `("/usr/bin/env" ,@command)) ; full path needed
1.6600- #+mkcl 'mk-ext:run-program
1.6601- #+sbcl 'sb-ext:run-program
1.6602- #+(or abcl clasp clozure cmucl ecl mkcl sbcl) ,@'(program arguments)
1.6603- #+(and (or clasp ecl sbcl) os-windows) ,@'(:escape-arguments escape-arguments)
1.6604- :input input :if-input-does-not-exist :error
1.6605- :output output :if-output-exists :append
1.6606- ,(or #+(or allegro lispworks) :error-output :error) error-output
1.6607- ,(or #+(or allegro lispworks) :if-error-output-exists :if-error-exists) :append
1.6608- :wait nil :element-type element-type :external-format external-format
1.6609- :allow-other-keys t
1.6610- #+allegro ,@`(:directory directory
1.6611- #+os-windows ,@'(:show-window (if interactive nil :hide)))
1.6612- #+lispworks ,@'(:save-exit-status t)
1.6613- #+mkcl ,@'(:directory (native-namestring directory))
1.6614- #-sbcl keys ;; on SBCL, don't pass :directory nil but remove it from the keys
1.6615- #+sbcl ,@'(:search t (if directory keys (remove-plist-key :directory keys)))))
1.6616- (labels ((prop (key value) (setf (slot-value process-info key) value)))
1.6617- #+allegro
1.6618- (cond
1.6619- (separate-streams
1.6620- (prop 'process pid-or-nil)
1.6621- (when (eq input :stream) (prop 'input-stream in-or-io))
1.6622- (when (eq output :stream) (prop 'output-stream out-or-err))
1.6623- (when (eq error-output :stream) (prop 'error-output-stream err-or-pid)))
1.6624- (t
1.6625- (prop 'process err-or-pid)
1.6626- (ecase (+ (if (eq input :stream) 1 0) (if (eq output :stream) 2 0))
1.6627- (0)
1.6628- (1 (prop 'input-stream in-or-io))
1.6629- (2 (prop 'output-stream in-or-io))
1.6630- (3 (prop 'bidir-stream in-or-io)))
1.6631- (when (eq error-output :stream)
1.6632- (prop 'error-output-stream out-or-err))))
1.6633- #+(or abcl clozure cmucl sbcl scl)
1.6634- (progn
1.6635- (prop 'process process)
1.6636- (when (eq input :stream)
1.6637- (nest
1.6638- (prop 'input-stream)
1.6639- #+abcl (symbol-call :sys :process-input)
1.6640- #+clozure (ccl:external-process-input-stream)
1.6641- #+(or cmucl scl) (ext:process-input)
1.6642- #+sbcl (sb-ext:process-input)
1.6643- process))
1.6644- (when (eq output :stream)
1.6645- (nest
1.6646- (prop 'output-stream)
1.6647- #+abcl (symbol-call :sys :process-output)
1.6648- #+clozure (ccl:external-process-output-stream)
1.6649- #+(or cmucl scl) (ext:process-output)
1.6650- #+sbcl (sb-ext:process-output)
1.6651- process))
1.6652- (when (eq error-output :stream)
1.6653- (nest
1.6654- (prop 'error-output-stream)
1.6655- #+abcl (symbol-call :sys :process-error)
1.6656- #+clozure (ccl:external-process-error-stream)
1.6657- #+(or cmucl scl) (ext:process-error)
1.6658- #+sbcl (sb-ext:process-error)
1.6659- process)))
1.6660- #+(or clasp ecl mkcl)
1.6661- (let ((mode (+ (if (eq input :stream) 1 0) (if (eq output :stream) 2 0))))
1.6662- code ;; ignore
1.6663- (unless (zerop mode)
1.6664- (prop (case mode (1 'input-stream) (2 'output-stream) (3 'bidir-stream)) stream))
1.6665- (when (eq error-output :stream)
1.6666- (prop 'error-output-stream
1.6667- (if (and #+clasp nil #-clasp t (version< (lisp-implementation-version) "16.0.0"))
1.6668- (symbol-call :ext :external-process-error process)
1.6669- (symbol-call :ext :external-process-error-stream process))))
1.6670- (prop 'process process))
1.6671- #+lispworks
1.6672- ;; See also the comments on the process-info class
1.6673- (let ((mode (+ (if (eq input :stream) 1 0) (if (eq output :stream) 2 0))))
1.6674- (cond
1.6675- ((or (plusp mode) (eq error-output :stream))
1.6676- (prop 'process #+lispworks7+ io-or-pid #-lispworks7+ pid-or-nil)
1.6677- (when (plusp mode)
1.6678- (prop (ecase mode (1 'input-stream) (2 'output-stream) (3 'bidir-stream))
1.6679- io-or-pid))
1.6680- (when (eq error-output :stream)
1.6681- (prop 'error-output-stream err-or-nil)))
1.6682- ;; Prior to Lispworks 7, this returned (pid); now it
1.6683- ;; returns (io err pid) of which we keep io.
1.6684- (t (prop 'process io-or-pid)))))
1.6685- process-info)))
1.6686-
1.6687-;;;; -------------------------------------------------------------------------
1.6688-;;;; run-program initially from xcvb-driver.
1.6689-
1.6690-(uiop/package:define-package :uiop/run-program
1.6691- (:nicknames :asdf/run-program) ; OBSOLETE. Used by cl-sane, printv.
1.6692- (:use :uiop/common-lisp :uiop/package :uiop/utility :uiop/version
1.6693- :uiop/pathname :uiop/os :uiop/filesystem :uiop/stream :uiop/launch-program)
1.6694- (:export
1.6695- #:run-program
1.6696- #:slurp-input-stream #:vomit-output-stream
1.6697- #:subprocess-error
1.6698- #:subprocess-error-code #:subprocess-error-command #:subprocess-error-process)
1.6699- (:import-from :uiop/launch-program
1.6700- #:%handle-if-does-not-exist #:%handle-if-exists #:%interactivep
1.6701- #:input-stream #:output-stream #:error-output-stream))
1.6702-(in-package :uiop/run-program)
1.6703-
1.6704-;;;; Slurping a stream, typically the output of another program
1.6705-(with-upgradability ()
1.6706- (defun call-stream-processor (fun processor stream)
1.6707- "Given FUN (typically SLURP-INPUT-STREAM or VOMIT-OUTPUT-STREAM,
1.6708-a PROCESSOR specification which is either an atom or a list specifying
1.6709-a processor an keyword arguments, call the specified processor with
1.6710-the given STREAM as input"
1.6711- (if (consp processor)
1.6712- (apply fun (first processor) stream (rest processor))
1.6713- (funcall fun processor stream)))
1.6714-
1.6715- (defgeneric slurp-input-stream (processor input-stream &key)
1.6716- (:documentation
1.6717- "SLURP-INPUT-STREAM is a generic function with two positional arguments
1.6718-PROCESSOR and INPUT-STREAM and additional keyword arguments, that consumes (slurps)
1.6719-the contents of the INPUT-STREAM and processes them according to a method
1.6720-specified by PROCESSOR.
1.6721-
1.6722-Built-in methods include the following:
1.6723-* if PROCESSOR is a function, it is called with the INPUT-STREAM as its argument
1.6724-* if PROCESSOR is a list, its first element should be a function. It will be applied to a cons of the
1.6725- INPUT-STREAM and the rest of the list. That is (x . y) will be treated as
1.6726- \(APPLY x <stream> y\)
1.6727-* if PROCESSOR is an output-stream, the contents of INPUT-STREAM is copied to the output-stream,
1.6728- per copy-stream-to-stream, with appropriate keyword arguments.
1.6729-* if PROCESSOR is the symbol CL:STRING or the keyword :STRING, then the contents of INPUT-STREAM
1.6730- are returned as a string, as per SLURP-STREAM-STRING.
1.6731-* if PROCESSOR is the keyword :LINES then the INPUT-STREAM will be handled by SLURP-STREAM-LINES.
1.6732-* if PROCESSOR is the keyword :LINE then the INPUT-STREAM will be handled by SLURP-STREAM-LINE.
1.6733-* if PROCESSOR is the keyword :FORMS then the INPUT-STREAM will be handled by SLURP-STREAM-FORMS.
1.6734-* if PROCESSOR is the keyword :FORM then the INPUT-STREAM will be handled by SLURP-STREAM-FORM.
1.6735-* if PROCESSOR is T, it is treated the same as *standard-output*. If it is NIL, NIL is returned.
1.6736-
1.6737-Programmers are encouraged to define their own methods for this generic function."))
1.6738-
1.6739- #-genera
1.6740- (defmethod slurp-input-stream ((function function) input-stream &key)
1.6741- (funcall function input-stream))
1.6742-
1.6743- (defmethod slurp-input-stream ((list cons) input-stream &key)
1.6744- (apply (first list) input-stream (rest list)))
1.6745-
1.6746- #-genera
1.6747- (defmethod slurp-input-stream ((output-stream stream) input-stream
1.6748- &key linewise prefix (element-type 'character) buffer-size)
1.6749- (copy-stream-to-stream
1.6750- input-stream output-stream
1.6751- :linewise linewise :prefix prefix :element-type element-type :buffer-size buffer-size))
1.6752-
1.6753- (defmethod slurp-input-stream ((x (eql 'string)) stream &key stripped)
1.6754- (slurp-stream-string stream :stripped stripped))
1.6755-
1.6756- (defmethod slurp-input-stream ((x (eql :string)) stream &key stripped)
1.6757- (slurp-stream-string stream :stripped stripped))
1.6758-
1.6759- (defmethod slurp-input-stream ((x (eql :lines)) stream &key count)
1.6760- (slurp-stream-lines stream :count count))
1.6761-
1.6762- (defmethod slurp-input-stream ((x (eql :line)) stream &key (at 0))
1.6763- (slurp-stream-line stream :at at))
1.6764-
1.6765- (defmethod slurp-input-stream ((x (eql :forms)) stream &key count)
1.6766- (slurp-stream-forms stream :count count))
1.6767-
1.6768- (defmethod slurp-input-stream ((x (eql :form)) stream &key (at 0))
1.6769- (slurp-stream-form stream :at at))
1.6770-
1.6771- (defmethod slurp-input-stream ((x (eql t)) stream &rest keys &key &allow-other-keys)
1.6772- (apply 'slurp-input-stream *standard-output* stream keys))
1.6773-
1.6774- (defmethod slurp-input-stream ((x null) (stream t) &key)
1.6775- nil)
1.6776-
1.6777- (defmethod slurp-input-stream ((pathname pathname) input
1.6778- &key
1.6779- (element-type *default-stream-element-type*)
1.6780- (external-format *utf-8-external-format*)
1.6781- (if-exists :rename-and-delete)
1.6782- (if-does-not-exist :create)
1.6783- buffer-size
1.6784- linewise)
1.6785- (with-output-file (output pathname
1.6786- :element-type element-type
1.6787- :external-format external-format
1.6788- :if-exists if-exists
1.6789- :if-does-not-exist if-does-not-exist)
1.6790- (copy-stream-to-stream
1.6791- input output
1.6792- :element-type element-type :buffer-size buffer-size :linewise linewise)))
1.6793-
1.6794- (defmethod slurp-input-stream (x stream
1.6795- &key linewise prefix (element-type 'character) buffer-size)
1.6796- (declare (ignorable stream linewise prefix element-type buffer-size))
1.6797- (cond
1.6798- #+genera
1.6799- ((functionp x) (funcall x stream))
1.6800- #+genera
1.6801- ((output-stream-p x)
1.6802- (copy-stream-to-stream
1.6803- stream x
1.6804- :linewise linewise :prefix prefix :element-type element-type :buffer-size buffer-size))
1.6805- (t
1.6806- (parameter-error "Invalid ~S destination ~S" 'slurp-input-stream x)))))
1.6807-
1.6808-;;;; Vomiting a stream, typically into the input of another program.
1.6809-(with-upgradability ()
1.6810- (defgeneric vomit-output-stream (processor output-stream &key)
1.6811- (:documentation
1.6812- "VOMIT-OUTPUT-STREAM is a generic function with two positional arguments
1.6813-PROCESSOR and OUTPUT-STREAM and additional keyword arguments, that produces (vomits)
1.6814-some content onto the OUTPUT-STREAM, according to a method specified by PROCESSOR.
1.6815-
1.6816-Built-in methods include the following:
1.6817-* if PROCESSOR is a function, it is called with the OUTPUT-STREAM as its argument
1.6818-* if PROCESSOR is a list, its first element should be a function.
1.6819- It will be applied to a cons of the OUTPUT-STREAM and the rest of the list.
1.6820- That is (x . y) will be treated as \(APPLY x <stream> y\)
1.6821-* if PROCESSOR is an input-stream, its contents will be copied the OUTPUT-STREAM,
1.6822- per copy-stream-to-stream, with appropriate keyword arguments.
1.6823-* if PROCESSOR is a string, its contents will be printed to the OUTPUT-STREAM.
1.6824-* if PROCESSOR is T, it is treated the same as *standard-input*. If it is NIL, nothing is done.
1.6825-
1.6826-Programmers are encouraged to define their own methods for this generic function."))
1.6827-
1.6828- #-genera
1.6829- (defmethod vomit-output-stream ((function function) output-stream &key)
1.6830- (funcall function output-stream))
1.6831-
1.6832- (defmethod vomit-output-stream ((list cons) output-stream &key)
1.6833- (apply (first list) output-stream (rest list)))
1.6834-
1.6835- #-genera
1.6836- (defmethod vomit-output-stream ((input-stream stream) output-stream
1.6837- &key linewise prefix (element-type 'character) buffer-size)
1.6838- (copy-stream-to-stream
1.6839- input-stream output-stream
1.6840- :linewise linewise :prefix prefix :element-type element-type :buffer-size buffer-size))
1.6841-
1.6842- (defmethod vomit-output-stream ((x string) stream &key fresh-line terpri)
1.6843- (princ x stream)
1.6844- (when fresh-line (fresh-line stream))
1.6845- (when terpri (terpri stream))
1.6846- (values))
1.6847-
1.6848- (defmethod vomit-output-stream ((x (eql t)) stream &rest keys &key &allow-other-keys)
1.6849- (apply 'vomit-output-stream *standard-input* stream keys))
1.6850-
1.6851- (defmethod vomit-output-stream ((x null) (stream t) &key)
1.6852- (values))
1.6853-
1.6854- (defmethod vomit-output-stream ((pathname pathname) input
1.6855- &key
1.6856- (element-type *default-stream-element-type*)
1.6857- (external-format *utf-8-external-format*)
1.6858- (if-exists :rename-and-delete)
1.6859- (if-does-not-exist :create)
1.6860- buffer-size
1.6861- linewise)
1.6862- (with-output-file (output pathname
1.6863- :element-type element-type
1.6864- :external-format external-format
1.6865- :if-exists if-exists
1.6866- :if-does-not-exist if-does-not-exist)
1.6867- (copy-stream-to-stream
1.6868- input output
1.6869- :element-type element-type :buffer-size buffer-size :linewise linewise)))
1.6870-
1.6871- (defmethod vomit-output-stream (x stream
1.6872- &key linewise prefix (element-type 'character) buffer-size)
1.6873- (declare (ignorable stream linewise prefix element-type buffer-size))
1.6874- (cond
1.6875- #+genera
1.6876- ((functionp x) (funcall x stream))
1.6877- #+genera
1.6878- ((input-stream-p x)
1.6879- (copy-stream-to-stream
1.6880- x stream
1.6881- :linewise linewise :prefix prefix :element-type element-type :buffer-size buffer-size))
1.6882- (t
1.6883- (parameter-error "Invalid ~S source ~S" 'vomit-output-stream x)))))
1.6884-
1.6885-
1.6886-;;;; Run-program: synchronously run a program in a subprocess, handling input, output and error-output.
1.6887-(with-upgradability ()
1.6888- (define-condition subprocess-error (error)
1.6889- ((code :initform nil :initarg :code :reader subprocess-error-code)
1.6890- (command :initform nil :initarg :command :reader subprocess-error-command)
1.6891- (process :initform nil :initarg :process :reader subprocess-error-process))
1.6892- (:report (lambda (condition stream)
1.6893- (format stream "Subprocess ~@[~S~% ~]~@[with command ~S~% ~]exited with error~@[ code ~D~]"
1.6894- (subprocess-error-process condition)
1.6895- (subprocess-error-command condition)
1.6896- (subprocess-error-code condition)))))
1.6897-
1.6898- (defun %check-result (exit-code &key command process ignore-error-status)
1.6899- (unless ignore-error-status
1.6900- (unless (eql exit-code 0)
1.6901- (cerror "IGNORE-ERROR-STATUS"
1.6902- 'subprocess-error :command command :code exit-code :process process)))
1.6903- exit-code)
1.6904-
1.6905- (defun %active-io-specifier-p (specifier)
1.6906- "Determines whether a run-program I/O specifier requires Lisp-side processing
1.6907-via SLURP-INPUT-STREAM or VOMIT-OUTPUT-STREAM (return T),
1.6908-or whether it's already taken care of by the implementation's underlying run-program."
1.6909- (not (typep specifier '(or null string pathname (member :interactive :output)
1.6910- #+(or cmucl (and sbcl os-unix) scl) (or stream (eql t))
1.6911- #+lispworks file-stream))))
1.6912-
1.6913- (defun %run-program (command &rest keys &key &allow-other-keys)
1.6914- "DEPRECATED. Use LAUNCH-PROGRAM instead."
1.6915- (apply 'launch-program command keys))
1.6916-
1.6917- (defun %call-with-program-io (gf tval stream-easy-p fun direction spec activep returner
1.6918- &key
1.6919- (element-type #-clozure *default-stream-element-type* #+clozure 'character)
1.6920- (external-format *utf-8-external-format*) &allow-other-keys)
1.6921- ;; handle redirection for run-program and system
1.6922- ;; SPEC is the specification for the subprocess's input or output or error-output
1.6923- ;; TVAL is the value used if the spec is T
1.6924- ;; GF is the generic function to call to handle arbitrary values of SPEC
1.6925- ;; STREAM-EASY-P is T if we're going to use a RUN-PROGRAM that copies streams in the background
1.6926- ;; (it's only meaningful on CMUCL, SBCL, SCL that actually do it)
1.6927- ;; DIRECTION is :INPUT, :OUTPUT or :ERROR-OUTPUT for the direction of this io argument
1.6928- ;; FUN is a function of the new reduced spec and an activity function to call with a stream
1.6929- ;; when the subprocess is active and communicating through that stream.
1.6930- ;; ACTIVEP is a boolean true if we will get to run code while the process is running
1.6931- ;; ELEMENT-TYPE and EXTERNAL-FORMAT control what kind of temporary file we may open.
1.6932- ;; RETURNER is a function called with the value of the activity.
1.6933- ;; --- TODO (fare@tunes.org): handle if-output-exists and such when doing it the hard way.
1.6934- (declare (ignorable stream-easy-p))
1.6935- (let* ((actual-spec (if (eq spec t) tval spec))
1.6936- (activity-spec (if (eq actual-spec :output)
1.6937- (ecase direction
1.6938- ((:input :output)
1.6939- (parameter-error "~S does not allow ~S as a ~S spec"
1.6940- 'run-program :output direction))
1.6941- ((:error-output)
1.6942- nil))
1.6943- actual-spec)))
1.6944- (labels ((activity (stream)
1.6945- (call-function returner (call-stream-processor gf activity-spec stream)))
1.6946- (easy-case ()
1.6947- (funcall fun actual-spec nil))
1.6948- (hard-case ()
1.6949- (if activep
1.6950- (funcall fun :stream #'activity)
1.6951- (with-temporary-file (:pathname tmp)
1.6952- (ecase direction
1.6953- (:input
1.6954- (with-output-file (s tmp :if-exists :overwrite
1.6955- :external-format external-format
1.6956- :element-type element-type)
1.6957- (activity s))
1.6958- (funcall fun tmp nil))
1.6959- ((:output :error-output)
1.6960- (multiple-value-prog1 (funcall fun tmp nil)
1.6961- (with-input-file (s tmp
1.6962- :external-format external-format
1.6963- :element-type element-type)
1.6964- (activity s)))))))))
1.6965- (typecase activity-spec
1.6966- ((or null string pathname (eql :interactive))
1.6967- (easy-case))
1.6968- #+(or cmucl (and sbcl os-unix) scl) ;; streams are only easy on implementations that try very hard
1.6969- (stream
1.6970- (if stream-easy-p (easy-case) (hard-case)))
1.6971- (t
1.6972- (hard-case))))))
1.6973-
1.6974- (defmacro place-setter (place)
1.6975- (when place
1.6976- (let ((value (gensym)))
1.6977- `#'(lambda (,value) (setf ,place ,value)))))
1.6978-
1.6979- (defmacro with-program-input (((reduced-input-var
1.6980- &optional (input-activity-var (gensym) iavp))
1.6981- input-form &key setf stream-easy-p active keys) &body body)
1.6982- `(apply '%call-with-program-io 'vomit-output-stream *standard-input* ,stream-easy-p
1.6983- #'(lambda (,reduced-input-var ,input-activity-var)
1.6984- ,@(unless iavp `((declare (ignore ,input-activity-var))))
1.6985- ,@body)
1.6986- :input ,input-form ,active (place-setter ,setf) ,keys))
1.6987-
1.6988- (defmacro with-program-output (((reduced-output-var
1.6989- &optional (output-activity-var (gensym) oavp))
1.6990- output-form &key setf stream-easy-p active keys) &body body)
1.6991- `(apply '%call-with-program-io 'slurp-input-stream *standard-output* ,stream-easy-p
1.6992- #'(lambda (,reduced-output-var ,output-activity-var)
1.6993- ,@(unless oavp `((declare (ignore ,output-activity-var))))
1.6994- ,@body)
1.6995- :output ,output-form ,active (place-setter ,setf) ,keys))
1.6996-
1.6997- (defmacro with-program-error-output (((reduced-error-output-var
1.6998- &optional (error-output-activity-var (gensym) eoavp))
1.6999- error-output-form &key setf stream-easy-p active keys)
1.7000- &body body)
1.7001- `(apply '%call-with-program-io 'slurp-input-stream *error-output* ,stream-easy-p
1.7002- #'(lambda (,reduced-error-output-var ,error-output-activity-var)
1.7003- ,@(unless eoavp `((declare (ignore ,error-output-activity-var))))
1.7004- ,@body)
1.7005- :error-output ,error-output-form ,active (place-setter ,setf) ,keys))
1.7006-
1.7007- (defun %use-launch-program (command &rest keys
1.7008- &key input output error-output ignore-error-status &allow-other-keys)
1.7009- ;; helper for RUN-PROGRAM when using LAUNCH-PROGRAM
1.7010- #+(or cormanlisp gcl (and lispworks os-windows) mcl xcl)
1.7011- (progn
1.7012- command keys input output error-output ignore-error-status ;; ignore
1.7013- (not-implemented-error '%use-launch-program))
1.7014- (when (member :stream (list input output error-output))
1.7015- (parameter-error "~S: ~S is not allowed as synchronous I/O redirection argument"
1.7016- 'run-program :stream))
1.7017- (let* ((active-input-p (%active-io-specifier-p input))
1.7018- (active-output-p (%active-io-specifier-p output))
1.7019- (active-error-output-p (%active-io-specifier-p error-output))
1.7020- (activity
1.7021- (cond
1.7022- (active-output-p :output)
1.7023- (active-input-p :input)
1.7024- (active-error-output-p :error-output)
1.7025- (t nil)))
1.7026- output-result error-output-result exit-code process-info)
1.7027- (with-program-output ((reduced-output output-activity)
1.7028- output :keys keys :setf output-result
1.7029- :stream-easy-p t :active (eq activity :output))
1.7030- (with-program-error-output ((reduced-error-output error-output-activity)
1.7031- error-output :keys keys :setf error-output-result
1.7032- :stream-easy-p t :active (eq activity :error-output))
1.7033- (with-program-input ((reduced-input input-activity)
1.7034- input :keys keys
1.7035- :stream-easy-p t :active (eq activity :input))
1.7036- (setf process-info
1.7037- (apply 'launch-program command
1.7038- :input reduced-input :output reduced-output
1.7039- :error-output (if (eq error-output :output) :output reduced-error-output)
1.7040- keys))
1.7041- (labels ((get-stream (stream-name &optional fallbackp)
1.7042- (or (slot-value process-info stream-name)
1.7043- (when fallbackp
1.7044- (slot-value process-info 'bidir-stream))))
1.7045- (run-activity (activity stream-name &optional fallbackp)
1.7046- (if-let (stream (get-stream stream-name fallbackp))
1.7047- (funcall activity stream)
1.7048- (error 'subprocess-error
1.7049- :code `(:missing ,stream-name)
1.7050- :command command :process process-info))))
1.7051- (unwind-protect
1.7052- (ecase activity
1.7053- ((nil))
1.7054- (:input (run-activity input-activity 'input-stream t))
1.7055- (:output (run-activity output-activity 'output-stream t))
1.7056- (:error-output (run-activity error-output-activity 'error-output-stream)))
1.7057- (close-streams process-info)
1.7058- (setf exit-code (wait-process process-info)))))))
1.7059- (%check-result exit-code
1.7060- :command command :process process-info
1.7061- :ignore-error-status ignore-error-status)
1.7062- (values output-result error-output-result exit-code)))
1.7063-
1.7064- (defun %normalize-system-command (command) ;; helper for %USE-SYSTEM
1.7065- (etypecase command
1.7066- (string command)
1.7067- (list (escape-shell-command
1.7068- (os-cond
1.7069- ((os-unix-p) (cons "exec" command))
1.7070- (t command))))))
1.7071-
1.7072- (defun %redirected-system-command (command in out err directory) ;; helper for %USE-SYSTEM
1.7073- (flet ((redirect (spec operator)
1.7074- (let ((pathname
1.7075- (typecase spec
1.7076- (null (null-device-pathname))
1.7077- (string (parse-native-namestring spec))
1.7078- (pathname spec)
1.7079- ((eql :output)
1.7080- (unless (equal operator " 2>>")
1.7081- (parameter-error "~S: only the ~S argument can be ~S"
1.7082- 'run-program :error-output :output))
1.7083- (return-from redirect '(" 2>&1"))))))
1.7084- (when pathname
1.7085- (list operator " "
1.7086- (escape-shell-token (native-namestring pathname)))))))
1.7087- (let* ((redirections (append (redirect in " <") (redirect out " >>") (redirect err " 2>>")))
1.7088- (normalized (%normalize-system-command command))
1.7089- (directory (or directory #+(or abcl xcl) (getcwd)))
1.7090- (chdir (when directory
1.7091- (let ((dir-arg (escape-shell-token (native-namestring directory))))
1.7092- (os-cond
1.7093- ((os-unix-p) `("cd " ,dir-arg " ; "))
1.7094- ((os-windows-p) `("cd /d " ,dir-arg " & ")))))))
1.7095- (reduce/strcat
1.7096- (os-cond
1.7097- ((os-unix-p) `(,@(when redirections `("exec " ,@redirections " ; ")) ,@chdir ,normalized))
1.7098- ((os-windows-p) `(,@redirections " (" ,@chdir ,normalized ")")))))))
1.7099-
1.7100- (defun %system (command &rest keys &key directory
1.7101- input (if-input-does-not-exist :error)
1.7102- output (if-output-exists :supersede)
1.7103- error-output (if-error-output-exists :supersede)
1.7104- &allow-other-keys)
1.7105- "A portable abstraction of a low-level call to libc's system()."
1.7106- (declare (ignorable keys directory input if-input-does-not-exist output
1.7107- if-output-exists error-output if-error-output-exists))
1.7108- (when (member :stream (list input output error-output))
1.7109- (parameter-error "~S: ~S is not allowed as synchronous I/O redirection argument"
1.7110- 'run-program :stream))
1.7111- #+(or abcl allegro clozure cmucl ecl (and lispworks os-unix) mkcl sbcl scl)
1.7112- (let (#+(or abcl ecl mkcl)
1.7113- (version (parse-version
1.7114- #-abcl
1.7115- (lisp-implementation-version)
1.7116- #+abcl
1.7117- (second (split-string (implementation-identifier) :separator '(#\-))))))
1.7118- (nest
1.7119- #+abcl (unless (lexicographic< '< version '(1 4 0)))
1.7120- #+ecl (unless (lexicographic<= '< version '(16 0 0)))
1.7121- #+mkcl (unless (lexicographic<= '< version '(1 1 9)))
1.7122- (return-from %system
1.7123- (wait-process
1.7124- (apply 'launch-program (%normalize-system-command command) keys)))))
1.7125- #+(or abcl clasp clisp cormanlisp ecl gcl genera (and lispworks os-windows) mkcl xcl)
1.7126- (let ((%command (%redirected-system-command command input output error-output directory)))
1.7127- ;; see comments for these functions
1.7128- (%handle-if-does-not-exist input if-input-does-not-exist)
1.7129- (%handle-if-exists output if-output-exists)
1.7130- (%handle-if-exists error-output if-error-output-exists)
1.7131- #+abcl (ext:run-shell-command %command)
1.7132- #+(or clasp ecl) (let ((*standard-input* *stdin*)
1.7133- (*standard-output* *stdout*)
1.7134- (*error-output* *stderr*))
1.7135- (ext:system %command))
1.7136- #+clisp
1.7137- (let ((raw-exit-code
1.7138- (or
1.7139- #.`(#+os-windows ,@'(ext:run-shell-command %command)
1.7140- #+os-unix ,@'(ext:run-program "/bin/sh" :arguments `("-c" ,%command))
1.7141- :wait t :input :terminal :output :terminal)
1.7142- 0)))
1.7143- (if (minusp raw-exit-code)
1.7144- (- 128 raw-exit-code)
1.7145- raw-exit-code))
1.7146- #+cormanlisp (win32:system %command)
1.7147- #+gcl (system:system %command)
1.7148- #+genera (not-implemented-error '%system)
1.7149- #+(and lispworks os-windows)
1.7150- (system:call-system %command :current-directory directory :wait t)
1.7151- #+mcl (ccl::with-cstrs ((%%command %command)) (_system %%command))
1.7152- #+mkcl (mkcl:system %command)
1.7153- #+xcl (system:%run-shell-command %command)))
1.7154-
1.7155- (defun %use-system (command &rest keys
1.7156- &key input output error-output ignore-error-status &allow-other-keys)
1.7157- ;; helper for RUN-PROGRAM when using %system
1.7158- (let (output-result error-output-result exit-code)
1.7159- (with-program-output ((reduced-output)
1.7160- output :keys keys :setf output-result)
1.7161- (with-program-error-output ((reduced-error-output)
1.7162- error-output :keys keys :setf error-output-result)
1.7163- (with-program-input ((reduced-input) input :keys keys)
1.7164- (setf exit-code (apply '%system command
1.7165- :input reduced-input :output reduced-output
1.7166- :error-output reduced-error-output keys)))))
1.7167- (%check-result exit-code
1.7168- :command command
1.7169- :ignore-error-status ignore-error-status)
1.7170- (values output-result error-output-result exit-code)))
1.7171-
1.7172- (defun run-program (command &rest keys
1.7173- &key ignore-error-status (force-shell nil force-shell-suppliedp)
1.7174- input (if-input-does-not-exist :error)
1.7175- output (if-output-exists :supersede)
1.7176- error-output (if-error-output-exists :supersede)
1.7177- (element-type #-clozure *default-stream-element-type* #+clozure 'character)
1.7178- (external-format *utf-8-external-format*)
1.7179- &allow-other-keys)
1.7180- "Run program specified by COMMAND,
1.7181-either a list of strings specifying a program and list of arguments,
1.7182-or a string specifying a shell command (/bin/sh on Unix, CMD.EXE on Windows);
1.7183-_synchronously_ process its output as specified and return the processing results
1.7184-when the program and its output processing are complete.
1.7185-
1.7186-Always call a shell (rather than directly execute the command when possible)
1.7187-if FORCE-SHELL is specified. Similarly, never call a shell if FORCE-SHELL is
1.7188-specified to be NIL.
1.7189-
1.7190-Signal a continuable SUBPROCESS-ERROR if the process wasn't successful (exit-code 0),
1.7191-unless IGNORE-ERROR-STATUS is specified.
1.7192-
1.7193-If OUTPUT is a pathname, a string designating a pathname, or NIL (the default)
1.7194-designating the null device, the file at that path is used as output.
1.7195-If it's :INTERACTIVE, output is inherited from the current process;
1.7196-beware that this may be different from your *STANDARD-OUTPUT*,
1.7197-and under SLIME will be on your *inferior-lisp* buffer.
1.7198-If it's T, output goes to your current *STANDARD-OUTPUT* stream.
1.7199-Otherwise, OUTPUT should be a value that is a suitable first argument to
1.7200-SLURP-INPUT-STREAM (qv.), or a list of such a value and keyword arguments.
1.7201-In this case, RUN-PROGRAM will create a temporary stream for the program output;
1.7202-the program output, in that stream, will be processed by a call to SLURP-INPUT-STREAM,
1.7203-using OUTPUT as the first argument (or the first element of OUTPUT, and the rest as keywords).
1.7204-The primary value resulting from that call (or NIL if no call was needed)
1.7205-will be the first value returned by RUN-PROGRAM.
1.7206-E.g., using :OUTPUT :STRING will have it return the entire output stream as a string.
1.7207-And using :OUTPUT '(:STRING :STRIPPED T) will have it return the same string
1.7208-stripped of any ending newline.
1.7209-
1.7210-IF-OUTPUT-EXISTS, which is only meaningful if OUTPUT is a string or a
1.7211-pathname, can take the values :ERROR, :APPEND, and :SUPERSEDE (the
1.7212-default). The meaning of these values and their effect on the case
1.7213-where OUTPUT does not exist, is analogous to the IF-EXISTS parameter
1.7214-to OPEN with :DIRECTION :OUTPUT.
1.7215-
1.7216-ERROR-OUTPUT is similar to OUTPUT, except that the resulting value is returned
1.7217-as the second value of RUN-PROGRAM. T designates the *ERROR-OUTPUT*.
1.7218-Also :OUTPUT means redirecting the error output to the output stream,
1.7219-in which case NIL is returned.
1.7220-
1.7221-IF-ERROR-OUTPUT-EXISTS is similar to IF-OUTPUT-EXIST, except that it
1.7222-affects ERROR-OUTPUT rather than OUTPUT.
1.7223-
1.7224-INPUT is similar to OUTPUT, except that VOMIT-OUTPUT-STREAM is used,
1.7225-no value is returned, and T designates the *STANDARD-INPUT*.
1.7226-
1.7227-IF-INPUT-DOES-NOT-EXIST, which is only meaningful if INPUT is a string
1.7228-or a pathname, can take the values :CREATE and :ERROR (the
1.7229-default). The meaning of these values is analogous to the
1.7230-IF-DOES-NOT-EXIST parameter to OPEN with :DIRECTION :INPUT.
1.7231-
1.7232-ELEMENT-TYPE and EXTERNAL-FORMAT are passed on
1.7233-to your Lisp implementation, when applicable, for creation of the output stream.
1.7234-
1.7235-One and only one of the stream slurping or vomiting may or may not happen
1.7236-in parallel in parallel with the subprocess,
1.7237-depending on options and implementation,
1.7238-and with priority being given to output processing.
1.7239-Other streams are completely produced or consumed
1.7240-before or after the subprocess is spawned, using temporary files.
1.7241-
1.7242-RUN-PROGRAM returns 3 values:
1.7243-0- the result of the OUTPUT slurping if any, or NIL
1.7244-1- the result of the ERROR-OUTPUT slurping if any, or NIL
1.7245-2- either 0 if the subprocess exited with success status,
1.7246-or an indication of failure via the EXIT-CODE of the process"
1.7247- (declare (ignorable input output error-output if-input-does-not-exist if-output-exists
1.7248- if-error-output-exists element-type external-format ignore-error-status))
1.7249- #-(or abcl allegro clasp clisp clozure cmucl cormanlisp ecl gcl lispworks mcl mkcl sbcl scl xcl)
1.7250- (not-implemented-error 'run-program)
1.7251- (apply (if (or force-shell
1.7252- ;; Per doc string, set FORCE-SHELL to T if we get command as a string.
1.7253- ;; But don't override user's specified preference. [2015/06/29:rpg]
1.7254- (and (stringp command)
1.7255- (or (not force-shell-suppliedp)
1.7256- #-(or allegro clisp clozure sbcl) (os-cond ((os-windows-p) t))))
1.7257- #+(or clasp clisp cormanlisp gcl (and lispworks os-windows) mcl xcl) t
1.7258- ;; A race condition in ECL <= 16.0.0 prevents using ext:run-program
1.7259- #+ecl #.(if-let (ver (parse-version (lisp-implementation-version)))
1.7260- (lexicographic<= '< ver '(16 0 0)))
1.7261- #+(and lispworks os-unix) (%interactivep input output error-output))
1.7262- '%use-system '%use-launch-program)
1.7263- command keys)))
1.7264-
1.7265-;;;; ---------------------------------------------------------------------------
1.7266-;;;; Generic support for configuration files
1.7267-
1.7268-(uiop/package:define-package :uiop/configuration
1.7269- (:recycle :uiop/configuration :asdf/configuration) ;; necessary to upgrade from 2.27.
1.7270- (:use :uiop/package :uiop/common-lisp :uiop/utility
1.7271- :uiop/os :uiop/pathname :uiop/filesystem :uiop/stream :uiop/image :uiop/lisp-build)
1.7272- (:export
1.7273- #:user-configuration-directories #:system-configuration-directories ;; implemented in backward-driver
1.7274- #:in-first-directory #:in-user-configuration-directory #:in-system-configuration-directory ;; idem
1.7275- #:get-folder-path
1.7276- #:xdg-data-home #:xdg-config-home #:xdg-data-dirs #:xdg-config-dirs
1.7277- #:xdg-cache-home #:xdg-runtime-dir #:system-config-pathnames
1.7278- #:filter-pathname-set #:xdg-data-pathnames #:xdg-config-pathnames
1.7279- #:find-preferred-file #:xdg-data-pathname #:xdg-config-pathname
1.7280- #:validate-configuration-form #:validate-configuration-file #:validate-configuration-directory
1.7281- #:configuration-inheritance-directive-p
1.7282- #:report-invalid-form #:invalid-configuration #:*ignored-configuration-form* #:*user-cache*
1.7283- #:*clear-configuration-hook* #:clear-configuration #:register-clear-configuration-hook
1.7284- #:resolve-location #:location-designator-p #:location-function-p #:*here-directory*
1.7285- #:resolve-relative-location #:resolve-absolute-location #:upgrade-configuration
1.7286- #:uiop-directory))
1.7287-(in-package :uiop/configuration)
1.7288-
1.7289-(with-upgradability ()
1.7290- (define-condition invalid-configuration ()
1.7291- ((form :reader condition-form :initarg :form)
1.7292- (location :reader condition-location :initarg :location)
1.7293- (format :reader condition-format :initarg :format)
1.7294- (arguments :reader condition-arguments :initarg :arguments :initform nil))
1.7295- (:report (lambda (c s)
1.7296- (format s (compatfmt "~@<~? (will be skipped)~@:>")
1.7297- (condition-format c)
1.7298- (list* (condition-form c) (condition-location c)
1.7299- (condition-arguments c))))))
1.7300-
1.7301- (defun configuration-inheritance-directive-p (x)
1.7302- "Is X a configuration inheritance directive?"
1.7303- (let ((kw '(:inherit-configuration :ignore-inherited-configuration)))
1.7304- (or (member x kw)
1.7305- (and (length=n-p x 1) (member (car x) kw)))))
1.7306-
1.7307- (defun report-invalid-form (reporter &rest args)
1.7308- "Report an invalid form according to REPORTER and various ARGS"
1.7309- (etypecase reporter
1.7310- (null
1.7311- (apply 'error 'invalid-configuration args))
1.7312- (function
1.7313- (apply reporter args))
1.7314- ((or symbol string)
1.7315- (apply 'error reporter args))
1.7316- (cons
1.7317- (apply 'apply (append reporter args)))))
1.7318-
1.7319- (defvar *ignored-configuration-form* nil
1.7320- "Have configuration forms been ignored while parsing the configuration?")
1.7321-
1.7322- (defun validate-configuration-form (form tag directive-validator
1.7323- &key location invalid-form-reporter)
1.7324- "Validate a configuration FORM. By default it will raise an error if the
1.7325-FORM is not valid. Otherwise it will return the validated form.
1.7326- Arguments control the behavior:
1.7327- The configuration FORM should be of the form (TAG . <rest>)
1.7328- Each element of <rest> will be checked by first seeing if it's a configuration inheritance
1.7329-directive (see CONFIGURATION-INHERITANCE-DIRECTIVE-P) then invoking DIRECTIVE-VALIDATOR
1.7330-on it.
1.7331- In the event of an invalid form, INVALID-FORM-REPORTER will be used to control
1.7332-reporting (see REPORT-INVALID-FORM) with LOCATION providing information about where
1.7333-the configuration form appeared."
1.7334- (unless (and (consp form) (eq (car form) tag))
1.7335- (setf *ignored-configuration-form* t)
1.7336- (report-invalid-form invalid-form-reporter :form form :location location)
1.7337- (return-from validate-configuration-form nil))
1.7338- (loop :with inherit = 0 :with ignore-invalid-p = nil :with x = (list tag)
1.7339- :for directive :in (cdr form)
1.7340- :when (cond
1.7341- ((configuration-inheritance-directive-p directive)
1.7342- (incf inherit) t)
1.7343- ((eq directive :ignore-invalid-entries)
1.7344- (setf ignore-invalid-p t) t)
1.7345- ((funcall directive-validator directive)
1.7346- t)
1.7347- (ignore-invalid-p
1.7348- nil)
1.7349- (t
1.7350- (setf *ignored-configuration-form* t)
1.7351- (report-invalid-form invalid-form-reporter :form directive :location location)
1.7352- nil))
1.7353- :do (push directive x)
1.7354- :finally
1.7355- (unless (= inherit 1)
1.7356- (report-invalid-form invalid-form-reporter
1.7357- :form form :location location
1.7358- ;; we throw away the form and location arguments, hence the ~2*
1.7359- ;; this is necessary because of the report in INVALID-CONFIGURATION
1.7360- :format (compatfmt "~@<Invalid source registry ~S~@[ in ~S~]. ~
1.7361- One and only one of ~S or ~S is required.~@:>")
1.7362- :arguments '(:inherit-configuration :ignore-inherited-configuration)))
1.7363- (return (nreverse x))))
1.7364-
1.7365- (defun validate-configuration-file (file validator &key description)
1.7366- "Validate a configuration FILE. The configuration file should have only one s-expression
1.7367-in it, which will be checked with the VALIDATOR FORM. DESCRIPTION argument used for error
1.7368-reporting."
1.7369- (let ((forms (read-file-forms file)))
1.7370- (unless (length=n-p forms 1)
1.7371- (error (compatfmt "~@<One and only one form allowed for ~A. Got: ~3i~_~S~@:>~%")
1.7372- description forms))
1.7373- (funcall validator (car forms) :location file)))
1.7374-
1.7375- (defun validate-configuration-directory (directory tag validator &key invalid-form-reporter)
1.7376- "Map the VALIDATOR across the .conf files in DIRECTORY, the TAG will
1.7377-be applied to the results to yield a configuration form. Current
1.7378-values of TAG include :source-registry and :output-translations."
1.7379- (let ((files (sort (ignore-errors ;; SORT w/o COPY-LIST is OK: DIRECTORY returns a fresh list
1.7380- (remove-if
1.7381- 'hidden-pathname-p
1.7382- (directory* (make-pathname :name *wild* :type "conf" :defaults directory))))
1.7383- #'string< :key #'namestring)))
1.7384- `(,tag
1.7385- ,@(loop :for file :in files :append
1.7386- (loop :with ignore-invalid-p = nil
1.7387- :for form :in (read-file-forms file)
1.7388- :when (eq form :ignore-invalid-entries)
1.7389- :do (setf ignore-invalid-p t)
1.7390- :else
1.7391- :when (funcall validator form)
1.7392- :collect form
1.7393- :else
1.7394- :when ignore-invalid-p
1.7395- :do (setf *ignored-configuration-form* t)
1.7396- :else
1.7397- :do (report-invalid-form invalid-form-reporter :form form :location file)))
1.7398- :inherit-configuration)))
1.7399-
1.7400- (defun resolve-relative-location (x &key ensure-directory wilden)
1.7401- "Given a designator X for an relative location, resolve it to a pathname."
1.7402- (ensure-pathname
1.7403- (etypecase x
1.7404- (null nil)
1.7405- (pathname x)
1.7406- (string (parse-unix-namestring
1.7407- x :ensure-directory ensure-directory))
1.7408- (cons
1.7409- (if (null (cdr x))
1.7410- (resolve-relative-location
1.7411- (car x) :ensure-directory ensure-directory :wilden wilden)
1.7412- (let* ((car (resolve-relative-location
1.7413- (car x) :ensure-directory t :wilden nil)))
1.7414- (merge-pathnames*
1.7415- (resolve-relative-location
1.7416- (cdr x) :ensure-directory ensure-directory :wilden wilden)
1.7417- car))))
1.7418- ((eql :*/) *wild-directory*)
1.7419- ((eql :**/) *wild-inferiors*)
1.7420- ((eql :*.*.*) *wild-file*)
1.7421- ((eql :implementation)
1.7422- (parse-unix-namestring
1.7423- (implementation-identifier) :ensure-directory t))
1.7424- ((eql :implementation-type)
1.7425- (parse-unix-namestring
1.7426- (string-downcase (implementation-type)) :ensure-directory t))
1.7427- ((eql :hostname)
1.7428- (parse-unix-namestring (hostname) :ensure-directory t)))
1.7429- :wilden (and wilden (not (pathnamep x)) (not (member x '(:*/ :**/ :*.*.*))))
1.7430- :want-relative t))
1.7431-
1.7432- (defvar *here-directory* nil
1.7433- "This special variable is bound to the currect directory during calls to
1.7434-PROCESS-SOURCE-REGISTRY in order that we be able to interpret the :here
1.7435-directive.")
1.7436-
1.7437- (defvar *user-cache* nil
1.7438- "A specification as per RESOLVE-LOCATION of where the user keeps his FASL cache")
1.7439-
1.7440- (defun resolve-absolute-location (x &key ensure-directory wilden)
1.7441- "Given a designator X for an absolute location, resolve it to a pathname"
1.7442- (ensure-pathname
1.7443- (etypecase x
1.7444- (null nil)
1.7445- (pathname x)
1.7446- (string
1.7447- (let ((p #-mcl (parse-namestring x)
1.7448- #+mcl (probe-posix x)))
1.7449- #+mcl (unless p (error "POSIX pathname ~S does not exist" x))
1.7450- (if ensure-directory (ensure-directory-pathname p) p)))
1.7451- (cons
1.7452- (return-from resolve-absolute-location
1.7453- (if (null (cdr x))
1.7454- (resolve-absolute-location
1.7455- (car x) :ensure-directory ensure-directory :wilden wilden)
1.7456- (merge-pathnames*
1.7457- (resolve-relative-location
1.7458- (cdr x) :ensure-directory ensure-directory :wilden wilden)
1.7459- (resolve-absolute-location
1.7460- (car x) :ensure-directory t :wilden nil)))))
1.7461- ((eql :root)
1.7462- ;; special magic! we return a relative pathname,
1.7463- ;; but what it means to the output-translations is
1.7464- ;; "relative to the root of the source pathname's host and device".
1.7465- (return-from resolve-absolute-location
1.7466- (let ((p (make-pathname :directory '(:relative))))
1.7467- (if wilden (wilden p) p))))
1.7468- ((eql :home) (user-homedir-pathname))
1.7469- ((eql :here) (resolve-absolute-location
1.7470- (or *here-directory* (pathname-directory-pathname (truename (load-pathname))))
1.7471- :ensure-directory t :wilden nil))
1.7472- ((eql :user-cache) (resolve-absolute-location
1.7473- *user-cache* :ensure-directory t :wilden nil)))
1.7474- :wilden (and wilden (not (pathnamep x)))
1.7475- :resolve-symlinks *resolve-symlinks*
1.7476- :want-absolute t))
1.7477-
1.7478- ;; Try to override declaration in previous versions of ASDF.
1.7479- (declaim (ftype (function (t &key (:directory boolean) (:wilden boolean)
1.7480- (:ensure-directory boolean)) t) resolve-location))
1.7481-
1.7482- (defun resolve-location (x &key ensure-directory wilden directory)
1.7483- "Resolve location designator X into a PATHNAME"
1.7484- ;; :directory backward compatibility, until 2014-01-16: accept directory as well as ensure-directory
1.7485- (loop :with dirp = (or directory ensure-directory)
1.7486- :with (first . rest) = (if (atom x) (list x) x)
1.7487- :with path = (or (resolve-absolute-location
1.7488- first :ensure-directory (and (or dirp rest) t)
1.7489- :wilden (and wilden (null rest)))
1.7490- (return nil))
1.7491- :for (element . morep) :on rest
1.7492- :for dir = (and (or morep dirp) t)
1.7493- :for wild = (and wilden (not morep))
1.7494- :for sub = (merge-pathnames*
1.7495- (resolve-relative-location
1.7496- element :ensure-directory dir :wilden wild)
1.7497- path)
1.7498- :do (setf path (if (absolute-pathname-p sub) (resolve-symlinks* sub) sub))
1.7499- :finally (return path)))
1.7500-
1.7501- (defun location-designator-p (x)
1.7502- "Is X a designator for a location?"
1.7503- ;; NIL means "skip this entry", or as an output translation, same as translation input.
1.7504- ;; T means "any input" for a translation, or as output, same as translation input.
1.7505- (flet ((absolute-component-p (c)
1.7506- (typep c '(or string pathname
1.7507- (member :root :home :here :user-cache))))
1.7508- (relative-component-p (c)
1.7509- (typep c '(or string pathname
1.7510- (member :*/ :**/ :*.*.* :implementation :implementation-type)))))
1.7511- (or (typep x 'boolean)
1.7512- (absolute-component-p x)
1.7513- (and (consp x) (absolute-component-p (first x)) (every #'relative-component-p (rest x))))))
1.7514-
1.7515- (defun location-function-p (x)
1.7516- "Is X the specification of a location function?"
1.7517- ;; Location functions are allowed in output translations, and notably used by ABCL for JAR file support.
1.7518- (and (length=n-p x 2) (eq (car x) :function)))
1.7519-
1.7520- (defvar *clear-configuration-hook* '())
1.7521-
1.7522- (defun register-clear-configuration-hook (hook-function &optional call-now-p)
1.7523- "Register a function to be called when clearing configuration"
1.7524- (register-hook-function '*clear-configuration-hook* hook-function call-now-p))
1.7525-
1.7526- (defun clear-configuration ()
1.7527- "Call the functions in *CLEAR-CONFIGURATION-HOOK*"
1.7528- (call-functions *clear-configuration-hook*))
1.7529-
1.7530- (register-image-dump-hook 'clear-configuration)
1.7531-
1.7532- (defun upgrade-configuration ()
1.7533- "If a previous version of ASDF failed to read some configuration, try again now."
1.7534- (when *ignored-configuration-form*
1.7535- (clear-configuration)
1.7536- (setf *ignored-configuration-form* nil)))
1.7537-
1.7538-
1.7539- (defun get-folder-path (folder)
1.7540- "Semi-portable implementation of a subset of LispWorks' sys:get-folder-path,
1.7541-this function tries to locate the Windows FOLDER for one of
1.7542-:LOCAL-APPDATA, :APPDATA or :COMMON-APPDATA.
1.7543- Returns NIL when the folder is not defined (e.g., not on Windows)."
1.7544- (or #+(and lispworks os-windows) (sys:get-folder-path folder)
1.7545- ;; read-windows-registry HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\AppData
1.7546- (ecase folder
1.7547- (:local-appdata (or (getenv-absolute-directory "LOCALAPPDATA")
1.7548- (subpathname* (get-folder-path :appdata) "Local")))
1.7549- (:appdata (getenv-absolute-directory "APPDATA"))
1.7550- (:common-appdata (or (getenv-absolute-directory "ALLUSERSAPPDATA")
1.7551- (subpathname* (getenv-absolute-directory "ALLUSERSPROFILE") "Application Data/"))))))
1.7552-
1.7553-
1.7554- ;; Support for the XDG Base Directory Specification
1.7555- (defun xdg-data-home (&rest more)
1.7556- "Returns an absolute pathname for the directory containing user-specific data files.
1.7557-MORE may contain specifications for a subpath relative to this directory: a
1.7558-subpathname specification and keyword arguments as per RESOLVE-LOCATION \(see
1.7559-also \"Configuration DSL\"\) in the ASDF manual."
1.7560- (resolve-absolute-location
1.7561- `(,(or (getenv-absolute-directory "XDG_DATA_HOME")
1.7562- (os-cond
1.7563- ((os-windows-p) (get-folder-path :local-appdata))
1.7564- (t (subpathname (user-homedir-pathname) ".local/share/"))))
1.7565- ,more)))
1.7566-
1.7567- (defun xdg-config-home (&rest more)
1.7568- "Returns a pathname for the directory containing user-specific configuration files.
1.7569-MORE may contain specifications for a subpath relative to this directory: a
1.7570-subpathname specification and keyword arguments as per RESOLVE-LOCATION \(see
1.7571-also \"Configuration DSL\"\) in the ASDF manual."
1.7572- (resolve-absolute-location
1.7573- `(,(or (getenv-absolute-directory "XDG_CONFIG_HOME")
1.7574- (os-cond
1.7575- ((os-windows-p) (xdg-data-home "config/"))
1.7576- (t (subpathname (user-homedir-pathname) ".config/"))))
1.7577- ,more)))
1.7578-
1.7579- (defun xdg-data-dirs (&rest more)
1.7580- "The preference-ordered set of additional paths to search for data files.
1.7581-Returns a list of absolute directory pathnames.
1.7582-MORE may contain specifications for a subpath relative to these directories: a
1.7583-subpathname specification and keyword arguments as per RESOLVE-LOCATION \(see
1.7584-also \"Configuration DSL\"\) in the ASDF manual."
1.7585- (mapcar #'(lambda (d) (resolve-location `(,d ,more)))
1.7586- (or (remove nil (getenv-absolute-directories "XDG_DATA_DIRS"))
1.7587- (os-cond
1.7588- ((os-windows-p) (mapcar 'get-folder-path '(:appdata :common-appdata)))
1.7589- ;; macOS' separate read-only system volume means that the contents
1.7590- ;; of /usr/share are frozen by Apple. Unlike when running natively
1.7591- ;; on macOS, Genera must access the filesystem through NFS. Attempting
1.7592- ;; to export either the root (/) or /usr/share simply doesn't work.
1.7593- ;; (Genera will go into an infinite loop trying to access those mounts.)
1.7594- ;; So, when running Genera on macOS, only search /usr/local/share.
1.7595- ((os-genera-p)
1.7596- #+Genera (sys:system-case
1.7597- (darwin-vlm (mapcar 'parse-unix-namestring '("/usr/local/share/")))
1.7598- (otherwise (mapcar 'parse-unix-namestring '("/usr/local/share/" "/usr/share/")))))
1.7599- (t (mapcar 'parse-unix-namestring '("/usr/local/share/" "/usr/share/")))))))
1.7600-
1.7601- (defun xdg-config-dirs (&rest more)
1.7602- "The preference-ordered set of additional base paths to search for configuration files.
1.7603-Returns a list of absolute directory pathnames.
1.7604-MORE may contain specifications for a subpath relative to these directories:
1.7605-subpathname specification and keyword arguments as per RESOLVE-LOCATION \(see
1.7606-also \"Configuration DSL\"\) in the ASDF manual."
1.7607- (mapcar #'(lambda (d) (resolve-location `(,d ,more)))
1.7608- (or (remove nil (getenv-absolute-directories "XDG_CONFIG_DIRS"))
1.7609- (os-cond
1.7610- ((os-windows-p) (xdg-data-dirs "config/"))
1.7611- (t (mapcar 'parse-unix-namestring '("/etc/xdg/")))))))
1.7612-
1.7613- (defun xdg-cache-home (&rest more)
1.7614- "The base directory relative to which user specific non-essential data files should be stored.
1.7615-Returns an absolute directory pathname.
1.7616-MORE may contain specifications for a subpath relative to this directory: a
1.7617-subpathname specification and keyword arguments as per RESOLVE-LOCATION \(see
1.7618-also \"Configuration DSL\"\) in the ASDF manual."
1.7619- (resolve-absolute-location
1.7620- `(,(or (getenv-absolute-directory "XDG_CACHE_HOME")
1.7621- (os-cond
1.7622- ((os-windows-p) (xdg-data-home "cache/"))
1.7623- (t (subpathname* (user-homedir-pathname) ".cache/"))))
1.7624- ,more)))
1.7625-
1.7626- (defun xdg-runtime-dir (&rest more)
1.7627- "Pathname for user-specific non-essential runtime files and other file objects,
1.7628-such as sockets, named pipes, etc.
1.7629-Returns an absolute directory pathname.
1.7630-MORE may contain specifications for a subpath relative to this directory: a
1.7631-subpathname specification and keyword arguments as per RESOLVE-LOCATION \(see
1.7632-also \"Configuration DSL\"\) in the ASDF manual."
1.7633- ;; The XDG spec says that if not provided by the login system, the application should
1.7634- ;; issue a warning and provide a replacement. UIOP is not equipped to do that and returns NIL.
1.7635- (resolve-absolute-location `(,(getenv-absolute-directory "XDG_RUNTIME_DIR") ,more)))
1.7636-
1.7637- ;;; NOTE: modified the docstring because "system user configuration
1.7638- ;;; directories" seems self-contradictory. I'm not sure my wording is right.
1.7639- (defun system-config-pathnames (&rest more)
1.7640- "Return a list of directories where are stored the system's default user configuration information.
1.7641-MORE may contain specifications for a subpath relative to these directories: a
1.7642-subpathname specification and keyword arguments as per RESOLVE-LOCATION \(see
1.7643-also \"Configuration DSL\"\) in the ASDF manual."
1.7644- (declare (ignorable more))
1.7645- (os-cond
1.7646- ((os-unix-p) (list (resolve-absolute-location `(,(parse-unix-namestring "/etc/") ,more))))))
1.7647-
1.7648- (defun filter-pathname-set (dirs)
1.7649- "Parse strings as unix namestrings and remove duplicates and non absolute-pathnames in a list."
1.7650- (remove-duplicates (remove-if-not #'absolute-pathname-p dirs) :from-end t :test 'equal))
1.7651-
1.7652- (defun xdg-data-pathnames (&rest more)
1.7653- "Return a list of absolute pathnames for application data directories. With APP,
1.7654-returns directory for data for that application, without APP, returns the set of directories
1.7655-for storing all application configurations.
1.7656-MORE may contain specifications for a subpath relative to these directories: a
1.7657-subpathname specification and keyword arguments as per RESOLVE-LOCATION \(see
1.7658-also \"Configuration DSL\"\) in the ASDF manual."
1.7659- (filter-pathname-set
1.7660- `(,(xdg-data-home more)
1.7661- ,@(xdg-data-dirs more))))
1.7662-
1.7663- (defun xdg-config-pathnames (&rest more)
1.7664- "Return a list of pathnames for application configuration.
1.7665-MORE may contain specifications for a subpath relative to these directories: a
1.7666-subpathname specification and keyword arguments as per RESOLVE-LOCATION \(see
1.7667-also \"Configuration DSL\"\) in the ASDF manual."
1.7668- (filter-pathname-set
1.7669- `(,(xdg-config-home more)
1.7670- ,@(xdg-config-dirs more))))
1.7671-
1.7672- (defun find-preferred-file (files &key (direction :input))
1.7673- "Find first file in the list of FILES that exists (for direction :input or :probe)
1.7674-or just the first one (for direction :output or :io).
1.7675- Note that when we say \"file\" here, the files in question may be directories."
1.7676- (find-if (ecase direction ((:probe :input) 'probe-file*) ((:output :io) 'identity)) files))
1.7677-
1.7678- (defun xdg-data-pathname (&optional more (direction :input))
1.7679- (find-preferred-file (xdg-data-pathnames more) :direction direction))
1.7680-
1.7681- (defun xdg-config-pathname (&optional more (direction :input))
1.7682- (find-preferred-file (xdg-config-pathnames more) :direction direction))
1.7683-
1.7684- (defun compute-user-cache ()
1.7685- "Compute (and return) the location of the default user-cache for translate-output
1.7686-objects. Side-effects for cached file location computation."
1.7687- (setf *user-cache* (xdg-cache-home "common-lisp" :implementation)))
1.7688- (register-image-restore-hook 'compute-user-cache)
1.7689-
1.7690- (defun uiop-directory ()
1.7691- "Try to locate the UIOP source directory at runtime"
1.7692- (labels ((pf (x) (ignore-errors (probe-file* x)))
1.7693- (sub (x y) (pf (subpathname x y)))
1.7694- (ssd (x) (ignore-errors (symbol-call :asdf :system-source-directory x))))
1.7695- ;; NB: conspicuously *not* including searches based on #.(current-lisp-pathname)
1.7696- (or
1.7697- ;; Look under uiop if available as source override, under asdf if avaiable as source
1.7698- (ssd "uiop")
1.7699- (sub (ssd "asdf") "uiop/")
1.7700- ;; Look in recommended path for user-visible source installation
1.7701- (sub (user-homedir-pathname) "common-lisp/asdf/uiop/")
1.7702- ;; Look in XDG paths under known package names for user-invisible source installation
1.7703- (xdg-data-pathname "common-lisp/source/asdf/uiop/")
1.7704- (xdg-data-pathname "common-lisp/source/cl-asdf/uiop/") ; traditional Debian location
1.7705- ;; The last one below is useful for Fare, primary (sole?) known user
1.7706- (sub (user-homedir-pathname) "cl/asdf/uiop/")
1.7707- (cerror "Configure source registry to include UIOP source directory and retry."
1.7708- "Unable to find UIOP directory")
1.7709- (uiop-directory)))))
1.7710-;;; -------------------------------------------------------------------------
1.7711-;;; Hacks for backward-compatibility with older versions of UIOP
1.7712-
1.7713-(uiop/package:define-package :uiop/backward-driver
1.7714- (:recycle :uiop/backward-driver :asdf/backward-driver :uiop)
1.7715- (:use :uiop/common-lisp :uiop/package :uiop/utility :uiop/version
1.7716- :uiop/pathname :uiop/stream :uiop/os :uiop/image
1.7717- :uiop/run-program :uiop/lisp-build :uiop/configuration)
1.7718- (:export
1.7719- #:coerce-pathname
1.7720- #:user-configuration-directories #:system-configuration-directories
1.7721- #:in-first-directory #:in-user-configuration-directory #:in-system-configuration-directory
1.7722- #:version-compatible-p))
1.7723-(in-package :uiop/backward-driver)
1.7724-
1.7725-(eval-when (:compile-toplevel :load-toplevel :execute)
1.7726-(with-deprecation ((version-deprecation *uiop-version* :style-warning "3.2" :warning "3.4"))
1.7727- ;; Backward compatibility with ASDF 2.000 to 2.26
1.7728-
1.7729- ;; For backward-compatibility only, for people using internals
1.7730- ;; Reported users in quicklisp 2015-11: hu.dwim.asdf (removed in next release)
1.7731- ;; Will be removed after 2015-12.
1.7732- (defun coerce-pathname (name &key type defaults)
1.7733- "DEPRECATED. Please use UIOP:PARSE-UNIX-NAMESTRING instead."
1.7734- (parse-unix-namestring name :type type :defaults defaults))
1.7735-
1.7736- ;; Backward compatibility for ASDF 2.27 to 3.1.4
1.7737- (defun user-configuration-directories ()
1.7738- "Return the current user's list of user configuration directories
1.7739-for configuring common-lisp.
1.7740-DEPRECATED. Use UIOP:XDG-CONFIG-PATHNAMES instead."
1.7741- (xdg-config-pathnames "common-lisp"))
1.7742- (defun system-configuration-directories ()
1.7743- "Return the list of system configuration directories for common-lisp.
1.7744-DEPRECATED. Use UIOP:SYSTEM-CONFIG-PATHNAMES (with argument \"common-lisp\"),
1.7745-instead."
1.7746- (system-config-pathnames "common-lisp"))
1.7747- (defun in-first-directory (dirs x &key (direction :input))
1.7748- "Finds the first appropriate file named X in the list of DIRS for I/O
1.7749-in DIRECTION \(which may be :INPUT, :OUTPUT, :IO, or :PROBE).
1.7750-If direction is :INPUT or :PROBE, will return the first extant file named
1.7751-X in one of the DIRS.
1.7752-If direction is :OUTPUT or :IO, will simply return the file named X in the
1.7753-first element of DIRS that exists. DEPRECATED."
1.7754- (find-preferred-file
1.7755- (mapcar #'(lambda (dir) (subpathname (ensure-directory-pathname dir) x)) dirs)
1.7756- :direction direction))
1.7757- (defun in-user-configuration-directory (x &key (direction :input))
1.7758- "Return the file named X in the user configuration directory for common-lisp.
1.7759-DEPRECATED."
1.7760- (xdg-config-pathname `("common-lisp" ,x) direction))
1.7761- (defun in-system-configuration-directory (x &key (direction :input))
1.7762- "Return the pathname for the file named X under the system configuration directory
1.7763-for common-lisp. DEPRECATED."
1.7764- (find-preferred-file (system-config-pathnames "common-lisp" x) :direction direction))
1.7765-
1.7766-
1.7767- ;; Backward compatibility with ASDF 1 to ASDF 2.32
1.7768-
1.7769- (defun version-compatible-p (provided-version required-version)
1.7770- "Is the provided version a compatible substitution for the required-version?
1.7771-If major versions differ, it's not compatible.
1.7772-If they are equal, then any later version is compatible,
1.7773-with later being determined by a lexicographical comparison of minor numbers.
1.7774-DEPRECATED."
1.7775- (let ((x (parse-version provided-version nil))
1.7776- (y (parse-version required-version nil)))
1.7777- (and x y (= (car x) (car y)) (lexicographic<= '< (cdr y) (cdr x)))))))
1.7778-
1.7779-;;;; ---------------------------------------------------------------------------
1.7780-;;;; Re-export all the functionality in UIOP
1.7781-
1.7782-(uiop/package:define-package :uiop/driver
1.7783- (:nicknames :uiop ;; Official name we recommend should be used for all references to uiop symbols.
1.7784- :asdf/driver) ;; DO NOT USE, a deprecated name, not supported anymore.
1.7785- ;; We should remove the name :asdf/driver at some point,
1.7786- ;; but not until it has been eradicated from Quicklisp for a year or two.
1.7787- ;; The last known user was cffi (PR merged in May 2020).
1.7788- (:use :uiop/common-lisp)
1.7789- ;; NB: We are not reexporting uiop/common-lisp
1.7790- ;; which include all of CL with compatibility modifications on select platforms,
1.7791- ;; because that would cause potential conflicts for packages that
1.7792- ;; might want to :use (:cl :uiop) or :use (:closer-common-lisp :uiop), etc.
1.7793- (:use-reexport
1.7794- :uiop/package* :uiop/utility :uiop/version
1.7795- :uiop/os :uiop/pathname :uiop/filesystem :uiop/stream :uiop/image
1.7796- :uiop/launch-program :uiop/run-program
1.7797- :uiop/lisp-build :uiop/configuration :uiop/backward-driver))
1.7798-
1.7799-;; Provide both lowercase and uppercase, to satisfy more implementations.
1.7800-(provide "uiop") (provide "UIOP")
1.7801-;;;; -------------------------------------------------------------------------
1.7802-;;;; Handle upgrade as forward- and backward-compatibly as possible
1.7803-;; See https://bugs.launchpad.net/asdf/+bug/485687
1.7804-
1.7805-(uiop/package:define-package :asdf/upgrade
1.7806- (:recycle :asdf/upgrade :asdf)
1.7807- (:use :uiop/common-lisp :uiop)
1.7808- (:export
1.7809- #:asdf-version #:*previous-asdf-versions* #:*asdf-version*
1.7810- #:asdf-message #:*verbose-out*
1.7811- #:upgrading-p #:when-upgrading #:upgrade-asdf #:defparameter*
1.7812- #:*post-upgrade-cleanup-hook* #:cleanup-upgraded-asdf
1.7813- ;; There will be no symbol left behind!
1.7814- #:with-asdf-deprecation
1.7815- #:intern*)
1.7816- (:import-from :uiop/package #:intern* #:find-symbol*))
1.7817-(in-package :asdf/upgrade)
1.7818-
1.7819-;;; Special magic to detect if this is an upgrade
1.7820-
1.7821-(with-upgradability ()
1.7822- (defun asdf-version ()
1.7823- "Exported interface to the version of ASDF currently installed. A string.
1.7824-You can compare this string with e.g.: (ASDF:VERSION-SATISFIES (ASDF:ASDF-VERSION) \"3.4.5.67\")."
1.7825- (when (find-package :asdf)
1.7826- (or (symbol-value (find-symbol (string :*asdf-version*) :asdf))
1.7827- (let* ((revsym (find-symbol (string :*asdf-revision*) :asdf))
1.7828- (rev (and revsym (boundp revsym) (symbol-value revsym))))
1.7829- (etypecase rev
1.7830- (string rev)
1.7831- (cons (format nil "~{~D~^.~}" rev))
1.7832- (null "1.0"))))))
1.7833- ;; This (private) variable contains a list of versions of previously loaded variants of ASDF,
1.7834- ;; from which ASDF was upgraded.
1.7835- ;; Important: define *p-a-v* /before/ *a-v* so that they initialize correctly.
1.7836- (defvar *previous-asdf-versions*
1.7837- (let ((previous (asdf-version)))
1.7838- (when previous
1.7839- ;; Punt on upgrade from ASDF1 or ASDF2, by renaming (or deleting) the package.
1.7840- (when (version< previous "2.27") ;; 2.27 is the first to have the :asdf3 feature.
1.7841- (let ((away (format nil "~A-~A" :asdf previous)))
1.7842- (rename-package :asdf away)
1.7843- (when *load-verbose*
1.7844- (format t "~&; Renamed old ~A package away to ~A~%" :asdf away))))
1.7845- (list previous))))
1.7846- ;; This public variable will be bound shortly to the currently loaded version of ASDF.
1.7847- (defvar *asdf-version* nil)
1.7848- ;; We need to clear systems from versions older than the one in this (private) parameter.
1.7849- ;; The latest incompatible defclass is 2.32.13 renaming a slot in component,
1.7850- ;; or 3.2.0.2 for CCL (incompatibly changing some superclasses).
1.7851- ;; the latest incompatible gf change is in 3.1.7.20 (see redefined-functions below).
1.7852- (defparameter *oldest-forward-compatible-asdf-version* "3.2.0.2")
1.7853- ;; Semi-private variable: a designator for a stream on which to output ASDF progress messages
1.7854- (defvar *verbose-out* nil)
1.7855- ;; Private function by which ASDF outputs progress messages and warning messages:
1.7856- (defun asdf-message (format-string &rest format-args)
1.7857- (when *verbose-out* (apply 'format *verbose-out* format-string format-args)))
1.7858- ;; Private hook for functions to run after ASDF has upgraded itself from an older variant:
1.7859- (defvar *post-upgrade-cleanup-hook* ())
1.7860- ;; Private variable for post upgrade cleanup to communicate if an upgrade has
1.7861- ;; actually occured.
1.7862- (defvar *asdf-upgraded-p*)
1.7863- ;; Private function to detect whether the current upgrade counts as an incompatible
1.7864- ;; data schema upgrade implying the need to drop data.
1.7865- (defun upgrading-p (&optional (oldest-compatible-version *oldest-forward-compatible-asdf-version*))
1.7866- (and *previous-asdf-versions*
1.7867- (version< (first *previous-asdf-versions*) oldest-compatible-version)))
1.7868- ;; Private variant of defparameter that works in presence of incompatible upgrades:
1.7869- ;; behaves like defvar in a compatible upgrade (e.g. reloading system after simple code change),
1.7870- ;; but behaves like defparameter if in presence of an incompatible upgrade.
1.7871- (defmacro defparameter* (var value &optional docstring (version *oldest-forward-compatible-asdf-version*))
1.7872- (let* ((name (string-trim "*" var))
1.7873- (valfun (intern (format nil "%~A-~A-~A" :compute name :value))))
1.7874- `(progn
1.7875- (defun ,valfun () ,value)
1.7876- (defvar ,var (,valfun) ,@(ensure-list docstring))
1.7877- (when (upgrading-p ,version)
1.7878- (setf ,var (,valfun))))))
1.7879- ;; Private macro to declare sections of code that are only compiled and run when upgrading.
1.7880- ;; The use of eval portably ensures that the code will not have adverse compile-time side-effects,
1.7881- ;; whereas the use of handler-bind portably ensures that it will not issue warnings when it runs.
1.7882- (defmacro when-upgrading ((&key (version *oldest-forward-compatible-asdf-version*)
1.7883- (upgrading-p `(upgrading-p ,version)) when) &body body)
1.7884- "A wrapper macro for code that should only be run when upgrading a
1.7885-previously-loaded version of ASDF."
1.7886- `(with-upgradability ()
1.7887- (when (and ,upgrading-p ,@(when when `(,when)))
1.7888- (handler-bind ((style-warning #'muffle-warning))
1.7889- (eval '(progn ,@body))))))
1.7890- ;; Only now can we safely update the version.
1.7891- (let* (;; For bug reporting sanity, please always bump this version when you modify this file.
1.7892- ;; Please also modify asdf.asd to reflect this change. make bump-version v=3.4.5.67.8
1.7893- ;; can help you do these changes in synch (look at the source for documentation).
1.7894- ;; Relying on its automation, the version is now redundantly present on top of asdf.lisp.
1.7895- ;; "3.4" would be the general branch for major version 3, minor version 4.
1.7896- ;; "3.4.5" would be an official release in the 3.4 branch.
1.7897- ;; "3.4.5.67" would be a development version in the official branch, on top of 3.4.5.
1.7898- ;; "3.4.5.0.8" would be your eighth local modification of official release 3.4.5
1.7899- ;; "3.4.5.67.8" would be your eighth local modification of development version 3.4.5.67
1.7900- (asdf-version "3.3.6")
1.7901- (existing-version (asdf-version)))
1.7902- (setf *asdf-version* asdf-version)
1.7903- (when (and existing-version (not (equal asdf-version existing-version)))
1.7904- (push existing-version *previous-asdf-versions*)
1.7905- (when (or *verbose-out* *load-verbose*)
1.7906- (format (or *verbose-out* *trace-output*)
1.7907- (compatfmt "~&~@<; ~@;Upgrading ASDF ~@[from version ~A ~]to version ~A~@:>~%")
1.7908- existing-version asdf-version)))))
1.7909-
1.7910-;;; Upon upgrade, specially frob some functions and classes that are being incompatibly redefined
1.7911-(when-upgrading ()
1.7912- (let* ((previous-version (first *previous-asdf-versions*))
1.7913- (redefined-functions ;; List of functions that changed incompatibly since 2.27:
1.7914- ;; gf signature changed, defun that became a generic function (but not way around),
1.7915- ;; method removed that will mess up with new ones
1.7916- ;; (especially :around :before :after, more specific or call-next-method'ed method)
1.7917- ;; and/or semantics otherwise modified. Oops.
1.7918- ;; NB: it's too late to do anything about functions in UIOP!
1.7919- ;; If you introduce some critical incompatibility there, you MUST change the function name.
1.7920- ;; Note that we don't need do anything about functions that changed incompatibly
1.7921- ;; from ASDF 2.26 or earlier: we wholly punt on the entire ASDF package in such an upgrade.
1.7922- ;; Also, the strong constraints apply most importantly for functions called from
1.7923- ;; the continuation of compiling or loading some of the code in ASDF or UIOP.
1.7924- ;; See discussion at https://gitlab.common-lisp.net/asdf/asdf/merge_requests/36
1.7925- ;; and at https://gitlab.common-lisp.net/asdf/asdf/-/merge_requests/141
1.7926- `(,@(when (version< previous-version "2.31") '(#:normalize-version)) ;; pathname became &key
1.7927- ,@(when (version< previous-version "3.1.2") '(#:component-depends-on #:input-files)) ;; crucial methods *removed* before 3.1.2
1.7928- ,@(when (version< previous-version "3.1.7.20") '(#:find-component)))) ;; added &key registered
1.7929- (redefined-classes
1.7930- ;; with the old ASDF during upgrade, and many implementations bork
1.7931- (when (or #+(or clozure mkcl) t)
1.7932- '((#:compile-concatenated-source-op (#:operation) ())
1.7933- (#:compile-bundle-op (#:operation) ())
1.7934- (#:concatenate-source-op (#:operation) ())
1.7935- (#:dll-op (#:operation) ())
1.7936- (#:lib-op (#:operation) ())
1.7937- (#:monolithic-compile-bundle-op (#:operation) ())
1.7938- (#:monolithic-concatenate-source-op (#:operation) ())))))
1.7939- (loop :for name :in redefined-functions
1.7940- :for sym = (find-symbol* name :asdf nil)
1.7941- :do (when sym (fmakunbound sym)))
1.7942- (labels ((asym (x) (multiple-value-bind (s p)
1.7943- (if (consp x) (values (car x) (cadr x)) (values x :asdf))
1.7944- (find-symbol* s p nil)))
1.7945- (asyms (l) (mapcar #'asym l)))
1.7946- (loop :for (name superclasses slots) :in redefined-classes
1.7947- :for sym = (find-symbol* name :asdf nil)
1.7948- :when (and sym (find-class sym))
1.7949- :do #+ccl (eval `(defclass ,sym ,(asyms superclasses) ,(asyms slots)))
1.7950- #-ccl (setf (find-class sym) nil))))) ;; mkcl
1.7951-
1.7952-;;; Self-upgrade functions
1.7953-(with-upgradability ()
1.7954- ;; This private function is called at the end of asdf/footer and ensures that,
1.7955- ;; *if* this loading of ASDF was an upgrade, then all registered cleanup functions will be called.
1.7956- (defun cleanup-upgraded-asdf (&optional (old-version (first *previous-asdf-versions*)))
1.7957- (let ((new-version (asdf-version)))
1.7958- (unless (equal old-version new-version)
1.7959- (push new-version *previous-asdf-versions*)
1.7960- (when (boundp '*asdf-upgraded-p*)
1.7961- (setf *asdf-upgraded-p* t))
1.7962- (when old-version
1.7963- (if (version<= new-version old-version)
1.7964- (error (compatfmt "~&~@<; ~@;Downgraded ASDF from version ~A to version ~A~@:>~%")
1.7965- old-version new-version)
1.7966- (asdf-message (compatfmt "~&~@<; ~@;Upgraded ASDF from version ~A to version ~A~@:>~%")
1.7967- old-version new-version))
1.7968- ;; In case the previous version was too old to be forward-compatible, clear systems.
1.7969- ;; TODO: if needed, we may have to define a separate hook to run
1.7970- ;; in case of forward-compatible upgrade.
1.7971- ;; Or to move the tests forward-compatibility test inside each hook function?
1.7972- (unless (version<= *oldest-forward-compatible-asdf-version* old-version)
1.7973- (call-functions (reverse *post-upgrade-cleanup-hook*)))
1.7974- t))))
1.7975-
1.7976- (defun upgrade-asdf ()
1.7977- "Try to upgrade of ASDF. If a different version was used, return T.
1.7978- We need do that before we operate on anything that may possibly depend on ASDF."
1.7979- (let ((*load-print* nil)
1.7980- (*compile-print* nil)
1.7981- (*asdf-upgraded-p* nil))
1.7982- (handler-bind (((or style-warning) #'muffle-warning))
1.7983- (symbol-call :asdf :load-system :asdf :verbose nil))
1.7984- *asdf-upgraded-p*))
1.7985-
1.7986- (defmacro with-asdf-deprecation ((&rest keys &key &allow-other-keys) &body body)
1.7987- `(with-upgradability ()
1.7988- (with-deprecation ((version-deprecation *asdf-version* ,@keys))
1.7989- ,@body))))
1.7990-;;;; -------------------------------------------------------------------------
1.7991-;;;; Session
1.7992-
1.7993-(uiop/package:define-package :asdf/session
1.7994- (:recycle :asdf/session :asdf/cache :asdf/component
1.7995- :asdf/action :asdf/find-system :asdf/plan :asdf)
1.7996- (:use :uiop/common-lisp :uiop :asdf/upgrade)
1.7997- (:export
1.7998- #:get-file-stamp #:compute-file-stamp #:register-file-stamp
1.7999- #:asdf-cache #:set-asdf-cache-entry #:unset-asdf-cache-entry #:consult-asdf-cache
1.8000- #:do-asdf-cache #:normalize-namestring
1.8001- #:call-with-asdf-session #:with-asdf-session
1.8002- #:*asdf-session* #:*asdf-session-class* #:session #:toplevel-asdf-session
1.8003- #:session-cache #:forcing #:asdf-upgraded-p
1.8004- #:visited-actions #:visiting-action-set #:visiting-action-list
1.8005- #:total-action-count #:planned-action-count #:planned-output-action-count
1.8006- #:clear-configuration-and-retry #:retry
1.8007- #:operate-level
1.8008- ;; conditions
1.8009- #:system-definition-error ;; top level, moved here because this is the earliest place for it.
1.8010- #:formatted-system-definition-error #:format-control #:format-arguments #:sysdef-error))
1.8011-(in-package :asdf/session)
1.8012-
1.8013-
1.8014-(with-upgradability ()
1.8015- ;; The session variable.
1.8016- ;; NIL when outside a session.
1.8017- (defvar *asdf-session* nil)
1.8018- (defparameter* *asdf-session-class* 'session
1.8019- "The default class for sessions")
1.8020-
1.8021- (defclass session ()
1.8022- (;; The ASDF session cache is used to memoize some computations.
1.8023- ;; It is instrumental in achieving:
1.8024- ;; * Consistency in the view of the world relied on by ASDF within a given session.
1.8025- ;; Inconsistencies in file stamps, system definitions, etc., could cause infinite loops
1.8026- ;; (a.k.a. stack overflows) and other erratic behavior.
1.8027- ;; * Speed and reliability of ASDF, with fewer side-effects from access to the filesystem, and
1.8028- ;; no expensive recomputations of transitive dependencies for input-files or output-files.
1.8029- ;; * Testability of ASDF with the ability to fake timestamps without actually touching files.
1.8030- (ancestor
1.8031- :initform nil :initarg :ancestor :reader session-ancestor
1.8032- :documentation "Top level session that this is part of")
1.8033- (session-cache
1.8034- :initform (make-hash-table :test 'equal) :initarg :session-cache :reader session-cache
1.8035- :documentation "Memoize expensive computations")
1.8036- (operate-level
1.8037- :initform 0 :initarg :operate-level :accessor session-operate-level
1.8038- :documentation "Number of nested calls to operate we're under (for toplevel session only)")
1.8039- ;; shouldn't the below be superseded by the session-wide caching of action-status
1.8040- ;; for (load-op "asdf") ?
1.8041- (asdf-upgraded-p
1.8042- :initform nil :initarg :asdf-upgraded-p :accessor asdf-upgraded-p
1.8043- :documentation "Was ASDF already upgraded in this session - only valid for toplevel-asdf-session.")
1.8044- (forcing
1.8045- :initform nil :initarg :forcing :accessor forcing
1.8046- :documentation "Forcing parameters for the session")
1.8047- ;; Table that to actions already visited while walking the dependencies associates status
1.8048- (visited-actions :initform (make-hash-table :test 'equal) :accessor visited-actions)
1.8049- ;; Actions that depend on those being currently walked through, to detect circularities
1.8050- (visiting-action-set ;; as a set
1.8051- :initform (make-hash-table :test 'equal) :accessor visiting-action-set)
1.8052- (visiting-action-list :initform () :accessor visiting-action-list) ;; as a list
1.8053- ;; Counts of total actions in plan
1.8054- (total-action-count :initform 0 :accessor total-action-count)
1.8055- ;; Count of actions that need to be performed
1.8056- (planned-action-count :initform 0 :accessor planned-action-count)
1.8057- ;; Count of actions that need to be performed that have a non-empty list of output-files.
1.8058- (planned-output-action-count :initform 0 :accessor planned-output-action-count))
1.8059- (:documentation "An ASDF session with a cache to memoize some computations"))
1.8060-
1.8061- (defun toplevel-asdf-session ()
1.8062- (when *asdf-session* (or (session-ancestor *asdf-session*) *asdf-session*)))
1.8063-
1.8064- (defun operate-level ()
1.8065- (session-operate-level (toplevel-asdf-session)))
1.8066-
1.8067- (defun (setf operate-level) (new-level)
1.8068- (setf (session-operate-level (toplevel-asdf-session)) new-level))
1.8069-
1.8070- (defun asdf-cache ()
1.8071- (session-cache *asdf-session*))
1.8072-
1.8073- ;; Set a session cache entry for KEY to a list of values VALUE-LIST, when inside a session.
1.8074- ;; Return those values.
1.8075- (defun set-asdf-cache-entry (key value-list)
1.8076- (values-list (if *asdf-session*
1.8077- (setf (gethash key (asdf-cache)) value-list)
1.8078- value-list)))
1.8079-
1.8080- ;; Unset the session cache entry for KEY, when inside a session.
1.8081- (defun unset-asdf-cache-entry (key)
1.8082- (when *asdf-session*
1.8083- (remhash key (session-cache *asdf-session*))))
1.8084-
1.8085- ;; Consult the session cache entry for KEY if present and in a session;
1.8086- ;; if not present, compute it by calling the THUNK,
1.8087- ;; and set the session cache entry accordingly, if in a session.
1.8088- ;; Return the values from the cache and/or the thunk computation.
1.8089- (defun consult-asdf-cache (key &optional thunk)
1.8090- (if *asdf-session*
1.8091- (multiple-value-bind (results foundp) (gethash key (session-cache *asdf-session*))
1.8092- (if foundp
1.8093- (values-list results)
1.8094- (set-asdf-cache-entry key (multiple-value-list (call-function thunk)))))
1.8095- (call-function thunk)))
1.8096-
1.8097- ;; Syntactic sugar for consult-asdf-cache
1.8098- (defmacro do-asdf-cache (key &body body)
1.8099- `(consult-asdf-cache ,key #'(lambda () ,@body)))
1.8100-
1.8101- ;; Compute inside a ASDF session with a cache.
1.8102- ;; First, make sure an ASDF session is underway, by binding the session cache variable
1.8103- ;; to a new hash-table if it's currently null (or even if it isn't, if OVERRIDE is true).
1.8104- ;; Second, if a new session was started, establish restarts for retrying the overall computation.
1.8105- ;; Finally, consult the cache if a KEY was specified with the THUNK as a fallback when the cache
1.8106- ;; entry isn't found, or just call the THUNK if no KEY was specified.
1.8107- (defun call-with-asdf-session (thunk &key override key override-cache override-forcing)
1.8108- (let ((fun (if key #'(lambda () (consult-asdf-cache key thunk)) thunk)))
1.8109- (if (and (not override) *asdf-session*)
1.8110- (funcall fun)
1.8111- (loop
1.8112- (restart-case
1.8113- (let ((*asdf-session*
1.8114- (apply 'make-instance *asdf-session-class*
1.8115- (when *asdf-session*
1.8116- `(:ancestor ,(toplevel-asdf-session)
1.8117- ,@(unless override-forcing
1.8118- `(:forcing ,(forcing *asdf-session*)))
1.8119- ,@(unless override-cache
1.8120- `(:session-cache ,(session-cache *asdf-session*))))))))
1.8121- (return (funcall fun)))
1.8122- (retry ()
1.8123- :report (lambda (s)
1.8124- (format s (compatfmt "~@<Retry ASDF operation.~@:>"))))
1.8125- (clear-configuration-and-retry ()
1.8126- :report (lambda (s)
1.8127- (format s (compatfmt "~@<Retry ASDF operation after resetting the configuration.~@:>")))
1.8128- (unless (null *asdf-session*)
1.8129- (clrhash (session-cache *asdf-session*)))
1.8130- (clear-configuration)))))))
1.8131-
1.8132- ;; Syntactic sugar for call-with-asdf-session
1.8133- (defmacro with-asdf-session ((&key key override override-cache override-forcing) &body body)
1.8134- `(call-with-asdf-session
1.8135- #'(lambda () ,@body)
1.8136- :override ,override :key ,key
1.8137- :override-cache ,override-cache :override-forcing ,override-forcing))
1.8138-
1.8139-
1.8140- ;;; Define specific accessor for file (date) stamp.
1.8141-
1.8142- ;; Normalize a namestring for use as a key in the session cache.
1.8143- (defun normalize-namestring (pathname)
1.8144- (let ((resolved (resolve-symlinks*
1.8145- (ensure-absolute-pathname
1.8146- (physicalize-pathname pathname)
1.8147- 'get-pathname-defaults))))
1.8148- (with-pathname-defaults () (namestring resolved))))
1.8149-
1.8150- ;; Compute the file stamp for a normalized namestring
1.8151- (defun compute-file-stamp (normalized-namestring)
1.8152- (with-pathname-defaults ()
1.8153- (or (safe-file-write-date normalized-namestring) t)))
1.8154-
1.8155- ;; Override the time STAMP associated to a given FILE in the session cache.
1.8156- ;; If no STAMP is specified, recompute a new one from the filesystem.
1.8157- (defun register-file-stamp (file &optional (stamp nil stampp))
1.8158- (let* ((namestring (normalize-namestring file))
1.8159- (stamp (if stampp stamp (compute-file-stamp namestring))))
1.8160- (set-asdf-cache-entry `(get-file-stamp ,namestring) (list stamp))))
1.8161-
1.8162- ;; Get or compute a memoized stamp for given FILE from the session cache.
1.8163- (defun get-file-stamp (file)
1.8164- (when file
1.8165- (let ((namestring (normalize-namestring file)))
1.8166- (do-asdf-cache `(get-file-stamp ,namestring) (compute-file-stamp namestring)))))
1.8167-
1.8168-
1.8169- ;;; Conditions
1.8170-
1.8171- (define-condition system-definition-error (error) ()
1.8172- ;; [this use of :report should be redundant, but unfortunately it's not.
1.8173- ;; cmucl's lisp::output-instance prefers the kernel:slot-class-print-function
1.8174- ;; over print-object; this is always conditions::%print-condition for
1.8175- ;; condition objects, which in turn does inheritance of :report options at
1.8176- ;; run-time. fortunately, inheritance means we only need this kludge here in
1.8177- ;; order to fix all conditions that build on it. -- rgr, 28-Jul-02.]
1.8178- #+cmucl (:report print-object))
1.8179-
1.8180- (define-condition formatted-system-definition-error (system-definition-error)
1.8181- ((format-control :initarg :format-control :reader format-control)
1.8182- (format-arguments :initarg :format-arguments :reader format-arguments))
1.8183- (:report (lambda (c s)
1.8184- (apply 'format s (format-control c) (format-arguments c)))))
1.8185-
1.8186- (defun sysdef-error (format &rest arguments)
1.8187- (error 'formatted-system-definition-error :format-control
1.8188- format :format-arguments arguments)))
1.8189-;;;; -------------------------------------------------------------------------
1.8190-;;;; Components
1.8191-
1.8192-(uiop/package:define-package :asdf/component
1.8193- (:recycle :asdf/component :asdf/find-component :asdf)
1.8194- (:use :uiop/common-lisp :uiop :asdf/upgrade :asdf/session)
1.8195- (:export
1.8196- #:component #:component-find-path
1.8197- #:find-component ;; methods defined in find-component
1.8198- #:component-name #:component-pathname #:component-relative-pathname
1.8199- #:component-parent #:component-system #:component-parent-pathname
1.8200- #:child-component #:parent-component #:module
1.8201- #:file-component
1.8202- #:source-file #:c-source-file #:java-source-file
1.8203- #:static-file #:doc-file #:html-file
1.8204- #:file-type
1.8205- #:source-file-type #:source-file-explicit-type ;; backward-compatibility
1.8206- #:component-in-order-to #:component-sideway-dependencies
1.8207- #:component-if-feature #:around-compile-hook
1.8208- #:component-description #:component-long-description
1.8209- #:component-version #:version-satisfies
1.8210- #:component-inline-methods ;; backward-compatibility only. DO NOT USE!
1.8211- #:component-operation-times ;; For internal use only.
1.8212- ;; portable ASDF encoding and implementation-specific external-format
1.8213- #:component-external-format #:component-encoding
1.8214- #:component-children-by-name #:component-children #:compute-children-by-name
1.8215- #:component-build-operation
1.8216- #:module-default-component-class
1.8217- #:module-components ;; backward-compatibility. DO NOT USE.
1.8218- #:sub-components
1.8219-
1.8220- ;; conditions
1.8221- #:duplicate-names
1.8222-
1.8223- ;; Internals we'd like to share with the ASDF package, especially for upgrade purposes
1.8224- #:name #:version #:description #:long-description #:author #:maintainer #:licence
1.8225- #:components-by-name #:components #:children #:children-by-name
1.8226- #:default-component-class #:source-file
1.8227- #:defsystem-depends-on ; This symbol retained for backward compatibility.
1.8228- #:sideway-dependencies #:if-feature #:in-order-to #:inline-methods
1.8229- #:relative-pathname #:absolute-pathname #:operation-times #:around-compile
1.8230- #:%encoding #:properties #:component-properties #:parent))
1.8231-(in-package :asdf/component)
1.8232-
1.8233-(with-upgradability ()
1.8234- (defgeneric component-name (component)
1.8235- (:documentation "Name of the COMPONENT, unique relative to its parent"))
1.8236- (defgeneric component-system (component)
1.8237- (:documentation "Top-level system containing the COMPONENT"))
1.8238- (defgeneric component-pathname (component)
1.8239- (:documentation "Pathname of the COMPONENT if any, or NIL."))
1.8240- (defgeneric component-relative-pathname (component)
1.8241- ;; in ASDF4, rename that to component-specified-pathname ?
1.8242- (:documentation "Specified pathname of the COMPONENT,
1.8243-intended to be merged with the pathname of that component's parent if any, using merged-pathnames*.
1.8244-Despite the function's name, the return value can be an absolute pathname, in which case the merge
1.8245-will leave it unmodified."))
1.8246- (defgeneric component-external-format (component)
1.8247- (:documentation "The external-format of the COMPONENT.
1.8248-By default, deduced from the COMPONENT-ENCODING."))
1.8249- (defgeneric component-encoding (component)
1.8250- (:documentation "The encoding of the COMPONENT. By default, only :utf-8 is supported.
1.8251-Use asdf-encodings to support more encodings."))
1.8252- (defgeneric version-satisfies (component version)
1.8253- (:documentation "Check whether a COMPONENT satisfies the constraint of being at least as recent
1.8254-as the specified VERSION, which must be a string of dot-separated natural numbers, or NIL."))
1.8255- (defgeneric component-version (component)
1.8256- (:documentation "Return the version of a COMPONENT, which must be a string of dot-separated
1.8257-natural numbers, or NIL."))
1.8258- (defgeneric (setf component-version) (new-version component)
1.8259- (:documentation "Updates the version of a COMPONENT, which must be a string of dot-separated
1.8260-natural numbers, or NIL."))
1.8261- (defgeneric component-parent (component)
1.8262- (:documentation "The parent of a child COMPONENT,
1.8263-or NIL for top-level components (a.k.a. systems)"))
1.8264- ;; NIL is a designator for the absence of a component, in which case the parent is also absent.
1.8265- (defmethod component-parent ((component null)) nil)
1.8266-
1.8267- ;; Deprecated: Backward compatible way of computing the FILE-TYPE of a component.
1.8268- (with-asdf-deprecation (:style-warning "3.4")
1.8269- (defgeneric source-file-type (component system)
1.8270- (:documentation "DEPRECATED. Use the FILE-TYPE of a COMPONENT instead.")))
1.8271-
1.8272- (define-condition duplicate-names (system-definition-error)
1.8273- ((name :initarg :name :reader duplicate-names-name))
1.8274- (:report (lambda (c s)
1.8275- (format s (compatfmt "~@<Error while defining system: multiple components are given same name ~S~@:>")
1.8276- (duplicate-names-name c))))))
1.8277-
1.8278-
1.8279-(with-upgradability ()
1.8280- (defclass component ()
1.8281- ((name :accessor component-name :initarg :name :type string :documentation
1.8282- "Component name: designator for a string composed of portable pathname characters")
1.8283- ;; We might want to constrain version with
1.8284- ;; :type (and string (satisfies parse-version))
1.8285- ;; but we cannot until we fix all systems that don't use it correctly!
1.8286- (version :accessor component-version :initarg :version :initform nil)
1.8287- (description :accessor component-description :initarg :description :initform nil)
1.8288- (long-description :accessor component-long-description :initarg :long-description :initform nil)
1.8289- (sideway-dependencies :accessor component-sideway-dependencies :initform nil)
1.8290- (if-feature :accessor component-if-feature :initform nil :initarg :if-feature)
1.8291- ;; In the ASDF object model, dependencies exist between *actions*,
1.8292- ;; where an action is a pair of an operation and a component.
1.8293- ;; Dependencies are represented as alists of operations
1.8294- ;; to a list where each entry is a pair of an operation and a list of component specifiers.
1.8295- ;; Up until ASDF 2.26.9, there used to be two kinds of dependencies:
1.8296- ;; in-order-to and do-first, each stored in its own slot. Now there is only in-order-to.
1.8297- ;; in-order-to used to represent things that modify the filesystem (such as compiling a fasl)
1.8298- ;; and do-first things that modify the current image (such as loading a fasl).
1.8299- ;; These are now unified because we now correctly propagate timestamps between dependencies.
1.8300- ;; Happily, no one seems to have used do-first too much (especially since until ASDF 2.017,
1.8301- ;; anything you specified was overridden by ASDF itself anyway), but the name in-order-to remains.
1.8302- ;; The names are bad, but they have been the official API since Dan Barlow's ASDF 1.52!
1.8303- ;; LispWorks's defsystem has caused-by and requires for in-order-to and do-first respectively.
1.8304- ;; Maybe rename the slots in ASDF? But that's not very backward-compatible.
1.8305- ;; See our ASDF 2 paper for more complete explanations.
1.8306- (in-order-to :initform nil :initarg :in-order-to
1.8307- :accessor component-in-order-to)
1.8308- ;; Methods defined using the "inline" style inside a defsystem form:
1.8309- ;; we store them here so we can delete them when the system is re-evaluated.
1.8310- (inline-methods :accessor component-inline-methods :initform nil)
1.8311- ;; ASDF4: rename it from relative-pathname to specified-pathname. It need not be relative.
1.8312- ;; There is no initform and no direct accessor for this specified pathname,
1.8313- ;; so we only access the information through appropriate methods, after it has been processed.
1.8314- ;; Unhappily, some braindead systems directly access the slot. Make them stop before ASDF4.
1.8315- (relative-pathname :initarg :pathname)
1.8316- ;; The absolute-pathname is computed based on relative-pathname and parent pathname.
1.8317- ;; The slot is but a cache used by component-pathname.
1.8318- (absolute-pathname)
1.8319- (operation-times :initform (make-hash-table)
1.8320- :accessor component-operation-times)
1.8321- (around-compile :initarg :around-compile)
1.8322- ;; Properties are for backward-compatibility with ASDF2 only. DO NOT USE!
1.8323- (properties :accessor component-properties :initarg :properties
1.8324- :initform nil)
1.8325- (%encoding :accessor %component-encoding :initform nil :initarg :encoding)
1.8326- ;; For backward-compatibility, this slot is part of component rather than of child-component. ASDF4: stop it.
1.8327- (parent :initarg :parent :initform nil :reader component-parent)
1.8328- (build-operation
1.8329- :initarg :build-operation :initform nil :reader component-build-operation)
1.8330- ;; Cache for ADDITIONAL-INPUT-FILES function.
1.8331- (additional-input-files :accessor %additional-input-files :initform nil))
1.8332- (:documentation "Base class for all components of a build"))
1.8333-
1.8334- (defgeneric find-component (base path &key registered)
1.8335- (:documentation "Find a component by resolving the PATH starting from BASE parent.
1.8336-If REGISTERED is true, only search currently registered systems."))
1.8337-
1.8338- (defun component-find-path (component)
1.8339- "Return a path from a root system to the COMPONENT.
1.8340-The return value is a list of component NAMES; a list of strings."
1.8341- (check-type component (or null component))
1.8342- (reverse
1.8343- (loop :for c = component :then (component-parent c)
1.8344- :while c :collect (component-name c))))
1.8345-
1.8346- (defmethod print-object ((c component) stream)
1.8347- (print-unreadable-object (c stream :type t :identity nil)
1.8348- (format stream "~{~S~^ ~}" (component-find-path c))))
1.8349-
1.8350- (defmethod component-system ((component component))
1.8351- (if-let (system (component-parent component))
1.8352- (component-system system)
1.8353- component)))
1.8354-
1.8355-
1.8356-;;;; Component hierarchy within a system
1.8357-;; The tree typically but not necessarily follows the filesystem hierarchy.
1.8358-(with-upgradability ()
1.8359- (defclass child-component (component) ()
1.8360- (:documentation "A CHILD-COMPONENT is a COMPONENT that may be part of
1.8361-a PARENT-COMPONENT."))
1.8362-
1.8363- (defclass file-component (child-component)
1.8364- ((type :accessor file-type :initarg :type)) ; no default
1.8365- (:documentation "a COMPONENT that represents a file"))
1.8366- (defclass source-file (file-component)
1.8367- ((type :accessor source-file-explicit-type ;; backward-compatibility
1.8368- :initform nil))) ;; NB: many systems have come to rely on this default.
1.8369- (defclass c-source-file (source-file)
1.8370- ((type :initform "c")))
1.8371- (defclass java-source-file (source-file)
1.8372- ((type :initform "java")))
1.8373- (defclass static-file (source-file)
1.8374- ((type :initform nil))
1.8375- (:documentation "Component for a file to be included as is in the build output"))
1.8376- (defclass doc-file (static-file) ())
1.8377- (defclass html-file (doc-file)
1.8378- ((type :initform "html")))
1.8379-
1.8380- (defclass parent-component (component)
1.8381- ((children
1.8382- :initform nil
1.8383- :initarg :components
1.8384- :reader module-components ; backward-compatibility
1.8385- :accessor component-children)
1.8386- (children-by-name
1.8387- :reader module-components-by-name ; backward-compatibility
1.8388- :accessor component-children-by-name)
1.8389- (default-component-class
1.8390- :initform nil
1.8391- :initarg :default-component-class
1.8392- :accessor module-default-component-class))
1.8393- (:documentation "A PARENT-COMPONENT is a component that may have children.")))
1.8394-
1.8395-(with-upgradability ()
1.8396- ;; (Private) Function that given a PARENT component,
1.8397- ;; the list of children of which has been initialized,
1.8398- ;; compute the hash-table in slot children-by-name that allows to retrieve its children by name.
1.8399- ;; If ONLY-IF-NEEDED-P is defined, skip any (re)computation if the slot is already populated.
1.8400- (defun compute-children-by-name (parent &key only-if-needed-p)
1.8401- (unless (and only-if-needed-p (slot-boundp parent 'children-by-name))
1.8402- (let ((hash (make-hash-table :test 'equal)))
1.8403- (setf (component-children-by-name parent) hash)
1.8404- (loop :for c :in (component-children parent)
1.8405- :for name = (component-name c)
1.8406- :for previous = (gethash name hash)
1.8407- :do (when previous (error 'duplicate-names :name name))
1.8408- (setf (gethash name hash) c))
1.8409- hash))))
1.8410-
1.8411-(with-upgradability ()
1.8412- (defclass module (child-component parent-component)
1.8413- (#+clisp (components)) ;; backward compatibility during upgrade only
1.8414- (:documentation "A module is a intermediate component with both a parent and children,
1.8415-typically but not necessarily representing the files in a subdirectory of the build source.")))
1.8416-
1.8417-
1.8418-;;;; component pathnames
1.8419-(with-upgradability ()
1.8420- (defgeneric component-parent-pathname (component)
1.8421- (:documentation "The pathname of the COMPONENT's parent, if any, or NIL"))
1.8422- (defmethod component-parent-pathname (component)
1.8423- (component-pathname (component-parent component)))
1.8424-
1.8425- ;; The default method for component-pathname tries to extract a cached precomputed
1.8426- ;; absolute-pathname from the relevant slot, and if not, computes it by merging the
1.8427- ;; component-relative-pathname (which should be component-specified-pathname, it can be absolute)
1.8428- ;; with the directory of the component-parent-pathname.
1.8429- (defmethod component-pathname ((component component))
1.8430- (if (slot-boundp component 'absolute-pathname)
1.8431- (slot-value component 'absolute-pathname)
1.8432- (let ((pathname
1.8433- (merge-pathnames*
1.8434- (component-relative-pathname component)
1.8435- (pathname-directory-pathname (component-parent-pathname component)))))
1.8436- (unless (or (null pathname) (absolute-pathname-p pathname))
1.8437- (error (compatfmt "~@<Invalid relative pathname ~S for component ~S~@:>")
1.8438- pathname (component-find-path component)))
1.8439- (setf (slot-value component 'absolute-pathname) pathname)
1.8440- pathname)))
1.8441-
1.8442- ;; Default method for component-relative-pathname:
1.8443- ;; combine the contents of slot relative-pathname (from specified initarg :pathname)
1.8444- ;; with the appropriate source-file-type, which defaults to the file-type of the component.
1.8445- (defmethod component-relative-pathname ((component component))
1.8446- ;; SOURCE-FILE-TYPE below is strictly for backward-compatibility with ASDF1.
1.8447- ;; We ought to be able to extract this from the component alone with FILE-TYPE.
1.8448- ;; TODO: track who uses it in Quicklisp, and have them not use it anymore;
1.8449- ;; maybe issue a WARNING (then eventually CERROR) if the two methods diverge?
1.8450- (let (#+abcl
1.8451- (parent
1.8452- (component-parent-pathname component)))
1.8453- (parse-unix-namestring
1.8454- (or (and (slot-boundp component 'relative-pathname)
1.8455- (slot-value component 'relative-pathname))
1.8456- (component-name component))
1.8457- :want-relative
1.8458- #-abcl t
1.8459- ;; JAR-PATHNAMES always have absolute directories
1.8460- #+abcl (not (ext:pathname-jar-p parent))
1.8461- :type (source-file-type component (component-system component))
1.8462- :defaults (component-parent-pathname component))))
1.8463-
1.8464- (defmethod source-file-type ((component parent-component) (system parent-component))
1.8465- :directory)
1.8466-
1.8467- (defmethod source-file-type ((component file-component) (system parent-component))
1.8468- (file-type component)))
1.8469-
1.8470-
1.8471-;;;; Encodings
1.8472-(with-upgradability ()
1.8473- (defmethod component-encoding ((c component))
1.8474- (or (loop :for x = c :then (component-parent x)
1.8475- :while x :thereis (%component-encoding x))
1.8476- (detect-encoding (component-pathname c))))
1.8477-
1.8478- (defmethod component-external-format ((c component))
1.8479- (encoding-external-format (component-encoding c))))
1.8480-
1.8481-
1.8482-;;;; around-compile-hook
1.8483-(with-upgradability ()
1.8484- (defgeneric around-compile-hook (component)
1.8485- (:documentation "An optional hook function that will be called with one argument, a thunk.
1.8486-The hook function must call the thunk, that will compile code from the component, and may or may not
1.8487-also evaluate the compiled results. The hook function may establish dynamic variable bindings around
1.8488-this compilation, or check its results, etc."))
1.8489- (defmethod around-compile-hook ((c component))
1.8490- (cond
1.8491- ((slot-boundp c 'around-compile)
1.8492- (slot-value c 'around-compile))
1.8493- ((component-parent c)
1.8494- (around-compile-hook (component-parent c))))))
1.8495-
1.8496-
1.8497-;;;; version-satisfies
1.8498-(with-upgradability ()
1.8499- ;; short-circuit testing of null version specifications.
1.8500- ;; this is an all-pass, without warning
1.8501- (defmethod version-satisfies :around ((c t) (version null))
1.8502- t)
1.8503- (defmethod version-satisfies ((c component) version)
1.8504- (unless (and version (slot-boundp c 'version) (component-version c))
1.8505- (when version
1.8506- (warn "Requested version ~S but ~S has no version" version c))
1.8507- (return-from version-satisfies nil))
1.8508- (version-satisfies (component-version c) version))
1.8509-
1.8510- (defmethod version-satisfies ((cver string) version)
1.8511- (version<= version cver)))
1.8512-
1.8513-
1.8514-;;; all sub-components (of a given type)
1.8515-(with-upgradability ()
1.8516- (defun sub-components (component &key (type t))
1.8517- "Compute the transitive sub-components of given COMPONENT that are of given TYPE"
1.8518- (while-collecting (c)
1.8519- (labels ((recurse (x)
1.8520- (when (if-let (it (component-if-feature x)) (featurep it) t)
1.8521- (when (typep x type)
1.8522- (c x))
1.8523- (when (typep x 'parent-component)
1.8524- (map () #'recurse (component-children x))))))
1.8525- (recurse component)))))
1.8526-
1.8527-;;;; -------------------------------------------------------------------------
1.8528-;;;; Operations
1.8529-
1.8530-(uiop/package:define-package :asdf/operation
1.8531- (:recycle :asdf/operation :asdf/action :asdf) ;; asdf/action for FEATURE pre 2.31.5.
1.8532- (:use :uiop/common-lisp :uiop :asdf/upgrade :asdf/session)
1.8533- (:export
1.8534- #:operation
1.8535- #:*operations* #:make-operation #:find-operation
1.8536- #:feature)) ;; TODO: stop exporting the deprecated FEATURE feature.
1.8537-(in-package :asdf/operation)
1.8538-
1.8539-;;; Operation Classes
1.8540-(when-upgrading (:version "2.27" :when (find-class 'operation nil))
1.8541- ;; override any obsolete shared-initialize method when upgrading from ASDF2.
1.8542- (defmethod shared-initialize :after ((o operation) (slot-names t) &key)
1.8543- (values)))
1.8544-
1.8545-(with-upgradability ()
1.8546- (defclass operation ()
1.8547- ()
1.8548- (:documentation "The base class for all ASDF operations.
1.8549-
1.8550-ASDF does NOT and never did distinguish between multiple operations of the same class.
1.8551-Therefore, all slots of all operations MUST have :allocation :class and no initargs. No exceptions.
1.8552-"))
1.8553-
1.8554- (defvar *in-make-operation* nil)
1.8555-
1.8556- (defun check-operation-constructor ()
1.8557- "Enforce that OPERATION instances must be created with MAKE-OPERATION."
1.8558- (unless *in-make-operation*
1.8559- (sysdef-error "OPERATION instances must only be created through MAKE-OPERATION.")))
1.8560-
1.8561- (defmethod print-object ((o operation) stream)
1.8562- (print-unreadable-object (o stream :type t :identity nil)))
1.8563-
1.8564- ;;; Override previous methods (from 3.1.7 and earlier) and add proper error checking.
1.8565- #-genera ;; Genera adds its own system initargs, e.g. clos-internals:storage-area 8
1.8566- (defmethod initialize-instance :after ((o operation) &rest initargs &key &allow-other-keys)
1.8567- (unless (null initargs)
1.8568- (parameter-error "~S does not accept initargs" 'operation))))
1.8569-
1.8570-
1.8571-;;; make-operation, find-operation
1.8572-
1.8573-(with-upgradability ()
1.8574- ;; A table to memoize instances of a given operation. There shall be only one.
1.8575- (defparameter* *operations* (make-hash-table :test 'equal))
1.8576-
1.8577- ;; A memoizing way of creating instances of operation.
1.8578- (defun make-operation (operation-class)
1.8579- "This function creates and memoizes an instance of OPERATION-CLASS.
1.8580-All operation instances MUST be created through this function.
1.8581-
1.8582-Use of INITARGS is not supported at this time."
1.8583- (let ((class (coerce-class operation-class
1.8584- :package :asdf/interface :super 'operation :error 'sysdef-error))
1.8585- (*in-make-operation* t))
1.8586- (ensure-gethash class *operations* `(make-instance ,class))))
1.8587-
1.8588- ;; This function is mostly for backward and forward compatibility:
1.8589- ;; operations used to preserve the operation-original-initargs of the context,
1.8590- ;; and may in the future preserve some operation-canonical-initargs.
1.8591- ;; Still, the treatment of NIL as a disabling context is useful in some cases.
1.8592- (defgeneric find-operation (context spec)
1.8593- (:documentation "Find an operation by resolving the SPEC in the CONTEXT"))
1.8594- (defmethod find-operation ((context t) (spec operation))
1.8595- spec)
1.8596- (defmethod find-operation ((context t) (spec symbol))
1.8597- (when spec ;; NIL designates itself, i.e. absence of operation
1.8598- (make-operation spec))) ;; TODO: preserve the (operation-canonical-initargs context)
1.8599- (defmethod find-operation ((context t) (spec string))
1.8600- (make-operation spec))) ;; TODO: preserve the (operation-canonical-initargs context)
1.8601-
1.8602-;;;; -------------------------------------------------------------------------
1.8603-;;;; Systems
1.8604-
1.8605-(uiop/package:define-package :asdf/system
1.8606- (:recycle :asdf :asdf/system :asdf/find-system)
1.8607- (:use :uiop/common-lisp :uiop :asdf/upgrade :asdf/session :asdf/component)
1.8608- (:export
1.8609- #:system #:proto-system #:undefined-system #:reset-system-class
1.8610- #:system-source-file #:system-source-directory #:system-relative-pathname
1.8611- #:system-description #:system-long-description
1.8612- #:system-author #:system-maintainer #:system-licence #:system-license
1.8613- #:system-version
1.8614- #:definition-dependency-list #:definition-dependency-set #:system-defsystem-depends-on
1.8615- #:system-depends-on #:system-weakly-depends-on
1.8616- #:component-build-pathname #:build-pathname
1.8617- #:component-entry-point #:entry-point
1.8618- #:homepage #:system-homepage
1.8619- #:bug-tracker #:system-bug-tracker
1.8620- #:mailto #:system-mailto
1.8621- #:long-name #:system-long-name
1.8622- #:source-control #:system-source-control
1.8623- #:coerce-name #:primary-system-name #:primary-system-p #:coerce-filename
1.8624- #:find-system #:builtin-system-p)) ;; forward-reference, defined in find-system
1.8625-(in-package :asdf/system)
1.8626-
1.8627-(with-upgradability ()
1.8628- ;; The method is actually defined in asdf/find-system,
1.8629- ;; but we declare the function here to avoid a forward reference.
1.8630- (defgeneric find-system (system &optional error-p)
1.8631- (:documentation "Given a system designator, find the actual corresponding system object.
1.8632-If no system is found, then signal an error if ERROR-P is true (the default), or else return NIL.
1.8633-A system designator is usually a string (conventionally all lowercase) or a symbol, designating
1.8634-the same system as its downcased name; it can also be a system object (designating itself)."))
1.8635-
1.8636- (defgeneric system-source-file (system)
1.8637- (:documentation "Return the source file in which system is defined."))
1.8638-
1.8639- ;; This is bad design, but was the easiest kluge I found to let the user specify that
1.8640- ;; some special actions create outputs at locations controled by the user that are not affected
1.8641- ;; by the usual output-translations.
1.8642- ;; TODO: Fix operate to stop passing flags to operation (which in the current design shouldn't
1.8643- ;; have any flags, since the stamp cache, etc., can't distinguish them), and instead insert
1.8644- ;; *there* the ability of specifying special output paths, not in the system definition.
1.8645- (defgeneric component-build-pathname (component)
1.8646- (:documentation "The COMPONENT-BUILD-PATHNAME, when defined and not null, specifies the
1.8647-output pathname for the action using the COMPONENT-BUILD-OPERATION.
1.8648-
1.8649-NB: This interface is subject to change. Please contact ASDF maintainers if you use it."))
1.8650-
1.8651- ;; TODO: Should this have been made a SYSTEM-ENTRY-POINT instead?
1.8652- (defgeneric component-entry-point (component)
1.8653- (:documentation "The COMPONENT-ENTRY-POINT, when defined, specifies what function to call
1.8654-(with no argument) when running an image dumped from the COMPONENT.
1.8655-
1.8656-NB: This interface is subject to change. Please contact ASDF maintainers if you use it."))
1.8657-
1.8658- (defmethod component-entry-point ((c component))
1.8659- nil))
1.8660-
1.8661-
1.8662-;;;; The system class
1.8663-
1.8664-(with-upgradability ()
1.8665- (defclass proto-system () ; slots to keep when resetting a system
1.8666- ;; To preserve identity for all objects, we'd need keep the components slots
1.8667- ;; but also to modify parse-component-form to reset the recycled objects.
1.8668- ((name)
1.8669- (source-file)
1.8670- ;; These two slots contains the *inferred* dependencies of define-op,
1.8671- ;; from loading the .asd file, as list and as set.
1.8672- (definition-dependency-list
1.8673- :initform nil :accessor definition-dependency-list)
1.8674- (definition-dependency-set
1.8675- :initform (list-to-hash-set nil) :accessor definition-dependency-set))
1.8676- (:documentation "PROTO-SYSTEM defines the elements of identity that are preserved when
1.8677-a SYSTEM is redefined and its class is modified."))
1.8678-
1.8679- (defclass system (module proto-system)
1.8680- ;; Backward-compatibility: inherit from module. ASDF4: only inherit from parent-component.
1.8681- (;; {,long-}description is now inherited from component, but we add the legacy accessors
1.8682- (description :writer (setf system-description))
1.8683- (long-description :writer (setf system-long-description))
1.8684- (author :writer (setf system-author) :initarg :author :initform nil)
1.8685- (maintainer :writer (setf system-maintainer) :initarg :maintainer :initform nil)
1.8686- (licence :writer (setf system-licence) :initarg :licence
1.8687- :writer (setf system-license) :initarg :license
1.8688- :initform nil)
1.8689- (homepage :writer (setf system-homepage) :initarg :homepage :initform nil)
1.8690- (bug-tracker :writer (setf system-bug-tracker) :initarg :bug-tracker :initform nil)
1.8691- (mailto :writer (setf system-mailto) :initarg :mailto :initform nil)
1.8692- (long-name :writer (setf system-long-name) :initarg :long-name :initform nil)
1.8693- ;; Conventions for this slot aren't clear yet as of ASDF 2.27, but whenever they are, they will be enforced.
1.8694- ;; I'm introducing the slot before the conventions are set for maximum compatibility.
1.8695- (source-control :writer (setf system-source-control) :initarg :source-control :initform nil)
1.8696-
1.8697- (builtin-system-p :accessor builtin-system-p :initform nil :initarg :builtin-system-p)
1.8698- (build-pathname
1.8699- :initform nil :initarg :build-pathname :accessor component-build-pathname)
1.8700- (entry-point
1.8701- :initform nil :initarg :entry-point :accessor component-entry-point)
1.8702- (source-file :initform nil :initarg :source-file :accessor system-source-file)
1.8703- ;; This slot contains the *declared* defsystem-depends-on dependencies
1.8704- (defsystem-depends-on :reader system-defsystem-depends-on :initarg :defsystem-depends-on
1.8705- :initform nil)
1.8706- ;; these two are specially set in parse-component-form, so have no :INITARGs.
1.8707- (depends-on :reader system-depends-on :initform nil)
1.8708- (weakly-depends-on :reader system-weakly-depends-on :initform nil))
1.8709- (:documentation "SYSTEM is the base class for top-level components that users may request
1.8710-ASDF to build."))
1.8711-
1.8712- (defclass undefined-system (system) ()
1.8713- (:documentation "System that was not defined yet."))
1.8714-
1.8715- (defun reset-system-class (system new-class &rest keys &key &allow-other-keys)
1.8716- "Erase any data from a SYSTEM except its basic identity, then reinitialize it
1.8717-based on supplied KEYS."
1.8718- (change-class (change-class system 'proto-system) new-class)
1.8719- (apply 'reinitialize-instance system keys)))
1.8720-
1.8721-
1.8722-;;; Canonicalizing system names
1.8723-
1.8724-(with-upgradability ()
1.8725- (defun coerce-name (name)
1.8726- "Given a designator for a component NAME, return the name as a string.
1.8727-The designator can be a COMPONENT (designing its name; note that a SYSTEM is a component),
1.8728-a SYMBOL (designing its name, downcased), or a STRING (designing itself)."
1.8729- (typecase name
1.8730- (component (component-name name))
1.8731- (symbol (string-downcase name))
1.8732- (string name)
1.8733- (t (sysdef-error (compatfmt "~@<Invalid component designator: ~3i~_~A~@:>") name))))
1.8734-
1.8735- (defun primary-system-name (system-designator)
1.8736- "Given a system designator NAME, return the name of the corresponding
1.8737-primary system, after which the .asd file in which it is defined is named.
1.8738-If given a string or symbol (to downcase), do it syntactically
1.8739- by stripping anything from the first slash on.
1.8740-If given a component, do it semantically by extracting
1.8741-the system-primary-system-name of its system from its source-file if any,
1.8742-falling back to the syntactic criterion if none."
1.8743- (etypecase system-designator
1.8744- (string (if-let (p (position #\/ system-designator))
1.8745- (subseq system-designator 0 p) system-designator))
1.8746- (symbol (primary-system-name (coerce-name system-designator)))
1.8747- (component (let* ((system (component-system system-designator))
1.8748- (source-file (physicalize-pathname (system-source-file system))))
1.8749- (if source-file
1.8750- (and (equal (pathname-type source-file) "asd")
1.8751- (pathname-name source-file))
1.8752- (primary-system-name (component-name system)))))))
1.8753-
1.8754- (defun primary-system-p (system)
1.8755- "Given a system designator SYSTEM, return T if it designates a primary system, or else NIL.
1.8756-If given a string, do it syntactically and return true if the name does not contain a slash.
1.8757-If given a symbol, downcase to a string then fallback to previous case (NB: for NIL return T).
1.8758-If given a component, do it semantically and return T if it's a SYSTEM and its primary-system-name
1.8759-is the same as its component-name."
1.8760- (etypecase system
1.8761- (string (not (find #\/ system)))
1.8762- (symbol (primary-system-p (coerce-name system)))
1.8763- (component (and (typep system 'system)
1.8764- (equal (component-name system) (primary-system-name system))))))
1.8765-
1.8766- (defun coerce-filename (name)
1.8767- "Coerce a system designator NAME into a string suitable as a filename component.
1.8768-The (current) transformation is to replace characters /:\\ each by --,
1.8769-the former being forbidden in a filename component.
1.8770-NB: The onus is unhappily on the user to avoid clashes."
1.8771- (frob-substrings (coerce-name name) '("/" ":" "\\") "--")))
1.8772-
1.8773-
1.8774-;;; System virtual slot readers, recursing to the primary system if needed.
1.8775-(with-upgradability ()
1.8776- (defvar *system-virtual-slots* '(long-name description long-description
1.8777- author maintainer mailto
1.8778- homepage source-control
1.8779- licence version bug-tracker)
1.8780- "The list of system virtual slot names.")
1.8781- (defun system-virtual-slot-value (system slot-name)
1.8782- "Return SYSTEM's virtual SLOT-NAME value.
1.8783-If SYSTEM's SLOT-NAME value is NIL and SYSTEM is a secondary system, look in
1.8784-the primary one."
1.8785- (or (slot-value system slot-name)
1.8786- (unless (primary-system-p system)
1.8787- (slot-value (find-system (primary-system-name system))
1.8788- slot-name))))
1.8789- (defmacro define-system-virtual-slot-reader (slot-name)
1.8790- (let ((name (intern (strcat (string :system-) (string slot-name)))))
1.8791- `(progn
1.8792- (fmakunbound ',name) ;; These were gf from defgeneric before 3.3.2.11
1.8793- (declaim (notinline ,name))
1.8794- (defun ,name (system) (system-virtual-slot-value system ',slot-name)))))
1.8795- (defmacro define-system-virtual-slot-readers ()
1.8796- `(progn ,@(mapcar (lambda (slot-name)
1.8797- `(define-system-virtual-slot-reader ,slot-name))
1.8798- *system-virtual-slots*)))
1.8799- (define-system-virtual-slot-readers)
1.8800- (defun system-license (system)
1.8801- (system-virtual-slot-value system 'licence)))
1.8802-
1.8803-
1.8804-;;;; Pathnames
1.8805-
1.8806-(with-upgradability ()
1.8807- ;; Resolve a system designator to a system before extracting its system-source-file
1.8808- (defmethod system-source-file ((system-name string))
1.8809- (system-source-file (find-system system-name)))
1.8810- (defmethod system-source-file ((system-name symbol))
1.8811- (when system-name
1.8812- (system-source-file (find-system system-name))))
1.8813-
1.8814- (defun system-source-directory (system-designator)
1.8815- "Return a pathname object corresponding to the directory
1.8816-in which the system specification (.asd file) is located."
1.8817- (pathname-directory-pathname (system-source-file system-designator)))
1.8818-
1.8819- (defun system-relative-pathname (system name &key type)
1.8820- "Given a SYSTEM, and a (Unix-style relative path) NAME of a file (or directory) of given TYPE,
1.8821-return the absolute pathname of a corresponding file under that system's source code pathname."
1.8822- (subpathname (system-source-directory system) name :type type))
1.8823-
1.8824- (defmethod component-pathname ((system system))
1.8825- "Given a SYSTEM, and a (Unix-style relative path) NAME of a file (or directory) of given TYPE,
1.8826-return the absolute pathname of a corresponding file under that system's source code pathname."
1.8827- (let ((pathname (or (call-next-method) (system-source-directory system))))
1.8828- (unless (and (slot-boundp system 'relative-pathname) ;; backward-compatibility with ASDF1-age
1.8829- (slot-value system 'relative-pathname)) ;; systems that directly access this slot.
1.8830- (setf (slot-value system 'relative-pathname) pathname))
1.8831- pathname))
1.8832-
1.8833- ;; The default method of component-relative-pathname for a system:
1.8834- ;; if a pathname was specified in the .asd file, it must be relative to the .asd file
1.8835- ;; (actually, to its truename* if *resolve-symlinks* it true, the default).
1.8836- ;; The method will return an *absolute* pathname, once again showing that the historical name
1.8837- ;; component-relative-pathname is misleading and should have been component-specified-pathname.
1.8838- (defmethod component-relative-pathname ((system system))
1.8839- (parse-unix-namestring
1.8840- (and (slot-boundp system 'relative-pathname)
1.8841- (slot-value system 'relative-pathname))
1.8842- :want-relative t
1.8843- :type :directory
1.8844- :ensure-absolute t
1.8845- :defaults (system-source-directory system)))
1.8846-
1.8847- ;; A system has no parent; if some method wants to make a path "relative to its parent",
1.8848- ;; it will instead be relative to the system itself.
1.8849- (defmethod component-parent-pathname ((system system))
1.8850- (system-source-directory system))
1.8851-
1.8852- ;; Most components don't have a specified component-build-pathname, and therefore
1.8853- ;; no magic redirection of their output that disregards the output-translations.
1.8854- (defmethod component-build-pathname ((c component))
1.8855- nil))
1.8856-
1.8857-;;;; -------------------------------------------------------------------------
1.8858-;;;; Finding systems
1.8859-
1.8860-(uiop/package:define-package :asdf/system-registry
1.8861- (:recycle :asdf/system-registry :asdf/find-system :asdf)
1.8862- (:use :uiop/common-lisp :uiop :asdf/upgrade
1.8863- :asdf/session :asdf/component :asdf/system)
1.8864- (:export
1.8865- #:remove-entry-from-registry #:coerce-entry-to-directory
1.8866- #:registered-system #:register-system
1.8867- #:registered-systems* #:registered-systems
1.8868- #:clear-system #:map-systems
1.8869- #:*system-definition-search-functions* #:search-for-system-definition
1.8870- #:*central-registry* #:probe-asd #:sysdef-central-registry-search
1.8871- #:contrib-sysdef-search #:sysdef-find-asdf ;; backward compatibility symbols, functions removed
1.8872- #:sysdef-preloaded-system-search #:register-preloaded-system #:*preloaded-systems*
1.8873- #:find-system-if-being-defined #:mark-component-preloaded ;; forward references to asdf/find-system
1.8874- #:sysdef-immutable-system-search #:register-immutable-system #:*immutable-systems*
1.8875- #:*registered-systems* #:clear-registered-systems
1.8876- ;; defined in source-registry, but specially mentioned here:
1.8877- #:sysdef-source-registry-search))
1.8878-(in-package :asdf/system-registry)
1.8879-
1.8880-(with-upgradability ()
1.8881- ;;; Registry of Defined Systems
1.8882-
1.8883- (defvar *registered-systems* (make-hash-table :test 'equal)
1.8884- "This is a hash table whose keys are strings -- the names of systems --
1.8885-and whose values are systems.
1.8886-A system is referred to as \"registered\" if it is present in this table.")
1.8887-
1.8888- (defun registered-system (name)
1.8889- "Return a system of given NAME that was registered already,
1.8890-if such a system exists. NAME is a system designator, to be
1.8891-normalized by COERCE-NAME. The value returned is a system object,
1.8892-or NIL if not found."
1.8893- (gethash (coerce-name name) *registered-systems*))
1.8894-
1.8895- (defun registered-systems* ()
1.8896- "Return a list containing every registered system (as a system object)."
1.8897- (loop :for registered :being :the :hash-values :of *registered-systems*
1.8898- :collect registered))
1.8899-
1.8900- (defun registered-systems ()
1.8901- "Return a list of the names of every registered system."
1.8902- (mapcar 'coerce-name (registered-systems*)))
1.8903-
1.8904- (defun register-system (system)
1.8905- "Given a SYSTEM object, register it."
1.8906- (check-type system system)
1.8907- (let ((name (component-name system)))
1.8908- (check-type name string)
1.8909- (asdf-message (compatfmt "~&~@<; ~@;Registering system ~3i~_~A~@:>~%") name)
1.8910- (setf (gethash name *registered-systems*) system)))
1.8911-
1.8912- (defun map-systems (fn)
1.8913- "Apply FN to each defined system.
1.8914-
1.8915-FN should be a function of one argument. It will be
1.8916-called with an object of type asdf:system."
1.8917- (loop :for registered :being :the :hash-values :of *registered-systems*
1.8918- :do (funcall fn registered)))
1.8919-
1.8920-
1.8921- ;;; Preloaded systems: in the image even if you can't find source files backing them.
1.8922-
1.8923- (defvar *preloaded-systems* (make-hash-table :test 'equal)
1.8924- "Registration table for preloaded systems.")
1.8925-
1.8926- (declaim (ftype (function (t) t) mark-component-preloaded)) ; defined in asdf/find-system
1.8927-
1.8928- (defun make-preloaded-system (name keys)
1.8929- "Make a preloaded system of given NAME with build information from KEYS"
1.8930- (let ((system (apply 'make-instance (getf keys :class 'system)
1.8931- :name name :source-file (getf keys :source-file)
1.8932- (remove-plist-keys '(:class :name :source-file) keys))))
1.8933- (mark-component-preloaded system)
1.8934- system))
1.8935-
1.8936- (defun sysdef-preloaded-system-search (requested)
1.8937- "If REQUESTED names a system registered as preloaded, return a new system
1.8938-with its registration information."
1.8939- (let ((name (coerce-name requested)))
1.8940- (multiple-value-bind (keys foundp) (gethash name *preloaded-systems*)
1.8941- (when foundp
1.8942- (make-preloaded-system name keys)))))
1.8943-
1.8944- (defun ensure-preloaded-system-registered (name)
1.8945- "If there isn't a registered _defined_ system of given NAME,
1.8946-and a there is a registered _preloaded_ system of given NAME,
1.8947-then define and register said preloaded system."
1.8948- (if-let (system (and (not (registered-system name)) (sysdef-preloaded-system-search name)))
1.8949- (register-system system)))
1.8950-
1.8951- (defun register-preloaded-system (system-name &rest keys &key (version t) &allow-other-keys)
1.8952- "Register a system as being preloaded. If the system has not been loaded from the filesystem
1.8953-yet, or if its build information is later cleared with CLEAR-SYSTEM, a dummy system will be
1.8954-registered without backing filesystem information, based on KEYS (e.g. to provide a VERSION).
1.8955-If VERSION is the default T, and a system was already loaded, then its version will be preserved."
1.8956- (let ((name (coerce-name system-name)))
1.8957- (when (eql version t)
1.8958- (if-let (system (registered-system name))
1.8959- (setf (getf keys :version) (component-version system))))
1.8960- (setf (gethash name *preloaded-systems*) keys)
1.8961- (ensure-preloaded-system-registered system-name)))
1.8962-
1.8963-
1.8964- ;;; Immutable systems: in the image and can't be reloaded from source.
1.8965-
1.8966- (defvar *immutable-systems* nil
1.8967- "A hash-set (equal hash-table mapping keys to T) of systems that are immutable,
1.8968-i.e. already loaded in memory and not to be refreshed from the filesystem.
1.8969-They will be treated specially by find-system, and passed as :force-not argument to make-plan.
1.8970-
1.8971-For instance, to can deliver an image with many systems precompiled, that *will not* check the
1.8972-filesystem for them every time a user loads an extension, what more risk a problematic upgrade
1.8973- or catastrophic downgrade, before you dump an image, you may use:
1.8974- (map () 'asdf:register-immutable-system (asdf:already-loaded-systems))
1.8975-
1.8976-Note that direct access to this variable from outside ASDF is not supported.
1.8977-Please call REGISTER-IMMUTABLE-SYSTEM to add new immutable systems, and
1.8978-contact maintainers if you need a stable API to do more than that.")
1.8979-
1.8980- (defun sysdef-immutable-system-search (requested)
1.8981- (let ((name (coerce-name requested)))
1.8982- (when (and *immutable-systems* (gethash name *immutable-systems*))
1.8983- (or (registered-system requested)
1.8984- (error 'formatted-system-definition-error
1.8985- :format-control "Requested system ~A registered as an immutable-system, ~
1.8986-but not even registered as defined"
1.8987- :format-arguments (list name))))))
1.8988-
1.8989- (defun register-immutable-system (system-name &rest keys)
1.8990- "Register SYSTEM-NAME as preloaded and immutable.
1.8991-It will automatically be considered as passed to FORCE-NOT in a plan."
1.8992- (let ((system-name (coerce-name system-name)))
1.8993- (apply 'register-preloaded-system system-name keys)
1.8994- (unless *immutable-systems*
1.8995- (setf *immutable-systems* (list-to-hash-set nil)))
1.8996- (setf (gethash system-name *immutable-systems*) t)))
1.8997-
1.8998-
1.8999- ;;; Making systems undefined.
1.9000-
1.9001- (defun clear-system (system)
1.9002- "Clear the entry for a SYSTEM in the database of systems previously defined.
1.9003-However if the system was registered as PRELOADED (which it is if it is IMMUTABLE),
1.9004-then a new system with the same name will be defined and registered in its place
1.9005-from which build details will have been cleared.
1.9006-Note that this does NOT in any way cause any of the code of the system to be unloaded.
1.9007-Returns T if system was or is now undefined, NIL if a new preloaded system was redefined."
1.9008- ;; There is no "unload" operation in Common Lisp, and
1.9009- ;; a general such operation cannot be portably written,
1.9010- ;; considering how much CL relies on side-effects to global data structures.
1.9011- (let ((name (coerce-name system)))
1.9012- (remhash name *registered-systems*)
1.9013- (unset-asdf-cache-entry `(find-system ,name))
1.9014- (not (ensure-preloaded-system-registered name))))
1.9015-
1.9016- (defun clear-registered-systems ()
1.9017- "Clear all currently registered defined systems.
1.9018-Preloaded systems (including immutable ones) will be reset, other systems will be de-registered."
1.9019- (map () 'clear-system (registered-systems)))
1.9020-
1.9021-
1.9022- ;;; Searching for system definitions
1.9023-
1.9024- ;; For the sake of keeping things reasonably neat, we adopt a convention that
1.9025- ;; only symbols are to be pushed to this list (rather than e.g. function objects),
1.9026- ;; which makes upgrade easier. Also, the name of these symbols shall start with SYSDEF-
1.9027- (defvar *system-definition-search-functions* '()
1.9028- "A list that controls the ways that ASDF looks for system definitions.
1.9029-It contains symbols to be funcalled in order, with a requested system name as argument,
1.9030-until one returns a non-NIL result (if any), which must then be a fully initialized system object
1.9031-with that name.")
1.9032-
1.9033- ;; Initialize and/or upgrade the *system-definition-search-functions*
1.9034- ;; so it doesn't contain obsolete symbols, and does contain the current ones.
1.9035- (defun cleanup-system-definition-search-functions ()
1.9036- (setf *system-definition-search-functions*
1.9037- (append
1.9038- ;; Remove known-incompatible sysdef functions from old versions of asdf.
1.9039- ;; Order matters, so we can't just use set-difference.
1.9040- (let ((obsolete
1.9041- '(contrib-sysdef-search sysdef-find-asdf sysdef-preloaded-system-search)))
1.9042- (remove-if #'(lambda (x) (member x obsolete)) *system-definition-search-functions*))
1.9043- ;; Tuck our defaults at the end of the list if they were absent.
1.9044- ;; This is imperfect, in case they were removed on purpose,
1.9045- ;; but then it will be the responsibility of whoever removes these symmbols
1.9046- ;; to upgrade asdf before he does such a thing rather than after.
1.9047- (remove-if #'(lambda (x) (member x *system-definition-search-functions*))
1.9048- '(sysdef-central-registry-search
1.9049- sysdef-source-registry-search)))))
1.9050- (cleanup-system-definition-search-functions)
1.9051-
1.9052- ;; This (private) function does the search for a system definition using *s-d-s-f*;
1.9053- ;; it is to be called by locate-system.
1.9054- (defun search-for-system-definition (system)
1.9055- ;; Search for valid definitions of the system available in the current session.
1.9056- ;; Previous definitions as registered in *registered-systems* MUST NOT be considered;
1.9057- ;; they will be reconciled by locate-system then find-system.
1.9058- ;; There are two special treatments: first, specially search for objects being defined
1.9059- ;; in the current session, to avoid definition races between several files;
1.9060- ;; second, specially search for immutable systems, so they cannot be redefined.
1.9061- ;; Finally, use the search functions specified in *system-definition-search-functions*.
1.9062- (let ((name (coerce-name system)))
1.9063- (flet ((try (f) (if-let ((x (funcall f name))) (return-from search-for-system-definition x))))
1.9064- (try 'find-system-if-being-defined)
1.9065- (try 'sysdef-immutable-system-search)
1.9066- (map () #'try *system-definition-search-functions*))))
1.9067-
1.9068-
1.9069- ;;; The legacy way of finding a system: the *central-registry*
1.9070-
1.9071- ;; This variable contains a list of directories to be lazily searched for the requested asd
1.9072- ;; by sysdef-central-registry-search.
1.9073- (defvar *central-registry* nil
1.9074- "A list of 'system directory designators' ASDF uses to find systems.
1.9075-
1.9076-A 'system directory designator' is a pathname or an expression
1.9077-which evaluates to a pathname. For example:
1.9078-
1.9079- (setf asdf:*central-registry*
1.9080- (list '*default-pathname-defaults*
1.9081- #p\"/home/me/cl/systems/\"
1.9082- #p\"/usr/share/common-lisp/systems/\"))
1.9083-
1.9084-This variable is for backward compatibility.
1.9085-Going forward, we recommend new users should be using the source-registry.")
1.9086-
1.9087- ;; Function to look for an asd file of given NAME under a directory provided by DEFAULTS.
1.9088- ;; Return the truename of that file if it is found and TRUENAME is true.
1.9089- ;; Return NIL if the file is not found.
1.9090- ;; On Windows, follow shortcuts to .asd files.
1.9091- (defun probe-asd (name defaults &key truename)
1.9092- (block nil
1.9093- (when (directory-pathname-p defaults)
1.9094- (if-let (file (probe-file*
1.9095- (ensure-absolute-pathname
1.9096- (parse-unix-namestring name :type "asd")
1.9097- #'(lambda () (ensure-absolute-pathname defaults 'get-pathname-defaults nil))
1.9098- nil)
1.9099- :truename truename))
1.9100- (return file))
1.9101- #-(or clisp genera) ; clisp doesn't need it, plain genera doesn't have read-sequence(!)
1.9102- (os-cond
1.9103- ((os-windows-p)
1.9104- (when (physical-pathname-p defaults)
1.9105- (let ((shortcut
1.9106- (make-pathname
1.9107- :defaults defaults :case :local
1.9108- :name (strcat name ".asd")
1.9109- :type "lnk")))
1.9110- (when (probe-file* shortcut)
1.9111- (ensure-pathname (parse-windows-shortcut shortcut) :namestring :native)))))))))
1.9112-
1.9113- ;; Function to push onto *s-d-s-f* to use the *central-registry*
1.9114- (defun sysdef-central-registry-search (system)
1.9115- (let ((name (primary-system-name system))
1.9116- (to-remove nil)
1.9117- (to-replace nil))
1.9118- (block nil
1.9119- (unwind-protect
1.9120- (dolist (dir *central-registry*)
1.9121- (let ((defaults (eval dir))
1.9122- directorized)
1.9123- (when defaults
1.9124- (cond ((directory-pathname-p defaults)
1.9125- (let* ((file (probe-asd name defaults :truename *resolve-symlinks*)))
1.9126- (when file
1.9127- (return file))))
1.9128- (t
1.9129- (restart-case
1.9130- (let* ((*print-circle* nil)
1.9131- (message
1.9132- (format nil
1.9133- (compatfmt "~@<While searching for system ~S: ~3i~_~S evaluated to ~S which is not an absolute directory.~@:>")
1.9134- system dir defaults)))
1.9135- (error message))
1.9136- (remove-entry-from-registry ()
1.9137- :report "Remove entry from *central-registry* and continue"
1.9138- (push dir to-remove))
1.9139- (coerce-entry-to-directory ()
1.9140- :test (lambda (c) (declare (ignore c))
1.9141- (and (not (directory-pathname-p defaults))
1.9142- (directory-pathname-p
1.9143- (setf directorized
1.9144- (ensure-directory-pathname defaults)))))
1.9145- :report (lambda (s)
1.9146- (format s (compatfmt "~@<Coerce entry to ~a, replace ~a and continue.~@:>")
1.9147- directorized dir))
1.9148- (push (cons dir directorized) to-replace))))))))
1.9149- ;; cleanup
1.9150- (dolist (dir to-remove)
1.9151- (setf *central-registry* (remove dir *central-registry*)))
1.9152- (dolist (pair to-replace)
1.9153- (let* ((current (car pair))
1.9154- (new (cdr pair))
1.9155- (position (position current *central-registry*)))
1.9156- (setf *central-registry*
1.9157- (append (subseq *central-registry* 0 position)
1.9158- (list new)
1.9159- (subseq *central-registry* (1+ position)))))))))))
1.9160-
1.9161-;;;; -------------------------------------------------------------------------
1.9162-;;;; Actions
1.9163-
1.9164-(uiop/package:define-package :asdf/action
1.9165- (:nicknames :asdf-action)
1.9166- (:recycle :asdf/action :asdf/plan :asdf)
1.9167- (:use :uiop/common-lisp :uiop :asdf/upgrade :asdf/session :asdf/component :asdf/operation)
1.9168- (:import-from :asdf/operation #:check-operation-constructor)
1.9169- (:import-from :asdf/component #:%additional-input-files)
1.9170- (:export
1.9171- #:action #:define-convenience-action-methods
1.9172- #:action-description #:format-action
1.9173- #:downward-operation #:upward-operation #:sideway-operation #:selfward-operation
1.9174- #:non-propagating-operation
1.9175- #:component-depends-on
1.9176- #:input-files #:output-files #:output-file #:operation-done-p
1.9177- #:action-operation #:action-component #:make-action
1.9178- #:component-operation-time #:mark-operation-done #:compute-action-stamp
1.9179- #:perform #:perform-with-restarts #:retry #:accept
1.9180- #:action-path #:find-action
1.9181- #:operation-definition-warning #:operation-definition-error ;; condition
1.9182- #:action-valid-p
1.9183- #:circular-dependency #:circular-dependency-actions
1.9184- #:call-while-visiting-action #:while-visiting-action
1.9185- #:additional-input-files))
1.9186-(in-package :asdf/action)
1.9187-
1.9188-(eval-when (#-lispworks :compile-toplevel :load-toplevel :execute) ;; LispWorks issues spurious warning
1.9189-
1.9190- (deftype action ()
1.9191- "A pair of operation and component uniquely identifies a node in the dependency graph
1.9192-of steps to be performed while building a system."
1.9193- '(cons operation component))
1.9194-
1.9195- (deftype operation-designator ()
1.9196- "An operation designates itself. NIL designates a context-dependent current operation,
1.9197-and a class-name or class designates the canonical instance of the designated class."
1.9198- '(or operation null symbol class)))
1.9199-
1.9200-;;; these are pseudo accessors -- let us abstract away the CONS cell representation of plan
1.9201-;;; actions.
1.9202-(with-upgradability ()
1.9203- (defun make-action (operation component)
1.9204- (cons operation component))
1.9205- (defun action-operation (action)
1.9206- (car action))
1.9207- (defun action-component (action)
1.9208- (cdr action)))
1.9209-
1.9210-;;;; Reified representation for storage or debugging. Note: an action is identified by its class.
1.9211-(with-upgradability ()
1.9212- (defun action-path (action)
1.9213- "A readable data structure that identifies the action."
1.9214- (when action
1.9215- (let ((o (action-operation action))
1.9216- (c (action-component action)))
1.9217- (cons (type-of o) (component-find-path c)))))
1.9218- (defun find-action (path)
1.9219- "Reconstitute an action from its action-path"
1.9220- (destructuring-bind (o . c) path (make-action (make-operation o) (find-component () c)))))
1.9221-
1.9222-;;;; Convenience methods
1.9223-(with-upgradability ()
1.9224- ;; A macro that defines convenience methods for a generic function (gf) that
1.9225- ;; dispatches on operation and component. The convenience methods allow users
1.9226- ;; to call the gf with operation and/or component designators, that the
1.9227- ;; methods will resolve into actual operation and component objects, so that
1.9228- ;; the users can interact using readable designators, but developers only have
1.9229- ;; to write methods that handle operation and component objects.
1.9230- ;; FUNCTION is the generic function name
1.9231- ;; FORMALS is its list of arguments, which must include OPERATION and COMPONENT.
1.9232- ;; IF-NO-OPERATION is a form (defaults to NIL) describing what to do if no operation is found.
1.9233- ;; IF-NO-COMPONENT is a form (defaults to NIL) describing what to do if no component is found.
1.9234- (defmacro define-convenience-action-methods
1.9235- (function formals &key if-no-operation if-no-component)
1.9236- (let* ((rest (gensym "REST"))
1.9237- (found (gensym "FOUND"))
1.9238- (keyp (equal (last formals) '(&key)))
1.9239- (formals-no-key (if keyp (butlast formals) formals))
1.9240- (len (length formals-no-key))
1.9241- (operation 'operation)
1.9242- (component 'component)
1.9243- (opix (position operation formals))
1.9244- (coix (position component formals))
1.9245- (prefix (subseq formals 0 opix))
1.9246- (suffix (subseq formals (1+ coix) len))
1.9247- (more-args (when keyp `(&rest ,rest &key &allow-other-keys))))
1.9248- (assert (and (integerp opix) (integerp coix) (= coix (1+ opix))))
1.9249- (flet ((next-method (o c)
1.9250- (if keyp
1.9251- `(apply ',function ,@prefix ,o ,c ,@suffix ,rest)
1.9252- `(,function ,@prefix ,o ,c ,@suffix))))
1.9253- `(progn
1.9254- (defmethod ,function (,@prefix (,operation string) ,component ,@suffix ,@more-args)
1.9255- (declare (notinline ,function))
1.9256- (let ((,component (find-component () ,component))) ;; do it first, for defsystem-depends-on
1.9257- ,(next-method `(safe-read-from-string ,operation :package :asdf/interface) component)))
1.9258- (defmethod ,function (,@prefix (,operation symbol) ,component ,@suffix ,@more-args)
1.9259- (declare (notinline ,function))
1.9260- (if ,operation
1.9261- ,(next-method
1.9262- `(make-operation ,operation)
1.9263- `(or (find-component () ,component) ,if-no-component))
1.9264- ,if-no-operation))
1.9265- (defmethod ,function (,@prefix (,operation operation) ,component ,@suffix ,@more-args)
1.9266- (declare (notinline ,function))
1.9267- (if (typep ,component 'component)
1.9268- (error "No defined method for ~S on ~/asdf-action:format-action/"
1.9269- ',function (make-action ,operation ,component))
1.9270- (if-let (,found (find-component () ,component))
1.9271- ,(next-method operation found)
1.9272- ,if-no-component))))))))
1.9273-
1.9274-
1.9275-;;;; Self-description
1.9276-(with-upgradability ()
1.9277- (defgeneric action-description (operation component)
1.9278- (:documentation "returns a phrase that describes performing this operation
1.9279-on this component, e.g. \"loading /a/b/c\".
1.9280-You can put together sentences using this phrase."))
1.9281- (defmethod action-description (operation component)
1.9282- (format nil (compatfmt "~@<~A on ~A~@:>")
1.9283- operation component))
1.9284-
1.9285- (defun format-action (stream action &optional colon-p at-sign-p)
1.9286- "FORMAT helper to display an action's action-description.
1.9287-Use it in FORMAT control strings as ~/asdf-action:format-action/"
1.9288- (assert (null colon-p)) (assert (null at-sign-p))
1.9289- (destructuring-bind (operation . component) action
1.9290- (princ (action-description operation component) stream))))
1.9291-
1.9292-
1.9293-;;;; Detection of circular dependencies
1.9294-(with-upgradability ()
1.9295- (defun action-valid-p (operation component)
1.9296- "Is this action valid to include amongst dependencies?"
1.9297- ;; If either the operation or component was resolved to nil, the action is invalid.
1.9298- ;; :if-feature will invalidate actions on components for which the features don't apply.
1.9299- (and operation component
1.9300- (if-let (it (component-if-feature component)) (featurep it) t)))
1.9301-
1.9302- (define-condition circular-dependency (system-definition-error)
1.9303- ((actions :initarg :actions :reader circular-dependency-actions))
1.9304- (:report (lambda (c s)
1.9305- (format s (compatfmt "~@<Circular dependency of ~s on: ~3i~_~S~@:>")
1.9306- (first (circular-dependency-actions c))
1.9307- (circular-dependency-actions c)))))
1.9308-
1.9309- (defun call-while-visiting-action (operation component fun)
1.9310- "Detect circular dependencies"
1.9311- (with-asdf-session ()
1.9312- (with-accessors ((action-set visiting-action-set)
1.9313- (action-list visiting-action-list)) *asdf-session*
1.9314- (let ((action (cons operation component)))
1.9315- (when (gethash action action-set)
1.9316- (error 'circular-dependency :actions
1.9317- (member action (reverse action-list) :test 'equal)))
1.9318- (setf (gethash action action-set) t)
1.9319- (push action action-list)
1.9320- (unwind-protect
1.9321- (funcall fun)
1.9322- (pop action-list)
1.9323- (setf (gethash action action-set) nil))))))
1.9324-
1.9325- ;; Syntactic sugar for call-while-visiting-action
1.9326- (defmacro while-visiting-action ((o c) &body body)
1.9327- `(call-while-visiting-action ,o ,c #'(lambda () ,@body))))
1.9328-
1.9329-
1.9330-;;;; Dependencies
1.9331-(with-upgradability ()
1.9332- (defgeneric component-depends-on (operation component) ;; ASDF4: rename to component-dependencies
1.9333- (:documentation
1.9334- "Returns a list of dependencies needed by the component to perform
1.9335- the operation. A dependency has one of the following forms:
1.9336-
1.9337- (<operation> <component>*), where <operation> is an operation designator
1.9338- with respect to FIND-OPERATION in the context of the OPERATION argument,
1.9339- and each <component> is a component designator with respect to
1.9340- FIND-COMPONENT in the context of the COMPONENT argument,
1.9341- and means that the component depends on
1.9342- <operation> having been performed on each <component>;
1.9343-
1.9344- [Note: an <operation> is an operation designator -- it can be either an
1.9345- operation name or an operation object. Similarly, a <component> may be
1.9346- a component name or a component object. Also note that, the degenerate
1.9347- case of (<operation>) is a no-op.]
1.9348-
1.9349- Methods specialized on subclasses of existing component types
1.9350- should usually append the results of CALL-NEXT-METHOD to the list."))
1.9351- (define-convenience-action-methods component-depends-on (operation component))
1.9352-
1.9353- (defmethod component-depends-on :around ((o operation) (c component))
1.9354- (do-asdf-cache `(component-depends-on ,o ,c)
1.9355- (call-next-method))))
1.9356-
1.9357-
1.9358-;;;; upward-operation, downward-operation, sideway-operation, selfward-operation
1.9359-;; These together handle actions that propagate along the component hierarchy or operation universe.
1.9360-(with-upgradability ()
1.9361- (defclass downward-operation (operation)
1.9362- ((downward-operation
1.9363- :initform nil :reader downward-operation
1.9364- :type operation-designator :allocation :class))
1.9365- (:documentation "A DOWNWARD-OPERATION's dependencies propagate down the component hierarchy.
1.9366-I.e., if O is a DOWNWARD-OPERATION and its DOWNWARD-OPERATION slot designates operation D, then
1.9367-the action (O . M) of O on module M will depends on each of (D . C) for each child C of module M.
1.9368-The default value for slot DOWNWARD-OPERATION is NIL, which designates the operation O itself.
1.9369-E.g. in order for a MODULE to be loaded with LOAD-OP (resp. compiled with COMPILE-OP), all the
1.9370-children of the MODULE must have been loaded with LOAD-OP (resp. compiled with COMPILE-OP."))
1.9371- (defun downward-operation-depends-on (o c)
1.9372- `((,(or (downward-operation o) o) ,@(component-children c))))
1.9373- (defmethod component-depends-on ((o downward-operation) (c parent-component))
1.9374- `(,@(downward-operation-depends-on o c) ,@(call-next-method)))
1.9375-
1.9376- (defclass upward-operation (operation)
1.9377- ((upward-operation
1.9378- :initform nil :reader upward-operation
1.9379- :type operation-designator :allocation :class))
1.9380- (:documentation "An UPWARD-OPERATION has dependencies that propagate up the component hierarchy.
1.9381-I.e., if O is an instance of UPWARD-OPERATION, and its UPWARD-OPERATION slot designates operation U,
1.9382-then the action (O . C) of O on a component C that has the parent P will depends on (U . P).
1.9383-The default value for slot UPWARD-OPERATION is NIL, which designates the operation O itself.
1.9384-E.g. in order for a COMPONENT to be prepared for loading or compiling with PREPARE-OP, its PARENT
1.9385-must first be prepared for loading or compiling with PREPARE-OP."))
1.9386- ;; For backward-compatibility reasons, a system inherits from module and is a child-component
1.9387- ;; so we must guard against this case. ASDF4: remove that.
1.9388- (defun upward-operation-depends-on (o c)
1.9389- (if-let (p (component-parent c)) `((,(or (upward-operation o) o) ,p))))
1.9390- (defmethod component-depends-on ((o upward-operation) (c child-component))
1.9391- `(,@(upward-operation-depends-on o c) ,@(call-next-method)))
1.9392-
1.9393- (defclass sideway-operation (operation)
1.9394- ((sideway-operation
1.9395- :initform nil :reader sideway-operation
1.9396- :type operation-designator :allocation :class))
1.9397- (:documentation "A SIDEWAY-OPERATION has dependencies that propagate \"sideway\" to siblings
1.9398-that a component depends on. I.e. if O is a SIDEWAY-OPERATION, and its SIDEWAY-OPERATION slot
1.9399-designates operation S (where NIL designates O itself), then the action (O . C) of O on component C
1.9400-depends on each of (S . D) where D is a declared dependency of C.
1.9401-E.g. in order for a COMPONENT to be prepared for loading or compiling with PREPARE-OP,
1.9402-each of its declared dependencies must first be loaded as by LOAD-OP."))
1.9403- (defun sideway-operation-depends-on (o c)
1.9404- `((,(or (sideway-operation o) o) ,@(component-sideway-dependencies c))))
1.9405- (defmethod component-depends-on ((o sideway-operation) (c component))
1.9406- `(,@(sideway-operation-depends-on o c) ,@(call-next-method)))
1.9407-
1.9408- (defclass selfward-operation (operation)
1.9409- ((selfward-operation
1.9410- ;; NB: no :initform -- if an operation depends on others, it must explicitly specify which
1.9411- :type (or operation-designator list) :reader selfward-operation :allocation :class))
1.9412- (:documentation "A SELFWARD-OPERATION depends on another operation on the same component.
1.9413-I.e., if O is a SELFWARD-OPERATION, and its SELFWARD-OPERATION designates a list of operations L,
1.9414-then the action (O . C) of O on component C depends on each (S . C) for S in L.
1.9415-E.g. before a component may be loaded by LOAD-OP, it must have been compiled by COMPILE-OP.
1.9416-A operation-designator designates a singleton list of the designated operation;
1.9417-a list of operation-designators designates the list of designated operations;
1.9418-NIL is not a valid operation designator in that context. Note that any dependency
1.9419-ordering between the operations in a list of SELFWARD-OPERATION should be specified separately
1.9420-in the respective operation's COMPONENT-DEPENDS-ON methods so that they be scheduled properly."))
1.9421- (defun selfward-operation-depends-on (o c)
1.9422- (loop :for op :in (ensure-list (selfward-operation o)) :collect `(,op ,c)))
1.9423- (defmethod component-depends-on ((o selfward-operation) (c component))
1.9424- `(,@(selfward-operation-depends-on o c) ,@(call-next-method)))
1.9425-
1.9426- (defclass non-propagating-operation (operation)
1.9427- ()
1.9428- (:documentation "A NON-PROPAGATING-OPERATION is an operation that propagates
1.9429-no dependencies whatsoever. It is supplied in order that the programmer be able
1.9430-to specify that s/he is intentionally specifying an operation which invokes no
1.9431-dependencies.")))
1.9432-
1.9433-
1.9434-;;;---------------------------------------------------------------------------
1.9435-;;; Help programmers catch obsolete OPERATION subclasses
1.9436-;;;---------------------------------------------------------------------------
1.9437-(with-upgradability ()
1.9438- (define-condition operation-definition-warning (simple-warning)
1.9439- ()
1.9440- (:documentation "Warning condition related to definition of obsolete OPERATION objects."))
1.9441-
1.9442- (define-condition operation-definition-error (simple-error)
1.9443- ()
1.9444- (:documentation "Error condition related to definition of incorrect OPERATION objects."))
1.9445-
1.9446- (defmethod initialize-instance :before ((o operation) &key)
1.9447- (check-operation-constructor)
1.9448- (unless (typep o '(or downward-operation upward-operation sideway-operation
1.9449- selfward-operation non-propagating-operation))
1.9450- (warn 'operation-definition-warning
1.9451- :format-control
1.9452- "No dependency propagating scheme specified for operation class ~S.
1.9453-The class needs to be updated for ASDF 3.1 and specify appropriate propagation mixins."
1.9454- :format-arguments (list (type-of o)))))
1.9455-
1.9456- (defmethod initialize-instance :before ((o non-propagating-operation) &key)
1.9457- (when (typep o '(or downward-operation upward-operation sideway-operation selfward-operation))
1.9458- (error 'operation-definition-error
1.9459- :format-control
1.9460- "Inconsistent class: ~S
1.9461- NON-PROPAGATING-OPERATION is incompatible with propagating operation classes as superclasses."
1.9462- :format-arguments
1.9463- (list (type-of o)))))
1.9464-
1.9465- (defun backward-compatible-depends-on (o c)
1.9466- "DEPRECATED: all subclasses of OPERATION used in ASDF should inherit from one of
1.9467- DOWNWARD-OPERATION UPWARD-OPERATION SIDEWAY-OPERATION SELFWARD-OPERATION NON-PROPAGATING-OPERATION.
1.9468- The function BACKWARD-COMPATIBLE-DEPENDS-ON temporarily provides ASDF2 behaviour for those that
1.9469- don't. In the future this functionality will be removed, and the default will be no propagation."
1.9470- (uiop/version::notify-deprecated-function
1.9471- (version-deprecation *asdf-version* :style-warning "3.2")
1.9472- `(backward-compatible-depends-on :for-operation ,o))
1.9473- `(,@(sideway-operation-depends-on o c)
1.9474- ,@(when (typep c 'parent-component) (downward-operation-depends-on o c))))
1.9475-
1.9476- (defmethod component-depends-on ((o operation) (c component))
1.9477- `(;; Normal behavior, to allow user-specified in-order-to dependencies
1.9478- ,@(cdr (assoc (type-of o) (component-in-order-to c)))
1.9479- ;; For backward-compatibility with ASDF2, any operation that doesn't specify propagation
1.9480- ;; or non-propagation through an appropriate mixin will be downward and sideway.
1.9481- ,@(unless (typep o '(or downward-operation upward-operation sideway-operation
1.9482- selfward-operation non-propagating-operation))
1.9483- (backward-compatible-depends-on o c))))
1.9484-
1.9485- (defmethod downward-operation ((o operation)) nil)
1.9486- (defmethod sideway-operation ((o operation)) nil))
1.9487-
1.9488-
1.9489-;;;---------------------------------------------------------------------------
1.9490-;;; End of OPERATION class checking
1.9491-;;;---------------------------------------------------------------------------
1.9492-
1.9493-
1.9494-;;;; Inputs, Outputs, and invisible dependencies
1.9495-(with-upgradability ()
1.9496- (defgeneric output-files (operation component)
1.9497- (:documentation "Methods for this function return two values: a list of output files
1.9498-corresponding to this action, and a boolean indicating if they have already been subjected
1.9499-to relevant output translations and should not be further translated.
1.9500-
1.9501-Methods on PERFORM *must* call this function to determine where their outputs are to be located.
1.9502-They may rely on the order of the files to discriminate between outputs.
1.9503-"))
1.9504- (defgeneric input-files (operation component)
1.9505- (:documentation "A list of input files corresponding to this action.
1.9506-
1.9507-Methods on PERFORM *must* call this function to determine where their inputs are located.
1.9508-They may rely on the order of the files to discriminate between inputs.
1.9509-"))
1.9510- (defgeneric operation-done-p (operation component)
1.9511- (:documentation "Returns a boolean which is NIL if the action must be performed (again)."))
1.9512- (define-convenience-action-methods output-files (operation component))
1.9513- (define-convenience-action-methods input-files (operation component))
1.9514- (define-convenience-action-methods operation-done-p (operation component))
1.9515-
1.9516- (defmethod operation-done-p ((o operation) (c component))
1.9517- t)
1.9518-
1.9519- ;; Translate output files, unless asked not to. Memoize the result.
1.9520- (defmethod output-files :around ((operation t) (component t))
1.9521- (do-asdf-cache `(output-files ,operation ,component)
1.9522- (values
1.9523- (multiple-value-bind (pathnames fixedp) (call-next-method)
1.9524- ;; 1- Make sure we have absolute pathnames
1.9525- (let* ((directory (pathname-directory-pathname
1.9526- (component-pathname (find-component () component))))
1.9527- (absolute-pathnames
1.9528- (loop
1.9529- :for pathname :in pathnames
1.9530- :collect (ensure-absolute-pathname pathname directory))))
1.9531- ;; 2- Translate those pathnames as required
1.9532- (if fixedp
1.9533- absolute-pathnames
1.9534- (mapcar *output-translation-function* absolute-pathnames))))
1.9535- t)))
1.9536- (defmethod output-files ((o operation) (c component))
1.9537- nil)
1.9538- (defun output-file (operation component)
1.9539- "The unique output file of performing OPERATION on COMPONENT"
1.9540- (let ((files (output-files operation component)))
1.9541- (assert (length=n-p files 1))
1.9542- (first files)))
1.9543-
1.9544- (defgeneric additional-input-files (operation component)
1.9545- (:documentation "Additional input files for the operation on this
1.9546- component. These are files that are inferred, rather than
1.9547- explicitly specified, and these are typically NOT files that
1.9548- undergo operations directly. Instead, they are files that it is
1.9549- important for ASDF to know about in order to compute operation times,etc."))
1.9550- (define-convenience-action-methods additional-input-files (operation component))
1.9551- (defmethod additional-input-files ((op operation) (comp component))
1.9552- (cdr (assoc op (%additional-input-files comp))))
1.9553-
1.9554- ;; Memoize input files.
1.9555- (defmethod input-files :around (operation component)
1.9556- (do-asdf-cache `(input-files ,operation ,component)
1.9557- ;; get the additional input files, if any
1.9558- (append (call-next-method)
1.9559- ;; must come after the first, for other code that
1.9560- ;; assumes the first will be the "key" file
1.9561- (additional-input-files operation component))))
1.9562-
1.9563- ;; By default an action has no input-files.
1.9564- (defmethod input-files ((o operation) (c component))
1.9565- nil)
1.9566-
1.9567- ;; An action with a selfward-operation by default gets its input-files from the output-files of
1.9568- ;; the actions using selfward-operations it depends on (and the same component),
1.9569- ;; or if there are none, on the component-pathname of the component if it's a file
1.9570- ;; -- and then on the results of the next-method.
1.9571- (defmethod input-files ((o selfward-operation) (c component))
1.9572- `(,@(or (loop :for dep-o :in (ensure-list (selfward-operation o))
1.9573- :append (or (output-files dep-o c) (input-files dep-o c)))
1.9574- (if-let ((pathname (component-pathname c)))
1.9575- (and (file-pathname-p pathname) (list pathname))))
1.9576- ,@(call-next-method))))
1.9577-
1.9578-
1.9579-;;;; Done performing
1.9580-(with-upgradability ()
1.9581- ;; ASDF4: hide it behind plan-action-stamp
1.9582- (defgeneric component-operation-time (operation component)
1.9583- (:documentation "Return the timestamp for when an action was last performed"))
1.9584- (defgeneric (setf component-operation-time) (time operation component)
1.9585- (:documentation "Update the timestamp for when an action was last performed"))
1.9586- (define-convenience-action-methods component-operation-time (operation component))
1.9587-
1.9588- ;; ASDF4: hide it behind (setf plan-action-stamp)
1.9589- (defgeneric mark-operation-done (operation component)
1.9590- (:documentation "Mark a action as having been just done.
1.9591-
1.9592-Updates the action's COMPONENT-OPERATION-TIME to match the COMPUTE-ACTION-STAMP
1.9593-using the JUST-DONE flag."))
1.9594- (defgeneric compute-action-stamp (plan- operation component &key just-done)
1.9595- ;; NB: using plan- rather than plan above allows clisp to upgrade from 2.26(!)
1.9596- (:documentation "Has this action been successfully done already,
1.9597-and at what known timestamp has it been done at or will it be done at?
1.9598-* PLAN is a plan object modelling future effects of actions,
1.9599- or NIL to denote what actually happened.
1.9600-* OPERATION and COMPONENT denote the action.
1.9601-Takes keyword JUST-DONE:
1.9602-* JUST-DONE is a boolean that is true if the action was just successfully performed,
1.9603- at which point we want compute the actual stamp and warn if files are missing;
1.9604- otherwise we are making plans, anticipating the effects of the action.
1.9605-Returns two values:
1.9606-* a STAMP saying when it was done or will be done,
1.9607- or T if the action involves files that need to be recomputed.
1.9608-* a boolean DONE-P that indicates whether the action has actually been done,
1.9609- and both its output-files and its in-image side-effects are up to date."))
1.9610-
1.9611- (defmethod component-operation-time ((o operation) (c component))
1.9612- (gethash o (component-operation-times c)))
1.9613-
1.9614- (defmethod (setf component-operation-time) (stamp (o operation) (c component))
1.9615- (assert stamp () "invalid null stamp for ~A" (action-description o c))
1.9616- (setf (gethash o (component-operation-times c)) stamp))
1.9617-
1.9618- (defmethod mark-operation-done ((o operation) (c component))
1.9619- (let ((stamp (compute-action-stamp nil o c :just-done t)))
1.9620- (assert stamp () "Failed to compute a stamp for completed action ~A" (action-description o c))1
1.9621- (setf (component-operation-time o c) stamp))))
1.9622-
1.9623-
1.9624-;;;; Perform
1.9625-(with-upgradability ()
1.9626- (defgeneric perform (operation component)
1.9627- (:documentation "PERFORM an action, consuming its input-files and building its output-files"))
1.9628- (define-convenience-action-methods perform (operation component))
1.9629-
1.9630- (defmethod perform :around ((o operation) (c component))
1.9631- (while-visiting-action (o c) (call-next-method)))
1.9632- (defmethod perform :before ((o operation) (c component))
1.9633- (ensure-all-directories-exist (output-files o c)))
1.9634- (defmethod perform :after ((o operation) (c component))
1.9635- (mark-operation-done o c))
1.9636- (defmethod perform ((o operation) (c parent-component))
1.9637- nil)
1.9638- (defmethod perform ((o operation) (c source-file))
1.9639- ;; For backward compatibility, don't error on operations that don't specify propagation.
1.9640- (when (typep o '(or downward-operation upward-operation sideway-operation
1.9641- selfward-operation non-propagating-operation))
1.9642- (sysdef-error
1.9643- (compatfmt "~@<Required method ~S not implemented for ~/asdf-action:format-action/~@:>")
1.9644- 'perform (make-action o c))))
1.9645-
1.9646- ;; The restarts of the perform-with-restarts variant matter in an interactive context.
1.9647- ;; The retry strategies of p-w-r itself, and/or the background workers of a multiprocess build
1.9648- ;; may call perform directly rather than call p-w-r.
1.9649- (defgeneric perform-with-restarts (operation component)
1.9650- (:documentation "PERFORM an action in a context where suitable restarts are in place."))
1.9651- (defmethod perform-with-restarts (operation component)
1.9652- (perform operation component))
1.9653- (defmethod perform-with-restarts :around (operation component)
1.9654- (loop
1.9655- (restart-case
1.9656- (return (call-next-method))
1.9657- (retry ()
1.9658- :report
1.9659- (lambda (s)
1.9660- (format s (compatfmt "~@<Retry ~A.~@:>")
1.9661- (action-description operation component))))
1.9662- (accept ()
1.9663- :report
1.9664- (lambda (s)
1.9665- (format s (compatfmt "~@<Continue, treating ~A as having been successful.~@:>")
1.9666- (action-description operation component)))
1.9667- (mark-operation-done operation component)
1.9668- (return))))))
1.9669-;;;; -------------------------------------------------------------------------
1.9670-;;;; Actions to build Common Lisp software
1.9671-
1.9672-(uiop/package:define-package :asdf/lisp-action
1.9673- (:recycle :asdf/lisp-action :asdf)
1.9674- (:use :uiop/common-lisp :uiop :asdf/upgrade :asdf/session
1.9675- :asdf/component :asdf/system :asdf/operation :asdf/action)
1.9676- (:export
1.9677- #:try-recompiling
1.9678- #:cl-source-file #:cl-source-file.cl #:cl-source-file.lsp
1.9679- #:basic-load-op #:basic-compile-op
1.9680- #:load-op #:prepare-op #:compile-op #:test-op #:load-source-op #:prepare-source-op
1.9681- #:call-with-around-compile-hook
1.9682- #:perform-lisp-compilation #:perform-lisp-load-fasl #:perform-lisp-load-source
1.9683- #:lisp-compilation-output-files))
1.9684-(in-package :asdf/lisp-action)
1.9685-
1.9686-
1.9687-;;;; Component classes
1.9688-(with-upgradability ()
1.9689- (defclass cl-source-file (source-file)
1.9690- ((type :initform "lisp"))
1.9691- (:documentation "Component class for a Common Lisp source file (using type \"lisp\")"))
1.9692- (defclass cl-source-file.cl (cl-source-file)
1.9693- ((type :initform "cl"))
1.9694- (:documentation "Component class for a Common Lisp source file using type \"cl\""))
1.9695- (defclass cl-source-file.lsp (cl-source-file)
1.9696- ((type :initform "lsp"))
1.9697- (:documentation "Component class for a Common Lisp source file using type \"lsp\"")))
1.9698-
1.9699-
1.9700-;;;; Operation classes
1.9701-(with-upgradability ()
1.9702- (defclass basic-load-op (operation) ()
1.9703- (:documentation "Base class for operations that apply the load-time effects of a file"))
1.9704- (defclass basic-compile-op (operation) ()
1.9705- (:documentation "Base class for operations that apply the compile-time effects of a file")))
1.9706-
1.9707-
1.9708-;;; Our default operations: loading into the current lisp image
1.9709-(with-upgradability ()
1.9710- (defclass prepare-op (upward-operation sideway-operation)
1.9711- ((sideway-operation :initform 'load-op :allocation :class))
1.9712- (:documentation "Load the dependencies for the COMPILE-OP or LOAD-OP of a given COMPONENT."))
1.9713- (defclass load-op (basic-load-op downward-operation selfward-operation)
1.9714- ;; NB: even though compile-op depends on prepare-op it is not needed-in-image-p,
1.9715- ;; so we need to directly depend on prepare-op for its side-effects in the current image.
1.9716- ((selfward-operation :initform '(prepare-op compile-op) :allocation :class))
1.9717- (:documentation "Operation for loading the compiled FASL for a Lisp file"))
1.9718- (defclass compile-op (basic-compile-op downward-operation selfward-operation)
1.9719- ((selfward-operation :initform 'prepare-op :allocation :class))
1.9720- (:documentation "Operation for compiling a Lisp file to a FASL"))
1.9721-
1.9722-
1.9723- (defclass prepare-source-op (upward-operation sideway-operation)
1.9724- ((sideway-operation :initform 'load-source-op :allocation :class))
1.9725- (:documentation "Operation for loading the dependencies of a Lisp file as source."))
1.9726- (defclass load-source-op (basic-load-op downward-operation selfward-operation)
1.9727- ((selfward-operation :initform 'prepare-source-op :allocation :class))
1.9728- (:documentation "Operation for loading a Lisp file as source."))
1.9729-
1.9730- (defclass test-op (selfward-operation)
1.9731- ((selfward-operation :initform 'load-op :allocation :class))
1.9732- (:documentation "Operation for running the tests for system.
1.9733-If the tests fail, an error will be signaled.")))
1.9734-
1.9735-
1.9736-;;;; Methods for prepare-op, compile-op and load-op
1.9737-
1.9738-;;; prepare-op
1.9739-(with-upgradability ()
1.9740- (defmethod action-description ((o prepare-op) (c component))
1.9741- (format nil (compatfmt "~@<loading dependencies of ~3i~_~A~@:>") c))
1.9742- (defmethod perform ((o prepare-op) (c component))
1.9743- nil)
1.9744- (defmethod input-files ((o prepare-op) (s system))
1.9745- (if-let (it (system-source-file s)) (list it))))
1.9746-
1.9747-;;; compile-op
1.9748-(with-upgradability ()
1.9749- (defmethod action-description ((o compile-op) (c component))
1.9750- (format nil (compatfmt "~@<compiling ~3i~_~A~@:>") c))
1.9751- (defmethod action-description ((o compile-op) (c parent-component))
1.9752- (format nil (compatfmt "~@<completing compilation for ~3i~_~A~@:>") c))
1.9753- (defgeneric call-with-around-compile-hook (component thunk)
1.9754- (:documentation "A method to be called around the PERFORM'ing of actions that apply the
1.9755-compile-time side-effects of file (i.e., COMPILE-OP or LOAD-SOURCE-OP). This method can be used
1.9756-to setup readtables and other variables that control reading, macroexpanding, and compiling, etc.
1.9757-Note that it will NOT be called around the performing of LOAD-OP."))
1.9758- (defmethod call-with-around-compile-hook ((c component) function)
1.9759- (call-around-hook (around-compile-hook c) function))
1.9760- (defun perform-lisp-compilation (o c)
1.9761- "Perform the compilation of the Lisp file associated to the specified action (O . C)."
1.9762- (let (;; Before 2.26.53, that was unfortunately component-pathname. Now,
1.9763- ;; we consult input-files, the first of which should be the one to compile-file
1.9764- (input-file (first (input-files o c)))
1.9765- ;; On some implementations, there are more than one output-file,
1.9766- ;; but the first one should always be the primary fasl that gets loaded.
1.9767- (outputs (output-files o c)))
1.9768- (multiple-value-bind (output warnings-p failure-p)
1.9769- (destructuring-bind
1.9770- (output-file
1.9771- &optional
1.9772- #+(or clasp ecl mkcl) object-file
1.9773- #+clisp lib-file
1.9774- warnings-file &rest rest) outputs
1.9775- ;; Allow for extra outputs that are not of type warnings-file
1.9776- ;; The way we do it is kludgy. In ASDF4, output-files shall not be positional.
1.9777- (declare (ignore rest))
1.9778- (when warnings-file
1.9779- (unless (equal (pathname-type warnings-file) (warnings-file-type))
1.9780- (setf warnings-file nil)))
1.9781- (let ((*package* (find-package* '#:common-lisp-user)))
1.9782- (call-with-around-compile-hook
1.9783- c #'(lambda (&rest flags)
1.9784- (apply 'compile-file* input-file
1.9785- :output-file output-file
1.9786- :external-format (component-external-format c)
1.9787- :warnings-file warnings-file
1.9788- (append
1.9789- #+clisp (list :lib-file lib-file)
1.9790- #+(or clasp ecl mkcl) (list :object-file object-file)
1.9791- flags))))))
1.9792- (check-lisp-compile-results output warnings-p failure-p
1.9793- "~/asdf-action::format-action/" (list (cons o c))))))
1.9794- (defun report-file-p (f)
1.9795- "Is F a build report file containing, e.g., warnings to check?"
1.9796- (equalp (pathname-type f) "build-report"))
1.9797- (defun perform-lisp-warnings-check (o c)
1.9798- "Check the warnings associated with the dependencies of an action."
1.9799- (let* ((expected-warnings-files (remove-if-not #'warnings-file-p (input-files o c)))
1.9800- (actual-warnings-files (loop :for w :in expected-warnings-files
1.9801- :when (get-file-stamp w)
1.9802- :collect w
1.9803- :else :do (warn "Missing warnings file ~S while ~A"
1.9804- w (action-description o c)))))
1.9805- (check-deferred-warnings actual-warnings-files)
1.9806- (let* ((output (output-files o c))
1.9807- (report (find-if #'report-file-p output)))
1.9808- (when report
1.9809- (with-open-file (s report :direction :output :if-exists :supersede)
1.9810- (format s ":success~%"))))))
1.9811- (defmethod perform ((o compile-op) (c cl-source-file))
1.9812- (perform-lisp-compilation o c))
1.9813- (defun lisp-compilation-output-files (o c)
1.9814- "Compute the output-files for compiling the Lisp file for the specified action (O . C),
1.9815-an OPERATION and a COMPONENT."
1.9816- (let* ((i (first (input-files o c)))
1.9817- (f (compile-file-pathname
1.9818- i #+clasp :output-type #+ecl :type #+(or clasp ecl) :fasl
1.9819- #+mkcl :fasl-p #+mkcl t)))
1.9820- `(,f ;; the fasl is the primary output, in first position
1.9821- #+clasp
1.9822- ,@(unless nil ;; was (use-ecl-byte-compiler-p)
1.9823- `(,(compile-file-pathname i :output-type :object)))
1.9824- #+clisp
1.9825- ,@`(,(make-pathname :type "lib" :defaults f))
1.9826- #+ecl
1.9827- ,@(unless (use-ecl-byte-compiler-p)
1.9828- `(,(compile-file-pathname i :type :object)))
1.9829- #+mkcl
1.9830- ,(compile-file-pathname i :fasl-p nil) ;; object file
1.9831- ,@(when (and *warnings-file-type* (not (builtin-system-p (component-system c))))
1.9832- `(,(make-pathname :type *warnings-file-type* :defaults f))))))
1.9833- (defmethod output-files ((o compile-op) (c cl-source-file))
1.9834- (lisp-compilation-output-files o c))
1.9835- (defmethod perform ((o compile-op) (c static-file))
1.9836- nil)
1.9837-
1.9838- ;; Performing compile-op on a system will check the deferred warnings for the system
1.9839- (defmethod perform ((o compile-op) (c system))
1.9840- (when (and *warnings-file-type* (not (builtin-system-p c)))
1.9841- (perform-lisp-warnings-check o c)))
1.9842- (defmethod input-files ((o compile-op) (c system))
1.9843- (when (and *warnings-file-type* (not (builtin-system-p c)))
1.9844- ;; The most correct way to do it would be to use:
1.9845- ;; (collect-dependencies o c :other-systems nil :keep-operation 'compile-op :keep-component 'cl-source-file)
1.9846- ;; but it's expensive and we don't care too much about file order or ASDF extensions.
1.9847- (loop :for sub :in (sub-components c :type 'cl-source-file)
1.9848- :nconc (remove-if-not 'warnings-file-p (output-files o sub)))))
1.9849- (defmethod output-files ((o compile-op) (c system))
1.9850- (when (and *warnings-file-type* (not (builtin-system-p c)))
1.9851- (if-let ((pathname (component-pathname c)))
1.9852- (list (subpathname pathname (coerce-filename c) :type "build-report"))))))
1.9853-
1.9854-;;; load-op
1.9855-(with-upgradability ()
1.9856- (defmethod action-description ((o load-op) (c cl-source-file))
1.9857- (format nil (compatfmt "~@<loading FASL for ~3i~_~A~@:>") c))
1.9858- (defmethod action-description ((o load-op) (c parent-component))
1.9859- (format nil (compatfmt "~@<completing load for ~3i~_~A~@:>") c))
1.9860- (defmethod action-description ((o load-op) (c component))
1.9861- (format nil (compatfmt "~@<loading ~3i~_~A~@:>") c))
1.9862- (defmethod perform-with-restarts ((o load-op) (c cl-source-file))
1.9863- (loop
1.9864- (restart-case
1.9865- (return (call-next-method))
1.9866- (try-recompiling ()
1.9867- :report (lambda (s)
1.9868- (format s "Recompile ~a and try loading it again"
1.9869- (component-name c)))
1.9870- (perform (find-operation o 'compile-op) c)))))
1.9871- (defun perform-lisp-load-fasl (o c)
1.9872- "Perform the loading of a FASL associated to specified action (O . C),
1.9873-an OPERATION and a COMPONENT."
1.9874- (if-let (fasl (first (input-files o c)))
1.9875- (let ((*package* (find-package '#:common-lisp-user)))
1.9876- (load* fasl))))
1.9877- (defmethod perform ((o load-op) (c cl-source-file))
1.9878- (perform-lisp-load-fasl o c))
1.9879- (defmethod perform ((o load-op) (c static-file))
1.9880- nil))
1.9881-
1.9882-
1.9883-;;;; prepare-source-op, load-source-op
1.9884-
1.9885-;;; prepare-source-op
1.9886-(with-upgradability ()
1.9887- (defmethod action-description ((o prepare-source-op) (c component))
1.9888- (format nil (compatfmt "~@<loading source for dependencies of ~3i~_~A~@:>") c))
1.9889- (defmethod input-files ((o prepare-source-op) (s system))
1.9890- (if-let (it (system-source-file s)) (list it)))
1.9891- (defmethod perform ((o prepare-source-op) (c component))
1.9892- nil))
1.9893-
1.9894-;;; load-source-op
1.9895-(with-upgradability ()
1.9896- (defmethod action-description ((o load-source-op) (c component))
1.9897- (format nil (compatfmt "~@<Loading source of ~3i~_~A~@:>") c))
1.9898- (defmethod action-description ((o load-source-op) (c parent-component))
1.9899- (format nil (compatfmt "~@<Loaded source of ~3i~_~A~@:>") c))
1.9900- (defun perform-lisp-load-source (o c)
1.9901- "Perform the loading of a Lisp file as associated to specified action (O . C)"
1.9902- (call-with-around-compile-hook
1.9903- c #'(lambda ()
1.9904- (load* (first (input-files o c))
1.9905- :external-format (component-external-format c)))))
1.9906-
1.9907- (defmethod perform ((o load-source-op) (c cl-source-file))
1.9908- (perform-lisp-load-source o c))
1.9909- (defmethod perform ((o load-source-op) (c static-file))
1.9910- nil))
1.9911-
1.9912-
1.9913-;;;; test-op
1.9914-(with-upgradability ()
1.9915- (defmethod perform ((o test-op) (c component))
1.9916- nil)
1.9917- (defmethod operation-done-p ((o test-op) (c system))
1.9918- "Testing a system is _never_ done."
1.9919- nil))
1.9920-;;;; -------------------------------------------------------------------------
1.9921-;;;; Finding components
1.9922-
1.9923-(uiop/package:define-package :asdf/find-component
1.9924- (:recycle :asdf/find-component :asdf/find-system :asdf)
1.9925- (:use :uiop/common-lisp :uiop :asdf/upgrade :asdf/session
1.9926- :asdf/component :asdf/system :asdf/system-registry)
1.9927- (:export
1.9928- #:find-component
1.9929- #:resolve-dependency-name #:resolve-dependency-spec
1.9930- #:resolve-dependency-combination
1.9931- ;; Conditions
1.9932- #:missing-component #:missing-requires #:missing-parent #:missing-component-of-version #:retry
1.9933- #:missing-dependency #:missing-dependency-of-version
1.9934- #:missing-requires #:missing-parent
1.9935- #:missing-required-by #:missing-version))
1.9936-(in-package :asdf/find-component)
1.9937-
1.9938-;;;; Missing component conditions
1.9939-
1.9940-(with-upgradability ()
1.9941- (define-condition missing-component (system-definition-error)
1.9942- ((requires :initform "(unnamed)" :reader missing-requires :initarg :requires)
1.9943- (parent :initform nil :reader missing-parent :initarg :parent)))
1.9944-
1.9945- (define-condition missing-component-of-version (missing-component)
1.9946- ((version :initform nil :reader missing-version :initarg :version)))
1.9947-
1.9948- (define-condition missing-dependency (missing-component)
1.9949- ((required-by :initarg :required-by :reader missing-required-by)))
1.9950-
1.9951- (defmethod print-object ((c missing-dependency) s)
1.9952- (format s (compatfmt "~@<~A, required by ~A~@:>")
1.9953- (call-next-method c nil) (missing-required-by c)))
1.9954-
1.9955- (define-condition missing-dependency-of-version (missing-dependency
1.9956- missing-component-of-version)
1.9957- ())
1.9958-
1.9959- (defmethod print-object ((c missing-component) s)
1.9960- (format s (compatfmt "~@<Component ~S not found~@[ in ~A~]~@:>")
1.9961- (missing-requires c)
1.9962- (when (missing-parent c)
1.9963- (coerce-name (missing-parent c)))))
1.9964-
1.9965- (defmethod print-object ((c missing-component-of-version) s)
1.9966- (format s (compatfmt "~@<Component ~S does not match version ~A~@[ in ~A~]~@:>")
1.9967- (missing-requires c)
1.9968- (missing-version c)
1.9969- (when (missing-parent c)
1.9970- (coerce-name (missing-parent c))))))
1.9971-
1.9972-
1.9973-;;;; Finding components
1.9974-
1.9975-(with-upgradability ()
1.9976- (defgeneric resolve-dependency-combination (component combinator arguments)
1.9977- (:documentation "Return a component satisfying the dependency specification (COMBINATOR . ARGUMENTS)
1.9978-in the context of COMPONENT"))
1.9979-
1.9980- ;; Methods for find-component
1.9981-
1.9982- ;; If the base component is a string, resolve it as a system, then if not nil follow the path.
1.9983- (defmethod find-component ((base string) path &key registered)
1.9984- (if-let ((s (if registered
1.9985- (registered-system base)
1.9986- (find-system base nil))))
1.9987- (find-component s path :registered registered)))
1.9988-
1.9989- ;; If the base component is a symbol, coerce it to a name if not nil, and resolve that.
1.9990- ;; If nil, use the path as base if not nil, or else return nil.
1.9991- (defmethod find-component ((base symbol) path &key registered)
1.9992- (cond
1.9993- (base (find-component (coerce-name base) path :registered registered))
1.9994- (path (find-component path nil :registered registered))
1.9995- (t nil)))
1.9996-
1.9997- ;; If the base component is a cons cell, resolve its car, and add its cdr to the path.
1.9998- (defmethod find-component ((base cons) path &key registered)
1.9999- (find-component (car base) (cons (cdr base) path) :registered registered))
1.10000-
1.10001- ;; If the base component is a parent-component and the path a string, find the named child.
1.10002- (defmethod find-component ((parent parent-component) (name string) &key registered)
1.10003- (declare (ignorable registered))
1.10004- (compute-children-by-name parent :only-if-needed-p t)
1.10005- (values (gethash name (component-children-by-name parent))))
1.10006-
1.10007- ;; If the path is a symbol, coerce it to a name if non-nil, or else just return the base.
1.10008- (defmethod find-component (base (name symbol) &key registered)
1.10009- (if name
1.10010- (find-component base (coerce-name name) :registered registered)
1.10011- base))
1.10012-
1.10013- ;; If the path is a cons, first resolve its car as path, then its cdr.
1.10014- (defmethod find-component ((c component) (name cons) &key registered)
1.10015- (find-component (find-component c (car name) :registered registered)
1.10016- (cdr name) :registered registered))
1.10017-
1.10018- ;; If the path is a component, return it, disregarding the base.
1.10019- (defmethod find-component ((base t) (actual component) &key registered)
1.10020- (declare (ignorable registered))
1.10021- actual)
1.10022-
1.10023- ;; Resolve dependency NAME in the context of a COMPONENT, with given optional VERSION constraint.
1.10024- ;; This (private) function is used below by RESOLVE-DEPENDENCY-SPEC and by the :VERSION spec.
1.10025- (defun resolve-dependency-name (component name &optional version)
1.10026- (loop
1.10027- (restart-case
1.10028- (return
1.10029- (let ((comp (find-component (component-parent component) name)))
1.10030- (unless comp
1.10031- (error 'missing-dependency
1.10032- :required-by component
1.10033- :requires name))
1.10034- (when version
1.10035- (unless (version-satisfies comp version)
1.10036- (error 'missing-dependency-of-version
1.10037- :required-by component
1.10038- :version version
1.10039- :requires name)))
1.10040- comp))
1.10041- (retry ()
1.10042- :report (lambda (s)
1.10043- (format s (compatfmt "~@<Retry loading ~3i~_~A.~@:>") name))
1.10044- :test
1.10045- (lambda (c)
1.10046- (or (null c)
1.10047- (and (typep c 'missing-dependency)
1.10048- (eq (missing-required-by c) component)
1.10049- (equal (missing-requires c) name))))
1.10050- (unless (component-parent component)
1.10051- (let ((name (coerce-name name)))
1.10052- (unset-asdf-cache-entry `(find-system ,name))))))))
1.10053-
1.10054- ;; Resolve dependency specification DEP-SPEC in the context of COMPONENT.
1.10055- ;; This is notably used by MAP-DIRECT-DEPENDENCIES to process the results of COMPONENT-DEPENDS-ON
1.10056- ;; and by PARSE-DEFSYSTEM to process DEFSYSTEM-DEPENDS-ON.
1.10057- (defun resolve-dependency-spec (component dep-spec)
1.10058- (let ((component (find-component () component)))
1.10059- (if (atom dep-spec)
1.10060- (resolve-dependency-name component dep-spec)
1.10061- (resolve-dependency-combination component (car dep-spec) (cdr dep-spec)))))
1.10062-
1.10063- ;; Methods for RESOLVE-DEPENDENCY-COMBINATION to parse lists as dependency specifications.
1.10064- (defmethod resolve-dependency-combination (component combinator arguments)
1.10065- (parameter-error (compatfmt "~@<In ~S, bad dependency ~S for ~S~@:>")
1.10066- 'resolve-dependency-combination (cons combinator arguments) component))
1.10067-
1.10068- (defmethod resolve-dependency-combination (component (combinator (eql :feature)) arguments)
1.10069- (when (featurep (first arguments))
1.10070- (resolve-dependency-spec component (second arguments))))
1.10071-
1.10072- (defmethod resolve-dependency-combination (component (combinator (eql :version)) arguments)
1.10073- (resolve-dependency-name component (first arguments) (second arguments)))) ;; See lp#527788
1.10074-
1.10075-;;;; -------------------------------------------------------------------------
1.10076-;;;; Forcing
1.10077-
1.10078-(uiop/package:define-package :asdf/forcing
1.10079- (:recycle :asdf/forcing :asdf/plan :asdf)
1.10080- (:use :uiop/common-lisp :uiop :asdf/upgrade :asdf/session
1.10081- :asdf/component :asdf/operation :asdf/system :asdf/system-registry)
1.10082- (:export
1.10083- #:forcing #:make-forcing #:forced #:forced-not #:performable-p
1.10084- #:normalize-forced-systems #:normalize-forced-not-systems
1.10085- #:action-forced-p #:action-forced-not-p))
1.10086-(in-package :asdf/forcing)
1.10087-
1.10088-;;;; Forcing
1.10089-(with-upgradability ()
1.10090- (defclass forcing ()
1.10091- (;; Can plans using this forcing be PERFORMed? A plan that has different force and force-not
1.10092- ;; settings than the session can only be used for read-only queries that do not cause the
1.10093- ;; status of any action to be raised.
1.10094- (performable-p :initform nil :initarg :performable-p :reader performable-p)
1.10095- ;; Parameters
1.10096- (parameters :initform nil :initarg :parameters :reader parameters)
1.10097- ;; Table of systems specified via :force arguments
1.10098- (forced :initarg :forced :reader forced)
1.10099- ;; Table of systems specified via :force-not argument (and/or immutable)
1.10100- (forced-not :initarg :forced-not :reader forced-not)))
1.10101-
1.10102- (defgeneric action-forced-p (forcing operation component)
1.10103- (:documentation "Is this action forced to happen in this plan?"))
1.10104- (defgeneric action-forced-not-p (forcing operation component)
1.10105- (:documentation "Is this action forced to not happen in this plan?
1.10106-Takes precedence over action-forced-p."))
1.10107-
1.10108- (defun normalize-forced-systems (force system)
1.10109- "Given a SYSTEM on which operate is called and the specified FORCE argument,
1.10110-extract a hash-set of systems that are forced, or a predicate on system names,
1.10111-or NIL if none are forced, or :ALL if all are."
1.10112- (etypecase force
1.10113- ((or (member nil :all) hash-table function) force)
1.10114- (cons (list-to-hash-set (mapcar #'coerce-name force)))
1.10115- ((eql t) (when system (list-to-hash-set (list (coerce-name system)))))))
1.10116-
1.10117- (defun normalize-forced-not-systems (force-not system)
1.10118- "Given a SYSTEM on which operate is called, the specified FORCE-NOT argument,
1.10119-and the set of IMMUTABLE systems, extract a hash-set of systems that are effectively forced-not,
1.10120-or predicate on system names, or NIL if none are forced, or :ALL if all are."
1.10121- (let ((requested
1.10122- (etypecase force-not
1.10123- ((or (member nil :all) hash-table function) force-not)
1.10124- (cons (list-to-hash-set (mapcar #'coerce-name force-not)))
1.10125- ((eql t) (if system (let ((name (coerce-name system)))
1.10126- #'(lambda (x) (not (equal x name))))
1.10127- :all)))))
1.10128- (if (and *immutable-systems* requested)
1.10129- #'(lambda (x) (or (call-function requested x)
1.10130- (call-function *immutable-systems* x)))
1.10131- (or *immutable-systems* requested))))
1.10132-
1.10133- ;; TODO: shouldn't we be looking up the primary system name, rather than the system name?
1.10134- (defun action-override-p (forcing operation component override-accessor)
1.10135- "Given a plan, an action, and a function that given the plan accesses a set of overrides,
1.10136-i.e. force or force-not, see if the override applies to the current action."
1.10137- (declare (ignore operation))
1.10138- (call-function (funcall override-accessor forcing)
1.10139- (coerce-name (component-system (find-component () component)))))
1.10140-
1.10141- (defmethod action-forced-p (forcing operation component)
1.10142- (and
1.10143- ;; Did the user ask us to re-perform the action?
1.10144- (action-override-p forcing operation component 'forced)
1.10145- ;; You really can't force a builtin system and :all doesn't apply to it.
1.10146- (not (builtin-system-p (component-system component)))))
1.10147-
1.10148- (defmethod action-forced-not-p (forcing operation component)
1.10149- ;; Did the user ask us to not re-perform the action?
1.10150- ;; NB: force-not takes precedence over force, as it should
1.10151- (action-override-p forcing operation component 'forced-not))
1.10152-
1.10153- ;; Null forcing means no forcing either way
1.10154- (defmethod action-forced-p ((forcing null) (operation operation) (component component))
1.10155- nil)
1.10156- (defmethod action-forced-not-p ((forcing null) (operation operation) (component component))
1.10157- nil)
1.10158-
1.10159- (defun or-function (fun1 fun2)
1.10160- (cond
1.10161- ((or (null fun2) (eq fun1 :all)) fun1)
1.10162- ((or (null fun1) (eq fun2 :all)) fun2)
1.10163- (t #'(lambda (x) (or (call-function fun1 x) (call-function fun2 x))))))
1.10164-
1.10165- (defun make-forcing (&key performable-p system
1.10166- (force nil force-p) (force-not nil force-not-p) &allow-other-keys)
1.10167- (let* ((session-forcing (when *asdf-session* (forcing *asdf-session*)))
1.10168- (system (and system (coerce-name system)))
1.10169- (forced (normalize-forced-systems force system))
1.10170- (forced-not (normalize-forced-not-systems force-not system))
1.10171- (parameters `(,@(when force `(:force ,force))
1.10172- ,@(when force-not `(:force-not ,force-not))
1.10173- ,@(when (or (eq force t) (eq force-not t)) `(:system ,system))
1.10174- ,@(when performable-p `(:performable-p t))))
1.10175- forcing)
1.10176- (cond
1.10177- ((not session-forcing)
1.10178- (setf forcing (make-instance 'forcing
1.10179- :performable-p performable-p :parameters parameters
1.10180- :forced forced :forced-not forced-not))
1.10181- (when (and performable-p *asdf-session*)
1.10182- (setf (forcing *asdf-session*) forcing)))
1.10183- (performable-p
1.10184- (when (and (not (equal parameters (parameters session-forcing)))
1.10185- (or force-p force-not-p))
1.10186- (parameter-error "~*~S and ~S arguments not allowed in a nested call to ~3:*~S ~
1.10187-unless identically to toplevel"
1.10188- (find-symbol* :operate :asdf) :force :force-not))
1.10189- (setf forcing session-forcing))
1.10190- (t
1.10191- (setf forcing (make-instance 'forcing
1.10192- ;; Combine force and force-not with values from the toplevel-plan
1.10193- :parameters `(,@parameters :on-top-of ,(parameters session-forcing))
1.10194- :forced (or-function (forced session-forcing) forced)
1.10195- :forced-not (or-function (forced-not session-forcing) forced-not)))))
1.10196- forcing))
1.10197-
1.10198- (defmethod print-object ((forcing forcing) stream)
1.10199- (print-unreadable-object (forcing stream :type t)
1.10200- (format stream "~{~S~^ ~}" (parameters forcing))))
1.10201-
1.10202- ;; During upgrade, the *asdf-session* may legitimately be NIL, so we must handle that case.
1.10203- (defmethod forcing ((x null))
1.10204- (if-let (session (toplevel-asdf-session))
1.10205- (forcing session)
1.10206- (make-forcing :performable-p t)))
1.10207-
1.10208- ;; When performing a plan that is a list of actions, use the toplevel asdf sesssion forcing.
1.10209- (defmethod forcing ((x cons)) (forcing (toplevel-asdf-session))))
1.10210-;;;; -------------------------------------------------------------------------
1.10211-;;;; Plan
1.10212-
1.10213-(uiop/package:define-package :asdf/plan
1.10214- ;; asdf/action below is needed for required-components, traverse-action and traverse-sub-actions
1.10215- ;; that used to live there before 3.2.0.
1.10216- (:recycle :asdf/plan :asdf/action :asdf)
1.10217- (:use :uiop/common-lisp :uiop :asdf/upgrade :asdf/session
1.10218- :asdf/component :asdf/operation :asdf/action :asdf/lisp-action
1.10219- :asdf/system :asdf/system-registry :asdf/find-component :asdf/forcing)
1.10220- (:export
1.10221- #:plan #:plan-traversal #:sequential-plan #:*plan-class*
1.10222- #:action-status #:status-stamp #:status-index #:status-done-p #:status-keep-p #:status-need-p
1.10223- #:action-already-done-p
1.10224- #:+status-good+ #:+status-todo+ #:+status-void+
1.10225- #:system-out-of-date #:action-up-to-date-p
1.10226- #:circular-dependency #:circular-dependency-actions
1.10227- #:needed-in-image-p
1.10228- #:map-direct-dependencies #:reduce-direct-dependencies #:direct-dependencies
1.10229- #:compute-action-stamp #:traverse-action #:record-dependency
1.10230- #:make-plan #:plan-actions #:plan-actions-r #:perform-plan #:mark-as-done
1.10231- #:required-components #:filtered-sequential-plan
1.10232- #:plan-component-type #:plan-keep-operation #:plan-keep-component))
1.10233-(in-package :asdf/plan)
1.10234-
1.10235-;;;; Generic plan traversal class
1.10236-(with-upgradability ()
1.10237- (defclass plan () ()
1.10238- (:documentation "Base class for a plan based on which ASDF can build a system"))
1.10239- (defclass plan-traversal (plan)
1.10240- (;; The forcing parameters for this plan. Also indicates whether the plan is performable,
1.10241- ;; in which case the forcing is the same as for the entire session.
1.10242- (forcing :initform (forcing (toplevel-asdf-session)) :initarg :forcing :reader forcing))
1.10243- (:documentation "Base class for plans that simply traverse dependencies"))
1.10244- ;; Sequential plans (the default)
1.10245- (defclass sequential-plan (plan-traversal)
1.10246- ((actions-r :initform nil :accessor plan-actions-r))
1.10247- (:documentation "Simplest, default plan class, accumulating a sequence of actions"))
1.10248-
1.10249- (defgeneric plan-actions (plan)
1.10250- (:documentation "Extract from a plan a list of actions to perform in sequence"))
1.10251- (defmethod plan-actions ((plan list))
1.10252- plan)
1.10253- (defmethod plan-actions ((plan sequential-plan))
1.10254- (reverse (plan-actions-r plan)))
1.10255-
1.10256- (defgeneric record-dependency (plan operation component)
1.10257- (:documentation "Record that, within PLAN, performing OPERATION on COMPONENT depends on all
1.10258-of the (OPERATION . COMPONENT) actions in the current ASDF session's VISITING-ACTION-LIST.
1.10259-
1.10260-You can get a single action which dominates the set of dependencies corresponding to this call with
1.10261-(first (visiting-action-list *asdf-session*))
1.10262-since VISITING-ACTION-LIST is a stack whose top action depends directly on its second action,
1.10263-and whose second action depends directly on its third action, and so forth."))
1.10264-
1.10265- ;; No need to record a dependency to build a full graph, just accumulate nodes in order.
1.10266- (defmethod record-dependency ((plan sequential-plan) (o operation) (c component))
1.10267- (values)))
1.10268-
1.10269-(when-upgrading (:version "3.3.0")
1.10270- (defmethod initialize-instance :after ((plan plan-traversal) &key &allow-other-keys)))
1.10271-
1.10272-
1.10273-;;;; Planned action status
1.10274-(with-upgradability ()
1.10275- (defclass action-status ()
1.10276- ((bits
1.10277- :type fixnum :initarg :bits :reader status-bits
1.10278- :documentation "bitmap describing the status of the action.")
1.10279- (stamp
1.10280- :type (or integer boolean) :initarg :stamp :reader status-stamp
1.10281- :documentation "STAMP associated with the ACTION if it has been completed already in some
1.10282-previous session or image, T if it was done and builtin the image, or NIL if it needs to be done.")
1.10283- (level
1.10284- :type fixnum :initarg :level :initform 0 :reader status-level
1.10285- :documentation "the highest (operate-level) at which the action was needed")
1.10286- (index
1.10287- :type (or integer null) :initarg :index :initform nil :reader status-index
1.10288- :documentation "INDEX associated with the ACTION in the current session,
1.10289-or NIL if no the status is considered outside of a specific plan."))
1.10290- (:documentation "Status of an action in a plan"))
1.10291-
1.10292- ;; STAMP KEEP-P DONE-P NEED-P symbol bitmap previously currently
1.10293- ;; not-nil T T T => GOOD 7 up-to-date done (e.g. file previously loaded)
1.10294- ;; not-nil T T NIL => HERE 6 up-to-date unplanned yet done
1.10295- ;; not-nil T NIL T => REDO 5 up-to-date planned (e.g. file to load)
1.10296- ;; not-nil T NIL NIL => SKIP 4 up-to-date unplanned (e.g. file compiled)
1.10297- ;; not-nil NIL T T => DONE 3 out-of-date done
1.10298- ;; not-nil NIL T NIL => WHAT 2 out-of-date unplanned yet done(?)
1.10299- ;; NIL NIL NIL T => TODO 1 out-of-date planned
1.10300- ;; NIL NIL NIL NIL => VOID 0 out-of-date unplanned
1.10301- ;;
1.10302- ;; Note that a VOID status cannot happen as part of a transitive dependency of a wanted node
1.10303- ;; while traversing a node with TRAVERSE-ACTION; it can only happen while checking whether an
1.10304- ;; action is up-to-date with ACTION-UP-TO-DATE-P.
1.10305- ;;
1.10306- ;; When calling TRAVERSE-ACTION, the +need-bit+ is set,
1.10307- ;; unless the action is up-to-date and not needed-in-image (HERE, SKIP).
1.10308- ;; When PERFORMing an action, the +done-bit+ is set.
1.10309- ;; When the +need-bit+ is set but not the +done-bit+, the level slot indicates which level of
1.10310- ;; OPERATE it was last marked needed for; if it happens to be needed at a higher-level, then
1.10311- ;; its urgency (and that of its transitive dependencies) must be escalated so that it will be
1.10312- ;; done before the end of this level of operate.
1.10313- ;;
1.10314- ;; Also, when no ACTION-STATUS is associated to an action yet, NIL serves as a bottom value.
1.10315- ;;
1.10316- (defparameter +keep-bit+ 4)
1.10317- (defparameter +done-bit+ 2)
1.10318- (defparameter +need-bit+ 1)
1.10319- (defparameter +good-bits+ 7)
1.10320- (defparameter +todo-bits+ 1)
1.10321- (defparameter +void-bits+ 0)
1.10322-
1.10323- (defparameter +status-good+
1.10324- (make-instance 'action-status :bits +good-bits+ :stamp t))
1.10325- (defparameter +status-todo+
1.10326- (make-instance 'action-status :bits +todo-bits+ :stamp nil))
1.10327- (defparameter +status-void+
1.10328- (make-instance 'action-status :bits +void-bits+ :stamp nil)))
1.10329-
1.10330-(with-upgradability ()
1.10331- (defun make-action-status (&key bits stamp (level 0) index)
1.10332- (check-type bits (integer 0 7))
1.10333- (check-type stamp (or integer boolean))
1.10334- (check-type level (integer 0 #.most-positive-fixnum))
1.10335- (check-type index (or integer null))
1.10336- (assert (eq (null stamp) (zerop (logand bits #.(logior +keep-bit+ +done-bit+)))) ()
1.10337- "Bad action-status :bits ~S :stamp ~S" bits stamp)
1.10338- (block nil
1.10339- (when (and (null index) (zerop level))
1.10340- (case bits
1.10341- (#.+void-bits+ (return +status-void+))
1.10342- (#.+todo-bits+ (return +status-todo+))
1.10343- (#.+good-bits+ (when (eq stamp t) (return +status-good+)))))
1.10344- (make-instance 'action-status :bits bits :stamp stamp :level level :index index)))
1.10345-
1.10346- (defun status-keep-p (status)
1.10347- (plusp (logand (status-bits status) #.+keep-bit+)))
1.10348- (defun status-done-p (status)
1.10349- (plusp (logand (status-bits status) #.+done-bit+)))
1.10350- (defun status-need-p (status)
1.10351- (plusp (logand (status-bits status) #.+need-bit+)))
1.10352-
1.10353- (defun merge-action-status (status1 status2) ;; status-and
1.10354- "Return the earliest status later than both status1 and status2"
1.10355- (make-action-status
1.10356- :bits (logand (status-bits status1) (status-bits status2))
1.10357- :stamp (latest-timestamp (status-stamp status1) (status-stamp status2))
1.10358- :level (min (status-level status1) (status-level status2))
1.10359- :index (or (status-index status1) (status-index status2))))
1.10360-
1.10361- (defun mark-status-needed (status &optional (level (operate-level))) ;; limited status-or
1.10362- "Return the same status but with the need bit set, for the given level"
1.10363- (if (and (status-need-p status)
1.10364- (>= (status-level status) level))
1.10365- status
1.10366- (make-action-status
1.10367- :bits (logior (status-bits status) +need-bit+)
1.10368- :level (max level (status-level status))
1.10369- :stamp (status-stamp status)
1.10370- :index (status-index status))))
1.10371-
1.10372- (defmethod print-object ((status action-status) stream)
1.10373- (print-unreadable-object (status stream :type t)
1.10374- (with-slots (bits stamp level index) status
1.10375- (format stream "~{~S~^ ~}" `(:bits ,bits :stamp ,stamp :level ,level :index ,index)))))
1.10376-
1.10377- (defgeneric action-status (plan operation component)
1.10378- (:documentation "Returns the ACTION-STATUS associated to the action of OPERATION on COMPONENT
1.10379-in the PLAN, or NIL if the action wasn't visited yet as part of the PLAN."))
1.10380-
1.10381- (defgeneric (setf action-status) (new-status plan operation component)
1.10382- (:documentation "Sets the ACTION-STATUS associated to
1.10383-the action of OPERATION on COMPONENT in the PLAN"))
1.10384-
1.10385- (defmethod action-status ((plan null) (o operation) (c component))
1.10386- (multiple-value-bind (stamp done-p) (component-operation-time o c)
1.10387- (if done-p
1.10388- (make-action-status :bits #.+keep-bit+ :stamp stamp)
1.10389- +status-void+)))
1.10390-
1.10391- (defmethod (setf action-status) (new-status (plan null) (o operation) (c component))
1.10392- (let ((times (component-operation-times c)))
1.10393- (if (status-done-p new-status)
1.10394- (setf (gethash o times) (status-stamp new-status))
1.10395- (remhash o times)))
1.10396- new-status)
1.10397-
1.10398- ;; Handle FORCED-NOT: it makes an action return its current timestamp as status
1.10399- (defmethod action-status ((p plan) (o operation) (c component))
1.10400- ;; TODO: should we instead test something like:
1.10401- ;; (action-forced-not-p plan operation (primary-system component))
1.10402- (or (gethash (make-action o c) (visited-actions *asdf-session*))
1.10403- (when (action-forced-not-p (forcing p) o c)
1.10404- (let ((status (action-status nil o c)))
1.10405- (setf (gethash (make-action o c) (visited-actions *asdf-session*))
1.10406- (make-action-status
1.10407- :bits +good-bits+
1.10408- :stamp (or (and status (status-stamp status)) t)
1.10409- :index (incf (total-action-count *asdf-session*))))))))
1.10410-
1.10411- (defmethod (setf action-status) (new-status (p plan) (o operation) (c component))
1.10412- (setf (gethash (make-action o c) (visited-actions *asdf-session*)) new-status))
1.10413-
1.10414- (defmethod (setf action-status) :after
1.10415- (new-status (p sequential-plan) (o operation) (c component))
1.10416- (unless (status-done-p new-status)
1.10417- (push (make-action o c) (plan-actions-r p)))))
1.10418-
1.10419-
1.10420-;;;; Is the action needed in this image?
1.10421-(with-upgradability ()
1.10422- (defgeneric needed-in-image-p (operation component)
1.10423- (:documentation "Is the action of OPERATION on COMPONENT needed in the current image
1.10424-to be meaningful, or could it just as well have been done in another Lisp image?"))
1.10425-
1.10426- (defmethod needed-in-image-p ((o operation) (c component))
1.10427- ;; We presume that actions that modify the filesystem don't need be run
1.10428- ;; in the current image if they have already been done in another,
1.10429- ;; and can be run in another process (e.g. a fork),
1.10430- ;; whereas those that don't are meant to side-effect the current image and can't.
1.10431- (not (output-files o c))))
1.10432-
1.10433-
1.10434-;;;; Visiting dependencies of an action and computing action stamps
1.10435-(with-upgradability ()
1.10436- (defun map-direct-dependencies (operation component fun)
1.10437- "Call FUN on all the valid dependencies of the given action in the given plan"
1.10438- (loop :for (dep-o-spec . dep-c-specs) :in (component-depends-on operation component)
1.10439- :for dep-o = (find-operation operation dep-o-spec)
1.10440- :when dep-o
1.10441- :do (loop :for dep-c-spec :in dep-c-specs
1.10442- :for dep-c = (and dep-c-spec (resolve-dependency-spec component dep-c-spec))
1.10443- :when (action-valid-p dep-o dep-c)
1.10444- :do (funcall fun dep-o dep-c))))
1.10445-
1.10446- (defun reduce-direct-dependencies (operation component combinator seed)
1.10447- "Reduce the direct dependencies to a value computed by iteratively calling COMBINATOR
1.10448-for each dependency action on the dependency's operation and component and an accumulator
1.10449-initialized with SEED."
1.10450- (map-direct-dependencies
1.10451- operation component
1.10452- #'(lambda (dep-o dep-c) (setf seed (funcall combinator dep-o dep-c seed))))
1.10453- seed)
1.10454-
1.10455- (defun direct-dependencies (operation component)
1.10456- "Compute a list of the direct dependencies of the action within the plan"
1.10457- (reverse (reduce-direct-dependencies operation component #'acons nil)))
1.10458-
1.10459- ;; In a distant future, get-file-stamp, component-operation-time and latest-stamp
1.10460- ;; shall also be parametrized by the plan, or by a second model object,
1.10461- ;; so they need not refer to the state of the filesystem,
1.10462- ;; and the stamps could be cryptographic checksums rather than timestamps.
1.10463- ;; Such a change remarkably would only affect COMPUTE-ACTION-STAMP.
1.10464- (define-condition dependency-not-done (warning)
1.10465- ((op
1.10466- :initarg :op)
1.10467- (component
1.10468- :initarg :component)
1.10469- (dep-op
1.10470- :initarg :dep-op)
1.10471- (dep-component
1.10472- :initarg :dep-component)
1.10473- (plan
1.10474- :initarg :plan
1.10475- :initform nil))
1.10476- (:report (lambda (condition stream)
1.10477- (with-slots (op component dep-op dep-component plan) condition
1.10478- (format stream "Computing just-done stamp ~@[in plan ~S~] for action ~S, but dependency ~S wasn't done yet!"
1.10479- plan
1.10480- (action-path (make-action op component))
1.10481- (action-path (make-action dep-op dep-component)))))))
1.10482-
1.10483- (defmethod compute-action-stamp (plan (o operation) (c component) &key just-done)
1.10484- ;; Given an action, figure out at what time in the past it has been done,
1.10485- ;; or if it has just been done, return the time that it has.
1.10486- ;; Returns two values:
1.10487- ;; 1- the TIMESTAMP of the action if it has already been done and is up to date,
1.10488- ;; or NIL is either hasn't been done or is out of date.
1.10489- ;; (An ASDF extension could use a cryptographic digest instead.)
1.10490- ;; 2- the DONE-IN-IMAGE-P boolean flag that is T if the action has already been done
1.10491- ;; in the current image, or NIL if it hasn't.
1.10492- ;; Note that if e.g. LOAD-OP only depends on up-to-date files, but
1.10493- ;; hasn't been done in the current image yet, then it can have a non-NIL timestamp,
1.10494- ;; yet a NIL done-in-image-p flag: we can predict what timestamp it will have once loaded,
1.10495- ;; i.e. that of the input-files.
1.10496- ;; If just-done is NIL, these values return are the notional fields of
1.10497- ;; a KEEP, REDO or TODO status (VOID is possible, but probably an error).
1.10498- ;; If just-done is T, they are the notional fields of DONE status
1.10499- ;; (or, if something went wrong, TODO).
1.10500- (nest
1.10501- (block ())
1.10502- (let* ((dep-status ; collect timestamp from dependencies (or T if forced or out-of-date)
1.10503- (reduce-direct-dependencies
1.10504- o c
1.10505- #'(lambda (do dc status)
1.10506- ;; out-of-date dependency: don't bother looking further
1.10507- (let ((action-status (action-status plan do dc)))
1.10508- (cond
1.10509- ((and action-status (or (status-keep-p action-status)
1.10510- (and just-done (status-stamp action-status))))
1.10511- (merge-action-status action-status status))
1.10512- (just-done
1.10513- ;; It's OK to lose some ASDF action stamps during self-upgrade
1.10514- (unless (equal "asdf" (primary-system-name dc))
1.10515- (warn 'dependency-not-done
1.10516- :plan plan
1.10517- :op o :component c
1.10518- :dep-op do :dep-component dc))
1.10519- status)
1.10520- (t
1.10521- (return (values nil nil))))))
1.10522- +status-good+))
1.10523- (dep-stamp (status-stamp dep-status))))
1.10524- (let* (;; collect timestamps from inputs, and exit early if any is missing
1.10525- (in-files (input-files o c))
1.10526- (in-stamps (mapcar #'get-file-stamp in-files))
1.10527- (missing-in (loop :for f :in in-files :for s :in in-stamps :unless s :collect f))
1.10528- (latest-in (timestamps-latest (cons dep-stamp in-stamps))))
1.10529- (when (and missing-in (not just-done)) (return (values nil nil))))
1.10530- (let* (;; collect timestamps from outputs, and exit early if any is missing
1.10531- (out-files (remove-if 'null (output-files o c)))
1.10532- (out-stamps (mapcar (if just-done 'register-file-stamp 'get-file-stamp) out-files))
1.10533- (missing-out (loop :for f :in out-files :for s :in out-stamps :unless s :collect f))
1.10534- (earliest-out (timestamps-earliest out-stamps)))
1.10535- (when (and missing-out (not just-done)) (return (values nil nil))))
1.10536- (let (;; Time stamps from the files at hand, and whether any is missing
1.10537- (all-present (not (or missing-in missing-out)))
1.10538- ;; Has any input changed since we last generated the files?
1.10539- ;; Note that we use timestamp<= instead of timestamp< to play nice with generated files.
1.10540- ;; Any race condition is intrinsic to the limited timestamp resolution.
1.10541- (up-to-date-p (timestamp<= latest-in earliest-out))
1.10542- ;; If everything is up to date, the latest of inputs and outputs is our stamp
1.10543- (done-stamp (timestamps-latest (cons latest-in out-stamps))))
1.10544- ;; Warn if some files are missing:
1.10545- ;; either our model is wrong or some other process is messing with our files.
1.10546- (when (and just-done (not all-present))
1.10547- ;; Shouldn't that be an error instead?
1.10548- (warn "~A completed without ~:[~*~;~*its input file~:p~2:*~{ ~S~}~*~]~
1.10549- ~:[~; or ~]~:[~*~;~*its output file~:p~2:*~{ ~S~}~*~]"
1.10550- (action-description o c)
1.10551- missing-in (length missing-in) (and missing-in missing-out)
1.10552- missing-out (length missing-out))))
1.10553- (let (;; There are three kinds of actions:
1.10554- (out-op (and out-files t)) ; those that create files on the filesystem
1.10555- ;;(image-op (and in-files (null out-files))) ; those that load stuff into the image
1.10556- ;;(null-op (and (null out-files) (null in-files))) ; placeholders that do nothing
1.10557- ))
1.10558- (if (or just-done ;; The done-stamp is valid: if we're just done, or
1.10559- (and all-present ;; if all filesystem effects are up-to-date
1.10560- up-to-date-p
1.10561- (operation-done-p o c) ;; and there's no invalidating reason.
1.10562- (not (action-forced-p (forcing (or plan *asdf-session*)) o c))))
1.10563- (values done-stamp ;; return the hard-earned timestamp
1.10564- (or just-done
1.10565- out-op ;; A file-creating op is done when all files are up to date.
1.10566- ;; An image-effecting operation is done when
1.10567- (and (status-done-p dep-status) ;; all the dependencies were done, and
1.10568- (multiple-value-bind (perform-stamp perform-done-p)
1.10569- (component-operation-time o c)
1.10570- (and perform-done-p ;; the op was actually run,
1.10571- (equal perform-stamp done-stamp)))))) ;; with a matching stamp.
1.10572- ;; done-stamp invalid: return a timestamp in an indefinite future, action not done yet
1.10573- (values nil nil)))))
1.10574-
1.10575-
1.10576-;;;; The four different actual traversals:
1.10577-;; * TRAVERSE-ACTION o c T: Ensure all dependencies are either up-to-date in-image, or planned
1.10578-;; * TRAVERSE-ACTION o c NIL: Ensure all dependencies are up-to-date or planned, in-image or not
1.10579-;; * ACTION-UP-TO-DATE-P: Check whether some (defsystem-depends-on ?) dependencies are up to date
1.10580-;; * COLLECT-ACTION-DEPENDENCIES: Get the dependencies (filtered), don't change any status
1.10581-(with-upgradability ()
1.10582-
1.10583- ;; Compute the action status for a newly visited action.
1.10584- (defun compute-action-status (plan operation component need-p)
1.10585- (multiple-value-bind (stamp done-p)
1.10586- (compute-action-stamp plan operation component)
1.10587- (assert (or stamp (not done-p)))
1.10588- (make-action-status
1.10589- :bits (logior (if stamp #.+keep-bit+ 0)
1.10590- (if done-p #.+done-bit+ 0)
1.10591- (if need-p #.+need-bit+ 0))
1.10592- :stamp stamp
1.10593- :level (operate-level)
1.10594- :index (incf (total-action-count *asdf-session*)))))
1.10595-
1.10596- ;; TRAVERSE-ACTION, in the context of a given PLAN object that accumulates dependency data,
1.10597- ;; visits the action defined by its OPERATION and COMPONENT arguments,
1.10598- ;; and all its transitive dependencies (unless already visited),
1.10599- ;; in the context of the action being (or not) NEEDED-IN-IMAGE-P,
1.10600- ;; i.e. needs to be done in the current image vs merely have been done in a previous image.
1.10601- ;;
1.10602- ;; TRAVERSE-ACTION updates the VISITED-ACTIONS entries for the action and for all its
1.10603- ;; transitive dependencies (that haven't been sufficiently visited so far).
1.10604- ;; It does not return any usable value.
1.10605- ;;
1.10606- ;; Note that for an XCVB-like plan with one-image-per-file-outputting-action,
1.10607- ;; the below method would be insufficient, since it assumes a single image
1.10608- ;; to traverse each node at most twice; non-niip actions would be traversed only once,
1.10609- ;; but niip nodes could be traversed once per image, i.e. once plus once per non-niip action.
1.10610-
1.10611- (defun traverse-action (plan operation component needed-in-image-p)
1.10612- (block nil
1.10613- (unless (action-valid-p operation component) (return))
1.10614- ;; Record the dependency. This hook is needed by POIU, which tracks a full dependency graph,
1.10615- ;; instead of just a dependency order as in vanilla ASDF.
1.10616- ;; TODO: It is also needed to detect OPERATE-in-PERFORM.
1.10617- (record-dependency plan operation component)
1.10618- (while-visiting-action (operation component) ; maintain context, handle circularity.
1.10619- ;; needed-in-image distinguishes b/w things that must happen in the
1.10620- ;; current image and those things that simply need to have been done in a previous one.
1.10621- (let* ((aniip (needed-in-image-p operation component)) ; action-specific needed-in-image
1.10622- ;; effective niip: meaningful for the action and required by the plan as traversed
1.10623- (eniip (and aniip needed-in-image-p))
1.10624- ;; status: have we traversed that action previously, and if so what was its status?
1.10625- (status (action-status plan operation component))
1.10626- (level (operate-level)))
1.10627- (when (and status
1.10628- (or (status-done-p status) ;; all done
1.10629- (and (status-need-p status) (<= level (status-level status))) ;; already visited
1.10630- (and (status-keep-p status) (not eniip)))) ;; up-to-date and not eniip
1.10631- (return)) ; Already visited with sufficient need-in-image level!
1.10632- (labels ((visit-action (niip) ; We may visit the action twice, once with niip NIL, then T
1.10633- (map-direct-dependencies ; recursively traverse dependencies
1.10634- operation component #'(lambda (o c) (traverse-action plan o c niip)))
1.10635- ;; AFTER dependencies have been traversed, compute action stamp
1.10636- (let* ((status (if status
1.10637- (mark-status-needed status level)
1.10638- (compute-action-status plan operation component t)))
1.10639- (out-of-date-p (not (status-keep-p status)))
1.10640- (to-perform-p (or out-of-date-p (and niip (not (status-done-p status))))))
1.10641- (cond ; it needs be done if it's out of date or needed in image but absent
1.10642- ((and out-of-date-p (not niip)) ; if we need to do it,
1.10643- (visit-action t)) ; then we need to do it *in the (current) image*!
1.10644- (t
1.10645- (setf (action-status plan operation component) status)
1.10646- (when (status-done-p status)
1.10647- (setf (component-operation-time operation component)
1.10648- (status-stamp status)))
1.10649- (when to-perform-p ; if it needs to be added to the plan, count it
1.10650- (incf (planned-action-count *asdf-session*))
1.10651- (unless aniip ; if it's output-producing, count it
1.10652- (incf (planned-output-action-count *asdf-session*)))))))))
1.10653- (visit-action eniip)))))) ; visit the action
1.10654-
1.10655- ;; NB: This is not an error, not a warning, but a normal expected condition,
1.10656- ;; to be to signaled by FIND-SYSTEM when it detects an out-of-date system,
1.10657- ;; *before* it tries to replace it with a new definition.
1.10658- (define-condition system-out-of-date (condition)
1.10659- ((name :initarg :name :reader component-name))
1.10660- (:documentation "condition signaled when a system is detected as being out of date")
1.10661- (:report (lambda (c s)
1.10662- (format s "system ~A is out of date" (component-name c)))))
1.10663-
1.10664- (defun action-up-to-date-p (plan operation component)
1.10665- "Check whether an action was up-to-date at the beginning of the session.
1.10666-Update the VISITED-ACTIONS table with the known status, but don't add anything to the PLAN."
1.10667- (block nil
1.10668- (unless (action-valid-p operation component) (return t))
1.10669- (while-visiting-action (operation component) ; maintain context, handle circularity.
1.10670- ;; Do NOT record the dependency: it might be out of date.
1.10671- (let ((status (or (action-status plan operation component)
1.10672- (setf (action-status plan operation component)
1.10673- (let ((dependencies-up-to-date-p
1.10674- (handler-case
1.10675- (block nil
1.10676- (map-direct-dependencies
1.10677- operation component
1.10678- #'(lambda (o c)
1.10679- (unless (action-up-to-date-p plan o c)
1.10680- (return nil))))
1.10681- t)
1.10682- (system-out-of-date () nil))))
1.10683- (if dependencies-up-to-date-p
1.10684- (compute-action-status plan operation component nil)
1.10685- +status-void+))))))
1.10686- (and (status-keep-p status) (status-stamp status)))))))
1.10687-
1.10688-
1.10689-;;;; Incidental traversals
1.10690-
1.10691-;;; Making a FILTERED-SEQUENTIAL-PLAN can be used to, e.g., all of the source
1.10692-;;; files required by a bundling operation.
1.10693-(with-upgradability ()
1.10694- (defclass filtered-sequential-plan (sequential-plan)
1.10695- ((component-type :initform t :initarg :component-type :reader plan-component-type)
1.10696- (keep-operation :initform t :initarg :keep-operation :reader plan-keep-operation)
1.10697- (keep-component :initform t :initarg :keep-component :reader plan-keep-component))
1.10698- (:documentation "A variant of SEQUENTIAL-PLAN that only records a subset of actions."))
1.10699-
1.10700- (defmethod initialize-instance :after ((plan filtered-sequential-plan)
1.10701- &key system other-systems)
1.10702- ;; Ignore force and force-not, rely on other-systems:
1.10703- ;; force traversal of what we're interested in, i.e. current system or also others;
1.10704- ;; force-not traversal of what we're not interested in, i.e. other systems unless other-systems.
1.10705- (setf (slot-value plan 'forcing)
1.10706- (make-forcing :system system :force :all :force-not (if other-systems nil t))))
1.10707-
1.10708- (defmethod plan-actions ((plan filtered-sequential-plan))
1.10709- (with-slots (keep-operation keep-component) plan
1.10710- (loop :for action :in (call-next-method)
1.10711- :as o = (action-operation action)
1.10712- :as c = (action-component action)
1.10713- :when (and (typep o keep-operation) (typep c keep-component))
1.10714- :collect (make-action o c))))
1.10715-
1.10716- (defun collect-action-dependencies (plan operation component)
1.10717- (when (action-valid-p operation component)
1.10718- (while-visiting-action (operation component) ; maintain context, handle circularity.
1.10719- (let ((action (make-action operation component)))
1.10720- (unless (nth-value 1 (gethash action (visited-actions *asdf-session*)))
1.10721- (setf (gethash action (visited-actions *asdf-session*)) nil)
1.10722- (when (and (typep component (plan-component-type plan))
1.10723- (not (action-forced-not-p (forcing plan) operation component)))
1.10724- (map-direct-dependencies operation component
1.10725- #'(lambda (o c) (collect-action-dependencies plan o c)))
1.10726- (push action (plan-actions-r plan))))))))
1.10727-
1.10728- (defgeneric collect-dependencies (operation component &key &allow-other-keys)
1.10729- (:documentation "Given an action, build a plan for all of its dependencies."))
1.10730- (define-convenience-action-methods collect-dependencies (operation component &key))
1.10731- (defmethod collect-dependencies ((operation operation) (component component)
1.10732- &rest keys &key &allow-other-keys)
1.10733- (let ((plan (apply 'make-instance 'filtered-sequential-plan
1.10734- :system (component-system component) keys)))
1.10735- (loop :for action :in (direct-dependencies operation component)
1.10736- :do (collect-action-dependencies plan (action-operation action) (action-component action)))
1.10737- (plan-actions plan)))
1.10738-
1.10739- (defun required-components (system &rest keys &key (goal-operation 'load-op) &allow-other-keys)
1.10740- "Given a SYSTEM and a GOAL-OPERATION (default LOAD-OP), traverse the dependencies and
1.10741-return a list of the components involved in building the desired action."
1.10742- (with-asdf-session (:override t)
1.10743- (remove-duplicates
1.10744- (mapcar 'action-component
1.10745- (apply 'collect-dependencies goal-operation system
1.10746- (remove-plist-key :goal-operation keys)))
1.10747- :from-end t))))
1.10748-
1.10749-
1.10750-;;;; High-level interface: make-plan, perform-plan
1.10751-(with-upgradability ()
1.10752- (defgeneric make-plan (plan-class operation component &key &allow-other-keys)
1.10753- (:documentation "Generate and return a plan for performing OPERATION on COMPONENT."))
1.10754- (define-convenience-action-methods make-plan (plan-class operation component &key))
1.10755-
1.10756- (defgeneric mark-as-done (plan-class operation component)
1.10757- (:documentation "Mark an action as done in a plan, after performing it."))
1.10758- (define-convenience-action-methods mark-as-done (plan-class operation component))
1.10759-
1.10760- (defgeneric perform-plan (plan &key)
1.10761- (:documentation "Actually perform a plan and build the requested actions"))
1.10762-
1.10763- (defparameter* *plan-class* 'sequential-plan
1.10764- "The default plan class to use when building with ASDF")
1.10765-
1.10766- (defmethod make-plan (plan-class (o operation) (c component) &rest keys &key &allow-other-keys)
1.10767- (with-asdf-session ()
1.10768- (let ((plan (apply 'make-instance (or plan-class *plan-class*) keys)))
1.10769- (traverse-action plan o c t)
1.10770- plan)))
1.10771-
1.10772- (defmethod perform-plan :around ((plan t) &key)
1.10773- (assert (performable-p (forcing plan)) () "plan not performable")
1.10774- (let ((*package* *package*)
1.10775- (*readtable* *readtable*))
1.10776- (with-compilation-unit () ;; backward-compatibility.
1.10777- (call-next-method)))) ;; Going forward, see deferred-warning support in lisp-build.
1.10778-
1.10779- (defun action-already-done-p (plan operation component)
1.10780- (if-let (status (action-status plan operation component))
1.10781- (status-done-p status)))
1.10782-
1.10783- (defmethod perform-plan ((plan t) &key)
1.10784- (loop :for action :in (plan-actions plan)
1.10785- :as o = (action-operation action)
1.10786- :as c = (action-component action) :do
1.10787- (unless (action-already-done-p plan o c)
1.10788- (perform-with-restarts o c)
1.10789- (mark-as-done plan o c))))
1.10790-
1.10791- (defmethod mark-as-done ((plan plan) (o operation) (c component))
1.10792- (let ((plan-status (action-status plan o c))
1.10793- (perform-status (action-status nil o c)))
1.10794- (assert (and (status-stamp perform-status) (status-keep-p perform-status)) ()
1.10795- "Just performed ~A but failed to mark it done" (action-description o c))
1.10796- (setf (action-status plan o c)
1.10797- (make-action-status
1.10798- :bits (logior (status-bits plan-status) +done-bit+)
1.10799- :stamp (status-stamp perform-status)
1.10800- :level (status-level plan-status)
1.10801- :index (status-index plan-status))))))
1.10802-;;;; -------------------------------------------------------------------------
1.10803-;;;; Invoking Operations
1.10804-
1.10805-(uiop/package:define-package :asdf/operate
1.10806- (:recycle :asdf/operate :asdf)
1.10807- (:use :uiop/common-lisp :uiop :asdf/upgrade :asdf/session
1.10808- :asdf/component :asdf/system :asdf/system-registry :asdf/find-component
1.10809- :asdf/operation :asdf/action :asdf/lisp-action :asdf/forcing :asdf/plan)
1.10810- (:export
1.10811- #:operate #:oos #:build-op #:make
1.10812- #:load-system #:load-systems #:load-systems*
1.10813- #:compile-system #:test-system #:require-system #:module-provide-asdf
1.10814- #:component-loaded-p #:already-loaded-systems
1.10815- #:recursive-operate))
1.10816-(in-package :asdf/operate)
1.10817-
1.10818-(with-upgradability ()
1.10819- (defgeneric operate (operation component &key)
1.10820- (:documentation
1.10821- "Operate does mainly four things for the user:
1.10822-
1.10823-1. Resolves the OPERATION designator into an operation object.
1.10824- OPERATION is typically a symbol denoting an operation class, instantiated with MAKE-OPERATION.
1.10825-2. Resolves the COMPONENT designator into a component object.
1.10826- COMPONENT is typically a string or symbol naming a system, loaded from disk using FIND-SYSTEM.
1.10827-3. It then calls MAKE-PLAN with the operation and system as arguments.
1.10828-4. Finally calls PERFORM-PLAN on the resulting plan to actually build the system.
1.10829-
1.10830-The entire computation is wrapped in WITH-COMPILATION-UNIT and error handling code.
1.10831-If a VERSION argument is supplied, then operate also ensures that the system found satisfies it
1.10832-using the VERSION-SATISFIES method.
1.10833-If a PLAN-CLASS argument is supplied, that class is used for the plan.
1.10834-If a PLAN-OPTIONS argument is supplied, the options are passed to the plan.
1.10835-
1.10836-The :FORCE or :FORCE-NOT argument to OPERATE can be:
1.10837- T to force the inside of the specified system to be rebuilt (resp. not),
1.10838- without recursively forcing the other systems we depend on.
1.10839- :ALL to force all systems including other systems we depend on to be rebuilt (resp. not).
1.10840- (SYSTEM1 SYSTEM2 ... SYSTEMN) to force systems named in a given list
1.10841-:FORCE-NOT has precedence over :FORCE; builtin systems cannot be forced.
1.10842-
1.10843-For backward compatibility, all keyword arguments are passed to MAKE-OPERATION
1.10844-when instantiating a new operation, that will in turn be inherited by new operations.
1.10845-But do NOT depend on it, for this is deprecated behavior."))
1.10846-
1.10847- (define-convenience-action-methods operate (operation component &key)
1.10848- :if-no-component (error 'missing-component :requires component))
1.10849-
1.10850- ;; This method ensures that an ASDF upgrade is attempted as the very first thing,
1.10851- ;; with suitable state preservation in case in case it actually happens,
1.10852- ;; and that a few suitable dynamic bindings are established.
1.10853- (defmethod operate :around (operation component &rest keys
1.10854- &key verbose
1.10855- (on-warnings *compile-file-warnings-behaviour*)
1.10856- (on-failure *compile-file-failure-behaviour*))
1.10857- (nest
1.10858- (with-asdf-session ())
1.10859- (let* ((operation-remaker ;; how to remake the operation after ASDF was upgraded (if it was)
1.10860- (etypecase operation
1.10861- (operation (let ((name (type-of operation)))
1.10862- #'(lambda () (make-operation name))))
1.10863- ((or symbol string) (constantly operation))))
1.10864- (component-path (typecase component ;; to remake the component after ASDF upgrade
1.10865- (component (component-find-path component))
1.10866- (t component)))
1.10867- (system-name (labels ((first-name (x)
1.10868- (etypecase x
1.10869- ((or string symbol) x) ; NB: includes the NIL case.
1.10870- (cons (or (first-name (car x)) (first-name (cdr x)))))))
1.10871- (coerce-name (first-name component-path)))))
1.10872- (apply 'make-forcing :performable-p t :system system-name keys)
1.10873- ;; Before we operate on any system, make sure ASDF is up-to-date,
1.10874- ;; for if an upgrade is ever attempted at any later time, there may be BIG trouble.
1.10875- (unless (asdf-upgraded-p (toplevel-asdf-session))
1.10876- (setf (asdf-upgraded-p (toplevel-asdf-session)) t)
1.10877- (when (upgrade-asdf)
1.10878- ;; If we were upgraded, restart OPERATE the hardest of ways, for
1.10879- ;; its function may have been redefined.
1.10880- (return-from operate
1.10881- (with-asdf-session (:override t :override-cache t)
1.10882- (apply 'operate (funcall operation-remaker) component-path keys))))))
1.10883- ;; Setup proper bindings around any operate call.
1.10884- (let* ((*verbose-out* (and verbose *standard-output*))
1.10885- (*compile-file-warnings-behaviour* on-warnings)
1.10886- (*compile-file-failure-behaviour* on-failure)))
1.10887- (unwind-protect
1.10888- (progn
1.10889- (incf (operate-level))
1.10890- (call-next-method))
1.10891- (decf (operate-level)))))
1.10892-
1.10893- (defmethod operate :before ((operation operation) (component component)
1.10894- &key version)
1.10895- (unless (version-satisfies component version)
1.10896- (error 'missing-component-of-version :requires component :version version))
1.10897- (record-dependency nil operation component))
1.10898-
1.10899- (defmethod operate ((operation operation) (component component)
1.10900- &key plan-class plan-options)
1.10901- (let ((plan (apply 'make-plan plan-class operation component
1.10902- :forcing (forcing *asdf-session*) plan-options)))
1.10903- (perform-plan plan)
1.10904- (values operation plan)))
1.10905-
1.10906- (defun oos (operation component &rest args &key &allow-other-keys)
1.10907- (apply 'operate operation component args))
1.10908-
1.10909- (setf (documentation 'oos 'function)
1.10910- (format nil "Short for _operate on system_ and an alias for the OPERATE function.~%~%~a"
1.10911- (documentation 'operate 'function)))
1.10912-
1.10913- (define-condition recursive-operate (warning)
1.10914- ((operation :initarg :operation :reader condition-operation)
1.10915- (component :initarg :component :reader condition-component)
1.10916- (action :initarg :action :reader condition-action))
1.10917- (:report (lambda (c s)
1.10918- (format s (compatfmt "~@<Deprecated recursive use of (~S '~S '~S) while visiting ~S ~
1.10919-- please use proper dependencies instead~@:>")
1.10920- 'operate
1.10921- (type-of (condition-operation c))
1.10922- (component-find-path (condition-component c))
1.10923- (action-path (condition-action c)))))))
1.10924-
1.10925-;;;; Common operations
1.10926-(when-upgrading ()
1.10927- (defmethod component-depends-on ((o prepare-op) (s system))
1.10928- (call-next-method)))
1.10929-(with-upgradability ()
1.10930- (defclass build-op (non-propagating-operation) ()
1.10931- (:documentation "Since ASDF3, BUILD-OP is the recommended 'master' operation,
1.10932-to operate by default on a system or component, via the function BUILD.
1.10933-Its meaning is configurable via the :BUILD-OPERATION option of a component.
1.10934-which typically specifies the name of a specific operation to which to delegate the build,
1.10935-as a symbol or as a string later read as a symbol (after loading the defsystem-depends-on);
1.10936-if NIL is specified (the default), BUILD-OP falls back to LOAD-OP,
1.10937-that will load the system in the current image."))
1.10938- (defmethod component-depends-on ((o build-op) (c component))
1.10939- `((,(or (component-build-operation c) 'load-op) ,c)
1.10940- ,@(call-next-method)))
1.10941-
1.10942- (defun make (system &rest keys)
1.10943- "The recommended way to interact with ASDF3.1 is via (ASDF:MAKE :FOO).
1.10944-It will build system FOO using the operation BUILD-OP,
1.10945-the meaning of which is configurable by the system, and
1.10946-defaults to LOAD-OP, to load it in current image."
1.10947- (apply 'operate 'build-op system keys)
1.10948- t)
1.10949-
1.10950- (defun load-system (system &rest keys &key force force-not verbose version &allow-other-keys)
1.10951- "Shorthand for `(operate 'asdf:load-op system)`. See OPERATE for details."
1.10952- (declare (ignore force force-not verbose version))
1.10953- (apply 'operate 'load-op system keys)
1.10954- t)
1.10955-
1.10956- (defun load-systems* (systems &rest keys)
1.10957- "Loading multiple systems at once."
1.10958- (dolist (s systems) (apply 'load-system s keys)))
1.10959-
1.10960- (defun load-systems (&rest systems)
1.10961- "Loading multiple systems at once."
1.10962- (load-systems* systems))
1.10963-
1.10964- (defun compile-system (system &rest args &key force force-not verbose version &allow-other-keys)
1.10965- "Shorthand for `(asdf:operate 'asdf:compile-op system)`. See OPERATE for details."
1.10966- (declare (ignore force force-not verbose version))
1.10967- (apply 'operate 'compile-op system args)
1.10968- t)
1.10969-
1.10970- (defun test-system (system &rest args &key force force-not verbose version &allow-other-keys)
1.10971- "Shorthand for `(asdf:operate 'asdf:test-op system)`. See OPERATE for details."
1.10972- (declare (ignore force force-not verbose version))
1.10973- (apply 'operate 'test-op system args)
1.10974- t))
1.10975-
1.10976-;;;;; Define the function REQUIRE-SYSTEM, that, similarly to REQUIRE,
1.10977-;; only tries to load its specified target if it's not loaded yet.
1.10978-(with-upgradability ()
1.10979- (defun component-loaded-p (component)
1.10980- "Has the given COMPONENT been successfully loaded in the current image (yet)?
1.10981-Note that this returns true even if the component is not up to date."
1.10982- (if-let ((component (find-component component () :registered t)))
1.10983- (nth-value 1 (component-operation-time (make-operation 'load-op) component))))
1.10984-
1.10985- (defun already-loaded-systems ()
1.10986- "return a list of the names of the systems that have been successfully loaded so far"
1.10987- (mapcar 'coerce-name (remove-if-not 'component-loaded-p (registered-systems*)))))
1.10988-
1.10989-
1.10990-;;;; Define the class REQUIRE-SYSTEM, to be hooked into CL:REQUIRE when possible,
1.10991-;; i.e. for ABCL, CLISP, ClozureCL, CMUCL, ECL, MKCL and SBCL
1.10992-;; Note that despite the two being homonyms, the _function_ require-system
1.10993-;; and the _class_ require-system are quite distinct entities, fulfilling independent purposes.
1.10994-(with-upgradability ()
1.10995- (defvar *modules-being-required* nil)
1.10996-
1.10997- (defclass require-system (system)
1.10998- ((module :initarg :module :initform nil :accessor required-module))
1.10999- (:documentation "A SYSTEM subclass whose processing is handled by
1.11000-the implementation's REQUIRE rather than by internal ASDF mechanisms."))
1.11001-
1.11002- (defmethod perform ((o compile-op) (c require-system))
1.11003- nil)
1.11004-
1.11005- (defmethod perform ((o load-op) (s require-system))
1.11006- (let* ((module (or (required-module s) (coerce-name s)))
1.11007- (*modules-being-required* (cons module *modules-being-required*)))
1.11008- (assert (null (component-children s)))
1.11009- (require module)))
1.11010-
1.11011- (defmethod resolve-dependency-combination (component (combinator (eql :require)) arguments)
1.11012- (unless (and (length=n-p arguments 1)
1.11013- (typep (car arguments) '(or string (and symbol (not null)))))
1.11014- (parameter-error (compatfmt "~@<In ~S, bad dependency ~S for ~S. ~S takes one argument, a string or non-null symbol~@:>")
1.11015- 'resolve-dependency-combination
1.11016- (cons combinator arguments) component combinator))
1.11017- ;; :require must be prepared for some implementations providing modules using ASDF,
1.11018- ;; as SBCL used to do, and others may might do. Thus, the system provided in the end
1.11019- ;; would be a downcased name as per module-provide-asdf above. For the same reason,
1.11020- ;; we cannot assume that the system in the end will be of type require-system,
1.11021- ;; but must check whether we can use find-system and short-circuit cl:require.
1.11022- ;; Otherwise, calling cl:require could result in nasty reentrant calls between
1.11023- ;; cl:require and asdf:operate that could potentially blow up the stack,
1.11024- ;; all the while defeating the consistency of the dependency graph.
1.11025- (let* ((module (car arguments)) ;; NB: we already checked that it was not null
1.11026- ;; CMUCL, MKCL, SBCL like their module names to be all upcase.
1.11027- (module-name (string module))
1.11028- (system-name (string-downcase module))
1.11029- (system (find-system system-name nil)))
1.11030- (or system (let ((system (make-instance 'require-system :name system-name :module module-name)))
1.11031- (register-system system)
1.11032- system))))
1.11033-
1.11034- (defun module-provide-asdf (name)
1.11035- ;; We must use string-downcase, because modules are traditionally specified as symbols,
1.11036- ;; that implementations traditionally normalize as uppercase, for which we seek a system
1.11037- ;; with a name that is traditionally in lowercase. Case is lost along the way. That's fine.
1.11038- ;; We could make complex, non-portable rules to try to preserve case, and just documenting
1.11039- ;; them would be a hell that it would be a disservice to inflict on users.
1.11040- (let ((module-name (string name))
1.11041- (system-name (string-downcase name)))
1.11042- (unless (member module-name *modules-being-required* :test 'equal)
1.11043- (let ((*modules-being-required* (cons module-name *modules-being-required*))
1.11044- #+sbcl (sb-impl::*requiring* (remove module-name sb-impl::*requiring* :test 'equal)))
1.11045- (handler-bind
1.11046- (((or style-warning recursive-operate) #'muffle-warning)
1.11047- (missing-component (constantly nil))
1.11048- (fatal-condition
1.11049- #'(lambda (e)
1.11050- (format *error-output* (compatfmt "~@<ASDF could not load ~(~A~) because ~A.~@:>~%")
1.11051- name e))))
1.11052- (let ((*verbose-out* (make-broadcast-stream)))
1.11053- (let ((system (find-system system-name nil)))
1.11054- (when system
1.11055- ;; Do not use require-system after all, use load-system:
1.11056- ;; on the one hand, REQUIRE already uses *MODULES* not to load something twice,
1.11057- ;; on the other hand, REQUIRE-SYSTEM uses FORCE-NOT which may conflict with
1.11058- ;; the toplevel session forcing settings.
1.11059- (load-system system :verbose nil)
1.11060- t)))))))))
1.11061-
1.11062-
1.11063-;;;; Some upgrade magic
1.11064-(with-upgradability ()
1.11065- (defun restart-upgraded-asdf ()
1.11066- ;; If we're in the middle of something, restart it.
1.11067- (let ((systems-being-defined
1.11068- (when *asdf-session*
1.11069- (prog1
1.11070- (loop :for k :being :the hash-keys :of (asdf-cache)
1.11071- :when (eq (first k) 'find-system) :collect (second k))
1.11072- (clrhash (asdf-cache))))))
1.11073- ;; Regardless, clear defined systems, since they might be invalid
1.11074- ;; after an incompatible ASDF upgrade.
1.11075- (clear-registered-systems)
1.11076- ;; The configuration also may have to be upgraded.
1.11077- (upgrade-configuration)
1.11078- ;; If we were in the middle of an operation, be sure to restore the system being defined.
1.11079- (dolist (s systems-being-defined) (find-system s nil))))
1.11080- (register-hook-function '*post-upgrade-cleanup-hook* 'restart-upgraded-asdf))
1.11081-;;;; -------------------------------------------------------------------------
1.11082-;;;; Finding systems
1.11083-
1.11084-(uiop/package:define-package :asdf/find-system
1.11085- (:recycle :asdf/find-system :asdf)
1.11086- (:use :uiop/common-lisp :uiop :asdf/upgrade
1.11087- :asdf/session :asdf/component :asdf/system :asdf/operation :asdf/action :asdf/lisp-action
1.11088- :asdf/find-component :asdf/system-registry :asdf/plan :asdf/operate)
1.11089- (:import-from #:asdf/component #:%additional-input-files)
1.11090- (:export
1.11091- #:find-system #:locate-system #:load-asd #:define-op
1.11092- #:load-system-definition-error #:error-name #:error-pathname #:error-condition))
1.11093-(in-package :asdf/find-system)
1.11094-
1.11095-(with-upgradability ()
1.11096- (define-condition load-system-definition-error (system-definition-error)
1.11097- ((name :initarg :name :reader error-name)
1.11098- (pathname :initarg :pathname :reader error-pathname)
1.11099- (condition :initarg :condition :reader error-condition))
1.11100- (:report (lambda (c s)
1.11101- (format s (compatfmt "~@<Error while trying to load definition for system ~A from pathname ~A: ~3i~_~A~@:>")
1.11102- (error-name c) (error-pathname c) (error-condition c)))))
1.11103-
1.11104-
1.11105- ;;; Methods for find-system
1.11106-
1.11107- ;; Reject NIL as a system designator.
1.11108- (defmethod find-system ((name null) &optional (error-p t))
1.11109- (when error-p
1.11110- (sysdef-error (compatfmt "~@<NIL is not a valid system name~@:>"))))
1.11111-
1.11112- ;; Default method for find-system: resolve the argument using COERCE-NAME.
1.11113- (defmethod find-system (name &optional (error-p t))
1.11114- (find-system (coerce-name name) error-p))
1.11115-
1.11116- (defun find-system-if-being-defined (name)
1.11117- ;; This function finds systems being defined *in the current ASDF session*, as embodied by
1.11118- ;; its session cache, even before they are fully defined and registered in *registered-systems*.
1.11119- ;; The purpose of this function is to prevent races between two files that might otherwise
1.11120- ;; try overwrite each other's system objects, resulting in infinite loops and stack overflow.
1.11121- ;; This function explicitly MUST NOT find definitions merely registered in previous sessions.
1.11122- ;; NB: this function depends on a corresponding side-effect in parse-defsystem;
1.11123- ;; the precise protocol between the two functions may change in the future (or not).
1.11124- (first (gethash `(find-system ,(coerce-name name)) (asdf-cache))))
1.11125-
1.11126- (defclass define-op (non-propagating-operation) ()
1.11127- (:documentation "An operation to record dependencies on loading a .asd file."))
1.11128-
1.11129- (defmethod record-dependency ((plan null) (operation t) (component t))
1.11130- (unless (or (typep operation 'define-op)
1.11131- (and (typep operation 'load-op)
1.11132- (typep component 'system)
1.11133- (equal "asdf" (coerce-name component))))
1.11134- (if-let ((action (first (visiting-action-list *asdf-session*))))
1.11135- (let ((parent-operation (action-operation action))
1.11136- (parent-component (action-component action)))
1.11137- (cond
1.11138- ((and (typep parent-operation 'define-op)
1.11139- (typep parent-component 'system))
1.11140- (let ((action (cons operation component)))
1.11141- (unless (gethash action (definition-dependency-set parent-component))
1.11142- (push (cons operation component) (definition-dependency-list parent-component))
1.11143- (setf (gethash action (definition-dependency-set parent-component)) t))))
1.11144- (t
1.11145- (warn 'recursive-operate
1.11146- :operation operation :component component :action action)))))))
1.11147-
1.11148- (defmethod component-depends-on ((o define-op) (s system))
1.11149- `(;;NB: 1- ,@(system-defsystem-depends-on s)) ; Should be already included in the below.
1.11150- ;; 2- We don't call-next-method to avoid other methods
1.11151- ,@(loop :for (o . c) :in (definition-dependency-list s) :collect (list o c))))
1.11152-
1.11153- (defmethod component-depends-on ((o operation) (s system))
1.11154- `(,@(when (and (not (typep o 'define-op))
1.11155- (or (system-source-file s) (definition-dependency-list s)))
1.11156- `((define-op ,(primary-system-name s))))
1.11157- ,@(call-next-method)))
1.11158-
1.11159- (defmethod perform ((o operation) (c undefined-system))
1.11160- (sysdef-error "Trying to use undefined or incompletely defined system ~A" (coerce-name c)))
1.11161-
1.11162- ;; TODO: could this file be refactored so that locate-system is merely
1.11163- ;; the cache-priming call to input-files here?
1.11164- (defmethod input-files ((o define-op) (s system))
1.11165- (if-let ((asd (system-source-file s))) (list asd)))
1.11166-
1.11167- (defmethod perform ((o define-op) (s system))
1.11168- (nest
1.11169- (if-let ((pathname (first (input-files o s)))))
1.11170- (let ((readtable *readtable*) ;; save outer syntax tables. TODO: proper syntax-control
1.11171- (print-pprint-dispatch *print-pprint-dispatch*)))
1.11172- (with-standard-io-syntax)
1.11173- (let ((*print-readably* nil)
1.11174- ;; Note that our backward-compatible *readtable* is
1.11175- ;; a global readtable that gets globally side-effected. Ouch.
1.11176- ;; Same for the *print-pprint-dispatch* table.
1.11177- ;; We should do something about that for ASDF3 if possible, or else ASDF4.
1.11178- (*readtable* readtable) ;; restore inside syntax table
1.11179- (*print-pprint-dispatch* print-pprint-dispatch)
1.11180- (*package* (find-package :asdf-user))
1.11181- (*default-pathname-defaults*
1.11182- ;; resolve logical-pathnames so they won't wreak havoc in parsing namestrings.
1.11183- (pathname-directory-pathname (physicalize-pathname pathname)))))
1.11184- (handler-bind
1.11185- (((and error (not missing-component))
1.11186- #'(lambda (condition)
1.11187- (error 'load-system-definition-error
1.11188- :name (coerce-name s) :pathname pathname :condition condition))))
1.11189- (asdf-message (compatfmt "~&~@<; ~@;Loading system definition~@[ for ~A~] from ~A~@:>~%")
1.11190- (coerce-name s) pathname)
1.11191- ;; dependencies will depend on what's loaded via definition-dependency-list
1.11192- (unset-asdf-cache-entry `(component-depends-on ,o ,s))
1.11193- (unset-asdf-cache-entry `(input-files ,o ,s)))
1.11194- (load* pathname :external-format (encoding-external-format (detect-encoding pathname)))))
1.11195-
1.11196- (defun load-asd (pathname &key name)
1.11197- "Load system definitions from PATHNAME.
1.11198-NAME if supplied is the name of a system expected to be defined in that file.
1.11199-
1.11200-Do NOT try to load a .asd file directly with CL:LOAD. Always use ASDF:LOAD-ASD."
1.11201- (with-asdf-session ()
1.11202- ;; TODO: use OPERATE, so we consult the cache and only load once per session.
1.11203- (flet ((do-it (o c) (operate o c)))
1.11204- (let ((primary-name (primary-system-name (or name (pathname-name pathname))))
1.11205- (operation (make-operation 'define-op)))
1.11206- (if-let (system (registered-system primary-name))
1.11207- (progn
1.11208- ;; We already determine this to be obsolete ---
1.11209- ;; or should we move some tests from find-system to check for up-to-date-ness here?
1.11210- (setf (component-operation-time operation system) t
1.11211- (definition-dependency-list system) nil
1.11212- (definition-dependency-set system) (list-to-hash-set nil))
1.11213- (do-it operation system))
1.11214- (let ((system (make-instance 'undefined-system
1.11215- :name primary-name :source-file pathname)))
1.11216- (register-system system)
1.11217- (unwind-protect (do-it operation system)
1.11218- (when (typep system 'undefined-system)
1.11219- (clear-system system)))))))))
1.11220-
1.11221- (defvar *old-asdf-systems* (make-hash-table :test 'equal))
1.11222-
1.11223- ;; (Private) function to check that a system that was found isn't an asdf downgrade.
1.11224- ;; Returns T if everything went right, NIL if the system was an ASDF at an older version,
1.11225- ;; or UIOP of the same or older version, that shall not be loaded.
1.11226- ;; Also issue a warning if it was a strictly older version of ASDF.
1.11227- (defun check-not-old-asdf-system (name pathname)
1.11228- (or (not (member name '("asdf" "uiop") :test 'equal))
1.11229- (null pathname)
1.11230- (let* ((asdfp (equal name "asdf")) ;; otherwise, it's uiop
1.11231- (version-pathname
1.11232- (subpathname pathname "version" :type (if asdfp "lisp-expr" "lisp")))
1.11233- (version (and (probe-file* version-pathname :truename nil)
1.11234- (read-file-form version-pathname :at (if asdfp '(0) '(2 2 2)))))
1.11235- (old-version (asdf-version)))
1.11236- (cond
1.11237- ;; Same version is OK for ASDF, to allow loading from modified source.
1.11238- ;; However, do *not* load UIOP of the exact same version:
1.11239- ;; it was already loaded it as part of ASDF and would only be double-loading.
1.11240- ;; Be quiet about it, though, since it's a normal situation.
1.11241- ((equal old-version version) asdfp)
1.11242- ((version< old-version version) t) ;; newer version: Good!
1.11243- (t ;; old version: bad
1.11244- (ensure-gethash
1.11245- (list (namestring pathname) version) *old-asdf-systems*
1.11246- #'(lambda ()
1.11247- (let ((old-pathname (system-source-file (registered-system "asdf"))))
1.11248- (if asdfp
1.11249- (warn "~@<~
1.11250- You are using ASDF version ~A ~:[(probably from (require \"asdf\") ~
1.11251- or loaded by quicklisp)~;from ~:*~S~] and have an older version of ASDF ~
1.11252- ~:[(and older than 2.27 at that)~;~:*~A~] registered at ~S. ~
1.11253- Having an ASDF installed and registered is the normal way of configuring ASDF to upgrade itself, ~
1.11254- and having an old version registered is a configuration error. ~
1.11255- ASDF will ignore this configured system rather than downgrade itself. ~
1.11256- In the future, you may want to either: ~
1.11257- (a) upgrade this configured ASDF to a newer version, ~
1.11258- (b) install a newer ASDF and register it in front of the former in your configuration, or ~
1.11259- (c) uninstall or unregister this and any other old version of ASDF from your configuration. ~
1.11260- Note that the older ASDF might be registered implicitly through configuration inherited ~
1.11261- from your system installation, in which case you might have to specify ~
1.11262- :ignore-inherited-configuration in your in your ~~/.config/common-lisp/source-registry.conf ~
1.11263- or other source-registry configuration file, environment variable or lisp parameter. ~
1.11264- Indeed, a likely offender is an obsolete version of the cl-asdf debian or ubuntu package, ~
1.11265- that you might want to upgrade (if a recent enough version is available) ~
1.11266- or else remove altogether (since most implementations ship with a recent asdf); ~
1.11267- if you lack the system administration rights to upgrade or remove this package, ~
1.11268- then you might indeed want to either install and register a more recent version, ~
1.11269- or use :ignore-inherited-configuration to avoid registering the old one. ~
1.11270- Please consult ASDF documentation and/or experts.~@:>~%"
1.11271- old-version old-pathname version pathname)
1.11272- ;; NB: for UIOP, don't warn, just ignore.
1.11273- (warn "ASDF ~A (from ~A), UIOP ~A (from ~A)"
1.11274- old-version old-pathname version pathname)
1.11275- ))))
1.11276- nil))))) ;; only issue the warning the first time, but always return nil
1.11277-
1.11278- (defun locate-system (name)
1.11279- "Given a system NAME designator, try to locate where to load the system from.
1.11280-Returns six values: FOUNDP FOUND-SYSTEM PATHNAME PREVIOUS PREVIOUS-TIME PREVIOUS-PRIMARY
1.11281-FOUNDP is true when a system was found,
1.11282-either a new unregistered one or a previously registered one.
1.11283-FOUND-SYSTEM when not null is a SYSTEM object that may be REGISTER-SYSTEM'ed.
1.11284-PATHNAME when not null is a path from which to load the system,
1.11285-either associated with FOUND-SYSTEM, or with the PREVIOUS system.
1.11286-PREVIOUS when not null is a previously loaded SYSTEM object of same name.
1.11287-PREVIOUS-TIME when not null is the time at which the PREVIOUS system was loaded.
1.11288-PREVIOUS-PRIMARY when not null is the primary system for the PREVIOUS system."
1.11289- (with-asdf-session () ;; NB: We don't cache the results. We once used to, but it wasn't useful,
1.11290- ;; and keeping a negative cache was a bug (see lp#1335323), which required
1.11291- ;; explicit invalidation in clear-system and find-system (when unsucccessful).
1.11292- (let* ((name (coerce-name name))
1.11293- (previous (registered-system name)) ; load from disk if absent or newer on disk
1.11294- (previous-primary-name (and previous (primary-system-name previous)))
1.11295- (previous-primary-system (and previous-primary-name
1.11296- (registered-system previous-primary-name)))
1.11297- (previous-time (and previous-primary-system
1.11298- (component-operation-time 'define-op previous-primary-system)))
1.11299- (found (search-for-system-definition name))
1.11300- (found-system (and (typep found 'system) found))
1.11301- (pathname (ensure-pathname
1.11302- (or (and (typep found '(or pathname string)) (pathname found))
1.11303- (system-source-file found-system)
1.11304- (system-source-file previous))
1.11305- :want-absolute t :resolve-symlinks *resolve-symlinks*))
1.11306- (foundp (and (or found-system pathname previous) t)))
1.11307- (check-type found (or null pathname system))
1.11308- (unless (check-not-old-asdf-system name pathname)
1.11309- (check-type previous system) ;; asdf is preloaded, so there should be a previous one.
1.11310- (setf found-system nil pathname nil))
1.11311- (values foundp found-system pathname previous previous-time previous-primary-system))))
1.11312-
1.11313- ;; TODO: make a prepare-define-op node for this
1.11314- ;; so we can properly cache the answer rather than recompute it.
1.11315- (defun definition-dependencies-up-to-date-p (system)
1.11316- (check-type system system)
1.11317- (or (not (primary-system-p system))
1.11318- (handler-case
1.11319- (loop :with plan = (make-instance *plan-class*)
1.11320- :for action :in (definition-dependency-list system)
1.11321- :always (action-up-to-date-p
1.11322- plan (action-operation action) (action-component action))
1.11323- :finally
1.11324- (let ((o (make-operation 'define-op)))
1.11325- (multiple-value-bind (stamp done-p)
1.11326- (compute-action-stamp plan o system)
1.11327- (return (and (timestamp<= stamp (component-operation-time o system))
1.11328- done-p)))))
1.11329- (system-out-of-date () nil))))
1.11330-
1.11331- ;; Main method for find-system: first, make sure the computation is memoized in a session cache.
1.11332- ;; Unless the system is immutable, use locate-system to find the primary system;
1.11333- ;; reconcile the finding (if any) with any previous definition (in a previous session,
1.11334- ;; preloaded, with a previous configuration, or before filesystem changes), and
1.11335- ;; load a found .asd if appropriate. Finally, update registration table and return results.
1.11336- (defmethod find-system ((name string) &optional (error-p t))
1.11337- (nest
1.11338- (with-asdf-session (:key `(find-system ,name)))
1.11339- (let ((name-primary-p (primary-system-p name)))
1.11340- (unless name-primary-p (find-system (primary-system-name name) nil)))
1.11341- (or (and *immutable-systems* (gethash name *immutable-systems*) (registered-system name)))
1.11342- (multiple-value-bind (foundp found-system pathname previous previous-time previous-primary)
1.11343- (locate-system name)
1.11344- (assert (eq foundp (and (or found-system pathname previous) t))))
1.11345- (let ((previous-pathname (system-source-file previous))
1.11346- (system (or previous found-system)))
1.11347- (when (and found-system (not previous))
1.11348- (register-system found-system))
1.11349- (when (and system pathname)
1.11350- (setf (system-source-file system) pathname))
1.11351- (if-let ((stamp (get-file-stamp pathname)))
1.11352- (let ((up-to-date-p
1.11353- (and previous previous-primary
1.11354- (or (pathname-equal pathname previous-pathname)
1.11355- (and pathname previous-pathname
1.11356- (pathname-equal
1.11357- (physicalize-pathname pathname)
1.11358- (physicalize-pathname previous-pathname))))
1.11359- (timestamp<= stamp previous-time)
1.11360- ;; Check that all previous definition-dependencies are up-to-date,
1.11361- ;; traversing them without triggering the adding of nodes to the plan.
1.11362- ;; TODO: actually have a prepare-define-op, extract its timestamp,
1.11363- ;; and check that it is less than the stamp of the previous define-op ?
1.11364- (definition-dependencies-up-to-date-p previous-primary))))
1.11365- (unless up-to-date-p
1.11366- (restart-case
1.11367- (signal 'system-out-of-date :name name)
1.11368- (continue () :report "continue"))
1.11369- (load-asd pathname :name name)))))
1.11370- ;; Try again after having loaded from disk if needed
1.11371- (or (registered-system name)
1.11372- (when error-p (error 'missing-component :requires name)))))
1.11373-
1.11374- ;; Resolved forward reference for asdf/system-registry.
1.11375- (defun mark-component-preloaded (component)
1.11376- "Mark a component as preloaded."
1.11377- (let ((component (find-component component nil :registered t)))
1.11378- ;; Recurse to children, so asdf/plan will hopefully be happy.
1.11379- (map () 'mark-component-preloaded (component-children component))
1.11380- ;; Mark the timestamps of the common lisp-action operations as 0.
1.11381- (let ((cot (component-operation-times component)))
1.11382- (dolist (o `(,@(when (primary-system-p component) '(define-op))
1.11383- prepare-op compile-op load-op))
1.11384- (setf (gethash (make-operation o) cot) 0))))))
1.11385-;;;; -------------------------------------------------------------------------
1.11386-;;;; Defsystem
1.11387-
1.11388-(uiop/package:define-package :asdf/parse-defsystem
1.11389- (:recycle :asdf/parse-defsystem :asdf/defsystem :asdf)
1.11390- (:nicknames :asdf/defsystem) ;; previous name, to be compatible with, in case anyone cares
1.11391- (:use :uiop/common-lisp :asdf/driver :asdf/upgrade
1.11392- :asdf/session :asdf/component :asdf/system :asdf/system-registry
1.11393- :asdf/find-component :asdf/action :asdf/lisp-action :asdf/operate)
1.11394- (:import-from :asdf/system #:depends-on #:weakly-depends-on)
1.11395- ;; these needed for record-additional-system-input-file
1.11396- (:import-from :asdf/operation #:make-operation)
1.11397- (:import-from :asdf/component #:%additional-input-files)
1.11398- (:import-from :asdf/find-system #:define-op)
1.11399- (:export
1.11400- #:defsystem #:register-system-definition
1.11401- #:*default-component-class*
1.11402- #:determine-system-directory #:parse-component-form
1.11403- #:non-toplevel-system #:non-system-system #:bad-system-name
1.11404- #:*known-systems-with-bad-secondary-system-names*
1.11405- #:known-system-with-bad-secondary-system-names-p
1.11406- #:sysdef-error-component #:check-component-input
1.11407- #:explain
1.11408- ;; for extending the component types
1.11409- #:compute-component-children
1.11410- #:class-for-type))
1.11411-(in-package :asdf/parse-defsystem)
1.11412-
1.11413-;;; Pathname
1.11414-(with-upgradability ()
1.11415- (defun determine-system-directory (pathname)
1.11416- ;; The defsystem macro calls this function to determine the pathname of a system as follows:
1.11417- ;; 1. If the pathname argument is an pathname object (NOT a namestring),
1.11418- ;; that is already an absolute pathname, return it.
1.11419- ;; 2. Otherwise, the directory containing the LOAD-PATHNAME
1.11420- ;; is considered (as deduced from e.g. *LOAD-PATHNAME*), and
1.11421- ;; if it is indeed available and an absolute pathname, then
1.11422- ;; the PATHNAME argument is normalized to a relative pathname
1.11423- ;; as per PARSE-UNIX-NAMESTRING (with ENSURE-DIRECTORY T)
1.11424- ;; and merged into that DIRECTORY as per SUBPATHNAME.
1.11425- ;; Note: avoid *COMPILE-FILE-PATHNAME* because the .asd is loaded as source,
1.11426- ;; but may be from within the EVAL-WHEN of a file compilation.
1.11427- ;; If no absolute pathname was found, we return NIL.
1.11428- (check-type pathname (or null string pathname))
1.11429- (pathname-directory-pathname
1.11430- (resolve-symlinks*
1.11431- (ensure-absolute-pathname
1.11432- (parse-unix-namestring pathname :type :directory)
1.11433- #'(lambda () (ensure-absolute-pathname
1.11434- (load-pathname) 'get-pathname-defaults nil))
1.11435- nil)))))
1.11436-
1.11437-
1.11438-(when-upgrading (:version "3.3.4.17")
1.11439- ;; This turned into a generic function in 3.3.4.17
1.11440- (fmakunbound 'class-for-type))
1.11441-
1.11442-;;; Component class
1.11443-(with-upgradability ()
1.11444- ;; What :file gets interpreted as, unless overridden by a :default-component-class
1.11445- (defvar *default-component-class* 'cl-source-file)
1.11446-
1.11447- (defgeneric class-for-type (parent type-designator)
1.11448- (:documentation
1.11449- "Return a CLASS object to be used to instantiate components specified by TYPE-DESIGNATOR in the context of PARENT."))
1.11450-
1.11451- (defmethod class-for-type ((parent null) type)
1.11452- "If the PARENT is NIL, then TYPE must designate a subclass of SYSTEM."
1.11453- (or (coerce-class type :package :asdf/interface :super 'system :error nil)
1.11454- (sysdef-error "don't recognize component type ~S in the context of no parent" type)))
1.11455-
1.11456- (defmethod class-for-type ((parent parent-component) type)
1.11457- (or (coerce-class type :package :asdf/interface :super 'component :error nil)
1.11458- (and (eq type :file)
1.11459- (coerce-class
1.11460- (or (loop :for p = parent :then (component-parent p) :while p
1.11461- :thereis (module-default-component-class p))
1.11462- *default-component-class*)
1.11463- :package :asdf/interface :super 'component :error nil))
1.11464- (sysdef-error "don't recognize component type ~S" type))))
1.11465-
1.11466-
1.11467-;;; Check inputs
1.11468-(with-upgradability ()
1.11469- (define-condition non-system-system (system-definition-error)
1.11470- ((name :initarg :name :reader non-system-system-name)
1.11471- (class-name :initarg :class-name :reader non-system-system-class-name))
1.11472- (:report (lambda (c s)
1.11473- (format s (compatfmt "~@<Error while defining system ~S: class ~S isn't a subclass of ~S~@:>")
1.11474- (non-system-system-name c) (non-system-system-class-name c) 'system))))
1.11475-
1.11476- (define-condition non-toplevel-system (system-definition-error)
1.11477- ((parent :initarg :parent :reader non-toplevel-system-parent)
1.11478- (name :initarg :name :reader non-toplevel-system-name))
1.11479- (:report (lambda (c s)
1.11480- (format s (compatfmt "~@<Error while defining system: component ~S claims to have a system ~S as a child~@:>")
1.11481- (non-toplevel-system-parent c) (non-toplevel-system-name c)))))
1.11482-
1.11483- (define-condition bad-system-name (warning)
1.11484- ((name :initarg :name :reader component-name)
1.11485- (source-file :initarg :source-file :reader system-source-file))
1.11486- (:report (lambda (c s)
1.11487- (let* ((file (system-source-file c))
1.11488- (name (component-name c))
1.11489- (asd (pathname-name file)))
1.11490- (format s (compatfmt "~@<System definition file ~S contains definition for system ~S. ~
1.11491-Please only define ~S and secondary systems with a name starting with ~S (e.g. ~S) in that file.~@:>")
1.11492- file name asd (strcat asd "/") (strcat asd "/test"))))))
1.11493-
1.11494- (defun sysdef-error-component (msg type name value)
1.11495- (sysdef-error (strcat msg (compatfmt "~&~@<The value specified for ~(~A~) ~A is ~S~@:>"))
1.11496- type name value))
1.11497-
1.11498- (defun check-component-input (type name weakly-depends-on
1.11499- depends-on components)
1.11500- "A partial test of the values of a component."
1.11501- (unless (listp depends-on)
1.11502- (sysdef-error-component ":depends-on must be a list."
1.11503- type name depends-on))
1.11504- (unless (listp weakly-depends-on)
1.11505- (sysdef-error-component ":weakly-depends-on must be a list."
1.11506- type name weakly-depends-on))
1.11507- (unless (listp components)
1.11508- (sysdef-error-component ":components must be NIL or a list of components."
1.11509- type name components)))
1.11510-
1.11511-
1.11512- (defun record-additional-system-input-file (pathname component parent)
1.11513- (let* ((record-on (if parent
1.11514- (loop :with retval
1.11515- :for par = parent :then (component-parent par)
1.11516- :while par
1.11517- :do (setf retval par)
1.11518- :finally (return retval))
1.11519- component))
1.11520- (comp (if (typep record-on 'component)
1.11521- record-on
1.11522- ;; at this point there will be no parent for RECORD-ON
1.11523- (find-component record-on nil)))
1.11524- (op (make-operation 'define-op))
1.11525- (cell (or (assoc op (%additional-input-files comp))
1.11526- (let ((new-cell (list op)))
1.11527- (push new-cell (%additional-input-files comp))
1.11528- new-cell))))
1.11529- (pushnew pathname (cdr cell) :test 'pathname-equal)
1.11530- (values)))
1.11531-
1.11532- ;; Given a form used as :version specification, in the context of a system definition
1.11533- ;; in a file at PATHNAME, for given COMPONENT with given PARENT, normalize the form
1.11534- ;; to an acceptable ASDF-format version.
1.11535- (fmakunbound 'normalize-version) ;; signature changed between 2.27 and 2.31
1.11536- (defun normalize-version (form &key pathname component parent)
1.11537- (labels ((invalid (&optional (continuation "using NIL instead"))
1.11538- (warn (compatfmt "~@<Invalid :version specifier ~S~@[ for component ~S~]~@[ in ~S~]~@[ from file ~S~]~@[, ~A~]~@:>")
1.11539- form component parent pathname continuation))
1.11540- (invalid-parse (control &rest args)
1.11541- (unless (if-let (target (find-component parent component)) (builtin-system-p target))
1.11542- (apply 'warn control args)
1.11543- (invalid))))
1.11544- (if-let (v (typecase form
1.11545- ((or string null) form)
1.11546- (real
1.11547- (invalid "Substituting a string")
1.11548- (format nil "~D" form)) ;; 1.0 becomes "1.0"
1.11549- (cons
1.11550- (case (first form)
1.11551- ((:read-file-form)
1.11552- (destructuring-bind (subpath &key (at 0)) (rest form)
1.11553- (let ((path (subpathname pathname subpath)))
1.11554- (record-additional-system-input-file path component parent)
1.11555- (safe-read-file-form path
1.11556- :at at :package :asdf-user))))
1.11557- ((:read-file-line)
1.11558- (destructuring-bind (subpath &key (at 0)) (rest form)
1.11559- (let ((path (subpathname pathname subpath)))
1.11560- (record-additional-system-input-file path component parent)
1.11561- (safe-read-file-line (subpathname pathname subpath)
1.11562- :at at))))
1.11563- (otherwise
1.11564- (invalid))))
1.11565- (t
1.11566- (invalid))))
1.11567- (if-let (pv (parse-version v #'invalid-parse))
1.11568- (unparse-version pv)
1.11569- (invalid))))))
1.11570-
1.11571-
1.11572-;;; "inline methods"
1.11573-(with-upgradability ()
1.11574- (defparameter* +asdf-methods+
1.11575- '(perform-with-restarts perform explain output-files operation-done-p))
1.11576-
1.11577- (defun %remove-component-inline-methods (component)
1.11578- (dolist (name +asdf-methods+)
1.11579- (map ()
1.11580- ;; this is inefficient as most of the stored
1.11581- ;; methods will not be for this particular gf
1.11582- ;; But this is hardly performance-critical
1.11583- #'(lambda (m)
1.11584- (remove-method (symbol-function name) m))
1.11585- (component-inline-methods component)))
1.11586- (component-inline-methods component) nil)
1.11587-
1.11588- (defparameter *standard-method-combination-qualifiers*
1.11589- '(:around :before :after))
1.11590-
1.11591-;;; Find inline method definitions of the form
1.11592-;;;
1.11593-;;; :perform (test-op :before (operation component) ...)
1.11594-;;;
1.11595-;;; in REST (which is the plist of all DEFSYSTEM initargs) and define the specified methods.
1.11596- (defun %define-component-inline-methods (ret rest)
1.11597- ;; find key-value pairs that look like inline method definitions in REST. For each identified
1.11598- ;; definition, parse it and, if it is well-formed, define the method.
1.11599- (loop :for (key value) :on rest :by #'cddr
1.11600- :for name = (and (keywordp key) (find key +asdf-methods+ :test 'string=))
1.11601- :when name :do
1.11602- ;; parse VALUE as an inline method definition of the form
1.11603- ;;
1.11604- ;; (OPERATION-NAME [QUALIFIER] (OPERATION-PARAMETER COMPONENT-PARAMETER) &rest BODY)
1.11605- (destructuring-bind (operation-name &rest rest) value
1.11606- (let ((qualifiers '()))
1.11607- ;; ensure that OPERATION-NAME is a symbol.
1.11608- (unless (and (symbolp operation-name) (not (null operation-name)))
1.11609- (sysdef-error "Ill-formed inline method: ~S. The first element is not a symbol ~
1.11610- designating an operation but ~S."
1.11611- value operation-name))
1.11612- ;; ensure that REST starts with either a cons (potential lambda list, further checked
1.11613- ;; below) or a qualifier accepted by the standard method combination. Everything else
1.11614- ;; is ill-formed. In case of a valid qualifier, pop it from REST so REST now definitely
1.11615- ;; has to start with the lambda list.
1.11616- (cond
1.11617- ((consp (car rest)))
1.11618- ((not (member (car rest)
1.11619- *standard-method-combination-qualifiers*))
1.11620- (sysdef-error "Ill-formed inline method: ~S. Only a single of the standard ~
1.11621- qualifiers ~{~S~^ ~} is allowed, not ~S."
1.11622- value *standard-method-combination-qualifiers* (car rest)))
1.11623- (t
1.11624- (setf qualifiers (list (pop rest)))))
1.11625- ;; REST must start with a two-element lambda list.
1.11626- (unless (and (listp (car rest))
1.11627- (length=n-p (car rest) 2)
1.11628- (null (cddar rest)))
1.11629- (sysdef-error "Ill-formed inline method: ~S. The operation name ~S is not followed by ~
1.11630- a lambda-list of the form (OPERATION COMPONENT) and a method body."
1.11631- value operation-name))
1.11632- ;; define the method.
1.11633- (destructuring-bind ((o c) &rest body) rest
1.11634- (pushnew
1.11635- (eval `(defmethod ,name ,@qualifiers ((,o ,operation-name) (,c (eql ,ret))) ,@body))
1.11636- (component-inline-methods ret)))))))
1.11637-
1.11638- (defun %refresh-component-inline-methods (component rest)
1.11639- ;; clear methods, then add the new ones
1.11640- (%remove-component-inline-methods component)
1.11641- (%define-component-inline-methods component rest)))
1.11642-
1.11643-
1.11644-;;; Main parsing function
1.11645-(with-upgradability ()
1.11646- (defun parse-dependency-def (dd)
1.11647- (if (listp dd)
1.11648- (case (first dd)
1.11649- (:feature
1.11650- (unless (= (length dd) 3)
1.11651- (sysdef-error "Ill-formed feature dependency: ~s" dd))
1.11652- (let ((embedded (parse-dependency-def (third dd))))
1.11653- `(:feature ,(second dd) ,embedded)))
1.11654- (feature
1.11655- (sysdef-error "`feature' has been removed from the dependency spec language of ASDF. Use :feature instead in ~s." dd))
1.11656- (:require
1.11657- (unless (= (length dd) 2)
1.11658- (sysdef-error "Ill-formed require dependency: ~s" dd))
1.11659- dd)
1.11660- (:version
1.11661- (unless (= (length dd) 3)
1.11662- (sysdef-error "Ill-formed version dependency: ~s" dd))
1.11663- `(:version ,(coerce-name (second dd)) ,(third dd)))
1.11664- (otherwise (sysdef-error "Ill-formed dependency: ~s" dd)))
1.11665- (coerce-name dd)))
1.11666-
1.11667- (defun parse-dependency-defs (dd-list)
1.11668- "Parse the dependency defs in DD-LIST into canonical form by translating all
1.11669-system names contained using COERCE-NAME. Return the result."
1.11670- (mapcar 'parse-dependency-def dd-list))
1.11671-
1.11672- (defgeneric compute-component-children (component components serial-p)
1.11673- (:documentation
1.11674- "Return a list of children for COMPONENT.
1.11675-
1.11676-COMPONENTS is a list of the explicitly defined children descriptions.
1.11677-
1.11678-SERIAL-P is non-NIL if each child in COMPONENTS should depend on the previous
1.11679-children."))
1.11680-
1.11681- (defun stable-union (s1 s2 &key (test #'eql) (key 'identity))
1.11682- (append s1
1.11683- (remove-if #'(lambda (e2) (member (funcall key e2) (funcall key s1) :test test)) s2)))
1.11684-
1.11685- (defun parse-component-form (parent options &key previous-serial-components)
1.11686- (destructuring-bind
1.11687- (type name &rest rest &key
1.11688- (builtin-system-p () bspp)
1.11689- ;; the following list of keywords is reproduced below in the
1.11690- ;; remove-plist-keys form. important to keep them in sync
1.11691- components pathname perform explain output-files operation-done-p
1.11692- weakly-depends-on depends-on serial
1.11693- do-first if-component-dep-fails version
1.11694- ;; list ends
1.11695- &allow-other-keys) options
1.11696- (declare (ignore perform explain output-files operation-done-p builtin-system-p))
1.11697- (check-component-input type name weakly-depends-on depends-on components)
1.11698- (when (and parent
1.11699- (find-component parent name)
1.11700- (not ;; ignore the same object when rereading the defsystem
1.11701- (typep (find-component parent name)
1.11702- (class-for-type parent type))))
1.11703- (error 'duplicate-names :name name))
1.11704- (when do-first (error "DO-FIRST is not supported anymore as of ASDF 3"))
1.11705- (let* ((name (coerce-name name))
1.11706- (args `(:name ,name
1.11707- :pathname ,pathname
1.11708- ,@(when parent `(:parent ,parent))
1.11709- ,@(remove-plist-keys
1.11710- '(:components :pathname :if-component-dep-fails :version
1.11711- :perform :explain :output-files :operation-done-p
1.11712- :weakly-depends-on :depends-on :serial)
1.11713- rest)))
1.11714- (component (find-component parent name))
1.11715- (class (class-for-type parent type)))
1.11716- (when (and parent (subtypep class 'system))
1.11717- (error 'non-toplevel-system :parent parent :name name))
1.11718- (if component ; preserve identity
1.11719- (apply 'reinitialize-instance component args)
1.11720- (setf component (apply 'make-instance class args)))
1.11721- (component-pathname component) ; eagerly compute the absolute pathname
1.11722- (when (typep component 'system)
1.11723- ;; cache information for introspection
1.11724- (setf (slot-value component 'depends-on)
1.11725- (parse-dependency-defs depends-on)
1.11726- (slot-value component 'weakly-depends-on)
1.11727- ;; these must be a list of systems, cannot be features or versioned systems
1.11728- (mapcar 'coerce-name weakly-depends-on)))
1.11729- (let ((sysfile (system-source-file (component-system component)))) ;; requires the previous
1.11730- (when (and (typep component 'system) (not bspp))
1.11731- (setf (builtin-system-p component) (lisp-implementation-pathname-p sysfile)))
1.11732- (setf version (normalize-version version :component name :parent parent :pathname sysfile)))
1.11733- ;; Don't use the accessor: kluge to avoid upgrade issue on CCL 1.8.
1.11734- ;; A better fix is required.
1.11735- (setf (slot-value component 'version) version)
1.11736- (when (typep component 'parent-component)
1.11737- (setf (component-children component) (compute-component-children component components serial))
1.11738- (compute-children-by-name component))
1.11739- (when previous-serial-components
1.11740- (setf depends-on (stable-union depends-on previous-serial-components :test #'equal)))
1.11741- (when weakly-depends-on
1.11742- ;; ASDF4: deprecate this feature and remove it.
1.11743- (appendf depends-on
1.11744- (remove-if (complement #'(lambda (x) (find-system x nil))) weakly-depends-on)))
1.11745- ;; Used by POIU. ASDF4: rename to component-depends-on?
1.11746- (setf (component-sideway-dependencies component) depends-on)
1.11747- (%refresh-component-inline-methods component rest)
1.11748- (when if-component-dep-fails
1.11749- (error "The system definition for ~S uses deprecated ~
1.11750- ASDF option :IF-COMPONENT-DEP-FAILS. ~
1.11751- Starting with ASDF 3, please use :IF-FEATURE instead"
1.11752- (coerce-name (component-system component))))
1.11753- component)))
1.11754-
1.11755- (defmethod compute-component-children ((component parent-component) components serial-p)
1.11756- (loop
1.11757- :with previous-components = nil ; list of strings
1.11758- :for c-form :in components
1.11759- :for c = (parse-component-form component c-form
1.11760- :previous-serial-components previous-components)
1.11761- :for name :of-type string = (component-name c)
1.11762- :when serial-p
1.11763- ;; if this is an if-feature component, we need to make a serial link
1.11764- ;; from previous components to following components -- otherwise should
1.11765- ;; the IF-FEATURE component drop out, the chain of serial dependencies will be
1.11766- ;; broken.
1.11767- :unless (component-if-feature c)
1.11768- :do (setf previous-components nil)
1.11769- :end
1.11770- :and
1.11771- :do (push name previous-components)
1.11772- :end
1.11773- :collect c))
1.11774-
1.11775- ;; the following are all systems that Stas Boukarev maintains and refuses to fix,
1.11776- ;; hoping instead to make my life miserable. Instead, I just make ASDF ignore them.
1.11777- (defparameter* *known-systems-with-bad-secondary-system-names*
1.11778- (list-to-hash-set '("cl-ppcre" "cl-interpol")))
1.11779- (defun known-system-with-bad-secondary-system-names-p (asd-name)
1.11780- ;; Does .asd file with name ASD-NAME contain known exceptions
1.11781- ;; that should be screened out of checking for BAD-SYSTEM-NAME?
1.11782- (gethash asd-name *known-systems-with-bad-secondary-system-names*))
1.11783-
1.11784- (defun register-system-definition
1.11785- (name &rest options &key pathname (class 'system) (source-file () sfp)
1.11786- defsystem-depends-on &allow-other-keys)
1.11787- ;; The system must be registered before we parse the body,
1.11788- ;; otherwise we recur when trying to find an existing system
1.11789- ;; of the same name to reuse options (e.g. pathname) from.
1.11790- ;; To avoid infinite recursion in cases where you defsystem a system
1.11791- ;; that is registered to a different location to find-system,
1.11792- ;; we also need to remember it in the asdf-cache.
1.11793- (nest
1.11794- (with-asdf-session ())
1.11795- (let* ((name (coerce-name name))
1.11796- (source-file (if sfp source-file (resolve-symlinks* (load-pathname))))))
1.11797- (flet ((fix-case (x) (if (logical-pathname-p source-file) (string-downcase x) x))))
1.11798- (let* ((asd-name (and source-file
1.11799- (equal "asd" (fix-case (pathname-type source-file)))
1.11800- (fix-case (pathname-name source-file))))
1.11801- ;; note that PRIMARY-NAME is a *syntactically* primary name
1.11802- (primary-name (primary-system-name name)))
1.11803- (when (and asd-name
1.11804- (not (equal asd-name primary-name))
1.11805- (not (known-system-with-bad-secondary-system-names-p asd-name)))
1.11806- (warn (make-condition 'bad-system-name :source-file source-file :name name))))
1.11807- (let* (;; NB: handle defsystem-depends-on BEFORE to create the system object,
1.11808- ;; so that in case it fails, there is no incomplete object polluting the build.
1.11809- (checked-defsystem-depends-on
1.11810- (let* ((dep-forms (parse-dependency-defs defsystem-depends-on))
1.11811- (deps (loop :for spec :in dep-forms
1.11812- :when (resolve-dependency-spec nil spec)
1.11813- :collect :it)))
1.11814- (load-systems* deps)
1.11815- dep-forms))
1.11816- (system (or (find-system-if-being-defined name)
1.11817- (if-let (registered (registered-system name))
1.11818- (reset-system-class registered 'undefined-system
1.11819- :name name :source-file source-file)
1.11820- (register-system (make-instance 'undefined-system
1.11821- :name name :source-file source-file)))))
1.11822- (component-options
1.11823- (append
1.11824- (remove-plist-keys '(:defsystem-depends-on :class) options)
1.11825- ;; cache defsystem-depends-on in canonical form
1.11826- (when checked-defsystem-depends-on
1.11827- `(:defsystem-depends-on ,checked-defsystem-depends-on))))
1.11828- (directory (determine-system-directory pathname)))
1.11829- ;; This works hand in hand with asdf/find-system:find-system-if-being-defined:
1.11830- (set-asdf-cache-entry `(find-system ,name) (list system)))
1.11831- ;; We change-class AFTER we loaded the defsystem-depends-on
1.11832- ;; since the class might be defined as part of those.
1.11833- (let ((class (class-for-type nil class)))
1.11834- (unless (subtypep class 'system)
1.11835- (error 'non-system-system :name name :class-name (class-name class)))
1.11836- (unless (eq (type-of system) class)
1.11837- (reset-system-class system class)))
1.11838- (parse-component-form nil (list* :system name :pathname directory component-options))))
1.11839-
1.11840- (defmacro defsystem (name &body options)
1.11841- `(apply 'register-system-definition ',name ',options)))
1.11842-;;;; -------------------------------------------------------------------------
1.11843-;;;; ASDF-Bundle
1.11844-
1.11845-(uiop/package:define-package :asdf/bundle
1.11846- (:recycle :asdf/bundle :asdf)
1.11847- (:use :uiop/common-lisp :uiop :asdf/upgrade
1.11848- :asdf/component :asdf/system :asdf/operation
1.11849- :asdf/find-component ;; used by ECL
1.11850- :asdf/action :asdf/lisp-action :asdf/plan :asdf/operate :asdf/parse-defsystem)
1.11851- (:export
1.11852- #:bundle-op #:bundle-type #:program-system
1.11853- #:bundle-system #:bundle-pathname-type #:direct-dependency-files
1.11854- #:monolithic-op #:monolithic-bundle-op #:operation-monolithic-p
1.11855- #:basic-compile-bundle-op #:prepare-bundle-op
1.11856- #:compile-bundle-op #:load-bundle-op #:monolithic-compile-bundle-op #:monolithic-load-bundle-op
1.11857- #:lib-op #:monolithic-lib-op
1.11858- #:dll-op #:monolithic-dll-op
1.11859- #:deliver-asd-op #:monolithic-deliver-asd-op
1.11860- #:program-op #:image-op #:compiled-file #:precompiled-system #:prebuilt-system
1.11861- #:user-system-p #:user-system #:trivial-system-p
1.11862- #:prologue-code #:epilogue-code #:static-library))
1.11863-(in-package :asdf/bundle)
1.11864-
1.11865-(with-upgradability ()
1.11866- (defclass bundle-op (operation) ()
1.11867- (:documentation "base class for operations that bundle outputs from multiple components"))
1.11868- (defgeneric bundle-type (bundle-op))
1.11869-
1.11870- (defclass monolithic-op (operation) ()
1.11871- (:documentation "A MONOLITHIC operation operates on a system *and all of its
1.11872-dependencies*. So, for example, a monolithic concatenate operation will
1.11873-concatenate together a system's components and all of its dependencies, but a
1.11874-simple concatenate operation will concatenate only the components of the system
1.11875-itself."))
1.11876-
1.11877- (defclass monolithic-bundle-op (bundle-op monolithic-op)
1.11878- ;; Old style way of specifying prologue and epilogue on ECL: in the monolithic operation.
1.11879- ;; DEPRECATED. Supported replacement: Define slots on program-system instead.
1.11880- ((prologue-code :initform nil :accessor prologue-code)
1.11881- (epilogue-code :initform nil :accessor epilogue-code))
1.11882- (:documentation "operations that are both monolithic-op and bundle-op"))
1.11883-
1.11884- (defclass program-system (system)
1.11885- ;; New style (ASDF3.1) way of specifying prologue and epilogue on ECL: in the system
1.11886- ((prologue-code :initform nil :initarg :prologue-code :reader prologue-code)
1.11887- (epilogue-code :initform nil :initarg :epilogue-code :reader epilogue-code)
1.11888- (no-uiop :initform nil :initarg :no-uiop :reader no-uiop)
1.11889- (prefix-lisp-object-files :initarg :prefix-lisp-object-files
1.11890- :initform nil :accessor prefix-lisp-object-files)
1.11891- (postfix-lisp-object-files :initarg :postfix-lisp-object-files
1.11892- :initform nil :accessor postfix-lisp-object-files)
1.11893- (extra-object-files :initarg :extra-object-files
1.11894- :initform nil :accessor extra-object-files)
1.11895- (extra-build-args :initarg :extra-build-args
1.11896- :initform nil :accessor extra-build-args)))
1.11897-
1.11898- (defmethod prologue-code ((x system)) nil)
1.11899- (defmethod epilogue-code ((x system)) nil)
1.11900- (defmethod no-uiop ((x system)) nil)
1.11901- (defmethod prefix-lisp-object-files ((x system)) nil)
1.11902- (defmethod postfix-lisp-object-files ((x system)) nil)
1.11903- (defmethod extra-object-files ((x system)) nil)
1.11904- (defmethod extra-build-args ((x system)) nil)
1.11905-
1.11906- (defclass link-op (bundle-op) ()
1.11907- (:documentation "Abstract operation for linking files together"))
1.11908-
1.11909- (defclass gather-operation (bundle-op) ()
1.11910- (:documentation "Abstract operation for gathering many input files from a system"))
1.11911- (defgeneric gather-operation (gather-operation))
1.11912- (defmethod gather-operation ((o gather-operation)) nil)
1.11913- (defgeneric gather-type (gather-operation))
1.11914-
1.11915- (defun operation-monolithic-p (op)
1.11916- (typep op 'monolithic-op))
1.11917-
1.11918- ;; Dependencies of a gather-op are the actions of the dependent operation
1.11919- ;; for all the (sorted) required components for loading the system.
1.11920- ;; Monolithic operations typically use lib-op as the dependent operation,
1.11921- ;; and all system-level dependencies as required components.
1.11922- ;; Non-monolithic operations typically use compile-op as the dependent operation,
1.11923- ;; and all transitive sub-components as required components (excluding other systems).
1.11924- (defmethod component-depends-on ((o gather-operation) (s system))
1.11925- (let* ((mono (operation-monolithic-p o))
1.11926- (go (make-operation (or (gather-operation o) 'compile-op)))
1.11927- (bundle-p (typep go 'bundle-op))
1.11928- ;; In a non-mono operation, don't recurse to other systems.
1.11929- ;; In a mono operation gathering bundles, don't recurse inside systems.
1.11930- (component-type (if mono (if bundle-p 'system t) '(not system)))
1.11931- ;; In the end, only keep system bundles or non-system bundles, depending.
1.11932- (keep-component (if bundle-p 'system '(not system)))
1.11933- (deps
1.11934- ;; Required-components only looks at the dependencies of an action, excluding the action
1.11935- ;; itself, so it may be safely used by an action recursing on its dependencies (which
1.11936- ;; may or may not be an overdesigned API, since in practice we never use it that way).
1.11937- ;; Therefore, if we use :goal-operation 'load-op :keep-operation 'load-op, which looks
1.11938- ;; cleaner, we will miss the load-op on the requested system itself, which doesn't
1.11939- ;; matter for a regular system, but matters, a lot, for a package-inferred-system.
1.11940- ;; Using load-op as the goal operation and basic-compile-op as the keep-operation works
1.11941- ;; for our needs of gathering all the files we want to include in a bundle.
1.11942- ;; Note that we use basic-compile-op rather than compile-op so it will still work on
1.11943- ;; systems that would somehow load dependencies with load-bundle-op.
1.11944- (required-components
1.11945- s :other-systems mono :component-type component-type :keep-component keep-component
1.11946- :goal-operation 'load-op :keep-operation 'basic-compile-op)))
1.11947- `((,go ,@deps) ,@(call-next-method))))
1.11948-
1.11949- ;; Create a single fasl for the entire library
1.11950- (defclass basic-compile-bundle-op (bundle-op basic-compile-op) ()
1.11951- (:documentation "Base class for compiling into a bundle"))
1.11952- (defmethod bundle-type ((o basic-compile-bundle-op)) :fasb)
1.11953- (defmethod gather-type ((o basic-compile-bundle-op))
1.11954- #-(or clasp ecl mkcl) :fasl
1.11955- #+(or clasp ecl mkcl) :object)
1.11956-
1.11957- ;; Analog to prepare-op, for load-bundle-op and compile-bundle-op
1.11958- (defclass prepare-bundle-op (sideway-operation)
1.11959- ((sideway-operation
1.11960- :initform #+(or clasp ecl mkcl) 'load-bundle-op #-(or clasp ecl mkcl) 'load-op
1.11961- :allocation :class))
1.11962- (:documentation "Operation class for loading the bundles of a system's dependencies"))
1.11963-
1.11964- (defclass lib-op (link-op gather-operation non-propagating-operation) ()
1.11965- (:documentation "Compile the system and produce a linkable static library (.a/.lib)
1.11966-for all the linkable object files associated with the system. Compare with DLL-OP.
1.11967-
1.11968-On most implementations, these object files only include extensions to the runtime
1.11969-written in C or another language with a compiler producing linkable object files.
1.11970-On CLASP, ECL, MKCL, these object files _also_ include the contents of Lisp files
1.11971-themselves. In any case, this operation will produce what you need to further build
1.11972-a static runtime for your system, or a dynamic library to load in an existing runtime."))
1.11973- (defmethod bundle-type ((o lib-op)) :lib)
1.11974- (defmethod gather-type ((o lib-op)) :object)
1.11975-
1.11976- ;; What works: on ECL, CLASP(?), MKCL, we link the many .o files from the system into the .so;
1.11977- ;; on other implementations, we combine (usually concatenate) the .fasl files into one.
1.11978- (defclass compile-bundle-op (basic-compile-bundle-op selfward-operation gather-operation
1.11979- #+(or clasp ecl mkcl) link-op)
1.11980- ((selfward-operation :initform '(prepare-bundle-op) :allocation :class))
1.11981- (:documentation "This operator is an alternative to COMPILE-OP. Build a system
1.11982-and all of its dependencies, but build only a single (\"monolithic\") FASL, instead
1.11983-of one per source file, which may be more resource efficient. That monolithic
1.11984-FASL should be loaded with LOAD-BUNDLE-OP, rather than LOAD-OP."))
1.11985-
1.11986- (defclass load-bundle-op (basic-load-op selfward-operation)
1.11987- ((selfward-operation :initform '(prepare-bundle-op compile-bundle-op) :allocation :class))
1.11988- (:documentation "This operator is an alternative to LOAD-OP. Build a system
1.11989-and all of its dependencies, using COMPILE-BUNDLE-OP. The difference with
1.11990-respect to LOAD-OP is that it builds only a single FASL, which may be
1.11991-faster and more resource efficient."))
1.11992-
1.11993- ;; NB: since the monolithic-op's can't be sideway-operation's,
1.11994- ;; if we wanted lib-op, dll-op, deliver-asd-op to be sideway-operation's,
1.11995- ;; we'd have to have the monolithic-op not inherit from the main op,
1.11996- ;; but instead inherit from a basic-FOO-op as with basic-compile-bundle-op above.
1.11997-
1.11998- (defclass dll-op (link-op gather-operation non-propagating-operation) ()
1.11999- (:documentation "Compile the system and produce a dynamic loadable library (.so/.dll)
1.12000-for all the linkable object files associated with the system. Compare with LIB-OP."))
1.12001- (defmethod bundle-type ((o dll-op)) :dll)
1.12002- (defmethod gather-type ((o dll-op)) :object)
1.12003-
1.12004- (defclass deliver-asd-op (basic-compile-op selfward-operation)
1.12005- ((selfward-operation
1.12006- ;; TODO: implement link-op on all implementations, and make that
1.12007- ;; '(compile-bundle-op lib-op #-(or clasp ecl mkcl) dll-op)
1.12008- :initform '(compile-bundle-op #+(or clasp ecl mkcl) lib-op)
1.12009- :allocation :class))
1.12010- (:documentation "produce an asd file for delivering the system as a single fasl"))
1.12011-
1.12012-
1.12013- (defclass monolithic-deliver-asd-op (deliver-asd-op monolithic-bundle-op)
1.12014- ((selfward-operation
1.12015- ;; TODO: implement link-op on all implementations, and make that
1.12016- ;; '(monolithic-compile-bundle-op monolithic-lib-op #-(or clasp ecl mkcl) monolithic-dll-op)
1.12017- :initform '(monolithic-compile-bundle-op #+(or clasp ecl mkcl) monolithic-lib-op)
1.12018- :allocation :class))
1.12019- (:documentation "produce fasl and asd files for combined system and dependencies."))
1.12020-
1.12021- (defclass monolithic-compile-bundle-op
1.12022- (basic-compile-bundle-op monolithic-bundle-op
1.12023- #+(or clasp ecl mkcl) link-op gather-operation non-propagating-operation)
1.12024- ()
1.12025- (:documentation "Create a single fasl for the system and its dependencies."))
1.12026-
1.12027- (defclass monolithic-load-bundle-op (load-bundle-op monolithic-bundle-op)
1.12028- ((selfward-operation :initform 'monolithic-compile-bundle-op :allocation :class))
1.12029- (:documentation "Load a single fasl for the system and its dependencies."))
1.12030-
1.12031- (defclass monolithic-lib-op (lib-op monolithic-bundle-op non-propagating-operation) ()
1.12032- (:documentation "Compile the system and produce a linkable static library (.a/.lib)
1.12033-for all the linkable object files associated with the system or its dependencies. See LIB-OP."))
1.12034-
1.12035- (defclass monolithic-dll-op (dll-op monolithic-bundle-op non-propagating-operation) ()
1.12036- (:documentation "Compile the system and produce a dynamic loadable library (.so/.dll)
1.12037-for all the linkable object files associated with the system or its dependencies. See LIB-OP"))
1.12038-
1.12039- (defclass image-op (monolithic-bundle-op selfward-operation
1.12040- #+(or clasp ecl mkcl) link-op #+(or clasp ecl mkcl) gather-operation)
1.12041- ((selfward-operation :initform '(#-(or clasp ecl mkcl) load-op) :allocation :class))
1.12042- (:documentation "create an image file from the system and its dependencies"))
1.12043- (defmethod bundle-type ((o image-op)) :image)
1.12044- #+(or clasp ecl mkcl) (defmethod gather-operation ((o image-op)) 'lib-op)
1.12045- #+(or clasp ecl mkcl) (defmethod gather-type ((o image-op)) :static-library)
1.12046-
1.12047- (defclass program-op (image-op) ()
1.12048- (:documentation "create an executable file from the system and its dependencies"))
1.12049- (defmethod bundle-type ((o program-op)) :program)
1.12050-
1.12051- ;; From the ASDF-internal bundle-type identifier, get a filesystem-usable pathname type.
1.12052- (defun bundle-pathname-type (bundle-type)
1.12053- (etypecase bundle-type
1.12054- ((or null string) ;; pass through nil or string literal
1.12055- bundle-type)
1.12056- ((eql :no-output-file) ;; marker for a bundle-type that has NO output file
1.12057- (error "No output file, therefore no pathname type"))
1.12058- ((eql :fasl) ;; the type of a fasl
1.12059- (compile-file-type)) ; on image-based platforms, used as input and output
1.12060- ((eql :fasb) ;; the type of a fasl
1.12061- #-(or clasp ecl mkcl) (compile-file-type) ; on image-based platforms, used as input and output
1.12062- #+(or ecl mkcl) "fasb"
1.12063- #+clasp "fasp") ; on C-linking platforms, only used as output for system bundles
1.12064- ((member :image)
1.12065- #+allegro "dxl"
1.12066- #+(and clisp os-windows) "exe"
1.12067- #-(or allegro (and clisp os-windows)) "image")
1.12068- ;; NB: on CLASP and ECL these implementations, we better agree with
1.12069- ;; (compile-file-type :type bundle-type))
1.12070- ((eql :object) ;; the type of a linkable object file
1.12071- (os-cond ((os-unix-p)
1.12072- #+clasp "fasp" ;(core:build-extension cmp:*default-object-type*)
1.12073- #-clasp "o")
1.12074- ((os-windows-p) (if (featurep '(:or :mingw32 :mingw64)) "o" "obj"))))
1.12075- ((member :lib :static-library) ;; the type of a linkable library
1.12076- (os-cond ((os-unix-p) "a")
1.12077- ((os-windows-p) (if (featurep '(:or :mingw32 :mingw64)) "a" "lib"))))
1.12078- ((member :dll :shared-library) ;; the type of a shared library
1.12079- (os-cond ((os-macosx-p) "dylib") ((os-unix-p) "so") ((os-windows-p) "dll")))
1.12080- ((eql :program) ;; the type of an executable program
1.12081- (os-cond ((os-unix-p) nil) ((os-windows-p) "exe")))))
1.12082-
1.12083- ;; Compute the output-files for a given bundle action
1.12084- (defun bundle-output-files (o c)
1.12085- (let ((bundle-type (bundle-type o)))
1.12086- (unless (or (eq bundle-type :no-output-file) ;; NIL already means something regarding type.
1.12087- (and (null (input-files o c)) (not (member bundle-type '(:image :program)))))
1.12088- (let ((name (or (component-build-pathname c)
1.12089- (let ((suffix
1.12090- (unless (typep o 'program-op)
1.12091- ;; "." is no good separator for Logical Pathnames, so we use "--"
1.12092- (if (operation-monolithic-p o)
1.12093- "--all-systems"
1.12094- ;; These use a different type .fasb or .a instead of .fasl
1.12095- #-(or clasp ecl mkcl) "--system"))))
1.12096- (format nil "~A~@[~A~]" (coerce-filename (component-name c)) suffix))))
1.12097- (type (bundle-pathname-type bundle-type)))
1.12098- (values (list (subpathname (component-pathname c) name :type type))
1.12099- (eq (class-of o) (coerce-class (component-build-operation c)
1.12100- :package :asdf/interface
1.12101- :super 'operation
1.12102- :error nil)))))))
1.12103-
1.12104- (defmethod output-files ((o bundle-op) (c system))
1.12105- (bundle-output-files o c))
1.12106-
1.12107- #-(or clasp ecl mkcl)
1.12108- (progn
1.12109- (defmethod perform ((o image-op) (c system))
1.12110- (dump-image (output-file o c) :executable (typep o 'program-op)))
1.12111- (defmethod perform :before ((o program-op) (c system))
1.12112- (setf *image-entry-point* (ensure-function (component-entry-point c)))))
1.12113-
1.12114- (defclass compiled-file (file-component)
1.12115- ((type :initform #-(or clasp ecl mkcl) (compile-file-type) #+(or clasp ecl mkcl) "fasb"))
1.12116- (:documentation "Class for a file that is already compiled,
1.12117-e.g. as part of the implementation, of an outer build system that calls into ASDF,
1.12118-or of opaque libraries shipped along the source code."))
1.12119-
1.12120- (defclass precompiled-system (system)
1.12121- ((build-pathname :initarg :fasb :initarg :fasl))
1.12122- (:documentation "Class For a system that is delivered as a precompiled fasl"))
1.12123-
1.12124- (defclass prebuilt-system (system)
1.12125- ((build-pathname :initarg :static-library :initarg :lib
1.12126- :accessor prebuilt-system-static-library))
1.12127- (:documentation "Class for a system delivered with a linkable static library (.a/.lib)")))
1.12128-
1.12129-
1.12130-;;;
1.12131-;;; BUNDLE-OP
1.12132-;;;
1.12133-;;; This operation takes all components from one or more systems and
1.12134-;;; creates a single output file, which may be
1.12135-;;; a FASL, a statically linked library, a shared library, etc.
1.12136-;;; The different targets are defined by specialization.
1.12137-;;;
1.12138-(when-upgrading (:version "3.2.0")
1.12139- ;; Cancel any previously defined method
1.12140- (defmethod initialize-instance :after ((instance bundle-op) &rest initargs &key &allow-other-keys)
1.12141- (declare (ignore initargs))))
1.12142-
1.12143-(with-upgradability ()
1.12144- (defgeneric trivial-system-p (component))
1.12145-
1.12146- (defun user-system-p (s)
1.12147- (and (typep s 'system)
1.12148- (not (builtin-system-p s))
1.12149- (not (trivial-system-p s)))))
1.12150-
1.12151-(eval-when (#-lispworks :compile-toplevel :load-toplevel :execute)
1.12152- (deftype user-system () '(and system (satisfies user-system-p))))
1.12153-
1.12154-;;;
1.12155-;;; First we handle monolithic bundles.
1.12156-;;; These are standalone systems which contain everything,
1.12157-;;; including other ASDF systems required by the current one.
1.12158-;;; A PROGRAM is always monolithic.
1.12159-;;;
1.12160-;;; MONOLITHIC SHARED LIBRARIES, PROGRAMS, FASL
1.12161-;;;
1.12162-(with-upgradability ()
1.12163- (defun direct-dependency-files (o c &key (test 'identity) (key 'output-files) &allow-other-keys)
1.12164- ;; This function selects output files from direct dependencies;
1.12165- ;; your component-depends-on method must gather the correct dependencies in the correct order.
1.12166- (while-collecting (collect)
1.12167- (map-direct-dependencies
1.12168- o c #'(lambda (sub-o sub-c)
1.12169- (loop :for f :in (funcall key sub-o sub-c)
1.12170- :when (funcall test f) :do (collect f))))))
1.12171-
1.12172- (defun pathname-type-equal-function (type)
1.12173- #'(lambda (p) (equalp (pathname-type p) type)))
1.12174-
1.12175- (defmethod input-files ((o gather-operation) (c system))
1.12176- (unless (eq (bundle-type o) :no-output-file)
1.12177- (direct-dependency-files
1.12178- o c :key 'output-files
1.12179- :test (pathname-type-equal-function (bundle-pathname-type (gather-type o))))))
1.12180-
1.12181- ;; Find the operation that produces a given bundle-type
1.12182- (defun select-bundle-operation (type &optional monolithic)
1.12183- (ecase type
1.12184- ((:dll :shared-library)
1.12185- (if monolithic 'monolithic-dll-op 'dll-op))
1.12186- ((:lib :static-library)
1.12187- (if monolithic 'monolithic-lib-op 'lib-op))
1.12188- ((:fasb)
1.12189- (if monolithic 'monolithic-compile-bundle-op 'compile-bundle-op))
1.12190- ((:image)
1.12191- 'image-op)
1.12192- ((:program)
1.12193- 'program-op))))
1.12194-
1.12195-;;;
1.12196-;;; LOAD-BUNDLE-OP
1.12197-;;;
1.12198-;;; This is like ASDF's LOAD-OP, but using bundle fasl files.
1.12199-;;;
1.12200-(with-upgradability ()
1.12201- (defmethod component-depends-on ((o load-bundle-op) (c system))
1.12202- `((,o ,@(component-sideway-dependencies c))
1.12203- (,(if (user-system-p c) 'compile-bundle-op 'load-op) ,c)
1.12204- ,@(call-next-method)))
1.12205-
1.12206- (defmethod input-files ((o load-bundle-op) (c system))
1.12207- (when (user-system-p c)
1.12208- (output-files (find-operation o 'compile-bundle-op) c)))
1.12209-
1.12210- (defmethod perform ((o load-bundle-op) (c system))
1.12211- (when (input-files o c)
1.12212- (perform-lisp-load-fasl o c)))
1.12213-
1.12214- (defmethod mark-operation-done :after ((o load-bundle-op) (c system))
1.12215- (mark-operation-done (find-operation o 'load-op) c)))
1.12216-
1.12217-;;;
1.12218-;;; PRECOMPILED FILES
1.12219-;;;
1.12220-;;; This component can be used to distribute ASDF systems in precompiled form.
1.12221-;;; Only useful when the dependencies have also been precompiled.
1.12222-;;;
1.12223-(with-upgradability ()
1.12224- (defmethod trivial-system-p ((s system))
1.12225- (every #'(lambda (c) (typep c 'compiled-file)) (component-children s)))
1.12226-
1.12227- (defmethod input-files ((o operation) (c compiled-file))
1.12228- (list (component-pathname c)))
1.12229- (defmethod perform ((o load-op) (c compiled-file))
1.12230- (perform-lisp-load-fasl o c))
1.12231- (defmethod perform ((o load-source-op) (c compiled-file))
1.12232- (perform (find-operation o 'load-op) c))
1.12233- (defmethod perform ((o operation) (c compiled-file))
1.12234- nil))
1.12235-
1.12236-;;;
1.12237-;;; Pre-built systems
1.12238-;;;
1.12239-(with-upgradability ()
1.12240- (defmethod trivial-system-p ((s prebuilt-system))
1.12241- t)
1.12242-
1.12243- (defmethod perform ((o link-op) (c prebuilt-system))
1.12244- nil)
1.12245-
1.12246- (defmethod perform ((o basic-compile-bundle-op) (c prebuilt-system))
1.12247- nil)
1.12248-
1.12249- (defmethod perform ((o lib-op) (c prebuilt-system))
1.12250- nil)
1.12251-
1.12252- (defmethod perform ((o dll-op) (c prebuilt-system))
1.12253- nil)
1.12254-
1.12255- (defmethod component-depends-on ((o gather-operation) (c prebuilt-system))
1.12256- nil)
1.12257-
1.12258- (defmethod output-files ((o lib-op) (c prebuilt-system))
1.12259- (values (list (prebuilt-system-static-library c)) t)))
1.12260-
1.12261-
1.12262-;;;
1.12263-;;; PREBUILT SYSTEM CREATOR
1.12264-;;;
1.12265-(with-upgradability ()
1.12266- (defmethod output-files ((o deliver-asd-op) (s system))
1.12267- (list (make-pathname :name (coerce-filename (component-name s)) :type "asd"
1.12268- :defaults (component-pathname s))))
1.12269-
1.12270- ;; because of name collisions between the output files of different
1.12271- ;; subclasses of DELIVER-ASD-OP, we cannot trust the file system to
1.12272- ;; tell us if the output file is up-to-date, so just treat the
1.12273- ;; operation as never being done.
1.12274- (defmethod operation-done-p ((o deliver-asd-op) (s system))
1.12275- (declare (ignorable o s))
1.12276- nil)
1.12277-
1.12278- (defun space-for-crlf (s)
1.12279- (substitute-if #\space #'(lambda (x) (find x +crlf+)) s))
1.12280-
1.12281- (defmethod perform ((o deliver-asd-op) (s system))
1.12282- "Write an ASDF system definition for loading S as a delivered system."
1.12283- (let* ((inputs (input-files o s))
1.12284- (fasl (first inputs))
1.12285- (library (second inputs))
1.12286- (asd (output-file o s))
1.12287- (name (if (and fasl asd) (pathname-name asd) (return-from perform)))
1.12288- (version (component-version s))
1.12289- (dependencies
1.12290- (if (operation-monolithic-p o)
1.12291- ;; We want only dependencies, and we use basic-load-op rather than load-op so that
1.12292- ;; this will keep working on systems that load dependencies with load-bundle-op
1.12293- (remove-if-not 'builtin-system-p
1.12294- (required-components s :component-type 'system
1.12295- :keep-operation 'basic-load-op))
1.12296- (while-collecting (x) ;; resolve the sideway-dependencies of s
1.12297- (map-direct-dependencies
1.12298- 'prepare-op s
1.12299- #'(lambda (o c)
1.12300- (when (and (typep o 'load-op) (typep c 'system))
1.12301- (x c)))))))
1.12302- (depends-on (mapcar 'coerce-name dependencies)))
1.12303- (when (pathname-equal asd (system-source-file s))
1.12304- (cerror "overwrite the asd file"
1.12305- "~/asdf-action:format-action/ is going to overwrite the system definition file ~S ~
1.12306-which is probably not what you want; you probably need to tweak your output translations."
1.12307- (cons o s) asd))
1.12308- (with-open-file (s asd :direction :output :if-exists :supersede
1.12309- :if-does-not-exist :create)
1.12310- (format s ";;; Prebuilt~:[~; monolithic~] ASDF definition for system ~A~%"
1.12311- (operation-monolithic-p o) name)
1.12312- ;; this can cause bugs in cases where one of the functions returns a multi-line
1.12313- ;; string
1.12314- (let ((description-string (format nil ";;; Built for ~A ~A on a ~A/~A ~A"
1.12315- (lisp-implementation-type)
1.12316- (lisp-implementation-version)
1.12317- (software-type)
1.12318- (machine-type)
1.12319- (software-version))))
1.12320- ;; ensure the whole thing is on one line
1.12321- (println (space-for-crlf description-string) s))
1.12322- (let ((*package* (find-package :asdf-user)))
1.12323- (pprint `(defsystem ,name
1.12324- :class prebuilt-system
1.12325- :version ,version
1.12326- :depends-on ,depends-on
1.12327- :components ((:compiled-file ,(pathname-name fasl)))
1.12328- ,@(when library `(:lib ,(file-namestring library))))
1.12329- s)
1.12330- (terpri s)))))
1.12331-
1.12332- #-(or clasp ecl mkcl)
1.12333- (defmethod perform ((o basic-compile-bundle-op) (c system))
1.12334- (let* ((input-files (input-files o c))
1.12335- (fasl-files (remove (compile-file-type) input-files :key #'pathname-type :test-not #'equalp))
1.12336- (non-fasl-files (remove (compile-file-type) input-files :key #'pathname-type :test #'equalp))
1.12337- (output-files (output-files o c)) ; can't use OUTPUT-FILE fn because possibility it's NIL
1.12338- (output-file (first output-files)))
1.12339- (assert (eq (not input-files) (not output-files)))
1.12340- (when input-files
1.12341- (when non-fasl-files
1.12342- (error "On ~A, asdf/bundle can only bundle FASL files, but these were also produced: ~S"
1.12343- (implementation-type) non-fasl-files))
1.12344- (when (or (prologue-code c) (epilogue-code c))
1.12345- (error "prologue-code and epilogue-code are not supported on ~A"
1.12346- (implementation-type)))
1.12347- (with-staging-pathname (output-file)
1.12348- (combine-fasls fasl-files output-file)))))
1.12349-
1.12350- (defmethod input-files ((o load-op) (s precompiled-system))
1.12351- (bundle-output-files (find-operation o 'compile-bundle-op) s))
1.12352-
1.12353- (defmethod perform ((o load-op) (s precompiled-system))
1.12354- (perform-lisp-load-fasl o s))
1.12355-
1.12356- (defmethod component-depends-on ((o load-bundle-op) (s precompiled-system))
1.12357- `((load-op ,s) ,@(call-next-method))))
1.12358-
1.12359-#| ;; Example use:
1.12360-(asdf:defsystem :precompiled-asdf-utils :class asdf::precompiled-system :fasl (asdf:apply-output-translations (asdf:system-relative-pathname :asdf-utils "asdf-utils.system.fasl")))
1.12361-(asdf:load-system :precompiled-asdf-utils)
1.12362-|#
1.12363-
1.12364-#+(or clasp ecl mkcl)
1.12365-(with-upgradability ()
1.12366- (defun system-module-pathname (module)
1.12367- (let ((name (coerce-name module)))
1.12368- (some
1.12369- 'file-exists-p
1.12370- (list
1.12371- #+clasp (compile-file-pathname (make-pathname :name name :defaults "sys:") :output-type :object)
1.12372- #+ecl (compile-file-pathname (make-pathname :name name :defaults "sys:") :type :lib)
1.12373- #+ecl (compile-file-pathname (make-pathname :name (strcat "lib" name) :defaults "sys:") :type :lib)
1.12374- #+ecl (compile-file-pathname (make-pathname :name name :defaults "sys:") :type :object)
1.12375- #+mkcl (make-pathname :name name :type (bundle-pathname-type :lib) :defaults #p"sys:")
1.12376- #+mkcl (make-pathname :name name :type (bundle-pathname-type :lib) :defaults #p"sys:contrib;")))))
1.12377-
1.12378- (defun make-prebuilt-system (name &optional (pathname (system-module-pathname name)))
1.12379- "Creates a prebuilt-system if PATHNAME isn't NIL."
1.12380- (when pathname
1.12381- (make-instance 'prebuilt-system
1.12382- :name (coerce-name name)
1.12383- :static-library (resolve-symlinks* pathname))))
1.12384-
1.12385- (defun linkable-system (x)
1.12386- (or ;; If the system is available as source, use it.
1.12387- (if-let (s (find-system x))
1.12388- (and (output-files 'lib-op s) s))
1.12389- ;; If an ASDF upgrade is available from source, but not a UIOP upgrade to that,
1.12390- ;; then use the asdf/driver system instead of
1.12391- ;; the UIOP that was disabled by check-not-old-asdf-system.
1.12392- (if-let (s (and (equal (coerce-name x) "uiop")
1.12393- (output-files 'lib-op "asdf")
1.12394- (find-system "asdf/driver")))
1.12395- (and (output-files 'lib-op s) s))
1.12396- ;; If there was no source upgrade, look for modules provided by the implementation.
1.12397- (if-let (p (system-module-pathname (coerce-name x)))
1.12398- (make-prebuilt-system x p))))
1.12399-
1.12400- (defmethod component-depends-on :around ((o image-op) (c system))
1.12401- (let* ((next (call-next-method))
1.12402- (deps (make-hash-table :test 'equal))
1.12403- (linkable (loop :for (do . dcs) :in next :collect
1.12404- (cons do
1.12405- (loop :for dc :in dcs
1.12406- :for dep = (and dc (resolve-dependency-spec c dc))
1.12407- :when dep
1.12408- :do (setf (gethash (coerce-name (component-system dep)) deps) t)
1.12409- :collect (or (and (typep dep 'system) (linkable-system dep)) dep))))))
1.12410- `((lib-op
1.12411- ,@(unless (no-uiop c)
1.12412- (list (linkable-system "cmp")
1.12413- (unless (or (and (gethash "uiop" deps) (linkable-system "uiop"))
1.12414- (and (gethash "asdf" deps) (linkable-system "asdf")))
1.12415- (or (linkable-system "uiop")
1.12416- (linkable-system "asdf")
1.12417- "asdf")))))
1.12418- ,@linkable)))
1.12419-
1.12420- (defmethod perform ((o link-op) (c system))
1.12421- (let* ((object-files (input-files o c))
1.12422- (output (output-files o c))
1.12423- (bundle (first output))
1.12424- (programp (typep o 'program-op))
1.12425- (kind (bundle-type o)))
1.12426- (when output
1.12427- (apply 'create-image
1.12428- bundle (append
1.12429- (when programp (prefix-lisp-object-files c))
1.12430- object-files
1.12431- (when programp (postfix-lisp-object-files c)))
1.12432- :kind kind
1.12433- :prologue-code (when programp (prologue-code c))
1.12434- :epilogue-code (when programp (epilogue-code c))
1.12435- :build-args (when programp (extra-build-args c))
1.12436- :extra-object-files (when programp (extra-object-files c))
1.12437- :no-uiop (no-uiop c)
1.12438- (when programp `(:entry-point ,(component-entry-point c))))))))
1.12439-;;;; -------------------------------------------------------------------------
1.12440-;;;; Concatenate-source
1.12441-
1.12442-(uiop/package:define-package :asdf/concatenate-source
1.12443- (:recycle :asdf/concatenate-source :asdf)
1.12444- (:use :uiop/common-lisp :uiop :asdf/upgrade
1.12445- :asdf/component :asdf/operation
1.12446- :asdf/system
1.12447- :asdf/action :asdf/lisp-action :asdf/plan :asdf/bundle)
1.12448- (:export
1.12449- #:concatenate-source-op
1.12450- #:load-concatenated-source-op
1.12451- #:compile-concatenated-source-op
1.12452- #:load-compiled-concatenated-source-op
1.12453- #:monolithic-concatenate-source-op
1.12454- #:monolithic-load-concatenated-source-op
1.12455- #:monolithic-compile-concatenated-source-op
1.12456- #:monolithic-load-compiled-concatenated-source-op))
1.12457-(in-package :asdf/concatenate-source)
1.12458-
1.12459-;;;
1.12460-;;; Concatenate sources
1.12461-;;;
1.12462-(with-upgradability ()
1.12463- ;; Base classes for both regular and monolithic concatenate-source operations
1.12464- (defclass basic-concatenate-source-op (bundle-op) ())
1.12465- (defmethod bundle-type ((o basic-concatenate-source-op)) "lisp")
1.12466- (defclass basic-load-concatenated-source-op (basic-load-op selfward-operation) ())
1.12467- (defclass basic-compile-concatenated-source-op (basic-compile-op selfward-operation) ())
1.12468- (defclass basic-load-compiled-concatenated-source-op (basic-load-op selfward-operation) ())
1.12469-
1.12470- ;; Regular concatenate-source operations
1.12471- (defclass concatenate-source-op (basic-concatenate-source-op non-propagating-operation) ()
1.12472- (:documentation "Operation to concatenate all sources in a system into a single file"))
1.12473- (defclass load-concatenated-source-op (basic-load-concatenated-source-op)
1.12474- ((selfward-operation :initform '(prepare-op concatenate-source-op) :allocation :class))
1.12475- (:documentation "Operation to load the result of concatenate-source-op as source"))
1.12476- (defclass compile-concatenated-source-op (basic-compile-concatenated-source-op)
1.12477- ((selfward-operation :initform '(prepare-op concatenate-source-op) :allocation :class))
1.12478- (:documentation "Operation to compile the result of concatenate-source-op"))
1.12479- (defclass load-compiled-concatenated-source-op (basic-load-compiled-concatenated-source-op)
1.12480- ((selfward-operation :initform '(prepare-op compile-concatenated-source-op) :allocation :class))
1.12481- (:documentation "Operation to load the result of compile-concatenated-source-op"))
1.12482-
1.12483- (defclass monolithic-concatenate-source-op
1.12484- (basic-concatenate-source-op monolithic-bundle-op non-propagating-operation) ()
1.12485- (:documentation "Operation to concatenate all sources in a system and its dependencies
1.12486-into a single file"))
1.12487- (defclass monolithic-load-concatenated-source-op (basic-load-concatenated-source-op)
1.12488- ((selfward-operation :initform 'monolithic-concatenate-source-op :allocation :class))
1.12489- (:documentation "Operation to load the result of monolithic-concatenate-source-op as source"))
1.12490- (defclass monolithic-compile-concatenated-source-op (basic-compile-concatenated-source-op)
1.12491- ((selfward-operation :initform 'monolithic-concatenate-source-op :allocation :class))
1.12492- (:documentation "Operation to compile the result of monolithic-concatenate-source-op"))
1.12493- (defclass monolithic-load-compiled-concatenated-source-op
1.12494- (basic-load-compiled-concatenated-source-op)
1.12495- ((selfward-operation :initform 'monolithic-compile-concatenated-source-op :allocation :class))
1.12496- (:documentation "Operation to load the result of monolithic-compile-concatenated-source-op"))
1.12497-
1.12498- (defmethod input-files ((operation basic-concatenate-source-op) (s system))
1.12499- (loop :with encoding = (or (component-encoding s) *default-encoding*)
1.12500- :with other-encodings = '()
1.12501- :with around-compile = (around-compile-hook s)
1.12502- :with other-around-compile = '()
1.12503- :for c :in (required-components ;; see note about similar call to required-components
1.12504- s :goal-operation 'load-op ;; in bundle.lisp
1.12505- :keep-operation 'basic-compile-op
1.12506- :other-systems (operation-monolithic-p operation))
1.12507- :append
1.12508- (when (typep c 'cl-source-file)
1.12509- (let ((e (component-encoding c)))
1.12510- (unless (or (equal e encoding)
1.12511- (and (equal e :ASCII) (equal encoding :UTF-8)))
1.12512- (let ((a (assoc e other-encodings)))
1.12513- (if a (push (component-find-path c) (cdr a))
1.12514- (push (list e (component-find-path c)) other-encodings)))))
1.12515- (unless (equal around-compile (around-compile-hook c))
1.12516- (push (component-find-path c) other-around-compile))
1.12517- (input-files (make-operation 'compile-op) c)) :into inputs
1.12518- :finally
1.12519- (when other-encodings
1.12520- (warn "~S uses encoding ~A but has sources that use these encodings:~{ ~A~}"
1.12521- operation encoding
1.12522- (mapcar #'(lambda (x) (cons (car x) (list (reverse (cdr x)))))
1.12523- other-encodings)))
1.12524- (when other-around-compile
1.12525- (warn "~S uses around-compile hook ~A but has sources that use these hooks: ~A"
1.12526- operation around-compile other-around-compile))
1.12527- (return inputs)))
1.12528- (defmethod output-files ((o basic-compile-concatenated-source-op) (s system))
1.12529- (lisp-compilation-output-files o s))
1.12530-
1.12531- (defmethod perform ((o basic-concatenate-source-op) (s system))
1.12532- (let* ((ins (input-files o s))
1.12533- (out (output-file o s))
1.12534- (tmp (tmpize-pathname out)))
1.12535- (concatenate-files ins tmp)
1.12536- (rename-file-overwriting-target tmp out)))
1.12537- (defmethod perform ((o basic-load-concatenated-source-op) (s system))
1.12538- (perform-lisp-load-source o s))
1.12539- (defmethod perform ((o basic-compile-concatenated-source-op) (s system))
1.12540- (perform-lisp-compilation o s))
1.12541- (defmethod perform ((o basic-load-compiled-concatenated-source-op) (s system))
1.12542- (perform-lisp-load-fasl o s)))
1.12543-
1.12544-;;;; -------------------------------------------------------------------------
1.12545-;;;; Package systems in the style of quick-build or faslpath
1.12546-
1.12547-(uiop:define-package :asdf/package-inferred-system
1.12548- (:recycle :asdf/package-inferred-system :asdf/package-system :asdf)
1.12549- (:use :uiop/common-lisp :uiop
1.12550- :asdf/upgrade :asdf/session
1.12551- :asdf/component :asdf/system :asdf/system-registry :asdf/lisp-action
1.12552- :asdf/parse-defsystem)
1.12553- (:export
1.12554- #:package-inferred-system #:sysdef-package-inferred-system-search
1.12555- #:package-system ;; backward compatibility only. To be removed.
1.12556- #:register-system-packages
1.12557- #:*defpackage-forms* #:*package-inferred-systems* #:package-inferred-system-missing-package-error))
1.12558-(in-package :asdf/package-inferred-system)
1.12559-
1.12560-(with-upgradability ()
1.12561- ;; The names of the recognized defpackage forms.
1.12562- (defparameter *defpackage-forms* '(defpackage define-package))
1.12563-
1.12564- (defun initial-package-inferred-systems-table ()
1.12565- ;; Mark all existing packages are preloaded.
1.12566- (let ((h (make-hash-table :test 'equal)))
1.12567- (dolist (p (list-all-packages))
1.12568- (dolist (n (package-names p))
1.12569- (setf (gethash n h) t)))
1.12570- h))
1.12571-
1.12572- ;; Mapping from package names to systems that provide them.
1.12573- (defvar *package-inferred-systems* (initial-package-inferred-systems-table))
1.12574-
1.12575- (defclass package-inferred-system (system)
1.12576- ()
1.12577- (:documentation "Class for primary systems for which secondary systems are automatically
1.12578-in the one-file, one-file, one-system style: system names are mapped to files under the primary
1.12579-system's system-source-directory, dependencies are inferred from the first defpackage form in
1.12580-every such file"))
1.12581-
1.12582- ;; DEPRECATED. For backward compatibility only. To be removed in an upcoming release:
1.12583- (defclass package-system (package-inferred-system) ())
1.12584-
1.12585- ;; Is a given form recognizable as a defpackage form?
1.12586- (defun defpackage-form-p (form)
1.12587- (and (consp form)
1.12588- (member (car form) *defpackage-forms*)))
1.12589-
1.12590- ;; Find the first defpackage form in a stream, if any
1.12591- (defun stream-defpackage-form (stream)
1.12592- (loop :for form = (read stream nil nil) :while form
1.12593- :when (defpackage-form-p form) :return form))
1.12594-
1.12595- (defun file-defpackage-form (file)
1.12596- "Return the first DEFPACKAGE form in FILE."
1.12597- (with-input-file (f file)
1.12598- (stream-defpackage-form f)))
1.12599-
1.12600- (define-condition package-inferred-system-missing-package-error (system-definition-error)
1.12601- ((system :initarg :system :reader error-system)
1.12602- (pathname :initarg :pathname :reader error-pathname))
1.12603- (:report (lambda (c s)
1.12604- (format s (compatfmt "~@<No package form found while ~
1.12605- trying to define package-inferred-system ~A from file ~A~>")
1.12606- (error-system c) (error-pathname c)))))
1.12607-
1.12608- (defun package-dependencies (defpackage-form)
1.12609- "Return a list of packages depended on by the package
1.12610-defined in DEFPACKAGE-FORM. A package is depended upon if
1.12611-the DEFPACKAGE-FORM uses it or imports a symbol from it."
1.12612- (assert (defpackage-form-p defpackage-form))
1.12613- (remove-duplicates
1.12614- (while-collecting (dep)
1.12615- (loop :for (option . arguments) :in (cddr defpackage-form) :do
1.12616- (ecase option
1.12617- ((:use :mix :reexport :use-reexport :mix-reexport)
1.12618- (dolist (p arguments) (dep (string p))))
1.12619- ((:import-from :shadowing-import-from)
1.12620- (dep (string (first arguments))))
1.12621- #+package-local-nicknames
1.12622- ((:local-nicknames)
1.12623- (loop :for (nil actual-package-name) :in arguments :do
1.12624- (dep (string actual-package-name))))
1.12625- ((:nicknames :documentation :shadow :export :intern :unintern :recycle)))))
1.12626- :from-end t :test 'equal))
1.12627-
1.12628- (defun package-designator-name (package)
1.12629- "Normalize a package designator to a string"
1.12630- (etypecase package
1.12631- (package (package-name package))
1.12632- (string package)
1.12633- (symbol (string package))))
1.12634-
1.12635- (defun register-system-packages (system packages)
1.12636- "Register SYSTEM as providing PACKAGES."
1.12637- (let ((name (or (eq system t) (coerce-name system))))
1.12638- (dolist (p (ensure-list packages))
1.12639- (setf (gethash (package-designator-name p) *package-inferred-systems*) name))))
1.12640-
1.12641- (defun package-name-system (package-name)
1.12642- "Return the name of the SYSTEM providing PACKAGE-NAME, if such exists,
1.12643-otherwise return a default system name computed from PACKAGE-NAME."
1.12644- (check-type package-name string)
1.12645- (or (gethash package-name *package-inferred-systems*)
1.12646- (string-downcase package-name)))
1.12647-
1.12648- ;; Given a file in package-inferred-system style, find its dependencies
1.12649- (defun package-inferred-system-file-dependencies (file &optional system)
1.12650- (if-let (defpackage-form (file-defpackage-form file))
1.12651- (remove t (mapcar 'package-name-system (package-dependencies defpackage-form)))
1.12652- (error 'package-inferred-system-missing-package-error :system system :pathname file)))
1.12653-
1.12654- ;; Given package-inferred-system object, check whether its specification matches
1.12655- ;; the provided parameters
1.12656- (defun same-package-inferred-system-p (system name directory subpath around-compile dependencies)
1.12657- (and (eq (type-of system) 'package-inferred-system)
1.12658- (equal (component-name system) name)
1.12659- (pathname-equal directory (component-pathname system))
1.12660- (equal dependencies (component-sideway-dependencies system))
1.12661- (equal around-compile (around-compile-hook system))
1.12662- (let ((children (component-children system)))
1.12663- (and (length=n-p children 1)
1.12664- (let ((child (first children)))
1.12665- (and (eq (type-of child) 'cl-source-file)
1.12666- (equal (component-name child) "lisp")
1.12667- (and (slot-boundp child 'relative-pathname)
1.12668- (equal (slot-value child 'relative-pathname) subpath))))))))
1.12669-
1.12670- ;; sysdef search function to push into *system-definition-search-functions*
1.12671- (defun sysdef-package-inferred-system-search (system-name)
1.12672- "Takes SYSTEM-NAME and returns an initialized SYSTEM object, or NIL. Made to be added to
1.12673-*SYSTEM-DEFINITION-SEARCH-FUNCTIONS*."
1.12674- (let ((primary (primary-system-name system-name)))
1.12675- ;; this function ONLY does something if the primary system name is NOT the same as
1.12676- ;; SYSTEM-NAME. It is used to find the systems with names that are relative to
1.12677- ;; the primary system's name, and that are not explicitly specified in the system
1.12678- ;; definition
1.12679- (unless (equal primary system-name)
1.12680- (let ((top (find-system primary nil)))
1.12681- (when (typep top 'package-inferred-system)
1.12682- (if-let (dir (component-pathname top))
1.12683- (let* ((sub (subseq system-name (1+ (length primary))))
1.12684- (component-type (class-for-type top :file))
1.12685- (file-type (file-type (make-instance component-type)))
1.12686- (f (probe-file* (subpathname dir sub :type file-type)
1.12687- :truename *resolve-symlinks*)))
1.12688- (when (file-pathname-p f)
1.12689- (let ((dependencies (package-inferred-system-file-dependencies f system-name))
1.12690- (previous (registered-system system-name))
1.12691- (around-compile (around-compile-hook top)))
1.12692- (if (same-package-inferred-system-p previous system-name dir sub around-compile dependencies)
1.12693- previous
1.12694- (eval `(defsystem ,system-name
1.12695- :class package-inferred-system
1.12696- :default-component-class ,component-type
1.12697- :source-file ,(system-source-file top)
1.12698- :pathname ,dir
1.12699- :depends-on ,dependencies
1.12700- :around-compile ,around-compile
1.12701- :components ((,component-type file-type :pathname ,sub)))))))))))))))
1.12702-
1.12703-(with-upgradability ()
1.12704- (pushnew 'sysdef-package-inferred-system-search *system-definition-search-functions*)
1.12705- (setf *system-definition-search-functions*
1.12706- (remove (find-symbol* :sysdef-package-system-search :asdf/package-system nil)
1.12707- *system-definition-search-functions*)))
1.12708-;;;; ---------------------------------------------------------------------------
1.12709-;;;; asdf-output-translations
1.12710-
1.12711-(uiop/package:define-package :asdf/output-translations
1.12712- (:recycle :asdf/output-translations :asdf)
1.12713- (:use :uiop/common-lisp :uiop :asdf/upgrade)
1.12714- (:export
1.12715- #:*output-translations* #:*output-translations-parameter*
1.12716- #:invalid-output-translation
1.12717- #:output-translations #:output-translations-initialized-p
1.12718- #:initialize-output-translations #:clear-output-translations
1.12719- #:disable-output-translations #:ensure-output-translations
1.12720- #:apply-output-translations
1.12721- #:validate-output-translations-directive #:validate-output-translations-form
1.12722- #:validate-output-translations-file #:validate-output-translations-directory
1.12723- #:parse-output-translations-string #:wrapping-output-translations
1.12724- #:user-output-translations-pathname #:system-output-translations-pathname
1.12725- #:user-output-translations-directory-pathname #:system-output-translations-directory-pathname
1.12726- #:environment-output-translations #:process-output-translations
1.12727- #:compute-output-translations
1.12728- #+abcl #:translate-jar-pathname
1.12729- ))
1.12730-(in-package :asdf/output-translations)
1.12731-
1.12732-;; (setf output-translations) between 2.27 and 3.0.3 was using a defsetf macro
1.12733-;; for the sake of obsolete versions of GCL 2.6. Make sure it doesn't come to haunt us.
1.12734-(when-upgrading (:version "3.1.2") (fmakunbound '(setf output-translations)))
1.12735-
1.12736-(with-upgradability ()
1.12737- (define-condition invalid-output-translation (invalid-configuration warning)
1.12738- ((format :initform (compatfmt "~@<Invalid asdf output-translation ~S~@[ in ~S~]~@{ ~@?~}~@:>"))))
1.12739-
1.12740- (defvar *output-translations* ()
1.12741- "Either NIL (for uninitialized), or a list of one element,
1.12742-said element itself being a sorted list of mappings.
1.12743-Each mapping is a pair of a source pathname and destination pathname,
1.12744-and the order is by decreasing length of namestring of the source pathname.")
1.12745-
1.12746- (defun output-translations ()
1.12747- "Return the configured output-translations, if any"
1.12748- (car *output-translations*))
1.12749-
1.12750- ;; Set the output-translations, by sorting the provided new-value.
1.12751- (defun set-output-translations (new-value)
1.12752- (setf *output-translations*
1.12753- (list
1.12754- (stable-sort (copy-list new-value) #'>
1.12755- :key #'(lambda (x)
1.12756- (etypecase (car x)
1.12757- ((eql t) -1)
1.12758- (pathname
1.12759- (let ((directory
1.12760- (normalize-pathname-directory-component
1.12761- (pathname-directory (car x)))))
1.12762- (if (listp directory) (length directory) 0))))))))
1.12763- new-value)
1.12764- (defun (setf output-translations) (new-value) (set-output-translations new-value))
1.12765-
1.12766- (defun output-translations-initialized-p ()
1.12767- "Have the output-translations been initialized yet?"
1.12768- (and *output-translations* t))
1.12769-
1.12770- (defun clear-output-translations ()
1.12771- "Undoes any initialization of the output translations."
1.12772- (setf *output-translations* '())
1.12773- (values))
1.12774- (register-clear-configuration-hook 'clear-output-translations)
1.12775-
1.12776-
1.12777- ;;; Validation of the configuration directives...
1.12778-
1.12779- (defun validate-output-translations-directive (directive)
1.12780- (or (member directive '(:enable-user-cache :disable-cache nil))
1.12781- (and (consp directive)
1.12782- (or (and (length=n-p directive 2)
1.12783- (or (and (eq (first directive) :include)
1.12784- (typep (second directive) '(or string pathname null)))
1.12785- (and (location-designator-p (first directive))
1.12786- (or (location-designator-p (second directive))
1.12787- (location-function-p (second directive))))))
1.12788- (and (length=n-p directive 1)
1.12789- (location-designator-p (first directive)))))))
1.12790-
1.12791- (defun validate-output-translations-form (form &key location)
1.12792- (validate-configuration-form
1.12793- form
1.12794- :output-translations
1.12795- 'validate-output-translations-directive
1.12796- :location location :invalid-form-reporter 'invalid-output-translation))
1.12797-
1.12798- (defun validate-output-translations-file (file)
1.12799- (validate-configuration-file
1.12800- file 'validate-output-translations-form :description "output translations"))
1.12801-
1.12802- (defun validate-output-translations-directory (directory)
1.12803- (validate-configuration-directory
1.12804- directory :output-translations 'validate-output-translations-directive
1.12805- :invalid-form-reporter 'invalid-output-translation))
1.12806-
1.12807-
1.12808- ;;; Parse the ASDF_OUTPUT_TRANSLATIONS environment variable and/or some file contents
1.12809- (defun parse-output-translations-string (string &key location)
1.12810- (cond
1.12811- ((or (null string) (equal string ""))
1.12812- '(:output-translations :inherit-configuration))
1.12813- ((not (stringp string))
1.12814- (error (compatfmt "~@<Environment string isn't: ~3i~_~S~@:>") string))
1.12815- ((eql (char string 0) #\")
1.12816- (parse-output-translations-string (read-from-string string) :location location))
1.12817- ((eql (char string 0) #\()
1.12818- (validate-output-translations-form (read-from-string string) :location location))
1.12819- (t
1.12820- (loop
1.12821- :with inherit = nil
1.12822- :with directives = ()
1.12823- :with start = 0
1.12824- :with end = (length string)
1.12825- :with source = nil
1.12826- :with separator = (inter-directory-separator)
1.12827- :for i = (or (position separator string :start start) end) :do
1.12828- (let ((s (subseq string start i)))
1.12829- (cond
1.12830- (source
1.12831- (push (list source (if (equal "" s) nil s)) directives)
1.12832- (setf source nil))
1.12833- ((equal "" s)
1.12834- (when inherit
1.12835- (error (compatfmt "~@<Only one inherited configuration allowed: ~3i~_~S~@:>")
1.12836- string))
1.12837- (setf inherit t)
1.12838- (push :inherit-configuration directives))
1.12839- (t
1.12840- (setf source s)))
1.12841- (setf start (1+ i))
1.12842- (when (> start end)
1.12843- (when source
1.12844- (error (compatfmt "~@<Uneven number of components in source to destination mapping: ~3i~_~S~@:>")
1.12845- string))
1.12846- (unless inherit
1.12847- (push :ignore-inherited-configuration directives))
1.12848- (return `(:output-translations ,@(nreverse directives)))))))))
1.12849-
1.12850-
1.12851- ;; The default sources of configuration for output-translations
1.12852- (defparameter* *default-output-translations*
1.12853- '(environment-output-translations
1.12854- user-output-translations-pathname
1.12855- user-output-translations-directory-pathname
1.12856- system-output-translations-pathname
1.12857- system-output-translations-directory-pathname))
1.12858-
1.12859- ;; Compulsory implementation-dependent wrapping for the translations:
1.12860- ;; handle implementation-provided systems.
1.12861- (defun wrapping-output-translations ()
1.12862- `(:output-translations
1.12863- ;; Some implementations have precompiled ASDF systems,
1.12864- ;; so we must disable translations for implementation paths.
1.12865- #+(or clasp #|clozure|# ecl mkcl sbcl)
1.12866- ,@(let ((h (resolve-symlinks* (lisp-implementation-directory))))
1.12867- (when h `(((,h ,*wild-path*) ()))))
1.12868- #+mkcl (,(translate-logical-pathname "CONTRIB:") ())
1.12869- ;; All-import, here is where we want user stuff to be:
1.12870- :inherit-configuration
1.12871- ;; These are for convenience, and can be overridden by the user:
1.12872- #+abcl (#p"/___jar___file___root___/**/*.*" (:user-cache #p"**/*.*"))
1.12873- #+abcl (#p"jar:file:/**/*.jar!/**/*.*" (:function translate-jar-pathname))
1.12874- ;; We enable the user cache by default, and here is the place we do:
1.12875- :enable-user-cache))
1.12876-
1.12877- ;; Relative pathnames of output-translations configuration to XDG configuration directory
1.12878- (defparameter *output-translations-file* (parse-unix-namestring "common-lisp/asdf-output-translations.conf"))
1.12879- (defparameter *output-translations-directory* (parse-unix-namestring "common-lisp/asdf-output-translations.conf.d/"))
1.12880-
1.12881- ;; Locating various configuration pathnames, depending on input or output intent.
1.12882- (defun user-output-translations-pathname (&key (direction :input))
1.12883- (xdg-config-pathname *output-translations-file* direction))
1.12884- (defun system-output-translations-pathname (&key (direction :input))
1.12885- (find-preferred-file (system-config-pathnames *output-translations-file*)
1.12886- :direction direction))
1.12887- (defun user-output-translations-directory-pathname (&key (direction :input))
1.12888- (xdg-config-pathname *output-translations-directory* direction))
1.12889- (defun system-output-translations-directory-pathname (&key (direction :input))
1.12890- (find-preferred-file (system-config-pathnames *output-translations-directory*)
1.12891- :direction direction))
1.12892- (defun environment-output-translations ()
1.12893- (getenv "ASDF_OUTPUT_TRANSLATIONS"))
1.12894-
1.12895-
1.12896- ;;; Processing the configuration.
1.12897-
1.12898- (defgeneric process-output-translations (spec &key inherit collect))
1.12899-
1.12900- (defun inherit-output-translations (inherit &key collect)
1.12901- (when inherit
1.12902- (process-output-translations (first inherit) :collect collect :inherit (rest inherit))))
1.12903-
1.12904- (defun process-output-translations-directive (directive &key inherit collect)
1.12905- (if (atom directive)
1.12906- (ecase directive
1.12907- ((:enable-user-cache)
1.12908- (process-output-translations-directive '(t :user-cache) :collect collect))
1.12909- ((:disable-cache)
1.12910- (process-output-translations-directive '(t t) :collect collect))
1.12911- ((:inherit-configuration)
1.12912- (inherit-output-translations inherit :collect collect))
1.12913- ((:ignore-inherited-configuration :ignore-invalid-entries nil)
1.12914- nil))
1.12915- (let ((src (first directive))
1.12916- (dst (second directive)))
1.12917- (if (eq src :include)
1.12918- (when dst
1.12919- (process-output-translations (pathname dst) :inherit nil :collect collect))
1.12920- (when src
1.12921- (let ((trusrc (or (eql src t)
1.12922- (let ((loc (resolve-location src :ensure-directory t :wilden t)))
1.12923- (if (absolute-pathname-p loc) (resolve-symlinks* loc) loc)))))
1.12924- (cond
1.12925- ((location-function-p dst)
1.12926- (funcall collect
1.12927- (list trusrc (ensure-function (second dst)))))
1.12928- ((typep dst 'boolean)
1.12929- (funcall collect (list trusrc t)))
1.12930- (t
1.12931- (let* ((trudst (resolve-location dst :ensure-directory t :wilden t)))
1.12932- (funcall collect (list trudst t))
1.12933- (funcall collect (list trusrc trudst)))))))))))
1.12934-
1.12935- (defmethod process-output-translations ((x symbol) &key
1.12936- (inherit *default-output-translations*)
1.12937- collect)
1.12938- (process-output-translations (funcall x) :inherit inherit :collect collect))
1.12939- (defmethod process-output-translations ((pathname pathname) &key inherit collect)
1.12940- (cond
1.12941- ((directory-pathname-p pathname)
1.12942- (process-output-translations (validate-output-translations-directory pathname)
1.12943- :inherit inherit :collect collect))
1.12944- ((probe-file* pathname :truename *resolve-symlinks*)
1.12945- (process-output-translations (validate-output-translations-file pathname)
1.12946- :inherit inherit :collect collect))
1.12947- (t
1.12948- (inherit-output-translations inherit :collect collect))))
1.12949- (defmethod process-output-translations ((string string) &key inherit collect)
1.12950- (process-output-translations (parse-output-translations-string string)
1.12951- :inherit inherit :collect collect))
1.12952- (defmethod process-output-translations ((x null) &key inherit collect)
1.12953- (inherit-output-translations inherit :collect collect))
1.12954- (defmethod process-output-translations ((form cons) &key inherit collect)
1.12955- (dolist (directive (cdr (validate-output-translations-form form)))
1.12956- (process-output-translations-directive directive :inherit inherit :collect collect)))
1.12957-
1.12958-
1.12959- ;;; Top-level entry-points to configure output-translations
1.12960-
1.12961- (defun compute-output-translations (&optional parameter)
1.12962- "read the configuration, return it"
1.12963- (remove-duplicates
1.12964- (while-collecting (c)
1.12965- (inherit-output-translations
1.12966- `(wrapping-output-translations ,parameter ,@*default-output-translations*) :collect #'c))
1.12967- :test 'equal :from-end t))
1.12968-
1.12969- ;; Saving the user-provided parameter to output-translations, if any,
1.12970- ;; so we can recompute the translations after code upgrade.
1.12971- (defvar *output-translations-parameter* nil)
1.12972-
1.12973- ;; Main entry-point for users.
1.12974- (defun initialize-output-translations (&optional (parameter *output-translations-parameter*))
1.12975- "read the configuration, initialize the internal configuration variable,
1.12976-return the configuration"
1.12977- (setf *output-translations-parameter* parameter
1.12978- (output-translations) (compute-output-translations parameter)))
1.12979-
1.12980- (defun disable-output-translations ()
1.12981- "Initialize output translations in a way that maps every file to itself,
1.12982-effectively disabling the output translation facility."
1.12983- (initialize-output-translations
1.12984- '(:output-translations :disable-cache :ignore-inherited-configuration)))
1.12985-
1.12986- ;; checks an initial variable to see whether the state is initialized
1.12987- ;; or cleared. In the former case, return current configuration; in
1.12988- ;; the latter, initialize. ASDF will call this function at the start
1.12989- ;; of (asdf:find-system).
1.12990- (defun ensure-output-translations ()
1.12991- (if (output-translations-initialized-p)
1.12992- (output-translations)
1.12993- (initialize-output-translations)))
1.12994-
1.12995-
1.12996- ;; Top-level entry-point to _use_ output-translations
1.12997- (defun apply-output-translations (path)
1.12998- (etypecase path
1.12999- (logical-pathname
1.13000- path)
1.13001- ((or pathname string)
1.13002- (ensure-output-translations)
1.13003- (loop :with p = (resolve-symlinks* path)
1.13004- :for (source destination) :in (car *output-translations*)
1.13005- :for root = (when (or (eq source t)
1.13006- (and (pathnamep source)
1.13007- (not (absolute-pathname-p source))))
1.13008- (pathname-root p))
1.13009- :for absolute-source = (cond
1.13010- ((eq source t) (wilden root))
1.13011- (root (merge-pathnames* source root))
1.13012- (t source))
1.13013- :when (or (eq source t) (pathname-match-p p absolute-source))
1.13014- :return (translate-pathname* p absolute-source destination root source)
1.13015- :finally (return p)))))
1.13016-
1.13017-
1.13018- ;; Hook into uiop's output-translation mechanism
1.13019- #-cormanlisp
1.13020- (setf *output-translation-function* 'apply-output-translations)
1.13021-
1.13022-
1.13023- ;;; Implementation-dependent hacks
1.13024- #+abcl ;; ABCL: make it possible to use systems provided in the ABCL jar.
1.13025- (defun translate-jar-pathname (source wildcard)
1.13026- (declare (ignore wildcard))
1.13027- (flet ((normalize-device (pathname)
1.13028- (if (find :windows *features*)
1.13029- pathname
1.13030- (make-pathname :defaults pathname :device :unspecific))))
1.13031- (let* ((jar
1.13032- (pathname (first (pathname-device source))))
1.13033- (target-root-directory-namestring
1.13034- (format nil "/___jar___file___root___/~@[~A/~]"
1.13035- (and (find :windows *features*)
1.13036- (pathname-device jar))))
1.13037- (relative-source
1.13038- (relativize-pathname-directory source))
1.13039- (relative-jar
1.13040- (relativize-pathname-directory (ensure-directory-pathname jar)))
1.13041- (target-root-directory
1.13042- (normalize-device
1.13043- (pathname-directory-pathname
1.13044- (parse-namestring target-root-directory-namestring))))
1.13045- (target-root
1.13046- (merge-pathnames* relative-jar target-root-directory))
1.13047- (target
1.13048- (merge-pathnames* relative-source target-root)))
1.13049- (normalize-device (apply-output-translations target))))))
1.13050-
1.13051-;;;; -----------------------------------------------------------------
1.13052-;;;; Source Registry Configuration, by Francois-Rene Rideau
1.13053-;;;; See the Manual and https://bugs.launchpad.net/asdf/+bug/485918
1.13054-
1.13055-(uiop/package:define-package :asdf/source-registry
1.13056- ;; NB: asdf/find-system allows upgrade from <=3.2.1 that have initialize-source-registry there
1.13057- (:recycle :asdf/source-registry :asdf/find-system :asdf)
1.13058- (:use :uiop/common-lisp :uiop :asdf/upgrade :asdf/system :asdf/system-registry)
1.13059- (:export
1.13060- #:*source-registry-parameter* #:*default-source-registries*
1.13061- #:invalid-source-registry
1.13062- #:source-registry-initialized-p
1.13063- #:initialize-source-registry #:clear-source-registry #:*source-registry*
1.13064- #:ensure-source-registry #:*source-registry-parameter*
1.13065- #:*default-source-registry-exclusions* #:*source-registry-exclusions*
1.13066- #:*wild-asd* #:directory-asd-files #:register-asd-directory
1.13067- #:*recurse-beyond-asds* #:collect-asds-in-directory #:collect-sub*directories-asd-files
1.13068- #:validate-source-registry-directive #:validate-source-registry-form
1.13069- #:validate-source-registry-file #:validate-source-registry-directory
1.13070- #:parse-source-registry-string #:wrapping-source-registry
1.13071- #:default-user-source-registry #:default-system-source-registry
1.13072- #:user-source-registry #:system-source-registry
1.13073- #:user-source-registry-directory #:system-source-registry-directory
1.13074- #:environment-source-registry #:process-source-registry #:inherit-source-registry
1.13075- #:compute-source-registry #:flatten-source-registry
1.13076- #:sysdef-source-registry-search))
1.13077-(in-package :asdf/source-registry)
1.13078-
1.13079-(with-upgradability ()
1.13080- (define-condition invalid-source-registry (invalid-configuration warning)
1.13081- ((format :initform (compatfmt "~@<Invalid source registry ~S~@[ in ~S~]~@{ ~@?~}~@:>"))))
1.13082-
1.13083- ;; Default list of directories under which the source-registry tree search won't recurse
1.13084- (defvar *default-source-registry-exclusions*
1.13085- '(;;-- Using ack 1.2 exclusions
1.13086- ".bzr" ".cdv"
1.13087- ;; "~.dep" "~.dot" "~.nib" "~.plst" ; we don't support ack wildcards
1.13088- ".git" ".hg" ".pc" ".svn" "CVS" "RCS" "SCCS" "_darcs"
1.13089- "_sgbak" "autom4te.cache" "cover_db" "_build"
1.13090- ;;-- debian often builds stuff under the debian directory... BAD.
1.13091- "debian"))
1.13092-
1.13093- ;; Actual list of directories under which the source-registry tree search won't recurse
1.13094- (defvar *source-registry-exclusions* *default-source-registry-exclusions*)
1.13095-
1.13096- ;; The state of the source-registry after search in configured locations
1.13097- (defvar *source-registry* nil
1.13098- "Either NIL (for uninitialized), or an equal hash-table, mapping
1.13099-system names to pathnames of .asd files")
1.13100-
1.13101- ;; Saving the user-provided parameter to the source-registry, if any,
1.13102- ;; so we can recompute the source-registry after code upgrade.
1.13103- (defvar *source-registry-parameter* nil)
1.13104-
1.13105- (defun source-registry-initialized-p ()
1.13106- (typep *source-registry* 'hash-table))
1.13107-
1.13108- (defun clear-source-registry ()
1.13109- "Undoes any initialization of the source registry."
1.13110- (setf *source-registry* nil)
1.13111- (values))
1.13112- (register-clear-configuration-hook 'clear-source-registry)
1.13113-
1.13114- (defparameter *wild-asd*
1.13115- (make-pathname :directory nil :name *wild* :type "asd" :version :newest))
1.13116-
1.13117- (defun directory-asd-files (directory)
1.13118- (directory-files directory *wild-asd*))
1.13119-
1.13120- (defun collect-asds-in-directory (directory collect)
1.13121- (let ((asds (directory-asd-files directory)))
1.13122- (map () collect asds)
1.13123- asds))
1.13124-
1.13125- (defvar *recurse-beyond-asds* t
1.13126- "Should :tree entries of the source-registry recurse in subdirectories
1.13127-after having found a .asd file? True by default.")
1.13128-
1.13129- ;; When walking down a filesystem tree, if in a directory there is a .cl-source-registry.cache,
1.13130- ;; read its contents instead of further recursively querying the filesystem.
1.13131- (defun process-source-registry-cache (directory collect)
1.13132- (let ((cache (ignore-errors
1.13133- (safe-read-file-form (subpathname directory ".cl-source-registry.cache")))))
1.13134- (when (and (listp cache) (eq :source-registry-cache (first cache)))
1.13135- (loop :for s :in (rest cache) :do (funcall collect (subpathname directory s)))
1.13136- t)))
1.13137-
1.13138- (defun collect-sub*directories-asd-files
1.13139- (directory &key (exclude *default-source-registry-exclusions*) collect
1.13140- (recurse-beyond-asds *recurse-beyond-asds*) ignore-cache)
1.13141- (let ((visited (make-hash-table :test 'equalp)))
1.13142- (flet ((collectp (dir)
1.13143- (unless (and (not ignore-cache) (process-source-registry-cache dir collect))
1.13144- (let ((asds (collect-asds-in-directory dir collect)))
1.13145- (or recurse-beyond-asds (not asds)))))
1.13146- (recursep (x) ; x will be a directory pathname
1.13147- (and
1.13148- (not (member (car (last (pathname-directory x))) exclude :test #'equal))
1.13149- (flet ((pathname-key (x)
1.13150- (namestring (truename* x))))
1.13151- (let ((visitedp (gethash (pathname-key x) visited)))
1.13152- (if visitedp nil
1.13153- (setf (gethash (pathname-key x) visited) t)))))))
1.13154- (collect-sub*directories directory #'collectp #'recursep (constantly nil)))))
1.13155-
1.13156-
1.13157- ;;; Validate the configuration forms
1.13158-
1.13159- (defun validate-source-registry-directive (directive)
1.13160- (or (member directive '(:default-registry))
1.13161- (and (consp directive)
1.13162- (let ((rest (rest directive)))
1.13163- (case (first directive)
1.13164- ((:include :directory :tree)
1.13165- (and (length=n-p rest 1)
1.13166- (location-designator-p (first rest))))
1.13167- ((:exclude :also-exclude)
1.13168- (every #'stringp rest))
1.13169- ((:default-registry)
1.13170- (null rest)))))))
1.13171-
1.13172- (defun validate-source-registry-form (form &key location)
1.13173- (validate-configuration-form
1.13174- form :source-registry 'validate-source-registry-directive
1.13175- :location location :invalid-form-reporter 'invalid-source-registry))
1.13176-
1.13177- (defun validate-source-registry-file (file)
1.13178- (validate-configuration-file
1.13179- file 'validate-source-registry-form :description "a source registry"))
1.13180-
1.13181- (defun validate-source-registry-directory (directory)
1.13182- (validate-configuration-directory
1.13183- directory :source-registry 'validate-source-registry-directive
1.13184- :invalid-form-reporter 'invalid-source-registry))
1.13185-
1.13186-
1.13187- ;;; Parse the configuration string
1.13188-
1.13189- (defun parse-source-registry-string (string &key location)
1.13190- (cond
1.13191- ((or (null string) (equal string ""))
1.13192- '(:source-registry :inherit-configuration))
1.13193- ((not (stringp string))
1.13194- (error (compatfmt "~@<Environment string isn't: ~3i~_~S~@:>") string))
1.13195- ((find (char string 0) "\"(")
1.13196- (validate-source-registry-form (read-from-string string) :location location))
1.13197- (t
1.13198- (loop
1.13199- :with inherit = nil
1.13200- :with directives = ()
1.13201- :with start = 0
1.13202- :with end = (length string)
1.13203- :with separator = (inter-directory-separator)
1.13204- :for pos = (position separator string :start start) :do
1.13205- (let ((s (subseq string start (or pos end))))
1.13206- (flet ((check (dir)
1.13207- (unless (absolute-pathname-p dir)
1.13208- (error (compatfmt "~@<source-registry string must specify absolute pathnames: ~3i~_~S~@:>") string))
1.13209- dir))
1.13210- (cond
1.13211- ((equal "" s) ; empty element: inherit
1.13212- (when inherit
1.13213- (error (compatfmt "~@<Only one inherited configuration allowed: ~3i~_~S~@:>")
1.13214- string))
1.13215- (setf inherit t)
1.13216- (push ':inherit-configuration directives))
1.13217- ((string-suffix-p s "//") ;; TODO: allow for doubling of separator even outside Unix?
1.13218- (push `(:tree ,(check (subseq s 0 (- (length s) 2)))) directives))
1.13219- (t
1.13220- (push `(:directory ,(check s)) directives))))
1.13221- (cond
1.13222- (pos
1.13223- (setf start (1+ pos)))
1.13224- (t
1.13225- (unless inherit
1.13226- (push '(:ignore-inherited-configuration) directives))
1.13227- (return `(:source-registry ,@(nreverse directives))))))))))
1.13228-
1.13229- (defun register-asd-directory (directory &key recurse exclude collect)
1.13230- (if (not recurse)
1.13231- (collect-asds-in-directory directory collect)
1.13232- (collect-sub*directories-asd-files
1.13233- directory :exclude exclude :collect collect)))
1.13234-
1.13235- (defparameter* *default-source-registries*
1.13236- '(environment-source-registry
1.13237- user-source-registry
1.13238- user-source-registry-directory
1.13239- default-user-source-registry
1.13240- system-source-registry
1.13241- system-source-registry-directory
1.13242- default-system-source-registry)
1.13243- "List of default source registries" "3.1.0.102")
1.13244-
1.13245- (defparameter *source-registry-file* (parse-unix-namestring "common-lisp/source-registry.conf"))
1.13246- (defparameter *source-registry-directory* (parse-unix-namestring "common-lisp/source-registry.conf.d/"))
1.13247-
1.13248- (defun wrapping-source-registry ()
1.13249- `(:source-registry
1.13250- #+(or clasp ecl sbcl) (:tree ,(resolve-symlinks* (lisp-implementation-directory)))
1.13251- :inherit-configuration
1.13252- #+mkcl (:tree ,(translate-logical-pathname "SYS:"))
1.13253- #+cmucl (:tree #p"modules:")
1.13254- #+scl (:tree #p"file://modules/")))
1.13255- (defun default-user-source-registry ()
1.13256- `(:source-registry
1.13257- (:tree (:home "common-lisp/"))
1.13258- #+sbcl (:directory (:home ".sbcl/systems/"))
1.13259- (:directory ,(xdg-data-home "common-lisp/systems/"))
1.13260- (:tree ,(xdg-data-home "common-lisp/source/"))
1.13261- :inherit-configuration))
1.13262- (defun default-system-source-registry ()
1.13263- `(:source-registry
1.13264- ,@(loop :for dir :in (xdg-data-dirs "common-lisp/")
1.13265- :collect `(:directory (,dir "systems/"))
1.13266- :collect `(:tree (,dir "source/")))
1.13267- :inherit-configuration))
1.13268- (defun user-source-registry (&key (direction :input))
1.13269- (xdg-config-pathname *source-registry-file* direction))
1.13270- (defun system-source-registry (&key (direction :input))
1.13271- (find-preferred-file (system-config-pathnames *source-registry-file*)
1.13272- :direction direction))
1.13273- (defun user-source-registry-directory (&key (direction :input))
1.13274- (xdg-config-pathname *source-registry-directory* direction))
1.13275- (defun system-source-registry-directory (&key (direction :input))
1.13276- (find-preferred-file (system-config-pathnames *source-registry-directory*)
1.13277- :direction direction))
1.13278- (defun environment-source-registry ()
1.13279- (getenv "CL_SOURCE_REGISTRY"))
1.13280-
1.13281-
1.13282- ;;; Process the source-registry configuration
1.13283-
1.13284- (defgeneric process-source-registry (spec &key inherit register))
1.13285-
1.13286- (defun inherit-source-registry (inherit &key register)
1.13287- (when inherit
1.13288- (process-source-registry (first inherit) :register register :inherit (rest inherit))))
1.13289-
1.13290- (defun process-source-registry-directive (directive &key inherit register)
1.13291- (destructuring-bind (kw &rest rest) (if (consp directive) directive (list directive))
1.13292- (ecase kw
1.13293- ((:include)
1.13294- (destructuring-bind (pathname) rest
1.13295- (process-source-registry (resolve-location pathname) :inherit nil :register register)))
1.13296- ((:directory)
1.13297- (destructuring-bind (pathname) rest
1.13298- (when pathname
1.13299- (funcall register (resolve-location pathname :ensure-directory t)))))
1.13300- ((:tree)
1.13301- (destructuring-bind (pathname) rest
1.13302- (when pathname
1.13303- (funcall register (resolve-location pathname :ensure-directory t)
1.13304- :recurse t :exclude *source-registry-exclusions*))))
1.13305- ((:exclude)
1.13306- (setf *source-registry-exclusions* rest))
1.13307- ((:also-exclude)
1.13308- (appendf *source-registry-exclusions* rest))
1.13309- ((:default-registry)
1.13310- (inherit-source-registry
1.13311- '(default-user-source-registry default-system-source-registry) :register register))
1.13312- ((:inherit-configuration)
1.13313- (inherit-source-registry inherit :register register))
1.13314- ((:ignore-inherited-configuration)
1.13315- nil)))
1.13316- nil)
1.13317-
1.13318- (defmethod process-source-registry ((x symbol) &key inherit register)
1.13319- (process-source-registry (funcall x) :inherit inherit :register register))
1.13320- (defmethod process-source-registry ((pathname pathname) &key inherit register)
1.13321- (cond
1.13322- ((directory-pathname-p pathname)
1.13323- (let ((*here-directory* (resolve-symlinks* pathname)))
1.13324- (process-source-registry (validate-source-registry-directory pathname)
1.13325- :inherit inherit :register register)))
1.13326- ((probe-file* pathname :truename *resolve-symlinks*)
1.13327- (let ((*here-directory* (pathname-directory-pathname pathname)))
1.13328- (process-source-registry (validate-source-registry-file pathname)
1.13329- :inherit inherit :register register)))
1.13330- (t
1.13331- (inherit-source-registry inherit :register register))))
1.13332- (defmethod process-source-registry ((string string) &key inherit register)
1.13333- (process-source-registry (parse-source-registry-string string)
1.13334- :inherit inherit :register register))
1.13335- (defmethod process-source-registry ((x null) &key inherit register)
1.13336- (inherit-source-registry inherit :register register))
1.13337- (defmethod process-source-registry ((form cons) &key inherit register)
1.13338- (let ((*source-registry-exclusions* *default-source-registry-exclusions*))
1.13339- (dolist (directive (cdr (validate-source-registry-form form)))
1.13340- (process-source-registry-directive directive :inherit inherit :register register))))
1.13341-
1.13342-
1.13343- ;; Flatten the user-provided configuration into an ordered list of directories and trees
1.13344- (defun flatten-source-registry (&optional (parameter *source-registry-parameter*))
1.13345- (remove-duplicates
1.13346- (while-collecting (collect)
1.13347- (with-pathname-defaults () ;; be location-independent
1.13348- (inherit-source-registry
1.13349- `(wrapping-source-registry
1.13350- ,parameter
1.13351- ,@*default-source-registries*)
1.13352- :register #'(lambda (directory &key recurse exclude)
1.13353- (collect (list directory :recurse recurse :exclude exclude))))))
1.13354- :test 'equal :from-end t))
1.13355-
1.13356- ;; MAYBE: move this utility function to uiop/pathname and export it?
1.13357- (defun pathname-directory-depth (p)
1.13358- (length (normalize-pathname-directory-component (pathname-directory p))))
1.13359-
1.13360- (defun preferred-source-path-p (x y)
1.13361- "Return T iff X is to be preferred over Y as a source path"
1.13362- (let ((lx (pathname-directory-depth x))
1.13363- (ly (pathname-directory-depth y)))
1.13364- (or (< lx ly)
1.13365- (and (= lx ly)
1.13366- (string< (namestring x)
1.13367- (namestring y))))))
1.13368-
1.13369- ;; Will read the configuration and initialize all internal variables.
1.13370- (defun compute-source-registry (&optional (parameter *source-registry-parameter*)
1.13371- (registry *source-registry*))
1.13372- (dolist (entry (flatten-source-registry parameter))
1.13373- (destructuring-bind (directory &key recurse exclude) entry
1.13374- (let* ((h (make-hash-table :test 'equal))) ; table to detect duplicates
1.13375- (register-asd-directory
1.13376- directory :recurse recurse :exclude exclude :collect
1.13377- #'(lambda (asd)
1.13378- (let* ((name (pathname-name asd))
1.13379- (name (if (typep asd 'logical-pathname)
1.13380- ;; logical pathnames are upper-case,
1.13381- ;; at least in the CLHS and on SBCL,
1.13382- ;; yet (coerce-name :foo) is lower-case.
1.13383- ;; won't work well with (load-system "Foo")
1.13384- ;; instead of (load-system 'foo)
1.13385- (string-downcase name)
1.13386- name)))
1.13387- (unless (gethash name registry) ; already shadowed by something else
1.13388- (if-let (old (gethash name h))
1.13389- ;; If the name appears multiple times,
1.13390- ;; prefer the one with the shallowest directory,
1.13391- ;; or if they have same depth, compare unix-namestring with string<
1.13392- (multiple-value-bind (better worse)
1.13393- (if (preferred-source-path-p asd old)
1.13394- (progn (setf (gethash name h) asd) (values asd old))
1.13395- (values old asd))
1.13396- (when *verbose-out*
1.13397- (warn (compatfmt "~@<In source-registry entry ~A~@[/~*~] ~
1.13398- found several entries for ~A - picking ~S over ~S~:>")
1.13399- directory recurse name better worse)))
1.13400- (setf (gethash name h) asd))))))
1.13401- (maphash #'(lambda (k v) (setf (gethash k registry) v)) h))))
1.13402- (values))
1.13403-
1.13404- (defun initialize-source-registry (&optional (parameter *source-registry-parameter*))
1.13405- ;; Record the parameter used to configure the registry
1.13406- (setf *source-registry-parameter* parameter)
1.13407- ;; Clear the previous registry database:
1.13408- (setf *source-registry* (make-hash-table :test 'equal))
1.13409- ;; Do it!
1.13410- (compute-source-registry parameter))
1.13411-
1.13412- ;; Checks an initial variable to see whether the state is initialized
1.13413- ;; or cleared. In the former case, return current configuration; in
1.13414- ;; the latter, initialize. ASDF will call this function at the start
1.13415- ;; of (asdf:find-system) to make sure the source registry is initialized.
1.13416- ;; However, it will do so *without* a parameter, at which point it
1.13417- ;; will be too late to provide a parameter to this function, though
1.13418- ;; you may override the configuration explicitly by calling
1.13419- ;; initialize-source-registry directly with your parameter.
1.13420- (defun ensure-source-registry (&optional parameter)
1.13421- (unless (source-registry-initialized-p)
1.13422- (initialize-source-registry parameter))
1.13423- (values))
1.13424-
1.13425- (defun sysdef-source-registry-search (system)
1.13426- (ensure-source-registry)
1.13427- (values (gethash (primary-system-name system) *source-registry*))))
1.13428-
1.13429-
1.13430-;;;; -------------------------------------------------------------------------
1.13431-;;; Internal hacks for backward-compatibility
1.13432-
1.13433-(uiop/package:define-package :asdf/backward-internals
1.13434- (:recycle :asdf/backward-internals :asdf)
1.13435- (:use :uiop/common-lisp :uiop :asdf/upgrade :asdf/find-system)
1.13436- (:export #:load-sysdef))
1.13437-(in-package :asdf/backward-internals)
1.13438-
1.13439-(with-asdf-deprecation (:style-warning "3.2" :warning "3.4")
1.13440- (defun load-sysdef (name pathname)
1.13441- (declare (ignore name pathname))
1.13442- ;; Needed for backward compatibility with swank-asdf from SLIME 2015-12-01 or older.
1.13443- (error "Use asdf:load-asd instead of asdf::load-sysdef")))
1.13444-;;;; -------------------------------------------------------------------------
1.13445-;;; Backward-compatible interfaces
1.13446-
1.13447-(uiop/package:define-package :asdf/backward-interface
1.13448- (:recycle :asdf/backward-interface :asdf)
1.13449- (:use :uiop/common-lisp :uiop :asdf/upgrade :asdf/session
1.13450- :asdf/component :asdf/system :asdf/system-registry :asdf/operation :asdf/action
1.13451- :asdf/lisp-action :asdf/plan :asdf/operate
1.13452- :asdf/find-system :asdf/parse-defsystem :asdf/output-translations :asdf/bundle)
1.13453- (:export
1.13454- #:*asdf-verbose*
1.13455- #:operation-error #:compile-error #:compile-failed #:compile-warned
1.13456- #:error-component #:error-operation #:traverse
1.13457- #:component-load-dependencies
1.13458- #:enable-asdf-binary-locations-compatibility
1.13459- #:operation-on-failure #:operation-on-warnings #:on-failure #:on-warnings
1.13460- #:component-property
1.13461- #:run-shell-command
1.13462- #:system-definition-pathname #:system-registered-p #:require-system
1.13463- #:explain
1.13464- #+ecl #:make-build))
1.13465-(in-package :asdf/backward-interface)
1.13466-
1.13467-;; NB: the warning status of these functions may have to be distinguished later,
1.13468-;; as some get removed faster than the others in client code.
1.13469-(with-asdf-deprecation (:style-warning "3.2" :warning "3.4")
1.13470-
1.13471- ;; These conditions from ASDF 1 and 2 are used by many packages in Quicklisp;
1.13472- ;; but ASDF3 replaced them with somewhat different variants of uiop:compile-condition
1.13473- ;; that do not involve ASDF actions.
1.13474- ;; TODO: find the offenders and stop them.
1.13475- (progn
1.13476- (define-condition operation-error (error) ;; Bad, backward-compatible name
1.13477- ;; Used by SBCL, cffi-tests, clsql-mysql, clsql-uffi, qt, elephant, uffi-tests, sb-grovel
1.13478- ((component :reader error-component :initarg :component)
1.13479- (operation :reader error-operation :initarg :operation))
1.13480- (:report (lambda (c s)
1.13481- (format s (compatfmt "~@<~A while invoking ~A on ~A~@:>")
1.13482- (type-of c) (error-operation c) (error-component c)))))
1.13483- (define-condition compile-error (operation-error) ())
1.13484- (define-condition compile-failed (compile-error) ())
1.13485- (define-condition compile-warned (compile-error) ()))
1.13486-
1.13487- ;; In Quicklisp 2015-05, still used by lisp-executable, staple, repl-utilities, cffi
1.13488- (defun component-load-dependencies (component) ;; from ASDF 2.000 to 2.26
1.13489- "DEPRECATED. Please use COMPONENT-SIDEWAY-DEPENDENCIES instead; or better,
1.13490-define your operations with proper use of SIDEWAY-OPERATION, SELFWARD-OPERATION,
1.13491-or define methods on PREPARE-OP, etc."
1.13492- ;; Old deprecated name for the same thing. Please update your software.
1.13493- (component-sideway-dependencies component))
1.13494-
1.13495- ;; These old interfaces from ASDF1 have never been very meaningful
1.13496- ;; but are still used in obscure places.
1.13497- ;; In Quicklisp 2015-05, still used by cl-protobufs and clx.
1.13498- (defgeneric operation-on-warnings (operation)
1.13499- (:documentation "DEPRECATED. Please use UIOP:*COMPILE-FILE-WARNINGS-BEHAVIOUR* instead."))
1.13500- (defgeneric operation-on-failure (operation)
1.13501- (:documentation "DEPRECATED. Please use UIOP:*COMPILE-FILE-FAILURE-BEHAVIOUR* instead."))
1.13502- (defgeneric (setf operation-on-warnings) (x operation)
1.13503- (:documentation "DEPRECATED. Please SETF UIOP:*COMPILE-FILE-WARNINGS-BEHAVIOUR* instead."))
1.13504- (defgeneric (setf operation-on-failure) (x operation)
1.13505- (:documentation "DEPRECATED. Please SETF UIOP:*COMPILE-FILE-FAILURE-BEHAVIOUR* instead."))
1.13506- (progn
1.13507- (defmethod operation-on-warnings ((o operation))
1.13508- *compile-file-warnings-behaviour*)
1.13509- (defmethod operation-on-failure ((o operation))
1.13510- *compile-file-failure-behaviour*)
1.13511- (defmethod (setf operation-on-warnings) (x (o operation))
1.13512- (setf *compile-file-warnings-behaviour* x))
1.13513- (defmethod (setf operation-on-failure) (x (o operation))
1.13514- (setf *compile-file-failure-behaviour* x)))
1.13515-
1.13516- ;; Quicklisp 2015-05: Still used by SLIME's swank-asdf (!), common-lisp-stat,
1.13517- ;; js-parser, osicat, babel, staple, weblocks, cl-png, plain-odbc, autoproject,
1.13518- ;; cl-blapack, com.informatimago, cells-gtk3, asdf-dependency-grovel,
1.13519- ;; cl-glfw, cffi, jwacs, montezuma
1.13520- (defun system-definition-pathname (x)
1.13521- ;; As of 2.014.8, we mean to make this function obsolete,
1.13522- ;; but that won't happen until all clients have been updated.
1.13523- "DEPRECATED. This function used to expose ASDF internals with subtle
1.13524-differences with respect to user expectations, that have been refactored
1.13525-away since. We recommend you use ASDF:SYSTEM-SOURCE-FILE instead for a
1.13526-mostly compatible replacement that we're supporting, or even
1.13527-ASDF:SYSTEM-SOURCE-DIRECTORY or ASDF:SYSTEM-RELATIVE-PATHNAME
1.13528-if that's whay you mean." ;;)
1.13529- (system-source-file x))
1.13530-
1.13531- ;; TRAVERSE is the function used to compute a plan in ASDF 1 and 2.
1.13532- ;; It was never officially exposed but some people still used it.
1.13533- (defgeneric traverse (operation component &key &allow-other-keys)
1.13534- (:documentation
1.13535- "DEPRECATED. Use MAKE-PLAN and PLAN-ACTIONS, or REQUIRED-COMPONENTS,
1.13536-or some other supported interface instead.
1.13537-
1.13538-Generate and return a plan for performing OPERATION on COMPONENT.
1.13539-
1.13540-The plan returned is a list of dotted-pairs. Each pair is the CONS
1.13541-of ASDF operation object and a COMPONENT object. The pairs will be
1.13542-processed in order by OPERATE."))
1.13543- (progn
1.13544- (define-convenience-action-methods traverse (operation component &key)))
1.13545- (defmethod traverse ((o operation) (c component) &rest keys &key plan-class &allow-other-keys)
1.13546- (plan-actions (apply 'make-plan plan-class o c keys)))
1.13547-
1.13548-
1.13549- ;; ASDF-Binary-Locations compatibility
1.13550- ;; This remains supported for legacy user, but not recommended for new users.
1.13551- ;; We suspect there are no more legacy users in 2016.
1.13552- (defun enable-asdf-binary-locations-compatibility
1.13553- (&key
1.13554- (centralize-lisp-binaries nil)
1.13555- (default-toplevel-directory
1.13556- ;; Use ".cache/common-lisp/" instead ???
1.13557- (subpathname (user-homedir-pathname) ".fasls/"))
1.13558- (include-per-user-information nil)
1.13559- (map-all-source-files (or #+(or clasp clisp ecl mkcl) t nil))
1.13560- (source-to-target-mappings nil)
1.13561- (file-types `(,(compile-file-type)
1.13562- "build-report"
1.13563- #+clasp (compile-file-type :output-type :object)
1.13564- #+ecl (compile-file-type :type :object)
1.13565- #+mkcl (compile-file-type :fasl-p nil)
1.13566- #+clisp "lib" #+sbcl "cfasl"
1.13567- #+sbcl "sbcl-warnings" #+clozure "ccl-warnings")))
1.13568- "DEPRECATED. Use asdf-output-translations instead."
1.13569- #+(or clasp clisp ecl mkcl)
1.13570- (when (null map-all-source-files)
1.13571- (error "asdf:enable-asdf-binary-locations-compatibility doesn't support :map-all-source-files nil on CLISP, ECL and MKCL"))
1.13572- (let* ((patterns (if map-all-source-files (list *wild-file*)
1.13573- (loop :for type :in file-types
1.13574- :collect (make-pathname :type type :defaults *wild-file*))))
1.13575- (destination-directory
1.13576- (if centralize-lisp-binaries
1.13577- `(,default-toplevel-directory
1.13578- ,@(when include-per-user-information
1.13579- (cdr (pathname-directory (user-homedir-pathname))))
1.13580- :implementation ,*wild-inferiors*)
1.13581- `(:root ,*wild-inferiors* :implementation))))
1.13582- (initialize-output-translations
1.13583- `(:output-translations
1.13584- ,@source-to-target-mappings
1.13585- #+abcl (#p"jar:file:/**/*.jar!/**/*.*" (:function translate-jar-pathname))
1.13586- #+abcl (#p"/___jar___file___root___/**/*.*" (,@destination-directory))
1.13587- ,@(loop :for pattern :in patterns
1.13588- :collect `((:root ,*wild-inferiors* ,pattern)
1.13589- (,@destination-directory ,pattern)))
1.13590- (t t)
1.13591- :ignore-inherited-configuration))))
1.13592- (progn
1.13593- (defmethod operate :before (operation-class system &rest args &key &allow-other-keys)
1.13594- (declare (ignore operation-class system args))
1.13595- (when (find-symbol* '#:output-files-for-system-and-operation :asdf nil)
1.13596- (error "ASDF 2 is not compatible with ASDF-BINARY-LOCATIONS, which you are using.
1.13597-ASDF 2 now achieves the same purpose with its builtin ASDF-OUTPUT-TRANSLATIONS,
1.13598-which should be easier to configure. Please stop using ASDF-BINARY-LOCATIONS,
1.13599-and instead use ASDF-OUTPUT-TRANSLATIONS. See the ASDF manual for details.
1.13600-In case you insist on preserving your previous A-B-L configuration, but
1.13601-do not know how to achieve the same effect with A-O-T, you may use function
1.13602-ASDF:ENABLE-ASDF-BINARY-LOCATIONS-COMPATIBILITY as documented in the manual;
1.13603-call that function where you would otherwise have loaded and configured A-B-L."))))
1.13604-
1.13605-
1.13606- ;; run-shell-command from ASDF 2, lightly fixed from ASDF 1, copied from MK-DEFSYSTEM. Die!
1.13607- (defun run-shell-command (control-string &rest args)
1.13608- "PLEASE DO NOT USE. This function is not just DEPRECATED, but also dysfunctional.
1.13609-Please use UIOP:RUN-PROGRAM instead."
1.13610- #-(and ecl os-windows)
1.13611- (let ((command (apply 'format nil control-string args)))
1.13612- (asdf-message "; $ ~A~%" command)
1.13613- (let ((exit-code
1.13614- (ignore-errors
1.13615- (nth-value 2 (run-program command :force-shell t :ignore-error-status t
1.13616- :output *verbose-out*)))))
1.13617- (typecase exit-code
1.13618- ((integer 0 255) exit-code)
1.13619- (t 255))))
1.13620- #+(and ecl os-windows)
1.13621- (not-implemented-error "run-shell-command" "for ECL on Windows."))
1.13622-
1.13623- ;; HOW do we get rid of variables??? With a symbol-macro that issues a warning?
1.13624- ;; In Quicklisp 2015-05, cl-protobufs still uses it, but that should be fixed in next version.
1.13625- (progn
1.13626- (defvar *asdf-verbose* nil)) ;; backward-compatibility with ASDF2 only. Unused.
1.13627-
1.13628- ;; Do NOT use in new code. NOT SUPPORTED.
1.13629- ;; NB: When this goes away, remove the slot PROPERTY in COMPONENT.
1.13630- ;; In Quicklisp 2014-05, it's still used by yaclml, amazon-ecs, blackthorn-engine, cl-tidy.
1.13631- ;; See TODO for further cleanups required before to get rid of it.
1.13632- (defgeneric component-property (component property))
1.13633- (defgeneric (setf component-property) (new-value component property))
1.13634-
1.13635- (defmethod component-property ((c component) property)
1.13636- (cdr (assoc property (slot-value c 'properties) :test #'equal)))
1.13637-
1.13638- (defmethod (setf component-property) (new-value (c component) property)
1.13639- (let ((a (assoc property (slot-value c 'properties) :test #'equal)))
1.13640- (if a
1.13641- (setf (cdr a) new-value)
1.13642- (setf (slot-value c 'properties)
1.13643- (acons property new-value (slot-value c 'properties)))))
1.13644- new-value)
1.13645-
1.13646-
1.13647- ;; This method survives from ASDF 1, but really it is superseded by action-description.
1.13648- (defgeneric explain (operation component)
1.13649- (:documentation "Display a message describing an action.
1.13650-
1.13651-DEPRECATED. Use ASDF:ACTION-DESCRIPTION and/or ASDF::FORMAT-ACTION instead."))
1.13652- (progn
1.13653- (define-convenience-action-methods explain (operation component)))
1.13654- (defmethod explain ((o operation) (c component))
1.13655- (asdf-message (compatfmt "~&~@<; ~@;~A~:>~%") (action-description o c))))
1.13656-
1.13657-(with-asdf-deprecation (:style-warning "3.3")
1.13658- (defun system-registered-p (name)
1.13659- "DEPRECATED. Return a generalized boolean that is true if a system of given NAME was registered already.
1.13660-NAME is a system designator, to be normalized by COERCE-NAME.
1.13661-The value returned if true is a pair of a timestamp and a system object."
1.13662- (if-let (system (registered-system name))
1.13663- (cons (if-let (primary-system (registered-system (primary-system-name name)))
1.13664- (component-operation-time 'define-op primary-system))
1.13665- system)))
1.13666-
1.13667- (defun require-system (system &rest keys &key &allow-other-keys)
1.13668- "Ensure the specified SYSTEM is loaded, passing the KEYS to OPERATE, but do not update the
1.13669-system or its dependencies if it has already been loaded."
1.13670- (declare (ignore keys))
1.13671- (unless (component-loaded-p system)
1.13672- (load-system system))))
1.13673-
1.13674-;;; This function is for backward compatibility with ECL only.
1.13675-#+ecl
1.13676-(with-asdf-deprecation (:style-warning "3.2" :warning "9999")
1.13677- (defun make-build (system &rest args
1.13678- &key (monolithic nil) (type :fasl) (move-here nil move-here-p)
1.13679- prologue-code epilogue-code no-uiop
1.13680- prefix-lisp-object-files postfix-lisp-object-files extra-object-files
1.13681- &allow-other-keys)
1.13682- (let* ((operation (asdf/bundle::select-bundle-operation type monolithic))
1.13683- (move-here-path (if (and move-here
1.13684- (typep move-here '(or pathname string)))
1.13685- (ensure-pathname move-here :namestring :lisp :ensure-directory t)
1.13686- (system-relative-pathname system "asdf-output/")))
1.13687- (extra-build-args (remove-plist-keys
1.13688- '(:monolithic :type :move-here
1.13689- :prologue-code :epilogue-code :no-uiop
1.13690- :prefix-lisp-object-files :postfix-lisp-object-files
1.13691- :extra-object-files)
1.13692- args))
1.13693- (build-system (if (subtypep operation 'image-op)
1.13694- (eval `(defsystem "asdf.make-build"
1.13695- :class program-system
1.13696- :source-file nil
1.13697- :pathname ,(system-source-directory system)
1.13698- :build-operation ,operation
1.13699- :build-pathname ,(subpathname move-here-path
1.13700- (file-namestring (first (output-files operation system))))
1.13701- :depends-on (,(coerce-name system))
1.13702- :prologue-code ,prologue-code
1.13703- :epilogue-code ,epilogue-code
1.13704- :no-uiop ,no-uiop
1.13705- :prefix-lisp-object-files ,prefix-lisp-object-files
1.13706- :postfix-lisp-object-files ,postfix-lisp-object-files
1.13707- :extra-object-files ,extra-object-files
1.13708- :extra-build-args ,extra-build-args))
1.13709- system))
1.13710- (files (output-files operation build-system)))
1.13711- (operate operation build-system)
1.13712- (if (or move-here
1.13713- (and (null move-here-p) (member operation '(program-op image-op))))
1.13714- (loop :with dest-path = (resolve-symlinks* (ensure-directories-exist move-here-path))
1.13715- :for f :in files
1.13716- :for new-f = (make-pathname :name (pathname-name f)
1.13717- :type (pathname-type f)
1.13718- :defaults dest-path)
1.13719- :do (rename-file-overwriting-target f new-f)
1.13720- :collect new-f)
1.13721- files))))
1.13722-;;;; ---------------------------------------------------------------------------
1.13723-;;;; Handle ASDF package upgrade, including implementation-dependent magic.
1.13724-
1.13725-(uiop/package:define-package :asdf/interface
1.13726- (:nicknames :asdf :asdf-utilities)
1.13727- (:recycle :asdf/interface :asdf)
1.13728- (:unintern
1.13729- #:loaded-systems ; makes for annoying SLIME completion
1.13730- #:output-files-for-system-and-operation) ; ASDF-BINARY-LOCATION function we use to detect ABL
1.13731- (:use :uiop/common-lisp :uiop :asdf/upgrade :asdf/session
1.13732- :asdf/component :asdf/system :asdf/system-registry :asdf/find-component
1.13733- :asdf/operation :asdf/action :asdf/lisp-action
1.13734- :asdf/output-translations :asdf/source-registry
1.13735- :asdf/forcing :asdf/plan :asdf/operate :asdf/find-system :asdf/parse-defsystem
1.13736- :asdf/bundle :asdf/concatenate-source
1.13737- :asdf/backward-internals :asdf/backward-interface :asdf/package-inferred-system)
1.13738- ;; Note: (1) we are NOT automatically reexporting everything from previous packages.
1.13739- ;; (2) we only reexport UIOP functionality when backward-compatibility requires it.
1.13740- (:export
1.13741- #:defsystem #:find-system #:load-asd #:locate-system #:coerce-name
1.13742- #:primary-system-name #:primary-system-p
1.13743- #:oos #:operate #:make-plan #:perform-plan #:sequential-plan
1.13744- #:system-definition-pathname
1.13745- #:search-for-system-definition #:find-component #:component-find-path
1.13746- #:compile-system #:load-system #:load-systems #:load-systems*
1.13747- #:require-system #:test-system #:clear-system
1.13748- #:operation #:make-operation #:find-operation
1.13749- #:upward-operation #:downward-operation #:sideway-operation #:selfward-operation
1.13750- #:non-propagating-operation
1.13751- #:build-op #:make
1.13752- #:load-op #:prepare-op #:compile-op
1.13753- #:prepare-source-op #:load-source-op #:test-op #:define-op
1.13754- #:feature #:version #:version-satisfies #:upgrade-asdf
1.13755- #:implementation-identifier #:implementation-type #:hostname
1.13756- #:component-depends-on ; backward-compatible name rather than action-depends-on
1.13757- #:input-files #:additional-input-files
1.13758- #:output-files #:output-file #:perform #:perform-with-restarts
1.13759- #:operation-done-p #:explain #:action-description #:component-sideway-dependencies
1.13760- #:needed-in-image-p
1.13761- #:bundle-op #:monolithic-bundle-op #:precompiled-system #:compiled-file #:bundle-system
1.13762- #:program-system
1.13763- #:basic-compile-bundle-op #:prepare-bundle-op
1.13764- #:compile-bundle-op #:load-bundle-op #:monolithic-compile-bundle-op #:monolithic-load-bundle-op
1.13765- #:lib-op #:dll-op #:deliver-asd-op #:program-op #:image-op
1.13766- #:monolithic-lib-op #:monolithic-dll-op #:monolithic-deliver-asd-op
1.13767- #:concatenate-source-op
1.13768- #:load-concatenated-source-op
1.13769- #:compile-concatenated-source-op
1.13770- #:load-compiled-concatenated-source-op
1.13771- #:monolithic-concatenate-source-op
1.13772- #:monolithic-load-concatenated-source-op
1.13773- #:monolithic-compile-concatenated-source-op
1.13774- #:monolithic-load-compiled-concatenated-source-op
1.13775- #:operation-monolithic-p
1.13776- #:required-components
1.13777- #:component-loaded-p
1.13778- #:component #:parent-component #:child-component #:system #:module
1.13779- #:file-component #:source-file #:c-source-file #:java-source-file
1.13780- #:cl-source-file #:cl-source-file.cl #:cl-source-file.lsp
1.13781- #:static-file #:doc-file #:html-file
1.13782- #:file-type #:source-file-type
1.13783- #:register-preloaded-system #:sysdef-preloaded-system-search
1.13784- #:register-immutable-system #:sysdef-immutable-system-search
1.13785- #:package-inferred-system #:register-system-packages
1.13786- #:component-children
1.13787- #:component-children-by-name
1.13788- #:component-pathname
1.13789- #:component-relative-pathname
1.13790- #:component-name
1.13791- #:component-version
1.13792- #:component-parent
1.13793- #:component-system
1.13794- #:component-encoding
1.13795- #:component-external-format
1.13796- #:system-description
1.13797- #:system-long-description
1.13798- #:system-author
1.13799- #:system-maintainer
1.13800- #:system-license
1.13801- #:system-licence
1.13802- #:system-version
1.13803- #:system-source-file
1.13804- #:system-source-directory
1.13805- #:system-relative-pathname
1.13806- #:system-homepage
1.13807- #:system-mailto
1.13808- #:system-bug-tracker
1.13809- #:system-long-name
1.13810- #:system-source-control
1.13811- #:map-systems
1.13812- #:system-defsystem-depends-on
1.13813- #:system-depends-on
1.13814- #:system-weakly-depends-on
1.13815- #:*system-definition-search-functions* ; variables
1.13816- #:*central-registry*
1.13817- #:*compile-file-warnings-behaviour*
1.13818- #:*compile-file-failure-behaviour*
1.13819- #:*resolve-symlinks*
1.13820- #:*verbose-out*
1.13821- #:asdf-version
1.13822- #:compile-condition #:compile-file-error #:compile-warned-error #:compile-failed-error
1.13823- #:compile-warned-warning #:compile-failed-warning
1.13824- #:error-name
1.13825- #:error-pathname
1.13826- #:load-system-definition-error
1.13827- #:error-component #:error-operation
1.13828- #:system-definition-error
1.13829- #:missing-component
1.13830- #:missing-component-of-version
1.13831- #:missing-dependency
1.13832- #:missing-dependency-of-version
1.13833- #:circular-dependency ; errors
1.13834- #:duplicate-names #:non-toplevel-system #:non-system-system #:bad-system-name #:system-out-of-date
1.13835- #:package-inferred-system-missing-package-error
1.13836- #:operation-definition-warning #:operation-definition-error
1.13837- #:try-recompiling ; restarts
1.13838- #:retry
1.13839- #:accept
1.13840- #:coerce-entry-to-directory
1.13841- #:remove-entry-from-registry
1.13842- #:clear-configuration-and-retry
1.13843- #:*encoding-detection-hook*
1.13844- #:*encoding-external-format-hook*
1.13845- #:*default-encoding*
1.13846- #:*utf-8-external-format*
1.13847- #:clear-configuration
1.13848- #:*output-translations-parameter*
1.13849- #:initialize-output-translations
1.13850- #:disable-output-translations
1.13851- #:clear-output-translations
1.13852- #:ensure-output-translations
1.13853- #:apply-output-translations
1.13854- #:compile-file*
1.13855- #:compile-file-pathname*
1.13856- #:*warnings-file-type* #:enable-deferred-warnings-check #:disable-deferred-warnings-check
1.13857- #:enable-asdf-binary-locations-compatibility
1.13858- #:*default-source-registries*
1.13859- #:*source-registry-parameter*
1.13860- #:initialize-source-registry
1.13861- #:compute-source-registry
1.13862- #:clear-source-registry
1.13863- #:ensure-source-registry
1.13864- #:process-source-registry
1.13865- #:registered-system #:registered-systems #:already-loaded-systems
1.13866- #:resolve-location
1.13867- #:asdf-message
1.13868- #:*user-cache*
1.13869- #:user-output-translations-pathname
1.13870- #:system-output-translations-pathname
1.13871- #:user-output-translations-directory-pathname
1.13872- #:system-output-translations-directory-pathname
1.13873- #:user-source-registry
1.13874- #:system-source-registry
1.13875- #:user-source-registry-directory
1.13876- #:system-source-registry-directory
1.13877-
1.13878- ;; The symbols below are all DEPRECATED, do not use. To be removed in a further release.
1.13879- #:*asdf-verbose* #:run-shell-command
1.13880- #:component-load-dependencies #:system-registered-p #:package-system
1.13881- #+ecl #:make-build
1.13882- #:operation-on-warnings #:operation-on-failure #:operation-error
1.13883- #:compile-failed #:compile-warned #:compile-error
1.13884- #:module-components #:component-property #:traverse))
1.13885-;;;; ---------------------------------------------------------------------------
1.13886-;;;; ASDF-USER, where the action happens.
1.13887-
1.13888-(uiop/package:define-package :asdf/user
1.13889- (:nicknames :asdf-user)
1.13890- ;; NB: releases before 3.1.2 this :use'd only uiop/package instead of uiop below.
1.13891- ;; They also :use'd uiop/common-lisp, that reexports common-lisp and is not included in uiop.
1.13892- ;; ASDF3 releases from 2.27 to 2.31 called uiop asdf-driver and asdf/foo uiop/foo.
1.13893- ;; ASDF1 and ASDF2 releases (2.26 and earlier) create a temporary package
1.13894- ;; that only :use's :cl and :asdf
1.13895- (:use :uiop/common-lisp :uiop :asdf/interface))
1.13896-;;;; -----------------------------------------------------------------------
1.13897-;;;; ASDF Footer: last words and cleanup
1.13898-
1.13899-(uiop/package:define-package :asdf/footer
1.13900- (:recycle :asdf/footer :asdf)
1.13901- (:use :uiop/common-lisp :uiop
1.13902- :asdf/system ;; used by ECL
1.13903- :asdf/upgrade :asdf/system-registry :asdf/operate :asdf/bundle)
1.13904- ;; Happily, all those implementations all have the same module-provider hook interface.
1.13905- #+(or abcl clasp cmucl clozure ecl mezzano mkcl sbcl)
1.13906- (:import-from #+abcl :sys #+(or clasp cmucl ecl) :ext #+clozure :ccl #+mkcl :mk-ext #+sbcl sb-ext #+mezzano :sys.int
1.13907- #:*module-provider-functions*
1.13908- #+ecl #:*load-hooks*)
1.13909- #+(or clasp mkcl) (:import-from :si #:*load-hooks*))
1.13910-
1.13911-(in-package :asdf/footer)
1.13912-
1.13913-;;;; Register ASDF itself and all its subsystems as preloaded.
1.13914-(with-upgradability ()
1.13915- (dolist (s '("asdf" "asdf-package-system"))
1.13916- ;; Don't bother with these system names, no one relies on them anymore:
1.13917- ;; "asdf-utils" "asdf-bundle" "asdf-driver" "asdf-defsystem"
1.13918- (register-preloaded-system s :version *asdf-version*))
1.13919- (register-preloaded-system "uiop" :version *uiop-version*))
1.13920-
1.13921-;;;; Ensure that the version slot on the registered preloaded systems are
1.13922-;;;; correct, by CLEARing the system. However, we do not CLEAR-SYSTEM
1.13923-;;;; unconditionally. This is because it's possible the user has upgraded the
1.13924-;;;; systems using ASDF itself, meaning that the registered systems have real
1.13925-;;;; data from the file system that we want to preserve instead of blasting
1.13926-;;;; away and replacing with a blank preloaded system.
1.13927-(with-upgradability ()
1.13928- (unless (equal (system-version (registered-system "asdf")) (asdf-version))
1.13929- (clear-system "asdf"))
1.13930- ;; 3.1.2 is the last version where asdf-package-system was a separate system.
1.13931- (when (version< "3.1.2" (system-version (registered-system "asdf-package-system")))
1.13932- (clear-system "asdf-package-system"))
1.13933- (unless (equal (system-version (registered-system "uiop")) *uiop-version*)
1.13934- (clear-system "uiop")))
1.13935-
1.13936-;;;; Hook ASDF into the implementation's REQUIRE and other entry points.
1.13937-#+(or abcl clasp clisp clozure cmucl ecl mezzano mkcl sbcl)
1.13938-(with-upgradability ()
1.13939- ;; Hook into CL:REQUIRE.
1.13940- #-clisp (pushnew 'module-provide-asdf *module-provider-functions*)
1.13941- #+clisp (if-let (x (find-symbol* '#:*module-provider-functions* :custom nil))
1.13942- (eval `(pushnew 'module-provide-asdf ,x)))
1.13943-
1.13944- #+(or clasp ecl mkcl)
1.13945- (progn
1.13946- (pushnew '("fasb" . si::load-binary) *load-hooks* :test 'equal :key 'car)
1.13947-
1.13948- #+os-windows
1.13949- (unless (assoc "asd" *load-hooks* :test 'equal)
1.13950- (appendf *load-hooks* '(("asd" . si::load-source))))
1.13951-
1.13952- ;; Wrap module provider functions in an idempotent, upgrade friendly way
1.13953- (defvar *wrapped-module-provider* (make-hash-table))
1.13954- (setf (gethash 'module-provide-asdf *wrapped-module-provider*) 'module-provide-asdf)
1.13955- (defun wrap-module-provider (provider name)
1.13956- (let ((results (multiple-value-list (funcall provider name))))
1.13957- (when (first results) (register-preloaded-system (coerce-name name)))
1.13958- (values-list results)))
1.13959- (defun wrap-module-provider-function (provider)
1.13960- (ensure-gethash provider *wrapped-module-provider*
1.13961- (constantly
1.13962- #'(lambda (module-name)
1.13963- (wrap-module-provider provider module-name)))))
1.13964- (setf *module-provider-functions*
1.13965- (mapcar #'wrap-module-provider-function *module-provider-functions*))))
1.13966-
1.13967-#+cmucl ;; Hook into the CMUCL herald.
1.13968-(with-upgradability ()
1.13969- (defun herald-asdf (stream)
1.13970- (format stream " ASDF ~A" (asdf-version)))
1.13971- (setf (getf ext:*herald-items* :asdf) '(herald-asdf)))
1.13972-
1.13973-
1.13974-;;;; Done!
1.13975-(with-upgradability ()
1.13976- #+allegro ;; restore *w-o-n-r-c* setting as saved in uiop/common-lisp
1.13977- (when (boundp 'excl:*warn-on-nested-reader-conditionals*)
1.13978- (setf excl:*warn-on-nested-reader-conditionals* uiop/common-lisp::*acl-warn-save*))
1.13979-
1.13980- ;; Advertise the features we provide.
1.13981- (dolist (f '(:asdf :asdf2 :asdf3 :asdf3.1 :asdf3.2 :asdf3.3)) (pushnew f *features*))
1.13982-
1.13983- ;; Provide both lowercase and uppercase, to satisfy more people, especially LispWorks users.
1.13984- (provide "asdf") (provide "ASDF")
1.13985-
1.13986- ;; Finally, call a function that will cleanup in case this is an upgrade of an older ASDF.
1.13987- (cleanup-upgraded-asdf))
1.13988-
1.13989-(when *load-verbose*
1.13990- (asdf-message ";; ASDF, version ~a~%" (asdf-version)))