diff mbox series

[bug#49946,11/31] gnu: Add tree-sitter-c.

Message ID 20210808233354.6745-11-pierre.langlois@gmx.com
State New
Headers show
Series Tree-sitter, node-gyp addon support and emacs-tree-sitter | expand

Commit Message

Pierre Langlois Aug. 8, 2021, 11:33 p.m. UTC
* gnu/packages/tree-sitter.scm (tree-sitter-c): New variable.
---
 gnu/packages/tree-sitter.scm | 63 +++++++++++++++++++++++++++++++++++-
 1 file changed, 62 insertions(+), 1 deletion(-)

--
2.32.0

Comments

M Aug. 10, 2021, 6:30 p.m. UTC | #1
Pierre Langlois schreef op ma 09-08-2021 om 00:33 [+0100]:
> +             (setenv "CC" "gcc")))

Use (setenv "CC" ,(cc-for-target)) instead,
such that the cross-compiler TARGET-gcc is used
when cross-compiling.

reetings,
Maxime.
diff mbox series

Patch

diff --git a/gnu/packages/tree-sitter.scm b/gnu/packages/tree-sitter.scm
index 2c749dc0a8..c855cba347 100644
--- a/gnu/packages/tree-sitter.scm
+++ b/gnu/packages/tree-sitter.scm
@@ -19,10 +19,12 @@ 
 (define-module (gnu packages tree-sitter)
   #:use-module ((guix licenses) #:prefix license:)
   #:use-module (guix build-system cargo)
+  #:use-module (guix build-system node)
   #:use-module (guix git-download)
   #:use-module (guix packages)
   #:use-module (gnu packages crates-io)
-  #:use-module (gnu packages crates-graphics))
+  #:use-module (gnu packages crates-graphics)
+  #:use-module (gnu packages node-xyz))

 (define-public tree-sitter
   (package
@@ -96,3 +98,62 @@  can be embedded in any application.
 This package includes the @command{tree-sitter} tool as well as the runtime
 library.")
     (license license:expat)))
+
+(define-public tree-sitter-c
+  (package
+    (name "tree-sitter-c")
+    (version "0.19.0")
+    (source
+      (origin
+        (method git-fetch)
+        (uri (git-reference
+              (url "https://github.com/tree-sitter/tree-sitter-c")
+              (commit (string-append "v" version))))
+        (file-name (git-file-name name version))
+        (sha256
+         (base32
+          "1diys8yigvhm4ppbmp3a473yxjg2d5lk11y0ay7qprcz7233lakv"))))
+    (build-system node-build-system)
+    (native-inputs
+     `(("tree-sitter" ,tree-sitter)
+       ("node-nan" ,node-nan)))
+    (arguments
+     `(#:phases
+       (modify-phases %standard-phases
+         ;; tree-sitter-cli is listed as a Node.js dependency, however the
+         ;; node tree-sitter-cli package is just a wrapper which downloads a
+         ;; tree-sitter binary, see
+         ;; https://github.com/tree-sitter/tree-sitter/tree/master/cli/npm
+         ;; Instead we remove it as a dependency so that we can use our own
+         ;; tree-sitter package.
+         (add-after 'unpack 'fix-configure
+           (lambda _
+             (with-atomic-file-replacement "package.json"
+               (lambda (in out)
+                 (use-modules ((guix build json)))
+                 (let ((package-meta (read-json in)))
+                   (assoc-remove! (assoc-ref package-meta "devDependencies")
+                                  "tree-sitter-cli")
+                   (write-json package-meta out))))))
+         (add-before 'build 'set-cc
+           (lambda _
+             (setenv "CC" "gcc")))
+         (add-before 'build 'make-files-writable
+           (lambda _
+             (for-each make-file-writable (find-files "src" ".*"))))
+         (add-after 'install 'install-native-lib
+           (lambda* (#:key outputs #:allow-other-keys)
+             (let ((lib (string-append (assoc-ref outputs "out")
+                                       "/lib/tree-sitter")))
+               ;; Invoking `tree-sitter test' loads the grammar which
+               ;; compiles it to a .so binary that we install.
+               (invoke "tree-sitter" "test")
+               (mkdir-p lib)
+               (copy-recursively (string-append (getenv "HOME")
+                                                "/.tree-sitter/bin")
+                                 lib)))))))
+    (home-page "https://github.com/tree-sitter/tree-sitter-c")
+    (synopsis "Tree-sitter C grammar")
+    (description
+      "This package provides a C grammar for the Tree-sitter library.")
+    (license license:expat)))