]> rtime.felk.cvut.cz Git - coffee/buildroot.git/blob - package/pkg-cmake.mk
docs/manual: using a branch name as FOO_VERSION does not work
[coffee/buildroot.git] / package / pkg-cmake.mk
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
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)
29 else
30 CMAKE_HOST_C_COMPILER = $(HOSTCC)
31 CMAKE_HOST_CXX_COMPILER = $(HOSTCXX)
32 endif
33
34 ifneq ($(QUIET),)
35 CMAKE_QUIET = -DCMAKE_RULE_MESSAGES=OFF -DCMAKE_INSTALL_MESSAGE=NEVER
36 endif
37
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
43 #
44 #  argument 1 is the lowercase package name
45 #  argument 2 is the uppercase package name, including a HOST_ prefix
46 #             for host packages
47 #  argument 3 is the uppercase package name, without the HOST_ prefix
48 #             for host packages
49 #  argument 4 is the type (target or host)
50 ################################################################################
51
52 define inner-cmake-package
53
54 $(2)_CONF_ENV                   ?=
55 $(2)_CONF_OPTS                  ?=
56 $(2)_MAKE                       ?= $$(MAKE)
57 $(2)_MAKE_ENV                   ?=
58 $(2)_MAKE_OPTS                  ?=
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
62
63 $(2)_SRCDIR                     = $$($(2)_DIR)/$$($(2)_SUBDIR)
64
65 $(3)_SUPPORTS_IN_SOURCE_BUILD ?= YES
66
67
68 ifeq ($$($(3)_SUPPORTS_IN_SOURCE_BUILD),YES)
69 $(2)_BUILDDIR                   = $$($(2)_SRCDIR)
70 else
71 $(2)_BUILDDIR                   = $$($(2)_SRCDIR)/buildroot-build
72 endif
73
74 #
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
77 # packages.
78 #
79 ifndef $(2)_CONFIGURE_CMDS
80 ifeq ($(4),target)
81
82 # Configure package for target
83 #
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.
90 #
91 define $(2)_CONFIGURE_CMDS
92         (mkdir -p $$($$(PKG)_BUILDDIR) && \
93         cd $$($$(PKG)_BUILDDIR) && \
94         rm -f CMakeCache.txt && \
95         PATH=$$(BR_PATH) \
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 \
100                 -DBUILD_DOC=OFF \
101                 -DBUILD_DOCS=OFF \
102                 -DBUILD_EXAMPLE=OFF \
103                 -DBUILD_EXAMPLES=OFF \
104                 -DBUILD_TEST=OFF \
105                 -DBUILD_TESTS=OFF \
106                 -DBUILD_TESTING=OFF \
107                 -DBUILD_SHARED_LIBS=$$(if $$(BR2_STATIC_LIBS),OFF,ON) \
108                 $$(CMAKE_QUIET) \
109                 $$($$(PKG)_CONF_OPTS) \
110         )
111 endef
112 else
113
114 # Configure package for host
115 define $(2)_CONFIGURE_CMDS
116         (mkdir -p $$($$(PKG)_BUILDDIR) && \
117         cd $$($$(PKG)_BUILDDIR) && \
118         rm -f CMakeCache.txt && \
119         PATH=$$(BR_PATH) \
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)" \
142                 ) \
143                 -DCMAKE_COLOR_MAKEFILE=OFF \
144                 -DBUILD_DOC=OFF \
145                 -DBUILD_DOCS=OFF \
146                 -DBUILD_EXAMPLE=OFF \
147                 -DBUILD_EXAMPLES=OFF \
148                 -DBUILD_TEST=OFF \
149                 -DBUILD_TESTS=OFF \
150                 -DBUILD_TESTING=OFF \
151                 $$(CMAKE_QUIET) \
152                 $$($$(PKG)_CONF_OPTS) \
153         )
154 endef
155 endif
156 endif
157
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
161
162 $(2)_DEPENDENCIES += $(BR2_CMAKE_HOST_DEPENDENCY)
163
164 #
165 # Build step. Only define it if not already defined by the package .mk
166 # file.
167 #
168 ifndef $(2)_BUILD_CMDS
169 ifeq ($(4),target)
170 define $(2)_BUILD_CMDS
171         $$(TARGET_MAKE_ENV) $$($$(PKG)_MAKE_ENV) $$($$(PKG)_MAKE) $$($$(PKG)_MAKE_OPTS) -C $$($$(PKG)_BUILDDIR)
172 endef
173 else
174 define $(2)_BUILD_CMDS
175         $$(HOST_MAKE_ENV) $$($$(PKG)_MAKE_ENV) $$($$(PKG)_MAKE) $$($$(PKG)_MAKE_OPTS) -C $$($$(PKG)_BUILDDIR)
176 endef
177 endif
178 endif
179
180 #
181 # Host installation step. Only define it if not already defined by the
182 # package .mk file.
183 #
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)
187 endef
188 endif
189
190 #
191 # Staging installation step. Only define it if not already defined by
192 # the package .mk file.
193 #
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)
197 endef
198 endif
199
200 #
201 # Target installation step. Only define it if not already defined by
202 # the package .mk file.
203 #
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)
207 endef
208 endif
209
210 # Call the generic package infrastructure to generate the necessary
211 # make targets
212 $(call inner-generic-package,$(1),$(2),$(3),$(4))
213
214 endef
215
216 ################################################################################
217 # cmake-package -- the target generator macro for CMake packages
218 ################################################################################
219
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)
222
223 ################################################################################
224 # Generation of the CMake toolchain file
225 ################################################################################
226
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
238 endif
239
240 ifeq ($(BR2_arm),y)
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
248 else
249 CMAKE_SYSTEM_PROCESSOR = $(BR2_ARCH)
250 endif
251
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
258         sed \
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
274 endef
275
276 TOOLCHAIN_POST_INSTALL_STAGING_HOOKS += TOOLCHAIN_CMAKE_INSTALL_FILES
277 TOOLCHAIN_INSTALL_STAGING = YES