Skip to content

Instantly share code, notes, and snippets.

@svspire
Created September 5, 2024 16:05
Show Gist options
  • Save svspire/8a90e1cef0e985963dfbb037b041e1c9 to your computer and use it in GitHub Desktop.
Save svspire/8a90e1cef0e985963dfbb037b041e1c9 to your computer and use it in GitHub Desktop.
Tell ASDF to put your fasl files where you want. Part of the MAYB project.
;;; MAYB-fasl-sanity.lisp
(in-package :cl-user)
; Force ASDF *not* to use ~/.cache but to put fasl files in the same directory as their source.
(eval-when (:load-toplevel :execute)
(let ((sourcereg (probe-file #P"~/.config/common-lisp/source-registry.conf")))
(when sourcereg
(warn "Old-style asdf source registry file found at ~S. This will likely cause problems with ASDF ~
if you don't delete or rename it." sourcereg))))
(require :asdf)
(defparameter *fasl-file-type*
#+CCL (pathname-type ccl::*.fasl-pathname*)
#+Allegro "fasl"
#+Lispworks system::*binary-file-type*
#+SBCL "fasl")
(defparameter *fasl-directory-name* *fasl-file-type*)
(defparameter *force-fasl-directory* t "If true, force the creation of fasl subdirectories for compiled files")
(defun find-fasl (file)
"Puts fasl files in a subdirectory called *fasl-directory-name* within the directory of file itself.
(Creates that directory if necessary, when *force-fasl-directory* is true.)
This way your fasl files don't clutter your source code directory but are nevertheless nearby.
Creates a pathname to a fasl file in a directory called fasl (or whatever the value
of *fasl-directory-name* is) within current directory of file first, and
if no such directory, makes a path to a fasl file right in the file's
directory. If there is such a directory but it contains no fasl of this file,
while the current directory DOES contain one, the latter is returned. This is helpful
when you don't have the source for a fasl file and you want to just put the fasl at
top level."
(let* ((fasl-directory (make-pathname :defaults file
:directory (append (pathname-directory file)
(list *fasl-directory-name*))))
(fasl-dir? (if *force-fasl-directory*
(ensure-directories-exist fasl-directory)
(probe-file fasl-directory)))
(fasl-file (make-pathname :defaults file
:directory (if fasl-dir?
(pathname-directory fasl-directory)
(pathname-directory file))
:type *fasl-file-type*))
(other-fasl-file (make-pathname :defaults file
:type *fasl-file-type*)))
; last chance -- if it only exists at the top level, use that one
(when (and (not (probe-file fasl-file)) (probe-file other-fasl-file))
(setf fasl-file other-fasl-file))
fasl-file))
(eval-when (:compile-toplevel :load-toplevel :execute)
(asdf:disable-output-translations) ; tell ASDF not to put fasl files in its default location (wherever that is)
(asdf:clear-configuration) ; clear all of ASDF's preconceived notions about where things are
;;; Here's the key to making ASDF do your bidding: Tell it to ignore all of its impenetrable
;;; output-translation functionality and just run your own function (#'find-fasl) instead. Fortunately there's a hook for this.
;;; The other benefit here is that by turning off ASDF's misdesigned output-translation functionality,
;;; *LOAD-PATHNAME* and *LOAD-TRUENAME* again will revert to having reasonable values that you can depend on, so you can
;;; write trampoline source code files that make dynamic loading decisions despite ASDF's draconian
;;; bias toward making such decisions impossible.
(setf uiop/pathname::*output-translation-function* 'find-fasl)
;; Following lines stop ASDF from wasting time looking for sources in ridiculous places on a Mac.
;; Trace ASDF/SOURCE-REGISTRY:COLLECT-SUB*DIRECTORIES-ASD-FILES to see ASDF look in ridiculous places if you don't do this.
#+DARWIN
(progn
(setf asdf::*default-source-registries* (remove 'ASDF/SOURCE-REGISTRY:DEFAULT-USER-SOURCE-REGISTRY asdf::*default-source-registries*))
(setf asdf::*default-source-registries* (remove 'ASDF/SOURCE-REGISTRY:DEFAULT-SYSTEM-SOURCE-REGISTRY asdf::*default-source-registries*)))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment