node-build-system.scm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015 David Thompson <davet@gnu.org>
  3. ;;; Copyright © 2016, 2020 Jelle Licht <jlicht@fsfe.org>
  4. ;;; Copyright © 2019, 2021 Timothy Sample <samplet@ngyro.com>
  5. ;;; Copyright © 2021, 2022 Philip McGrath <philip@philipmcgrath.com>
  6. ;;; Copyright © 2022 Liliana Marie Prikler <liliana.prikler@gmail.com>
  7. ;;;
  8. ;;; This file is part of GNU Guix.
  9. ;;;
  10. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  11. ;;; under the terms of the GNU General Public License as published by
  12. ;;; the Free Software Foundation; either version 3 of the License, or (at
  13. ;;; your option) any later version.
  14. ;;;
  15. ;;; GNU Guix is distributed in the hope that it will be useful, but
  16. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;;; GNU General Public License for more details.
  19. ;;;
  20. ;;; You should have received a copy of the GNU General Public License
  21. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  22. (define-module (guix build node-build-system)
  23. #:use-module ((guix build gnu-build-system) #:prefix gnu:)
  24. #:use-module (guix build utils)
  25. #:use-module (guix build json)
  26. #:use-module (ice-9 ftw)
  27. #:use-module (ice-9 regex)
  28. #:use-module (ice-9 match)
  29. #:use-module (srfi srfi-1)
  30. #:use-module (srfi srfi-71)
  31. #:export (%standard-phases
  32. with-atomic-json-file-replacement
  33. delete-dependencies
  34. node-build))
  35. (define (with-atomic-json-file-replacement file proc)
  36. "Like 'with-atomic-file-replacement', but PROC is called with a single
  37. argument---the result of parsing FILE's contents as json---and should a value
  38. to be written as json to the replacement FILE."
  39. (with-atomic-file-replacement file
  40. (lambda (in out)
  41. (write-json (proc (read-json in)) out))))
  42. (define* (assoc-ref* alist key #:optional default)
  43. "Like assoc-ref, but return DEFAULT instead of #f if no value exists."
  44. (match (assoc key alist)
  45. (#f default)
  46. ((_ . value) value)))
  47. (define* (jsobject-ref obj key #:optional default)
  48. (match obj
  49. (('@ . alist) (assoc-ref* alist key default))))
  50. (define* (alist-pop alist key #:optional (= equal?))
  51. "Return two values, the first pair in ALIST with key KEY, and the other
  52. elements. Equality calls are made as (= KEY ALISTCAR)."
  53. (define (found? pair)
  54. (= key (car pair)))
  55. (let ((before after (break found? alist)))
  56. (if (pair? after)
  57. (values (car after) (append before (cdr after)))
  58. (values #f before))))
  59. (define* (alist-update alist key proc #:optional default (= equal?))
  60. "Return an association list like ALIST, but with KEY mapped to the result of
  61. PROC applied to the first value found under the comparison (= KEY ALISTCAR).
  62. If no such value exists, use DEFAULT instead.
  63. Unlike acons, this removes the previous association of KEY (assuming it is
  64. unique), but the result may still share storage with ALIST."
  65. (let ((pair rest (alist-pop alist key =)))
  66. (acons key
  67. (proc (if (pair? pair)
  68. (cdr pair)
  69. default))
  70. rest)))
  71. (define (jsobject-update* js . updates)
  72. "Return a json object like JS, but with all UPDATES applied. Each update is
  73. a list (KEY PROC [DEFAULT]), so that KEY is mapped to the result of PROC
  74. applied to the value to which KEY is mapped in JS. If no such mapping exists,
  75. PROC is instead applied to DEFAULT, or to '#f' is no DEFAULT is specified.
  76. The update takes place from left to right, so later UPDATERs will receive the
  77. values returned by earlier UPDATERs for the same KEY."
  78. (match js
  79. (('@ . alist)
  80. (let loop ((alist alist)
  81. (updates updates))
  82. (match updates
  83. (() (cons '@ alist))
  84. (((key proc) . updates)
  85. (loop (alist-update alist key proc #f equal?) updates))
  86. (((key proc default) . updates)
  87. (loop (alist-update alist key proc default equal?) updates)))))))
  88. (define (jsobject-union combine seed . objects)
  89. "Merge OBJECTS into SEED by applying (COMBINE KEY VAL0 VAL), where VAL0
  90. is the value found in the (possibly updated) SEED and VAL is the new value
  91. found in one of the OBJECTS."
  92. (match seed
  93. (('@ . aseed)
  94. (match objects
  95. (() seed)
  96. ((('@ . alists) ...)
  97. (cons
  98. '@
  99. (fold (lambda (alist aseed)
  100. (if (null? aseed) alist
  101. (fold
  102. (match-lambda*
  103. (((k . v) aseed)
  104. (let ((pair tail (alist-pop alist k)))
  105. (match pair
  106. (#f (acons k v aseed))
  107. ((_ . v0) (acons k (combine k v0 v) aseed))))))
  108. aseed
  109. alist)))
  110. aseed
  111. alists)))))))
  112. ;; Possibly useful helper functions:
  113. ;; (define (newest key val0 val) val)
  114. ;; (define (unkeyed->keyed proc) (lambda (_key val0 val) (proc val0 val)))
  115. ;;;
  116. ;;; Phases.
  117. ;;;
  118. (define (set-home . _)
  119. (with-directory-excursion ".."
  120. (let loop ((i 0))
  121. (let ((dir (string-append "npm-home-" (number->string i))))
  122. (if (directory-exists? dir)
  123. (loop (1+ i))
  124. (begin
  125. (mkdir dir)
  126. (setenv "HOME" (string-append (getcwd) "/" dir))
  127. (format #t "set HOME to ~s~%" (getenv "HOME")))))))
  128. #t)
  129. (define (module-name module)
  130. (let* ((package.json (string-append module "/package.json"))
  131. (package-meta (call-with-input-file package.json read-json)))
  132. (jsobject-ref package-meta "name")))
  133. (define (index-modules input-paths)
  134. (define (list-modules directory)
  135. (append-map (lambda (x)
  136. (if (string-prefix? "@" x)
  137. (list-modules (string-append directory "/" x))
  138. (list (string-append directory "/" x))))
  139. (filter (lambda (x)
  140. (not (member x '("." ".."))))
  141. (or (scandir directory) '()))))
  142. (let ((index (make-hash-table (* 2 (length input-paths)))))
  143. (for-each (lambda (dir)
  144. (let ((nm (string-append dir "/lib/node_modules")))
  145. (for-each (lambda (module)
  146. (hash-set! index (module-name module) module))
  147. (list-modules nm))))
  148. input-paths)
  149. index))
  150. (define* (patch-dependencies #:key inputs #:allow-other-keys)
  151. (define index (index-modules (map cdr inputs)))
  152. (define resolve-dependencies
  153. (match-lambda
  154. (('@ . alist)
  155. (cons '@ (map (match-lambda
  156. ((key . value)
  157. (cons key (hash-ref index key value))))
  158. alist)))))
  159. (with-atomic-json-file-replacement "package.json"
  160. (lambda (pkg-meta)
  161. (jsobject-update*
  162. pkg-meta
  163. `("devDependencies" ,resolve-dependencies (@))
  164. `("dependencies" ,(lambda (deps)
  165. (resolve-dependencies
  166. (jsobject-union
  167. (lambda (k a b) b)
  168. (jsobject-ref pkg-meta "peerDependencies" '(@))
  169. deps)))
  170. (@)))))
  171. #t)
  172. (define (delete-dependencies absent)
  173. "Rewrite 'package.json' to allow the build to proceed without packages
  174. listed in ABSENT, a list of strings naming npm packages.
  175. To prevent the deleted dependencies from being reintroduced, use this function
  176. only after the 'patch-dependencies' phase."
  177. (define delete-from-jsobject
  178. (match-lambda
  179. (('@ . alist)
  180. (cons '@ (filter (match-lambda
  181. ((k . _)
  182. (not (member k absent))))
  183. alist)))))
  184. (with-atomic-json-file-replacement "package.json"
  185. (lambda (pkg-meta)
  186. (jsobject-update*
  187. pkg-meta
  188. `("devDependencies" ,delete-from-jsobject (@))
  189. `("dependencies" ,delete-from-jsobject (@))))))
  190. (define* (delete-lockfiles #:key inputs #:allow-other-keys)
  191. "Delete 'package-lock.json', 'yarn.lock', and 'npm-shrinkwrap.json', if they
  192. exist."
  193. (for-each (lambda (pth)
  194. (when (file-exists? pth)
  195. (delete-file pth)))
  196. '("package-lock.json"
  197. "yarn.lock"
  198. "npm-shrinkwrap.json"))
  199. #t)
  200. (define* (configure #:key outputs inputs #:allow-other-keys)
  201. (let ((npm (string-append (assoc-ref inputs "node") "/bin/npm")))
  202. (invoke npm "--offline" "--ignore-scripts" "--install-links" "install")
  203. #t))
  204. (define* (build #:key inputs #:allow-other-keys)
  205. (let ((package-meta (call-with-input-file "package.json" read-json)))
  206. (if (jsobject-ref (jsobject-ref package-meta "scripts" '(@)) "build" #f)
  207. (let ((npm (string-append (assoc-ref inputs "node") "/bin/npm")))
  208. (invoke npm "run" "build"))
  209. (format #t "there is no build script to run~%"))
  210. #t))
  211. (define* (check #:key tests? inputs #:allow-other-keys)
  212. "Run 'npm test' if TESTS?"
  213. (if tests?
  214. (let ((npm (string-append (assoc-ref inputs "node") "/bin/npm")))
  215. (invoke npm "test"))
  216. (format #t "test suite not run~%"))
  217. #t)
  218. (define* (repack #:key inputs #:allow-other-keys)
  219. (invoke "tar"
  220. ;; Add options suggested by https://reproducible-builds.org/docs/archives/
  221. "--sort=name"
  222. (string-append "--mtime=@" (getenv "SOURCE_DATE_EPOCH"))
  223. "--owner=0"
  224. "--group=0"
  225. "--numeric-owner"
  226. "-czf" "../package.tgz" ".")
  227. #t)
  228. (define* (install #:key outputs inputs #:allow-other-keys)
  229. "Install the node module to the output store item."
  230. (let ((out (assoc-ref outputs "out"))
  231. (npm (string-append (assoc-ref inputs "node") "/bin/npm")))
  232. (invoke npm "--prefix" out
  233. "--global"
  234. "--offline"
  235. "--loglevel" "info"
  236. "--production"
  237. "--install-links"
  238. "install" "../package.tgz")
  239. #t))
  240. (define* (avoid-node-gyp-rebuild #:key outputs #:allow-other-keys)
  241. "Adjust the installed 'package.json' to remove an 'install' script that
  242. would try to run 'node-gyp rebuild'."
  243. ;; We want to take advantage of `npm install`'s automatic support for
  244. ;; building native addons with node-gyp: in particular, it helps us avoid
  245. ;; hard-coding the specifics of how npm's internal copy of node-gyp is
  246. ;; currently packaged. However, the mechanism by which the automatic support
  247. ;; is implemented causes problems for us.
  248. ;;
  249. ;; If a package contains a 'binding.gyp' file and does not define an
  250. ;; 'install' or 'preinstall' script, 'npm install' runs a default install
  251. ;; script consisting of 'node-gyp rebuild'. In our 'install' phase, this
  252. ;; implicit 'install' script, if it is applicable, is explicitly added to
  253. ;; the "package.json" file. However, if another Guix package were to use a
  254. ;; Node.js package with such an 'install' script, the dependent package's
  255. ;; build process would fail, because 'node-gyp rebuild' would try to write
  256. ;; to the store.
  257. ;;
  258. ;; Here, if the installed "package.json" defines scripts.install as
  259. ;; "node-gyp rebuild", we replace it with a no-op. Importantly, deleting the
  260. ;; install script definition would not be enough, because the default
  261. ;; install script would cause the same problem.
  262. ;;
  263. ;; For further details, see:
  264. ;; - https://docs.npmjs.com/cli/v8/configuring-npm/package-json#default-values
  265. ;; - https://docs.npmjs.com/cli/v8/using-npm/scripts#best-practices
  266. (define installed-package.json
  267. (search-input-file outputs (string-append "/lib/node_modules/"
  268. (module-name ".")
  269. "/package.json")))
  270. ;; We don't want to use an atomic replacement here, because we often don't
  271. ;; even need to overwrite this file. Therefore, let's use some helpers
  272. ;; that we'd otherwise not need.
  273. (define pkg-meta
  274. (call-with-input-file installed-package.json read-json))
  275. (define scripts
  276. (jsobject-ref pkg-meta "scripts" '(@)))
  277. (define (jsobject-set js key val)
  278. (jsobject-update* js (list key (const val))))
  279. (when (equal? "node-gyp rebuild" (jsobject-ref scripts "install" #f))
  280. (call-with-output-file installed-package.json
  281. (lambda (out)
  282. (write-json
  283. (jsobject-set pkg-meta
  284. "scripts"
  285. (jsobject-set scripts
  286. "install"
  287. "echo Guix: avoiding node-gyp rebuild"))
  288. out)))))
  289. (define %standard-phases
  290. (modify-phases gnu:%standard-phases
  291. (add-after 'unpack 'set-home set-home)
  292. (add-before 'configure 'patch-dependencies patch-dependencies)
  293. (add-after 'patch-dependencies 'delete-lockfiles delete-lockfiles)
  294. (replace 'configure configure)
  295. (replace 'build build)
  296. (replace 'check check)
  297. (add-before 'install 'repack repack)
  298. (replace 'install install)
  299. (add-after 'install 'avoid-node-gyp-rebuild avoid-node-gyp-rebuild)))
  300. (define* (node-build #:key inputs (phases %standard-phases)
  301. #:allow-other-keys #:rest args)
  302. (apply gnu:gnu-build #:inputs inputs #:phases phases args))