From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp12.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id MJUKNUZvgWJBmAAAbAwnHQ (envelope-from ) for ; Sun, 15 May 2022 23:23:18 +0200 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp12.migadu.com with LMTPS id eHTPNEZvgWJkGwEAauVa8A (envelope-from ) for ; Sun, 15 May 2022 23:23:18 +0200 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 654672B169 for ; Sun, 15 May 2022 23:23:18 +0200 (CEST) Received: from localhost ([::1]:42490 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nqLht-0006uD-Ji for larch@yhetil.org; Sun, 15 May 2022 17:23:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39528) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nqLhe-0006u2-SM for guix-patches@gnu.org; Sun, 15 May 2022 17:23:03 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:56991) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nqLhe-0001Gh-H5 for guix-patches@gnu.org; Sun, 15 May 2022 17:23:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nqLhe-0006er-AJ for guix-patches@gnu.org; Sun, 15 May 2022 17:23:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#49946] [PATCH v6 03/27] build-system: Add tree-sitter-build-system. Resent-From: Pierre Langlois Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Sun, 15 May 2022 21:23:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 49946 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 49946@debbugs.gnu.org Cc: Pierre Langlois Received: via spool by 49946-submit@debbugs.gnu.org id=B49946.165264977525566 (code B ref 49946); Sun, 15 May 2022 21:23:02 +0000 Received: (at 49946) by debbugs.gnu.org; 15 May 2022 21:22:55 +0000 Received: from localhost ([127.0.0.1]:50888 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nqLhT-0006eE-3e for submit@debbugs.gnu.org; Sun, 15 May 2022 17:22:55 -0400 Received: from mout.gmx.net ([212.227.17.22]:54489) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nqLhR-0006e0-1Y for 49946@debbugs.gnu.org; Sun, 15 May 2022 17:22:50 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1652649762; bh=/Gkw49QmnEmPsRAmo0qP32TKLtY/FYFJM5hQSILTTBQ=; h=X-UI-Sender-Class:References:From:To:Cc:Subject:Date:In-reply-to; b=MfXch8nQknfKW1q/36DAHttf0w+vpisYU+vL0p2pUynoiNhTkHk2K+qNNXBjyMivC ZWad3abfGI0REJEpV7BBoVybcNpFfNL6jeypPe/6CUk9pyT+zN6G3FwFf8uzjnP0Ja m9QTMRrIdQ1G2H6+nBFGBkCpMq8P5c3MoFYOMnMU= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from labiere ([82.69.64.142]) by mail.gmx.net (mrgmx105 [212.227.17.174]) with ESMTPSA (Nemesis) id 1MKbkC-1oBANC0j6a-00KwIV; Sun, 15 May 2022 23:22:42 +0200 References: <87mtfi63ut.fsf@gmx.com> <20220515183834.18771-1-pierre.langlois@gmx.com> <20220515183834.18771-3-pierre.langlois@gmx.com> User-agent: mu4e 1.6.10; emacs 28.1 From: Pierre Langlois Date: Sun, 15 May 2022 22:19:59 +0100 In-reply-to: <20220515183834.18771-3-pierre.langlois@gmx.com> Message-ID: <8735hav6ch.fsf@gmx.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Provags-ID: V03:K1:Ya6eGpNHeTdZXYk8b6KTwI1b7LSTXD8+lCA8xeqfKnSWTGsfpkc lKMgbL1+iqFFm9egmzK4seAGheEuy8oTJbw8vlsnJRFTNPbtEEBbagurGMBY/KyD5/Ee6SJ /96bvqv1uywnZ7amIpbSTStXwaHL4nqUqq7md/ZpYwna6ncjDZZ0NMHbMfZRObYRdkukEdO 2TvRskrfZeHvVDwPwCl6g== X-UI-Out-Filterresults: notjunk:1;V03:K0:upvYuFje34s=:aqGrRVM/pzBllPHoYgiWQu cJFNFGPbaW3cU95zoNrfYEdARguPiSRXuXvmrx/fGuNZAYeaBnXr2zkGo72EN5ahcX7Ffb1Vd 2LC++mCjWj5vMGZ4W7QIIM+2+Rcen/pSXKgp9/0bJQH6oHTMW75vLgeVOxmh7uYQila1q+xlZ /Ud15zjziPsdcv4PRPZzbrh7yk1Cy+eTlRs739YoqizlCtc4URMBbFKz7aJMZRINQrDig6K2m WSsp1tnUBiZhaqpAwJb3RyKgZZz+v30sVxbE1fPOw/UNnjo6saqwFQmONA2UHo42tIe5wrm+a dexqHJHX7TVSvoChnt4bgKUMB0Mrxf33GJ4A1PO/DGsvAPYR7AYalSXzLUsl2xSwLxWkXsbYf tJ0HvutfUV4xUd9PExtqzW3aFISD/Ut2eRIr1WQroY0Ec8q+hZ4bcf5CxZAl39EbcU/fhp/x0 nZUXcq/k09r9IsR9/7BrYy9tM8HYYZMAYmbOKJAhvuZR2IWh7mCIXh9/zeGBbwm5JPMNfsL3y 4iJW7WHph/zIIgtcCKtgv8ml2rDNMH0YU1k1j2kWv6K4DR0PhVzVAt0ZCP49XlP9kedrzxC4D hSZ3UOwhme78gmIjS436pypf0iiOxybOdHN1HiKMnHN+MZ0zZ6m///2xZFOcHQ8FrLqC2PO66 i5GY3WpE4C8kwcCNcTMtxcpRgqGCl9PjlCzcBqoKI0E7NjBozr4U6WWMD+oLz/Gi++umHSUdj EM/R68cohZm7hbmlO7Ban4LulT0lulz45mPvyl+TqhrejjLuCRy4bhrixYKkDNtOzLVpbLc6U 3EtYCnaB84C7elloT9zvBUpiG8JidSza/fXaJCHA8n1BZV0+0nsRGuppe6s+YuVxdvTWSr8f2 bAfd54M4JYwXzpCHw/kTtlJ2S/X9kEQyFpBPmCD8rfpX3s/UTn/unJv+bOEfwI/6RDPiR+azH 8YWlkvPKIJYfQTUx3DPoRLqDjm2Tkxl+nPRkRv9AZhfk3AZVu2mQd8He9eHscLxGjl6AfrU8E 6tuwDrosLkW+y3d60Mnm+jz1eiTZRTn8kHaSg3GTMC3ulf2Og6fcQNcGNb17Z73Swc2i1QrZQ QofENmIpK27WPs= X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: "Guix-patches" X-Migadu-Flow: FLOW_IN X-Migadu-To: larch@yhetil.org X-Migadu-Country: US ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1652649798; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:resent-cc:resent-from:resent-sender: resent-message-id:in-reply-to:in-reply-to:references:references: list-id:list-help:list-unsubscribe:list-subscribe:list-post: dkim-signature; bh=1ptkJ2f4CffJEJF8ClkRXTF4RLuhpBwBJPmFvVeOfC4=; b=RvSiIxlmPgVUd07YnEKqsEOO+aKwIvBBJdypw7d7SRzHqEWy+RGbeAppXcAlXepdYU/FbB Lfm4XeyKobWwqxP/HxcWUlhqIjmNby5oNbKHeayEOJjuQkLV2TAZUVmmOJXFNKnyxXEQGi MLcJuqUtveBwyL5BJz/pASfdZPIwBdAGHYUGTw2wEBJMSfd0hQ6fUOZkbwsNtUspvwbqFM dUi+prdNlGoCsrjkOUWofcz6/w9taHi0bIcAJSQOAzntIc3qiL3n6MdJi6eg6dgX9xeEpJ F3iE/9EibExbUVCnKKVHdJVmJZOMVGSWdbh+IdbWg3/WMoNuZPaMN3oo8TG20g== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1652649798; a=rsa-sha256; cv=none; b=iJ7unicllmdOoMkQ7Fr9WjVV76aou6B07xgtjlLsWZxl9CkjL5tlGmDRV6lk9lB4bVTjbH bqmyyKXVc8imt/fEcf5Oq2rFOx2Mq9qKVhpaQUqbdJsFfau569zJXnOzuueKhvHR00pAre 8lqkMJoZ929Jsl7n/6P7MRz33ocvNXCEuhrNFn3uBCstj+yYZPiA3DhiF9JKM4mfMe3Sd7 vaDi8SeDGeS96CKn64x99JTI5rbBM/LE2iPX84e+Kjfy0r549vSrMW5bLHoytxCbLaF+dm j2B0U/CpTcGmwcznPHZGMUTx1fbWO1okKS8cUVfx2wu0l+3d4JwYbozHPD8+Mg== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmx.net header.s=badeba3b8450 header.b=MfXch8nQ; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmx.com (policy=none); spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-Migadu-Spam-Score: 3.07 Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmx.net header.s=badeba3b8450 header.b=MfXch8nQ; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmx.com (policy=none); spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-Migadu-Queue-Id: 654672B169 X-Spam-Score: 3.07 X-Migadu-Scanner: scn0.migadu.com X-TUID: gQNEP6sUuqch --=-=-= Content-Type: multipart/signed; boundary="==-=-="; micalg=pgp-sha512; protocol="application/pgp-signature" --==-=-= Content-Type: text/plain I'm not sure why, but when I look at either https://mail.gnu.org/archive/html/guix-patches/ or https://issues.guix.gnu.org/49946 this patch always seems to be missing. I wonder why, I used `git send-email' and didn't see any errors. I'm sending it as an attachment in case people are not receiving it. Thanks, Pierre --==-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQFMBAEBCgA2FiEEctU9gYy29KFyWDdMqPyeRH9PfVQFAmKBbx4YHHBpZXJyZS5s YW5nbG9pc0BnbXguY29tAAoJEKj8nkR/T31UpD0H/R2UmjNB728+xRjunXOXfXuj 8c5dnvbRYALM0crpZIlvn2I8P1E/gtdeTYnnJ8rLfC3cAqQDnNhSFM4YtjiepXz4 QOvwhcsM+wsZboR7v4/aYjPK59O2h65iJFtD0YnWu7NPGWRIuhXVRCcXsNfkNMJQ 9MQADyFGYqhP/z7xASDVdfKuDyTW+k5iqlhhVxn8s8NrwU9D87C4SEfgpNOjf3/E rx6mW0W5texRqXGu9FPFNTII6AkEOFRSKaumgNS99HqHO4KhJEi0ahY3Po6R+3L+ LS3di6Cz7tImSkN4f3WpbbsoHtYIDDMnYz3q58lG17XaGEkipumWDrShWjNm8EE= =nOaF -----END PGP SIGNATURE----- --==-=-=-- --=-=-= Content-Type: text/x-patch; charset=utf-8 Content-Disposition: attachment; filename=0003-build-system-Add-tree-sitter-build-system.patch Content-Transfer-Encoding: quoted-printable >From f16fb42db676303292de5cd9063d0f21f0baceeb Mon Sep 17 00:00:00 2001 From: Pierre Langlois Date: Tue, 29 Mar 2022 20:13:34 +0100 Subject: [PATCH v6 03/27] build-system: Add tree-sitter-build-system. * guix/build-system/tree-sitter.scm: New module. * guix/build/tree-sitter-build-system.scm: Likewise. * Makefile.am (MODULES): Add them. * doc/guix.texi: Document it. --- Makefile.am | 2 + doc/guix.texi | 21 ++- guix/build-system/tree-sitter.scm | 190 ++++++++++++++++++++++++ guix/build/tree-sitter-build-system.scm | 153 +++++++++++++++++++ 4 files changed, 365 insertions(+), 1 deletion(-) create mode 100644 guix/build-system/tree-sitter.scm create mode 100644 guix/build/tree-sitter-build-system.scm diff --git a/Makefile.am b/Makefile.am index 85a22be99c..de5cdbd328 100644 --- a/Makefile.am +++ b/Makefile.am @@ -168,6 +168,7 @@ MODULES =3D \ guix/build-system/ruby.scm \ guix/build-system/scons.scm \ guix/build-system/texlive.scm \ + guix/build-system/tree-sitter.scm \ guix/build-system/trivial.scm \ guix/ftp-client.scm \ guix/http-client.scm \ @@ -221,6 +222,7 @@ MODULES =3D \ guix/build/ruby-build-system.scm \ guix/build/scons-build-system.scm \ guix/build/texlive-build-system.scm \ + guix/build/tree-sitter-build-system.scm \ guix/build/waf-build-system.scm \ guix/build/haskell-build-system.scm \ guix/build/julia-build-system.scm \ diff --git a/doc/guix.texi b/doc/guix.texi index e1fb50a634..1783d74551 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -79,7 +79,7 @@ Copyright @copyright{} 2020 Jack Hill@* Copyright @copyright{} 2020 Naga Malleswari@* Copyright @copyright{} 2020, 2021 Brice Waegeneire@* Copyright @copyright{} 2020 R Veera Kumar@* -Copyright @copyright{} 2020, 2021 Pierre Langlois@* +Copyright @copyright{} 2020, 2021, 2022 Pierre Langlois@* Copyright @copyright{} 2020 pinoaffe@* Copyright @copyright{} 2020 Andr=C3=A9 Batista@* Copyright @copyright{} 2020, 2021 Alexandru-Sergiu Marton@* @@ -9281,6 +9281,25 @@ be specified with the @code{#:node} parameter which = defaults to @code{node}. @end defvr =20 +@defvr {Scheme Variable} tree-sitter-build-system + +This variable is exported by @code{(guix build-system tree-sitter)}. It +implements procedures to compile grammars for the +@url{https://tree-sitter.github.io/tree-sitter/, Tree-sitter} parsing +library. It essentially runs @code{tree-sitter generate} to translate +@code{grammar.js} grammars to JSON and then to C. Which it then +compiles to native code. + +Tree-sitter packages may support multiple grammars, so this build system +supports a @code{#:grammar-directories} keyword to specify a list of +locations where a @code{grammar.js} file may be found. + +Grammars sometimes depend on each other, such as C++ depending on C and +TypeScript depending on JavaScript. You may use inputs to declare such +dependencies. + +@end defvr + Lastly, for packages that do not need anything as sophisticated, a ``trivial'' build system is provided. It is trivial in the sense that it provides basically no support: it does not pull any implicit inputs, diff --git a/guix/build-system/tree-sitter.scm b/guix/build-system/tree-sit= ter.scm new file mode 100644 index 0000000000..aeb96e3ef5 --- /dev/null +++ b/guix/build-system/tree-sitter.scm @@ -0,0 +1,190 @@ +;;; GNU Guix --- Functional package management for GNU +;;; Copyright =C2=A9 2022 Pierre Langlois +;;; +;;; This file is part of GNU Guix. +;;; +;;; GNU Guix is free software; you can redistribute it and/or modify it +;;; under the terms of the GNU General Public License as published by +;;; the Free Software Foundation; either version 3 of the License, or (at +;;; your option) any later version. +;;; +;;; GNU Guix is distributed in the hope that it will be useful, but +;;; WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with GNU Guix. If not, see . + +(define-module (guix build-system tree-sitter) + #:use-module (guix store) + #:use-module (guix utils) + #:use-module (guix packages) + #:use-module (guix gexp) + #:use-module (guix monads) + #:use-module (guix search-paths) + #:use-module (guix build-system) + #:use-module (guix build-system gnu) + #:use-module (guix build-system node) + #:use-module (ice-9 match) + #:export (%tree-sitter-build-system-modules + tree-sitter-build + tree-sitter-build-system)) + +(define %tree-sitter-build-system-modules + ;; Build-side modules imported by default. + `((guix build tree-sitter-build-system) + ,@%node-build-system-modules)) + +(define* (lower name + #:key source inputs native-inputs outputs system target + #:allow-other-keys + #:rest arguments) + "Return a bag for NAME from the given arguments." + (define private-keywords + `(#:inputs #:native-inputs #:outputs ,@(if target + '() + '(#:target)))) + (define node + (module-ref (resolve-interface '(gnu packages node)) + 'node-lts)) + (define tree-sitter + (module-ref (resolve-interface '(gnu packages tree-sitter)) + 'tree-sitter)) + (define tree-sitter-cli + (module-ref (resolve-interface '(gnu packages tree-sitter)) + 'tree-sitter-cli)) + ;; Grammars depend on each other via JS modules, which we package into a + ;; dedicated js output. + (define grammar-inputs + (map (match-lambda + ((name package) + `(,name ,package "js"))) + inputs)) + (bag + (name name) + (system system) (target target) + (build-inputs `(,@(if source + `(("source" ,source)) + '()) + ("node" ,node) + ("tree-sitter-cli" ,tree-sitter-cli) + ,@native-inputs + ,@(if target '() grammar-inputs) + ;; Keep the standard inputs of 'gnu-build-system'. + ,@(if target + (standard-cross-packages target 'host) + '()) + ,@(standard-packages))) + (host-inputs `(("tree-sitter" ,tree-sitter) + ,@(if target grammar-inputs '()))) + ;; Keep the standard inputs of 'gnu-buid-system'. + (target-inputs (if target + (standard-cross-packages target 'target) + '())) + ;; XXX: this is a hack to get around issue #41569. + (outputs (match outputs + (("out") (cons "js" outputs)) + (_ outputs))) + (build (if target tree-sitter-cross-build tree-sitter-build)) + (arguments (strip-keyword-arguments private-keywords arguments)))) + +(define* (tree-sitter-build name inputs + #:key + source + (phases '%standard-phases) + (grammar-directories '(".")) + (tests? #t) + (outputs '("out" "js")) + (search-paths '()) + (system (%current-system)) + (guile #f) + (imported-modules %tree-sitter-build-system-mo= dules) + (modules '((guix build utils) + (guix build tree-sitter-build-syste= m)))) + (define builder + (with-imported-modules imported-modules + #~(begin + (use-modules #$@(sexp->gexp modules)) + (tree-sitter-build #:name #$name + #:source #+source + #:system #$system + #:phases #$phases + #:tests? #$tests? + #:grammar-directories '#$grammar-directories + #:outputs #$(outputs->gexp outputs) + #:search-paths '#$(sexp->gexp + (map search-path-specifica= tion->sexp + search-paths)) + #:inputs #$(input-tuples->gexp inputs))))) + + (mlet %store-monad ((guile (package->derivation (or guile (default-guile= )) + system #:graft? #f))) + (gexp->derivation name builder + #:system system + #:guile-for-build guile))) + +(define* (tree-sitter-cross-build name + #:key + target + build-inputs target-inputs host-inputs + guile source + (phases '%standard-phases) + (grammar-directories '(".")) + (tests? #t) + (outputs '("out" "js")) + (search-paths '()) + (native-search-paths '()) + (system (%current-system)) + (build (nix-system->gnu-triplet system)) + (imported-modules %tree-sitter-build-sys= tem-modules) + (modules '((guix build utils) + (guix build tree-sitter-build= -system)))) + (define builder + (with-imported-modules imported-modules + #~(begin + (use-modules #$@(sexp->gexp modules)) + + (define %build-host-inputs + #+(input-tuples->gexp build-inputs)) + + (define %build-target-inputs + (append #$(input-tuples->gexp host-inputs) + #+(input-tuples->gexp target-inputs))) + + (define %build-inputs + (append %build-host-inputs %build-target-inputs)) + + (tree-sitter-build #:name #$name + #:source #+source + #:system #$system + #:build #$build + #:target #$target + #:phases #$phases + #:tests? #$tests? + #:grammar-directories '#$grammar-directories + #:outputs #$(outputs->gexp outputs) + #:inputs %build-target-inputs + #:native-inputs %build-host-inputs + #:search-paths '#$(sexp->gexp + (map search-path-specifica= tion->sexp + search-paths)) + #:native-search-paths '#$(sexp->gexp + (map + search-path-specif= ication->sexp + native-search-path= s)))))) + + (mlet %store-monad ((guile (package->derivation (or guile (default-guile= )) + system #:graft? #f))) + (gexp->derivation name builder + #:system system + #:target target + #:guile-for-build guile))) + +(define tree-sitter-build-system + (build-system + (name 'tree-sitter) + (description "The Tree-sitter grammar build system") + (lower lower))) + +;;; tree-sitter.scm ends here diff --git a/guix/build/tree-sitter-build-system.scm b/guix/build/tree-sitt= er-build-system.scm new file mode 100644 index 0000000000..ad59f56dfe --- /dev/null +++ b/guix/build/tree-sitter-build-system.scm @@ -0,0 +1,153 @@ +;;; GNU Guix --- Functional package management for GNU +;;; Copyright =C2=A9 2022 Pierre Langlois +;;; +;;; This file is part of GNU Guix. +;;; +;;; GNU Guix is free software; you can redistribute it and/or modify it +;;; under the terms of the GNU General Public License as published by +;;; the Free Software Foundation; either version 3 of the License, or (at +;;; your option) any later version. +;;; +;;; GNU Guix is distributed in the hope that it will be useful, but +;;; WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with GNU Guix. If not, see . + +(define-module (guix build tree-sitter-build-system) + #:use-module ((guix build node-build-system) #:prefix node:) + #:use-module (guix build json) + #:use-module (guix build utils) + #:use-module (ice-9 match) + #:use-module (ice-9 regex) + #:use-module (srfi srfi-1) + #:export (%standard-phases + tree-sitter-build)) + +;; Commentary: +;; +;; Build procedures for tree-sitter grammar packages. This is the +;; builder-side code, which builds on top fo the node build-system. +;; +;; Tree-sitter grammars are written in JavaScript and compiled to a native +;; shared object. The `tree-sitter generate' command invokes `node' in or= der +;; to evaluate the grammar.js into a grammar.json file, which is then +;; translated into C code. We then compile the C code ourselve. Packages +;; also sometimes add extra manually written C/C++ code. +;; +;; In order to support grammars depending on each other, such as C and C++, +;; JavaScript and TypeScript, this build-system installs the source of the +;; node module in a dedicated "js" output. +;; +;; Code: + +(define* (patch-dependencies #:key inputs #:allow-other-keys) + "Rewrite dependencies in 'package.json'. We remove all runtime dependen= cies +and replace development dependencies with tree-sitter grammar node modules= ." + + (define (rewrite package.json) + (map (match-lambda + (("dependencies" @ . _) + '("dependencies" @)) + (("devDependencies" @ . _) + `("devDependencies" @ + ,@(filter-map (match-lambda + ((key . directory) + (let ((node-module + (string-append directory + "/lib/node_modules/" + key))) + (and (directory-exists? node-module) + `(,key . ,node-module))))) + (alist-delete "node" inputs)))) + (other other)) + package.json)) + + (node:with-atomic-json-file-replacement "package.json" + (match-lambda + (('@ . package.json) + (cons '@ (rewrite package.json)))))) + +;; FIXME: The node build-system's configure phase does not support +;; cross-compiling so we re-define it. +(define* (configure #:key native-inputs inputs #:allow-other-keys) + (invoke (search-input-file (or native-inputs inputs) "/bin/npm") + "--offline" "--ignore-scripts" "install")) + +(define* (build #:key grammar-directories #:allow-other-keys) + (for-each (lambda (dir) + (with-directory-excursion dir + ;; Avoid generating binding code for other languages, we do + ;; not support this use-case yet and it relies on running + ;; `node-gyp' to build native addons. + (invoke "tree-sitter" "generate" "--no-bindings"))) + grammar-directories)) + +(define* (check #:key grammar-directories tests? #:allow-other-keys) + (when tests? + (for-each (lambda (dir) + (with-directory-excursion dir + (invoke "tree-sitter" "test"))) + grammar-directories))) + +(define* (install #:key target grammar-directories outputs #:allow-other-k= eys) + (let ((lib (string-append (assoc-ref outputs "out") + "/lib/tree-sitter"))) + (mkdir-p lib) + (define (compile-language dir) + (with-directory-excursion dir + (let ((lang (assoc-ref (call-with-input-file "src/grammar.json" + read-json) + "name")) + (source-file (lambda (path) + (if (file-exists? path) + path + #f)))) + (apply invoke + `(,(if target + (string-append target "-g++") + "g++") + "-shared" + "-fPIC" + "-fno-exceptions" + "-O2" + "-g" + "-o" ,(string-append lib "/" lang ".so") + ;; An additional `scanner.{c,cc}' file is sometimes + ;; provided. + ,@(cond + ((source-file "src/scanner.c") + =3D> (lambda (file) (list "-xc" "-std=3Dc99" file))) + ((source-file "src/scanner.cc") + =3D> (lambda (file) (list file))) + (else '())) + "-xc" "src/parser.c"))))) + (for-each compile-language grammar-directories))) + +(define* (install-js #:key native-inputs inputs outputs #:allow-other-keys) + (invoke (search-input-file (or native-inputs inputs) "/bin/npm") + "--prefix" (assoc-ref outputs "js") + "--global" + "--offline" + "--loglevel" "info" + "--production" + ;; Skip scripts to prevent building bindings via GYP. + "--ignore-scripts" + "install" "../package.tgz")) + +(define %standard-phases + (modify-phases node:%standard-phases + (replace 'patch-dependencies patch-dependencies) + (replace 'configure configure) + (replace 'build build) + (replace 'check check) + (replace 'install install) + (add-after 'install 'install-js install-js))) + +(define* (tree-sitter-build #:key inputs (phases %standard-phases) + #:allow-other-keys #:rest args) + (apply node:node-build #:inputs inputs #:phases phases args)) + +;;; tree-sitter-build-system.scm ends here --=20 2.36.0 --=-=-=--