changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > demo / tk.lisp

changeset 7: 315fedf35bc7
parent: 3d202b181d6c
child: bebb76da449c
author: ellis <ellis@rwest.io>
date: Sun, 07 May 2023 18:06:13 -0400
permissions: -rw-r--r--
description: rust ffi macros, general stuff
1 (in-package #:demo)
2 
3 (defvar *cargo-target* #P"/Users/ellis/dev/otom8/demo/target/")
4 
5 (defmacro rs-find-dll (name &optional debug)
6  "Find the rust dll specified by NAME."
7  (cond
8  ((uiop:directory-exists-p (merge-pathnames *cargo-target* "release"))
9  `,(mkstr "./target/release/" name))
10  ((uiop:directory-exists-p (merge-pathnames *cargo-target* "debug"))
11  `,(mkstr "./target/debug/" name))
12  (t (progn
13  (uiop:run-program `("cargo" "build" ,(unless debug "--release")) :output t)
14  `,(find-rust-dll name debug)))))
15 
16 (defmacro rs-macroexpand (env &body body)
17  "Cbindgen is quite the menace and really doesn't like our macros used
18 to generate C FFI bindings. To compensate for this, we use a tool
19 called cargo-expand by the most excellent dtolnay which expands Rust
20 macros. The expansions are assembled into an equivalent Rust source
21 file which cbindgen won't get stuck in an infinite compile loop on.")
22 
23 (defun random-id ()
24  (format NIL "~8,'0x-~8,'0x" (random #xFFFFFFFF) (get-universal-time)))
25 
26 (defun scan-dir (dir filename callback)
27  (dolist (path (directory (merge-pathnames (merge-pathnames filename "**/") dir)))
28  (funcall callback path)))
29 
30 (defun mkstr (&rest args)
31  (with-output-to-string (s)
32  (dolist (a args) (princ a s))))
33 
34 (defun symb (&rest args)
35  (values (intern (apply #'mkstr args))))
36 
37 (defun sbq-reader (stream sub-char numarg)
38  "The anaphoric sharp-backquote reader: #`((,a1))"
39  (declare (ignore sub-char))
40  (unless numarg (setq numarg 1))
41  `(lambda ,(loop for i from 1 to numarg
42  collect (symb 'a i))
43  ,(funcall
44  (get-macro-character #\`) stream nil)))
45 
46 (eval-when (:execute)
47  (set-dispatch-macro-character
48  #\# #\` #'demo:sbq-reader))