julia-build-system.scm 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019 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 (generate-load-path inputs outputs)
  35. (string-append
  36. (string-join (map (match-lambda
  37. ((_ . path)
  38. (string-append path %package-path)))
  39. ;; Restrict to inputs beginning with "julia-".
  40. (filter (match-lambda
  41. ((name . _)
  42. (string-prefix? "julia-" name)))
  43. inputs))
  44. ":")
  45. (string-append ":" (assoc-ref outputs "out") %package-path)
  46. ;; stdlib is always required to find Julia's standard libraries.
  47. ;; usually there are other two paths in this variable:
  48. ;; "@" and "@v#.#"
  49. ":@stdlib"))
  50. (define* (install #:key source inputs outputs #:allow-other-keys)
  51. (let* ((out (assoc-ref outputs "out"))
  52. (package-dir (string-append out %package-path
  53. (string-append
  54. (strip-store-file-name source)))))
  55. (setenv "JULIA_LOAD_PATH" (generate-load-path inputs outputs))
  56. (mkdir-p package-dir)
  57. (copy-recursively source package-dir))
  58. #t)
  59. ;; TODO: Precompilation is working, but I don't know how to tell
  60. ;; julia to use use it. If (on rantime) we set HOME to
  61. ;; store path, julia tries to write files there (failing)
  62. (define* (precompile #:key source inputs outputs #:allow-other-keys)
  63. (let* ((out (assoc-ref outputs "out"))
  64. (builddir (string-append out "/share/julia/"))
  65. (package (strip-store-file-name source)))
  66. (mkdir-p builddir)
  67. (setenv "JULIA_DEPOT_PATH" builddir)
  68. (setenv "JULIA_LOAD_PATH" (generate-load-path inputs outputs))
  69. ;; Actual precompilation
  70. (invoke-julia (string-append "using " package)))
  71. #t)
  72. (define* (check #:key source inputs outputs #:allow-other-keys)
  73. (let* ((out (assoc-ref outputs "out"))
  74. (package (strip-store-file-name source))
  75. (builddir (string-append out "/share/julia/")))
  76. (setenv "JULIA_DEPOT_PATH" builddir)
  77. (setenv "JULIA_LOAD_PATH" (generate-load-path inputs outputs))
  78. (invoke-julia (string-append "using Pkg;Pkg.test(\"" package "\")")))
  79. #t)
  80. (define (julia-create-package-toml outputs source
  81. name uuid version
  82. deps)
  83. "Some packages are not using the new Package.toml dependency specifications.
  84. Write this file manually, so that Julia can find its dependencies."
  85. (let ((f (open-file
  86. (string-append
  87. (assoc-ref outputs "out")
  88. %package-path
  89. (string-append
  90. name "/Project.toml"))
  91. "w")))
  92. (display (string-append
  93. "
  94. name = \"" name "\"
  95. uuid = \"" uuid "\"
  96. version = \"" version "\"
  97. ") f)
  98. (when (not (null? deps))
  99. (display "[deps]\n" f)
  100. (for-each (lambda dep
  101. (display (string-append (car (car dep)) " = \"" (cdr (car dep)) "\"\n")
  102. f))
  103. deps))
  104. (close-port f))
  105. #t)
  106. (define %standard-phases
  107. (modify-phases gnu:%standard-phases
  108. (delete 'check) ; tests must be run after installation
  109. (replace 'install install)
  110. (add-after 'install 'precompile precompile)
  111. ;; (add-after 'install 'check check)
  112. ;; TODO: In the future we could add a "system-image-generation" phase
  113. ;; where we use PackageCompiler.jl to speed up package loading times
  114. (delete 'configure)
  115. (delete 'bootstrap)
  116. (delete 'patch-usr-bin-file)
  117. (delete 'build)))
  118. (define* (julia-build #:key inputs (phases %standard-phases)
  119. #:allow-other-keys #:rest args)
  120. "Build the given Julia package, applying all of PHASES in order."
  121. (apply gnu:gnu-build
  122. #:inputs inputs #:phases phases
  123. args))