cmake.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013-2015, 2020-2022 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 search-paths)
  27. #:use-module (guix build-system)
  28. #:use-module (guix build-system gnu)
  29. #:use-module (guix packages)
  30. #:export (%cmake-build-system-modules
  31. cmake-build
  32. cmake-build-system))
  33. ;; Commentary:
  34. ;;
  35. ;; Standard build procedure for packages using CMake. This is implemented as an
  36. ;; extension of `gnu-build-system'.
  37. ;;
  38. ;; Code:
  39. (define %cmake-build-system-modules
  40. ;; Build-side modules imported by default.
  41. `((guix build cmake-build-system)
  42. ,@%gnu-build-system-modules))
  43. (define (default-cmake target)
  44. "Return the default CMake package."
  45. ;; Do not use `@' to avoid introducing circular dependencies.
  46. (let ((module (resolve-interface '(gnu packages cmake))))
  47. (module-ref module
  48. (if target
  49. 'cmake-minimal-cross
  50. 'cmake-minimal))))
  51. (define* (lower name
  52. #:key source inputs native-inputs outputs system target
  53. (cmake (default-cmake target))
  54. #:allow-other-keys
  55. #:rest arguments)
  56. "Return a bag for NAME."
  57. (define private-keywords
  58. `(#:cmake #:inputs #:native-inputs
  59. ,@(if target '() '(#:target))))
  60. (bag
  61. (name name)
  62. (system system)
  63. (target target)
  64. (build-inputs `(,@(if source
  65. `(("source" ,source))
  66. '())
  67. ,@`(("cmake" ,cmake))
  68. ,@native-inputs
  69. ,@(if target '() inputs)
  70. ,@(if target
  71. ;; Use the standard cross inputs of
  72. ;; 'gnu-build-system'.
  73. (standard-cross-packages target 'host)
  74. '())
  75. ;; Keep the standard inputs of 'gnu-build-system'.
  76. ,@(standard-packages)))
  77. (host-inputs (if target inputs '()))
  78. ;; The cross-libc is really a target package, but for bootstrapping
  79. ;; reasons, we can't put it in 'host-inputs'. Namely, 'cross-gcc' is a
  80. ;; native package, so it would end up using a "native" variant of
  81. ;; 'cross-libc' (built with 'gnu-build'), whereas all the other packages
  82. ;; would use a target variant (built with 'gnu-cross-build'.)
  83. (target-inputs (if target
  84. (standard-cross-packages target 'target)
  85. '()))
  86. (outputs outputs)
  87. (build (if target cmake-cross-build cmake-build))
  88. (arguments (strip-keyword-arguments private-keywords arguments))))
  89. (define* (cmake-build name inputs
  90. #:key guile source
  91. (outputs '("out")) (configure-flags ''())
  92. (search-paths '())
  93. (make-flags ''())
  94. (out-of-source? #t)
  95. (build-type "RelWithDebInfo")
  96. (tests? #t)
  97. (test-target "test")
  98. (parallel-build? #t) (parallel-tests? #t)
  99. (validate-runpath? #t)
  100. (patch-shebangs? #t)
  101. (strip-binaries? #t)
  102. (strip-flags %strip-flags)
  103. (strip-directories %strip-directories)
  104. (phases '%standard-phases)
  105. (system (%current-system))
  106. (substitutable? #t)
  107. (imported-modules %cmake-build-system-modules)
  108. (modules '((guix build cmake-build-system)
  109. (guix build utils)))
  110. disallowed-references)
  111. "Build SOURCE using CMAKE, and with INPUTS. This assumes that SOURCE
  112. provides a 'CMakeLists.txt' file as its build system."
  113. (define build
  114. (with-imported-modules imported-modules
  115. #~(begin
  116. (use-modules #$@(sexp->gexp modules))
  117. #$(with-build-variables inputs outputs
  118. #~(cmake-build #:source #+source
  119. #:system #$system
  120. #:outputs %outputs
  121. #:inputs %build-inputs
  122. #:search-paths '#$(sexp->gexp
  123. (map search-path-specification->sexp
  124. search-paths))
  125. #:phases #$(if (pair? phases)
  126. (sexp->gexp phases)
  127. phases)
  128. #:configure-flags #$(if (pair? configure-flags)
  129. (sexp->gexp configure-flags)
  130. configure-flags)
  131. #:make-flags #$make-flags
  132. #:out-of-source? #$out-of-source?
  133. #:build-type #$build-type
  134. #:tests? #$tests?
  135. #:test-target #$test-target
  136. #:parallel-build? #$parallel-build?
  137. #:parallel-tests? #$parallel-tests?
  138. #:validate-runpath? #$validate-runpath?
  139. #:patch-shebangs? #$patch-shebangs?
  140. #:strip-binaries? #$strip-binaries?
  141. #:strip-flags #$strip-flags
  142. #:strip-directories #$strip-directories)))))
  143. (mlet %store-monad ((guile (package->derivation (or guile (default-guile))
  144. system #:graft? #f)))
  145. (gexp->derivation name build
  146. #:system system
  147. #:target #f
  148. #:graft? #f
  149. #:substitutable? substitutable?
  150. #:disallowed-references disallowed-references
  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-flags)
  174. (strip-directories %strip-directories)
  175. (phases '%standard-phases)
  176. (substitutable? #t)
  177. (system (%current-system))
  178. (build (nix-system->gnu-triplet system))
  179. (imported-modules %cmake-build-system-modules)
  180. (modules '((guix build cmake-build-system)
  181. (guix build utils)))
  182. disallowed-references)
  183. "Cross-build NAME using CMAKE for TARGET, where TARGET is a GNU triplet and
  184. with INPUTS. This assumes that SOURCE provides a 'CMakeLists.txt' file as its
  185. build system."
  186. (define builder
  187. (with-imported-modules imported-modules
  188. #~(begin
  189. (use-modules #$@(sexp->gexp modules))
  190. (define %build-host-inputs
  191. #+(input-tuples->gexp build-inputs))
  192. (define %build-target-inputs
  193. (append #$(input-tuples->gexp host-inputs)
  194. #+(input-tuples->gexp target-inputs)))
  195. (define %build-inputs
  196. (append %build-host-inputs %build-target-inputs))
  197. (define %outputs
  198. #$(outputs->gexp outputs))
  199. (cmake-build #:source #+source
  200. #:system #$system
  201. #:build #$build
  202. #:target #$target
  203. #:outputs %outputs
  204. #:inputs %build-target-inputs
  205. #:native-inputs %build-host-inputs
  206. #:search-paths '#$(map search-path-specification->sexp
  207. search-paths)
  208. #:native-search-paths '#$(map
  209. search-path-specification->sexp
  210. native-search-paths)
  211. #:phases #$phases
  212. #:configure-flags #$configure-flags
  213. #:make-flags #$make-flags
  214. #:out-of-source? #$out-of-source?
  215. #:build-type #$build-type
  216. #:tests? #$tests?
  217. #:test-target #$test-target
  218. #:parallel-build? #$parallel-build?
  219. #:parallel-tests? #$parallel-tests?
  220. #:validate-runpath? #$validate-runpath?
  221. #:patch-shebangs? #$patch-shebangs?
  222. #:make-dynamic-linker-cache? #f ;cross-compiling
  223. #:strip-binaries? #$strip-binaries?
  224. #:strip-flags #$strip-flags
  225. #:strip-directories #$strip-directories))))
  226. (mlet %store-monad ((guile (package->derivation (or guile (default-guile))
  227. system #:graft? #f)))
  228. (gexp->derivation name builder
  229. #:system system
  230. #:target target
  231. #:graft? #f
  232. #:substitutable? substitutable?
  233. #:guile-for-build guile)))
  234. (define cmake-build-system
  235. (build-system
  236. (name 'cmake)
  237. (description "The standard CMake build system")
  238. (lower lower)))
  239. ;;; cmake.scm ends here