utils.scm 33 KB

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