scripts.scm 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2014 Deck Pickard <deck.r.pickard@gmail.com>
  4. ;;; Copyright © 2015, 2016 Alex Kost <alezost@gmail.com>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (guix scripts)
  21. #:use-module (guix grafts)
  22. #:use-module (guix utils)
  23. #:use-module (guix ui)
  24. #:use-module (guix store)
  25. #:use-module (guix monads)
  26. #:use-module (guix packages)
  27. #:use-module (guix derivations)
  28. #:use-module (srfi srfi-1)
  29. #:use-module (srfi srfi-37)
  30. #:use-module (ice-9 match)
  31. #:export (args-fold*
  32. parse-command-line
  33. maybe-build
  34. build-package
  35. build-package-source))
  36. ;;; Commentary:
  37. ;;;
  38. ;;; General code for Guix scripts.
  39. ;;;
  40. ;;; Code:
  41. (define (args-fold* options unrecognized-option-proc operand-proc . seeds)
  42. "A wrapper on top of `args-fold' that does proper user-facing error
  43. reporting."
  44. (catch 'misc-error
  45. (lambda ()
  46. (apply args-fold options unrecognized-option-proc
  47. operand-proc seeds))
  48. (lambda (key proc msg args . rest)
  49. ;; XXX: MSG is not i18n'd.
  50. (leave (_ "invalid argument: ~a~%")
  51. (apply format #f msg args)))))
  52. (define (environment-build-options)
  53. "Return additional build options passed as environment variables."
  54. (arguments-from-environment-variable "GUIX_BUILD_OPTIONS"))
  55. (define %default-argument-handler
  56. ;; The default handler for non-option command-line arguments.
  57. (lambda (arg result)
  58. (alist-cons 'argument arg result)))
  59. (define* (parse-command-line args options seeds
  60. #:key
  61. (argument-handler %default-argument-handler))
  62. "Parse the command-line arguments ARGS as well as arguments passed via the
  63. 'GUIX_BUILD_OPTIONS' environment variable according to OPTIONS (a list of
  64. SRFI-37 options) and return the result, seeded by SEEDS.
  65. Command-line options take precedence those passed via 'GUIX_BUILD_OPTIONS'.
  66. ARGUMENT-HANDLER is called for non-option arguments, like the 'operand-proc'
  67. parameter of 'args-fold'."
  68. (define (parse-options-from args seeds)
  69. ;; Actual parsing takes place here.
  70. (apply args-fold* args options
  71. (lambda (opt name arg . rest)
  72. (leave (_ "~A: unrecognized option~%") name))
  73. argument-handler
  74. seeds))
  75. (call-with-values
  76. (lambda ()
  77. (parse-options-from (environment-build-options) seeds))
  78. (lambda seeds
  79. ;; ARGS take precedence over what the environment variable specifies.
  80. (parse-options-from args seeds))))
  81. (define* (maybe-build drvs
  82. #:key dry-run? use-substitutes?)
  83. "Show what will/would be built, and actually build DRVS, unless DRY-RUN? is
  84. true."
  85. (with-monad %store-monad
  86. (>>= (show-what-to-build* drvs
  87. #:dry-run? dry-run?
  88. #:use-substitutes? use-substitutes?)
  89. (lambda (_)
  90. (if dry-run?
  91. (return #f)
  92. (built-derivations drvs))))))
  93. (define* (build-package package
  94. #:key dry-run? (use-substitutes? #t)
  95. #:allow-other-keys
  96. #:rest build-options)
  97. "Build PACKAGE using BUILD-OPTIONS acceptable by 'set-build-options'.
  98. Show what and how will/would be built."
  99. (mlet %store-monad ((grafting? ((lift0 %graft? %store-monad))))
  100. (apply set-build-options*
  101. #:use-substitutes? use-substitutes?
  102. (strip-keyword-arguments '(#:dry-run?) build-options))
  103. (mlet %store-monad ((derivation (package->derivation
  104. package #:graft? (and (not dry-run?)
  105. grafting?))))
  106. (mbegin %store-monad
  107. (maybe-build (list derivation)
  108. #:use-substitutes? use-substitutes?
  109. #:dry-run? dry-run?)
  110. (return (show-derivation-outputs derivation))))))
  111. (define* (build-package-source package
  112. #:key dry-run? (use-substitutes? #t)
  113. #:allow-other-keys
  114. #:rest build-options)
  115. "Build PACKAGE source using BUILD-OPTIONS."
  116. (mbegin %store-monad
  117. (apply set-build-options*
  118. #:use-substitutes? use-substitutes?
  119. (strip-keyword-arguments '(#:dry-run?) build-options))
  120. (mlet %store-monad ((derivation (origin->derivation
  121. (package-source package))))
  122. (mbegin %store-monad
  123. (maybe-build (list derivation)
  124. #:use-substitutes? use-substitutes?
  125. #:dry-run? dry-run?)
  126. (return (show-derivation-outputs derivation))))))
  127. ;;; scripts.scm ends here