Subject: [bug#49946] [PATCH v2 12/33] gnu: Add tree-sitter-c.

From: Pierre Langlois <[email-masked]>
Date: Sun, 29 Aug 2021 11:45:47 +0100

* gnu/packages/tree-sitter.scm (tree-sitter-c): New variable.
---
 gnu/packages/tree-sitter.scm | 63 +++++++++++++++++++++++++++++++++++-
 1 file changed, 62 insertions(+), 1 deletion(-)

diff --git a/gnu/packages/tree-sitter.scm b/gnu/packages/tree-sitter.scm
index d8d4a305da..bc04a4a5a7 100644
--- a/gnu/packages/tree-sitter.scm
+++ b/gnu/packages/tree-sitter.scm
@@ -20,11 +20,13 @@
   #:use-module ((guix licenses) #:prefix license:)
   #:use-module (guix build-system cargo)
   #:use-module (guix build-system gnu)
+  #:use-module (guix build-system node)
   #:use-module (guix git-download)
   #:use-module (guix packages)
   #:use-module (guix utils)
   #:use-module (gnu packages crates-graphics)
-  #:use-module (gnu packages crates-io))
+  #:use-module (gnu packages crates-io)
+  #:use-module (gnu packages node-xyz))

 (define-public tree-sitter
   (package
@@ -130,3 +132,62 @@ can be embedded in any application.

 This package includes the @command{tree-sitter} command-line tool.")
     (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-cli" ,tree-sitter-cli)
+       ("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" ,(cc-for-target))))
+         (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)))
--
2.33.0