julia-build-system.scm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019, 2020 Nicolò Balzarotti <nicolo@nixo.xyz>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (guix build julia-build-system)
  19. #:use-module ((guix build gnu-build-system) #:prefix gnu:)
  20. #:use-module (guix build utils)
  21. #:use-module (ice-9 match)
  22. #:export (%standard-phases
  23. julia-create-package-toml
  24. julia-build))
  25. ;; Commentary:
  26. ;;
  27. ;; Builder-side code of the standard build procedure for Julia packages.
  28. ;;
  29. ;; Code:
  30. (define (invoke-julia code)
  31. (invoke "julia" "-e" code))
  32. ;; subpath where we store the package content
  33. (define %package-path "/share/julia/packages/")
  34. (define* (install #:key source inputs outputs #:allow-other-keys)
  35. (let* ((out (assoc-ref outputs "out"))
  36. (package-dir (string-append out %package-path
  37. (strip-store-file-name source))))
  38. (mkdir-p package-dir)
  39. (copy-recursively (getcwd) package-dir))
  40. #t)
  41. (define* (precompile #:key source inputs outputs #:allow-other-keys)
  42. (let* ((out (assoc-ref outputs "out"))
  43. (builddir (string-append out "/share/julia/"))
  44. (package (strip-store-file-name source)))
  45. (mkdir-p builddir)
  46. ;; With a patch, SOURCE_DATE_EPOCH is honored
  47. (setenv "SOURCE_DATE_EPOCH" "1")
  48. (setenv "JULIA_DEPOT_PATH" builddir)
  49. ;; Add new package dir to the load path.
  50. (setenv "JULIA_LOAD_PATH"
  51. (string-append builddir "packages/" ":"
  52. (or (getenv "JULIA_LOAD_PATH")
  53. "")))
  54. ;; Actual precompilation:
  55. (invoke-julia
  56. ;; When using Julia as a user, Julia writes precompile cache to the first
  57. ;; entry of the DEPOT_PATH list (by default, the home dir). We want to
  58. ;; write it to the store, so let's push the store path as the first
  59. ;; element of DEPOT_PATH. Once the cache file exists, this hack is not
  60. ;; needed anymore (like in the check phase). If the user install new
  61. ;; packages, those will be installed and precompiled in the home dir.
  62. (string-append "pushfirst!(DEPOT_PATH, pop!(DEPOT_PATH)); using " package)))
  63. #t)
  64. (define* (check #:key source inputs outputs #:allow-other-keys)
  65. (let* ((out (assoc-ref outputs "out"))
  66. (package (strip-store-file-name source))
  67. (builddir (string-append out "/share/julia/")))
  68. ;; With a patch, SOURCE_DATE_EPOCH is honored
  69. (setenv "SOURCE_DATE_EPOCH" "1")
  70. (setenv "JULIA_DEPOT_PATH" builddir)
  71. (setenv "JULIA_LOAD_PATH" (string-append builddir "packages/"))
  72. (invoke-julia (string-append "using Pkg;Pkg.test(\"" package "\")")))
  73. #t)
  74. (define (julia-create-package-toml outputs source
  75. name uuid version
  76. deps)
  77. "Some packages are not using the new Package.toml dependency specifications.
  78. Write this file manually, so that Julia can find its dependencies."
  79. (let ((f (open-file
  80. (string-append
  81. (assoc-ref outputs "out")
  82. %package-path
  83. (string-append
  84. name "/Project.toml"))
  85. "w")))
  86. (display (string-append
  87. "
  88. name = \"" name "\"
  89. uuid = \"" uuid "\"
  90. version = \"" version "\"
  91. ") f)
  92. (when (not (null? deps))
  93. (display "[deps]\n" f)
  94. (for-each (lambda dep
  95. (display (string-append (car (car dep)) " = \"" (cdr (car dep)) "\"\n")
  96. f))
  97. deps))
  98. (close-port f))
  99. #t)
  100. (define %standard-phases
  101. (modify-phases gnu:%standard-phases
  102. (delete 'check) ; tests must be run after installation
  103. (replace 'install install)
  104. (add-after 'install 'precompile precompile)
  105. ;; (add-after 'install 'check check)
  106. ;; TODO: In the future we could add a "system-image-generation" phase
  107. ;; where we use PackageCompiler.jl to speed up package loading times
  108. (delete 'configure)
  109. (delete 'bootstrap)
  110. (delete 'patch-usr-bin-file)
  111. (delete 'build)))
  112. (define* (julia-build #:key inputs (phases %standard-phases)
  113. #:allow-other-keys #:rest args)
  114. "Build the given Julia package, applying all of PHASES in order."
  115. (apply gnu:gnu-build
  116. #:inputs inputs #:phases phases
  117. args))