1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
| | ;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2019, 2020 Nicolò Balzarotti <nicolo@nixo.xyz>
;;; Copyright © 2021 Jean-Baptiste Volatier <jbv@pm.me>
;;; Copyright © 2021, 2022 Simon Tournier <zimon.toutoune@gmail.com>
;;;
;;; 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 <http://www.gnu.org/licenses/>.
(define-module (guix build julia-build-system)
#:use-module ((guix build gnu-build-system) #:prefix gnu:)
#:use-module (guix build utils)
#:use-module (rnrs io ports)
#:use-module (ice-9 match)
#:use-module (ice-9 regex)
#:use-module (ice-9 rdelim)
#:use-module (ice-9 popen)
#:use-module (srfi srfi-1)
#:export (%standard-phases
julia-build))
;; Commentary:
;;
;; Builder-side code of the standard build procedure for Julia packages.
;;
;; Code:
(define (invoke-julia code)
(invoke "julia" "-e" code))
;; subpath where we store the package content
(define %package-path "/share/julia/loadpath/")
(define (project.toml->name file)
"Look for Julia package name in the TOML file FILE (usually named
Project.toml)."
(call-with-input-file file
(lambda (in)
(let loop ((line (read-line in 'concat)))
(if (eof-object? line)
#f
(let ((m (string-match "name\\s*=\\s*\"(.*)\"" line)))
(if m (match:substring m 1)
(loop (read-line in 'concat)))))))))
(define (project.toml->uuid file)
"Look for Julia package uuid in the TOML file FILE (usually named
Project.toml)."
(call-with-input-file file
(lambda (in)
(let loop ((line (read-line in 'concat)))
(if (eof-object? line)
#f
(let ((m (string-match "uuid\\s*=\\s*\"(.*)\"" line)))
(if m (match:substring m 1)
(loop (read-line in 'concat)))))))))
(define* (install #:key source inputs outputs julia-package-name
#:allow-other-keys)
(let* ((out (assoc-ref outputs "out"))
(package-dir (string-append out %package-path
(or
julia-package-name
(project.toml->name "Project.toml")))))
(mkdir-p package-dir)
(copy-recursively (getcwd) package-dir)))
(define* (precompile #:key source inputs outputs julia-package-name
#:allow-other-keys)
(let* ((out (assoc-ref outputs "out"))
(builddir (string-append out "/share/julia/"))
(package (or julia-package-name (project.toml->name "Project.toml"))))
(mkdir-p builddir)
;; With a patch, SOURCE_DATE_EPOCH is honored
(setenv "SOURCE_DATE_EPOCH" "1")
(setenv "JULIA_DEPOT_PATH" builddir)
;; Add new package dir to the load path.
(setenv "JULIA_LOAD_PATH"
(string-append builddir "loadpath/" ":"
(or (getenv "JULIA_LOAD_PATH")
"")))
;; Actual precompilation:
(invoke-julia
;; When using Julia as a user, Julia writes precompile cache to the first
;; entry of the DEPOT_PATH list (by default, the home dir). We want to
;; write it to the store, so let's push the store path as the first
;; element of DEPOT_PATH. Once the cache file exists, this hack is not
;; needed anymore (like in the check phase). If the user install new
;; packages, those will be installed and precompiled in the home dir.
(string-append "pushfirst!(DEPOT_PATH, pop!(DEPOT_PATH)); using "
package))))
(define* (check #:key tests? source inputs outputs julia-package-name
parallel-tests? #:allow-other-keys)
(when tests?
(let* ((out (assoc-ref outputs "out"))
(package (or julia-package-name (project.toml->name "Project.toml")))
(builddir (string-append out "/share/julia/"))
(job-count (if parallel-tests?
(parallel-job-count)
1))
;; The --proc argument of Julia *adds* extra processors rather than
;; specify the exact count to use, so zero must be specified to
;; disable parallel processing...
(additional-procs (max 0 (1- job-count))))
;; With a patch, SOURCE_DATE_EPOCH is honored
(setenv "SOURCE_DATE_EPOCH" "1")
(setenv "JULIA_DEPOT_PATH" builddir)
(setenv "JULIA_LOAD_PATH"
(string-append builddir "loadpath/" ":"
(or (getenv "JULIA_LOAD_PATH")
"")))
(setenv "JULIA_CPU_THREADS" (number->string job-count))
(setenv "HOME" "/tmp")
(apply invoke "julia"
`("--depwarn=yes"
,@(if parallel-tests?
;; XXX: ... but '--procs' doesn't accept 0 as a valid
;; value, so just omit the argument entirely.
(list (string-append "--procs="
(number->string additional-procs)))
'())
,(string-append builddir "loadpath/"
package "/test/runtests.jl"))))))
(define* (link-depot #:key source inputs outputs
julia-package-name julia-package-uuid #:allow-other-keys)
(let* ((out (assoc-ref outputs "out"))
(name+version (strip-store-file-name out))
(version (last (string-split name+version #\-)))
(package-name (or
julia-package-name
(project.toml->name "Project.toml")))
(package-dir (string-append out %package-path package-name))
(uuid (or julia-package-uuid (project.toml->uuid "Project.toml")))
(pipe (open-pipe* OPEN_READ "julia" "-e"
(format #f "using Pkg;
println(Base.version_slug(Base.UUID(\"~a\"),
Base.SHA1(Pkg.GitTools.tree_hash(\".\"))))" uuid)))
(slug (string-trim-right (get-string-all pipe))))
;; Few packages do not have the regular Project.toml file, then when they
;; are propagated, dependencies do not find them and an raise error.
(unless (file-exists? "Project.toml")
(julia-create-package-toml (getcwd)
julia-package-name julia-package-uuid
version
#:file "Project.toml"))
;; When installing a package, julia looks first at in the JULIA_DEPOT_PATH
;; for a path like packages/PACKAGE/XXXX
;; Where XXXX is a slug encoding the package UUID and SHA1 of the files
;; Here we create a link with the correct path to enable julia to find the
;; package
(mkdir-p (string-append out "/share/julia/packages/" package-name))
(symlink package-dir (string-append out "/share/julia/packages/"
package-name "/" slug))))
(define* (julia-create-package-toml location
name uuid version
#:optional
(deps '())
#:key
(file "Project.toml"))
"Some packages are not using the new Project.toml dependency specifications.
Write this FILE manually, so that Julia can find its dependencies."
(let ((f (open-file
(string-append location "/" file)
"w")))
(display (string-append
"
name = \"" name "\"
uuid = \"" uuid "\"
version = \"" version "\"
") f)
(when (not (null? deps))
(display "[deps]\n" f)
(for-each (lambda dep
(display (string-append (car (car dep)) " = \"" (cdr (car dep)) "\"\n")
f))
deps))
(close-port f)))
(define %standard-phases
(modify-phases gnu:%standard-phases
(delete 'check) ; tests must be run after installation
(replace 'install install)
(add-after 'install 'precompile precompile)
(add-after 'unpack 'link-depot link-depot)
(add-after 'install 'check check)
;; TODO: In the future we could add a "system-image-generation" phase
;; where we use PackageCompiler.jl to speed up package loading times
(delete 'configure)
(delete 'bootstrap)
(delete 'patch-usr-bin-file)
(delete 'build)))
(define* (julia-build #:key inputs julia-package-name julia-package-uuid
(phases %standard-phases)
#:allow-other-keys #:rest args)
"Build the given Julia package, applying all of PHASES in order."
(apply gnu:gnu-build
#:inputs inputs #:phases phases
#:julia-package-name julia-package-name
#:julia-package-uuid julia-package-uuid
args))
|