cmake.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2013 Cyril Roelandt <tipecaml@gmail.com>
  4. ;;; Copyright © 2017 Ricardo Wurmus <rekado@elephly.net>
  5. ;;; Copyright © 2020 Efraim Flashner <efraim@flashner.co.il>
  6. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  21. (define-module (guix build-system cmake)
  22. #:use-module (guix store)
  23. #:use-module (guix gexp)
  24. #:use-module (guix utils)
  25. #:use-module (guix monads)
  26. #:use-module (guix derivations)
  27. #:use-module (guix search-paths)
  28. #:use-module (guix build-system)
  29. #:use-module (guix build-system gnu)
  30. #:use-module (guix packages)
  31. #:use-module (ice-9 match)
  32. #:export (%cmake-build-system-modules
  33. cmake-build
  34. cmake-build-system))
  35. ;; Commentary:
  36. ;;
  37. ;; Standard build procedure for packages using CMake. This is implemented as an
  38. ;; extension of `gnu-build-system'.
  39. ;;
  40. ;; Code:
  41. (define %cmake-build-system-modules
  42. ;; Build-side modules imported by default.
  43. `((guix build cmake-build-system)
  44. ,@%gnu-build-system-modules))
  45. (define (default-cmake target)
  46. "Return the default CMake package."
  47. ;; Do not use `@' to avoid introducing circular dependencies.
  48. (let ((module (resolve-interface '(gnu packages cmake))))
  49. (module-ref module
  50. (if target
  51. 'cmake-minimal-cross
  52. 'cmake-minimal))))
  53. (define* (lower name
  54. #:key source inputs native-inputs outputs system target
  55. (cmake (default-cmake target))
  56. #:allow-other-keys
  57. #:rest arguments)
  58. "Return a bag for NAME."
  59. (define private-keywords
  60. `(#:cmake #:inputs #:native-inputs
  61. ,@(if target '() '(#:target))))
  62. (bag
  63. (name name)
  64. (system system)
  65. (target target)
  66. (build-inputs `(,@(if source
  67. `(("source" ,source))
  68. '())
  69. ,@`(("cmake" ,cmake))
  70. ,@native-inputs
  71. ,@(if target '() inputs)
  72. ,@(if target
  73. ;; Use the standard cross inputs of
  74. ;; 'gnu-build-system'.
  75. (standard-cross-packages target 'host)
  76. '())
  77. ;; Keep the standard inputs of 'gnu-build-system'.
  78. ,@(standard-packages)))
  79. (host-inputs (if target inputs '()))
  80. ;; The cross-libc is really a target package, but for bootstrapping
  81. ;; reasons, we can't put it in 'host-inputs'. Namely, 'cross-gcc' is a
  82. ;; native package, so it would end up using a "native" variant of
  83. ;; 'cross-libc' (built with 'gnu-build'), whereas all the other packages
  84. ;; would use a target variant (built with 'gnu-cross-build'.)
  85. (target-inputs (if target
  86. (standard-cross-packages target 'target)
  87. '()))
  88. (outputs outputs)
  89. (build (if target cmake-cross-build cmake-build))
  90. (arguments (strip-keyword-arguments private-keywords arguments))))
  91. (define* (cmake-build name inputs
  92. #:key guile source
  93. (outputs '("out")) (configure-flags ''())
  94. (search-paths '())
  95. (make-flags ''())
  96. (out-of-source? #t)
  97. (build-type "RelWithDebInfo")
  98. (tests? #t)
  99. (test-target "test")
  100. (parallel-build? #t) (parallel-tests? #t)
  101. (validate-runpath? #t)
  102. (patch-shebangs? #t)
  103. (strip-binaries? #t)
  104. (strip-flags ''("--strip-debug"))
  105. (strip-directories ''("lib" "lib64" "libexec"
  106. "bin" "sbin"))
  107. (phases '%standard-phases)
  108. (system (%current-system))
  109. (substitutable? #t)
  110. (imported-modules %cmake-build-system-modules)
  111. (modules '((guix build cmake-build-system)
  112. (guix build utils))))
  113. "Build SOURCE using CMAKE, and with INPUTS. This assumes that SOURCE
  114. provides a 'CMakeLists.txt' file as its build system."
  115. (define build
  116. (with-imported-modules imported-modules
  117. #~(begin
  118. (use-modules #$@(sexp->gexp modules))
  119. #$(with-build-variables inputs outputs
  120. #~(cmake-build #:source #+source
  121. #:system #$system
  122. #:outputs %outputs
  123. #:inputs %build-inputs
  124. #:search-paths '#$(sexp->gexp
  125. (map search-path-specification->sexp
  126. search-paths))
  127. #:phases #$(if (pair? phases)
  128. (sexp->gexp phases)
  129. phases)
  130. #:configure-flags #$(if (pair? configure-flags)
  131. (sexp->gexp configure-flags)
  132. configure-flags)
  133. #:make-flags #$make-flags
  134. #:out-of-source? #$out-of-source?
  135. #:build-type #$build-type
  136. #:tests? #$tests?
  137. #:test-target #$test-target
  138. #:parallel-build? #$parallel-build?
  139. #:parallel-tests? #$parallel-tests?
  140. #:validate-runpath? #$validate-runpath?
  141. #:patch-shebangs? #$patch-shebangs?
  142. #:strip-binaries? #$strip-binaries?
  143. #:strip-flags #$(sexp->gexp strip-flags)
  144. #:strip-directories #$(sexp->gexp strip-directories))))))
  145. (mlet %store-monad ((guile (package->derivation (or guile (default-guile))
  146. system #:graft? #f)))
  147. (gexp->derivation name build
  148. #:system system
  149. #:target #f
  150. #:substitutable? substitutable?
  151. #:guile-for-build guile)))
  152. ;;;
  153. ;;; Cross-compilation.
  154. ;;;
  155. (define* (cmake-cross-build name
  156. #:key
  157. target
  158. build-inputs target-inputs host-inputs
  159. source guile
  160. (outputs '("out"))
  161. (configure-flags ''())
  162. (search-paths '())
  163. (native-search-paths '())
  164. (make-flags ''())
  165. (out-of-source? #t)
  166. (build-type "RelWithDebInfo")
  167. (tests? #f) ; nothing can be done
  168. (test-target "test")
  169. (parallel-build? #t) (parallel-tests? #t)
  170. (validate-runpath? #t)
  171. (patch-shebangs? #t)
  172. (strip-binaries? #t)
  173. (strip-flags ''("--strip-debug"
  174. "--enable-deterministic-archives"))
  175. (strip-directories ''("lib" "lib64" "libexec"
  176. "bin" "sbin"))
  177. (phases '%standard-phases)
  178. (substitutable? #t)
  179. (system (%current-system))
  180. (build (nix-system->gnu-triplet system))
  181. (imported-modules %cmake-build-system-modules)
  182. (modules '((guix build cmake-build-system)
  183. (guix build utils))))
  184. "Cross-build NAME using CMAKE for TARGET, where TARGET is a GNU triplet and
  185. with INPUTS. This assumes that SOURCE provides a 'CMakeLists.txt' file as its
  186. build system."
  187. (define builder
  188. (with-imported-modules imported-modules
  189. #~(begin
  190. (use-modules #$@(sexp->gexp modules))
  191. (define %build-host-inputs
  192. #+(input-tuples->gexp build-inputs))
  193. (define %build-target-inputs
  194. (append #$(input-tuples->gexp host-inputs)
  195. #+(input-tuples->gexp target-inputs)))
  196. (define %outputs
  197. #$(outputs->gexp outputs))
  198. (cmake-build #:source #+source
  199. #:system #$system
  200. #:build #$build
  201. #:target #$target
  202. #:outputs %outputs
  203. #:inputs %build-target-inputs
  204. #:native-inputs %build-host-inputs
  205. #:search-paths '#$(map search-path-specification->sexp
  206. search-paths)
  207. #:native-search-paths '#$(map
  208. search-path-specification->sexp
  209. native-search-paths)
  210. #:phases #$phases
  211. #:configure-flags #$configure-flags
  212. #:make-flags #$make-flags
  213. #:out-of-source? #$out-of-source?
  214. #:build-type #$build-type
  215. #:tests? #$tests?
  216. #:test-target #$test-target
  217. #:parallel-build? #$parallel-build?
  218. #:parallel-tests? #$parallel-tests?
  219. #:validate-runpath? #$validate-runpath?
  220. #:patch-shebangs? #$patch-shebangs?
  221. #:strip-binaries? #$strip-binaries?
  222. #:strip-flags #$strip-flags
  223. #:strip-directories #$strip-directories))))
  224. (mlet %store-monad ((guile (package->derivation (or guile (default-guile))
  225. system #:graft? #f)))
  226. (gexp->derivation name builder
  227. #:system system
  228. #:target target
  229. #:substitutable? substitutable?
  230. #:guile-for-build guile)))
  231. (define cmake-build-system
  232. (build-system
  233. (name 'cmake)
  234. (description "The standard CMake build system")
  235. (lower lower)))
  236. ;;; cmake.scm ends here