From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp12.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms8.migadu.com with LMTPS id KO9yEfKGUmXKggAAauVa8A:P1 (envelope-from ) for ; Mon, 13 Nov 2023 21:28:34 +0100 Received: from aspmx1.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp12.migadu.com with LMTPS id KO9yEfKGUmXKggAAauVa8A (envelope-from ) for ; Mon, 13 Nov 2023 21:28:34 +0100 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 B2791588BA for ; Mon, 13 Nov 2023 21:28:33 +0100 (CET) Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=deeplinks-com.20230601.gappssmtp.com header.s=20230601 header.b=mhQC8uHk; 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"; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1699907314; 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: content-transfer-encoding:content-transfer-encoding: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=E3yprZr7aNLp9SlO0js7RNw4h+PqkoNmILFFiQnFNSY=; b=AwXuUH0u5tOZ/sBOanPCw0DHlH8Rgy+fHN2ebsQQZI/vY5iNsOPkd7VBJ2snOVLoNsGEyj xCUmAR//l/Il9lV4Crs3DpZ0MX6h8/OirkN7h9hSkTHgtiZrjHlAFSolglGtXhe7VnoHuj 7ylEzG/B091JhpGEk0M+SDJQL1KEPCC3CUszCYs7u2xounn6529/AUzrtOJHA0VXVYR6ra XxF6mGw4nTfGlU4B3W8/xWPmeaur0gHJz2Hzkyx20i0bh3lIra2/A8U47xh/e3Cui76/5s BvGTb9x0dN+JXgeJ5j0mu/5FxdNiXKv0wE0ld/hUw5Q1RH0l+vJqem7RUevEtA== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=deeplinks-com.20230601.gappssmtp.com header.s=20230601 header.b=mhQC8uHk; 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"; dmarc=none ARC-Seal: i=1; s=key1; d=yhetil.org; t=1699907314; a=rsa-sha256; cv=none; b=Z/IxTo75OFMLMZW9a1Z8he4dxMKNbXiz427317/ew6VYAiHlbW34GLKs3viIIC9D8zEb0Y ca1UhtUYhfXdRifG6n/bboIln/TRoH5Fh1HRKp6Yo4rjm2ZLRAoj7qjxFcd2afbVZ59+CW nDhaXq9+Cy08lHvER9qhEBwgseA9jXwceZ+mvLpqlpYOFYT+3ne7McBgeq9xdtkNeYmqvr QVsdR9L9QG9mlOgOVcGEfymX48XxzxNMw6Ahjjwh7XD5nxrF2s3IgShHrpXO+LLBOnm2/U wzMjBpgK9LXaPBtR9MbYC6GcBzaKrjmR4gbVTyXD2ZnONB6q06mG9g9FDrd00A== Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r2dXi-0003mp-MC; Mon, 13 Nov 2023 15:28:22 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1r2dXh-0003l2-AG for guix-patches@gnu.org; Mon, 13 Nov 2023 15:28:21 -0500 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1r2dXh-0006EK-26 for guix-patches@gnu.org; Mon, 13 Nov 2023 15:28:21 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1r2dYM-0002Pb-ET for guix-patches@gnu.org; Mon, 13 Nov 2023 15:29:02 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#66801] ['PATCH v2' 01/14] build-system: Add mix-build-system. References: <68117eb2b3e0e6adcc7449d878e602c7b831ffee.1698524350.git.phfrohring@deeplinks.com> In-Reply-To: <68117eb2b3e0e6adcc7449d878e602c7b831ffee.1698524350.git.phfrohring@deeplinks.com> Resent-From: Pierre-Henry =?UTF-8?Q?Fr=C3=B6hring?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Mon, 13 Nov 2023 20:29:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 66801 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 66801@debbugs.gnu.org Cc: liliana.prikler@gmail.com, Pierre-Henry =?UTF-8?Q?Fr=C3=B6hring?= Received: via spool by 66801-submit@debbugs.gnu.org id=B66801.16999073259247 (code B ref 66801); Mon, 13 Nov 2023 20:29:02 +0000 Received: (at 66801) by debbugs.gnu.org; 13 Nov 2023 20:28:45 +0000 Received: from localhost ([127.0.0.1]:59398 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1r2dY3-0002P2-Pq for submit@debbugs.gnu.org; Mon, 13 Nov 2023 15:28:45 -0500 Received: from mail-wr1-x42b.google.com ([2a00:1450:4864:20::42b]:45080) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1r2dXs-0002Of-2n for 66801@debbugs.gnu.org; Mon, 13 Nov 2023 15:28:42 -0500 Received: by mail-wr1-x42b.google.com with SMTP id ffacd0b85a97d-32f7bd27c2aso3342362f8f.2 for <66801@debbugs.gnu.org>; Mon, 13 Nov 2023 12:27:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=deeplinks-com.20230601.gappssmtp.com; s=20230601; t=1699907263; x=1700512063; darn=debbugs.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=E3yprZr7aNLp9SlO0js7RNw4h+PqkoNmILFFiQnFNSY=; b=mhQC8uHks8VfbMbvcypm3jUH7TMWzaxH5RAWeqA14OYt27SWh2pRREN2f3OqjH95Ru qb+ToLL2HSDykQO5gE3aA72PAqW++fPZnC96zbldaFLxIFW0c+jPGk9rwfyeb04EkgDh FZ7tFg0UO2p6aLeEpkUlhEfadsUjN8FRy470SDy5mmUB389a9ORKllXeSCGsMbNqhCXo etglX/jIDct+43of5k2Ba08kc0cow/OlE3sMyPTt0HqJSxgscVcv693GSgUZCEMMZiRb WSxrERvmGAtRbfZLGUM8+aV2+n4lhC+hq47JLk/s05iRtFzQx+B4S4rG1FHDa1nRO9t2 7AOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699907263; x=1700512063; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=E3yprZr7aNLp9SlO0js7RNw4h+PqkoNmILFFiQnFNSY=; b=mBSsvev7OgjfiJTPVEl/HIurs/G1V9pSDW41CKw2TyD5BcFagheAclJYuQB17bbSWY ZUt1V57nirTGKC5G+ualrd56NLCSjVrzHTWhWHS3mmIsBPfgFo45Yort0HqvztajutYe Q5kyIoLSQOc2306wCok2/FGIJspc9rMG2MHxETLOIIRWq/KghaaiVk4JQWQ2pPXyTmbo cVNK4zW/x5cOdhDuahJAZg9MPoX0V4gaiLewIB3dhHTSYEkeygfVZzpRIF4cjRHrtlp1 PFKa2GjzaHoQbPZ5tED1TZoJyjVGnhwTDUtX7voro7FY50gSRzsVTsIx4CtYnEmyhpvR IE3Q== X-Gm-Message-State: AOJu0YxeoCBivrQ6b3s1hD2/IQZz0m1pjamaPcW6m3bxR5fy0xxmQtBL JVLLWIAJWNrG/Hmnvf8zSOm4YK6IlO6M8O4pfL4= X-Google-Smtp-Source: AGHT+IEy6MaYHATWGGjmbZ2qHbHBtbWfDBrdBsY2/ySDl/rPF5teL49ig0etYTGmbiypV7v4DdF2pw== X-Received: by 2002:adf:ea07:0:b0:32f:c188:38f2 with SMTP id q7-20020adfea07000000b0032fc18838f2mr4897976wrm.37.1699907262965; Mon, 13 Nov 2023 12:27:42 -0800 (PST) Received: from doug.home ([2a01:cb19:85a1:c200:ee66:fd17:945:f033]) by smtp.gmail.com with ESMTPSA id n7-20020a5d4847000000b0032d88e370basm6122774wrs.34.2023.11.13.12.27.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Nov 2023 12:27:42 -0800 (PST) From: Pierre-Henry =?UTF-8?Q?Fr=C3=B6hring?= Date: Mon, 13 Nov 2023 21:26:09 +0100 Message-ID: <26ef9c0f4bf2ff942ba2b42e1fadeb6174bbaa6a.1699906775.git.phfrohring@deeplinks.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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-bounces+larch=yhetil.org@gnu.org X-Migadu-Flow: FLOW_IN X-Migadu-Country: US X-Migadu-Spam-Score: -1.09 X-Spam-Score: -1.09 X-Migadu-Queue-Id: B2791588BA X-Migadu-Scanner: mx13.migadu.com X-TUID: 6rdMfSzovBsp * guix/build-system/mix.scm, * guix/build/mix-build-system.scm: New modules. Change-Id: I2cbf6c963a530e73420da0eb17ffaf92827451bf --- guix/build-system/mix.scm | 237 +++++++++++++++++++++++++ guix/build/mix-build-system.scm | 303 ++++++++++++++++++++++++++++++++ 2 files changed, 540 insertions(+) create mode 100644 guix/build-system/mix.scm create mode 100644 guix/build/mix-build-system.scm diff --git a/guix/build-system/mix.scm b/guix/build-system/mix.scm new file mode 100644 index 00000000..ae4407c9 --- /dev/null +++ b/guix/build-system/mix.scm @@ -0,0 +1,237 @@ +;;; GNU Guix --- Functional package management for GNU +;;; Copyright © 2023 Pierre-Henry Fröhring +;;; +;;; 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 . + +;; Commentary: +;; +;; Standard build procedure for Elixir packages using 'mix'. This is +;; implemented as an extension of 'gnu-build-system'. +;; +;; Code: + +(define-module (guix build-system mix) + #:use-module (gnu packages base) + #:use-module (gnu packages elixir) + #:use-module (gnu packages elixir-xyz) + #:use-module (gnu packages erlang) + #:use-module (guix build mix-build-system) + #:use-module (guix build-system gnu) + #:use-module (guix build-system) + #:use-module (guix gexp) + #:use-module (guix monads) + #:use-module (guix packages) + #:use-module (guix search-paths) + #:use-module (guix store) + #:use-module (guix utils) + #:use-module (ice-9 match) + #:use-module (srfi srfi-1) + #:use-module (srfi srfi-26) + #:export (mix-build-system hexpm-uri)) + +(define (hexpm-uri name version) + "Return the URI where to fetch the sources of a Hex package NAME at VERSION. +See: https://github.com/hexpm/specifications/blob/main/endpoints.md" + (string-append "https://repo.hex.pm/tarballs/" + (string-replace-substring (strip-elixir-prefix name) "-" "_") + "-" version ".tar")) + +(define glibc-utf8-locales + (make-glibc-utf8-locales glibc + #:locales (list "en_US") + #:name "glibc-utf8-locales")) + +(define (default-elixir) + "Return the default Elixir package." + ;; Lazily resolve the binding to avoid a circular dependency. + (let ((elixir (resolve-interface '(gnu packages elixir)))) + (module-ref elixir 'elixir))) + +(define imported-modules + `((guix build mix-build-system) + ,@%gnu-build-system-modules)) + +(define modules + '((guix build mix-build-system) + (guix build utils))) + +;; The prefix of Elixir packages. +(define %elixir-prefix "elixir-") + +;; The prefix of Erlang packages. +(define %erlang-prefix "erlang-") + +(define (erlang-package? package) + "Tell whether PACKAGE is an Erlang package." + (string-prefix? %erlang-prefix (package-name package))) + +(define (elixir-package? package) + "Tell whether PACKAGE is an Elixir package." + (string-prefix? %elixir-prefix (package-name package))) + +(define (erlang-or-elixir-pkg? package) + "Tell whether PACKAGE is an Elixir or an Erlang package." + (or (erlang-package? package) + (elixir-package? package))) + +(define (erlang-or-elixir-input? input) + "Tell whether INPUT is an Elixir or an Erlang input." + (match input + ((_ package) + (erlang-or-elixir-pkg? package)))) + +(define (input=? input1 input2) + "Tell whether inputs INPUT1 and INPUT2 are equal." + (define pkg1 (match input1 ((_ pkg) pkg))) + (define pkg2 (match input2 ((_ pkg) pkg))) + (string=? (package-name pkg1) (package-name pkg2))) + +;; A number of environment variables specific to the Mix build system are reflected here. +;; They are documented here: https://hexdocs.pm/mix/1.15.7/Mix.html#module-environment-variables. +;; Other parameters located in mix.exs are defined here: +;; https://hexdocs.pm/mix/1.15.7/Mix.Project.html#module-configuration +(define* (mix-build name + inputs + #:key + source + elixir-X.Y ;The major and minor of Elixir. + (tests? #t) + (mix-path #f) ;See MIX_PATH. + (mix-exs "mix.exs") ;See MIX_EXS. + (build-per-environment #t) ;See :build_per_environment. + (phases '%standard-phases) + (outputs '("out")) + (search-paths '()) + (system (%current-system)) + (guile #f) + (imported-modules imported-modules) + (modules modules)) + "Build SOURCE using Elixir, and with INPUTS." + + ;; Check the documentation of :build_per_environment here: + ;; https://hexdocs.pm/mix/1.15.7/Mix.Project.html#module-configuration And + ;; "Environments" here: + ;; https://hexdocs.pm/mix/1.15.7/Mix.html#module-environments + (define mix-environments + (if build-per-environment + `("prod" ,@(if tests? '("test") '())) + '("shared"))) + + (define builder + (with-imported-modules imported-modules + #~(begin + + (use-modules #$@(sexp->gexp modules)) + + #$(with-build-variables inputs outputs + #~(mix-build #:name #$name + #:source #+source + #:system #$system + #:tests? #$tests? + #:mix-path #$mix-path + #:mix-exs #$mix-exs + #:elixir-X.Y #$elixir-X.Y + #:mix-environments '#$mix-environments + #:build-per-environment #$build-per-environment + #:phases #$(if (pair? phases) + (sexp->gexp phases) + phases) + #:outputs %outputs + #:search-paths '#$(sexp->gexp + (map + search-path-specification->sexp + search-paths)) + #:inputs + %build-inputs))))) + + (mlet %store-monad ((guile (package->derivation (or guile (default-guile)) + system + #:graft? #f))) + (gexp->derivation name + builder + #:system system + #:graft? #f ;consistent with 'gnu-build' + #:target #f + #:guile-for-build guile))) + +(define* (lower name + #:key + source + (inputs '()) + (tests? #t) + (native-inputs '()) + (propagated-inputs '()) + outputs + system + target + (elixir (default-elixir)) + #:allow-other-keys #:rest arguments) + "Return a bag for NAME." + (define private-keywords + '(#:inputs #:native-inputs #:outputs #:system #:target #:elixir)) + + ;; Libraries are compiled using a given version of Elixir. This fact is + ;; encoded by the name of a sub-directory like lib/elixir/X.Y. We compute + ;; the value of X.Y here which is valid for the whole build. + (define elixir-X.Y (version-major+minor (package-version elixir))) + + ;; Elixir depends on a specific version of Erlang, this one. + (define erlang (lookup-package-input elixir "erlang")) + + ;; For mix to compile and test a package, it needs to find all inputs, + ;; native-inputs and propagated-inputs (including the transitive ones) under + ;; _build directories like _build/prod/lib. + ;; + ;; Given inputs and native-inputs of the package, we need to compute the + ;; transitive closure of all Erlang and Elixir propagated inputs and add + ;; them to the build inputs. + (define all-propagated-inputs + ((compose + (cut delete-duplicates <> input=?) + (cut filter erlang-or-elixir-input? <>) + (cut append-map package-transitive-propagated-inputs <>) + (cut map cadr <>)) + (append inputs native-inputs))) + + (define build-inputs + `(,@(standard-packages) + ("glibc-utf8-locales" ,glibc-utf8-locales) + ("erlang" ,erlang) + ("rebar3" ,rebar3) + ("elixir" ,elixir) + ("elixir-hex" ,elixir-hex) + ,@all-propagated-inputs + ,@inputs + ,@native-inputs)) + + ;; Some inputs, such as C programs, may be architecture dependent. + (define host-inputs (if target inputs '())) + + (bag (name name) + (system system) + (build-inputs build-inputs) + (host-inputs host-inputs) + (outputs outputs) + (build mix-build) + (arguments (append `(#:elixir-X.Y ,elixir-X.Y) + (strip-keyword-arguments private-keywords arguments))))) + +(define mix-build-system + (build-system (name 'mix) + (description "The standard Mix build system") + (lower lower))) + +;;; mix.scm ends here diff --git a/guix/build/mix-build-system.scm b/guix/build/mix-build-system.scm new file mode 100644 index 00000000..86a5db51 --- /dev/null +++ b/guix/build/mix-build-system.scm @@ -0,0 +1,303 @@ +;;; GNU Guix --- Functional package management for GNU +;;; Copyright © 2023 Pierre-Henry Fröhring +;;; +;;; 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 . + +;; Commentary: +;; +;; Code: + +(define-module (guix build mix-build-system) + #:use-module ((guix build gnu-build-system) #:prefix gnu:) + #:use-module (guix build utils) + #:use-module (ice-9 ftw) + #:use-module (ice-9 match) + #:use-module (ice-9 regex) + #:use-module (ice-9 string-fun) + #:use-module (srfi srfi-1) + #:use-module (srfi srfi-26) + #:export (mix-build + strip-elixir-prefix + %standard-phases)) + +(define (f-join . paths) + "Return a path (a string) formed from joining each component in PATHS (list of strings). +Example: \"a\" \"b\" \"c\" → \"a/b/c\"" + (string-join paths file-name-separator-string)) + +(define (strip-prefix prefix name) + "Return NAME without the prefix PREFIX." + (if (string-prefix? prefix name) + (string-drop name (string-length prefix)) + name)) + +;; All Elixir package names start with this prefix. If a package name starts +;; with this prefix, then it is an Elixir package. +(define %elixir-prefix "elixir-") + +(define (path->elixir-lib path X.Y) + "Return the path to the directory within PATH where libraries of an Elixir +package are installed. Here, X.Y represents the major and minor version +numbers of Elixir used for compilation." + (f-join path "lib" "elixir" X.Y)) + +(define (elixir-name? name) + "Determines if NAME is the name or the label associated to an Elixir +package." + (string-prefix? %elixir-prefix name)) + +(define (elixir-input? X.Y input) + "Determines if the given INPUT is an Elixir input." + (match input + ((label . path) + ;; XXX: The second condition may be enough. + (and (elixir-name? label) + (directory-exists? (path->elixir-lib path X.Y)))))) + +(define (strip-elixir-prefix name) + "Strip %elixir-prefix from NAME." + (strip-prefix %elixir-prefix name)) + +;; All Erlang package names start with this prefix. If a package name starts +;; with this prefix, then it is an Erlang package. +(define %erlang-prefix "erlang-") + +(define (path->erlang-lib path) + "Return the path of the directory where libraries of an Erlang package are +installed in the store." + (f-join path "lib" "erlang" "lib")) + +(define (erlang-name? name) + "Determines if NAME is the name or the label associated to an Erlang +package." + (string-prefix? %erlang-prefix name)) + +(define (erlang-input? input) + "Determines if the given INPUT is an Erlang input." + (match input + ((label . path) + ;; XXX: one condition may be enough. Without the first one, the erlang + ;; input is considered an input when we just want Erlang packages. + (and (erlang-name? label) + (directory-exists? (path->erlang-lib path)))))) + +(define (strip-erlang-prefix name) + "Strip %erlang-prefix from NAME." + (strip-prefix %erlang-prefix name)) + +(define (erlang-or-elixir-input? X.Y input) + "Determines if the given INPUT is an Erlang or Elixir input." + (or (erlang-input? input) + (elixir-input? X.Y input))) + +(define (snakecase-name name) + "Return a snakecase version of NAME." + (string-replace-substring (string-downcase name) "-" "_")) + +(define (label->library-name label) + "Return the library name associated to an input label LABEL." + (define stripped-label + (cond + ((erlang-name? label) + (strip-erlang-prefix label)) + ((elixir-name? label) + (strip-elixir-prefix label)) + (#t (error "Invalid label: expected an Erlang or Elixir label." 'label label)))) + (snakecase-name stripped-label)) + +(define (pkg-name->library-name name) + "Return the name of the library deduced from the name of the Guix package. +Example: elixir-a-pkg-1.0.2 → a_pkg +See: https://www.erlang.org/doc/man/code#code-path" + ((compose + label->library-name + (cut string-join <> "-") + (cut drop-right <> 1) + (cut string-split <> #\-)) + name)) + +;; We fix as many variables as possible. +;; See: https://hexdocs.pm/mix/1.15.7/Mix.html#module-environment-variables +(define MIX_HOME "MIX_HOME") + +(define MIX_ARCHIVES "MIX_ARCHIVES") +(define (%mix-archives mix-home) (f-join mix-home "archives")) +(define MIX_BUILD_ROOT "MIX_BUILD_ROOT") +(define %mix-build-root "_build") +(define MIX_DEPS_PATH "MIX_DEPS_PATH") +(define %mix-deps-path "deps") +(define MIX_PATH "MIX_PATH") +(define MIX_REBAR3 "MIX_REBAR3") +(define MIX_EXS "MIX_EXS") +(define %mix-exs "mix.exs") +;; XXX: if different architecture are needed, then use this variable. +(define MIX_TARGET "MIX_TARGET") +(define MIX_ENV "MIX_ENV") +(define %mix-env-prod "prod") +(define %mix-env-test "test") +(define %mix-env-shared "shared") + +;; The name of the directory where compiled libraries by mix are stored. +(define %mix-lib "lib") + +;; Useful because Elixir expects a UTF-8 locale. +(define LC_ALL "LC_ALL") + +(define (mix-build-dir mix-env) + "Return the directory where build artifacts are to be installed according to +en environment MIX-ENV in the current directory." + (f-join %mix-build-root mix-env %mix-lib)) + +(define* (unpack #:key source mix-path #:allow-other-keys) + "Unpack SOURCE in the working directory, and change directory within the +source. When SOURCE is a directory, copy it in a sub-directory of the current +working directory." + (let ((gnu-unpack (assoc-ref gnu:%standard-phases 'unpack))) + (gnu-unpack #:source source) + (let ((contents "contents.tar.gz")) + (when (file-exists? contents) + (invoke "tar" "xvf" contents))))) + +(define* (configure #:key inputs mix-path mix-exs #:allow-other-keys) + "Set environment variables." + (setenv LC_ALL "en_US.UTF-8") + (setenv MIX_HOME (getcwd)) + (setenv MIX_ARCHIVES (%mix-archives (getenv MIX_HOME))) + (setenv MIX_BUILD_ROOT %mix-build-root) + (setenv MIX_DEPS_PATH %mix-deps-path) + (setenv MIX_PATH (or mix-path "")) + (setenv MIX_REBAR3 (f-join (assoc-ref inputs "rebar3") "bin" "rebar3")) + (setenv MIX_EXS mix-exs)) + +(define* (install-hex #:key inputs name elixir-X.Y #:allow-other-keys) + "Install Hex." + (define hex-name "hex") + (define hex-path (assoc-ref inputs "elixir-hex")) + (define hex-lib (f-join (path->elixir-lib hex-path elixir-X.Y) hex-name)) + (define hex-archive-path (f-join (getenv MIX_ARCHIVES) hex-name)) + (mkdir-p hex-archive-path) + (symlink hex-lib (f-join hex-archive-path hex-name))) + +(define* (install-dependencies #:key + name + tests? + build-per-environment + (inputs '()) + (native-inputs '()) + elixir-X.Y + mix-environments + #:allow-other-keys + #:rest rest) + "Install dependencies. +Given an environment mix-env, we define all-inputs(mix-env) as the set of all +necessary Erlang and Elixir inputs and associated propagated inputs (and +transitive propagated inputs). + +For example, all-inputs(prod) represents all the Erlang and Elixir inputs and +propagated inputs necessary to compile the Mix project for the prod +environment. + +If dep belongs to all-inputs(mix-env) and its library name is dep-name, then +it is installed under (f-join (mix-build-dir mix-env) dep-name) as a symbolic +link." + (define (all-inputs mix-env) + (define env-inputs + (cond + ((string=? mix-env %mix-env-prod) + inputs) + ((member mix-env (list %mix-env-test %mix-env-shared)) + (append inputs native-inputs)) + (#t (error "Unexpected Mix environment." 'mix-env mix-env)))) + (filter (cut erlang-or-elixir-input? elixir-X.Y <>) env-inputs)) + + (define (install-input mix-env input) + (let ((dir (mix-build-dir mix-env))) + (mkdir-p dir) + (match input + ((label . path) + (let ((lib-name (label->library-name label))) + (symlink + (f-join (path->elixir-lib path elixir-X.Y) lib-name) + (f-join dir lib-name))))))) + + (define (install-inputs mix-env) + (for-each (cut install-input mix-env <>) + (all-inputs mix-env))) + + (for-each install-inputs mix-environments)) + +(define* (build #:key mix-environments #:allow-other-keys) + "Builds the Mix project." + (define (compile mix-env) + (setenv MIX_ENV mix-env) + (invoke "mix" "compile" "--no-deps-check")) + (for-each compile mix-environments)) + +(define* (check #:key (tests? #t) #:allow-other-keys) + "Test the Mix project." + (if tests? + (invoke "mix" "test" "--no-deps-check") + (format #t "tests? = ~a~%" tests?))) + +(define* (remove-mix-dirs . _) + "Remove all .mix/ directories. +We do not want to copy them to the installation directory." + (define mix-dirs + (find-files "." + (file-name-predicate "\\.mix$") + #:directories? #t)) + (for-each delete-file-recursively mix-dirs)) + +(define* (install #:key + inputs + outputs + name + build-per-environment + elixir-X.Y + #:allow-other-keys) + "Install build artifacts in the store." + (define lib-name (pkg-name->library-name name)) + + (define dir-build + (f-join (mix-build-dir (if build-per-environment %mix-env-prod %mix-env-shared)) + lib-name)) + + (define dir-install + (f-join (path->elixir-lib (assoc-ref outputs "out") elixir-X.Y) + lib-name)) + (mkdir-p dir-install) + + (copy-recursively dir-build dir-install + #:follow-symlinks? #t)) + +(define %standard-phases + (modify-phases gnu:%standard-phases + (delete 'bootstrap) + (replace 'configure configure) + (replace 'unpack unpack) + (add-after 'patch-generated-file-shebangs 'install-hex install-hex) + (add-after 'install-hex 'install-dependencies install-dependencies) + (replace 'build build) + (replace 'check check) + (add-before 'install 'remove-mix-dirs remove-mix-dirs) + (replace 'install install))) + +(define* (mix-build #:key inputs (phases %standard-phases) + #:allow-other-keys #:rest args) + "Build the given Mix package, applying all of PHASES in order." + (apply gnu:gnu-build #:inputs inputs #:phases phases args)) + +;;; mix-build-system.scm ends here base-commit: a0d337e79c87d7c38c79d0291974f490cb137a52 -- 2.41.0