blob: d3f0d70847e57028a410f0824967646eabf81364 [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':
29 d.setVar('OECMAKE_C_COMPILER', '%s %s' % (cc_list[0], cc_list[1]))
30 else:
31 d.setVar('OECMAKE_C_COMPILER', cc_list[0])
32
33 if not d.getVar('OECMAKE_CXX_COMPILER'):
34 cxx_list = d.getVar('CXX').split()
35 if cxx_list[0] == 'ccache':
36 d.setVar('OECMAKE_CXX_COMPILER', '%s %s' % (cxx_list[0], cxx_list[1]))
37 else:
38 d.setVar('OECMAKE_CXX_COMPILER', cxx_list[0])
Brad Bishop316dfdd2018-06-25 12:45:53 -040039}
Patrick Williamsc124f4f2015-09-15 14:41:29 -050040OECMAKE_AR ?= "${AR}"
41
42# Compiler flags
43OECMAKE_C_FLAGS ?= "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS} ${CFLAGS}"
44OECMAKE_CXX_FLAGS ?= "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS} ${CXXFLAGS}"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050045OECMAKE_C_FLAGS_RELEASE ?= "-DNDEBUG"
46OECMAKE_CXX_FLAGS_RELEASE ?= "-DNDEBUG"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050047OECMAKE_C_LINK_FLAGS ?= "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS} ${CPPFLAGS} ${LDFLAGS}"
48OECMAKE_CXX_LINK_FLAGS ?= "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS} ${CXXFLAGS} ${LDFLAGS}"
Brad Bishop37a0e4d2017-12-04 01:01:44 -050049CXXFLAGS += "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS}"
50CFLAGS += "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050051
52OECMAKE_RPATH ?= ""
53OECMAKE_PERLNATIVE_DIR ??= ""
54OECMAKE_EXTRA_ROOT_PATH ?= ""
55
56OECMAKE_FIND_ROOT_PATH_MODE_PROGRAM = "ONLY"
57OECMAKE_FIND_ROOT_PATH_MODE_PROGRAM_class-native = "BOTH"
58
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050059EXTRA_OECMAKE_append = " ${PACKAGECONFIG_CONFARGS}"
60
Brad Bishopd7bf8c12018-02-25 22:55:05 -050061EXTRA_OECMAKE_BUILD_prepend_task-compile = "${PARALLEL_MAKE} "
62EXTRA_OECMAKE_BUILD_prepend_task-install = "${PARALLEL_MAKEINST} "
63
Brad Bishop316dfdd2018-06-25 12:45:53 -040064OECMAKE_TARGET_COMPILE ?= "all"
65OECMAKE_TARGET_INSTALL ?= "install"
66
Patrick Williamsc124f4f2015-09-15 14:41:29 -050067# CMake expects target architectures in the format of uname(2),
68# which do not always match TARGET_ARCH, so all the necessary
69# conversions should happen here.
70def map_target_arch_to_uname_arch(target_arch):
71 if target_arch == "powerpc":
72 return "ppc"
73 if target_arch == "powerpc64":
74 return "ppc64"
75 return target_arch
76
77cmake_do_generate_toolchain_file() {
Brad Bishop6e60e8b2018-02-01 10:27:11 -050078 if [ "${BUILD_SYS}" = "${HOST_SYS}" ]; then
79 cmake_crosscompiling="set( CMAKE_CROSSCOMPILING FALSE )"
80 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050081 cat > ${WORKDIR}/toolchain.cmake <<EOF
82# CMake system name must be something like "Linux".
83# This is important for cross-compiling.
Brad Bishop6e60e8b2018-02-01 10:27:11 -050084$cmake_crosscompiling
Patrick Williamsc124f4f2015-09-15 14:41:29 -050085set( CMAKE_SYSTEM_NAME `echo ${TARGET_OS} | sed -e 's/^./\u&/' -e 's/^\(Linux\).*/\1/'` )
Brad Bishop6e60e8b2018-02-01 10:27:11 -050086set( CMAKE_SYSTEM_PROCESSOR ${@map_target_arch_to_uname_arch(d.getVar('TARGET_ARCH'))} )
Patrick Williamsc124f4f2015-09-15 14:41:29 -050087set( CMAKE_C_COMPILER ${OECMAKE_C_COMPILER} )
88set( CMAKE_CXX_COMPILER ${OECMAKE_CXX_COMPILER} )
89set( CMAKE_ASM_COMPILER ${OECMAKE_C_COMPILER} )
90set( CMAKE_AR ${OECMAKE_AR} CACHE FILEPATH "Archiver" )
91set( CMAKE_C_FLAGS "${OECMAKE_C_FLAGS}" CACHE STRING "CFLAGS" )
92set( CMAKE_CXX_FLAGS "${OECMAKE_CXX_FLAGS}" CACHE STRING "CXXFLAGS" )
93set( CMAKE_ASM_FLAGS "${OECMAKE_C_FLAGS}" CACHE STRING "ASM FLAGS" )
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050094set( CMAKE_C_FLAGS_RELEASE "${OECMAKE_C_FLAGS_RELEASE}" CACHE STRING "Additional CFLAGS for release" )
95set( CMAKE_CXX_FLAGS_RELEASE "${OECMAKE_CXX_FLAGS_RELEASE}" CACHE STRING "Additional CXXFLAGS for release" )
96set( CMAKE_ASM_FLAGS_RELEASE "${OECMAKE_C_FLAGS_RELEASE}" CACHE STRING "Additional ASM FLAGS for release" )
Patrick Williamsc124f4f2015-09-15 14:41:29 -050097set( CMAKE_C_LINK_FLAGS "${OECMAKE_C_LINK_FLAGS}" CACHE STRING "LDFLAGS" )
98set( CMAKE_CXX_LINK_FLAGS "${OECMAKE_CXX_LINK_FLAGS}" CACHE STRING "LDFLAGS" )
99
100# only search in the paths provided so cmake doesnt pick
101# up libraries and tools from the native build machine
102set( CMAKE_FIND_ROOT_PATH ${STAGING_DIR_HOST} ${STAGING_DIR_NATIVE} ${CROSS_DIR} ${OECMAKE_PERLNATIVE_DIR} ${OECMAKE_EXTRA_ROOT_PATH} ${EXTERNAL_TOOLCHAIN})
103set( CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY )
104set( CMAKE_FIND_ROOT_PATH_MODE_PROGRAM ${OECMAKE_FIND_ROOT_PATH_MODE_PROGRAM} )
105set( CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY )
106set( CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY )
107
108# Use qt.conf settings
109set( ENV{QT_CONF_PATH} ${WORKDIR}/qt.conf )
110
111# We need to set the rpath to the correct directory as cmake does not provide any
112# directory as rpath by default
113set( CMAKE_INSTALL_RPATH ${OECMAKE_RPATH} )
114
115# Use native cmake modules
116list(APPEND CMAKE_MODULE_PATH "${STAGING_DATADIR}/cmake/Modules/")
117
118# add for non /usr/lib libdir, e.g. /usr/lib64
119set( CMAKE_LIBRARY_PATH ${libdir} ${base_libdir})
120
Brad Bishop19323692019-04-05 15:28:33 -0400121# add include dir to implicit includes in case it differs from /usr/include
122list(APPEND CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES ${includedir})
123list(APPEND CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES ${includedir})
124
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500125EOF
126}
127
128addtask generate_toolchain_file after do_patch before do_configure
129
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600130CONFIGURE_FILES = "CMakeLists.txt"
131
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500132cmake_do_configure() {
133 if [ "${OECMAKE_BUILDPATH}" ]; then
134 bbnote "cmake.bbclass no longer uses OECMAKE_BUILDPATH. The default behaviour is now out-of-tree builds with B=WORKDIR/build."
135 fi
136
137 if [ "${S}" != "${B}" ]; then
138 rm -rf ${B}
139 mkdir -p ${B}
140 cd ${B}
141 else
142 find ${B} -name CMakeFiles -or -name Makefile -or -name cmake_install.cmake -or -name CMakeCache.txt -delete
143 fi
144
145 # Just like autotools cmake can use a site file to cache result that need generated binaries to run
146 if [ -e ${WORKDIR}/site-file.cmake ] ; then
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500147 oecmake_sitefile="-C ${WORKDIR}/site-file.cmake"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500148 else
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500149 oecmake_sitefile=
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500150 fi
151
152 cmake \
Brad Bishop316dfdd2018-06-25 12:45:53 -0400153 ${OECMAKE_GENERATOR_ARGS} \
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500154 $oecmake_sitefile \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500155 ${OECMAKE_SOURCEPATH} \
156 -DCMAKE_INSTALL_PREFIX:PATH=${prefix} \
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500157 -DCMAKE_INSTALL_BINDIR:PATH=${@os.path.relpath(d.getVar('bindir'), d.getVar('prefix'))} \
158 -DCMAKE_INSTALL_SBINDIR:PATH=${@os.path.relpath(d.getVar('sbindir'), d.getVar('prefix'))} \
159 -DCMAKE_INSTALL_LIBEXECDIR:PATH=${@os.path.relpath(d.getVar('libexecdir'), d.getVar('prefix'))} \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500160 -DCMAKE_INSTALL_SYSCONFDIR:PATH=${sysconfdir} \
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500161 -DCMAKE_INSTALL_SHAREDSTATEDIR:PATH=${@os.path.relpath(d.getVar('sharedstatedir'), d. getVar('prefix'))} \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500162 -DCMAKE_INSTALL_LOCALSTATEDIR:PATH=${localstatedir} \
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500163 -DCMAKE_INSTALL_LIBDIR:PATH=${@os.path.relpath(d.getVar('libdir'), d.getVar('prefix'))} \
164 -DCMAKE_INSTALL_INCLUDEDIR:PATH=${@os.path.relpath(d.getVar('includedir'), d.getVar('prefix'))} \
165 -DCMAKE_INSTALL_DATAROOTDIR:PATH=${@os.path.relpath(d.getVar('datadir'), d.getVar('prefix'))} \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500166 -DCMAKE_INSTALL_SO_NO_EXE=0 \
167 -DCMAKE_TOOLCHAIN_FILE=${WORKDIR}/toolchain.cmake \
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600168 -DCMAKE_NO_SYSTEM_FROM_IMPORTED=1 \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500169 ${EXTRA_OECMAKE} \
170 -Wno-dev
171}
172
Brad Bishop316dfdd2018-06-25 12:45:53 -0400173cmake_runcmake_build() {
174 bbnote ${DESTDIR:+DESTDIR=${DESTDIR} }VERBOSE=1 cmake --build '${B}' "$@" -- ${EXTRA_OECMAKE_BUILD}
175 eval ${DESTDIR:+DESTDIR=${DESTDIR} }VERBOSE=1 cmake --build '${B}' "$@" -- ${EXTRA_OECMAKE_BUILD}
176}
177
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500178cmake_do_compile() {
Brad Bishop316dfdd2018-06-25 12:45:53 -0400179 cmake_runcmake_build --target ${OECMAKE_TARGET_COMPILE}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500180}
181
182cmake_do_install() {
Brad Bishop316dfdd2018-06-25 12:45:53 -0400183 DESTDIR='${D}' cmake_runcmake_build --target ${OECMAKE_TARGET_INSTALL}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500184}
185
186EXPORT_FUNCTIONS do_configure do_compile do_install do_generate_toolchain_file