blob: e16630434e5aa8ca7ad8d5e1aeb953b3cd2fe0a7 [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")
13 if generator == "Unix Makefiles":
14 args = "-G 'Unix Makefiles' -DCMAKE_MAKE_PROGRAM=" + d.getVar("MAKE")
15 d.setVar("OECMAKE_GENERATOR_ARGS", args)
16 d.setVarFlag("do_compile", "progress", "percent")
17 elif generator == "Ninja":
18 d.appendVar("DEPENDS", " ninja-native")
19 d.setVar("OECMAKE_GENERATOR_ARGS", "-G Ninja -DCMAKE_MAKE_PROGRAM=ninja")
Brad Bishop977dc1a2019-02-06 16:01:43 -050020 d.setVarFlag("do_compile", "progress", r"outof:^\[(\d+)/(\d+)\]\s+")
Brad Bishop316dfdd2018-06-25 12:45:53 -040021 else:
22 bb.fatal("Unknown CMake Generator %s" % generator)
Brad Bishop19323692019-04-05 15:28:33 -040023
24 # C/C++ Compiler (without cpu arch/tune arguments)
25 if not d.getVar('OECMAKE_C_COMPILER'):
26 cc_list = d.getVar('CC').split()
27 if cc_list[0] == 'ccache':
28 d.setVar('OECMAKE_C_COMPILER', '%s %s' % (cc_list[0], cc_list[1]))
29 else:
30 d.setVar('OECMAKE_C_COMPILER', cc_list[0])
31
32 if not d.getVar('OECMAKE_CXX_COMPILER'):
33 cxx_list = d.getVar('CXX').split()
34 if cxx_list[0] == 'ccache':
35 d.setVar('OECMAKE_CXX_COMPILER', '%s %s' % (cxx_list[0], cxx_list[1]))
36 else:
37 d.setVar('OECMAKE_CXX_COMPILER', cxx_list[0])
Brad Bishop316dfdd2018-06-25 12:45:53 -040038}
Patrick Williamsc124f4f2015-09-15 14:41:29 -050039OECMAKE_AR ?= "${AR}"
40
41# Compiler flags
42OECMAKE_C_FLAGS ?= "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS} ${CFLAGS}"
43OECMAKE_CXX_FLAGS ?= "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS} ${CXXFLAGS}"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050044OECMAKE_C_FLAGS_RELEASE ?= "-DNDEBUG"
45OECMAKE_CXX_FLAGS_RELEASE ?= "-DNDEBUG"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050046OECMAKE_C_LINK_FLAGS ?= "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS} ${CPPFLAGS} ${LDFLAGS}"
47OECMAKE_CXX_LINK_FLAGS ?= "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS} ${CXXFLAGS} ${LDFLAGS}"
Brad Bishop37a0e4d2017-12-04 01:01:44 -050048CXXFLAGS += "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS}"
49CFLAGS += "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050050
51OECMAKE_RPATH ?= ""
52OECMAKE_PERLNATIVE_DIR ??= ""
53OECMAKE_EXTRA_ROOT_PATH ?= ""
54
55OECMAKE_FIND_ROOT_PATH_MODE_PROGRAM = "ONLY"
56OECMAKE_FIND_ROOT_PATH_MODE_PROGRAM_class-native = "BOTH"
57
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050058EXTRA_OECMAKE_append = " ${PACKAGECONFIG_CONFARGS}"
59
Brad Bishopd7bf8c12018-02-25 22:55:05 -050060EXTRA_OECMAKE_BUILD_prepend_task-compile = "${PARALLEL_MAKE} "
61EXTRA_OECMAKE_BUILD_prepend_task-install = "${PARALLEL_MAKEINST} "
62
Brad Bishop316dfdd2018-06-25 12:45:53 -040063OECMAKE_TARGET_COMPILE ?= "all"
64OECMAKE_TARGET_INSTALL ?= "install"
65
Patrick Williamsc124f4f2015-09-15 14:41:29 -050066# CMake expects target architectures in the format of uname(2),
67# which do not always match TARGET_ARCH, so all the necessary
68# conversions should happen here.
69def map_target_arch_to_uname_arch(target_arch):
70 if target_arch == "powerpc":
71 return "ppc"
72 if target_arch == "powerpc64":
73 return "ppc64"
74 return target_arch
75
76cmake_do_generate_toolchain_file() {
Brad Bishop6e60e8b2018-02-01 10:27:11 -050077 if [ "${BUILD_SYS}" = "${HOST_SYS}" ]; then
78 cmake_crosscompiling="set( CMAKE_CROSSCOMPILING FALSE )"
79 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050080 cat > ${WORKDIR}/toolchain.cmake <<EOF
81# CMake system name must be something like "Linux".
82# This is important for cross-compiling.
Brad Bishop6e60e8b2018-02-01 10:27:11 -050083$cmake_crosscompiling
Patrick Williamsc124f4f2015-09-15 14:41:29 -050084set( CMAKE_SYSTEM_NAME `echo ${TARGET_OS} | sed -e 's/^./\u&/' -e 's/^\(Linux\).*/\1/'` )
Brad Bishop6e60e8b2018-02-01 10:27:11 -050085set( CMAKE_SYSTEM_PROCESSOR ${@map_target_arch_to_uname_arch(d.getVar('TARGET_ARCH'))} )
Patrick Williamsc124f4f2015-09-15 14:41:29 -050086set( CMAKE_C_COMPILER ${OECMAKE_C_COMPILER} )
87set( CMAKE_CXX_COMPILER ${OECMAKE_CXX_COMPILER} )
88set( CMAKE_ASM_COMPILER ${OECMAKE_C_COMPILER} )
89set( CMAKE_AR ${OECMAKE_AR} CACHE FILEPATH "Archiver" )
90set( CMAKE_C_FLAGS "${OECMAKE_C_FLAGS}" CACHE STRING "CFLAGS" )
91set( CMAKE_CXX_FLAGS "${OECMAKE_CXX_FLAGS}" CACHE STRING "CXXFLAGS" )
92set( CMAKE_ASM_FLAGS "${OECMAKE_C_FLAGS}" CACHE STRING "ASM FLAGS" )
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050093set( CMAKE_C_FLAGS_RELEASE "${OECMAKE_C_FLAGS_RELEASE}" CACHE STRING "Additional CFLAGS for release" )
94set( CMAKE_CXX_FLAGS_RELEASE "${OECMAKE_CXX_FLAGS_RELEASE}" CACHE STRING "Additional CXXFLAGS for release" )
95set( CMAKE_ASM_FLAGS_RELEASE "${OECMAKE_C_FLAGS_RELEASE}" CACHE STRING "Additional ASM FLAGS for release" )
Patrick Williamsc124f4f2015-09-15 14:41:29 -050096set( CMAKE_C_LINK_FLAGS "${OECMAKE_C_LINK_FLAGS}" CACHE STRING "LDFLAGS" )
97set( CMAKE_CXX_LINK_FLAGS "${OECMAKE_CXX_LINK_FLAGS}" CACHE STRING "LDFLAGS" )
98
99# only search in the paths provided so cmake doesnt pick
100# up libraries and tools from the native build machine
101set( CMAKE_FIND_ROOT_PATH ${STAGING_DIR_HOST} ${STAGING_DIR_NATIVE} ${CROSS_DIR} ${OECMAKE_PERLNATIVE_DIR} ${OECMAKE_EXTRA_ROOT_PATH} ${EXTERNAL_TOOLCHAIN})
102set( CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY )
103set( CMAKE_FIND_ROOT_PATH_MODE_PROGRAM ${OECMAKE_FIND_ROOT_PATH_MODE_PROGRAM} )
104set( CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY )
105set( CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY )
106
107# Use qt.conf settings
108set( ENV{QT_CONF_PATH} ${WORKDIR}/qt.conf )
109
110# We need to set the rpath to the correct directory as cmake does not provide any
111# directory as rpath by default
112set( CMAKE_INSTALL_RPATH ${OECMAKE_RPATH} )
113
114# Use native cmake modules
115list(APPEND CMAKE_MODULE_PATH "${STAGING_DATADIR}/cmake/Modules/")
116
117# add for non /usr/lib libdir, e.g. /usr/lib64
118set( CMAKE_LIBRARY_PATH ${libdir} ${base_libdir})
119
Brad Bishop19323692019-04-05 15:28:33 -0400120# add include dir to implicit includes in case it differs from /usr/include
121list(APPEND CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES ${includedir})
122list(APPEND CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES ${includedir})
123
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500124EOF
125}
126
127addtask generate_toolchain_file after do_patch before do_configure
128
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600129CONFIGURE_FILES = "CMakeLists.txt"
130
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500131cmake_do_configure() {
132 if [ "${OECMAKE_BUILDPATH}" ]; then
133 bbnote "cmake.bbclass no longer uses OECMAKE_BUILDPATH. The default behaviour is now out-of-tree builds with B=WORKDIR/build."
134 fi
135
136 if [ "${S}" != "${B}" ]; then
137 rm -rf ${B}
138 mkdir -p ${B}
139 cd ${B}
140 else
141 find ${B} -name CMakeFiles -or -name Makefile -or -name cmake_install.cmake -or -name CMakeCache.txt -delete
142 fi
143
144 # Just like autotools cmake can use a site file to cache result that need generated binaries to run
145 if [ -e ${WORKDIR}/site-file.cmake ] ; then
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500146 oecmake_sitefile="-C ${WORKDIR}/site-file.cmake"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500147 else
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500148 oecmake_sitefile=
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500149 fi
150
151 cmake \
Brad Bishop316dfdd2018-06-25 12:45:53 -0400152 ${OECMAKE_GENERATOR_ARGS} \
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500153 $oecmake_sitefile \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500154 ${OECMAKE_SOURCEPATH} \
155 -DCMAKE_INSTALL_PREFIX:PATH=${prefix} \
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500156 -DCMAKE_INSTALL_BINDIR:PATH=${@os.path.relpath(d.getVar('bindir'), d.getVar('prefix'))} \
157 -DCMAKE_INSTALL_SBINDIR:PATH=${@os.path.relpath(d.getVar('sbindir'), d.getVar('prefix'))} \
158 -DCMAKE_INSTALL_LIBEXECDIR:PATH=${@os.path.relpath(d.getVar('libexecdir'), d.getVar('prefix'))} \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500159 -DCMAKE_INSTALL_SYSCONFDIR:PATH=${sysconfdir} \
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500160 -DCMAKE_INSTALL_SHAREDSTATEDIR:PATH=${@os.path.relpath(d.getVar('sharedstatedir'), d. getVar('prefix'))} \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500161 -DCMAKE_INSTALL_LOCALSTATEDIR:PATH=${localstatedir} \
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500162 -DCMAKE_INSTALL_LIBDIR:PATH=${@os.path.relpath(d.getVar('libdir'), d.getVar('prefix'))} \
163 -DCMAKE_INSTALL_INCLUDEDIR:PATH=${@os.path.relpath(d.getVar('includedir'), d.getVar('prefix'))} \
164 -DCMAKE_INSTALL_DATAROOTDIR:PATH=${@os.path.relpath(d.getVar('datadir'), d.getVar('prefix'))} \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500165 -DCMAKE_INSTALL_SO_NO_EXE=0 \
166 -DCMAKE_TOOLCHAIN_FILE=${WORKDIR}/toolchain.cmake \
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600167 -DCMAKE_NO_SYSTEM_FROM_IMPORTED=1 \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500168 ${EXTRA_OECMAKE} \
169 -Wno-dev
170}
171
Brad Bishop316dfdd2018-06-25 12:45:53 -0400172cmake_runcmake_build() {
173 bbnote ${DESTDIR:+DESTDIR=${DESTDIR} }VERBOSE=1 cmake --build '${B}' "$@" -- ${EXTRA_OECMAKE_BUILD}
174 eval ${DESTDIR:+DESTDIR=${DESTDIR} }VERBOSE=1 cmake --build '${B}' "$@" -- ${EXTRA_OECMAKE_BUILD}
175}
176
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500177cmake_do_compile() {
Brad Bishop316dfdd2018-06-25 12:45:53 -0400178 cmake_runcmake_build --target ${OECMAKE_TARGET_COMPILE}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500179}
180
181cmake_do_install() {
Brad Bishop316dfdd2018-06-25 12:45:53 -0400182 DESTDIR='${D}' cmake_runcmake_build --target ${OECMAKE_TARGET_INSTALL}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500183}
184
185EXPORT_FUNCTIONS do_configure do_compile do_install do_generate_toolchain_file