blob: b364d2bc20d8ec142110f02d0b3d02d67897a59a [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
7# We need to unset CCACHE otherwise cmake gets too confused
8CCACHE = ""
9
Brad Bishop316dfdd2018-06-25 12:45:53 -040010# What CMake generator to use.
11# The supported options are "Unix Makefiles" or "Ninja".
12OECMAKE_GENERATOR ?= "Ninja"
13
14python() {
15 generator = d.getVar("OECMAKE_GENERATOR")
16 if generator == "Unix Makefiles":
17 args = "-G 'Unix Makefiles' -DCMAKE_MAKE_PROGRAM=" + d.getVar("MAKE")
18 d.setVar("OECMAKE_GENERATOR_ARGS", args)
19 d.setVarFlag("do_compile", "progress", "percent")
20 elif generator == "Ninja":
21 d.appendVar("DEPENDS", " ninja-native")
22 d.setVar("OECMAKE_GENERATOR_ARGS", "-G Ninja -DCMAKE_MAKE_PROGRAM=ninja")
Brad Bishop977dc1a2019-02-06 16:01:43 -050023 d.setVarFlag("do_compile", "progress", r"outof:^\[(\d+)/(\d+)\]\s+")
Brad Bishop316dfdd2018-06-25 12:45:53 -040024 else:
25 bb.fatal("Unknown CMake Generator %s" % generator)
26}
Patrick Williamsc124f4f2015-09-15 14:41:29 -050027# C/C++ Compiler (without cpu arch/tune arguments)
28OECMAKE_C_COMPILER ?= "`echo ${CC} | sed 's/^\([^ ]*\).*/\1/'`"
29OECMAKE_CXX_COMPILER ?= "`echo ${CXX} | sed 's/^\([^ ]*\).*/\1/'`"
30OECMAKE_AR ?= "${AR}"
31
32# Compiler flags
33OECMAKE_C_FLAGS ?= "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS} ${CFLAGS}"
34OECMAKE_CXX_FLAGS ?= "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS} ${CXXFLAGS}"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050035OECMAKE_C_FLAGS_RELEASE ?= "-DNDEBUG"
36OECMAKE_CXX_FLAGS_RELEASE ?= "-DNDEBUG"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050037OECMAKE_C_LINK_FLAGS ?= "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS} ${CPPFLAGS} ${LDFLAGS}"
38OECMAKE_CXX_LINK_FLAGS ?= "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS} ${CXXFLAGS} ${LDFLAGS}"
Brad Bishop37a0e4d2017-12-04 01:01:44 -050039CXXFLAGS += "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS}"
40CFLAGS += "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050041
42OECMAKE_RPATH ?= ""
43OECMAKE_PERLNATIVE_DIR ??= ""
44OECMAKE_EXTRA_ROOT_PATH ?= ""
45
46OECMAKE_FIND_ROOT_PATH_MODE_PROGRAM = "ONLY"
47OECMAKE_FIND_ROOT_PATH_MODE_PROGRAM_class-native = "BOTH"
48
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050049EXTRA_OECMAKE_append = " ${PACKAGECONFIG_CONFARGS}"
50
Brad Bishopd7bf8c12018-02-25 22:55:05 -050051EXTRA_OECMAKE_BUILD_prepend_task-compile = "${PARALLEL_MAKE} "
52EXTRA_OECMAKE_BUILD_prepend_task-install = "${PARALLEL_MAKEINST} "
53
Brad Bishop316dfdd2018-06-25 12:45:53 -040054OECMAKE_TARGET_COMPILE ?= "all"
55OECMAKE_TARGET_INSTALL ?= "install"
56
Patrick Williamsc124f4f2015-09-15 14:41:29 -050057# CMake expects target architectures in the format of uname(2),
58# which do not always match TARGET_ARCH, so all the necessary
59# conversions should happen here.
60def map_target_arch_to_uname_arch(target_arch):
61 if target_arch == "powerpc":
62 return "ppc"
63 if target_arch == "powerpc64":
64 return "ppc64"
65 return target_arch
66
67cmake_do_generate_toolchain_file() {
Brad Bishop6e60e8b2018-02-01 10:27:11 -050068 if [ "${BUILD_SYS}" = "${HOST_SYS}" ]; then
69 cmake_crosscompiling="set( CMAKE_CROSSCOMPILING FALSE )"
70 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050071 cat > ${WORKDIR}/toolchain.cmake <<EOF
72# CMake system name must be something like "Linux".
73# This is important for cross-compiling.
Brad Bishop6e60e8b2018-02-01 10:27:11 -050074$cmake_crosscompiling
Patrick Williamsc124f4f2015-09-15 14:41:29 -050075set( CMAKE_SYSTEM_NAME `echo ${TARGET_OS} | sed -e 's/^./\u&/' -e 's/^\(Linux\).*/\1/'` )
Brad Bishop6e60e8b2018-02-01 10:27:11 -050076set( CMAKE_SYSTEM_PROCESSOR ${@map_target_arch_to_uname_arch(d.getVar('TARGET_ARCH'))} )
Patrick Williamsc124f4f2015-09-15 14:41:29 -050077set( CMAKE_C_COMPILER ${OECMAKE_C_COMPILER} )
78set( CMAKE_CXX_COMPILER ${OECMAKE_CXX_COMPILER} )
79set( CMAKE_ASM_COMPILER ${OECMAKE_C_COMPILER} )
80set( CMAKE_AR ${OECMAKE_AR} CACHE FILEPATH "Archiver" )
81set( CMAKE_C_FLAGS "${OECMAKE_C_FLAGS}" CACHE STRING "CFLAGS" )
82set( CMAKE_CXX_FLAGS "${OECMAKE_CXX_FLAGS}" CACHE STRING "CXXFLAGS" )
83set( CMAKE_ASM_FLAGS "${OECMAKE_C_FLAGS}" CACHE STRING "ASM FLAGS" )
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050084set( CMAKE_C_FLAGS_RELEASE "${OECMAKE_C_FLAGS_RELEASE}" CACHE STRING "Additional CFLAGS for release" )
85set( CMAKE_CXX_FLAGS_RELEASE "${OECMAKE_CXX_FLAGS_RELEASE}" CACHE STRING "Additional CXXFLAGS for release" )
86set( CMAKE_ASM_FLAGS_RELEASE "${OECMAKE_C_FLAGS_RELEASE}" CACHE STRING "Additional ASM FLAGS for release" )
Patrick Williamsc124f4f2015-09-15 14:41:29 -050087set( CMAKE_C_LINK_FLAGS "${OECMAKE_C_LINK_FLAGS}" CACHE STRING "LDFLAGS" )
88set( CMAKE_CXX_LINK_FLAGS "${OECMAKE_CXX_LINK_FLAGS}" CACHE STRING "LDFLAGS" )
89
90# only search in the paths provided so cmake doesnt pick
91# up libraries and tools from the native build machine
92set( CMAKE_FIND_ROOT_PATH ${STAGING_DIR_HOST} ${STAGING_DIR_NATIVE} ${CROSS_DIR} ${OECMAKE_PERLNATIVE_DIR} ${OECMAKE_EXTRA_ROOT_PATH} ${EXTERNAL_TOOLCHAIN})
93set( CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY )
94set( CMAKE_FIND_ROOT_PATH_MODE_PROGRAM ${OECMAKE_FIND_ROOT_PATH_MODE_PROGRAM} )
95set( CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY )
96set( CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY )
97
98# Use qt.conf settings
99set( ENV{QT_CONF_PATH} ${WORKDIR}/qt.conf )
100
101# We need to set the rpath to the correct directory as cmake does not provide any
102# directory as rpath by default
103set( CMAKE_INSTALL_RPATH ${OECMAKE_RPATH} )
104
105# Use native cmake modules
106list(APPEND CMAKE_MODULE_PATH "${STAGING_DATADIR}/cmake/Modules/")
107
108# add for non /usr/lib libdir, e.g. /usr/lib64
109set( CMAKE_LIBRARY_PATH ${libdir} ${base_libdir})
110
111EOF
112}
113
114addtask generate_toolchain_file after do_patch before do_configure
115
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600116CONFIGURE_FILES = "CMakeLists.txt"
117
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500118cmake_do_configure() {
119 if [ "${OECMAKE_BUILDPATH}" ]; then
120 bbnote "cmake.bbclass no longer uses OECMAKE_BUILDPATH. The default behaviour is now out-of-tree builds with B=WORKDIR/build."
121 fi
122
123 if [ "${S}" != "${B}" ]; then
124 rm -rf ${B}
125 mkdir -p ${B}
126 cd ${B}
127 else
128 find ${B} -name CMakeFiles -or -name Makefile -or -name cmake_install.cmake -or -name CMakeCache.txt -delete
129 fi
130
131 # Just like autotools cmake can use a site file to cache result that need generated binaries to run
132 if [ -e ${WORKDIR}/site-file.cmake ] ; then
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500133 oecmake_sitefile="-C ${WORKDIR}/site-file.cmake"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500134 else
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500135 oecmake_sitefile=
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500136 fi
137
138 cmake \
Brad Bishop316dfdd2018-06-25 12:45:53 -0400139 ${OECMAKE_GENERATOR_ARGS} \
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500140 $oecmake_sitefile \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500141 ${OECMAKE_SOURCEPATH} \
142 -DCMAKE_INSTALL_PREFIX:PATH=${prefix} \
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500143 -DCMAKE_INSTALL_BINDIR:PATH=${@os.path.relpath(d.getVar('bindir'), d.getVar('prefix'))} \
144 -DCMAKE_INSTALL_SBINDIR:PATH=${@os.path.relpath(d.getVar('sbindir'), d.getVar('prefix'))} \
145 -DCMAKE_INSTALL_LIBEXECDIR:PATH=${@os.path.relpath(d.getVar('libexecdir'), d.getVar('prefix'))} \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500146 -DCMAKE_INSTALL_SYSCONFDIR:PATH=${sysconfdir} \
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500147 -DCMAKE_INSTALL_SHAREDSTATEDIR:PATH=${@os.path.relpath(d.getVar('sharedstatedir'), d. getVar('prefix'))} \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500148 -DCMAKE_INSTALL_LOCALSTATEDIR:PATH=${localstatedir} \
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500149 -DCMAKE_INSTALL_LIBDIR:PATH=${@os.path.relpath(d.getVar('libdir'), d.getVar('prefix'))} \
150 -DCMAKE_INSTALL_INCLUDEDIR:PATH=${@os.path.relpath(d.getVar('includedir'), d.getVar('prefix'))} \
151 -DCMAKE_INSTALL_DATAROOTDIR:PATH=${@os.path.relpath(d.getVar('datadir'), d.getVar('prefix'))} \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500152 -DCMAKE_INSTALL_SO_NO_EXE=0 \
153 -DCMAKE_TOOLCHAIN_FILE=${WORKDIR}/toolchain.cmake \
154 -DCMAKE_VERBOSE_MAKEFILE=1 \
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600155 -DCMAKE_NO_SYSTEM_FROM_IMPORTED=1 \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500156 ${EXTRA_OECMAKE} \
157 -Wno-dev
158}
159
Brad Bishop316dfdd2018-06-25 12:45:53 -0400160cmake_runcmake_build() {
161 bbnote ${DESTDIR:+DESTDIR=${DESTDIR} }VERBOSE=1 cmake --build '${B}' "$@" -- ${EXTRA_OECMAKE_BUILD}
162 eval ${DESTDIR:+DESTDIR=${DESTDIR} }VERBOSE=1 cmake --build '${B}' "$@" -- ${EXTRA_OECMAKE_BUILD}
163}
164
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500165cmake_do_compile() {
Brad Bishop316dfdd2018-06-25 12:45:53 -0400166 cmake_runcmake_build --target ${OECMAKE_TARGET_COMPILE}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500167}
168
169cmake_do_install() {
Brad Bishop316dfdd2018-06-25 12:45:53 -0400170 DESTDIR='${D}' cmake_runcmake_build --target ${OECMAKE_TARGET_INSTALL}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500171}
172
173EXPORT_FUNCTIONS do_configure do_compile do_install do_generate_toolchain_file