cmake.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 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. #:graft? #f
  151. #:substitutable? substitutable?
  152. #:guile-for-build guile)))
  153. ;;;
  154. ;;; Cross-compilation.
  155. ;;;
  156. (define* (cmake-cross-build name
  157. #:key
  158. target
  159. build-inputs target-inputs host-inputs
  160. source guile
  161. (outputs '("out"))
  162. (configure-flags ''())
  163. (search-paths '())
  164. (native-search-paths '())
  165. (make-flags ''())
  166. (out-of-source? #t)
  167. (build-type "RelWithDebInfo")
  168. (tests? #f) ; nothing can be done
  169. (test-target "test")
  170. (parallel-build? #t) (parallel-tests? #t)
  171. (validate-runpath? #t)
  172. (patch-shebangs? #t)
  173. (strip-binaries? #t)
  174. (strip-flags ''("--strip-debug"
  175. "--enable-deterministic-archives"))
  176. (strip-directories ''("lib" "lib64" "libexec"
  177. "bin" "sbin"))
  178. (phases '%standard-phases)
  179. (substitutable? #t)
  180. (system (%current-system))
  181. (build (nix-system->gnu-triplet system))
  182. (imported-modules %cmake-build-system-modules)
  183. (modules '((guix build cmake-build-system)
  184. (guix build utils))))
  185. "Cross-build NAME using CMAKE for TARGET, where TARGET is a GNU triplet and
  186. with INPUTS. This assumes that SOURCE provides a 'CMakeLists.txt' file as its
  187. build system."
  188. (define builder
  189. (with-imported-modules imported-modules
  190. #~(begin
  191. (use-modules #$@(sexp->gexp modules))
  192. (define %build-host-inputs
  193. #+(input-tuples->gexp build-inputs))
  194. (define %build-target-inputs
  195. (append #$(input-tuples->gexp host-inputs)
  196. #+(input-tuples->gexp target-inputs)))
  197. (define %build-inputs
  198. (append %build-host-inputs %build-target-inputs))
  199. (define %outputs
  200. #$(outputs->gexp outputs))
  201. (cmake-build #:source #+source
  202. #:system #$system
  203. #:build #$build
  204. #:target #$target
  205. #:outputs %outputs
  206. #:inputs %build-target-inputs
  207. #:native-inputs %build-host-inputs
  208. #:search-paths '#$(map search-path-specification->sexp
  209. search-paths)
  210. #:native-search-paths '#$(map
  211. search-path-specification->sexp
  212. native-search-paths)
  213. #:phases #$phases
  214. #:configure-flags #$configure-flags
  215. #:make-flags #$make-flags
  216. #:out-of-source? #$out-of-source?
  217. #:build-type #$build-type
  218. #:tests? #$tests?
  219. #:test-target #$test-target
  220. #:parallel-build? #$parallel-build?
  221. #:parallel-tests? #$parallel-tests?
  222. #:validate-runpath? #$validate-runpath?
  223. #:patch-shebangs? #$patch-shebangs?
  224. #:make-dynamic-linker-cache? #f ;cross-compiling
  225. #:strip-binaries? #$strip-binaries?
  226. #:strip-flags #$strip-flags
  227. #:strip-directories #$strip-directories))))
  228. (mlet %store-monad ((guile (package->derivation (or guile (default-guile))
  229. system #:graft? #f)))
  230. (gexp->derivation name builder
  231. #:system system
  232. #:target target
  233. #:graft? #f
  234. #:substitutable? substitutable?
  235. #:guile-for-build guile)))
  236. (define cmake-build-system
  237. (build-system
  238. (name 'cmake)
  239. (description "The standard CMake build system")
  240. (lower lower)))
  241. ;;; cmake.scm ends here