utils.scm 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2013, 2014, 2015 Mark H Weaver <mhw@netris.org>
  4. ;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org>
  5. ;;; Copyright © 2014 Ian Denhardt <ian@zenhack.net>
  6. ;;; Copyright © 2016 Mathieu Lirzin <mthl@gnu.org>
  7. ;;; Copyright © 2015 David Thompson <davet@gnu.org>
  8. ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
  9. ;;; Copyright © 2018, 2020 Marius Bakke <marius@gnu.org>
  10. ;;; Copyright © 2020 Efraim Flashner <efraim@flashner.co.il>
  11. ;;; Copyright © 2021 Simon Tournier <zimon.toutoune@gmail.com>
  12. ;;; Copyright © 2021 Chris Marusich <cmmarusich@gmail.com>
  13. ;;;
  14. ;;; This file is part of GNU Guix.
  15. ;;;
  16. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  17. ;;; under the terms of the GNU General Public License as published by
  18. ;;; the Free Software Foundation; either version 3 of the License, or (at
  19. ;;; your option) any later version.
  20. ;;;
  21. ;;; GNU Guix is distributed in the hope that it will be useful, but
  22. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  23. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. ;;; GNU General Public License for more details.
  25. ;;;
  26. ;;; You should have received a copy of the GNU General Public License
  27. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  28. (define-module (guix utils)
  29. #:use-module (guix config)
  30. #:use-module (srfi srfi-1)
  31. #:use-module (srfi srfi-9)
  32. #:use-module (srfi srfi-11)
  33. #:use-module (srfi srfi-26)
  34. #:use-module (srfi srfi-39)
  35. #:use-module (ice-9 ftw)
  36. #:use-module (rnrs io ports) ;need 'port-position' etc.
  37. #:use-module ((rnrs bytevectors) #:select (bytevector-u8-set!))
  38. #:use-module (guix memoization)
  39. #:use-module ((guix build utils) #:select (dump-port mkdir-p delete-file-recursively))
  40. #:use-module ((guix build syscalls) #:select (mkdtemp! fdatasync))
  41. #:use-module ((guix combinators) #:select (fold2))
  42. #:use-module (guix diagnostics) ;<location>, &error-location, etc.
  43. #:use-module (ice-9 format)
  44. #:use-module (ice-9 regex)
  45. #:use-module (ice-9 match)
  46. #:use-module (ice-9 format)
  47. #:use-module ((ice-9 iconv) #:prefix iconv:)
  48. #:autoload (zlib) (make-zlib-input-port make-zlib-output-port)
  49. #:use-module (system foreign)
  50. #:re-export (<location> ;for backwards compatibility
  51. location
  52. location?
  53. location-file
  54. location-line
  55. location-column
  56. source-properties->location
  57. location->source-properties
  58. &error-location
  59. error-location?
  60. error-location
  61. &fix-hint
  62. fix-hint?
  63. condition-fix-hint)
  64. #:export (strip-keyword-arguments
  65. default-keyword-arguments
  66. substitute-keyword-arguments
  67. ensure-keyword-arguments
  68. current-source-directory
  69. nix-system->gnu-triplet
  70. gnu-triplet->nix-system
  71. %current-system
  72. %current-target-system
  73. package-name->name+version
  74. target-mingw?
  75. target-arm32?
  76. target-aarch64?
  77. target-arm?
  78. target-powerpc?
  79. target-64bit?
  80. cc-for-target
  81. cxx-for-target
  82. pkg-config-for-target
  83. version-compare
  84. version>?
  85. version>=?
  86. version-prefix
  87. version-major+minor+point
  88. version-major+minor
  89. version-major
  90. version-unique-prefix
  91. guile-version>?
  92. version-prefix?
  93. string-replace-substring
  94. file-extension
  95. file-sans-extension
  96. tarball-sans-extension
  97. compressed-file?
  98. switch-symlinks
  99. call-with-temporary-output-file
  100. call-with-temporary-directory
  101. with-atomic-file-output
  102. with-environment-variables
  103. arguments-from-environment-variable
  104. config-directory
  105. cache-directory
  106. readlink*
  107. edit-expression
  108. filtered-port
  109. decompressed-port
  110. call-with-decompressed-port
  111. compressed-output-port
  112. call-with-compressed-output-port
  113. canonical-newline-port
  114. string-distance
  115. string-closest))
  116. ;;;
  117. ;;; Environment variables.
  118. ;;;
  119. (define (call-with-environment-variables variables thunk)
  120. "Call THUNK with the environment VARIABLES set."
  121. (let ((environment (environ)))
  122. (dynamic-wind
  123. (lambda ()
  124. (for-each (match-lambda
  125. ((variable value)
  126. (setenv variable value)))
  127. variables))
  128. thunk
  129. (lambda ()
  130. (environ environment)))))
  131. (define-syntax-rule (with-environment-variables variables exp ...)
  132. "Evaluate EXP with the given environment VARIABLES set."
  133. (call-with-environment-variables variables
  134. (lambda () exp ...)))
  135. (define (arguments-from-environment-variable variable)
  136. "Retrieve value of environment variable denoted by string VARIABLE in the
  137. form of a list of strings (`char-set:graphic' tokens) suitable for consumption
  138. by `args-fold', if VARIABLE is defined, otherwise return an empty list."
  139. (let ((env (getenv variable)))
  140. (if env
  141. (string-tokenize env char-set:graphic)
  142. '())))
  143. ;;;
  144. ;;; Filtering & pipes.
  145. ;;;
  146. (define (filtered-port command input)
  147. "Return an input port where data drained from INPUT is filtered through
  148. COMMAND (a list). In addition, return a list of PIDs that the caller must
  149. wait. When INPUT is a file port, it must be unbuffered; otherwise, any
  150. buffered data is lost."
  151. (let loop ((input input)
  152. (pids '()))
  153. (if (file-port? input)
  154. (match (pipe)
  155. ((in . out)
  156. (match (primitive-fork)
  157. (0
  158. (dynamic-wind
  159. (const #f)
  160. (lambda ()
  161. (close-port in)
  162. (close-port (current-input-port))
  163. (dup2 (fileno input) 0)
  164. (close-port (current-output-port))
  165. (dup2 (fileno out) 1)
  166. (catch 'system-error
  167. (lambda ()
  168. (apply execl (car command) command))
  169. (lambda args
  170. (format (current-error-port)
  171. "filtered-port: failed to execute '~{~a ~}': ~a~%"
  172. command (strerror (system-error-errno args))))))
  173. (lambda ()
  174. (primitive-_exit 1))))
  175. (child
  176. (close-port out)
  177. (values in (cons child pids))))))
  178. ;; INPUT is not a file port, so fork just for the sake of tunneling it
  179. ;; through a file port.
  180. (match (pipe)
  181. ((in . out)
  182. (match (primitive-fork)
  183. (0
  184. (dynamic-wind
  185. (const #t)
  186. (lambda ()
  187. (close-port in)
  188. (dump-port input out))
  189. (lambda ()
  190. (close-port input)
  191. (false-if-exception (close out))
  192. (primitive-_exit 0))))
  193. (child
  194. (close-port input)
  195. (close-port out)
  196. (loop in (cons child pids)))))))))
  197. (define (lzip-port proc port . args)
  198. "Return the lzip port produced by calling PROC (a symbol) on PORT and ARGS.
  199. Raise an error if lzlib support is missing."
  200. (let ((make-port (module-ref (resolve-interface '(lzlib)) proc)))
  201. (make-port port)))
  202. (define (zstd-port proc port . args)
  203. "Return the zstd port produced by calling PROC (a symbol) on PORT and ARGS.
  204. Raise an error if zstd support is missing."
  205. (let ((make-port (module-ref (resolve-interface '(zstd)) proc)))
  206. (make-port port)))
  207. (define (decompressed-port compression input)
  208. "Return an input port where INPUT is decompressed according to COMPRESSION,
  209. a symbol such as 'xz."
  210. (match compression
  211. ((or #f 'none) (values input '()))
  212. ('bzip2 (filtered-port `(,%bzip2 "-dc") input))
  213. ('xz (filtered-port `(,%xz "-dc") input))
  214. ('gzip (values (make-zlib-input-port input #:format 'gzip)
  215. '()))
  216. ('lzip (values (lzip-port 'make-lzip-input-port input)
  217. '()))
  218. ('zstd (values (zstd-port 'make-zstd-input-port input)
  219. '()))
  220. (_ (error "unsupported compression scheme" compression))))
  221. (define (call-with-decompressed-port compression port proc)
  222. "Call PROC with a wrapper around PORT, a file port, that decompresses data
  223. read from PORT according to COMPRESSION, a symbol such as 'xz."
  224. (let-values (((decompressed pids)
  225. (decompressed-port compression port)))
  226. (dynamic-wind
  227. (const #f)
  228. (lambda ()
  229. (proc decompressed))
  230. (lambda ()
  231. (close-port decompressed)
  232. (unless (every (compose zero? cdr waitpid) pids)
  233. (error "decompressed-port failure" pids))))))
  234. (define (filtered-output-port command output)
  235. "Return an output port. Data written to that port is filtered through
  236. COMMAND and written to OUTPUT, an output file port. In addition, return a
  237. list of PIDs to wait for. OUTPUT must be unbuffered; otherwise, any buffered
  238. data is lost."
  239. (match (pipe)
  240. ((in . out)
  241. (match (primitive-fork)
  242. (0
  243. (dynamic-wind
  244. (const #f)
  245. (lambda ()
  246. (close-port out)
  247. (close-port (current-input-port))
  248. (dup2 (fileno in) 0)
  249. (close-port (current-output-port))
  250. (dup2 (fileno output) 1)
  251. (catch 'system-error
  252. (lambda ()
  253. (apply execl (car command) command))
  254. (lambda args
  255. (format (current-error-port)
  256. "filtered-output-port: failed to execute '~{~a ~}': ~a~%"
  257. command (strerror (system-error-errno args))))))
  258. (lambda ()
  259. (primitive-_exit 1))))
  260. (child
  261. (close-port in)
  262. (values out (list child)))))))
  263. (define* (compressed-output-port compression output
  264. #:key (options '()))
  265. "Return an output port whose input is compressed according to COMPRESSION,
  266. a symbol such as 'xz, and then written to OUTPUT. In addition return a list
  267. of PIDs to wait for. OPTIONS is a list of strings passed to the compression
  268. program--e.g., '(\"--fast\")."
  269. (match compression
  270. ((or #f 'none) (values output '()))
  271. ('bzip2 (filtered-output-port `(,%bzip2 "-c" ,@options) output))
  272. ('xz (filtered-output-port `(,%xz "-c" ,@options) output))
  273. ('gzip (values (make-zlib-output-port output #:format 'gzip)
  274. '()))
  275. ('lzip (values (lzip-port 'make-lzip-output-port output)
  276. '()))
  277. ('zstd (values (zstd-port 'make-zstd-output-port output)
  278. '()))
  279. (_ (error "unsupported compression scheme" compression))))
  280. (define* (call-with-compressed-output-port compression port proc
  281. #:key (options '()))
  282. "Call PROC with a wrapper around PORT, a file port, that compresses data
  283. that goes to PORT according to COMPRESSION, a symbol such as 'xz. OPTIONS is
  284. a list of command-line arguments passed to the compression program."
  285. (let-values (((compressed pids)
  286. (compressed-output-port compression port
  287. #:options options)))
  288. (dynamic-wind
  289. (const #f)
  290. (lambda ()
  291. (proc compressed))
  292. (lambda ()
  293. (close-port compressed)
  294. (unless (every (compose zero? cdr waitpid) pids)
  295. (error "compressed-output-port failure" pids))))))
  296. (define* (edit-expression source-properties proc #:key (encoding "UTF-8"))
  297. "Edit the expression specified by SOURCE-PROPERTIES using PROC, which should
  298. be a procedure that takes the original expression in string and returns a new
  299. one. ENCODING will be used to interpret all port I/O, it default to UTF-8.
  300. This procedure returns #t on success."
  301. (with-fluids ((%default-port-encoding encoding))
  302. (let* ((file (assq-ref source-properties 'filename))
  303. (line (assq-ref source-properties 'line))
  304. (column (assq-ref source-properties 'column))
  305. (in (open-input-file file))
  306. ;; The start byte position of the expression.
  307. (start (begin (while (not (and (= line (port-line in))
  308. (= column (port-column in))))
  309. (when (eof-object? (read-char in))
  310. (error (format #f "~a: end of file~%" in))))
  311. (ftell in)))
  312. ;; The end byte position of the expression.
  313. (end (begin (read in) (ftell in))))
  314. (seek in 0 SEEK_SET) ; read from the beginning of the file.
  315. (let* ((pre-bv (get-bytevector-n in start))
  316. ;; The expression in string form.
  317. (str (iconv:bytevector->string
  318. (get-bytevector-n in (- end start))
  319. (port-encoding in)))
  320. (post-bv (get-bytevector-all in))
  321. (str* (proc str)))
  322. ;; Verify the edited expression is still a scheme expression.
  323. (call-with-input-string str* read)
  324. ;; Update the file with edited expression.
  325. (with-atomic-file-output file
  326. (lambda (out)
  327. (put-bytevector out pre-bv)
  328. (display str* out)
  329. ;; post-bv maybe the end-of-file object.
  330. (when (not (eof-object? post-bv))
  331. (put-bytevector out post-bv))
  332. #t))))))
  333. ;;;
  334. ;;; Keyword arguments.
  335. ;;;
  336. (define (strip-keyword-arguments keywords args)
  337. "Remove all of the keyword arguments listed in KEYWORDS from ARGS."
  338. (let loop ((args args)
  339. (result '()))
  340. (match args
  341. (()
  342. (reverse result))
  343. (((? keyword? kw) arg . rest)
  344. (loop rest
  345. (if (memq kw keywords)
  346. result
  347. (cons* arg kw result))))
  348. ((head . tail)
  349. (loop tail (cons head result))))))
  350. (define (default-keyword-arguments args defaults)
  351. "Return ARGS augmented with any keyword/value from DEFAULTS for
  352. keywords not already present in ARGS."
  353. (let loop ((defaults defaults)
  354. (args args))
  355. (match defaults
  356. ((kw value rest ...)
  357. (loop rest
  358. (if (memq kw args)
  359. args
  360. (cons* kw value args))))
  361. (()
  362. args))))
  363. (define-syntax collect-default-args
  364. (syntax-rules ()
  365. ((_)
  366. '())
  367. ((_ (_ _) rest ...)
  368. (collect-default-args rest ...))
  369. ((_ (kw _ dflt) rest ...)
  370. (cons* kw dflt (collect-default-args rest ...)))))
  371. (define-syntax substitute-keyword-arguments
  372. (syntax-rules ()
  373. "Return a new list of arguments where the value for keyword arg KW is
  374. replaced by EXP. EXP is evaluated in a context where VAR is bound to the
  375. previous value of the keyword argument, or DFLT if given."
  376. ((_ original-args ((kw var dflt ...) exp) ...)
  377. (let loop ((args (default-keyword-arguments
  378. original-args
  379. (collect-default-args (kw var dflt ...) ...)))
  380. (before '()))
  381. (match args
  382. ((kw var rest (... ...))
  383. (loop rest (cons* exp kw before)))
  384. ...
  385. ((x rest (... ...))
  386. (loop rest (cons x before)))
  387. (()
  388. (reverse before)))))))
  389. (define (delkw kw lst)
  390. "Remove KW and its associated value from LST, a keyword/value list such
  391. as '(#:foo 1 #:bar 2)."
  392. (let loop ((lst lst)
  393. (result '()))
  394. (match lst
  395. (()
  396. (reverse result))
  397. ((kw? value rest ...)
  398. (if (eq? kw? kw)
  399. (append (reverse result) rest)
  400. (loop rest (cons* value kw? result)))))))
  401. (define (ensure-keyword-arguments args kw/values)
  402. "Force the keywords arguments KW/VALUES in the keyword argument list ARGS.
  403. For instance:
  404. (ensure-keyword-arguments '(#:foo 2) '(#:foo 2))
  405. => (#:foo 2)
  406. (ensure-keyword-arguments '(#:foo 2) '(#:bar 3))
  407. => (#:foo 2 #:bar 3)
  408. (ensure-keyword-arguments '(#:foo 2) '(#:bar 3 #:foo 42))
  409. => (#:foo 42 #:bar 3)
  410. "
  411. (let loop ((args args)
  412. (kw/values kw/values)
  413. (result '()))
  414. (match args
  415. (()
  416. (append (reverse result) kw/values))
  417. ((kw value rest ...)
  418. (match (memq kw kw/values)
  419. ((_ value . _)
  420. (loop rest (delkw kw kw/values) (cons* value kw result)))
  421. (#f
  422. (loop rest kw/values (cons* value kw result))))))))
  423. ;;;
  424. ;;; System strings.
  425. ;;;
  426. (define* (nix-system->gnu-triplet
  427. #:optional (system (%current-system)) (vendor "unknown"))
  428. "Return a guess of the GNU triplet corresponding to Nix system
  429. identifier SYSTEM."
  430. (match system
  431. ("armhf-linux"
  432. (string-append "arm-" vendor "-linux-gnueabihf"))
  433. (_
  434. (let* ((dash (string-index system #\-))
  435. (arch (substring system 0 dash))
  436. (os (substring system (+ 1 dash))))
  437. (string-append arch
  438. "-" vendor "-"
  439. (if (string=? os "linux")
  440. "linux-gnu"
  441. os))))))
  442. (define (gnu-triplet->nix-system triplet)
  443. "Return the Nix system type corresponding to TRIPLET, a GNU triplet as
  444. returned by `config.guess'."
  445. (let ((triplet (cond ((string-match "^i[345]86-(.*)$" triplet)
  446. =>
  447. (lambda (m)
  448. (string-append "i686-" (match:substring m 1))))
  449. (else triplet))))
  450. (cond ((string-match "^arm[^-]*-([^-]+-)?linux-gnueabihf" triplet)
  451. "armhf-linux")
  452. ((string-match "^([^-]+)-([^-]+-)?linux-gnu.*" triplet)
  453. =>
  454. (lambda (m)
  455. ;; Nix omits `-gnu' for GNU/Linux.
  456. (string-append (match:substring m 1) "-linux")))
  457. ((string-match "^([^-]+)-([^-]+-)?([[:alpha:]]+)([0-9]+\\.?)*$" triplet)
  458. =>
  459. (lambda (m)
  460. ;; Nix strip the version number from names such as `gnu0.3',
  461. ;; `darwin10.2.0', etc., and always strips the vendor part.
  462. (string-append (match:substring m 1) "-"
  463. (match:substring m 3))))
  464. (else triplet))))
  465. (define %current-system
  466. ;; System type as expected by Nix, usually ARCHITECTURE-KERNEL.
  467. ;; By default, this is equal to (gnu-triplet->nix-system %host-type).
  468. (make-parameter %system))
  469. (define %current-target-system
  470. ;; Either #f or a GNU triplet representing the target system we are
  471. ;; cross-building to.
  472. (make-parameter #f))
  473. (define* (package-name->name+version spec
  474. #:optional (delimiter #\@))
  475. "Given SPEC, a package name like \"foo@0.9.1b\", return two values: \"foo\"
  476. and \"0.9.1b\". When the version part is unavailable, SPEC and #f are
  477. returned. Both parts must not contain any '@'. Optionally, DELIMITER can be
  478. a character other than '@'."
  479. (match (string-rindex spec delimiter)
  480. (#f (values spec #f))
  481. (idx (values (substring spec 0 idx)
  482. (substring spec (1+ idx))))))
  483. (define* (target-mingw? #:optional (target (%current-target-system)))
  484. (and target
  485. (string-suffix? "-mingw32" target)))
  486. (define* (target-arm32? #:optional (target (or (%current-target-system)
  487. (%current-system))))
  488. (string-prefix? "arm" target))
  489. (define* (target-aarch64? #:optional (target (or (%current-target-system)
  490. (%current-system))))
  491. (string-prefix? "aarch64" target))
  492. (define* (target-arm? #:optional (target (or (%current-target-system)
  493. (%current-system))))
  494. (or (target-arm32? target) (target-aarch64? target)))
  495. (define* (target-powerpc? #:optional (target (or (%current-target-system)
  496. (%current-system))))
  497. (string-prefix? "powerpc" target))
  498. (define* (target-64bit? #:optional (system (or (%current-target-system)
  499. (%current-system))))
  500. (any (cut string-prefix? <> system) '("x86_64" "aarch64" "mips64" "powerpc64")))
  501. (define* (cc-for-target #:optional (target (%current-target-system)))
  502. (if target
  503. (string-append target "-gcc")
  504. "gcc"))
  505. (define* (cxx-for-target #:optional (target (%current-target-system)))
  506. (if target
  507. (string-append target "-g++")
  508. "g++"))
  509. (define* (pkg-config-for-target #:optional (target (%current-target-system)))
  510. (if target
  511. (string-append target "-pkg-config")
  512. "pkg-config"))
  513. (define version-compare
  514. (let ((strverscmp
  515. (let ((sym (or (dynamic-func "strverscmp" (dynamic-link))
  516. (error "could not find `strverscmp' (from GNU libc)"))))
  517. (pointer->procedure int sym (list '* '*)))))
  518. (lambda (a b)
  519. "Return '> when A denotes a newer version than B,
  520. '< when A denotes a older version than B,
  521. or '= when they denote equal versions."
  522. (let ((result (strverscmp (string->pointer a) (string->pointer b))))
  523. (cond ((positive? result) '>)
  524. ((negative? result) '<)
  525. (else '=))))))
  526. (define (version-prefix version-string num-parts)
  527. "Truncate version-string to the first num-parts components of the version.
  528. For example, (version-prefix \"2.1.47.4.23\" 3) returns \"2.1.47\""
  529. (string-join (take (string-split version-string #\.) num-parts) "."))
  530. (define (version-major+minor+point version-string)
  531. "Return \"major>.<minor>.<point>\", where major, minor and point are the
  532. major, minor and point version numbers from the version-string. For example,
  533. (version-major+minor+point \"6.4.5.2\") returns \"6.4.5\" or
  534. (version-major+minor+point \"1.19.2-2581-324ca14c3003\") returns \"1.19.2\"."
  535. (let* ((3-dot (version-prefix version-string 3))
  536. (index (string-index 3-dot #\-)))
  537. (or (false-if-exception (substring 3-dot 0 index))
  538. 3-dot)))
  539. (define (version-major+minor version-string)
  540. "Return \"<major>.<minor>\", where major and minor are the major and
  541. minor version numbers from version-string."
  542. (version-prefix version-string 2))
  543. (define (version-major version-string)
  544. "Return the major version number as string from the version-string."
  545. (version-prefix version-string 1))
  546. (define (version-unique-prefix version versions)
  547. "Return the shortest version prefix to unambiguously identify VERSION among
  548. VERSIONS. For example:
  549. (version-unique-prefix \"2.0\" '(\"3.0\" \"2.0\"))
  550. => \"2\"
  551. (version-unique-prefix \"2.2\" '(\"3.0.5\" \"2.0.9\" \"2.2.7\"))
  552. => \"2.2\"
  553. (version-unique-prefix \"27.1\" '(\"27.1\"))
  554. => \"\"
  555. "
  556. (define not-dot
  557. (char-set-complement (char-set #\.)))
  558. (define other-versions
  559. (delete version versions))
  560. (let loop ((prefix '())
  561. (components (string-tokenize version not-dot)))
  562. (define prefix-str
  563. (string-join prefix "."))
  564. (if (any (cut string-prefix? prefix-str <>) other-versions)
  565. (match components
  566. ((head . tail)
  567. (loop `(,@prefix ,head) tail))
  568. (()
  569. version))
  570. prefix-str)))
  571. (define (version>? a b)
  572. "Return #t when A denotes a version strictly newer than B."
  573. (eq? '> (version-compare a b)))
  574. (define (version>=? a b)
  575. "Return #t when A denotes a version newer or equal to B."
  576. (case (version-compare a b)
  577. ((> =) #t)
  578. (else #f)))
  579. (define (guile-version>? str)
  580. "Return #t if the running Guile version is greater than STR."
  581. ;; Note: Using (version>? (version) "2.0.5") or similar doesn't work,
  582. ;; because the result of (version) can have a prefix, like "2.0.5-deb1".
  583. (version>? (string-append (major-version) "."
  584. (minor-version) "."
  585. (micro-version))
  586. str))
  587. (define version-prefix?
  588. (let ((not-dot (char-set-complement (char-set #\.))))
  589. (lambda (v1 v2)
  590. "Return true if V1 is a version prefix of V2:
  591. (version-prefix? \"4.1\" \"4.16.2\") => #f
  592. (version-prefix? \"4.1\" \"4.1.2\") => #t
  593. "
  594. (define (list-prefix? lst1 lst2)
  595. (match lst1
  596. (() #t)
  597. ((head1 tail1 ...)
  598. (match lst2
  599. (() #f)
  600. ((head2 tail2 ...)
  601. (and (equal? head1 head2)
  602. (list-prefix? tail1 tail2)))))))
  603. (list-prefix? (string-tokenize v1 not-dot)
  604. (string-tokenize v2 not-dot)))))
  605. ;;;
  606. ;;; Files.
  607. ;;;
  608. (define (file-extension file)
  609. "Return the extension of FILE or #f if there is none."
  610. (let ((dot (string-rindex file #\.)))
  611. (and dot (substring file (+ 1 dot) (string-length file)))))
  612. (define (file-sans-extension file)
  613. "Return the substring of FILE without its extension, if any."
  614. (let ((dot (string-rindex file #\.)))
  615. (if dot
  616. (substring file 0 dot)
  617. file)))
  618. (define (tarball-sans-extension tarball)
  619. "Return TARBALL without its .tar.* or .zip extension."
  620. (let ((end (or (string-contains tarball ".tar")
  621. (string-contains tarball ".zip"))))
  622. (substring tarball 0 end)))
  623. (define (compressed-file? file)
  624. "Return true if FILE denotes a compressed file."
  625. (->bool (member (file-extension file)
  626. '("gz" "bz2" "xz" "lz" "lzma" "tgz" "tbz2" "zip"))))
  627. (define (switch-symlinks link target)
  628. "Atomically switch LINK, a symbolic link, to point to TARGET. Works
  629. both when LINK already exists and when it does not."
  630. (let ((pivot (string-append link ".new")))
  631. (symlink target pivot)
  632. (rename-file pivot link)))
  633. (define* (string-replace-substring str substr replacement
  634. #:optional
  635. (start 0)
  636. (end (string-length str)))
  637. "Replace all occurrences of SUBSTR in the START--END range of STR by
  638. REPLACEMENT."
  639. (match (string-length substr)
  640. (0
  641. (error "string-replace-substring: empty substring"))
  642. (substr-length
  643. (let loop ((start start)
  644. (pieces (list (substring str 0 start))))
  645. (match (string-contains str substr start end)
  646. (#f
  647. (string-concatenate-reverse
  648. (cons (substring str start) pieces)))
  649. (index
  650. (loop (+ index substr-length)
  651. (cons* replacement
  652. (substring str start index)
  653. pieces))))))))
  654. (define (call-with-temporary-output-file proc)
  655. "Call PROC with a name of a temporary file and open output port to that
  656. file; close the file and delete it when leaving the dynamic extent of this
  657. call."
  658. (let* ((directory (or (getenv "TMPDIR") "/tmp"))
  659. (template (string-append directory "/guix-file.XXXXXX"))
  660. (out (mkstemp! template)))
  661. (dynamic-wind
  662. (lambda ()
  663. #t)
  664. (lambda ()
  665. (proc template out))
  666. (lambda ()
  667. (false-if-exception (close out))
  668. (false-if-exception (delete-file template))))))
  669. (define (call-with-temporary-directory proc)
  670. "Call PROC with a name of a temporary directory; close the directory and
  671. delete it when leaving the dynamic extent of this call."
  672. (let* ((directory (or (getenv "TMPDIR") "/tmp"))
  673. (template (string-append directory "/guix-directory.XXXXXX"))
  674. (tmp-dir (mkdtemp! template)))
  675. (dynamic-wind
  676. (const #t)
  677. (lambda ()
  678. (proc tmp-dir))
  679. (lambda ()
  680. (false-if-exception (delete-file-recursively tmp-dir))))))
  681. (define (with-atomic-file-output file proc)
  682. "Call PROC with an output port for the file that is going to replace FILE.
  683. Upon success, FILE is atomically replaced by what has been written to the
  684. output port, and PROC's result is returned."
  685. (let* ((template (string-append file ".XXXXXX"))
  686. (out (mkstemp! template)))
  687. (with-throw-handler #t
  688. (lambda ()
  689. (let ((result (proc out)))
  690. (fdatasync out)
  691. (close-port out)
  692. (rename-file template file)
  693. result))
  694. (lambda (key . args)
  695. (false-if-exception (delete-file template))
  696. (close-port out)))))
  697. (define* (xdg-directory variable suffix #:key (ensure? #t))
  698. "Return the name of the XDG directory that matches VARIABLE and SUFFIX,
  699. after making sure that it exists if ENSURE? is true. VARIABLE is an
  700. environment variable name like \"XDG_CONFIG_HOME\"; SUFFIX is a suffix like
  701. \"/.config\". Honor the XDG specs,
  702. <http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html>."
  703. (let ((dir (and=> (or (getenv variable)
  704. (and=> (or (getenv "HOME")
  705. (passwd:dir (getpwuid (getuid))))
  706. (cut string-append <> suffix)))
  707. (cut string-append <> "/guix"))))
  708. (when ensure?
  709. (mkdir-p dir))
  710. dir))
  711. (define config-directory
  712. (cut xdg-directory "XDG_CONFIG_HOME" "/.config" <...>))
  713. (define cache-directory
  714. (cut xdg-directory "XDG_CACHE_HOME" "/.cache" <...>))
  715. (define (readlink* file)
  716. "Call 'readlink' until the result is not a symlink."
  717. (define %max-symlink-depth 50)
  718. (let loop ((file file)
  719. (depth 0))
  720. (define (absolute target)
  721. (if (absolute-file-name? target)
  722. target
  723. (string-append (dirname file) "/" target)))
  724. (if (>= depth %max-symlink-depth)
  725. file
  726. (call-with-values
  727. (lambda ()
  728. (catch 'system-error
  729. (lambda ()
  730. (values #t (readlink file)))
  731. (lambda args
  732. (let ((errno (system-error-errno args)))
  733. (if (or (= errno EINVAL))
  734. (values #f file)
  735. (apply throw args))))))
  736. (lambda (success? target)
  737. (if success?
  738. (loop (absolute target) (+ depth 1))
  739. file))))))
  740. (define (canonical-newline-port port)
  741. "Return an input port that wraps PORT such that all newlines consist
  742. of a single linefeed."
  743. (define (get-position)
  744. (if (port-has-port-position? port) (port-position port) #f))
  745. (define (set-position! position)
  746. (if (port-has-set-port-position!? port)
  747. (set-port-position! position port)
  748. #f))
  749. (define (close) (close-port port))
  750. (define (read! bv start n)
  751. (let loop ((count 0)
  752. (byte (get-u8 port)))
  753. (cond ((eof-object? byte) count)
  754. ;; XXX: consume all CRs even if not followed by LF.
  755. ((eqv? byte (char->integer #\return)) (loop count (get-u8 port)))
  756. ((= count (- n 1))
  757. (bytevector-u8-set! bv (+ start count) byte)
  758. n)
  759. (else
  760. (bytevector-u8-set! bv (+ start count) byte)
  761. (loop (+ count 1) (get-u8 port))))))
  762. (make-custom-binary-input-port "canonical-newline-port"
  763. read!
  764. get-position
  765. set-position!
  766. close))
  767. ;;;
  768. ;;; Source location.
  769. ;;;
  770. (define absolute-dirname
  771. ;; Memoize to avoid repeated 'stat' storms from 'search-path'.
  772. (mlambda (file)
  773. "Return the absolute name of the directory containing FILE, or #f upon
  774. failure."
  775. (match (search-path %load-path file)
  776. (#f #f)
  777. ((? string? file)
  778. ;; If there are relative names in %LOAD-PATH, FILE can be relative and
  779. ;; needs to be canonicalized.
  780. (if (string-prefix? "/" file)
  781. (dirname file)
  782. (canonicalize-path (dirname file)))))))
  783. (define-syntax current-source-directory
  784. (lambda (s)
  785. "Return the absolute name of the current directory, or #f if it could not
  786. be determined."
  787. (syntax-case s ()
  788. ((_)
  789. (match (assq 'filename (or (syntax-source s) '()))
  790. (('filename . (? string? file-name))
  791. ;; If %FILE-PORT-NAME-CANONICALIZATION is 'relative, then FILE-NAME
  792. ;; can be relative. In that case, we try to find out at run time
  793. ;; the absolute file name by looking at %LOAD-PATH; doing this at
  794. ;; run time rather than expansion time is necessary to allow files
  795. ;; to be moved on the file system.
  796. (if (string-prefix? "/" file-name)
  797. (dirname file-name)
  798. #`(absolute-dirname #,file-name)))
  799. ((or ('filename . #f) #f)
  800. ;; raising an error would upset Geiser users
  801. #f))))))
  802. ;;;
  803. ;;; String comparison.
  804. ;;;
  805. (define (string-distance s1 s2)
  806. "Compute the Levenshtein distance between two strings."
  807. ;; Naive implemenation
  808. (define loop
  809. (mlambda (as bt)
  810. (match as
  811. (() (length bt))
  812. ((a s ...)
  813. (match bt
  814. (() (length as))
  815. ((b t ...)
  816. (if (char=? a b)
  817. (loop s t)
  818. (1+ (min
  819. (loop as t)
  820. (loop s bt)
  821. (loop s t))))))))))
  822. (let ((c1 (string->list s1))
  823. (c2 (string->list s2)))
  824. (loop c1 c2)))
  825. (define* (string-closest trial tests #:key (threshold 3))
  826. "Return the string from TESTS that is the closest from the TRIAL,
  827. according to 'string-distance'. If the TESTS are too far from TRIAL,
  828. according to THRESHOLD, then #f is returned."
  829. (identity ;discard second return value
  830. (fold2 (lambda (test closest minimal)
  831. (let ((dist (string-distance trial test)))
  832. (if (and (< dist minimal) (< dist threshold))
  833. (values test dist)
  834. (values closest minimal))))
  835. #f +inf.0
  836. tests)))
  837. ;;; Local Variables:
  838. ;;; eval: (put 'call-with-progress-reporter 'scheme-indent-function 1)
  839. ;;; End: