pkg-cmake.mk 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 = $(HOSTCC_NOCCACHE)
  25. CMAKE_HOST_CXX_COMPILER = $(HOSTCXX_NOCCACHE)
  26. CMAKE_HOST_C_COMPILER_LAUNCHER = $(HOST_DIR)/bin/ccache
  27. CMAKE_HOST_CXX_COMPILER_LAUNCHER = $(HOST_DIR)/bin/ccache
  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_LAUNCHER),\
  127. -DCMAKE_C_COMPILER_LAUNCHER="$$(CMAKE_HOST_C_COMPILER_LAUNCHER)" \
  128. -DCMAKE_CXX_COMPILER_LAUNCHER="$$(CMAKE_HOST_CXX_COMPILER_LAUNCHER)" \
  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. -DBUILD_SHARED_LIBS=ON \
  139. $$(CMAKE_QUIET) \
  140. $$($$(PKG)_CONF_OPTS) \
  141. )
  142. endef
  143. endif
  144. endif
  145. # Since some CMake modules (even upstream ones) use pgk_check_modules
  146. # primitives to find {C,LD}FLAGS, add it to the dependency list.
  147. $(2)_DEPENDENCIES += host-pkgconf
  148. $(2)_DEPENDENCIES += $(BR2_CMAKE_HOST_DEPENDENCY)
  149. #
  150. # Build step. Only define it if not already defined by the package .mk
  151. # file.
  152. #
  153. ifndef $(2)_BUILD_CMDS
  154. ifeq ($(4),target)
  155. define $(2)_BUILD_CMDS
  156. $$(TARGET_MAKE_ENV) $$($$(PKG)_MAKE_ENV) $$($$(PKG)_MAKE) $$($$(PKG)_MAKE_OPTS) -C $$($$(PKG)_BUILDDIR)
  157. endef
  158. else
  159. define $(2)_BUILD_CMDS
  160. $$(HOST_MAKE_ENV) $$($$(PKG)_MAKE_ENV) $$($$(PKG)_MAKE) $$($$(PKG)_MAKE_OPTS) -C $$($$(PKG)_BUILDDIR)
  161. endef
  162. endif
  163. endif
  164. #
  165. # Host installation step. Only define it if not already defined by the
  166. # package .mk file.
  167. #
  168. ifndef $(2)_INSTALL_CMDS
  169. define $(2)_INSTALL_CMDS
  170. $$(HOST_MAKE_ENV) $$($$(PKG)_MAKE_ENV) $$($$(PKG)_MAKE) $$($$(PKG)_MAKE_OPTS) $$($$(PKG)_INSTALL_OPTS) -C $$($$(PKG)_BUILDDIR)
  171. endef
  172. endif
  173. #
  174. # Staging installation step. Only define it if not already defined by
  175. # the package .mk file.
  176. #
  177. ifndef $(2)_INSTALL_STAGING_CMDS
  178. define $(2)_INSTALL_STAGING_CMDS
  179. $$(TARGET_MAKE_ENV) $$($$(PKG)_MAKE_ENV) $$($$(PKG)_MAKE) $$($$(PKG)_MAKE_OPTS) $$($$(PKG)_INSTALL_STAGING_OPTS) -C $$($$(PKG)_BUILDDIR)
  180. endef
  181. endif
  182. #
  183. # Target installation step. Only define it if not already defined by
  184. # the package .mk file.
  185. #
  186. ifndef $(2)_INSTALL_TARGET_CMDS
  187. define $(2)_INSTALL_TARGET_CMDS
  188. $$(TARGET_MAKE_ENV) $$($$(PKG)_MAKE_ENV) $$($$(PKG)_MAKE) $$($$(PKG)_MAKE_OPTS) $$($$(PKG)_INSTALL_TARGET_OPTS) -C $$($$(PKG)_BUILDDIR)
  189. endef
  190. endif
  191. # Call the generic package infrastructure to generate the necessary
  192. # make targets
  193. $(call inner-generic-package,$(1),$(2),$(3),$(4))
  194. endef
  195. ################################################################################
  196. # cmake-package -- the target generator macro for CMake packages
  197. ################################################################################
  198. cmake-package = $(call inner-cmake-package,$(pkgname),$(call UPPERCASE,$(pkgname)),$(call UPPERCASE,$(pkgname)),target)
  199. host-cmake-package = $(call inner-cmake-package,host-$(pkgname),$(call UPPERCASE,host-$(pkgname)),$(call UPPERCASE,$(pkgname)),host)
  200. ################################################################################
  201. # Generation of the CMake toolchain file
  202. ################################################################################
  203. # CMAKE_SYSTEM_PROCESSOR should match uname -m
  204. ifeq ($(BR2_ARM_CPU_ARMV4),y)
  205. CMAKE_SYSTEM_PROCESSOR_ARM_VARIANT = armv4
  206. else ifeq ($(BR2_ARM_CPU_ARMV5),y)
  207. CMAKE_SYSTEM_PROCESSOR_ARM_VARIANT = armv5
  208. else ifeq ($(BR2_ARM_CPU_ARMV6),y)
  209. CMAKE_SYSTEM_PROCESSOR_ARM_VARIANT = armv6
  210. else ifeq ($(BR2_ARM_CPU_ARMV7A),y)
  211. CMAKE_SYSTEM_PROCESSOR_ARM_VARIANT = armv7
  212. else ifeq ($(BR2_ARM_CPU_ARMV8A),y)
  213. CMAKE_SYSTEM_PROCESSOR_ARM_VARIANT = armv8
  214. endif
  215. ifeq ($(BR2_arm),y)
  216. CMAKE_SYSTEM_PROCESSOR = $(CMAKE_SYSTEM_PROCESSOR_ARM_VARIANT)l
  217. else ifeq ($(BR2_armeb),y)
  218. CMAKE_SYSTEM_PROCESSOR = $(CMAKE_SYSTEM_PROCESSOR_ARM_VARIANT)b
  219. else ifeq ($(call qstrip,$(BR2_ARCH)),powerpc64)
  220. CMAKE_SYSTEM_PROCESSOR = ppc64
  221. else ifeq ($(call qstrip,$(BR2_ARCH)),powerpc64le)
  222. CMAKE_SYSTEM_PROCESSOR = ppc64le
  223. else
  224. CMAKE_SYSTEM_PROCESSOR = $(BR2_ARCH)
  225. endif
  226. # In order to allow the toolchain to be relocated, we calculate the HOST_DIR
  227. # based on the toolchainfile.cmake file's location: $(HOST_DIR)/share/buildroot
  228. # In all the other variables, HOST_DIR will be replaced by RELOCATED_HOST_DIR,
  229. # so we have to strip "$(HOST_DIR)/" from the paths that contain it.
  230. define TOOLCHAIN_CMAKE_INSTALL_FILES
  231. @mkdir -p $(HOST_DIR)/share/buildroot
  232. sed \
  233. -e 's#@@STAGING_SUBDIR@@#$(call qstrip,$(STAGING_SUBDIR))#' \
  234. -e 's#@@TARGET_CFLAGS@@#$(call qstrip,$(TARGET_CFLAGS))#' \
  235. -e 's#@@TARGET_CXXFLAGS@@#$(call qstrip,$(TARGET_CXXFLAGS))#' \
  236. -e 's#@@TARGET_FCFLAGS@@#$(call qstrip,$(TARGET_FCFLAGS))#' \
  237. -e 's#@@TARGET_LDFLAGS@@#$(call qstrip,$(TARGET_LDFLAGS))#' \
  238. -e 's#@@TARGET_CC@@#$(subst $(HOST_DIR)/,,$(call qstrip,$(TARGET_CC)))#' \
  239. -e 's#@@TARGET_CXX@@#$(subst $(HOST_DIR)/,,$(call qstrip,$(TARGET_CXX)))#' \
  240. -e 's#@@TARGET_FC@@#$(subst $(HOST_DIR)/,,$(call qstrip,$(TARGET_FC)))#' \
  241. -e 's#@@CMAKE_SYSTEM_PROCESSOR@@#$(call qstrip,$(CMAKE_SYSTEM_PROCESSOR))#' \
  242. -e 's#@@TOOLCHAIN_HAS_FORTRAN@@#$(if $(BR2_TOOLCHAIN_HAS_FORTRAN),1,0)#' \
  243. -e 's#@@CMAKE_BUILD_TYPE@@#$(if $(BR2_ENABLE_DEBUG),Debug,Release)#' \
  244. $(TOPDIR)/support/misc/toolchainfile.cmake.in \
  245. > $(HOST_DIR)/share/buildroot/toolchainfile.cmake
  246. $(Q)$(INSTALL) -D -m 0644 support/misc/Buildroot.cmake \
  247. $(HOST_DIR)/share/buildroot/Platform/Buildroot.cmake
  248. endef
  249. TOOLCHAIN_POST_INSTALL_STAGING_HOOKS += TOOLCHAIN_CMAKE_INSTALL_FILES