blob: 301367b515e62bf6e38aed5367bb7ac1ca871a20 [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
54OECMAKE_C_COMPILER_allarch = ""
55OECMAKE_C_COMPILER_LAUNCHER_allarch = ""
56OECMAKE_CXX_COMPILER_allarch = ""
57OECMAKE_CXX_COMPILER_LAUNCHER_allarch = ""
58
59OECMAKE_RPATH ?= ""
60OECMAKE_PERLNATIVE_DIR ??= ""
61OECMAKE_EXTRA_ROOT_PATH ?= ""
62
63OECMAKE_FIND_ROOT_PATH_MODE_PROGRAM = "ONLY"
64OECMAKE_FIND_ROOT_PATH_MODE_PROGRAM:class-native = "BOTH"
65
66EXTRA_OECMAKE:append = " ${PACKAGECONFIG_CONFARGS}"
67
68export CMAKE_BUILD_PARALLEL_LEVEL
69CMAKE_BUILD_PARALLEL_LEVEL:task-compile = "${@oe.utils.parallel_make(d, False)}"
70CMAKE_BUILD_PARALLEL_LEVEL:task-install = "${@oe.utils.parallel_make(d, True)}"
71
72OECMAKE_TARGET_COMPILE ?= "all"
73OECMAKE_TARGET_INSTALL ?= "install"
74
75def map_host_os_to_system_name(host_os):
76 if host_os.startswith('mingw'):
77 return 'Windows'
78 if host_os.startswith('linux'):
79 return 'Linux'
80 return host_os
81
82# CMake expects target architectures in the format of uname(2),
83# which do not always match TARGET_ARCH, so all the necessary
84# conversions should happen here.
85def map_host_arch_to_uname_arch(host_arch):
86 if host_arch == "powerpc":
87 return "ppc"
88 if host_arch == "powerpc64le":
89 return "ppc64le"
90 if host_arch == "powerpc64":
91 return "ppc64"
92 return host_arch
93
94cmake_do_generate_toolchain_file() {
95 if [ "${BUILD_SYS}" = "${HOST_SYS}" ]; then
96 cmake_crosscompiling="set( CMAKE_CROSSCOMPILING FALSE )"
Patrick Williams8e7b46e2023-05-01 14:19:06 -050097 else
98 cmake_sysroot="set( CMAKE_SYSROOT \"${RECIPE_SYSROOT}\" )"
Patrick Williams92b42cb2022-09-03 06:53:57 -050099 fi
100 cat > ${WORKDIR}/toolchain.cmake <<EOF
101# CMake system name must be something like "Linux".
102# This is important for cross-compiling.
103$cmake_crosscompiling
104set( CMAKE_SYSTEM_NAME ${@map_host_os_to_system_name(d.getVar('HOST_OS'))} )
105set( CMAKE_SYSTEM_PROCESSOR ${@map_host_arch_to_uname_arch(d.getVar('HOST_ARCH'))} )
106set( CMAKE_C_COMPILER ${OECMAKE_C_COMPILER} )
107set( CMAKE_CXX_COMPILER ${OECMAKE_CXX_COMPILER} )
108set( CMAKE_C_COMPILER_LAUNCHER ${OECMAKE_C_COMPILER_LAUNCHER} )
109set( CMAKE_CXX_COMPILER_LAUNCHER ${OECMAKE_CXX_COMPILER_LAUNCHER} )
110set( CMAKE_ASM_COMPILER ${OECMAKE_C_COMPILER} )
111find_program( CMAKE_AR ${OECMAKE_AR} DOC "Archiver" REQUIRED )
112
113set( CMAKE_C_FLAGS "${OECMAKE_C_FLAGS}" CACHE STRING "CFLAGS" )
114set( CMAKE_CXX_FLAGS "${OECMAKE_CXX_FLAGS}" CACHE STRING "CXXFLAGS" )
115set( CMAKE_ASM_FLAGS "${OECMAKE_C_FLAGS}" CACHE STRING "ASM FLAGS" )
116set( CMAKE_C_FLAGS_RELEASE "${OECMAKE_C_FLAGS_RELEASE}" CACHE STRING "Additional CFLAGS for release" )
117set( CMAKE_CXX_FLAGS_RELEASE "${OECMAKE_CXX_FLAGS_RELEASE}" CACHE STRING "Additional CXXFLAGS for release" )
118set( CMAKE_ASM_FLAGS_RELEASE "${OECMAKE_C_FLAGS_RELEASE}" CACHE STRING "Additional ASM FLAGS for release" )
119set( CMAKE_C_LINK_FLAGS "${OECMAKE_C_LINK_FLAGS}" CACHE STRING "LDFLAGS" )
120set( CMAKE_CXX_LINK_FLAGS "${OECMAKE_CXX_LINK_FLAGS}" CACHE STRING "LDFLAGS" )
121
122# only search in the paths provided so cmake doesnt pick
123# up libraries and tools from the native build machine
124set( CMAKE_FIND_ROOT_PATH ${STAGING_DIR_HOST} ${STAGING_DIR_NATIVE} ${CROSS_DIR} ${OECMAKE_PERLNATIVE_DIR} ${OECMAKE_EXTRA_ROOT_PATH} ${EXTERNAL_TOOLCHAIN} ${HOSTTOOLS_DIR})
125set( CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY )
126set( CMAKE_FIND_ROOT_PATH_MODE_PROGRAM ${OECMAKE_FIND_ROOT_PATH_MODE_PROGRAM} )
127set( CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY )
128set( CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY )
129set( CMAKE_PROGRAM_PATH "/" )
130
Patrick Williams8e7b46e2023-05-01 14:19:06 -0500131$cmake_sysroot
132
Patrick Williams92b42cb2022-09-03 06:53:57 -0500133# Use qt.conf settings
134set( ENV{QT_CONF_PATH} ${WORKDIR}/qt.conf )
135
136# We need to set the rpath to the correct directory as cmake does not provide any
137# directory as rpath by default
138set( CMAKE_INSTALL_RPATH ${OECMAKE_RPATH} )
139
140# Use RPATHs relative to build directory for reproducibility
141set( CMAKE_BUILD_RPATH_USE_ORIGIN ON )
142
143# Use our cmake modules
144list(APPEND CMAKE_MODULE_PATH "${STAGING_DATADIR}/cmake/Modules/")
145
146# add for non /usr/lib libdir, e.g. /usr/lib64
147set( CMAKE_LIBRARY_PATH ${libdir} ${base_libdir})
148
149# add include dir to implicit includes in case it differs from /usr/include
150list(APPEND CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES ${includedir})
151list(APPEND CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES ${includedir})
152
153EOF
154}
155
156addtask generate_toolchain_file after do_patch before do_configure
157
158CONFIGURE_FILES = "CMakeLists.txt"
159
160do_configure[cleandirs] = "${@d.getVar('B') if d.getVar('S') != d.getVar('B') else ''}"
161
162cmake_do_configure() {
163 if [ "${OECMAKE_BUILDPATH}" ]; then
164 bbnote "cmake.bbclass no longer uses OECMAKE_BUILDPATH. The default behaviour is now out-of-tree builds with B=WORKDIR/build."
165 fi
166
167 if [ "${S}" = "${B}" ]; then
168 find ${B} -name CMakeFiles -or -name Makefile -or -name cmake_install.cmake -or -name CMakeCache.txt -delete
169 fi
170
171 # Just like autotools cmake can use a site file to cache result that need generated binaries to run
172 if [ -e ${WORKDIR}/site-file.cmake ] ; then
173 oecmake_sitefile="-C ${WORKDIR}/site-file.cmake"
174 else
175 oecmake_sitefile=
176 fi
177
178 cmake \
179 ${OECMAKE_GENERATOR_ARGS} \
180 $oecmake_sitefile \
181 ${OECMAKE_SOURCEPATH} \
182 -DCMAKE_INSTALL_PREFIX:PATH=${prefix} \
183 -DCMAKE_INSTALL_BINDIR:PATH=${@os.path.relpath(d.getVar('bindir'), d.getVar('prefix') + '/')} \
184 -DCMAKE_INSTALL_SBINDIR:PATH=${@os.path.relpath(d.getVar('sbindir'), d.getVar('prefix') + '/')} \
185 -DCMAKE_INSTALL_LIBEXECDIR:PATH=${@os.path.relpath(d.getVar('libexecdir'), d.getVar('prefix') + '/')} \
186 -DCMAKE_INSTALL_SYSCONFDIR:PATH=${sysconfdir} \
187 -DCMAKE_INSTALL_SHAREDSTATEDIR:PATH=${@os.path.relpath(d.getVar('sharedstatedir'), d. getVar('prefix') + '/')} \
188 -DCMAKE_INSTALL_LOCALSTATEDIR:PATH=${localstatedir} \
189 -DCMAKE_INSTALL_LIBDIR:PATH=${@os.path.relpath(d.getVar('libdir'), d.getVar('prefix') + '/')} \
190 -DCMAKE_INSTALL_INCLUDEDIR:PATH=${@os.path.relpath(d.getVar('includedir'), d.getVar('prefix') + '/')} \
191 -DCMAKE_INSTALL_DATAROOTDIR:PATH=${@os.path.relpath(d.getVar('datadir'), d.getVar('prefix') + '/')} \
192 -DPYTHON_EXECUTABLE:PATH=${PYTHON} \
193 -DPython_EXECUTABLE:PATH=${PYTHON} \
194 -DPython3_EXECUTABLE:PATH=${PYTHON} \
195 -DLIB_SUFFIX=${@d.getVar('baselib').replace('lib', '')} \
196 -DCMAKE_INSTALL_SO_NO_EXE=0 \
197 -DCMAKE_TOOLCHAIN_FILE=${WORKDIR}/toolchain.cmake \
198 -DCMAKE_NO_SYSTEM_FROM_IMPORTED=1 \
199 -DCMAKE_EXPORT_NO_PACKAGE_REGISTRY=ON \
200 -DFETCHCONTENT_FULLY_DISCONNECTED=ON \
201 ${EXTRA_OECMAKE} \
202 -Wno-dev
203}
204
205# To disable verbose cmake logs for a given recipe or globally config metadata e.g. local.conf
206# add following
207#
208# CMAKE_VERBOSE = ""
209#
210
211CMAKE_VERBOSE ??= "VERBOSE=1"
212
213# Then run do_compile again
214cmake_runcmake_build() {
215 bbnote ${DESTDIR:+DESTDIR=${DESTDIR} }${CMAKE_VERBOSE} cmake --build '${B}' "$@" -- ${EXTRA_OECMAKE_BUILD}
216 eval ${DESTDIR:+DESTDIR=${DESTDIR} }${CMAKE_VERBOSE} cmake --build '${B}' "$@" -- ${EXTRA_OECMAKE_BUILD}
217}
218
219cmake_do_compile() {
220 cmake_runcmake_build --target ${OECMAKE_TARGET_COMPILE}
221}
222
223cmake_do_install() {
224 DESTDIR='${D}' cmake_runcmake_build --target ${OECMAKE_TARGET_INSTALL}
225}
226
227EXPORT_FUNCTIONS do_configure do_compile do_install do_generate_toolchain_file