cmake.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2013 Cyril Roelandt <tipecaml@gmail.com>
  4. ;;; Copyright © 2017 Ricardo Wurmus <rekado@elephly.net>
  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 build-system cmake)
  21. #:use-module (guix store)
  22. #:use-module (guix utils)
  23. #:use-module (guix derivations)
  24. #:use-module (guix search-paths)
  25. #:use-module (guix build-system)
  26. #:use-module (guix build-system gnu)
  27. #:use-module (guix packages)
  28. #:use-module (ice-9 match)
  29. #:export (%cmake-build-system-modules
  30. cmake-build
  31. cmake-build-system))
  32. ;; Commentary:
  33. ;;
  34. ;; Standard build procedure for packages using CMake. This is implemented as an
  35. ;; extension of `gnu-build-system'.
  36. ;;
  37. ;; Code:
  38. (define %cmake-build-system-modules
  39. ;; Build-side modules imported by default.
  40. `((guix build cmake-build-system)
  41. ,@%gnu-build-system-modules))
  42. (define (default-cmake)
  43. "Return the default CMake package."
  44. ;; Do not use `@' to avoid introducing circular dependencies.
  45. (let ((module (resolve-interface '(gnu packages cmake))))
  46. (module-ref module 'cmake-minimal)))
  47. (define* (lower name
  48. #:key source inputs native-inputs outputs system target
  49. (cmake (default-cmake))
  50. #:allow-other-keys
  51. #:rest arguments)
  52. "Return a bag for NAME."
  53. (define private-keywords
  54. `(#:source #:cmake #:inputs #:native-inputs #:outputs
  55. ,@(if target '() '(#:target))))
  56. (bag
  57. (name name)
  58. (system system)
  59. (target target)
  60. (build-inputs `(,@(if source
  61. `(("source" ,source))
  62. '())
  63. ,@`(("cmake" ,cmake))
  64. ,@native-inputs
  65. ,@(if target
  66. ;; Use the standard cross inputs of
  67. ;; 'gnu-build-system'.
  68. (standard-cross-packages target 'host)
  69. '())
  70. ;; Keep the standard inputs of 'gnu-build-system'.
  71. ,@(standard-packages)))
  72. (host-inputs inputs)
  73. ;; The cross-libc is really a target package, but for bootstrapping
  74. ;; reasons, we can't put it in 'host-inputs'. Namely, 'cross-gcc' is a
  75. ;; native package, so it would end up using a "native" variant of
  76. ;; 'cross-libc' (built with 'gnu-build'), whereas all the other packages
  77. ;; would use a target variant (built with 'gnu-cross-build'.)
  78. (target-inputs (if target
  79. (standard-cross-packages target 'target)
  80. '()))
  81. (outputs outputs)
  82. (build (if target cmake-cross-build cmake-build))
  83. (arguments (strip-keyword-arguments private-keywords arguments))))
  84. (define* (cmake-build store name inputs
  85. #:key (guile #f)
  86. (outputs '("out")) (configure-flags ''())
  87. (search-paths '())
  88. (make-flags ''())
  89. (out-of-source? #t)
  90. (build-type "RelWithDebInfo")
  91. (tests? #t)
  92. (test-target "test")
  93. (parallel-build? #t) (parallel-tests? #f)
  94. (validate-runpath? #t)
  95. (patch-shebangs? #t)
  96. (strip-binaries? #t)
  97. (strip-flags ''("--strip-debug"))
  98. (strip-directories ''("lib" "lib64" "libexec"
  99. "bin" "sbin"))
  100. (phases '(@ (guix build cmake-build-system)
  101. %standard-phases))
  102. (system (%current-system))
  103. (imported-modules %cmake-build-system-modules)
  104. (modules '((guix build cmake-build-system)
  105. (guix build utils))))
  106. "Build SOURCE using CMAKE, and with INPUTS. This assumes that SOURCE
  107. provides a 'CMakeLists.txt' file as its build system."
  108. (define builder
  109. `(begin
  110. (use-modules ,@modules)
  111. (cmake-build #:source ,(match (assoc-ref inputs "source")
  112. (((? derivation? source))
  113. (derivation->output-path source))
  114. ((source)
  115. source)
  116. (source
  117. source))
  118. #:system ,system
  119. #:outputs %outputs
  120. #:inputs %build-inputs
  121. #:search-paths ',(map search-path-specification->sexp
  122. search-paths)
  123. #:phases ,phases
  124. #:configure-flags ,configure-flags
  125. #:make-flags ,make-flags
  126. #:out-of-source? ,out-of-source?
  127. #:build-type ,build-type
  128. #:tests? ,tests?
  129. #:test-target ,test-target
  130. #:parallel-build? ,parallel-build?
  131. #:parallel-tests? ,parallel-tests?
  132. #:validate-runpath? ,validate-runpath?
  133. #:patch-shebangs? ,patch-shebangs?
  134. #:strip-binaries? ,strip-binaries?
  135. #:strip-flags ,strip-flags
  136. #:strip-directories ,strip-directories)))
  137. (define guile-for-build
  138. (match guile
  139. ((? package?)
  140. (package-derivation store guile system #:graft? #f))
  141. (#f ; the default
  142. (let* ((distro (resolve-interface '(gnu packages commencement)))
  143. (guile (module-ref distro 'guile-final)))
  144. (package-derivation store guile system #:graft? #f)))))
  145. (build-expression->derivation store name builder
  146. #:system system
  147. #:inputs inputs
  148. #:modules imported-modules
  149. #:outputs outputs
  150. #:guile-for-build guile-for-build))
  151. ;;;
  152. ;;; Cross-compilation.
  153. ;;;
  154. (define* (cmake-cross-build store name
  155. #:key
  156. target native-drvs target-drvs
  157. (guile #f)
  158. (outputs '("out"))
  159. (configure-flags ''())
  160. (search-paths '())
  161. (native-search-paths '())
  162. (make-flags ''())
  163. (out-of-source? #t)
  164. (build-type "RelWithDebInfo")
  165. (tests? #f) ; nothing can be done
  166. (test-target "test")
  167. (parallel-build? #t) (parallel-tests? #f)
  168. (validate-runpath? #t)
  169. (patch-shebangs? #t)
  170. (strip-binaries? #t)
  171. (strip-flags ''("--strip-debug"
  172. "--enable-deterministic-archives"))
  173. (strip-directories ''("lib" "lib64" "libexec"
  174. "bin" "sbin"))
  175. (phases '(@ (guix build cmake-build-system)
  176. %standard-phases))
  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. "Cross-build NAME using CMAKE for TARGET, where TARGET is a GNU triplet and
  183. with INPUTS. This assumes that SOURCE provides a 'CMakeLists.txt' file as its
  184. build system."
  185. (define builder
  186. `(begin
  187. (use-modules ,@modules)
  188. (let ()
  189. (define %build-host-inputs
  190. ',(map (match-lambda
  191. ((name (? derivation? drv) sub ...)
  192. `(,name . ,(apply derivation->output-path drv sub)))
  193. ((name path)
  194. `(,name . ,path)))
  195. native-drvs))
  196. (define %build-target-inputs
  197. ',(map (match-lambda
  198. ((name (? derivation? drv) sub ...)
  199. `(,name . ,(apply derivation->output-path drv sub)))
  200. ((name (? package? pkg) sub ...)
  201. (let ((drv (package-cross-derivation store pkg
  202. target system)))
  203. `(,name . ,(apply derivation->output-path drv sub))))
  204. ((name path)
  205. `(,name . ,path)))
  206. target-drvs))
  207. (cmake-build #:source ,(match (assoc-ref native-drvs "source")
  208. (((? derivation? source))
  209. (derivation->output-path source))
  210. ((source)
  211. source)
  212. (source
  213. source))
  214. #:system ,system
  215. #:build ,build
  216. #:target ,target
  217. #:outputs %outputs
  218. #:inputs %build-target-inputs
  219. #:native-inputs %build-host-inputs
  220. #:search-paths ',(map search-path-specification->sexp
  221. search-paths)
  222. #:native-search-paths ',(map
  223. search-path-specification->sexp
  224. native-search-paths)
  225. #:phases ,phases
  226. #:configure-flags ,configure-flags
  227. #:make-flags ,make-flags
  228. #:out-of-source? ,out-of-source?
  229. #:build-type ,build-type
  230. #:tests? ,tests?
  231. #:test-target ,test-target
  232. #:parallel-build? ,parallel-build?
  233. #:parallel-tests? ,parallel-tests?
  234. #:validate-runpath? ,validate-runpath?
  235. #:patch-shebangs? ,patch-shebangs?
  236. #:strip-binaries? ,strip-binaries?
  237. #:strip-flags ,strip-flags
  238. #:strip-directories ,strip-directories))))
  239. (define guile-for-build
  240. (match guile
  241. ((? package?)
  242. (package-derivation store guile system #:graft? #f))
  243. (#f ; the default
  244. (let* ((distro (resolve-interface '(gnu packages commencement)))
  245. (guile (module-ref distro 'guile-final)))
  246. (package-derivation store guile system #:graft? #f)))))
  247. (build-expression->derivation store name builder
  248. #:system system
  249. #:inputs (append native-drvs target-drvs)
  250. #:outputs outputs
  251. #:modules imported-modules
  252. #:guile-for-build guile-for-build))
  253. (define cmake-build-system
  254. (build-system
  255. (name 'cmake)
  256. (description "The standard CMake build system")
  257. (lower lower)))
  258. ;;; cmake.scm ends here