cmake.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2020 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 utils)
  24. #:use-module (guix derivations)
  25. #:use-module (guix search-paths)
  26. #:use-module (guix build-system)
  27. #:use-module (guix build-system gnu)
  28. #:use-module (guix packages)
  29. #:use-module (ice-9 match)
  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. `(#:source #:cmake #:inputs #:native-inputs #:outputs
  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 store name inputs
  90. #:key (guile #f)
  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-debug"))
  103. (strip-directories ''("lib" "lib64" "libexec"
  104. "bin" "sbin"))
  105. (phases '(@ (guix build cmake-build-system)
  106. %standard-phases))
  107. (system (%current-system))
  108. (substitutable? #t)
  109. (imported-modules %cmake-build-system-modules)
  110. (modules '((guix build cmake-build-system)
  111. (guix build utils))))
  112. "Build SOURCE using CMAKE, and with INPUTS. This assumes that SOURCE
  113. provides a 'CMakeLists.txt' file as its build system."
  114. (define builder
  115. `(begin
  116. (use-modules ,@modules)
  117. (cmake-build #:source ,(match (assoc-ref inputs "source")
  118. (((? derivation? source))
  119. (derivation->output-path source))
  120. ((source)
  121. source)
  122. (source
  123. source))
  124. #:system ,system
  125. #:outputs %outputs
  126. #:inputs %build-inputs
  127. #:search-paths ',(map search-path-specification->sexp
  128. search-paths)
  129. #:phases ,phases
  130. #:configure-flags ,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. (define guile-for-build
  144. (match guile
  145. ((? package?)
  146. (package-derivation store guile system #:graft? #f))
  147. (#f ; the default
  148. (let* ((distro (resolve-interface '(gnu packages commencement)))
  149. (guile (module-ref distro 'guile-final)))
  150. (package-derivation store guile system #:graft? #f)))))
  151. (build-expression->derivation store name builder
  152. #:system system
  153. #:inputs inputs
  154. #:modules imported-modules
  155. #:outputs outputs
  156. #:substitutable? substitutable?
  157. #:guile-for-build guile-for-build))
  158. ;;;
  159. ;;; Cross-compilation.
  160. ;;;
  161. (define* (cmake-cross-build store name
  162. #:key
  163. target native-drvs target-drvs
  164. (guile #f)
  165. (outputs '("out"))
  166. (configure-flags ''())
  167. (search-paths '())
  168. (native-search-paths '())
  169. (make-flags ''())
  170. (out-of-source? #t)
  171. (build-type "RelWithDebInfo")
  172. (tests? #f) ; nothing can be done
  173. (test-target "test")
  174. (parallel-build? #t) (parallel-tests? #t)
  175. (validate-runpath? #t)
  176. (patch-shebangs? #t)
  177. (strip-binaries? #t)
  178. (strip-flags ''("--strip-debug"
  179. "--enable-deterministic-archives"))
  180. (strip-directories ''("lib" "lib64" "libexec"
  181. "bin" "sbin"))
  182. (phases '(@ (guix build cmake-build-system)
  183. %standard-phases))
  184. (substitutable? #t)
  185. (system (%current-system))
  186. (build (nix-system->gnu-triplet system))
  187. (imported-modules %cmake-build-system-modules)
  188. (modules '((guix build cmake-build-system)
  189. (guix build utils))))
  190. "Cross-build NAME using CMAKE for TARGET, where TARGET is a GNU triplet and
  191. with INPUTS. This assumes that SOURCE provides a 'CMakeLists.txt' file as its
  192. build system."
  193. (define builder
  194. `(begin
  195. (use-modules ,@modules)
  196. (let ()
  197. (define %build-host-inputs
  198. ',(map (match-lambda
  199. ((name (? derivation? drv) sub ...)
  200. `(,name . ,(apply derivation->output-path drv sub)))
  201. ((name path)
  202. `(,name . ,path)))
  203. native-drvs))
  204. (define %build-target-inputs
  205. ',(map (match-lambda
  206. ((name (? derivation? drv) sub ...)
  207. `(,name . ,(apply derivation->output-path drv sub)))
  208. ((name (? package? pkg) sub ...)
  209. (let ((drv (package-cross-derivation store pkg
  210. target system)))
  211. `(,name . ,(apply derivation->output-path drv sub))))
  212. ((name path)
  213. `(,name . ,path)))
  214. target-drvs))
  215. (cmake-build #:source ,(match (assoc-ref native-drvs "source")
  216. (((? derivation? source))
  217. (derivation->output-path source))
  218. ((source)
  219. source)
  220. (source
  221. source))
  222. #:system ,system
  223. #:build ,build
  224. #:target ,target
  225. #:outputs %outputs
  226. #:inputs %build-target-inputs
  227. #:native-inputs %build-host-inputs
  228. #:search-paths ',(map search-path-specification->sexp
  229. search-paths)
  230. #:native-search-paths ',(map
  231. search-path-specification->sexp
  232. native-search-paths)
  233. #:phases ,phases
  234. #:configure-flags ,configure-flags
  235. #:make-flags ,make-flags
  236. #:out-of-source? ,out-of-source?
  237. #:build-type ,build-type
  238. #:tests? ,tests?
  239. #:test-target ,test-target
  240. #:parallel-build? ,parallel-build?
  241. #:parallel-tests? ,parallel-tests?
  242. #:validate-runpath? ,validate-runpath?
  243. #:patch-shebangs? ,patch-shebangs?
  244. #:strip-binaries? ,strip-binaries?
  245. #:strip-flags ,strip-flags
  246. #:strip-directories ,strip-directories))))
  247. (define guile-for-build
  248. (match guile
  249. ((? package?)
  250. (package-derivation store guile system #:graft? #f))
  251. (#f ; the default
  252. (let* ((distro (resolve-interface '(gnu packages commencement)))
  253. (guile (module-ref distro 'guile-final)))
  254. (package-derivation store guile system #:graft? #f)))))
  255. (build-expression->derivation store name builder
  256. #:system system
  257. #:inputs (append native-drvs target-drvs)
  258. #:outputs outputs
  259. #:modules imported-modules
  260. #:substitutable? substitutable?
  261. #:guile-for-build guile-for-build))
  262. (define cmake-build-system
  263. (build-system
  264. (name 'cmake)
  265. (description "The standard CMake build system")
  266. (lower lower)))
  267. ;;; cmake.scm ends here