[asdf-devel] Local package renaming at compile time

Faré fahree at gmail.com
Mon Mar 5 04:45:29 UTC 2012

It's possible to build CL software with local package renamings
using ASDF 2.018 or later, and I have proof:

However it (at least currently) requires quite some groundwork
if you want to do more than add a non-clashing nickname to an existing package.

Below my sigblock is the current relevant section of the README.

—♯ƒ • François-René ÐVB Rideau •Reflection&Cybernethics• http://fare.tunes.org
The naturalistic fallacy: "if it's natural, it's good."
The anti-naturalistic fallacy: "if it's natural, it's bad."
The a-naturalistic fallacy: "nature has no relationship to good and bad."

with-effective-package-renamings and call-with-effective-package-renamings
are pretty low-level primitives that let you do what you need,
but require you to do a lot of the groundwork.

First, an important constraint to understand about building CL software.
By intentional design, ASDF only has an :around-compile hook,
but no :around-load hook around load,
or :around-build hook around both compile and load;
indeed, some implementations that rely on linking, such as ECL or GCL,
might not even allow any kind of useful hook around loading,
and some things one might want to do with fasls
(such as linking or concatenating them together into a big fasl)
might preclude any such hook.
Therefore, whatever renamings you do at compile-time
will NOT be done available at load-time.
When the fasl is loaded, only the non-renamed names are available,
and symbols, stored with the principal name of their package at compile-time,
will be resolved against the set of packages at load-time.
This imposes this important constraint on renamings that are admissible
in ASDF's :around-compile hook:

For instance, if your target package is COM.FOO.BLAH
and has nicknames CFBLAH and BLAH,
then during your rename, the main name of the package must be one of
or the symbols will fail to resolve properly at load-time.
You can add as many new local nicknames as you want, and use them locally
in your files, you cannot delete all the original names,
and you must use the original names in any operation that will happen
at runtime or load-time, such as defpackage or find-symbol.

Now, if you want to use package-renaming
to locally override a package A with another B,
it is still possible; but you have to use further tricks.
The trick is to somehow ADD a new unique non-clashing nickname
to each of the packages that are involved in a clash,
at some point after they are defined and before they are used by your code.
For instance, give A the nickname A.at.runtime and B the nickname B.at.runtime.
Then, you can make B.at.runtime the principal name
of whatever renaming you make that targets B,
even with local nickname A,
whereas A is previously renamed away to A.at.runtime.

With this constraint in mind,
a typical way to use package-renaming would be
to use the following in your my-system.asd file:

(defsystem my-system
  :depends-on (:my-system-meta)
  :around-compile "my-system-meta::call-with-renamings"

And in B-renaming.asd:

(defsystem my-system-meta
  :depends-on (:package-renaming)
  :components ((:file "renamings")))

And in renamings.lisp:

(defpackage :my-system-meta (:use :cl :package-renaming))

  '((:A (:A :A.regular.nick :A.at.runtime))
    (:B (:B :B.regular.nick :B.at.runtime))))

(defun with-renamings (thunk)
    '((:A :A.at.runtime)
      (:B :B.at.runtime :A :B.other.nick))

That's not all that simple, but at least it's possible.
If you implement an easier all-in-one solution based on this,
I'll happily merge it into this package-renaming.

More information about the asdf-devel mailing list