Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 1 | # |
| 2 | # Copyright OpenEmbedded Contributors |
| 3 | # |
| 4 | # SPDX-License-Identifier: MIT |
| 5 | # |
| 6 | |
| 7 | # Path to the CMake file to process. |
| 8 | OECMAKE_SOURCEPATH ??= "${S}" |
| 9 | |
| 10 | DEPENDS:prepend = "cmake-native " |
| 11 | B = "${WORKDIR}/build" |
| 12 | |
| 13 | # What CMake generator to use. |
| 14 | # The supported options are "Unix Makefiles" or "Ninja". |
| 15 | OECMAKE_GENERATOR ?= "Ninja" |
| 16 | |
| 17 | python() { |
| 18 | generator = d.getVar("OECMAKE_GENERATOR") |
| 19 | if "Unix Makefiles" in generator: |
| 20 | args = "-G '" + generator + "' -DCMAKE_MAKE_PROGRAM=" + d.getVar("MAKE") |
| 21 | d.setVar("OECMAKE_GENERATOR_ARGS", args) |
| 22 | d.setVarFlag("do_compile", "progress", "percent") |
| 23 | elif "Ninja" in generator: |
| 24 | args = "-G '" + generator + "' -DCMAKE_MAKE_PROGRAM=ninja" |
| 25 | d.appendVar("DEPENDS", " ninja-native") |
| 26 | d.setVar("OECMAKE_GENERATOR_ARGS", args) |
| 27 | d.setVarFlag("do_compile", "progress", r"outof:^\[(\d+)/(\d+)\]\s+") |
| 28 | else: |
| 29 | bb.fatal("Unknown CMake Generator %s" % generator) |
| 30 | } |
| 31 | OECMAKE_AR ?= "${AR}" |
| 32 | |
| 33 | # Compiler flags |
| 34 | OECMAKE_C_FLAGS ?= "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS} ${CFLAGS}" |
| 35 | OECMAKE_CXX_FLAGS ?= "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS} ${CXXFLAGS}" |
| 36 | OECMAKE_C_FLAGS_RELEASE ?= "-DNDEBUG" |
| 37 | OECMAKE_CXX_FLAGS_RELEASE ?= "-DNDEBUG" |
| 38 | OECMAKE_C_LINK_FLAGS ?= "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS} ${CPPFLAGS} ${LDFLAGS}" |
| 39 | OECMAKE_CXX_LINK_FLAGS ?= "${HOST_CC_ARCH} ${TOOLCHAIN_OPTIONS} ${CXXFLAGS} ${LDFLAGS}" |
| 40 | |
| 41 | def oecmake_map_compiler(compiler, d): |
| 42 | args = d.getVar(compiler).split() |
| 43 | if args[0] == "ccache": |
| 44 | return args[1], args[0] |
| 45 | return args[0], "" |
| 46 | |
| 47 | # C/C++ Compiler (without cpu arch/tune arguments) |
| 48 | OECMAKE_C_COMPILER ?= "${@oecmake_map_compiler('CC', d)[0]}" |
| 49 | OECMAKE_C_COMPILER_LAUNCHER ?= "${@oecmake_map_compiler('CC', d)[1]}" |
| 50 | OECMAKE_CXX_COMPILER ?= "${@oecmake_map_compiler('CXX', d)[0]}" |
| 51 | OECMAKE_CXX_COMPILER_LAUNCHER ?= "${@oecmake_map_compiler('CXX', d)[1]}" |
| 52 | |
| 53 | # clear compiler vars for allarch to avoid sig hash difference |
Andrew Geissler | 5082cc7 | 2023-09-11 08:41:39 -0400 | [diff] [blame] | 54 | OECMAKE_C_COMPILER:allarch = "" |
| 55 | OECMAKE_C_COMPILER_LAUNCHER:allarch = "" |
| 56 | OECMAKE_CXX_COMPILER:allarch = "" |
| 57 | OECMAKE_CXX_COMPILER_LAUNCHER:allarch = "" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 58 | |
| 59 | OECMAKE_RPATH ?= "" |
| 60 | OECMAKE_PERLNATIVE_DIR ??= "" |
| 61 | OECMAKE_EXTRA_ROOT_PATH ?= "" |
| 62 | |
| 63 | OECMAKE_FIND_ROOT_PATH_MODE_PROGRAM = "ONLY" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 64 | |
| 65 | EXTRA_OECMAKE:append = " ${PACKAGECONFIG_CONFARGS}" |
| 66 | |
| 67 | export CMAKE_BUILD_PARALLEL_LEVEL |
| 68 | CMAKE_BUILD_PARALLEL_LEVEL:task-compile = "${@oe.utils.parallel_make(d, False)}" |
| 69 | CMAKE_BUILD_PARALLEL_LEVEL:task-install = "${@oe.utils.parallel_make(d, True)}" |
| 70 | |
| 71 | OECMAKE_TARGET_COMPILE ?= "all" |
| 72 | OECMAKE_TARGET_INSTALL ?= "install" |
| 73 | |
| 74 | def map_host_os_to_system_name(host_os): |
Patrick Williams | f52e3dd | 2024-01-26 13:04:43 -0600 | [diff] [blame] | 75 | if host_os.startswith('darwin'): |
| 76 | return 'Darwin' |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 77 | if host_os.startswith('mingw'): |
| 78 | return 'Windows' |
| 79 | if host_os.startswith('linux'): |
| 80 | return 'Linux' |
| 81 | return host_os |
| 82 | |
| 83 | # CMake expects target architectures in the format of uname(2), |
| 84 | # which do not always match TARGET_ARCH, so all the necessary |
| 85 | # conversions should happen here. |
| 86 | def map_host_arch_to_uname_arch(host_arch): |
| 87 | if host_arch == "powerpc": |
| 88 | return "ppc" |
| 89 | if host_arch == "powerpc64le": |
| 90 | return "ppc64le" |
| 91 | if host_arch == "powerpc64": |
| 92 | return "ppc64" |
| 93 | return host_arch |
| 94 | |
Andrew Geissler | 5082cc7 | 2023-09-11 08:41:39 -0400 | [diff] [blame] | 95 | |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 96 | cmake_do_generate_toolchain_file() { |
| 97 | if [ "${BUILD_SYS}" = "${HOST_SYS}" ]; then |
| 98 | cmake_crosscompiling="set( CMAKE_CROSSCOMPILING FALSE )" |
Andrew Geissler | 5082cc7 | 2023-09-11 08:41:39 -0400 | [diff] [blame] | 99 | else |
| 100 | cmake_sysroot="set( CMAKE_SYSROOT \"${RECIPE_SYSROOT}\" )" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 101 | fi |
Andrew Geissler | 5082cc7 | 2023-09-11 08:41:39 -0400 | [diff] [blame] | 102 | |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 103 | cat > ${WORKDIR}/toolchain.cmake <<EOF |
| 104 | # CMake system name must be something like "Linux". |
| 105 | # This is important for cross-compiling. |
| 106 | $cmake_crosscompiling |
| 107 | set( CMAKE_SYSTEM_NAME ${@map_host_os_to_system_name(d.getVar('HOST_OS'))} ) |
| 108 | set( CMAKE_SYSTEM_PROCESSOR ${@map_host_arch_to_uname_arch(d.getVar('HOST_ARCH'))} ) |
| 109 | set( CMAKE_C_COMPILER ${OECMAKE_C_COMPILER} ) |
| 110 | set( CMAKE_CXX_COMPILER ${OECMAKE_CXX_COMPILER} ) |
| 111 | set( CMAKE_C_COMPILER_LAUNCHER ${OECMAKE_C_COMPILER_LAUNCHER} ) |
| 112 | set( CMAKE_CXX_COMPILER_LAUNCHER ${OECMAKE_CXX_COMPILER_LAUNCHER} ) |
| 113 | set( CMAKE_ASM_COMPILER ${OECMAKE_C_COMPILER} ) |
| 114 | find_program( CMAKE_AR ${OECMAKE_AR} DOC "Archiver" REQUIRED ) |
| 115 | |
| 116 | set( CMAKE_C_FLAGS "${OECMAKE_C_FLAGS}" CACHE STRING "CFLAGS" ) |
| 117 | set( CMAKE_CXX_FLAGS "${OECMAKE_CXX_FLAGS}" CACHE STRING "CXXFLAGS" ) |
| 118 | set( CMAKE_ASM_FLAGS "${OECMAKE_C_FLAGS}" CACHE STRING "ASM FLAGS" ) |
| 119 | set( CMAKE_C_FLAGS_RELEASE "${OECMAKE_C_FLAGS_RELEASE}" CACHE STRING "Additional CFLAGS for release" ) |
| 120 | set( CMAKE_CXX_FLAGS_RELEASE "${OECMAKE_CXX_FLAGS_RELEASE}" CACHE STRING "Additional CXXFLAGS for release" ) |
| 121 | set( CMAKE_ASM_FLAGS_RELEASE "${OECMAKE_C_FLAGS_RELEASE}" CACHE STRING "Additional ASM FLAGS for release" ) |
| 122 | set( CMAKE_C_LINK_FLAGS "${OECMAKE_C_LINK_FLAGS}" CACHE STRING "LDFLAGS" ) |
| 123 | set( CMAKE_CXX_LINK_FLAGS "${OECMAKE_CXX_LINK_FLAGS}" CACHE STRING "LDFLAGS" ) |
| 124 | |
| 125 | # only search in the paths provided so cmake doesnt pick |
| 126 | # up libraries and tools from the native build machine |
| 127 | set( CMAKE_FIND_ROOT_PATH ${STAGING_DIR_HOST} ${STAGING_DIR_NATIVE} ${CROSS_DIR} ${OECMAKE_PERLNATIVE_DIR} ${OECMAKE_EXTRA_ROOT_PATH} ${EXTERNAL_TOOLCHAIN} ${HOSTTOOLS_DIR}) |
| 128 | set( CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY ) |
| 129 | set( CMAKE_FIND_ROOT_PATH_MODE_PROGRAM ${OECMAKE_FIND_ROOT_PATH_MODE_PROGRAM} ) |
| 130 | set( CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY ) |
| 131 | set( CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY ) |
| 132 | set( CMAKE_PROGRAM_PATH "/" ) |
| 133 | |
Patrick Williams | 8e7b46e | 2023-05-01 14:19:06 -0500 | [diff] [blame] | 134 | $cmake_sysroot |
| 135 | |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 136 | # Use qt.conf settings |
| 137 | set( ENV{QT_CONF_PATH} ${WORKDIR}/qt.conf ) |
| 138 | |
| 139 | # We need to set the rpath to the correct directory as cmake does not provide any |
| 140 | # directory as rpath by default |
| 141 | set( CMAKE_INSTALL_RPATH ${OECMAKE_RPATH} ) |
| 142 | |
| 143 | # Use RPATHs relative to build directory for reproducibility |
| 144 | set( CMAKE_BUILD_RPATH_USE_ORIGIN ON ) |
| 145 | |
| 146 | # Use our cmake modules |
| 147 | list(APPEND CMAKE_MODULE_PATH "${STAGING_DATADIR}/cmake/Modules/") |
| 148 | |
| 149 | # add for non /usr/lib libdir, e.g. /usr/lib64 |
| 150 | set( CMAKE_LIBRARY_PATH ${libdir} ${base_libdir}) |
| 151 | |
| 152 | # add include dir to implicit includes in case it differs from /usr/include |
| 153 | list(APPEND CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES ${includedir}) |
| 154 | list(APPEND CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES ${includedir}) |
| 155 | |
| 156 | EOF |
| 157 | } |
| 158 | |
| 159 | addtask generate_toolchain_file after do_patch before do_configure |
| 160 | |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 161 | CONFIGURE_FILES = "CMakeLists.txt *.cmake" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 162 | |
| 163 | do_configure[cleandirs] = "${@d.getVar('B') if d.getVar('S') != d.getVar('B') else ''}" |
| 164 | |
Andrew Geissler | 5082cc7 | 2023-09-11 08:41:39 -0400 | [diff] [blame] | 165 | OECMAKE_ARGS = "\ |
| 166 | -DCMAKE_INSTALL_PREFIX:PATH=${prefix} \ |
| 167 | -DCMAKE_INSTALL_BINDIR:PATH=${@os.path.relpath(d.getVar('bindir'), d.getVar('prefix') + '/')} \ |
| 168 | -DCMAKE_INSTALL_SBINDIR:PATH=${@os.path.relpath(d.getVar('sbindir'), d.getVar('prefix') + '/')} \ |
| 169 | -DCMAKE_INSTALL_LIBEXECDIR:PATH=${@os.path.relpath(d.getVar('libexecdir'), d.getVar('prefix') + '/')} \ |
| 170 | -DCMAKE_INSTALL_SYSCONFDIR:PATH=${sysconfdir} \ |
| 171 | -DCMAKE_INSTALL_SHAREDSTATEDIR:PATH=${@os.path.relpath(d.getVar('sharedstatedir'), d. getVar('prefix') + '/')} \ |
| 172 | -DCMAKE_INSTALL_LOCALSTATEDIR:PATH=${localstatedir} \ |
| 173 | -DCMAKE_INSTALL_LIBDIR:PATH=${@os.path.relpath(d.getVar('libdir'), d.getVar('prefix') + '/')} \ |
| 174 | -DCMAKE_INSTALL_INCLUDEDIR:PATH=${@os.path.relpath(d.getVar('includedir'), d.getVar('prefix') + '/')} \ |
| 175 | -DCMAKE_INSTALL_DATAROOTDIR:PATH=${@os.path.relpath(d.getVar('datadir'), d.getVar('prefix') + '/')} \ |
| 176 | -DPYTHON_EXECUTABLE:PATH=${PYTHON} \ |
| 177 | -DPython_EXECUTABLE:PATH=${PYTHON} \ |
| 178 | -DPython3_EXECUTABLE:PATH=${PYTHON} \ |
| 179 | -DLIB_SUFFIX=${@d.getVar('baselib').replace('lib', '')} \ |
| 180 | -DCMAKE_INSTALL_SO_NO_EXE=0 \ |
| 181 | -DCMAKE_TOOLCHAIN_FILE:FILEPATH=${WORKDIR}/toolchain.cmake \ |
| 182 | -DCMAKE_NO_SYSTEM_FROM_IMPORTED=1 \ |
| 183 | -DCMAKE_EXPORT_NO_PACKAGE_REGISTRY=ON \ |
| 184 | -DFETCHCONTENT_FULLY_DISCONNECTED=ON \ |
| 185 | -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=ON \ |
| 186 | " |
| 187 | |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 188 | cmake_do_configure() { |
| 189 | if [ "${OECMAKE_BUILDPATH}" ]; then |
| 190 | bbnote "cmake.bbclass no longer uses OECMAKE_BUILDPATH. The default behaviour is now out-of-tree builds with B=WORKDIR/build." |
| 191 | fi |
| 192 | |
| 193 | if [ "${S}" = "${B}" ]; then |
| 194 | find ${B} -name CMakeFiles -or -name Makefile -or -name cmake_install.cmake -or -name CMakeCache.txt -delete |
| 195 | fi |
| 196 | |
| 197 | # Just like autotools cmake can use a site file to cache result that need generated binaries to run |
| 198 | if [ -e ${WORKDIR}/site-file.cmake ] ; then |
| 199 | oecmake_sitefile="-C ${WORKDIR}/site-file.cmake" |
| 200 | else |
| 201 | oecmake_sitefile= |
| 202 | fi |
| 203 | |
| 204 | cmake \ |
| 205 | ${OECMAKE_GENERATOR_ARGS} \ |
| 206 | $oecmake_sitefile \ |
| 207 | ${OECMAKE_SOURCEPATH} \ |
Andrew Geissler | 5082cc7 | 2023-09-11 08:41:39 -0400 | [diff] [blame] | 208 | ${OECMAKE_ARGS} \ |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 209 | ${EXTRA_OECMAKE} \ |
| 210 | -Wno-dev |
| 211 | } |
| 212 | |
| 213 | # To disable verbose cmake logs for a given recipe or globally config metadata e.g. local.conf |
| 214 | # add following |
| 215 | # |
| 216 | # CMAKE_VERBOSE = "" |
| 217 | # |
| 218 | |
| 219 | CMAKE_VERBOSE ??= "VERBOSE=1" |
| 220 | |
| 221 | # Then run do_compile again |
| 222 | cmake_runcmake_build() { |
| 223 | bbnote ${DESTDIR:+DESTDIR=${DESTDIR} }${CMAKE_VERBOSE} cmake --build '${B}' "$@" -- ${EXTRA_OECMAKE_BUILD} |
| 224 | eval ${DESTDIR:+DESTDIR=${DESTDIR} }${CMAKE_VERBOSE} cmake --build '${B}' "$@" -- ${EXTRA_OECMAKE_BUILD} |
| 225 | } |
| 226 | |
Patrick Williams | 73bd93f | 2024-02-20 08:07:48 -0600 | [diff] [blame] | 227 | # Install an already-generated project binary tree. Not checking the compile |
| 228 | # dependencies again is particularly important for SDK use cases. |
| 229 | cmake_runcmake_install() { |
| 230 | bbnote ${DESTDIR:+DESTDIR=${DESTDIR} }${CMAKE_VERBOSE} cmake --install '${B}' |
| 231 | eval ${DESTDIR:+DESTDIR=${DESTDIR} }${CMAKE_VERBOSE} cmake --install '${B}' |
| 232 | } |
| 233 | |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 234 | cmake_do_compile() { |
| 235 | cmake_runcmake_build --target ${OECMAKE_TARGET_COMPILE} |
| 236 | } |
| 237 | |
| 238 | cmake_do_install() { |
Patrick Williams | 73bd93f | 2024-02-20 08:07:48 -0600 | [diff] [blame] | 239 | if [ "${OECMAKE_TARGET_INSTALL}" = "install" ]; then |
| 240 | DESTDIR='${D}' cmake_runcmake_install |
| 241 | else |
| 242 | # Legacy path which supports also custom install targets |
| 243 | DESTDIR='${D}' cmake_runcmake_build --target ${OECMAKE_TARGET_INSTALL} |
| 244 | fi |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | EXPORT_FUNCTIONS do_configure do_compile do_install do_generate_toolchain_file |