macにroswellを使ってcommon-lisp-jupyter をインストールする

インストールするときにハマったのでメモ

# 最終的にやったこと

1. `brew` で `roswell` のインストール
1. `brew` で `czmq` のインストール
1. 環境変数 `C_INCLUDE_PATH` を `/opt/homebrew/include` に設定
1. `roswell` で `common-lisp-jupyter` のインストール


# インストール作業

## roswell のインストール

```bash
brew install roswell
```

roswell のインストール後は roswell がインストールしたコマンドを実行できるように、 `PATH` に追加しました。

```bash:.zshrc
export PATH="$PATH:$HOME/.roswell/bin"
```


## czmq のインストール

https://yitzchak.github.io/common-lisp-jupyter/install#requirements

homebrew だと `czmq` という名前であるようです。

```bash
brew install czmq
```

## 環境変数の設定

私の環境だと `cc` は Apple clang でした。

```bash
cc --version
Apple clang version 15.0.0 (clang-1500.3.9.4)
Target: arm64-apple-darwin23.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
```

コンパイラに `include` パスを渡したいので下のように設定しました。

```bash
export C_INCLUDE_PATH=/opt/homebrew/include
```

## common-lisp-jupyter のインストール

```bash
ros install common-lisp-jupyter
```

# 実行

```bash
jupyter lab
```

起動するとブラウザが開きます。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/13469/f7167e1e-4eaf-2a66-598b-cbf0487d8d48.png)

Launcher に Common Lisp が入っていれば大丈夫です。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/13469/4537d890-8fe0-d9dd-42e5-664c8507990d.png)

いくつか実行してみました。
ちゃんと実行できています。

# 環境変数の設定について

当初 `ros install common-lisp-jupyter` を実行したら以下のエラーになりました。

```plaintext
% ros install common-lisp-jupyter
To load "common-lisp-jupyter":
  Load 1 ASDF system:
    common-lisp-jupyter
; Loading "common-lisp-jupyter"
...................[1/3] System 'common-lisp-jupyter' found. Loading the system..; cc -o /Users/tamurashingo/.cache/common-lisp/sbcl-2.4.9.roswell-macosx-arm64/Users/tamurashingo/.roswell/lisp/quicklisp/dists/quicklisp/software/pzmq-20210531-git/grovel__grovel-tmpAAURSO1.o -c -g -Wall -Wundef -Wsign-compare -Wpointer-arith -O3 -g -Wall -fdollars-in-identifiers -fno-omit-frame-pointer -fPIC -I/Users/tamurashingo/.roswell/lisp/quicklisp/dists/quicklisp/software/cffi-20241012-git/ /Users/tamurashingo/.cache/common-lisp/sbcl-2.4.9.roswell-macosx-arm64/Users/tamurashingo/.roswell/lisp/quicklisp/dists/quicklisp/software/pzmq-20210531-git/grovel__grovel.c
/Users/tamurashingo/.cache/common-lisp/sbcl-2.4.9.roswell-macosx-arm64/Users/tamurashingo/.roswell/lisp/quicklisp/dists/quicklisp/software/pzmq-20210531-git/grovel__grovel.c:6:10: fatal error: 'zmq.h' file not found
#include <zmq.h>
         ^~~~~~~
1 error generated.
Aborted during step [1/3].
Unhandled CFFI-GROVEL:GROVEL-ERROR in thread #<error printing a SB-THREAD:THREAD: #<PRINT-NOT-READABLE {7008F5EA43}>>: Subprocess #<UIOP/LAUNCH-PROGRAM::PROCESS-INFO {7008EA8923}>
 with command ("cc" "-o" "/Users/tamurashingo/.cache/common-lisp/sbcl-2.4.9.roswell-macosx-arm64/Users/tamurashingo/.roswell/lisp/quicklisp/dists/quicklisp/software/pzmq-20210531-git/grovel__grovel-tmpAAURSO1.o" "-c" "-g" "-Wall" "-Wundef" "-Wsign-compare" "-Wpointer-arith" "-O3" "-g" "-Wall" "-fdollars-in-identifiers" "-fno-omit-frame-pointer" "-fPIC" "-I/Users/tamurashingo/.roswell/lisp/quicklisp/dists/quicklisp/software/cffi-20241012-git/" "/Users/tamurashingo/.cache/common-lisp/sbcl-2.4.9.roswell-macosx-arm64/Users/tamurashingo/.roswell/lisp/quicklisp/dists/quicklisp/software/pzmq-20210531-git/grovel__grovel.c")
 exited with error code 1

Backtrace for: #<SB-THREAD:THREAD tid=259 "main thread" RUNNING {7004F901E3}>
0: (SB-DEBUG::DEBUGGER-DISABLED-HOOK #<CFFI-GROVEL:GROVEL-ERROR "~a" {7008F5DD23}> #<unused argument> :QUIT T)
1: (SB-DEBUG::RUN-HOOK *INVOKE-DEBUGGER-HOOK* #<CFFI-GROVEL:GROVEL-ERROR "~a" {7008F5DD23}>)
2: (INVOKE-DEBUGGER #<CFFI-GROVEL:GROVEL-ERROR "~a" {7008F5DD23}>)
3: (ERROR CFFI-GROVEL:GROVEL-ERROR :FORMAT-CONTROL "~a" :FORMAT-ARGUMENTS (#<UIOP/RUN-PROGRAM:SUBPROCESS-ERROR {7008F5D753}>))
4: (CFFI-GROVEL:GROVEL-ERROR "~a" #<UIOP/RUN-PROGRAM:SUBPROCESS-ERROR {7008F5D753}>)
....
....
```

`zmq.h` は homebrew でインストールしたので `/opt/homebrew/include` に入ってます。

cffi のコードを見ると、 `include` ディレクトリは `/opt/local/include` を指しているのでこれでは `zmq.h` が見つからなくてコンパイルに失敗します。


```lisp
    (setf *cc*
          (or (getenvp "CC")
              #+(or cygwin (not windows)) "cc"
              "gcc")
          *cc-flags*
          (append
           arch-flags
           ;; For MacPorts
           #+darwin (list "-I" "/opt/local/include/")
           ;; ECL internal flags
           #+ecl (parse-command-flags c::*cc-flags*)
           ;; FreeBSD non-base header files
           #+freebsd (list "-I" "/usr/local/include/")
           (split-cflags (getenv "CFLAGS")))
```

https://github.com/cffi/cffi/blob/32c90d4a9a01e809f591276c058e5b3c7f39b154/toolchain/c-toolchain.lisp#L210

さらにコードを見ると `CFLAGS` 環境変数から取得しているようですが、`CFLAGS` に `-I/opt/homebrew/include` を設定しても何故かコンパイルに失敗します。

そのため、コンパイラに指定するのではなくコンパイラが直接参照する環境変数(ここでは`C_INCLUDE_PATH`)を設定したところ、コンパイルが成功しました。


```plaintext
% ros install common-lisp-jupyter
To load "common-lisp-jupyter":
  Load 1 ASDF system:
    common-lisp-jupyter
; Loading "common-lisp-jupyter"
...................[1/3] System 'common-lisp-jupyter' found. Loading the system..; cc -o /Users/tamurashingo/.cache/common-lisp/sbcl-2.4.9.roswell-macosx-arm64/Users/tamurashingo/.roswell/lisp/quicklisp/dists/quicklisp/software/pzmq-20210531-git/grovel__grovel-tmpAAURSO1.o -c -g -Wall -Wundef -Wsign-compare -Wpointer-arith -O3 -g -Wall -fdollars-in-identifiers -fno-omit-frame-pointer -fPIC -I/Users/tamurashingo/.roswell/lisp/quicklisp/dists/quicklisp/software/cffi-20241012-git/ /Users/tamurashingo/.cache/common-lisp/sbcl-2.4.9.roswell-macosx-arm64/Users/tamurashingo/.roswell/lisp/quicklisp/dists/quicklisp/software/pzmq-20210531-git/grovel__grovel.c
/Users/tamurashingo/.cache/common-lisp/sbcl-2.4.9.roswell-macosx-arm64/Users/tamurashingo/.roswell/lisp/quicklisp/dists/quicklisp/software/pzmq-20210531-git/grovel__grovel.c:11:7: warning: unused variable 'autotype_tmp' [-Wunused-variable]
  int autotype_tmp;
      ^
1 warning generated.
; cc -o /Users/tamurashingo/.cache/common-lisp/sbcl-2.4.9.roswell-macosx-arm64/Users/tamurashingo/.roswell/lisp/quicklisp/dists/quicklisp/software/pzmq-20210531-git/grovel__grovel-tmp5GEXGEG5 -g -dynamic -twolevel_namespace /Users/tamurashingo/.cache/common-lisp/sbcl-2.4.9.roswell-macosx-arm64/Users/tamurashingo/.roswell/lisp/quicklisp/dists/quicklisp/software/pzmq-20210531-git/grovel__grovel.o
; /Users/tamurashingo/.cache/common-lisp/sbcl-2.4.9.roswell-macosx-arm64/Users/tamurashingo/.roswell/lisp/quicklisp/dists/quicklisp/software/pzmq-20210531-git/grovel__grovel /Users/tamurashingo/.cache/common-lisp/sbcl-2.4.9.roswell-macosx-arm64/Users/tamurashingo/.roswell/lisp/quicklisp/dists/quicklisp/software/pzmq-20210531-git/grovel__grovel.grovel-tmp.lisp
...............................
[package puri]....................................
[package trivial-do]..............................
[package shasht]..................................
[package static-vectors]..........................
[package trivial-mimes]...........................
[package jupyter].................................
[package jupyter/markdown-formatter]..............
[package jupyter/widgets].........................
[package jupyter/common-lisp].....................
[package jupyter/convert].........................
...............................
; compiling file "/Users/tamurashingo/.roswell/lisp/quicklisp/dists/quicklisp/software/common-lisp-jupyter-20241012-git/src/packages.lisp" (written 10 OCT 2024 05:13:06 AM):


; file: /Users/tamurashingo/.roswell/lisp/quicklisp/dists/quicklisp/software/common-lisp-jupyter-20241012-git/src/packages.lisp
; in: DEFPACKAGE #:JUPYTER/WIDGETS
;     (DEFPACKAGE #:JUPYTER/WIDGETS
;       (:USE #:COMMON-LISP)
;       (:NICKNAMES :JW :JUPYTER-WIDGETS)
;       (:DOCUMENTATION "Package for core Jupyter Widget support.")
;       (:EXPORT #:ACCORDION
;                #:AUDIO
;                #:BLUR
;                #:BOUNDED-FLOAT-TEXT
;                #:BOUNDED-INT-TEXT
;                #:BOX
;                #:BUTTON
;                #:BUTTON-STYLE
;                #:CHECKBOX
;                #:CHECKBOX-STYLE
;                #:COLOR-PICKER
;                ...))
; ==>
;   (SB-IMPL::%DEFPACKAGE "JUPYTER/WIDGETS" '("JW" "JUPYTER-WIDGETS") 'NIL 'NIL
;                         'NIL '("COMMON-LISP") 'NIL 'NIL
;                         '("ACCORDION" "AUDIO" "BLUR" "BOUNDED-FLOAT-TEXT"
;                           "BOUNDED-INT-TEXT" "BOX" "BUTTON" "BUTTON-STYLE"
;                           "CHECKBOX" "CHECKBOX-STYLE" "COLOR-PICKER" "COMBOBOX"
;                           ...)
;                         '("JUPYTER/WIDGETS") 'NIL ...)
;
; caught WARNING:
;   JUPYTER/WIDGETS also exports the following symbols:
;     (JUPYTER/WIDGETS:MAKE-IMAGE JUPYTER/WIDGETS:MAKE-CHECKBOX-STYLE
;                                 JUPYTER/WIDGETS:MAKE-ACCORDION
;                                 JUPYTER/WIDGETS:MAKE-PROGRESS-STYLE
;                                 JUPYTER/WIDGETS:MAKE-DATE-PICKER
;                                 JUPYTER/WIDGETS:MAKE-CONTROLLER
;                                 JUPYTER/WIDGETS:MAKE-INT-SLIDER
;                                 JUPYTER/WIDGETS:MAKE-CHECKBOX
;                                 JUPYTER/WIDGETS:MAKE-BOUNDED-FLOAT-TEXT
;                                 JUPYTER/WIDGETS:MAKE-RADIO-BUTTONS
;                                 JUPYTER/WIDGETS:MAKE-CONTROLLER-BUTTON
;                                 JUPYTER/WIDGETS:MAKE-AUDIO
;                                 JUPYTER/WIDGETS:MAKE-CONTROLLER-AXIS
;                                 JUPYTER/WIDGETS:MAKE-FLOAT-RANGE-SLIDER
;                                 JUPYTER/WIDGETS:MAKE-GRID-BOX
;                                 JUPYTER/WIDGETS:MAKE-SELECT-MULTIPLE
;                                 JUPYTER/WIDGETS:MAKE-BOUNDED-INT-TEXT
;                                 JUPYTER/WIDGETS:MAKE-HTML
;                                 JUPYTER/WIDGETS:MAKE-LINK
;                                 JUPYTER/WIDGETS:MAKE-DESCRIPTION-STYLE
;                                 JUPYTER/WIDGETS:MAKE-TEXT-AREA
;                                 JUPYTER/WIDGETS:MAKE-TOGGLE-BUTTON-STYLE
;                                 JUPYTER/WIDGETS:MAKE-OUTPUT
;                                 JUPYTER/WIDGETS:MAKE-V-BOX
;                                 JUPYTER/WIDGETS:MAKE-SLIDER-STYLE
;                                 JUPYTER/WIDGETS:MAKE-SIDECAR
;                                 JUPYTER/WIDGETS:MAKE-FLOAT-SLIDER
;                                 JUPYTER/WIDGETS:MAKE-TOGGLE-BUTTONS
;                                 JUPYTER/WIDGETS:MAKE-INT-TEXT
;                                 JUPYTER/WIDGETS:MAKE-VALID
;                                 JUPYTER/WIDGETS:MAKE-HTML-MATH-STYLE
;                                 JUPYTER/WIDGETS:MAKE-DROPDOWN
;                                 JUPYTER/WIDGETS:MAKE-TOGGLE-BUTTON
;                                 JUPYTER/WIDGETS:MAKE-FLOAT-PROGRESS
;                                 JUPYTER/WIDGETS:MAKE-TEXT
;                                 JUPYTER/WIDGETS:MAKE-PASSWORD
;                                 JUPYTER/WIDGETS:MAKE-DIRECTIONAL-LINK
;                                 JUPYTER/WIDGETS:MAKE-TOGGLE-BUTTONS-STYLE
;                                 JUPYTER/WIDGETS:MAKE-INT-PROGRESS
;                                 JUPYTER/WIDGETS:MAKE-PLAY
;                                 JUPYTER/WIDGETS:MAKE-LABEL
;                                 JUPYTER/WIDGETS:MAKE-LABEL-STYLE
;                                 JUPYTER/WIDGETS:MAKE-COMBOBOX
;                                 JUPYTER/WIDGETS:MAKE-FLOAT-TEXT
;                                 JUPYTER/WIDGETS:MAKE-COLOR-PICKER
;                                 JUPYTER/WIDGETS:MAKE-HTML-STYLE
;                                 JUPYTER/WIDGETS:MAKE-BUTTON-STYLE
;                                 JUPYTER/WIDGETS:MAKE-TAB
;                                 JUPYTER/WIDGETS:MAKE-SELECT
;                                 JUPYTER/WIDGETS:MAKE-LAYOUT
;                                 JUPYTER/WIDGETS:MAKE-BOX
;                                 JUPYTER/WIDGETS:MAKE-FLOAT-LOG-SLIDER
;                                 JUPYTER/WIDGETS:MAKE-FILE-UPLOAD
;                                 JUPYTER/WIDGETS:MAKE-SELECTION-SLIDER
;                                 JUPYTER/WIDGETS:MAKE-VIDEO
;                                 JUPYTER/WIDGETS:MAKE-BUTTON
;                                 JUPYTER/WIDGETS:MAKE-HTML-MATH
;                                 JUPYTER/WIDGETS:MAKE-TEXT-STYLE
;                                 JUPYTER/WIDGETS:MAKE-SELECTION-RANGE-SLIDER
;                                 JUPYTER/WIDGETS:MAKE-H-BOX
;                                 JUPYTER/WIDGETS:MAKE-INT-RANGE-SLIDER)
;   See also:
;     The ANSI Standard, Macro DEFPACKAGE
;     The SBCL Manual, Variable SB-EXT:*ON-PACKAGE-VARIANCE*


; wrote /Users/tamurashingo/.cache/common-lisp/sbcl-2.4.9.roswell-macosx-arm64/Users/tamurashingo/.roswell/lisp/quicklisp/dists/quicklisp/software/common-lisp-jupyter-20241012-git/src/packages-tmpULDB0RG.fasl
; compilation finished in 0:00:00.003
;
; compilation unit aborted
;   caught 1 fatal ERROR condition
;   caught 1 WARNING condition
Creating directories.
Installing kernel spec file /Users/tamurashingo/Library/Jupyter/kernels/common-lisp/kernel.json
Installing kernel resources to /Users/tamurashingo/Library/Jupyter/kernels/common-lisp/.
[2/3] Processing build-hook..
[3/3] Attempting to install the scripts in roswell/ subdirectory of the system...
Found 2 scripts: cl-jupyter lisp-to-nb
/Users/tamurashingo/.roswell/bin/cl-jupyter
/Users/tamurashingo/.roswell/bin/lisp-to-nb
```


コンパイル時に警告が出ていますが、未使用変数があるという警告なので問題ありません。

また、ライブラリ側ですが、なぜか `/opt/homebrew/lib` を見ているようなのでこちらは何も設定しなくて問題ありませんでした。

```lisp
(defun darwin-fallback-library-path ()
  (or (explode-path-environment-variable "DYLD_FALLBACK_LIBRARY_PATH")
      (list (merge-pathnames #p"lib/" (user-homedir-pathname))
            #+arm64 #p"/opt/homebrew/lib/"
            #p"/opt/local/lib/"
            #p"/usr/local/lib/"
            #p"/usr/lib/")))
```

https://github.com/cffi/cffi/blob/32c90d4a9a01e809f591276c058e5b3c7f39b154/src/libraries.lisp#L54