blob: fac7bbca7a36f72b6f3f7ee9a011a66e3e8e9fe0 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# Path to the CMake file to process.
Brad Bishop6e60e8b2018-02-01 10:27:11 -05002OECMAKE_SOURCEPATH ??= "${S}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -05003
Patrick Williams213cb262021-08-07 19:21:33 -05004DEPENDS:prepend = "cmake-native "
Patrick Williamsc124f4f2015-09-15 14:41:29 -05005B = "${WORKDIR}/build"
6
Brad Bishop316dfdd2018-06-25 12:45:53 -04007# What CMake generator to use.
8# The supported options are "Unix Makefiles" or "Ninja".
9OECMAKE_GENERATOR ?= "Ninja"
10
11python() {
12 generator = d.getVar("OECMAKE_GENERATOR")
Brad Bishopd89cb5f2019-04-10 09:02:41 -040013 if "Unix Makefiles" in generator:
14 args = "-G '" + generator + "' -DCMAKE_MAKE_PROGRAM=" + d.getVar("MAKE")
Brad Bishop316dfdd2018-06-25 12:45:53 -040015 d.setVar("OECMAKE_GENERATOR_ARGS", args)
16 d.setVarFlag("do_compile", "progress", "percent")
Brad Bishopd89cb5f2019-04-10 09:02:41 -040017 elif "Ninja" in generator:
18 args = "-G '" + generator + "' -DCMAKE_MAKE_PROGRAM=ninja"
Brad Bishop316dfdd2018-06-25 12:45:53 -040019 d.appendVar("DEPENDS", " ninja-native")
Brad Bishopd89cb5f2019-04-10 09:02:41 -040020 d.setVar("OECMAKE_GENERATOR_ARGS", args)
Brad Bishop977dc1a2019-02-06 16:01:43 -050021 d.setVarFlag("do_compile", "progress", r"outof:^\[(\d+)/(\d+)\]\s+")
Brad Bishop316dfdd2018-06-25 12:45:53 -040022 else:
23 bb.fatal("Unknown CMake Generator %s" % generator)
24}
Patrick Williamsc124f4f2015-09-15 14:41:29 -050025OECMAKE_AR ?= "${AR}"
26
27# Compiler flags
28OECMAKE_C_FLAGS ?= "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS} ${CFLAGS}"
29OECMAKE_CXX_FLAGS ?= "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS} ${CXXFLAGS}"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050030OECMAKE_C_FLAGS_RELEASE ?= "-DNDEBUG"
31OECMAKE_CXX_FLAGS_RELEASE ?= "-DNDEBUG"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050032OECMAKE_C_LINK_FLAGS ?= "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS} ${CPPFLAGS} ${LDFLAGS}"
33OECMAKE_CXX_LINK_FLAGS ?= "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS} ${CXXFLAGS} ${LDFLAGS}"
Brad Bishop37a0e4d2017-12-04 01:01:44 -050034CXXFLAGS += "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS}"
35CFLAGS += "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050036
Andrew Geissler635e0e42020-08-21 15:58:33 -050037def oecmake_map_compiler(compiler, d):
38 args = d.getVar(compiler).split()
39 if args[0] == "ccache":
40 return args[1], args[0]
41 return args[0], ""
42
43# C/C++ Compiler (without cpu arch/tune arguments)
44OECMAKE_C_COMPILER ?= "${@oecmake_map_compiler('CC', d)[0]}"
45OECMAKE_C_COMPILER_LAUNCHER ?= "${@oecmake_map_compiler('CC', d)[1]}"
46OECMAKE_CXX_COMPILER ?= "${@oecmake_map_compiler('CXX', d)[0]}"
47OECMAKE_CXX_COMPILER_LAUNCHER ?= "${@oecmake_map_compiler('CXX', d)[1]}"
48
49# clear compiler vars for allarch to avoid sig hash difference
50OECMAKE_C_COMPILER_allarch = ""
51OECMAKE_C_COMPILER_LAUNCHER_allarch = ""
52OECMAKE_CXX_COMPILER_allarch = ""
53OECMAKE_CXX_COMPILER_LAUNCHER_allarch = ""
Brad Bishop15ae2502019-06-18 21:44:24 -040054
Patrick Williamsc124f4f2015-09-15 14:41:29 -050055OECMAKE_RPATH ?= ""
56OECMAKE_PERLNATIVE_DIR ??= ""
57OECMAKE_EXTRA_ROOT_PATH ?= ""
58
59OECMAKE_FIND_ROOT_PATH_MODE_PROGRAM = "ONLY"
Patrick Williams213cb262021-08-07 19:21:33 -050060OECMAKE_FIND_ROOT_PATH_MODE_PROGRAM:class-native = "BOTH"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050061
Patrick Williams213cb262021-08-07 19:21:33 -050062EXTRA_OECMAKE:append = " ${PACKAGECONFIG_CONFARGS}"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050063
Andrew Geissler82c905d2020-04-13 13:39:40 -050064export CMAKE_BUILD_PARALLEL_LEVEL
Patrick Williams213cb262021-08-07 19:21:33 -050065CMAKE_BUILD_PARALLEL_LEVEL:task-compile = "${@oe.utils.parallel_make(d, False)}"
66CMAKE_BUILD_PARALLEL_LEVEL:task-install = "${@oe.utils.parallel_make(d, True)}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050067
Brad Bishop316dfdd2018-06-25 12:45:53 -040068OECMAKE_TARGET_COMPILE ?= "all"
69OECMAKE_TARGET_INSTALL ?= "install"
70
Andrew Geisslerb7d28612020-07-24 16:15:54 -050071def map_host_os_to_system_name(host_os):
72 if host_os.startswith('mingw'):
73 return 'Windows'
74 if host_os.startswith('linux'):
75 return 'Linux'
76 return host_os
77
Patrick Williamsc124f4f2015-09-15 14:41:29 -050078# CMake expects target architectures in the format of uname(2),
79# which do not always match TARGET_ARCH, so all the necessary
80# conversions should happen here.
Andrew Geisslerb7d28612020-07-24 16:15:54 -050081def map_host_arch_to_uname_arch(host_arch):
82 if host_arch == "powerpc":
Patrick Williamsc124f4f2015-09-15 14:41:29 -050083 return "ppc"
Andrew Geissler95ac1b82021-03-31 14:34:31 -050084 if host_arch == "powerpc64le":
85 return "ppc64le"
Andrew Geisslerb7d28612020-07-24 16:15:54 -050086 if host_arch == "powerpc64":
Patrick Williamsc124f4f2015-09-15 14:41:29 -050087 return "ppc64"
Andrew Geisslerb7d28612020-07-24 16:15:54 -050088 return host_arch
Patrick Williamsc124f4f2015-09-15 14:41:29 -050089
90cmake_do_generate_toolchain_file() {
Brad Bishop6e60e8b2018-02-01 10:27:11 -050091 if [ "${BUILD_SYS}" = "${HOST_SYS}" ]; then
92 cmake_crosscompiling="set( CMAKE_CROSSCOMPILING FALSE )"
93 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050094 cat > ${WORKDIR}/toolchain.cmake <<EOF
95# CMake system name must be something like "Linux".
96# This is important for cross-compiling.
Brad Bishop6e60e8b2018-02-01 10:27:11 -050097$cmake_crosscompiling
Andrew Geisslerb7d28612020-07-24 16:15:54 -050098set( CMAKE_SYSTEM_NAME ${@map_host_os_to_system_name(d.getVar('HOST_OS'))} )
99set( CMAKE_SYSTEM_PROCESSOR ${@map_host_arch_to_uname_arch(d.getVar('HOST_ARCH'))} )
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500100set( CMAKE_C_COMPILER ${OECMAKE_C_COMPILER} )
101set( CMAKE_CXX_COMPILER ${OECMAKE_CXX_COMPILER} )
Brad Bishop15ae2502019-06-18 21:44:24 -0400102set( CMAKE_C_COMPILER_LAUNCHER ${OECMAKE_C_COMPILER_LAUNCHER} )
103set( CMAKE_CXX_COMPILER_LAUNCHER ${OECMAKE_CXX_COMPILER_LAUNCHER} )
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500104set( CMAKE_ASM_COMPILER ${OECMAKE_C_COMPILER} )
Andrew Geissler95ac1b82021-03-31 14:34:31 -0500105find_program( CMAKE_AR ${OECMAKE_AR} DOC "Archiver" REQUIRED )
106
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500107set( CMAKE_C_FLAGS "${OECMAKE_C_FLAGS}" CACHE STRING "CFLAGS" )
108set( CMAKE_CXX_FLAGS "${OECMAKE_CXX_FLAGS}" CACHE STRING "CXXFLAGS" )
109set( CMAKE_ASM_FLAGS "${OECMAKE_C_FLAGS}" CACHE STRING "ASM FLAGS" )
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500110set( CMAKE_C_FLAGS_RELEASE "${OECMAKE_C_FLAGS_RELEASE}" CACHE STRING "Additional CFLAGS for release" )
111set( CMAKE_CXX_FLAGS_RELEASE "${OECMAKE_CXX_FLAGS_RELEASE}" CACHE STRING "Additional CXXFLAGS for release" )
112set( CMAKE_ASM_FLAGS_RELEASE "${OECMAKE_C_FLAGS_RELEASE}" CACHE STRING "Additional ASM FLAGS for release" )
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500113set( CMAKE_C_LINK_FLAGS "${OECMAKE_C_LINK_FLAGS}" CACHE STRING "LDFLAGS" )
114set( CMAKE_CXX_LINK_FLAGS "${OECMAKE_CXX_LINK_FLAGS}" CACHE STRING "LDFLAGS" )
115
116# only search in the paths provided so cmake doesnt pick
117# up libraries and tools from the native build machine
Brad Bishop64c979e2019-11-04 13:55:29 -0500118set( CMAKE_FIND_ROOT_PATH ${STAGING_DIR_HOST} ${STAGING_DIR_NATIVE} ${CROSS_DIR} ${OECMAKE_PERLNATIVE_DIR} ${OECMAKE_EXTRA_ROOT_PATH} ${EXTERNAL_TOOLCHAIN} ${HOSTTOOLS_DIR})
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500119set( CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY )
120set( CMAKE_FIND_ROOT_PATH_MODE_PROGRAM ${OECMAKE_FIND_ROOT_PATH_MODE_PROGRAM} )
121set( CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY )
122set( CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY )
Brad Bishop64c979e2019-11-04 13:55:29 -0500123set( CMAKE_PROGRAM_PATH "/" )
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500124
125# Use qt.conf settings
126set( ENV{QT_CONF_PATH} ${WORKDIR}/qt.conf )
127
128# We need to set the rpath to the correct directory as cmake does not provide any
129# directory as rpath by default
130set( CMAKE_INSTALL_RPATH ${OECMAKE_RPATH} )
131
Andrew Geissler82c905d2020-04-13 13:39:40 -0500132# Use RPATHs relative to build directory for reproducibility
133set( CMAKE_BUILD_RPATH_USE_ORIGIN ON )
134
Brad Bishopf3fd2882019-06-21 08:06:37 -0400135# Use our cmake modules
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500136list(APPEND CMAKE_MODULE_PATH "${STAGING_DATADIR}/cmake/Modules/")
137
138# add for non /usr/lib libdir, e.g. /usr/lib64
139set( CMAKE_LIBRARY_PATH ${libdir} ${base_libdir})
140
Brad Bishop19323692019-04-05 15:28:33 -0400141# add include dir to implicit includes in case it differs from /usr/include
142list(APPEND CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES ${includedir})
143list(APPEND CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES ${includedir})
144
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500145EOF
146}
147
148addtask generate_toolchain_file after do_patch before do_configure
149
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600150CONFIGURE_FILES = "CMakeLists.txt"
151
Andrew Geisslerc926e172021-05-07 16:11:35 -0500152do_configure[cleandirs] = "${@d.getVar('B') if d.getVar('S') != d.getVar('B') else ''}"
153
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500154cmake_do_configure() {
155 if [ "${OECMAKE_BUILDPATH}" ]; then
156 bbnote "cmake.bbclass no longer uses OECMAKE_BUILDPATH. The default behaviour is now out-of-tree builds with B=WORKDIR/build."
157 fi
158
Andrew Geisslerc926e172021-05-07 16:11:35 -0500159 if [ "${S}" = "${B}" ]; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500160 find ${B} -name CMakeFiles -or -name Makefile -or -name cmake_install.cmake -or -name CMakeCache.txt -delete
161 fi
162
163 # Just like autotools cmake can use a site file to cache result that need generated binaries to run
164 if [ -e ${WORKDIR}/site-file.cmake ] ; then
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500165 oecmake_sitefile="-C ${WORKDIR}/site-file.cmake"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500166 else
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500167 oecmake_sitefile=
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500168 fi
169
170 cmake \
Brad Bishop316dfdd2018-06-25 12:45:53 -0400171 ${OECMAKE_GENERATOR_ARGS} \
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500172 $oecmake_sitefile \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500173 ${OECMAKE_SOURCEPATH} \
174 -DCMAKE_INSTALL_PREFIX:PATH=${prefix} \
Brad Bishop15ae2502019-06-18 21:44:24 -0400175 -DCMAKE_INSTALL_BINDIR:PATH=${@os.path.relpath(d.getVar('bindir'), d.getVar('prefix') + '/')} \
176 -DCMAKE_INSTALL_SBINDIR:PATH=${@os.path.relpath(d.getVar('sbindir'), d.getVar('prefix') + '/')} \
177 -DCMAKE_INSTALL_LIBEXECDIR:PATH=${@os.path.relpath(d.getVar('libexecdir'), d.getVar('prefix') + '/')} \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500178 -DCMAKE_INSTALL_SYSCONFDIR:PATH=${sysconfdir} \
Brad Bishop15ae2502019-06-18 21:44:24 -0400179 -DCMAKE_INSTALL_SHAREDSTATEDIR:PATH=${@os.path.relpath(d.getVar('sharedstatedir'), d. getVar('prefix') + '/')} \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500180 -DCMAKE_INSTALL_LOCALSTATEDIR:PATH=${localstatedir} \
Brad Bishop15ae2502019-06-18 21:44:24 -0400181 -DCMAKE_INSTALL_LIBDIR:PATH=${@os.path.relpath(d.getVar('libdir'), d.getVar('prefix') + '/')} \
182 -DCMAKE_INSTALL_INCLUDEDIR:PATH=${@os.path.relpath(d.getVar('includedir'), d.getVar('prefix') + '/')} \
183 -DCMAKE_INSTALL_DATAROOTDIR:PATH=${@os.path.relpath(d.getVar('datadir'), d.getVar('prefix') + '/')} \
Andrew Geissler82c905d2020-04-13 13:39:40 -0500184 -DPYTHON_EXECUTABLE:PATH=${PYTHON} \
185 -DPython_EXECUTABLE:PATH=${PYTHON} \
186 -DPython3_EXECUTABLE:PATH=${PYTHON} \
187 -DLIB_SUFFIX=${@d.getVar('baselib').replace('lib', '')} \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500188 -DCMAKE_INSTALL_SO_NO_EXE=0 \
189 -DCMAKE_TOOLCHAIN_FILE=${WORKDIR}/toolchain.cmake \
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600190 -DCMAKE_NO_SYSTEM_FROM_IMPORTED=1 \
Andrew Geissler9b4d8b02021-02-19 12:26:16 -0600191 -DCMAKE_EXPORT_NO_PACKAGE_REGISTRY=ON \
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000192 -DFETCHCONTENT_FULLY_DISCONNECTED=ON \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500193 ${EXTRA_OECMAKE} \
194 -Wno-dev
195}
196
Andrew Geissler82c905d2020-04-13 13:39:40 -0500197# To disable verbose cmake logs for a given recipe or globally config metadata e.g. local.conf
198# add following
199#
200# CMAKE_VERBOSE = ""
201#
202
203CMAKE_VERBOSE ??= "VERBOSE=1"
204
205# Then run do_compile again
Brad Bishop316dfdd2018-06-25 12:45:53 -0400206cmake_runcmake_build() {
Andrew Geissler82c905d2020-04-13 13:39:40 -0500207 bbnote ${DESTDIR:+DESTDIR=${DESTDIR} }${CMAKE_VERBOSE} cmake --build '${B}' "$@" -- ${EXTRA_OECMAKE_BUILD}
208 eval ${DESTDIR:+DESTDIR=${DESTDIR} }${CMAKE_VERBOSE} cmake --build '${B}' "$@" -- ${EXTRA_OECMAKE_BUILD}
Brad Bishop316dfdd2018-06-25 12:45:53 -0400209}
210
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500211cmake_do_compile() {
Brad Bishop316dfdd2018-06-25 12:45:53 -0400212 cmake_runcmake_build --target ${OECMAKE_TARGET_COMPILE}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500213}
214
215cmake_do_install() {
Brad Bishop316dfdd2018-06-25 12:45:53 -0400216 DESTDIR='${D}' cmake_runcmake_build --target ${OECMAKE_TARGET_INSTALL}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500217}
218
219EXPORT_FUNCTIONS do_configure do_compile do_install do_generate_toolchain_file