julia-build-system.scm 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #:use-module (ice-9 regex)
  23. #:use-module (ice-9 rdelim)
  24. #:export (%standard-phases
  25. julia-create-package-toml
  26. julia-build))
  27. ;; Commentary:
  28. ;;
  29. ;; Builder-side code of the standard build procedure for Julia packages.
  30. ;;
  31. ;; Code:
  32. (define (invoke-julia code)
  33. (invoke "julia" "-e" code))
  34. ;; subpath where we store the package content
  35. (define %package-path "/share/julia/packages/")
  36. (define (project.toml->name file)
  37. "Look for Julia package name in the TOML file FILE (usually named
  38. Project.toml)."
  39. (call-with-input-file file
  40. (lambda (in)
  41. (let loop ((line (read-line in 'concat)))
  42. (if (eof-object? line)
  43. #f
  44. (let ((m (string-match "name\\s*=\\s*\"(.*)\"" line)))
  45. (if m (match:substring m 1)
  46. (loop (read-line in 'concat)))))))))
  47. (define* (install #:key source inputs outputs julia-package-name
  48. #:allow-other-keys)
  49. (let* ((out (assoc-ref outputs "out"))
  50. (package-dir (string-append out %package-path
  51. (or
  52. julia-package-name
  53. (project.toml->name "Project.toml")))))
  54. (mkdir-p package-dir)
  55. (copy-recursively (getcwd) package-dir))
  56. #t)
  57. (define* (precompile #:key source inputs outputs julia-package-name
  58. #:allow-other-keys)
  59. (let* ((out (assoc-ref outputs "out"))
  60. (builddir (string-append out "/share/julia/"))
  61. (package (or julia-package-name (project.toml->name "Project.toml"))))
  62. (mkdir-p builddir)
  63. ;; With a patch, SOURCE_DATE_EPOCH is honored
  64. (setenv "SOURCE_DATE_EPOCH" "1")
  65. (setenv "JULIA_DEPOT_PATH" builddir)
  66. ;; Add new package dir to the load path.
  67. (setenv "JULIA_LOAD_PATH"
  68. (string-append builddir "packages/" ":"
  69. (or (getenv "JULIA_LOAD_PATH")
  70. "")))
  71. ;; Actual precompilation:
  72. (invoke-julia
  73. ;; When using Julia as a user, Julia writes precompile cache to the first
  74. ;; entry of the DEPOT_PATH list (by default, the home dir). We want to
  75. ;; write it to the store, so let's push the store path as the first
  76. ;; element of DEPOT_PATH. Once the cache file exists, this hack is not
  77. ;; needed anymore (like in the check phase). If the user install new
  78. ;; packages, those will be installed and precompiled in the home dir.
  79. (string-append "pushfirst!(DEPOT_PATH, pop!(DEPOT_PATH)); using " package)))
  80. #t)
  81. (define* (check #:key tests? source inputs outputs julia-package-name
  82. #:allow-other-keys)
  83. (when tests?
  84. (let* ((out (assoc-ref outputs "out"))
  85. (package (or julia-package-name (project.toml->name "Project.toml")))
  86. (builddir (string-append out "/share/julia/")))
  87. ;; With a patch, SOURCE_DATE_EPOCH is honored
  88. (setenv "SOURCE_DATE_EPOCH" "1")
  89. (setenv "JULIA_DEPOT_PATH" builddir)
  90. (setenv "JULIA_LOAD_PATH"
  91. (string-append builddir "packages/" ":"
  92. (or (getenv "JULIA_LOAD_PATH")
  93. "")))
  94. (setenv "HOME" "/tmp")
  95. (invoke "julia" "--depwarn=yes"
  96. (string-append builddir "packages/"
  97. package "/test/runtests.jl"))))
  98. #t)
  99. (define (julia-create-package-toml outputs source
  100. name uuid version
  101. deps)
  102. "Some packages are not using the new Package.toml dependency specifications.
  103. Write this file manually, so that Julia can find its dependencies."
  104. (let ((f (open-file
  105. (string-append
  106. (assoc-ref outputs "out")
  107. %package-path
  108. (string-append
  109. name "/Project.toml"))
  110. "w")))
  111. (display (string-append
  112. "
  113. name = \"" name "\"
  114. uuid = \"" uuid "\"
  115. version = \"" version "\"
  116. ") f)
  117. (when (not (null? deps))
  118. (display "[deps]\n" f)
  119. (for-each (lambda dep
  120. (display (string-append (car (car dep)) " = \"" (cdr (car dep)) "\"\n")
  121. f))
  122. deps))
  123. (close-port f))
  124. #t)
  125. (define %standard-phases
  126. (modify-phases gnu:%standard-phases
  127. (delete 'check) ; tests must be run after installation
  128. (replace 'install install)
  129. (add-after 'install 'precompile precompile)
  130. (add-after 'install 'check check)
  131. ;; TODO: In the future we could add a "system-image-generation" phase
  132. ;; where we use PackageCompiler.jl to speed up package loading times
  133. (delete 'configure)
  134. (delete 'bootstrap)
  135. (delete 'patch-usr-bin-file)
  136. (delete 'build)))
  137. (define* (julia-build #:key inputs julia-package-name
  138. (phases %standard-phases)
  139. #:allow-other-keys #:rest args)
  140. "Build the given Julia package, applying all of PHASES in order."
  141. (apply gnu:gnu-build
  142. #:inputs inputs #:phases phases
  143. #:julia-package-name julia-package-name
  144. args))