[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
master e2f79171529 02/10: Refactor treesit--install-language-grammar-1
From: |
Yuan Fu |
Subject: |
master e2f79171529 02/10: Refactor treesit--install-language-grammar-1 |
Date: |
Mon, 30 Dec 2024 03:24:09 -0500 (EST) |
branch: master
commit e2f791715299d97f401a38c75fa11bb51fdb8292
Author: Yuan Fu <casouri@gmail.com>
Commit: Yuan Fu <casouri@gmail.com>
Refactor treesit--install-language-grammar-1
Separate treesit--install-language-grammar-1 into two functions,
one clones the repository, the other builds and installs the
grammar.
* lisp/treesit.el (treesit--install-language-grammar-1):
Refactor out treesit--build-grammar.
(treesit--build-grammar): New function.
(treesit--language-git-revision): Now takes REPO-DIR as an
argument.
---
lisp/treesit.el | 154 +++++++++++++++++++++++++++++++-------------------------
1 file changed, 85 insertions(+), 69 deletions(-)
diff --git a/lisp/treesit.el b/lisp/treesit.el
index 090197d86e3..73e7e5446d4 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -4229,17 +4229,18 @@ Return the git revision of the installed grammar, but
it only works when
err)))))
version))
-(defun treesit--language-git-revision ()
- "Return the Git revision of current directory.
+(defun treesit--language-git-revision (repo-dir)
+ "Return the Git revision of the repo in REPO-DIR.
Return the output of \"git describe\". If anything goes wrong, return
nil."
(with-temp-buffer
(cond
- ((eq 0 (call-process "git" nil t nil "describe" "--tags"))
+ ((eq 0 (call-process "git" nil t nil "-C" repo-dir "describe" "--tags"))
(string-trim (buffer-string)))
((eq 0 (progn (erase-buffer)
- (call-process "git" nil t nil "rev-parse" "HEAD")))
+ (call-process "git" nil t nil
+ "-C" repo-dir "rev-parse" "HEAD")))
(string-trim (buffer-string)))
(t nil))))
@@ -4284,7 +4285,7 @@ clone if `treesit--install-language-grammar-blobless' is
t."
(defun treesit--install-language-grammar-1
(out-dir lang url &optional revision source-dir cc c++ commit)
- "Install and compile a tree-sitter language grammar library.
+ "Compile and install a tree-sitter language grammar library.
OUT-DIR is the directory to put the compiled library file. If it
is nil, the \"tree-sitter\" directory under user's Emacs
@@ -4292,31 +4293,19 @@ configuration directory is used (and automatically
created if it
does not exist).
For LANG, URL, REVISION, SOURCE-DIR, GRAMMAR-DIR, CC, C++, COMMIT, see
-`treesit-language-source-alist'. If anything goes wrong, this function
-signals an error.
+`treesit-language-source-alist'.
Return the git revision of the installed grammar. The revision is
generated by \"git describe\". It only works when
-`treesit--install-language-grammar-full-clone' is t."
- (let* ((lang (symbol-name lang))
+`treesit--install-language-grammar-full-clone' is t.
+
+If anything goes wrong, this function signals an `treesit-error'."
+ (let* ((default-directory (make-temp-file "treesit-workdir" t))
(maybe-repo-dir (expand-file-name url))
(url-is-dir (file-accessible-directory-p maybe-repo-dir))
- (default-directory (make-temp-file "treesit-workdir" t))
(workdir (if url-is-dir
maybe-repo-dir
(expand-file-name "repo")))
- (source-dir (expand-file-name (or source-dir "src") workdir))
- (cc (or cc (seq-find #'executable-find '("cc" "gcc" "c99"))
- ;; If no C compiler found, just use cc and let
- ;; `call-process' signal the error.
- "cc"))
- (c++ (or c++ (seq-find #'executable-find '("c++" "g++"))
- "c++"))
- (soext (or (car dynamic-library-suffixes)
- (signal 'treesit-error '("Emacs cannot figure out the file
extension for dynamic libraries for this system, because
`dynamic-library-suffixes' is nil"))))
- (out-dir (or (and out-dir (expand-file-name out-dir))
- (locate-user-emacs-file "tree-sitter")))
- (lib-name (concat "libtree-sitter-" lang soext))
version)
(unwind-protect
(with-temp-buffer
@@ -4326,59 +4315,86 @@ generated by \"git describe\". It only works when
(treesit--git-clone-repo url revision workdir))
(when commit
(treesit--git-checkout-branch workdir commit))
- ;; We need to go into the source directory because some
- ;; header files use relative path (#include "../xxx").
- ;; cd "${sourcedir}"
- (setq default-directory source-dir)
- (setq version (treesit--language-git-revision))
- (message "Compiling library")
- ;; cc -fPIC -c -I. parser.c
- (treesit--call-process-signal
- cc nil t nil "-fPIC" "-c" "-I." "parser.c")
- ;; cc -fPIC -c -I. scanner.c
- (when (file-exists-p "scanner.c")
- (treesit--call-process-signal
- cc nil t nil "-fPIC" "-c" "-I." "scanner.c"))
- ;; c++ -fPIC -I. -c scanner.cc
- (when (file-exists-p "scanner.cc")
- (treesit--call-process-signal
- c++ nil t nil "-fPIC" "-c" "-I." "scanner.cc"))
- ;; cc/c++ -fPIC -shared *.o -o "libtree-sitter-${lang}.${soext}"
- (apply #'treesit--call-process-signal
- (if (file-exists-p "scanner.cc") c++ cc)
- nil t nil
- (if (eq system-type 'cygwin)
- `("-shared" "-Wl,-dynamicbase"
- ,@(directory-files
- default-directory nil
- (rx bos (+ anychar) ".o" eos))
- "-o" ,lib-name)
- `("-fPIC" "-shared"
- ,@(directory-files
- default-directory nil
- (rx bos (+ anychar) ".o" eos))
- "-o" ,lib-name)))
- ;; Copy out.
- (unless (file-exists-p out-dir)
- (make-directory out-dir t))
- (let* ((library-fname (expand-file-name lib-name out-dir))
- (old-fname (concat library-fname ".old")))
- ;; Rename the existing shared library, if any, then
- ;; install the new one, and try deleting the old one.
- ;; This is for Windows systems, where we cannot simply
- ;; overwrite a DLL that is being used.
- (if (file-exists-p library-fname)
- (rename-file library-fname old-fname t))
- (copy-file lib-name (file-name-as-directory out-dir) t t)
- ;; Ignore errors, in case the old version is still used.
- (ignore-errors (delete-file old-fname)))
- (message "Library installed to %s/%s" out-dir lib-name))
+ (setq version (treesit--language-git-revision workdir))
+ (treesit--build-grammar workdir out-dir lang source-dir cc c++))
;; Remove workdir if it's not a repo owned by user and we
;; managed to create it in the first place.
(when (and (not url-is-dir) (file-exists-p workdir))
(delete-directory workdir t)))
version))
+(defun treesit--build-grammar (workdir out-dir lang source-dir cc c++)
+ "Compile a tree-sitter language grammar library.
+
+WORKDIR is the cloned repo's directory. OUT-DIR is the directory to put
+the compiled library file. If it is nil, the \"tree-sitter\" directory
+under user's Emacs configuration directory is used (and automatically
+created if it does not exist).
+
+For LANG, SOURCE-DIR, CC, C++, see `treesit-language-source-alist'.
+
+If anything goes wrong, this function signals an `treesit-error'."
+ (let* ((lang (symbol-name lang))
+ (source-dir (expand-file-name (or source-dir "src") workdir))
+ (cc (or cc (seq-find #'executable-find '("cc" "gcc" "c99"))
+ ;; If no C compiler found, just use cc and let
+ ;; `call-process' signal the error.
+ "cc"))
+ (c++ (or c++ (seq-find #'executable-find '("c++" "g++"))
+ "c++"))
+ (soext (or (car dynamic-library-suffixes)
+ (signal 'treesit-error '("Emacs cannot figure out the file
extension for dynamic libraries for this system, because
`dynamic-library-suffixes' is nil"))))
+ (out-dir (or (and out-dir (expand-file-name out-dir))
+ (locate-user-emacs-file "tree-sitter")))
+ (lib-name (concat "libtree-sitter-" lang soext)))
+ (with-temp-buffer
+ ;; We need to go into the source directory because some
+ ;; header files use relative path (#include "../xxx").
+ ;; cd "${sourcedir}"
+ (setq default-directory source-dir)
+ (message "Compiling library")
+ ;; cc -fPIC -c -I. parser.c
+ (treesit--call-process-signal
+ cc nil t nil "-fPIC" "-c" "-I." "parser.c")
+ ;; cc -fPIC -c -I. scanner.c
+ (when (file-exists-p "scanner.c")
+ (treesit--call-process-signal
+ cc nil t nil "-fPIC" "-c" "-I." "scanner.c"))
+ ;; c++ -fPIC -I. -c scanner.cc
+ (when (file-exists-p "scanner.cc")
+ (treesit--call-process-signal
+ c++ nil t nil "-fPIC" "-c" "-I." "scanner.cc"))
+ ;; cc/c++ -fPIC -shared *.o -o "libtree-sitter-${lang}.${soext}"
+ (apply #'treesit--call-process-signal
+ (if (file-exists-p "scanner.cc") c++ cc)
+ nil t nil
+ (if (eq system-type 'cygwin)
+ `("-shared" "-Wl,-dynamicbase"
+ ,@(directory-files
+ default-directory nil
+ (rx bos (+ anychar) ".o" eos))
+ "-o" ,lib-name)
+ `("-fPIC" "-shared"
+ ,@(directory-files
+ default-directory nil
+ (rx bos (+ anychar) ".o" eos))
+ "-o" ,lib-name)))
+ ;; Copy out.
+ (unless (file-exists-p out-dir)
+ (make-directory out-dir t))
+ (let* ((library-fname (expand-file-name lib-name out-dir))
+ (old-fname (concat library-fname ".old")))
+ ;; Rename the existing shared library, if any, then
+ ;; install the new one, and try deleting the old one.
+ ;; This is for Windows systems, where we cannot simply
+ ;; overwrite a DLL that is being used.
+ (if (file-exists-p library-fname)
+ (rename-file library-fname old-fname t))
+ (copy-file lib-name (file-name-as-directory out-dir) t t)
+ ;; Ignore errors, in case the old version is still used.
+ (ignore-errors (delete-file old-fname)))
+ (message "Library installed to %s/%s" out-dir lib-name))))
+
;;; Shortdocs
(defun treesit--generate-shortdoc-examples ()
- master updated (dc653bf0636 -> ec3f9434c7d), Yuan Fu, 2024/12/30
- master 9e1e9fdff44 03/10: Refactor treesit-admin--verify-major-mode-queries, Yuan Fu, 2024/12/30
- master c9624c21117 05/10: Add treesit-admin--last-compatible-grammar-for-modes, Yuan Fu, 2024/12/30
- master d9cfe1fe92f 07/10: Add treesit-admin-generate-compatibility-report, Yuan Fu, 2024/12/30
- master a22730f4d78 01/10: Support COMMIT in treesit-language-source-alist, Yuan Fu, 2024/12/30
- master d5ad51f25fb 04/10: Add treesit-admin--find-latest-compatible-revision, Yuan Fu, 2024/12/30
- master e2f79171529 02/10: Refactor treesit--install-language-grammar-1,
Yuan Fu <=
- master 0b1986ba524 08/10: Generate compatibility report for multiple Emacs versions, Yuan Fu, 2024/12/30
- master 732a1108b0b 09/10: Add verified grammar version comment for tree-sitter modes, Yuan Fu, 2024/12/30
- master ec3f9434c7d 10/10: Make typescript-ts-mode work with latest grammar, Yuan Fu, 2024/12/30
- master 5ec170985f4 06/10: ; Move treesit-admin into tree-sitter directory, Yuan Fu, 2024/12/30