|
| 1 | +# This is a rewrite over time of the CMake file from the libe57 reference implementation |
| 2 | +# https://en.wikipedia.org/wiki/Ship_of_Theseus |
| 3 | +# |
| 4 | +# Use git blame to see all the changes and who has contributed. |
| 5 | +# |
| 6 | +# Copyright 2018-2023 Andy Maloney <[email protected]> |
| 7 | +# Original work Copyright 2010-2012 Roland Schwarz, Riegl LMS GmbH |
| 8 | +# |
| 9 | +# Permission is hereby granted, free of charge, to any person or organization |
| 10 | +# obtaining a copy of the software and accompanying documentation covered by |
| 11 | +# this license (the "Software") to use, reproduce, display, distribute, |
| 12 | +# execute, and transmit the Software, and to prepare derivative works of the |
| 13 | +# Software, and to permit third-parties to whom the Software is furnished to |
| 14 | +# do so, all subject to the following: |
| 15 | +# |
| 16 | +# The copyright notices in the Software and this entire statement, including |
| 17 | +# the above license grant, this restriction and the following disclaimer, |
| 18 | +# must be included in all copies of the Software, in whole or in part, and |
| 19 | +# all derivative works of the Software, unless such copies or derivative |
| 20 | +# works are solely in the form of machine-executable object code generated by |
| 21 | +# a source language processor. |
| 22 | +# |
| 23 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 24 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 25 | +# FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
| 26 | +# SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
| 27 | +# FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
| 28 | +# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 29 | +# DEALINGS IN THE SOFTWARE. |
| 30 | + |
| 31 | +cmake_minimum_required( VERSION 3.15 ) |
| 32 | + |
| 33 | +# Set a private module find path |
| 34 | +set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/" ) |
| 35 | + |
| 36 | +message( STATUS "Using CMake ${CMAKE_VERSION}" ) |
| 37 | + |
| 38 | +project( E57Format |
| 39 | + DESCRIPTION |
| 40 | + "E57Format is a library to read and write E57 files" |
| 41 | + LANGUAGES |
| 42 | + CXX |
| 43 | + VERSION |
| 44 | + 3.2.0 |
| 45 | +) |
| 46 | + |
| 47 | +string( TOUPPER ${PROJECT_NAME} PROJECT_NAME_UPPERCASE ) |
| 48 | + |
| 49 | +# Creates a build tag which is used in the REVISION_ID |
| 50 | +# e.g. "x86_64-darwin-AppleClang140" |
| 51 | +include( Tags ) |
| 52 | +include( GitInfo ) |
| 53 | + |
| 54 | +# Collect all our current git info using: |
| 55 | +# https://github.com/cppmf/GitInfo.cmake |
| 56 | +GitInfo( ${CMAKE_CURRENT_SOURCE_DIR} ) |
| 57 | +# GitInfoReport() |
| 58 | + |
| 59 | +# Check if we are building ourself or being included and use this to set some defaults |
| 60 | +if ( ${PROJECT_NAME} STREQUAL ${CMAKE_PROJECT_NAME} ) |
| 61 | + set( E57_BUILDING_SELF ON ) |
| 62 | +endif() |
| 63 | + |
| 64 | +# propose a default installation directory |
| 65 | +if ( E57_BUILDING_SELF ) |
| 66 | + if( CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT ) |
| 67 | + string( REGEX REPLACE "/${PROJECT_NAME}" "" CMAKE_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX} ) |
| 68 | + set( T_ ${PROJECT_NAME} ) |
| 69 | + set( T_ ${T_}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} ) |
| 70 | + set( T_ ${T_}-${${PROJECT_NAME}_BUILD_TAG} ) |
| 71 | + set( CMAKE_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX}/${T_} |
| 72 | + CACHE PATH |
| 73 | + "Install path prefix, prepended onto install directories." |
| 74 | + FORCE |
| 75 | + ) |
| 76 | + endif() |
| 77 | +endif() |
| 78 | + |
| 79 | +find_package( Threads REQUIRED ) |
| 80 | +find_package( XercesC 3.2 REQUIRED ) |
| 81 | + |
| 82 | +option( E57_BUILD_SHARED |
| 83 | + "Compile E57Format as a shared library" |
| 84 | + OFF |
| 85 | +) |
| 86 | + |
| 87 | +if( BUILD_SHARED_LIBS ) |
| 88 | + set( E57_BUILD_SHARED ON ) |
| 89 | +endif() |
| 90 | + |
| 91 | +######################################################################################### |
| 92 | +# Cross checking and runtime verification in the code. |
| 93 | +# The extra code does not change the file contents. |
| 94 | +# E57_VALIDATION_LEVEL=0 off |
| 95 | +# E57_VALIDATION_LEVEL=1 basic |
| 96 | +# E57_VALIDATION_LEVEL=2 deep (implies basic) - checks at the packet level, so it will be slower |
| 97 | +set( E57_VALIDATION_LEVEL "1" CACHE STRING "Runtime validation level (0 = OFF, 1 = basic, 2 = deep)" ) |
| 98 | + |
| 99 | +# Output detailed logging while processing. |
| 100 | +option( E57_VERBOSE "Compile library with verbose logging" OFF ) |
| 101 | + |
| 102 | +# Enable/disable code which dumps detailed node info to std::ostream. (See NodeImpl::dump()) |
| 103 | +# Instead of always including this code, it is an option for backwards compatibility. |
| 104 | +# The only real reason to turn this off would be for slightly smaller binaries. |
| 105 | +option( E57_ENABLE_DIAGNOSTIC_OUTPUT "Include code for diagnostic output using dump() on nodes" ON ) |
| 106 | + |
| 107 | +# Enable writing packets that are correct but will stress the reader. |
| 108 | +option( E57_WRITE_CRAZY_PACKET_MODE "Compile library to enable reader-stressing packets" OFF ) |
| 109 | + |
| 110 | +# Other compile options |
| 111 | + |
| 112 | +# Link-time optiomization |
| 113 | +# CMake forces "thin" LTO (see https://gitlab.kitware.com/cmake/cmake/-/issues/23136) |
| 114 | +# which is a problem if compiling statically for distribution (e.g. in a package manager). |
| 115 | +# Generally you will only want to turn this off for distributing static release builds. |
| 116 | +option( E57_RELEASE_LTO "Compile release library with link-time optimization" ON ) |
| 117 | + |
| 118 | +######################################################################################### |
| 119 | + |
| 120 | +set( REVISION_ID "${PROJECT_NAME}-${PROJECT_VERSION}-${${PROJECT_NAME}_BUILD_TAG}" ) |
| 121 | +if ( GIT_LATEST_TAG ) |
| 122 | + set( REVISION_ID "${REVISION_ID} (git ${GIT_LATEST_TAG})" ) |
| 123 | +endif() |
| 124 | +message( STATUS "[${PROJECT_NAME}] Revision ID: ${REVISION_ID}" ) |
| 125 | + |
| 126 | +# Target |
| 127 | +if ( E57_BUILD_SHARED ) |
| 128 | + message( STATUS "[${PROJECT_NAME}] Building shared library" ) |
| 129 | + add_library( E57Format SHARED ) |
| 130 | +else() |
| 131 | + message( STATUS "[${PROJECT_NAME}] Building static library" ) |
| 132 | + add_library( E57Format STATIC ) |
| 133 | +endif() |
| 134 | + |
| 135 | +include( E57ExportHeader ) |
| 136 | +include( GitUpdate ) |
| 137 | + |
| 138 | +# Main sources and includes |
| 139 | +add_subdirectory( extern/CRCpp ) |
| 140 | +add_subdirectory( include ) |
| 141 | +add_subdirectory( src ) |
| 142 | + |
| 143 | +# Target properties |
| 144 | +set_target_properties( E57Format |
| 145 | + PROPERTIES |
| 146 | + CXX_EXTENSIONS NO |
| 147 | + EXPORT_COMPILE_COMMANDS ON |
| 148 | + POSITION_INDEPENDENT_CODE ON |
| 149 | + INTERPROCEDURAL_OPTIMIZATION ${E57_RELEASE_LTO} |
| 150 | + INTERPROCEDURAL_OPTIMIZATION_DEBUG OFF |
| 151 | +) |
| 152 | + |
| 153 | +if ( NOT DEFINED CMAKE_DEBUG_POSTFIX ) |
| 154 | + set_target_properties( E57Format |
| 155 | + PROPERTIES |
| 156 | + DEBUG_POSTFIX "-d" |
| 157 | + ) |
| 158 | +endif() |
| 159 | + |
| 160 | +if ( E57_BUILD_SHARED ) |
| 161 | + set_target_properties( E57Format |
| 162 | + PROPERTIES |
| 163 | + VERSION ${PROJECT_VERSION} |
| 164 | + SOVERSION ${PROJECT_VERSION_MAJOR} |
| 165 | + ) |
| 166 | +endif() |
| 167 | + |
| 168 | +target_compile_features( ${PROJECT_NAME} |
| 169 | + PRIVATE |
| 170 | + cxx_std_14 |
| 171 | +) |
| 172 | + |
| 173 | +# Warnings |
| 174 | +include( CompilerWarnings ) |
| 175 | + |
| 176 | +# Check our validation level |
| 177 | +string( STRIP "${E57_VALIDATION_LEVEL}" E57_VALIDATION_LEVEL ) |
| 178 | +if ( NOT E57_VALIDATION_LEVEL MATCHES "^[0-2]$" ) |
| 179 | + message( FATAL_ERROR "[${PROJECT_NAME}] E57_VALIDATION_LEVEL should be 0-2: '${E57_VALIDATION_LEVEL}'" ) |
| 180 | +else() |
| 181 | + message( STATUS "[${PROJECT_NAME}] Setting validation level to ${E57_VALIDATION_LEVEL}" ) |
| 182 | +endif () |
| 183 | + |
| 184 | +# Target definitions |
| 185 | +target_compile_definitions( E57Format |
| 186 | + PRIVATE |
| 187 | + REVISION_ID="${REVISION_ID}" |
| 188 | + E57_VALIDATION_LEVEL=${E57_VALIDATION_LEVEL} |
| 189 | + $<$<BOOL:${E57_ENABLE_DIAGNOSTIC_OUTPUT}>:E57_ENABLE_DIAGNOSTIC_OUTPUT> |
| 190 | + $<$<BOOL:${E57_VERBOSE}>:E57_VERBOSE> |
| 191 | + $<$<BOOL:${E57_WRITE_CRAZY_PACKET_MODE}>:E57_WRITE_CRAZY_PACKET_MODE> |
| 192 | +) |
| 193 | + |
| 194 | +# sanitizers |
| 195 | +include( Sanitizers ) |
| 196 | + |
| 197 | +# Target Libraries |
| 198 | +target_link_libraries( E57Format PRIVATE XercesC::XercesC ) |
| 199 | + |
| 200 | +# Install |
| 201 | +install( |
| 202 | + TARGETS |
| 203 | + E57Format |
| 204 | + EXPORT |
| 205 | + E57Format-export |
| 206 | +) |
| 207 | + |
| 208 | +# ccache |
| 209 | +# Turns on ccache if found |
| 210 | +include( ccache ) |
| 211 | + |
| 212 | +# Formatting |
| 213 | +include( ClangFormat ) |
| 214 | + |
| 215 | +# Testing |
| 216 | +option( E57_BUILD_TEST |
| 217 | + "Build tests" |
| 218 | + ${E57_BUILDING_SELF} |
| 219 | +) |
| 220 | + |
| 221 | +if ( E57_BUILD_TEST ) |
| 222 | + message( STATUS "[${PROJECT_NAME}] Testing enabled" ) |
| 223 | + |
| 224 | + enable_testing() |
| 225 | + |
| 226 | + add_subdirectory( test ) |
| 227 | +endif() |
| 228 | + |
| 229 | +# CMake package files |
| 230 | +include( GNUInstallDirs ) |
| 231 | +set( E57_INSTALL_CMAKEDIR |
| 232 | + "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" |
| 233 | + CACHE STRING |
| 234 | + "Install path for ${PROJECT_NAME} CMake files" |
| 235 | +) |
| 236 | + |
| 237 | +message( STATUS "[${PROJECT_NAME}] CMake files install to ${CMAKE_INSTALL_PREFIX}/${E57_INSTALL_CMAKEDIR}" ) |
| 238 | + |
| 239 | +install( |
| 240 | + EXPORT |
| 241 | + E57Format-export |
| 242 | + DESTINATION |
| 243 | + "${E57_INSTALL_CMAKEDIR}" |
| 244 | +) |
| 245 | + |
| 246 | +include(CMakePackageConfigHelpers) |
| 247 | +write_basic_package_version_file ( |
| 248 | + e57format-config-version.cmake |
| 249 | + VERSION ${PROJECT_VERSION} |
| 250 | + COMPATIBILITY SameMajorVersion |
| 251 | +) |
| 252 | + |
| 253 | +install( |
| 254 | + FILES |
| 255 | + ${CMAKE_CURRENT_SOURCE_DIR}/cmake/e57format-config.cmake |
| 256 | + ${CMAKE_CURRENT_BINARY_DIR}/e57format-config-version.cmake |
| 257 | + DESTINATION |
| 258 | + "${E57_INSTALL_CMAKEDIR}" |
| 259 | +) |
0 commit comments