pkg-cmake.mk 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. ################################################################################
  2. # CMake package infrastructure
  3. #
  4. # This file implements an infrastructure that eases development of
  5. # package .mk files for CMake packages. It should be used for all
  6. # packages that use CMake as their build system.
  7. #
  8. # See the Buildroot documentation for details on the usage of this
  9. # infrastructure
  10. #
  11. # In terms of implementation, this CMake infrastructure requires
  12. # the .mk file to only specify metadata information about the
  13. # package: name, version, download URL, etc.
  14. #
  15. # We still allow the package .mk file to override what the different
  16. # steps are doing, if needed. For example, if <PKG>_BUILD_CMDS is
  17. # already defined, it is used as the list of commands to perform to
  18. # build the package, instead of the default CMake behaviour. The
  19. # package can also define some post operation hooks.
  20. #
  21. ################################################################################
  22. # Set compiler variables.
  23. ifeq ($(BR2_CCACHE),y)
  24. CMAKE_HOST_C_COMPILER = $(HOST_DIR)/bin/ccache
  25. CMAKE_HOST_CXX_COMPILER = $(HOST_DIR)/bin/ccache
  26. CMAKE_HOST_C_COMPILER_ARG1 = $(HOSTCC_NOCCACHE)
  27. CMAKE_HOST_CXX_COMPILER_ARG1 = $(HOSTCXX_NOCCACHE)
  28. else
  29. CMAKE_HOST_C_COMPILER = $(HOSTCC)
  30. CMAKE_HOST_CXX_COMPILER = $(HOSTCXX)
  31. endif
  32. ifneq ($(QUIET),)
  33. CMAKE_QUIET = -DCMAKE_RULE_MESSAGES=OFF -DCMAKE_INSTALL_MESSAGE=NEVER
  34. endif
  35. ################################################################################
  36. # inner-cmake-package -- defines how the configuration, compilation and
  37. # installation of a CMake package should be done, implements a few hooks to
  38. # tune the build process and calls the generic package infrastructure to
  39. # generate the necessary make targets
  40. #
  41. # argument 1 is the lowercase package name
  42. # argument 2 is the uppercase package name, including a HOST_ prefix
  43. # for host packages
  44. # argument 3 is the uppercase package name, without the HOST_ prefix
  45. # for host packages
  46. # argument 4 is the type (target or host)
  47. ################################################################################
  48. define inner-cmake-package
  49. $(2)_CONF_ENV ?=
  50. $(2)_CONF_OPTS ?=
  51. $(2)_MAKE ?= $$(MAKE)
  52. $(2)_MAKE_ENV ?=
  53. $(2)_MAKE_OPTS ?=
  54. $(2)_INSTALL_OPTS ?= install
  55. $(2)_INSTALL_STAGING_OPTS ?= DESTDIR=$$(STAGING_DIR) install/fast
  56. $(2)_INSTALL_TARGET_OPTS ?= DESTDIR=$$(TARGET_DIR) install/fast
  57. $(3)_SUPPORTS_IN_SOURCE_BUILD ?= YES
  58. ifeq ($$($(3)_SUPPORTS_IN_SOURCE_BUILD),YES)
  59. $(2)_BUILDDIR = $$($(2)_SRCDIR)
  60. else
  61. $(2)_BUILDDIR = $$($(2)_SRCDIR)/buildroot-build
  62. endif
  63. #
  64. # Configure step. Only define it if not already defined by the package
  65. # .mk file. And take care of the differences between host and target
  66. # packages.
  67. #
  68. ifndef $(2)_CONFIGURE_CMDS
  69. ifeq ($(4),target)
  70. # Configure package for target
  71. #
  72. # - We are passing BUILD_SHARED_LIBS because it is documented as a
  73. # standard CMake variable to control the build of shared libraries
  74. # (see https://cmake.org/cmake/help/v3.8/manual/cmake-variables.7.html#variables-that-change-behavior)
  75. # - We are not passing BUILD_STATIC_LIBS because it is *not*
  76. # documented as a standard CMake variable. If a package supports it,
  77. # it must handle it explicitly.
  78. #
  79. define $(2)_CONFIGURE_CMDS
  80. (mkdir -p $$($$(PKG)_BUILDDIR) && \
  81. cd $$($$(PKG)_BUILDDIR) && \
  82. rm -f CMakeCache.txt && \
  83. PATH=$$(BR_PATH) \
  84. $$($$(PKG)_CONF_ENV) $$(BR2_CMAKE) $$($$(PKG)_SRCDIR) \
  85. -DCMAKE_TOOLCHAIN_FILE="$$(HOST_DIR)/share/buildroot/toolchainfile.cmake" \
  86. -DCMAKE_INSTALL_PREFIX="/usr" \
  87. -DCMAKE_COLOR_MAKEFILE=OFF \
  88. -DBUILD_DOC=OFF \
  89. -DBUILD_DOCS=OFF \
  90. -DBUILD_EXAMPLE=OFF \
  91. -DBUILD_EXAMPLES=OFF \
  92. -DBUILD_TEST=OFF \
  93. -DBUILD_TESTS=OFF \
  94. -DBUILD_TESTING=OFF \
  95. -DBUILD_SHARED_LIBS=$$(if $$(BR2_STATIC_LIBS),OFF,ON) \
  96. $$(CMAKE_QUIET) \
  97. $$($$(PKG)_CONF_OPTS) \
  98. )
  99. endef
  100. else
  101. # Configure package for host
  102. define $(2)_CONFIGURE_CMDS
  103. (mkdir -p $$($$(PKG)_BUILDDIR) && \
  104. cd $$($$(PKG)_BUILDDIR) && \
  105. rm -f CMakeCache.txt && \
  106. PATH=$$(BR_PATH) \
  107. PKG_CONFIG="$$(PKG_CONFIG_HOST_BINARY)" \
  108. PKG_CONFIG_SYSROOT_DIR="/" \
  109. PKG_CONFIG_LIBDIR="$$(HOST_DIR)/lib/pkgconfig:$$(HOST_DIR)/share/pkgconfig" \
  110. PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=1 \
  111. PKG_CONFIG_ALLOW_SYSTEM_LIBS=1 \
  112. $$($$(PKG)_CONF_ENV) $$(BR2_CMAKE) $$($$(PKG)_SRCDIR) \
  113. -DCMAKE_INSTALL_SO_NO_EXE=0 \
  114. -DCMAKE_FIND_ROOT_PATH="$$(HOST_DIR)" \
  115. -DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM="BOTH" \
  116. -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY="BOTH" \
  117. -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE="BOTH" \
  118. -DCMAKE_INSTALL_PREFIX="$$(HOST_DIR)" \
  119. -DCMAKE_C_FLAGS="$$(HOST_CFLAGS)" \
  120. -DCMAKE_CXX_FLAGS="$$(HOST_CXXFLAGS)" \
  121. -DCMAKE_EXE_LINKER_FLAGS="$$(HOST_LDFLAGS)" \
  122. -DCMAKE_SHARED_LINKER_FLAGS="$$(HOST_LDFLAGS)" \
  123. -DCMAKE_ASM_COMPILER="$$(HOSTAS)" \
  124. -DCMAKE_C_COMPILER="$$(CMAKE_HOST_C_COMPILER)" \
  125. -DCMAKE_CXX_COMPILER="$$(CMAKE_HOST_CXX_COMPILER)" \
  126. $(if $$(CMAKE_HOST_C_COMPILER_ARG1),\
  127. -DCMAKE_C_COMPILER_ARG1="$$(CMAKE_HOST_C_COMPILER_ARG1)" \
  128. -DCMAKE_CXX_COMPILER_ARG1="$$(CMAKE_HOST_CXX_COMPILER_ARG1)" \
  129. ) \
  130. -DCMAKE_COLOR_MAKEFILE=OFF \
  131. -DBUILD_DOC=OFF \
  132. -DBUILD_DOCS=OFF \
  133. -DBUILD_EXAMPLE=OFF \
  134. -DBUILD_EXAMPLES=OFF \
  135. -DBUILD_TEST=OFF \
  136. -DBUILD_TESTS=OFF \
  137. -DBUILD_TESTING=OFF \
  138. $$(CMAKE_QUIET) \
  139. $$($$(PKG)_CONF_OPTS) \
  140. )
  141. endef
  142. endif
  143. endif
  144. # Since some CMake modules (even upstream ones) use pgk_check_modules
  145. # primitives to find {C,LD}FLAGS, add it to the dependency list.
  146. $(2)_DEPENDENCIES += host-pkgconf
  147. $(2)_DEPENDENCIES += $(BR2_CMAKE_HOST_DEPENDENCY)
  148. #
  149. # Build step. Only define it if not already defined by the package .mk
  150. # file.
  151. #
  152. ifndef $(2)_BUILD_CMDS
  153. ifeq ($(4),target)
  154. define $(2)_BUILD_CMDS
  155. $$(TARGET_MAKE_ENV) $$($$(PKG)_MAKE_ENV) $$($$(PKG)_MAKE) $$($$(PKG)_MAKE_OPTS) -C $$($$(PKG)_BUILDDIR)
  156. endef
  157. else
  158. define $(2)_BUILD_CMDS
  159. $$(HOST_MAKE_ENV) $$($$(PKG)_MAKE_ENV) $$($$(PKG)_MAKE) $$($$(PKG)_MAKE_OPTS) -C $$($$(PKG)_BUILDDIR)
  160. endef
  161. endif
  162. endif
  163. #
  164. # Host installation step. Only define it if not already defined by the
  165. # package .mk file.
  166. #
  167. ifndef $(2)_INSTALL_CMDS
  168. define $(2)_INSTALL_CMDS
  169. $$(HOST_MAKE_ENV) $$($$(PKG)_MAKE_ENV) $$($$(PKG)_MAKE) $$($$(PKG)_MAKE_OPTS) $$($$(PKG)_INSTALL_OPTS) -C $$($$(PKG)_BUILDDIR)
  170. endef
  171. endif
  172. #
  173. # Staging installation step. Only define it if not already defined by
  174. # the package .mk file.
  175. #
  176. ifndef $(2)_INSTALL_STAGING_CMDS
  177. define $(2)_INSTALL_STAGING_CMDS
  178. $$(TARGET_MAKE_ENV) $$($$(PKG)_MAKE_ENV) $$($$(PKG)_MAKE) $$($$(PKG)_MAKE_OPTS) $$($$(PKG)_INSTALL_STAGING_OPTS) -C $$($$(PKG)_BUILDDIR)
  179. endef
  180. endif
  181. #
  182. # Target installation step. Only define it if not already defined by
  183. # the package .mk file.
  184. #
  185. ifndef $(2)_INSTALL_TARGET_CMDS
  186. define $(2)_INSTALL_TARGET_CMDS
  187. $$(TARGET_MAKE_ENV) $$($$(PKG)_MAKE_ENV) $$($$(PKG)_MAKE) $$($$(PKG)_MAKE_OPTS) $$($$(PKG)_INSTALL_TARGET_OPTS) -C $$($$(PKG)_BUILDDIR)
  188. endef
  189. endif
  190. # Call the generic package infrastructure to generate the necessary
  191. # make targets
  192. $(call inner-generic-package,$(1),$(2),$(3),$(4))
  193. endef
  194. ################################################################################
  195. # cmake-package -- the target generator macro for CMake packages
  196. ################################################################################
  197. cmake-package = $(call inner-cmake-package,$(pkgname),$(call UPPERCASE,$(pkgname)),$(call UPPERCASE,$(pkgname)),target)
  198. host-cmake-package = $(call inner-cmake-package,host-$(pkgname),$(call UPPERCASE,host-$(pkgname)),$(call UPPERCASE,$(pkgname)),host)
  199. ################################################################################
  200. # Generation of the CMake toolchain file
  201. ################################################################################
  202. # CMAKE_SYSTEM_PROCESSOR should match uname -m
  203. ifeq ($(BR2_ARM_CPU_ARMV4),y)
  204. CMAKE_SYSTEM_PROCESSOR_ARM_VARIANT = armv4
  205. else ifeq ($(BR2_ARM_CPU_ARMV5),y)
  206. CMAKE_SYSTEM_PROCESSOR_ARM_VARIANT = armv5
  207. else ifeq ($(BR2_ARM_CPU_ARMV6),y)
  208. CMAKE_SYSTEM_PROCESSOR_ARM_VARIANT = armv6
  209. else ifeq ($(BR2_ARM_CPU_ARMV7A),y)
  210. CMAKE_SYSTEM_PROCESSOR_ARM_VARIANT = armv7
  211. else ifeq ($(BR2_ARM_CPU_ARMV8A),y)
  212. CMAKE_SYSTEM_PROCESSOR_ARM_VARIANT = armv8
  213. endif
  214. ifeq ($(BR2_arm),y)
  215. CMAKE_SYSTEM_PROCESSOR = $(CMAKE_SYSTEM_PROCESSOR_ARM_VARIANT)l
  216. else ifeq ($(BR2_armeb),y)
  217. CMAKE_SYSTEM_PROCESSOR = $(CMAKE_SYSTEM_PROCESSOR_ARM_VARIANT)b
  218. else ifeq ($(call qstrip,$(BR2_ARCH)),powerpc64)
  219. CMAKE_SYSTEM_PROCESSOR = ppc64
  220. else ifeq ($(call qstrip,$(BR2_ARCH)),powerpc64le)
  221. CMAKE_SYSTEM_PROCESSOR = ppc64le
  222. else
  223. CMAKE_SYSTEM_PROCESSOR = $(BR2_ARCH)
  224. endif
  225. # In order to allow the toolchain to be relocated, we calculate the HOST_DIR
  226. # based on the toolchainfile.cmake file's location: $(HOST_DIR)/share/buildroot
  227. # In all the other variables, HOST_DIR will be replaced by RELOCATED_HOST_DIR,
  228. # so we have to strip "$(HOST_DIR)/" from the paths that contain it.
  229. define TOOLCHAIN_CMAKE_INSTALL_FILES
  230. @mkdir -p $(HOST_DIR)/share/buildroot
  231. sed \
  232. -e 's#@@STAGING_SUBDIR@@#$(call qstrip,$(STAGING_SUBDIR))#' \
  233. -e 's#@@TARGET_CFLAGS@@#$(call qstrip,$(TARGET_CFLAGS))#' \
  234. -e 's#@@TARGET_CXXFLAGS@@#$(call qstrip,$(TARGET_CXXFLAGS))#' \
  235. -e 's#@@TARGET_FCFLAGS@@#$(call qstrip,$(TARGET_FCFLAGS))#' \
  236. -e 's#@@TARGET_LDFLAGS@@#$(call qstrip,$(TARGET_LDFLAGS))#' \
  237. -e 's#@@TARGET_CC@@#$(subst $(HOST_DIR)/,,$(call qstrip,$(TARGET_CC)))#' \
  238. -e 's#@@TARGET_CXX@@#$(subst $(HOST_DIR)/,,$(call qstrip,$(TARGET_CXX)))#' \
  239. -e 's#@@TARGET_FC@@#$(subst $(HOST_DIR)/,,$(call qstrip,$(TARGET_FC)))#' \
  240. -e 's#@@CMAKE_SYSTEM_PROCESSOR@@#$(call qstrip,$(CMAKE_SYSTEM_PROCESSOR))#' \
  241. -e 's#@@TOOLCHAIN_HAS_FORTRAN@@#$(if $(BR2_TOOLCHAIN_HAS_FORTRAN),1,0)#' \
  242. -e 's#@@CMAKE_BUILD_TYPE@@#$(if $(BR2_ENABLE_DEBUG),Debug,Release)#' \
  243. $(TOPDIR)/support/misc/toolchainfile.cmake.in \
  244. > $(HOST_DIR)/share/buildroot/toolchainfile.cmake
  245. $(Q)$(INSTALL) -D -m 0644 support/misc/Buildroot.cmake \
  246. $(HOST_DIR)/share/buildroot/Platform/Buildroot.cmake
  247. endef
  248. TOOLCHAIN_POST_INSTALL_STAGING_HOOKS += TOOLCHAIN_CMAKE_INSTALL_FILES