|
| 1 | +# Copyright (C) 2008 by BogDan Vatra < bogdan@licentia.eu > |
| 2 | +# Copyright (C) 2009-2025 Robin Stuart <rstuart114@gmail.com> |
| 3 | +# vim: set ts=4 sw=4 et : |
| 4 | + |
| 5 | +cmake_minimum_required(VERSION 3.10) |
| 6 | +project(zint-package) |
| 7 | + |
| 8 | +set(CMAKE_INCLUDE_CURRENT_DIR ON) |
| 9 | + |
| 10 | +set(ZINT_VERSION_MAJOR 2) |
| 11 | +set(ZINT_VERSION_MINOR 15) |
| 12 | +set(ZINT_VERSION_RELEASE 0) |
| 13 | +set(ZINT_VERSION_BUILD 0) # Set to 0 before release, set to 9 after release |
| 14 | +set(ZINT_VERSION "${ZINT_VERSION_MAJOR}.${ZINT_VERSION_MINOR}.${ZINT_VERSION_RELEASE}.${ZINT_VERSION_BUILD}") |
| 15 | + |
| 16 | +add_definitions(-DZINT_VERSION=\"${ZINT_VERSION}\") |
| 17 | +
|
| 18 | +option(ZINT_DEBUG "Set debug compile flags" OFF) |
| 19 | +option(ZINT_NOOPT "Set no optimize compile flags" OFF) |
| 20 | +option(ZINT_SANITIZE "Set sanitize address/undefined" OFF) |
| 21 | +option(ZINT_SANITIZEM "Set sanitize memory (ignored if ZINT_SANITIZE)" OFF) |
| 22 | +option(ZINT_TEST "Set test compile flag" OFF) |
| 23 | +option(ZINT_COVERAGE "Set code coverage flags" OFF) |
| 24 | +option(ZINT_SHARED "Build shared library" ON) |
| 25 | +option(ZINT_STATIC "Build static library" OFF) |
| 26 | +option(ZINT_USE_PNG "Build with PNG support" OFF) |
| 27 | +option(ZINT_USE_QT "Build with Qt support" OFF) |
| 28 | +option(ZINT_QT6 "If ZINT_USE_QT, use Qt6" OFF) |
| 29 | +option(ZINT_UNINSTALL "Add uninstall target" OFF) |
| 30 | +option(ZINT_FRONTEND "Build frontend" OFF) |
| 31 | +
|
| 32 | +if(NOT ZINT_SHARED AND NOT ZINT_STATIC) |
| 33 | + message(SEND_ERROR "Either ZINT_SHARED or ZINT_STATIC or both must be set") |
| 34 | +endif() |
| 35 | +
|
| 36 | +include(GNUInstallDirs) |
| 37 | +# Taken from old (2008) KDE "SetPaths.cmake" |
| 38 | +# Set a default build type for single-configuration CMake generators if no build type is set |
| 39 | +if(NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE) |
| 40 | + set(CMAKE_BUILD_TYPE RelWithDebInfo) |
| 41 | +endif() |
| 42 | +
|
| 43 | +include(CheckCCompilerFlag) |
| 44 | +include(CheckFunctionExists) |
| 45 | +
|
| 46 | +if(NOT MSVC) # Use default warnings if MSVC otherwise inundated |
| 47 | + check_c_compiler_flag("-Wall" C_COMPILER_FLAG_WALL) |
| 48 | + if(C_COMPILER_FLAG_WALL) |
| 49 | + add_compile_options("-Wall") |
| 50 | + endif() |
| 51 | +
|
| 52 | + check_c_compiler_flag("-Wextra" C_COMPILER_FLAG_WEXTRA) |
| 53 | + if(C_COMPILER_FLAG_WEXTRA) |
| 54 | + add_compile_options("-Wextra") |
| 55 | + endif() |
| 56 | +
|
| 57 | + check_c_compiler_flag("-Wpedantic" C_COMPILER_FLAG_WPEDANTIC) |
| 58 | + if(C_COMPILER_FLAG_WPEDANTIC) |
| 59 | + add_compile_options("-Wpedantic") |
| 60 | + endif() |
| 61 | +
|
| 62 | + check_c_compiler_flag("-Wcast-align" C_COMPILER_FLAG_WCASTALIGN) |
| 63 | + if(C_COMPILER_FLAG_WCASTALIGN) |
| 64 | + add_compile_options("-Wcast-align") |
| 65 | + endif() |
| 66 | + check_c_compiler_flag("-Wpointer-arith" C_COMPILER_FLAG_WPOINTERARITH) |
| 67 | + if(C_COMPILER_FLAG_WPOINTERARITH) |
| 68 | + add_compile_options("-Wpointer-arith") |
| 69 | + endif() |
| 70 | + check_c_compiler_flag("-Wshadow" C_COMPILER_FLAG_WSHADOW) |
| 71 | + if(C_COMPILER_FLAG_WSHADOW) |
| 72 | + add_compile_options("-Wshadow") |
| 73 | + endif() |
| 74 | + check_c_compiler_flag("-Wundef" C_COMPILER_FLAG_WUNDEF) |
| 75 | + if(C_COMPILER_FLAG_WUNDEF) |
| 76 | + add_compile_options("-Wundef") |
| 77 | + endif() |
| 78 | + check_c_compiler_flag("-Wwrite-strings" C_COMPILER_FLAG_WWRITESTRINGS) |
| 79 | + if(C_COMPILER_FLAG_WWRITESTRINGS) |
| 80 | + add_compile_options("-Wwrite-strings") |
| 81 | + endif() |
| 82 | +endif() |
| 83 | +
|
| 84 | +if(ZINT_DEBUG) |
| 85 | + check_c_compiler_flag("-g" C_COMPILER_FLAG_G) |
| 86 | + if(C_COMPILER_FLAG_G) |
| 87 | + add_compile_options("-g") |
| 88 | + endif() |
| 89 | +endif() |
| 90 | +
|
| 91 | +if(ZINT_NOOPT) |
| 92 | + check_c_compiler_flag("-O0" C_COMPILER_FLAG_O0) |
| 93 | + if(C_COMPILER_FLAG_O0) |
| 94 | + add_compile_options("-O0") |
| 95 | + endif() |
| 96 | +endif() |
| 97 | +
|
| 98 | +if(ZINT_SANITIZE) |
| 99 | + if(MSVC) |
| 100 | + if(MSVC_VERSION GREATER_EQUAL 1920) |
| 101 | + add_compile_options(-fsanitize=address) |
| 102 | + message(STATUS "ZINT_SANITIZE: setting -fsanitize=address for MSVC 2019") |
| 103 | + else() |
| 104 | + message(STATUS "ZINT_SANITIZE: ignoring for MSVC < 2019") |
| 105 | + endif() |
| 106 | + else() |
| 107 | + set(SANITIZERS address undefined leak) |
| 108 | + foreach(sanitizer IN ITEMS ${SANITIZERS}) |
| 109 | + set(CMAKE_REQUIRED_LIBRARIES -fsanitize=${sanitizer}) |
| 110 | + check_c_compiler_flag(-fsanitize=${sanitizer} C_COMPILER_FLAG_FSANITIZE_${sanitizer}) |
| 111 | + if(C_COMPILER_FLAG_FSANITIZE_${sanitizer}) |
| 112 | + add_compile_options(-fsanitize=${sanitizer}) |
| 113 | + link_libraries(-fsanitize=${sanitizer}) |
| 114 | + endif() |
| 115 | + unset(CMAKE_REQUIRED_LIBRARIES) |
| 116 | + endforeach() |
| 117 | +
|
| 118 | + if(CMAKE_C_COMPILER_ID MATCHES "Clang") |
| 119 | + # Recent clangs added deprecation warnings for `sprintf()` that are only triggered on sanitize - suppress |
| 120 | + add_compile_options(-Wno-deprecated-declarations) |
| 121 | + endif() |
| 122 | + endif() |
| 123 | +endif() |
| 124 | +
|
| 125 | +if(ZINT_TEST) |
| 126 | + enable_testing() |
| 127 | +endif() |
| 128 | +
|
| 129 | +if(ZINT_COVERAGE) |
| 130 | + set(CMAKE_REQUIRED_LIBRARIES -fprofile-arcs) |
| 131 | + check_c_compiler_flag(--coverage C_COMPILER_FLAG_COVERAGE) |
| 132 | + unset(CMAKE_REQUIRED_LIBRARIES) |
| 133 | + if(C_COMPILER_FLAG_COVERAGE) |
| 134 | + add_compile_options(--coverage) |
| 135 | + link_libraries(-fprofile-arcs) |
| 136 | +
|
| 137 | + check_c_compiler_flag(-O0 C_COMPILER_FLAG_O0) |
| 138 | + if(C_COMPILER_FLAG_O0) |
| 139 | + add_compile_options(-O0) |
| 140 | + endif() |
| 141 | + endif() |
| 142 | +endif() |
| 143 | +
|
| 144 | +check_function_exists(getopt_long_only HAVE_GETOPT_LONG_ONLY) |
| 145 | +if(NOT HAVE_GETOPT_LONG_ONLY) |
| 146 | + add_subdirectory(getopt) |
| 147 | +endif() |
| 148 | +
|
| 149 | +add_subdirectory(backend) |
| 150 | +if(ZINT_FRONTEND) |
| 151 | + add_subdirectory(frontend) |
| 152 | +endif() |
| 153 | +
|
| 154 | +if(NOT ZINT_USE_QT) |
| 155 | + message(STATUS "Qt support was disabled for this build") |
| 156 | +elseif(ZINT_QT6 OR ($ENV{CMAKE_PREFIX_PATH} MATCHES "6[.][0-9][.][0-9]")) |
| 157 | + set(USE_QT6 TRUE) |
| 158 | + message(STATUS "Using Qt6") |
| 159 | + cmake_policy(SET CMP0012 NEW) # Recognize constants in if() |
| 160 | + cmake_policy(SET CMP0072 NEW) # Choose OpenGL over legacy GL |
| 161 | + find_package(Qt6Widgets) |
| 162 | + find_package(Qt6Gui) |
| 163 | + find_package(Qt6UiTools) |
| 164 | + find_package(Qt6Svg) |
| 165 | +
|
| 166 | + if(Qt6Widgets_FOUND AND Qt6Gui_FOUND AND Qt6UiTools_FOUND AND Qt6Svg_FOUND) |
| 167 | + message(STATUS "Qt version: " ${Qt6Core_VERSION_MAJOR}.${Qt6Core_VERSION_MINOR}.${Qt6Core_VERSION_PATCH}) |
| 168 | + add_subdirectory(backend_qt) |
| 169 | + add_subdirectory(frontend_qt) |
| 170 | + else() |
| 171 | + message(STATUS "Could NOT find Qt6") |
| 172 | + endif() |
| 173 | +else() |
| 174 | + message(STATUS "Using Qt5") |
| 175 | + find_package(Qt5Widgets) |
| 176 | + find_package(Qt5Gui) |
| 177 | + find_package(Qt5UiTools) |
| 178 | + find_package(Qt5Svg) |
| 179 | +
|
| 180 | + if(Qt5Widgets_FOUND AND Qt5Gui_FOUND AND Qt5UiTools_FOUND AND Qt5Svg_FOUND) |
| 181 | + message(STATUS "Qt version: " ${Qt5Core_VERSION_STRING}) |
| 182 | + # Old Qt does not provide QT_VERSION_MAJOR |
| 183 | + if (NOT QT_VERSION_MAJOR) |
| 184 | + string(SUBSTRING ${Qt5Core_VERSION_STRING} 0 1 QT_VERSION_MAJOR) |
| 185 | + endif() |
| 186 | + add_subdirectory(backend_qt) |
| 187 | + add_subdirectory(frontend_qt) |
| 188 | + else() |
| 189 | + message(STATUS "Could NOT find Qt5") |
| 190 | + endif() |
| 191 | +endif() |
| 192 | +
|
| 193 | +if(ZINT_UNINSTALL) |
| 194 | + configure_file( |
| 195 | + "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in" |
| 196 | + "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake" |
| 197 | + IMMEDIATE @ONLY) |
| 198 | + |
| 199 | + add_custom_target(uninstall |
| 200 | + "${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake") |
| 201 | +endif() |
| 202 | +
|
| 203 | +configure_file("zint-config.cmake.in" "zint-config.cmake" @ONLY) |
| 204 | +install(FILES "${CMAKE_CURRENT_BINARY_DIR}/zint-config.cmake" DESTINATION "${CMAKE_INSTALL_DATADIR}/zint") |
0 commit comments