pkg-cmake.mk 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 informations 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. ################################################################################
  23. # inner-cmake-package -- defines how the configuration, compilation and
  24. # installation of a CMake package should be done, implements a few hooks to
  25. # tune the build process and calls the generic package infrastructure to
  26. # generate the necessary make targets
  27. #
  28. # argument 1 is the lowercase package name
  29. # argument 2 is the uppercase package name, including an HOST_ prefix
  30. # for host packages
  31. # argument 3 is the uppercase package name, without the HOST_ prefix
  32. # for host packages
  33. # argument 4 is the package directory prefix
  34. # argument 5 is the type (target or host)
  35. ################################################################################
  36. define inner-cmake-package
  37. $(2)_CONF_ENV ?=
  38. $(2)_CONF_OPT ?=
  39. $(2)_MAKE ?= $(MAKE)
  40. $(2)_MAKE_ENV ?=
  41. $(2)_MAKE_OPT ?=
  42. $(2)_INSTALL_HOST_OPT ?= install
  43. $(2)_INSTALL_STAGING_OPT ?= DESTDIR=$$(STAGING_DIR) install
  44. $(2)_INSTALL_TARGET_OPT ?= DESTDIR=$$(TARGET_DIR) install
  45. $(2)_CLEAN_OPT ?= clean
  46. $(2)_SRCDIR = $$($(2)_DIR)/$($(2)_SUBDIR)
  47. $(2)_BUILDDIR = $$($(2)_SRCDIR)
  48. #
  49. # Configure step. Only define it if not already defined by the package
  50. # .mk file. And take care of the differences between host and target
  51. # packages.
  52. #
  53. ifndef $(2)_CONFIGURE_CMDS
  54. ifeq ($(5),target)
  55. # Configure package for target
  56. define $(2)_CONFIGURE_CMDS
  57. (cd $$($$(PKG)_BUILDDIR) && \
  58. rm -f CMakeCache.txt && \
  59. $$($$(PKG)_CONF_ENV) $(HOST_DIR)/usr/bin/cmake $$($$(PKG)_SRCDIR) \
  60. -DCMAKE_TOOLCHAIN_FILE="$$(HOST_DIR)/usr/share/buildroot/toolchainfile.cmake" \
  61. -DCMAKE_INSTALL_PREFIX="/usr" \
  62. $$($$(PKG)_CONF_OPT) \
  63. )
  64. endef
  65. else
  66. # Configure package for host
  67. define $(2)_CONFIGURE_CMDS
  68. (cd $$($$(PKG)_BUILDDIR) && \
  69. rm -f CMakeCache.txt && \
  70. $(HOST_DIR)/usr/bin/cmake $$($$(PKG)_SRCDIR) \
  71. -DCMAKE_INSTALL_SO_NO_EXE=0 \
  72. -DCMAKE_FIND_ROOT_PATH="$$(HOST_DIR)" \
  73. -DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM="BOTH" \
  74. -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY="BOTH" \
  75. -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE="BOTH" \
  76. -DCMAKE_INSTALL_PREFIX="$$(HOST_DIR)/usr" \
  77. $$($$(PKG)_CONF_OPT) \
  78. )
  79. endef
  80. endif
  81. endif
  82. # This must be repeated from inner-generic-package, otherwise we only get
  83. # host-cmake in _DEPENDENCIES because of the following line
  84. $(2)_DEPENDENCIES ?= $(filter-out $(1),$(patsubst host-host-%,host-%,$(addprefix host-,$($(3)_DEPENDENCIES))))
  85. $(2)_DEPENDENCIES += host-cmake
  86. #
  87. # Build step. Only define it if not already defined by the package .mk
  88. # file.
  89. #
  90. ifndef $(2)_BUILD_CMDS
  91. ifeq ($(5),target)
  92. define $(2)_BUILD_CMDS
  93. $(TARGET_MAKE_ENV) $$($$(PKG)_MAKE_ENV) $$($$(PKG)_MAKE) $$($$(PKG)_MAKE_OPT) -C $$($$(PKG)_BUILDDIR)
  94. endef
  95. else
  96. define $(2)_BUILD_CMDS
  97. $(HOST_MAKE_ENV) $$($$(PKG)_MAKE_ENV) $$($$(PKG)_MAKE) $$($$(PKG)_MAKE_OPT) -C $$($$(PKG)_BUILDDIR)
  98. endef
  99. endif
  100. endif
  101. #
  102. # Host installation step. Only define it if not already defined by the
  103. # package .mk file.
  104. #
  105. ifndef $(2)_INSTALL_CMDS
  106. define $(2)_INSTALL_CMDS
  107. $(HOST_MAKE_ENV) $$($$(PKG)_MAKE_ENV) $$($$(PKG)_MAKE) $$($$(PKG)_MAKE_OPT) $$($$(PKG)_INSTALL_HOST_OPT) -C $$($$(PKG)_BUILDDIR)
  108. endef
  109. endif
  110. #
  111. # Staging installation step. Only define it if not already defined by
  112. # the package .mk file.
  113. #
  114. ifndef $(2)_INSTALL_STAGING_CMDS
  115. define $(2)_INSTALL_STAGING_CMDS
  116. $(TARGET_MAKE_ENV) $$($$(PKG)_MAKE_ENV) $$($$(PKG)_MAKE) $$($$(PKG)_MAKE_OPT) $$($$(PKG)_INSTALL_STAGING_OPT) -C $$($$(PKG)_BUILDDIR)
  117. endef
  118. endif
  119. #
  120. # Target installation step. Only define it if not already defined by
  121. # the package .mk file.
  122. #
  123. ifndef $(2)_INSTALL_TARGET_CMDS
  124. define $(2)_INSTALL_TARGET_CMDS
  125. $(TARGET_MAKE_ENV) $$($$(PKG)_MAKE_ENV) $$($$(PKG)_MAKE) $$($$(PKG)_MAKE_OPT) $$($$(PKG)_INSTALL_TARGET_OPT) -C $$($$(PKG)_BUILDDIR)
  126. endef
  127. endif
  128. #
  129. # Clean step. Only define it if not already defined by
  130. # the package .mk file.
  131. #
  132. ifndef $(2)_CLEAN_CMDS
  133. define $(2)_CLEAN_CMDS
  134. -$(TARGET_MAKE_ENV) $$($$(PKG)_MAKE_ENV) $$($$(PKG)_MAKE) $$($$(PKG)_MAKE_OPT) $$($$(PKG)_CLEAN_OPT) -C $$($$(PKG)_BUILDDIR)
  135. endef
  136. endif
  137. #
  138. # Uninstall from staging step. Only define it if not already defined by
  139. # the package .mk file.
  140. #
  141. ifndef $(2)_UNINSTALL_STAGING_CMDS
  142. define $(2)_UNINSTALL_STAGING_CMDS
  143. (cd $$($$(PKG)_BUILDDIR) && sed "s:\(.*\):$$(STAGING_DIR)\1:" install_manifest.txt | xargs rm -f)
  144. endef
  145. endif
  146. #
  147. # Uninstall from target step. Only define it if not already defined
  148. # by the package .mk file.
  149. #
  150. ifndef $(2)_UNINSTALL_TARGET_CMDS
  151. define $(2)_UNINSTALL_TARGET_CMDS
  152. (cd $$($$(PKG)_BUILDDIR) && sed "s:\(.*\):$$(TARGET_DIR)\1:" install_manifest.txt | xargs rm -f)
  153. endef
  154. endif
  155. # Call the generic package infrastructure to generate the necessary
  156. # make targets
  157. $(call inner-generic-package,$(1),$(2),$(3),$(4),$(5))
  158. endef
  159. ################################################################################
  160. # cmake-package -- the target generator macro for CMake packages
  161. ################################################################################
  162. cmake-package = $(call inner-cmake-package,$(call pkgname),$(call UPPERCASE,$(call pkgname)),$(call UPPERCASE,$(call pkgname)),$(call pkgparentdir),target)
  163. host-cmake-package = $(call inner-cmake-package,host-$(call pkgname),$(call UPPERCASE,host-$(call pkgname)),$(call UPPERCASE,$(call pkgname)),$(call pkgparentdir),host)
  164. ################################################################################
  165. # Generation of the CMake toolchain file
  166. ################################################################################
  167. $(HOST_DIR)/usr/share/buildroot/toolchainfile.cmake:
  168. @mkdir -p $(@D)
  169. @echo -en "\
  170. set(CMAKE_SYSTEM_NAME Linux)\n\
  171. set(CMAKE_C_COMPILER $(TARGET_CC_NOCCACHE))\n\
  172. set(CMAKE_CXX_COMPILER $(TARGET_CXX_NOCCACHE))\n\
  173. set(CMAKE_C_FLAGS \"\$${CMAKE_C_FLAGS} $(TARGET_CFLAGS)\" CACHE STRING \"Buildroot CFLAGS\" FORCE)\n\
  174. set(CMAKE_CXX_FLAGS \"\$${CMAKE_CXX_FLAGS} $(TARGET_CXXFLAGS)\" CACHE STRING \"Buildroot CXXFLAGS\" FORCE)\n\
  175. set(CMAKE_INSTALL_SO_NO_EXE 0)\n\
  176. set(CMAKE_PROGRAM_PATH \"$(HOST_DIR)/usr/bin\")\n\
  177. set(CMAKE_FIND_ROOT_PATH \"$(STAGING_DIR)\")\n\
  178. set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)\n\
  179. set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)\n\
  180. set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)\n\
  181. set(ENV{PKG_CONFIG_SYSROOT_DIR} \"$(STAGING_DIR)\")\n\
  182. " > $@