You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASDF中:around-compile参数的编译文件跟踪及:perform替代方案问询

关于ASDF :around-compile钩子与编译跟踪的问题解答

一、你的思路修正

方向是对的,但给出的代码存在问题:input-file在你写的lambda作用域里是未定义变量,直接引用会报错。

ASDF的:around-compile钩子是高阶函数:它接收的next参数是原始的compile-file*函数,你需要包装这个next,在调用它之前获取传入的编译参数(包括input-file)。正确的写法有两种:

写法1:直接接收input-file参数

(defsystem :project
  ;; 其他系统定义参数
  :around-compile (lambda (next)
                    ;; 返回一个新函数,接收compile-file*的所有参数
                    (lambda (input-file &rest keys)
                      (format t "~&Compiling ~A" input-file)
                      ;; 调用原始的compile-file*函数
                      (apply next input-file keys)))
  ;; 其他系统定义参数)

写法2:用关键字参数解构

(defsystem :project
  ;; 其他系统定义参数
  :around-compile (lambda (next)
                    (lambda (&rest args &key input-file &allow-other-keys)
                      (format t "~&Compiling ~A" input-file)
                      (apply next args)))
  ;; 其他系统定义参数)

这两种写法都能正确获取当前编译的文件路径并打印。

二、用:perform关键字实现的方案

完全可以通过:perform自定义compile-op的执行逻辑来实现同样的跟踪功能,具体做法是重写compile-op针对cl-source-file组件的perform方法:

(defmethod perform ((o compile-op) (c cl-source-file))
  ;; 打印当前编译的组件路径
  (format t "~&Compiling ~A" (component-pathname c))
  ;; 调用ASDF原本的编译逻辑
  (call-next-method))

这种方式的优势是可以更灵活地控制编译前后的逻辑,甚至针对不同类型的组件做差异化处理。

内容的提问来源于stack exchange,提问作者davypough

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 11:03:13