使用 asdf:defsystem 的 :around-compile 参数

问题描述 投票:0回答:1

为了更好地理解 :around-compile 的工作原理,我想跟踪(即打印输出)当前正在编译哪个组件文件。

ASDF 手册似乎暗示该值可以是带有一个参数的 lambda 表达式——例如,(lambda (next) ... (funcall next ...) ...)——它将调用 ASDF 的编译文件* 函数,它可以接受参数(输入文件&rest键&key编译检查输出文件警告文件emit-cfasl&allow-other-keys)。该手册还说 around-compile 挂钩可能会提供额外的关键字参数来传递给对compile-file*的调用,我假设它指的是输入文件之类的项目。下期以后可以用吗?例如,一个简单的案例可能会执行以下操作:

(defsystem :project
  ...
  :around-compile (lambda (next)
                    (format t "~&Compiling ~A" input-file)
                    (funcall next))
  ...)

这是否走在正确的轨道上?这也可以通过 :perform 关键字来完成吗?感谢您的任何见解。

compilation common-lisp asdf
1个回答
0
投票

此 bash 脚本生成

my-system
,一个示例包。

您需要

uiop::*compile-file-pathname*
才能引用编译文件的路径。

我省略了实际上告诉 asdf 使用当前文件夹作为项目文件夹的部分。因此,您必须仅在您的

~/quicklisp/local-projects/
文件夹中生成此 bash 脚本,以便 asdf 可以轻松找到要加载的项目。

因此你会:

cd ~/quicklisp/local-projects/
nano build-my-system.sh

并输入其内容:

#!/bin/bash

# Define the project name and directory
PROJECT_NAME="my-system"
PROJECT_DIR="$(pwd)/$PROJECT_NAME"

# Create the directory structure
echo "Creating directory structure..."
mkdir -p "$PROJECT_DIR/src"

# Create the system definition file (my-system.asd)
echo "Creating $PROJECT_NAME.asd..."
cat <<EOF > "$PROJECT_DIR/$PROJECT_NAME.asd"
(asdf:defsystem "$PROJECT_NAME"
  :description "A simple ASDF system with custom compilation messages"
  :version "0.1"
  :serial t
  :components ((:file "src/file1")
               (:file "src/file2"))
  :around-compile (lambda (thunk)
                    (let ((compiled-file uiop::*compile-file-pathname*))
                      ;; Before compilation
                      (format t "Compiling file: ~A~%" compiled-file)
                      (let ((result (funcall thunk)))
                        ;; After compilation
                        (format t "File ~A is successfully compiled!~%" compiled-file)
                        result))))
EOF

# Create source file 1 (src/file1.lisp)
echo "Creating src/file1.lisp..."
cat <<EOF > "$PROJECT_DIR/src/file1.lisp"
(defun hello ()
  (format t "Hello from file1!~%"))
EOF

# Create source file 2 (src/file2.lisp)
echo "Creating src/file2.lisp..."
cat <<EOF > "$PROJECT_DIR/src/file2.lisp"
(defun goodbye ()
  (format t "Goodbye from file2!~%"))
EOF

# Create a README.md file (optional)
echo "Creating README.md..."
cat <<EOF > "$PROJECT_DIR/README.md"
# My System

This is a simple Common Lisp system with custom compile-time messages using ASDF.
EOF


# Build the system using SBCL
echo "Building the system with SBCL..."
sbcl --eval "(asdf:load-system \"$PROJECT_NAME\")" --eval "(hello)" --eval "(goodbye)" --quit

echo "Build complete!"

并运行脚本:

bash build-my-system.sh

然后它将生成

my-system
及其文件夹和带有 lisp 代码的文件。

它将运行

sbcl
并加载
my-system

输出:

% bash build-my-system.sh
Creating directory structure...
Creating my-system.asd...
Creating src/file1.lisp...
Creating src/file2.lisp...
Creating README.md...
Building the system with SBCL...
This is SBCL 2.4.6, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.

SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses.  See the CREDITS and COPYING files in the
distribution for more information.
Loading ~/.sbclrc...
Compiling file: NIL
; compiling file "/Users/josephus/quicklisp/local-projects/my-system/src/file1.lisp" (written 18 OCT 2024 07:54:17 AM):

; wrote /Users/josephus/.cache/common-lisp/sbcl-2.4.6-macosx-arm64/Users/josephus/quicklisp/local-projects/my-system/src/file1-tmpGHU3ALSV.fasl
; compilation finished in 0:00:00.001
File NIL is successfully compiled!
Compiling file: NIL
; compiling file "/Users/josephus/quicklisp/local-projects/my-system/src/file2.lisp" (written 18 OCT 2024 07:54:17 AM):

; wrote /Users/josephus/.cache/common-lisp/sbcl-2.4.6-macosx-arm64/Users/josephus/quicklisp/local-projects/my-system/src/file2-tmpAAURSO1.fasl
; compilation finished in 0:00:00.000
File NIL is successfully compiled!
Hello from file1!
Goodbye from file2!
Build complete!

对于 Windows 用户,您可以在

quicklisp/local-projects/
文件夹中创建一个 powershell 文件 文件名:
build-my-system.ps1

# Define the project name and directory
$ProjectName = "my-system"
$ProjectDir = Join-Path (Get-Location) $ProjectName

# Create the directory structure
Write-Host "Creating directory structure..."
New-Item -ItemType Directory -Path "$ProjectDir\src" -Force

# Create the system definition file (my-system.asd)
Write-Host "Creating $ProjectName.asd..."
@"
(asdf:defsystem "$ProjectName"
  :description "A simple ASDF system with custom compilation messages"
  :version "0.1"
  :serial t
  :components ((:file "src/file1")
               (:file "src/file2"))
  :around-compile (lambda (thunk)
                    (let ((compiled-file uiop::*compile-file-pathname*))
                      ;; Before compilation
                      (format t "Compiling file: ~A~%" compiled-file)
                      (let ((result (funcall thunk)))
                        ;; After compilation
                        (format t "File ~A is successfully compiled!~%" compiled-file)
                        result))))
"@ | Out-File "$ProjectDir\$ProjectName.asd" -Encoding UTF8

# Create source file 1 (src/file1.lisp)
Write-Host "Creating src/file1.lisp..."
@"
(defun hello ()
  (format t "Hello from file1!~%"))
"@ | Out-File "$ProjectDir\src\file1.lisp" -Encoding UTF8

# Create source file 2 (src/file2.lisp)
Write-Host "Creating src/file2.lisp..."
@"
(defun goodbye ()
  (format t "Goodbye from file2!~%"))
"@ | Out-File "$ProjectDir\src\file2.lisp" -Encoding UTF8

# Create a README.md file (optional)
Write-Host "Creating README.md..."
@"
# My System

This is a simple Common Lisp system with custom compile-time messages using ASDF.
"@ | Out-File "$ProjectDir\README.md" -Encoding UTF8

# Build the system using SBCL
Write-Host "Building the system with SBCL..."
sbcl --eval "(asdf:load-system \"$ProjectName\")" --eval "(hello)" --eval "(goodbye)" --quit

Write-Host "Build complete!"

然后从那里运行它:

./build-my-system.ps1

我希望这能起作用,因为我没有测试 Windows powershell 脚本。 但 bash 脚本可以在我的 Mac 上运行。

© www.soinside.com 2019 - 2024. All rights reserved.