1 ################################################################################
2 # CMake package infrastructure
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.
8 # See the Buildroot documentation for details on the usage of this
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.
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.
21 ################################################################################
23 # Set compiler variables.
24 ifeq ($(BR2_CCACHE),y)
25 CMAKE_HOST_C_COMPILER = $(HOST_DIR)/bin/ccache
26 CMAKE_HOST_CXX_COMPILER = $(HOST_DIR)/bin/ccache
27 CMAKE_HOST_C_COMPILER_ARG1 = $(HOSTCC_NOCCACHE)
28 CMAKE_HOST_CXX_COMPILER_ARG1 = $(HOSTCXX_NOCCACHE)
30 CMAKE_HOST_C_COMPILER = $(HOSTCC)
31 CMAKE_HOST_CXX_COMPILER = $(HOSTCXX)
35 CMAKE_QUIET = -DCMAKE_RULE_MESSAGES=OFF -DCMAKE_INSTALL_MESSAGE=NEVER
38 ################################################################################
39 # inner-cmake-package -- defines how the configuration, compilation and
40 # installation of a CMake package should be done, implements a few hooks to
41 # tune the build process and calls the generic package infrastructure to
42 # generate the necessary make targets
44 # argument 1 is the lowercase package name
45 # argument 2 is the uppercase package name, including a HOST_ prefix
47 # argument 3 is the uppercase package name, without the HOST_ prefix
49 # argument 4 is the type (target or host)
50 ################################################################################
52 define inner-cmake-package
59 $(2)_INSTALL_OPTS ?= install
60 $(2)_INSTALL_STAGING_OPTS ?= DESTDIR=$$(STAGING_DIR) install/fast
61 $(2)_INSTALL_TARGET_OPTS ?= DESTDIR=$$(TARGET_DIR) install/fast
63 $(2)_SRCDIR = $$($(2)_DIR)/$$($(2)_SUBDIR)
65 $(3)_SUPPORTS_IN_SOURCE_BUILD ?= YES
68 ifeq ($$($(3)_SUPPORTS_IN_SOURCE_BUILD),YES)
69 $(2)_BUILDDIR = $$($(2)_SRCDIR)
71 $(2)_BUILDDIR = $$($(2)_SRCDIR)/buildroot-build
75 # Configure step. Only define it if not already defined by the package
76 # .mk file. And take care of the differences between host and target
79 ifndef $(2)_CONFIGURE_CMDS
82 # Configure package for target
84 # - We are passing BUILD_SHARED_LIBS because it is documented as a
85 # standard CMake variable to control the build of shared libraries
86 # (see https://cmake.org/cmake/help/v3.8/manual/cmake-variables.7.html#variables-that-change-behavior)
87 # - We are not passing BUILD_STATIC_LIBS because it is *not*
88 # documented as a standard CMake variable. If a package supports it,
89 # it must handle it explicitly.
91 define $(2)_CONFIGURE_CMDS
92 (mkdir -p $$($$(PKG)_BUILDDIR) && \
93 cd $$($$(PKG)_BUILDDIR) && \
94 rm -f CMakeCache.txt && \
96 $$($$(PKG)_CONF_ENV) $$(BR2_CMAKE) $$($$(PKG)_SRCDIR) \
97 -DCMAKE_TOOLCHAIN_FILE="$$(HOST_DIR)/share/buildroot/toolchainfile.cmake" \
98 -DCMAKE_INSTALL_PREFIX="/usr" \
99 -DCMAKE_COLOR_MAKEFILE=OFF \
102 -DBUILD_EXAMPLE=OFF \
103 -DBUILD_EXAMPLES=OFF \
106 -DBUILD_TESTING=OFF \
107 -DBUILD_SHARED_LIBS=$$(if $$(BR2_STATIC_LIBS),OFF,ON) \
109 $$($$(PKG)_CONF_OPTS) \
114 # Configure package for host
115 define $(2)_CONFIGURE_CMDS
116 (mkdir -p $$($$(PKG)_BUILDDIR) && \
117 cd $$($$(PKG)_BUILDDIR) && \
118 rm -f CMakeCache.txt && \
120 PKG_CONFIG="$$(PKG_CONFIG_HOST_BINARY)" \
121 PKG_CONFIG_SYSROOT_DIR="/" \
122 PKG_CONFIG_LIBDIR="$$(HOST_DIR)/lib/pkgconfig:$$(HOST_DIR)/share/pkgconfig" \
123 PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=1 \
124 PKG_CONFIG_ALLOW_SYSTEM_LIBS=1 \
125 $$($$(PKG)_CONF_ENV) $$(BR2_CMAKE) $$($$(PKG)_SRCDIR) \
126 -DCMAKE_INSTALL_SO_NO_EXE=0 \
127 -DCMAKE_FIND_ROOT_PATH="$$(HOST_DIR)" \
128 -DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM="BOTH" \
129 -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY="BOTH" \
130 -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE="BOTH" \
131 -DCMAKE_INSTALL_PREFIX="$$(HOST_DIR)" \
132 -DCMAKE_C_FLAGS="$$(HOST_CFLAGS)" \
133 -DCMAKE_CXX_FLAGS="$$(HOST_CXXFLAGS)" \
134 -DCMAKE_EXE_LINKER_FLAGS="$$(HOST_LDFLAGS)" \
135 -DCMAKE_SHARED_LINKER_FLAGS="$$(HOST_LDFLAGS)" \
136 -DCMAKE_ASM_COMPILER="$$(HOSTAS)" \
137 -DCMAKE_C_COMPILER="$$(CMAKE_HOST_C_COMPILER)" \
138 -DCMAKE_CXX_COMPILER="$$(CMAKE_HOST_CXX_COMPILER)" \
139 $(if $$(CMAKE_HOST_C_COMPILER_ARG1),\
140 -DCMAKE_C_COMPILER_ARG1="$$(CMAKE_HOST_C_COMPILER_ARG1)" \
141 -DCMAKE_CXX_COMPILER_ARG1="$$(CMAKE_HOST_CXX_COMPILER_ARG1)" \
143 -DCMAKE_COLOR_MAKEFILE=OFF \
146 -DBUILD_EXAMPLE=OFF \
147 -DBUILD_EXAMPLES=OFF \
150 -DBUILD_TESTING=OFF \
152 $$($$(PKG)_CONF_OPTS) \
158 # Since some CMake modules (even upstream ones) use pgk_check_modules
159 # primitives to find {C,LD}FLAGS, add it to the dependency list.
160 $(2)_DEPENDENCIES += host-pkgconf
162 $(2)_DEPENDENCIES += $(BR2_CMAKE_HOST_DEPENDENCY)
165 # Build step. Only define it if not already defined by the package .mk
168 ifndef $(2)_BUILD_CMDS
170 define $(2)_BUILD_CMDS
171 $$(TARGET_MAKE_ENV) $$($$(PKG)_MAKE_ENV) $$($$(PKG)_MAKE) $$($$(PKG)_MAKE_OPTS) -C $$($$(PKG)_BUILDDIR)
174 define $(2)_BUILD_CMDS
175 $$(HOST_MAKE_ENV) $$($$(PKG)_MAKE_ENV) $$($$(PKG)_MAKE) $$($$(PKG)_MAKE_OPTS) -C $$($$(PKG)_BUILDDIR)
181 # Host installation step. Only define it if not already defined by the
184 ifndef $(2)_INSTALL_CMDS
185 define $(2)_INSTALL_CMDS
186 $$(HOST_MAKE_ENV) $$($$(PKG)_MAKE_ENV) $$($$(PKG)_MAKE) $$($$(PKG)_MAKE_OPTS) $$($$(PKG)_INSTALL_OPTS) -C $$($$(PKG)_BUILDDIR)
191 # Staging installation step. Only define it if not already defined by
192 # the package .mk file.
194 ifndef $(2)_INSTALL_STAGING_CMDS
195 define $(2)_INSTALL_STAGING_CMDS
196 $$(TARGET_MAKE_ENV) $$($$(PKG)_MAKE_ENV) $$($$(PKG)_MAKE) $$($$(PKG)_MAKE_OPTS) $$($$(PKG)_INSTALL_STAGING_OPTS) -C $$($$(PKG)_BUILDDIR)
201 # Target installation step. Only define it if not already defined by
202 # the package .mk file.
204 ifndef $(2)_INSTALL_TARGET_CMDS
205 define $(2)_INSTALL_TARGET_CMDS
206 $$(TARGET_MAKE_ENV) $$($$(PKG)_MAKE_ENV) $$($$(PKG)_MAKE) $$($$(PKG)_MAKE_OPTS) $$($$(PKG)_INSTALL_TARGET_OPTS) -C $$($$(PKG)_BUILDDIR)
210 # Call the generic package infrastructure to generate the necessary
212 $(call inner-generic-package,$(1),$(2),$(3),$(4))
216 ################################################################################
217 # cmake-package -- the target generator macro for CMake packages
218 ################################################################################
220 cmake-package = $(call inner-cmake-package,$(pkgname),$(call UPPERCASE,$(pkgname)),$(call UPPERCASE,$(pkgname)),target)
221 host-cmake-package = $(call inner-cmake-package,host-$(pkgname),$(call UPPERCASE,host-$(pkgname)),$(call UPPERCASE,$(pkgname)),host)
223 ################################################################################
224 # Generation of the CMake toolchain file
225 ################################################################################
227 # CMAKE_SYSTEM_PROCESSOR should match uname -m
228 ifeq ($(BR2_ARM_CPU_ARMV4),y)
229 CMAKE_SYSTEM_PROCESSOR_ARM_VARIANT = armv4
230 else ifeq ($(BR2_ARM_CPU_ARMV5),y)
231 CMAKE_SYSTEM_PROCESSOR_ARM_VARIANT = armv5
232 else ifeq ($(BR2_ARM_CPU_ARMV6),y)
233 CMAKE_SYSTEM_PROCESSOR_ARM_VARIANT = armv6
234 else ifeq ($(BR2_ARM_CPU_ARMV7A),y)
235 CMAKE_SYSTEM_PROCESSOR_ARM_VARIANT = armv7
236 else ifeq ($(BR2_ARM_CPU_ARMV8A),y)
237 CMAKE_SYSTEM_PROCESSOR_ARM_VARIANT = armv8
241 CMAKE_SYSTEM_PROCESSOR = $(CMAKE_SYSTEM_PROCESSOR_ARM_VARIANT)l
242 else ifeq ($(BR2_armeb),y)
243 CMAKE_SYSTEM_PROCESSOR = $(CMAKE_SYSTEM_PROCESSOR_ARM_VARIANT)b
244 else ifeq ($(call qstrip,$(BR2_ARCH)),powerpc64)
245 CMAKE_SYSTEM_PROCESSOR = ppc64
246 else ifeq ($(call qstrip,$(BR2_ARCH)),powerpc64le)
247 CMAKE_SYSTEM_PROCESSOR = ppc64le
249 CMAKE_SYSTEM_PROCESSOR = $(BR2_ARCH)
252 # In order to allow the toolchain to be relocated, we calculate the HOST_DIR
253 # based on the toolchainfile.cmake file's location: $(HOST_DIR)/share/buildroot
254 # In all the other variables, HOST_DIR will be replaced by RELOCATED_HOST_DIR,
255 # so we have to strip "$(HOST_DIR)/" from the paths that contain it.
256 define TOOLCHAIN_CMAKE_INSTALL_FILES
257 @mkdir -p $(HOST_DIR)/share/buildroot
259 -e 's#@@STAGING_SUBDIR@@#$(call qstrip,$(STAGING_SUBDIR))#' \
260 -e 's#@@TARGET_CFLAGS@@#$(call qstrip,$(TARGET_CFLAGS))#' \
261 -e 's#@@TARGET_CXXFLAGS@@#$(call qstrip,$(TARGET_CXXFLAGS))#' \
262 -e 's#@@TARGET_FCFLAGS@@#$(call qstrip,$(TARGET_FCFLAGS))#' \
263 -e 's#@@TARGET_LDFLAGS@@#$(call qstrip,$(TARGET_LDFLAGS))#' \
264 -e 's#@@TARGET_CC@@#$(subst $(HOST_DIR)/,,$(call qstrip,$(TARGET_CC)))#' \
265 -e 's#@@TARGET_CXX@@#$(subst $(HOST_DIR)/,,$(call qstrip,$(TARGET_CXX)))#' \
266 -e 's#@@TARGET_FC@@#$(subst $(HOST_DIR)/,,$(call qstrip,$(TARGET_FC)))#' \
267 -e 's#@@CMAKE_SYSTEM_PROCESSOR@@#$(call qstrip,$(CMAKE_SYSTEM_PROCESSOR))#' \
268 -e 's#@@TOOLCHAIN_HAS_FORTRAN@@#$(if $(BR2_TOOLCHAIN_HAS_FORTRAN),1,0)#' \
269 -e 's#@@CMAKE_BUILD_TYPE@@#$(if $(BR2_ENABLE_DEBUG),Debug,Release)#' \
270 $(TOPDIR)/support/misc/toolchainfile.cmake.in \
271 > $(HOST_DIR)/share/buildroot/toolchainfile.cmake
272 $(Q)$(INSTALL) -D -m 0644 support/misc/Buildroot.cmake \
273 $(HOST_DIR)/share/buildroot/Platform/Buildroot.cmake
276 TOOLCHAIN_POST_INSTALL_STAGING_HOOKS += TOOLCHAIN_CMAKE_INSTALL_FILES
277 TOOLCHAIN_INSTALL_STAGING = YES