Skip to content

Commit

Permalink
Use .dylib extension for the dynamic module on macOS, instead of .so
Browse files Browse the repository at this point in the history
  • Loading branch information
ubolonton committed Jan 26, 2020
1 parent da4201f commit fdbcf67
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cask
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

(files "*.el"
"README.md"
"tree-sitter-dyn.dylib"
"tree-sitter-dyn.so"
"tree-sitter-dyn.dll"
"Cargo.toml"
Expand Down
3 changes: 1 addition & 2 deletions bin/env.bash
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export TARGET=${TARGET:-debug}
export MODULE_DIR="$PROJECT_ROOT/target/$TARGET"
export MODULE_ORIGINAL=${MODULE_ORIGINAL:-libtree_sitter_dyn.$ext}
export MODULE_NAME=${MODULE_NAME:-tree-sitter-dyn}
export MODULE_RENAMED=${MODULE_NAME}.so
export MODULE_RENAMED=${MODULE_NAME}.$ext
export TEST_FILE=tree-sitter-tests.el

export EMACS=${EMACS:-emacs}
31 changes: 31 additions & 0 deletions tree-sitter--mac-load.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(require 'seq)

;; XXX: Using `require' after setting`module-file-suffix' to `.dylib' results in "Cannot open load
;; file: No such file or directory, tree-sitter-dyn".
;;
;; XXX: Using `load' results in an error message with garbled text: "Symbol’s value as variable is
;; void: Ïúíþ".
;;
;; Therefore, we use `module-load' directly.
(defun ts--try-load-dyn (name)
(or (featurep 'tree-sitter-dyn)
(condition-case _
(module-load name)
(module-open-failed nil))))

(unwind-protect
(let ((name "tree-sitter-dyn.dylib"))
;; Try directory containing `load-file-name'. Typical case.
(when load-file-name
(ts--try-load-dyn (concat (file-name-directory load-file-name)
name)))
;; Try working directory (e.g. when invoked by `cask').
(ts--try-load-dyn name)
;; Fall back to `load-path'.
(seq-find (lambda (dir)
(let ((full-name (concat (file-name-as-directory
(expand-file-name dir))
name)))
(ts--try-load-dyn full-name)))
load-path))
(fmakunbound 'ts--try-load-dyn))
9 changes: 9 additions & 0 deletions tree-sitter-core.el
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@
(unless (functionp 'module-load)
(error "Dynamic module feature not available, please compile Emacs --with-modules option turned on"))

;; Load the dynamic module at compile time as well, to satisfy the byte compiler.
(eval-and-compile
;; XXX: We want a universal package containing binaries for all platforms, so we use a unique
;; extension for each. On macOS, we use`.dylib', which is more sensible than `.so' anyway.
(when (eq system-type 'darwin)
(load "tree-sitter--mac-load.el")))

;; We still call this on macOS, as it's useful for other things as well.
(require 'tree-sitter-dyn)

(require 'simple)
(require 'map)
(require 'pp)
Expand Down

0 comments on commit fdbcf67

Please sign in to comment.