blob: d978b8894406b220b2b0bd3789b29a141d0cf2a6 [file] [log] [blame]
Patrick Williams92b42cb2022-09-03 06:53:57 -05001#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7# Path to the CMake file to process.
8OECMAKE_SOURCEPATH ??= "${S}"
9
10DEPENDS:prepend = "cmake-native "
11B = "${WORKDIR}/build"
12
13# What CMake generator to use.
14# The supported options are "Unix Makefiles" or "Ninja".
15OECMAKE_GENERATOR ?= "Ninja"
16
17python() {
18 generator = d.getVar("OECMAKE_GENERATOR")
19 if "Unix Makefiles" in generator:
20 args = "-G '" + generator + "' -DCMAKE_MAKE_PROGRAM=" + d.getVar("MAKE")
21 d.setVar("OECMAKE_GENERATOR_ARGS", args)
22 d.setVarFlag("do_compile", "progress", "percent")
23 elif "Ninja" in generator:
24 args = "-G '" + generator + "' -DCMAKE_MAKE_PROGRAM=ninja"
25 d.appendVar("DEPENDS", " ninja-native")
26 d.setVar("OECMAKE_GENERATOR_ARGS", args)
27 d.setVarFlag("do_compile", "progress", r"outof:^\[(\d+)/(\d+)\]\s+")
28 else:
29 bb.fatal("Unknown CMake Generator %s" % generator)
30}
31OECMAKE_AR ?= "${AR}"
32
33# Compiler flags
34OECMAKE_C_FLAGS ?= "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS} ${CFLAGS}"
35OECMAKE_CXX_FLAGS ?= "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS} ${CXXFLAGS}"
36OECMAKE_C_FLAGS_RELEASE ?= "-DNDEBUG"
37OECMAKE_CXX_FLAGS_RELEASE ?= "-DNDEBUG"
38OECMAKE_C_LINK_FLAGS ?= "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS} ${CPPFLAGS} ${LDFLAGS}"
39OECMAKE_CXX_LINK_FLAGS ?= "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS} ${CXXFLAGS} ${LDFLAGS}"
40
41def oecmake_map_compiler(compiler, d):
42 args = d.getVar(compiler).split()
43 if args[0] == "ccache":
44 return args[1], args[0]
45 return args[0], ""
46
47# C/C++ Compiler (without cpu arch/tune arguments)
48OECMAKE_C_COMPILER ?= "${@oecmake_map_compiler('CC', d)[0]}"
49OECMAKE_C_COMPILER_LAUNCHER ?= "${@oecmake_map_compiler('CC', d)[1]}"
50OECMAKE_CXX_COMPILER ?= "${@oecmake_map_compiler('CXX', d)[0]}"
51OECMAKE_CXX_COMPILER_LAUNCHER ?= "${@oecmake_map_compiler('CXX', d)[1]}"
52
53# clear compiler vars for allarch to avoid sig hash difference
Andrew Geissler5082cc72023-09-11 08:41:39 -040054OECMAKE_C_COMPILER:allarch = ""
55OECMAKE_C_COMPILER_LAUNCHER:allarch = ""
56OECMAKE_CXX_COMPILER:allarch = ""
57OECMAKE_CXX_COMPILER_LAUNCHER:allarch = ""
Patrick Williams92b42cb2022-09-03 06:53:57 -050058
59OECMAKE_RPATH ?= ""
60OECMAKE_PERLNATIVE_DIR ??= ""
61OECMAKE_EXTRA_ROOT_PATH ?= ""
62
63OECMAKE_FIND_ROOT_PATH_MODE_PROGRAM = "ONLY"
Patrick Williams92b42cb2022-09-03 06:53:57 -050064
65EXTRA_OECMAKE:append = " ${PACKAGECONFIG_CONFARGS}"
66
67export CMAKE_BUILD_PARALLEL_LEVEL
68CMAKE_BUILD_PARALLEL_LEVEL:task-compile = "${@oe.utils.parallel_make(d, False)}"
69CMAKE_BUILD_PARALLEL_LEVEL:task-install = "${@oe.utils.parallel_make(d, True)}"
70
71OECMAKE_TARGET_COMPILE ?= "all"
72OECMAKE_TARGET_INSTALL ?= "install"
73
74def map_host_os_to_system_name(host_os):
75 if host_os.startswith('mingw'):
76 return 'Windows'
77 if host_os.startswith('linux'):
78 return 'Linux'
79 return host_os
80
81# CMake expects target architectures in the format of uname(2),
82# which do not always match TARGET_ARCH, so all the necessary
83# conversions should happen here.
84def map_host_arch_to_uname_arch(host_arch):
85 if host_arch == "powerpc":
86 return "ppc"
87 if host_arch == "powerpc64le":
88 return "ppc64le"
89 if host_arch == "powerpc64":
90 return "ppc64"
91 return host_arch
92
Andrew Geissler5082cc72023-09-11 08:41:39 -040093
Patrick Williams92b42cb2022-09-03 06:53:57 -050094cmake_do_generate_toolchain_file() {
95 if [ "${BUILD_SYS}" = "${HOST_SYS}" ]; then
96 cmake_crosscompiling="set( CMAKE_CROSSCOMPILING FALSE )"
Andrew Geissler5082cc72023-09-11 08:41:39 -040097 else
98 cmake_sysroot="set( CMAKE_SYSROOT \"${RECIPE_SYSROOT}\" )"
Patrick Williams92b42cb2022-09-03 06:53:57 -050099 fi
Andrew Geissler5082cc72023-09-11 08:41:39 -0400100
Patrick Williams92b42cb2022-09-03 06:53:57 -0500101 cat > ${WORKDIR}/toolchain.cmake <<EOF
102# CMake system name must be something like "Linux".
103# This is important for cross-compiling.
104$cmake_crosscompiling
105set( CMAKE_SYSTEM_NAME ${@map_host_os_to_system_name(d.getVar('HOST_OS'))} )
106set( CMAKE_SYSTEM_PROCESSOR ${@map_host_arch_to_uname_arch(d.getVar('HOST_ARCH'))} )
107set( CMAKE_C_COMPILER ${OECMAKE_C_COMPILER} )
108set( CMAKE_CXX_COMPILER ${OECMAKE_CXX_COMPILER} )
109set( CMAKE_C_COMPILER_LAUNCHER ${OECMAKE_C_COMPILER_LAUNCHER} )
110set( CMAKE_CXX_COMPILER_LAUNCHER ${OECMAKE_CXX_COMPILER_LAUNCHER} )
111set( CMAKE_ASM_COMPILER ${OECMAKE_C_COMPILER} )
112find_program( CMAKE_AR ${OECMAKE_AR} DOC "Archiver" REQUIRED )
113
114set( CMAKE_C_FLAGS "${OECMAKE_C_FLAGS}" CACHE STRING "CFLAGS" )
115set( CMAKE_CXX_FLAGS "${OECMAKE_CXX_FLAGS}" CACHE STRING "CXXFLAGS" )
116set( CMAKE_ASM_FLAGS "${OECMAKE_C_FLAGS}" CACHE STRING "ASM FLAGS" )
117set( CMAKE_C_FLAGS_RELEASE "${OECMAKE_C_FLAGS_RELEASE}" CACHE STRING "Additional CFLAGS for release" )
118set( CMAKE_CXX_FLAGS_RELEASE "${OECMAKE_CXX_FLAGS_RELEASE}" CACHE STRING "Additional CXXFLAGS for release" )
119set( CMAKE_ASM_FLAGS_RELEASE "${OECMAKE_C_FLAGS_RELEASE}" CACHE STRING "Additional ASM FLAGS for release" )
120set( CMAKE_C_LINK_FLAGS "${OECMAKE_C_LINK_FLAGS}" CACHE STRING "LDFLAGS" )
121set( CMAKE_CXX_LINK_FLAGS "${OECMAKE_CXX_LINK_FLAGS}" CACHE STRING "LDFLAGS" )
122
123# only search in the paths provided so cmake doesnt pick
124# up libraries and tools from the native build machine
125set( CMAKE_FIND_ROOT_PATH ${STAGING_DIR_HOST} ${STAGING_DIR_NATIVE} ${CROSS_DIR} ${OECMAKE_PERLNATIVE_DIR} ${OECMAKE_EXTRA_ROOT_PATH} ${EXTERNAL_TOOLCHAIN} ${HOSTTOOLS_DIR})
126set( CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY )
127set( CMAKE_FIND_ROOT_PATH_MODE_PROGRAM ${OECMAKE_FIND_ROOT_PATH_MODE_PROGRAM} )
128set( CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY )
129set( CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY )
130set( CMAKE_PROGRAM_PATH "/" )
131
Patrick Williams8e7b46e2023-05-01 14:19:06 -0500132$cmake_sysroot
133
Patrick Williams92b42cb2022-09-03 06:53:57 -0500134# Use qt.conf settings
135set( ENV{QT_CONF_PATH} ${WORKDIR}/qt.conf )
136
137# We need to set the rpath to the correct directory as cmake does not provide any
138# directory as rpath by default
139set( CMAKE_INSTALL_RPATH ${OECMAKE_RPATH} )
140
141# Use RPATHs relative to build directory for reproducibility
142set( CMAKE_BUILD_RPATH_USE_ORIGIN ON )
143
144# Use our cmake modules
145list(APPEND CMAKE_MODULE_PATH "${STAGING_DATADIR}/cmake/Modules/")
146
147# add for non /usr/lib libdir, e.g. /usr/lib64
148set( CMAKE_LIBRARY_PATH ${libdir} ${base_libdir})
149
150# add include dir to implicit includes in case it differs from /usr/include
151list(APPEND CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES ${includedir})
152list(APPEND CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES ${includedir})
153
154EOF
155}
156
157addtask generate_toolchain_file after do_patch before do_configure
158
Andrew Geissler20137392023-10-12 04:59:14 -0600159CONFIGURE_FILES = "CMakeLists.txt *.cmake"
Patrick Williams92b42cb2022-09-03 06:53:57 -0500160
161do_configure[cleandirs] = "${@d.getVar('B') if d.getVar('S') != d.getVar('B') else ''}"
162
Andrew Geissler5082cc72023-09-11 08:41:39 -0400163OECMAKE_ARGS = "\
164 -DCMAKE_INSTALL_PREFIX:PATH=${prefix} \
165 -DCMAKE_INSTALL_BINDIR:PATH=${@os.path.relpath(d.getVar('bindir'), d.getVar('prefix') + '/')} \
166 -DCMAKE_INSTALL_SBINDIR:PATH=${@os.path.relpath(d.getVar('sbindir'), d.getVar('prefix') + '/')} \
167 -DCMAKE_INSTALL_LIBEXECDIR:PATH=${@os.path.relpath(d.getVar('libexecdir'), d.getVar('prefix') + '/')} \
168 -DCMAKE_INSTALL_SYSCONFDIR:PATH=${sysconfdir} \
169 -DCMAKE_INSTALL_SHAREDSTATEDIR:PATH=${@os.path.relpath(d.getVar('sharedstatedir'), d. getVar('prefix') + '/')} \
170 -DCMAKE_INSTALL_LOCALSTATEDIR:PATH=${localstatedir} \
171 -DCMAKE_INSTALL_LIBDIR:PATH=${@os.path.relpath(d.getVar('libdir'), d.getVar('prefix') + '/')} \
172 -DCMAKE_INSTALL_INCLUDEDIR:PATH=${@os.path.relpath(d.getVar('includedir'), d.getVar('prefix') + '/')} \
173 -DCMAKE_INSTALL_DATAROOTDIR:PATH=${@os.path.relpath(d.getVar('datadir'), d.getVar('prefix') + '/')} \
174 -DPYTHON_EXECUTABLE:PATH=${PYTHON} \
175 -DPython_EXECUTABLE:PATH=${PYTHON} \
176 -DPython3_EXECUTABLE:PATH=${PYTHON} \
177 -DLIB_SUFFIX=${@d.getVar('baselib').replace('lib', '')} \
178 -DCMAKE_INSTALL_SO_NO_EXE=0 \
179 -DCMAKE_TOOLCHAIN_FILE:FILEPATH=${WORKDIR}/toolchain.cmake \
180 -DCMAKE_NO_SYSTEM_FROM_IMPORTED=1 \
181 -DCMAKE_EXPORT_NO_PACKAGE_REGISTRY=ON \
182 -DFETCHCONTENT_FULLY_DISCONNECTED=ON \
183 -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=ON \
184"
185
Patrick Williams92b42cb2022-09-03 06:53:57 -0500186cmake_do_configure() {
187 if [ "${OECMAKE_BUILDPATH}" ]; then
188 bbnote "cmake.bbclass no longer uses OECMAKE_BUILDPATH. The default behaviour is now out-of-tree builds with B=WORKDIR/build."
189 fi
190
191 if [ "${S}" = "${B}" ]; then
192 find ${B} -name CMakeFiles -or -name Makefile -or -name cmake_install.cmake -or -name CMakeCache.txt -delete
193 fi
194
195 # Just like autotools cmake can use a site file to cache result that need generated binaries to run
196 if [ -e ${WORKDIR}/site-file.cmake ] ; then
197 oecmake_sitefile="-C ${WORKDIR}/site-file.cmake"
198 else
199 oecmake_sitefile=
200 fi
201
202 cmake \
203 ${OECMAKE_GENERATOR_ARGS} \
204 $oecmake_sitefile \
205 ${OECMAKE_SOURCEPATH} \
Andrew Geissler5082cc72023-09-11 08:41:39 -0400206 ${OECMAKE_ARGS} \
Patrick Williams92b42cb2022-09-03 06:53:57 -0500207 ${EXTRA_OECMAKE} \
208 -Wno-dev
209}
210
211# To disable verbose cmake logs for a given recipe or globally config metadata e.g. local.conf
212# add following
213#
214# CMAKE_VERBOSE = ""
215#
216
217CMAKE_VERBOSE ??= "VERBOSE=1"
218
219# Then run do_compile again
220cmake_runcmake_build() {
221 bbnote ${DESTDIR:+DESTDIR=${DESTDIR} }${CMAKE_VERBOSE} cmake --build '${B}' "$@" -- ${EXTRA_OECMAKE_BUILD}
222 eval ${DESTDIR:+DESTDIR=${DESTDIR} }${CMAKE_VERBOSE} cmake --build '${B}' "$@" -- ${EXTRA_OECMAKE_BUILD}
223}
224
225cmake_do_compile() {
226 cmake_runcmake_build --target ${OECMAKE_TARGET_COMPILE}
227}
228
229cmake_do_install() {
230 DESTDIR='${D}' cmake_runcmake_build --target ${OECMAKE_TARGET_INSTALL}
231}
232
233EXPORT_FUNCTIONS do_configure do_compile do_install do_generate_toolchain_file