blob: 94ed8061bb2902ca4baf57f7e7f41f9c1f419edc [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
4DEPENDS_prepend = "cmake-native "
5B = "${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)
Brad Bishop19323692019-04-05 15:28:33 -040024
25 # C/C++ Compiler (without cpu arch/tune arguments)
26 if not d.getVar('OECMAKE_C_COMPILER'):
27 cc_list = d.getVar('CC').split()
28 if cc_list[0] == 'ccache':
Brad Bishop15ae2502019-06-18 21:44:24 -040029 d.setVar('OECMAKE_C_COMPILER_LAUNCHER', cc_list[0])
30 d.setVar('OECMAKE_C_COMPILER', cc_list[1])
Brad Bishop19323692019-04-05 15:28:33 -040031 else:
32 d.setVar('OECMAKE_C_COMPILER', cc_list[0])
33
34 if not d.getVar('OECMAKE_CXX_COMPILER'):
35 cxx_list = d.getVar('CXX').split()
36 if cxx_list[0] == 'ccache':
Brad Bishop15ae2502019-06-18 21:44:24 -040037 d.setVar('OECMAKE_CXX_COMPILER_LAUNCHER', cxx_list[0])
38 d.setVar('OECMAKE_CXX_COMPILER', cxx_list[1])
Brad Bishop19323692019-04-05 15:28:33 -040039 else:
40 d.setVar('OECMAKE_CXX_COMPILER', cxx_list[0])
Brad Bishop316dfdd2018-06-25 12:45:53 -040041}
Patrick Williamsc124f4f2015-09-15 14:41:29 -050042OECMAKE_AR ?= "${AR}"
43
44# Compiler flags
45OECMAKE_C_FLAGS ?= "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS} ${CFLAGS}"
46OECMAKE_CXX_FLAGS ?= "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS} ${CXXFLAGS}"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050047OECMAKE_C_FLAGS_RELEASE ?= "-DNDEBUG"
48OECMAKE_CXX_FLAGS_RELEASE ?= "-DNDEBUG"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050049OECMAKE_C_LINK_FLAGS ?= "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS} ${CPPFLAGS} ${LDFLAGS}"
50OECMAKE_CXX_LINK_FLAGS ?= "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS} ${CXXFLAGS} ${LDFLAGS}"
Brad Bishop37a0e4d2017-12-04 01:01:44 -050051CXXFLAGS += "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS}"
52CFLAGS += "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050053
Brad Bishop15ae2502019-06-18 21:44:24 -040054OECMAKE_C_COMPILER_LAUNCHER ?= ""
55OECMAKE_CXX_COMPILER_LAUNCHER ?= ""
56
Patrick Williamsc124f4f2015-09-15 14:41:29 -050057OECMAKE_RPATH ?= ""
58OECMAKE_PERLNATIVE_DIR ??= ""
59OECMAKE_EXTRA_ROOT_PATH ?= ""
60
61OECMAKE_FIND_ROOT_PATH_MODE_PROGRAM = "ONLY"
62OECMAKE_FIND_ROOT_PATH_MODE_PROGRAM_class-native = "BOTH"
63
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050064EXTRA_OECMAKE_append = " ${PACKAGECONFIG_CONFARGS}"
65
Andrew Geissler82c905d2020-04-13 13:39:40 -050066export CMAKE_BUILD_PARALLEL_LEVEL
67CMAKE_BUILD_PARALLEL_LEVEL_task-compile = "${@oe.utils.parallel_make(d, False)}"
68CMAKE_BUILD_PARALLEL_LEVEL_task-install = "${@oe.utils.parallel_make(d, True)}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050069
Brad Bishop316dfdd2018-06-25 12:45:53 -040070OECMAKE_TARGET_COMPILE ?= "all"
71OECMAKE_TARGET_INSTALL ?= "install"
72
Patrick Williamsc124f4f2015-09-15 14:41:29 -050073# CMake expects target architectures in the format of uname(2),
74# which do not always match TARGET_ARCH, so all the necessary
75# conversions should happen here.
76def map_target_arch_to_uname_arch(target_arch):
77 if target_arch == "powerpc":
78 return "ppc"
79 if target_arch == "powerpc64":
80 return "ppc64"
81 return target_arch
82
83cmake_do_generate_toolchain_file() {
Brad Bishop6e60e8b2018-02-01 10:27:11 -050084 if [ "${BUILD_SYS}" = "${HOST_SYS}" ]; then
85 cmake_crosscompiling="set( CMAKE_CROSSCOMPILING FALSE )"
86 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050087 cat > ${WORKDIR}/toolchain.cmake <<EOF
88# CMake system name must be something like "Linux".
89# This is important for cross-compiling.
Brad Bishop6e60e8b2018-02-01 10:27:11 -050090$cmake_crosscompiling
Patrick Williamsc124f4f2015-09-15 14:41:29 -050091set( CMAKE_SYSTEM_NAME `echo ${TARGET_OS} | sed -e 's/^./\u&/' -e 's/^\(Linux\).*/\1/'` )
Brad Bishop6e60e8b2018-02-01 10:27:11 -050092set( CMAKE_SYSTEM_PROCESSOR ${@map_target_arch_to_uname_arch(d.getVar('TARGET_ARCH'))} )
Patrick Williamsc124f4f2015-09-15 14:41:29 -050093set( CMAKE_C_COMPILER ${OECMAKE_C_COMPILER} )
94set( CMAKE_CXX_COMPILER ${OECMAKE_CXX_COMPILER} )
Brad Bishop15ae2502019-06-18 21:44:24 -040095set( CMAKE_C_COMPILER_LAUNCHER ${OECMAKE_C_COMPILER_LAUNCHER} )
96set( CMAKE_CXX_COMPILER_LAUNCHER ${OECMAKE_CXX_COMPILER_LAUNCHER} )
Patrick Williamsc124f4f2015-09-15 14:41:29 -050097set( CMAKE_ASM_COMPILER ${OECMAKE_C_COMPILER} )
98set( CMAKE_AR ${OECMAKE_AR} CACHE FILEPATH "Archiver" )
99set( CMAKE_C_FLAGS "${OECMAKE_C_FLAGS}" CACHE STRING "CFLAGS" )
100set( CMAKE_CXX_FLAGS "${OECMAKE_CXX_FLAGS}" CACHE STRING "CXXFLAGS" )
101set( CMAKE_ASM_FLAGS "${OECMAKE_C_FLAGS}" CACHE STRING "ASM FLAGS" )
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500102set( CMAKE_C_FLAGS_RELEASE "${OECMAKE_C_FLAGS_RELEASE}" CACHE STRING "Additional CFLAGS for release" )
103set( CMAKE_CXX_FLAGS_RELEASE "${OECMAKE_CXX_FLAGS_RELEASE}" CACHE STRING "Additional CXXFLAGS for release" )
104set( CMAKE_ASM_FLAGS_RELEASE "${OECMAKE_C_FLAGS_RELEASE}" CACHE STRING "Additional ASM FLAGS for release" )
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500105set( CMAKE_C_LINK_FLAGS "${OECMAKE_C_LINK_FLAGS}" CACHE STRING "LDFLAGS" )
106set( CMAKE_CXX_LINK_FLAGS "${OECMAKE_CXX_LINK_FLAGS}" CACHE STRING "LDFLAGS" )
107
108# only search in the paths provided so cmake doesnt pick
109# up libraries and tools from the native build machine
Brad Bishop64c979e2019-11-04 13:55:29 -0500110set( 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 -0500111set( CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY )
112set( CMAKE_FIND_ROOT_PATH_MODE_PROGRAM ${OECMAKE_FIND_ROOT_PATH_MODE_PROGRAM} )
113set( CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY )
114set( CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY )
Brad Bishop64c979e2019-11-04 13:55:29 -0500115set( CMAKE_PROGRAM_PATH "/" )
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500116
117# Use qt.conf settings
118set( ENV{QT_CONF_PATH} ${WORKDIR}/qt.conf )
119
120# We need to set the rpath to the correct directory as cmake does not provide any
121# directory as rpath by default
122set( CMAKE_INSTALL_RPATH ${OECMAKE_RPATH} )
123
Andrew Geissler82c905d2020-04-13 13:39:40 -0500124# Use RPATHs relative to build directory for reproducibility
125set( CMAKE_BUILD_RPATH_USE_ORIGIN ON )
126
Brad Bishopf3fd2882019-06-21 08:06:37 -0400127# Use our cmake modules
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500128list(APPEND CMAKE_MODULE_PATH "${STAGING_DATADIR}/cmake/Modules/")
129
130# add for non /usr/lib libdir, e.g. /usr/lib64
131set( CMAKE_LIBRARY_PATH ${libdir} ${base_libdir})
132
Brad Bishop19323692019-04-05 15:28:33 -0400133# add include dir to implicit includes in case it differs from /usr/include
134list(APPEND CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES ${includedir})
135list(APPEND CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES ${includedir})
136
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500137EOF
138}
139
140addtask generate_toolchain_file after do_patch before do_configure
141
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600142CONFIGURE_FILES = "CMakeLists.txt"
143
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500144cmake_do_configure() {
145 if [ "${OECMAKE_BUILDPATH}" ]; then
146 bbnote "cmake.bbclass no longer uses OECMAKE_BUILDPATH. The default behaviour is now out-of-tree builds with B=WORKDIR/build."
147 fi
148
149 if [ "${S}" != "${B}" ]; then
150 rm -rf ${B}
151 mkdir -p ${B}
152 cd ${B}
153 else
154 find ${B} -name CMakeFiles -or -name Makefile -or -name cmake_install.cmake -or -name CMakeCache.txt -delete
155 fi
156
157 # Just like autotools cmake can use a site file to cache result that need generated binaries to run
158 if [ -e ${WORKDIR}/site-file.cmake ] ; then
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500159 oecmake_sitefile="-C ${WORKDIR}/site-file.cmake"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500160 else
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500161 oecmake_sitefile=
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500162 fi
163
164 cmake \
Brad Bishop316dfdd2018-06-25 12:45:53 -0400165 ${OECMAKE_GENERATOR_ARGS} \
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500166 $oecmake_sitefile \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500167 ${OECMAKE_SOURCEPATH} \
168 -DCMAKE_INSTALL_PREFIX:PATH=${prefix} \
Brad Bishop15ae2502019-06-18 21:44:24 -0400169 -DCMAKE_INSTALL_BINDIR:PATH=${@os.path.relpath(d.getVar('bindir'), d.getVar('prefix') + '/')} \
170 -DCMAKE_INSTALL_SBINDIR:PATH=${@os.path.relpath(d.getVar('sbindir'), d.getVar('prefix') + '/')} \
171 -DCMAKE_INSTALL_LIBEXECDIR:PATH=${@os.path.relpath(d.getVar('libexecdir'), d.getVar('prefix') + '/')} \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500172 -DCMAKE_INSTALL_SYSCONFDIR:PATH=${sysconfdir} \
Brad Bishop15ae2502019-06-18 21:44:24 -0400173 -DCMAKE_INSTALL_SHAREDSTATEDIR:PATH=${@os.path.relpath(d.getVar('sharedstatedir'), d. getVar('prefix') + '/')} \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500174 -DCMAKE_INSTALL_LOCALSTATEDIR:PATH=${localstatedir} \
Brad Bishop15ae2502019-06-18 21:44:24 -0400175 -DCMAKE_INSTALL_LIBDIR:PATH=${@os.path.relpath(d.getVar('libdir'), d.getVar('prefix') + '/')} \
176 -DCMAKE_INSTALL_INCLUDEDIR:PATH=${@os.path.relpath(d.getVar('includedir'), d.getVar('prefix') + '/')} \
177 -DCMAKE_INSTALL_DATAROOTDIR:PATH=${@os.path.relpath(d.getVar('datadir'), d.getVar('prefix') + '/')} \
Andrew Geissler82c905d2020-04-13 13:39:40 -0500178 -DPYTHON_EXECUTABLE:PATH=${PYTHON} \
179 -DPython_EXECUTABLE:PATH=${PYTHON} \
180 -DPython3_EXECUTABLE:PATH=${PYTHON} \
181 -DLIB_SUFFIX=${@d.getVar('baselib').replace('lib', '')} \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500182 -DCMAKE_INSTALL_SO_NO_EXE=0 \
183 -DCMAKE_TOOLCHAIN_FILE=${WORKDIR}/toolchain.cmake \
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600184 -DCMAKE_NO_SYSTEM_FROM_IMPORTED=1 \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500185 ${EXTRA_OECMAKE} \
186 -Wno-dev
187}
188
Andrew Geissler82c905d2020-04-13 13:39:40 -0500189# To disable verbose cmake logs for a given recipe or globally config metadata e.g. local.conf
190# add following
191#
192# CMAKE_VERBOSE = ""
193#
194
195CMAKE_VERBOSE ??= "VERBOSE=1"
196
197# Then run do_compile again
Brad Bishop316dfdd2018-06-25 12:45:53 -0400198cmake_runcmake_build() {
Andrew Geissler82c905d2020-04-13 13:39:40 -0500199 bbnote ${DESTDIR:+DESTDIR=${DESTDIR} }${CMAKE_VERBOSE} cmake --build '${B}' "$@" -- ${EXTRA_OECMAKE_BUILD}
200 eval ${DESTDIR:+DESTDIR=${DESTDIR} }${CMAKE_VERBOSE} cmake --build '${B}' "$@" -- ${EXTRA_OECMAKE_BUILD}
Brad Bishop316dfdd2018-06-25 12:45:53 -0400201}
202
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500203cmake_do_compile() {
Brad Bishop316dfdd2018-06-25 12:45:53 -0400204 cmake_runcmake_build --target ${OECMAKE_TARGET_COMPILE}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500205}
206
207cmake_do_install() {
Brad Bishop316dfdd2018-06-25 12:45:53 -0400208 DESTDIR='${D}' cmake_runcmake_build --target ${OECMAKE_TARGET_INSTALL}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500209}
210
211EXPORT_FUNCTIONS do_configure do_compile do_install do_generate_toolchain_file