package.lisp 26.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
;;;; ---------------------------------------------------------------------------
;;;; Handle ASDF package upgrade, including implementation-dependent magic.
;;
;; See https://bugs.launchpad.net/asdf/+bug/485687
;;
;; CAUTION: we must handle the first few packages specially for hot-upgrade.
;; asdf/package will be frozen as of 2.27
;; to forever export the same exact symbols.
;; Any other symbol must be import-from'ed
;; and reexported in a different package
;; (alternatively the package may be dropped & replaced by one with a new name).

(defpackage :asdf/package
  (:use :common-lisp)
  (:export
16 17 18
   #:find-package* #:find-symbol* #:symbol-call #:intern* #:unintern*
   #:symbol-shadowing-p #:rehome-symbol
   #:delete-package* #:package-names #:packages-from-names
19
   #:package-definition-form #:ensure-package #:define-package))
20 21 22 23 24 25

(in-package :asdf/package)

;;;; General purpose package utilities

(eval-when (:load-toplevel :compile-toplevel :execute)
26 27 28 29 30 31 32
  (defun find-package* (package-designator &optional (error t))
    (let ((package (find-package package-designator)))
      (cond
        (package package)
        (error (error "No package named ~S" (string package-designator)))
        (t nil))))
  (defun find-symbol* (name package-designator &optional (error t))
33 34 35 36 37 38
    "Find a symbol in a package of given string'ified NAME;
unless CL:FIND-SYMBOL, work well with 'modern' case sensitive syntax
by letting you supply a symbol or keyword for the name;
also works well when the package is not present.
If optional ERROR argument is NIL, return NIL instead of an error
when the symbol is not found."
39 40
    (block nil
      (let ((package (find-package* package-designator error)))
41
        (when package ;; package error handled by find-package* already
42 43 44 45 46
          (multiple-value-bind (symbol status) (find-symbol (string name) package)
            (cond
              (status (return (values symbol status)))
              (error (error "There is no symbol ~S in package ~S" name (package-name package))))))
        (values nil nil))))
47 48 49 50 51
  (defun symbol-call (package name &rest args)
    "Call a function associated with symbol of given name in given package,
with given ARGS. Useful when the call is read before the package is loaded,
or when loading the package is optional."
    (apply (find-symbol* name package) args))
52
  (defun intern* (name package-designator &optional (error t))
53
    (intern (string name) (find-package* package-designator error)))
54 55 56 57 58 59 60 61 62 63 64
  (defun unintern* (name package-designator &optional (error t))
    (block nil
      (let ((package (find-package* package-designator error)))
        (when package
          (multiple-value-bind (symbol status) (find-symbol* name package error)
            (cond
              (status (unintern symbol package)
                      (return (values symbol status)))
              (error (error "symbol ~A not present in package ~A"
                            (string symbol) (package-name package))))))
        (values nil nil))))
65
  (defun symbol-shadowing-p (symbol package)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
66 67 68 69 70 71 72 73 74 75 76 77 78
    (and (member symbol (package-shadowing-symbols package)) t))
  (defun symbol-package-name (symbol)
    (let ((package (symbol-package symbol)))
      (and package (package-name package))))
  (defun symbol-vector (symbol)
    (vector (symbol-name symbol) (symbol-package-name symbol)))
  (defun vector-symbol (vector)
    (let* ((symbol-name (aref vector 0))
           (package-name (aref vector 1)))
      (if package-name (intern symbol-name package-name)
          (make-symbol symbol-name)))))

(eval-when (:load-toplevel :compile-toplevel :execute)
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
  #+(or clisp clozure)
  (defun get-setf-function-symbol (symbol)
    #+clisp (let ((sym (get symbol 'system::setf-function)))
              (if sym (values sym :setf-function)
                  (let ((sym (get symbol 'system::setf-expander)))
                    (if sym (values sym :setf-expander)
                        (values nil nil)))))
    #+clozure (gethash symbol ccl::%setf-function-names%))
  #+(or clisp clozure)
  (defun set-setf-function-symbol (new-setf-symbol symbol &optional kind)
    #+clisp (assert (member kind '(:setf-function :setf-expander)))
    #+clozure (assert (eq kind t))
    #+clisp
    (cond
      ((null new-setf-symbol)
       (remprop symbol 'system::setf-function)
       (remprop symbol 'system::setf-expander))
      ((eq kind :setf-function)
       (setf (get symbol 'system::setf-function) new-setf-symbol))
      ((eq kind :setf-expander)
       (setf (get symbol 'system::setf-expander) new-setf-symbol)))
    #+clozure
    (progn
      (gethash symbol ccl::%setf-function-names%) new-setf-symbol
      (gethash new-setf-symbol ccl::%setf-function-name-inverses%) symbol))
  #+(or clisp clozure)
  (defun create-setf-function-symbol (symbol)
    #+clisp (system::setf-symbol symbol)
    #+clozure (ccl::construct-setf-function-name symbol))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
  (defun set-dummy-symbol (symbol reason other-symbol)
    (setf (get symbol 'dummy-symbol) (cons reason other-symbol)))
  (defun make-dummy-symbol (symbol)
    (let ((dummy (copy-symbol symbol)))
      (set-dummy-symbol dummy 'replacing symbol)
      (set-dummy-symbol symbol 'replaced-by dummy)
      dummy))
  (defun dummy-symbol (symbol)
    (get symbol 'dummy-symbol))
  (defun get-dummy-symbol (symbol)
    (let ((existing (dummy-symbol symbol)))
      (if existing (values (cdr existing) (car existing))
          (make-dummy-symbol symbol))))
  (defun nuke-symbol-in-package (symbol package-designator)
    (let ((package (find-package* package-designator))
          (name (symbol-name symbol)))
      (multiple-value-bind (sym stat) (find-symbol name package)
        (when (and (member stat '(:internal :external)) (eq symbol sym))
          (if (symbol-shadowing-p symbol package)
              (shadowing-import (get-dummy-symbol symbol) package)
              (unintern symbol package))))))
  (defun nuke-symbol (symbol &optional (packages (list-all-packages)))
    #+(or clisp clozure)
    (multiple-value-bind (setf-symbol kind)
        (get-setf-function-symbol symbol)
      (when kind (nuke-symbol setf-symbol)))
    (loop :for p :in packages :do (nuke-symbol-in-package symbol p)))
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
  (defun rehome-symbol (symbol package-designator)
    "Changes the home package of a symbol, also leaving it present in its old home if any"
    (let* ((name (symbol-name symbol))
           (package (find-package* package-designator))
           (old-package (symbol-package symbol))
           (old-status (and old-package (nth-value 1 (find-symbol name old-package))))
           (shadowing (and old-package (symbol-shadowing-p symbol old-package) (make-symbol name))))
      (multiple-value-bind (overwritten-symbol overwritten-symbol-status) (find-symbol name package)
        (unless (eq package old-package)
          (let ((overwritten-symbol-shadowing-p
                  (and overwritten-symbol-status
                       (symbol-shadowing-p overwritten-symbol package))))
            (when old-package
              (if shadowing
                  (shadowing-import shadowing old-package))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
150
              (unintern symbol old-package))
151 152 153 154 155 156 157 158 159 160
            (cond
              (overwritten-symbol-shadowing-p
               (shadowing-import symbol package))
              (t
               (when overwritten-symbol-status
                 (unintern overwritten-symbol package))
               (import symbol package)))
            (if shadowing
                (shadowing-import symbol old-package)
                (import symbol old-package))
161 162 163 164 165 166 167 168 169
            #+(or clisp clozure)
            (multiple-value-bind (setf-symbol kind)
                (get-setf-function-symbol symbol)
              (when kind
                (let* ((setf-function (fdefinition setf-symbol))
                       (new-setf-symbol (create-setf-function-symbol symbol)))
                  (setf (fdefinition new-setf-symbol) setf-function)
                  (set-setf-function-symbol symbol new-setf-symbol kind))))
            #+(or clisp clozure)
170
            (multiple-value-bind (overwritten-setf foundp)
171
                (get-setf-function-symbol overwritten-symbol)
172 173 174 175 176 177 178
              (when foundp
                (unintern overwritten-setf)))
            (when (eq old-status :external)
              (export symbol old-package))
            (when (eq overwritten-symbol-status :external)
              (export symbol package))))
        (values overwritten-symbol overwritten-symbol-status))))
179 180 181
  (defun ensure-package-unused (package)
    (loop :for p :in (package-used-by-list package) :do
      (unuse-package package p)))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
182
  (defun delete-package* (package &optional nuke)
183 184
    (let ((p (find-package package)))
      (when p
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
185
        (when nuke (do-symbols (s p) (when (eq p (symbol-package s)) (nuke-symbol s))))
186 187
        (ensure-package-unused p)
        (delete-package package))))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
188 189 190 191 192 193 194 195
  (defun fresh-package-name (&optional (prefix :%TO-BE-DELETED)
                               (index (random most-positive-fixnum)))
    (loop :for i :from index
          :for n = (format nil "~A-~D" prefix i)
          :thereis (and (not (find-package n)) n)))
  (defun rename-package-away (p)
    (rename-package
     p (fresh-package-name (format nil "__~A__" (package-name p)) 0)))
196 197
  (defun package-names (package)
    (cons (package-name package) (package-nicknames package)))
198 199 200 201 202 203 204
  (defun packages-from-names (names)
    (remove-duplicates (remove nil (mapcar #'find-package names)) :from-end t)))


;;; Communicable representation of symbol and package information

(eval-when (:load-toplevel :compile-toplevel :execute)
205
  (defun package-definition-form (package-designator &key internp (nicknamesp t) (error t))
206 207 208 209 210 211 212 213 214
    (let* ((package (find-package* package-designator error))
           (name (package-name package))
           (nicknames (package-nicknames package))
           (use (mapcar #'package-name (package-use-list package)))
           (shadow ())
           (shadowing-import (make-hash-table :test 'equal))
           (import (make-hash-table :test 'equal))
           (export ())
           (intern ()))
215
      (when package
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
        (loop :for sym :being :the :symbols :in package
              :for status = (nth-value 1 (find-symbol* sym package)) :do
                (ecase status
                  ((nil :inherited))
                  ((:internal :external)
                   (let* ((name (symbol-name sym))
                          (external (eq status :external))
                          (home (symbol-package sym))
                          (home-name (package-name home))
                          (imported (not (eq home package)))
                          (shadowing (symbol-shadowing-p sym package)))
                     (cond
                       ((and shadowing import)
                        (push name (gethash home-name shadowing-import)))
                       (shadowing
                        (push name shadow))
                       (imported
                        (push name (gethash home-name import))))
                     (cond
                       (external
                        (push name export))
                       (imported)
                       (t (push name intern)))))))
        (labels ((sort-names (names)
                   (sort names #'string<))
                 (table-keys (table)
                   (loop :for k :being :the :hash-keys :of table :collect k))
                 (when-relevant (key value)
                   (when value (list (cons key value))))
                 (import-options (key table)
                   (loop :for i :in (sort-names (table-keys table))
                         :collect `(,key ,i ,@(sort-names (gethash i table))))))
          `(defpackage ,name
249
             ,@(when-relevant :nicknames (and nicknamesp (sort-names nicknames)))
250 251 252 253 254 255
             (:use ,@(sort-names use))
             ,@(when-relevant :shadow (sort-names shadow))
             ,@(import-options :shadowing-import-from shadowing-import)
             ,@(import-options :import-from import)
             ,@(when-relevant :export (sort-names export))
             ,@(when-relevant :intern (and internp (sort-names intern)))))))))
256

257 258 259

;;; ensure-package, define-package

260
(eval-when (:load-toplevel :compile-toplevel :execute)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
261
  (defvar *fishy-package-changes* '(t))
262
  (defun ensure-package (name &key
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
263
                                (fishyp *fishy-package-changes*)
264 265 266 267
                                nicknames documentation use
                                shadow shadowing-import-from
                                import-from export intern
                                recycle mix reexport
268
                                unintern)
269
    (let* ((name (string name))
270
           (nicknames (mapcar #'string nicknames))
271
           (names (cons name nicknames))
272
           (previous (packages-from-names names))
273
           (discarded (cdr previous))
274
           (to-delete ())
275 276
           (package (or (first previous) (make-package name :nicknames nicknames)))
           (recycle (packages-from-names recycle))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
277 278 279 280 281 282 283
           (use (mapcar 'find-package* use))
           (mix (mapcar 'find-package* mix))
           (reexport (mapcar 'find-package* reexport))
           (shadow (mapcar 'string shadow))
           (export (mapcar 'string export))
           (intern (mapcar 'string intern))
           (unintern (mapcar 'string unintern))
284 285 286
           (shadowed (make-hash-table :test 'equal)) ; string to bool
           (imported (make-hash-table :test 'equal)) ; string to bool
           (exported (make-hash-table :test 'equal)) ; string to bool
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
           ;; string to list canonical package and providing package:
           (inherited (make-hash-table :test 'equal))
           (fishy ())) ; fishy stuff we did
      (macrolet ((fishy (&rest info)
                   `(when fishyp (push (list ,@info) fishy))))
        (labels
            ((ensure-shadowing-import (sym p)
               (let ((name (string sym))
                     (import (find-symbol* name p)))
                 (multiple-value-bind (existing status) (find-symbol name package)
                   (cond
                     ((gethash name shadowed)
                      (unless (eq import existing)
                        (error "Conflicting shadowings for ~A" name)))
                     (t
                      (setf (gethash name shadowed) t)
                      (setf (gethash name imported) t)
                      (unless (or (null status)
                                  (and (member status '(:internal :external))
                                       (eq existing sym)
                                       (symbol-shadowing-p existing package)))
                        (fishy :shadowing-import
                               name (package-name p) (symbol-package-name sym)
                               (and status (symbol-package-name existing)) status))
                      (shadowing-import import package))))))
             (ensure-import (sym p)
               (let* ((name (string sym))
                      (import (find-symbol* name p)))
                 (multiple-value-bind (existing status) (find-symbol name package)
                   (cond
                     ((gethash name imported)
                      (unless (eq import existing)
                        (error "Can't import ~S from both ~S and ~S"
                               name (package-name (symbol-package existing)) (package-name p))))
                     ((gethash name shadowed)
                      (error "Can't both shadow ~S and import it from ~S" name (package-name p)))
                     (t
                      (setf (gethash name imported) t)
                      (unless (and status (eq import existing))
                        (when status
                          (unintern* existing package)
                          (fishy :import name (package-name p) (symbol-package-name import)
                                 (and status (symbol-package-name existing)) status))
                        (import import package)))))))
             (ensure-mix (sym p)
               (let* ((name (symbol-name sym))
                      (sp (symbol-package sym)))
                 (unless (or (gethash name shadowed) (gethash name imported))
                   (let ((ip (gethash name inherited)))
                     (cond
                       ((and ip (eq sp (first ip))))
                       (ip
                        (remhash name inherited)
                        (ensure-shadowing-import name (second ip)))
                       (t
                        (ensure-inherited name sym p)))))))
             (ensure-inherited (name symbol p)
               (multiple-value-bind (existing status) (find-symbol name package)
                 (let* ((sp (symbol-package symbol))
                        (ip (gethash name inherited))
                        (xp (and status (symbol-package existing))))
348
                   (cond
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
349
                     ((gethash name shadowed))
350
                     (ip
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
351 352 353 354 355 356 357
                      (unless (equal sp (first ip))
                        (error "Can't inherit ~S from ~S, it is inherited from ~S"
                               name (package-name sp) (package-name (first ip)))))
                     ((gethash name imported)
                      (unless (eq symbol existing)
                        (error "Can't inherit ~S from ~S, it is imported from ~S"
                               name (package-name sp) (package-name xp))))
358
                     (t
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
                      (setf (gethash name inherited) (list sp p))
                      (when status
                        (unintern* existing package)
                        (fishy :inherited name (package-name p) (package-name sp)
                               (package-name xp))))))))
             (home-package-p (symbol package)
               (eq (symbol-package symbol) (find-package* package)))
             (recycle-symbol (name)
               (let (recycled foundp)
                 (dolist (r recycle (values recycled foundp))
                   (multiple-value-bind (symbol status) (find-symbol name r)
                     (when (and status (home-package-p symbol r))
                       (cond
                         (foundp
                          (fishy :recycled-duplicate name (package-name foundp) (package-name r))
                          (nuke-symbol symbol))
                         (t
                          (setf recycled symbol foundp r))))))))
             (symbol-recycled-p (sym)
               (member (symbol-package sym) recycle))
             (ensure-symbol (name &optional intern)
               (unless (or (gethash name shadowed)
                           (gethash name imported)
                           (gethash name inherited))
                 (multiple-value-bind (existing status)
                     (find-symbol name package)
                   (multiple-value-bind (recycled previous) (recycle-symbol name)
                     (cond
                       ((and status (eq existing recycled) (eq previous package)))
                       (previous
                        (rehome-symbol recycled package))
                       ((and status (eq package (symbol-package existing))))
                       (t
                        (when status
                          (unintern existing)
                          (fishy :ensure-symbol name (symbol-package-name existing) status intern))
                        (when intern
                          (intern* name package))))))))
             (ensure-export (name p)
               (multiple-value-bind (symbol status) (find-symbol* name p)
                 (unless (eq status :external)
                   (ensure-exported name symbol p))))
             (ensure-exported (name sym p)
               (dolist (u (package-used-by-list p))
                 (ensure-exported-to-user name sym u))
               (export sym p))
             (ensure-exported-to-user (name sym u)
               (multiple-value-bind (usym ustat) (find-symbol name u)
                 (unless (and ustat (eq sym usym))
                   (let ((accessible
                           (when ustat
                             (let ((shadowing (symbol-shadowing-p usym u))
                                   (recycled (symbol-recycled-p usym)))
                               (unless (and shadowing (not recycled))
                                 (if (or (eq ustat :inherited) shadowing)
                                     (shadowing-import sym u)
                                     (unintern usym u))
                                 (fishy :ensure-export name (symbol-package-name sym)
                                        (package-name u)
                                        (and ustat (symbol-package-name usym)) ustat shadowing)
                                 t)))))
                     (when (and accessible (eq ustat :external))
                       (ensure-exported name sym u)))))))
          #-gcl (setf (documentation package t) documentation) #+gcl documentation
          (loop :for p :in (set-difference (package-use-list package) (append mix use))
                :do (unuse-package p package) (fishy :use (package-names p)))
          (loop :for p :in discarded
                :for n = (remove-if #'(lambda (x) (member x names :test 'equal))
                                    (package-names p))
                :do (fishy :nickname (package-names p))
                    (if n (rename-package p (first n) (rest n))
                        (progn
                          (rename-package-away p)
                          (push p to-delete))))
          (rename-package package name nicknames)
          (dolist (name unintern)
            (multiple-value-bind (existing status) (find-symbol name package)
              (when status
                (unless (eq status :inherited)
                  (unintern* name package nil))
                (fishy :unintern name (symbol-package-name existing) status))))
          (dolist (name export)
            (setf (gethash name exported) t))
          (dolist (p reexport)
            (do-external-symbols (sym p)
              (setf (gethash (string sym) exported) t)))
          (do-external-symbols (sym package)
            (let ((name (symbol-name sym)))
              (unless (gethash name exported)
                (fishy :over-exported name (symbol-package-name sym))
                (unexport sym package))))
          (dolist (name shadow)
            (setf (gethash name shadowed) t)
            (multiple-value-bind (existing status) (find-symbol name package)
              (multiple-value-bind (recycled previous) (recycle-symbol name)
                (let ((shadowing (and status (symbol-shadowing-p existing package))))
                  (cond
                    ((eq previous package))
                    (previous
                     (fishy :shadow-recycled name (package-name previous)
                            (and status (symbol-package-name existing)) status shadowing)
                     (rehome-symbol recycled package))
                    ((or (member status '(nil :inherited))
                         (home-package-p existing package)))
                    (t
                     (let ((dummy (make-symbol name)))
                       (fishy :shadow-imported name (symbol-package-name existing) status shadowing)
                       (shadowing-import dummy package)
                       (import dummy package)))))))
            (shadow name package))
          (loop :for (p . syms) :in shadowing-import-from :do
            (dolist (sym syms) (ensure-shadowing-import sym p)))
          (dolist (p mix)
            (do-external-symbols (sym p) (ensure-mix sym p)))
          (loop :for (p . syms) :in import-from :do
            (dolist (sym syms) (ensure-import sym p)))
          (dolist (p (append use mix))
            (do-external-symbols (sym p) (ensure-inherited (string sym) sym p))
            (use-package p package))
          (loop :for name :being :the :hash-keys :of exported :do
            (ensure-symbol (string name) t)
            (ensure-export name package))
          (dolist (name intern)
            (ensure-symbol (string name) t))
          (do-symbols (sym package)
            (ensure-symbol (symbol-name sym)))
          (map () 'delete-package* to-delete)
          (when fishy (push (cons name fishy) *fishy-package-changes*))
          package)))))
488 489

(eval-when (:load-toplevel :compile-toplevel :execute)
490
  (defun parse-define-package-form (package clauses)
491 492
    (loop
      :with use-p = nil :with recycle-p = nil
493
      :with documentation = nil
494
      :for (kw . args) :in clauses
495
      :when (eq kw :nicknames) :append args :into nicknames :else
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
496 497 498 499 500
        :when (eq kw :documentation)
          :do (cond
                (documentation (error "define-package: can't define documentation twice"))
                ((or (atom args) (cdr args)) (error "define-package: bad documentation"))
                (t (setf documentation (car args)))) :else
501
      :when (eq kw :use) :append args :into use :and :do (setf use-p t) :else
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
502 503 504 505 506 507 508 509 510 511
        :when (eq kw :shadow) :append args :into shadow :else
          :when (eq kw :shadowing-import-from) :collect args :into shadowing-import-from :else
            :when (eq kw :import-from) :collect args :into import-from :else
              :when (eq kw :export) :append args :into export :else
                :when (eq kw :intern) :append args :into intern :else
                  :when (eq kw :recycle) :append args :into recycle :and :do (setf recycle-p t) :else
                    :when (eq kw :mix) :append args :into mix :else
                      :when (eq kw :reexport) :append args :into reexport :else
                        :when (eq kw :unintern) :append args :into unintern :else
                          :do (error "unrecognized define-package keyword ~S" kw)
512 513
      :finally (return `(,package
                         :nicknames ,nicknames :documentation ,documentation
514 515 516 517
                         :use ,(if use-p use '(:common-lisp))
                         :shadow ,shadow :shadowing-import-from ,shadowing-import-from
                         :import-from ,import-from :export ,export :intern ,intern
                         :recycle ,(if recycle-p recycle (cons package nicknames))
518
                         :mix ,mix :reexport ,reexport :unintern ,unintern)))))
519 520 521

(defmacro define-package (package &rest clauses)
  `(eval-when (:compile-toplevel :load-toplevel :execute)
522
     #+(or ecl gcl) (defpackage ,package (:use))
523 524
     #+clisp (macrolet ((foo ()
                          (apply 'ensure-package ',(parse-define-package-form package clauses))
525
                          (package-definition-form ',package :nicknamesp nil)))
526
               (foo))
527
     (apply 'ensure-package ',(parse-define-package-form package clauses))))
528

529 530
#+clisp
(eval-when (:compile-toplevel :load-toplevel :execute)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
531 532 533 534
  (unless (member :asdf2.27 *features*)
    (when (find-package :asdf)
      (delete-package* :asdf t))
    (make-package :asdf :use ())))