CMakeLists.txt (14483B)
1 # Ubuntu 14.04 (Trusty) 2 cmake_minimum_required (VERSION 2.8.12.2) 3 # Centos 7 4 #cmake_minimum_required (VERSION 2.8.11) 5 #cmake_minimum_required (VERSION 2.8) 6 7 # Experimental for generating compile_commands.json so editors with 8 # clangd language server support can use it. Symlink 9 # build/Debug/compile_commands.json to project root where it is 10 # gitignored. 11 #set(CMAKE_EXPORT_COMPILE_COMMANDS 1) 12 13 # Disable build of tests and samples. Due to custom build step 14 # dependency on flatcc tool, some custom build configurations may 15 # experience issues, and this option can then help. 16 option(FLATCC_TEST "enable tests" ON) 17 18 # Only active if FLATCC_TEST is active. Used to ensure that C++ users 19 # can include generatd C source. Old GCC pre 4.7 won't compile C++ test 20 # project. 21 option(FLATCC_CXX_TEST "enable C++ tests" ON) 22 23 # Note that linking with flatcc debug libraries may require souce code to also use 24 # the sanitize flag. 25 option(FLATCC_DEBUG_CLANG_SANITIZE "enable clang sanitize flag for debug build" ON) 26 27 # Conditionally set project languages based on FLATCC_TEST, as C++ is 28 # only necessary if building the tests. 29 if (FLATCC_TEST AND FLATCC_CXX_TEST) 30 project (FlatCC C CXX) 31 else() 32 project (FlatCC C) 33 endif() 34 35 # 36 # NOTE: when changing build options, clean the build using on of: 37 # 38 # scripts/cleanall.sh 39 # scripts/test.sh 40 # 41 42 # Force use of portable shims such as providing `static_assert`, and 43 # `stdaligh.h`. Otherwise this option is automatically enabled for some 44 # known compiler configurations below. 45 option (FLATCC_PORTABLE 46 "include extra headers for compilers that do not support certain C11 features" OFF) 47 48 # It is not possible to detect posix_memalign when compiling with 49 # -std=c11 but aligned_alloc is not always available either. 50 # This options assumes that posix_memalign is then available. 51 # Without C11, detection depends on _POSIX_C_SOURCE. 52 option (FLATCC_GNU_POSIX_MEMALIGN 53 "use posix_memalign on gnu systems also when C11 is configured" ON) 54 55 # Only build the runtime library - mostly intended in combination with 56 # FLATCC_INSTALL for cross compiling targets. 57 option(FLATCC_RTONLY "enable build of runtime library only" OFF) 58 59 # Use with or witout FLATCC_RTONLY to enable install targets. 60 # Libraries are built statically by default, but can CMake's 61 # cmake -DBUILD_SHARED_LIBS=on can override. 62 option(FLATCC_INSTALL "enable install targets" OFF) 63 64 # Use with debug build with testing enabled only. Enables generation 65 # of coverage information during build and run. Adds target "coverage" 66 # which collects data and makes HTML report in build directory 67 option(FLATCC_COVERAGE "enable coverage" OFF) 68 69 # Affects the flatbuffer verify operation. Normally a verify should just 70 # quickly reject invalid buffers but for troubleshooting, assertions can 71 # enabled. This requires rebuilding the runtime library and will likely 72 # break test cases (those that tests that an invalid buffer is invalid). 73 option (FLATCC_DEBUG_VERIFY 74 "assert on verify failure in runtime lib" OFF) 75 76 # Print detailed traces of binary buffer contents when calling verify. 77 option (FLATCC_TRACE_VERIFY 78 "assert on verify failure in runtime lib" OFF) 79 80 # Reflection is the compilers ability to generate binary schema output 81 # (.bfbs files). This requires using generated code from 82 # `reflection.fbs`. During development it may not be possible to 83 # compile with reflection enabled because it can become impossible to 84 # fix broken builds. It may also be disabled simple because it isn't 85 # needed. 86 option (FLATCC_REFLECTION 87 "generation of binary flatbuffer schema files" ON) 88 89 # FLATCC_NATIVE_OPTIM and FLATCC_FAST_DOUBLE affects json parsing, 90 # especially if the content is pretty printed. But it is plenty 91 # fast without these settings in most cases. Not recommended. 92 option (FLATCC_NATIVE_OPTIM 93 "use machine native optimizations like SSE 4.2" OFF) 94 95 # Fast grisu3 string/floating point conversion still depends on strtod 96 # for about 1-2% of the conversions in order to produce an exact result. 97 # By allowing a minor difference in the least significant bits, this 98 # dependeny can be avoided, and speed improved. Some strtod 99 # implementations call strlen which is really slow on large JSON 100 # buffers, and catastrophic on buffers that are not zero-terminated - 101 # regardless of size. Most platforms have a decent strtod these days. 102 option (FLATCC_FAST_DOUBLE 103 "faster but slightly incorrect floating point parser (json)" OFF) 104 105 # -Werror is only set for some compiler versions that are believed to 106 # to not generate any warnings. If the assumption breaks, disable 107 # this option if the warning is not significant. 108 option (FLATCC_ALLOW_WERROR "allow -Werror to be configured" ON) 109 110 # Experimental setting - sometimes the code branches on a constant 111 # expression in order to select the best option for a given type size or 112 # similar. Sometimes compilers don't like that. If this issue surfaces, 113 # try using this option. 114 option (FLATCC_IGNORE_CONST_COND "silence const condition warnings" OFF) 115 116 if (FLATCC_RTONLY) 117 set(FLATCC_TEST off) 118 endif() 119 120 if (FLATCC_TEST) 121 enable_testing() 122 endif() 123 124 if (NOT FLATCC_TEST) 125 set(FLATCC_COVERAGE off) 126 endif() 127 128 if (NOT CMAKE_BUILD_TYPE MATCHES Debug) 129 set(FLATCC_COVERAGE off) 130 endif() 131 132 if (FLATCC_COVERAGE) 133 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --coverage -DNDEBUG") 134 endif() 135 136 if (FLATCC_DEBUG_VERIFY) 137 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DFLATCC_DEBUG_VERIFY=1") 138 endif() 139 140 if (FLATCC_TRACE_VERIFY) 141 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DFLATCC_TRACE_VERIFY=1") 142 endif() 143 144 145 if (FLATCC_REFLECTION) 146 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DFLATCC_REFLECTION=1") 147 else() 148 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DFLATCC_REFLECTION=0") 149 endif() 150 151 152 if (FLATCC_NATIVE_OPTIM) 153 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=native -DFLATCC_USE_SSE4_2=1") 154 endif() 155 156 if (FLATCC_FAST_DOUBLE) 157 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DGRISU3_PARSE_ALLOW_ERROR -DFLATCC_USE_GRISU3=1") 158 endif() 159 160 if (NOT DEFINED FLATCC_INSTALL_LIB) 161 set(lib_dir lib) 162 else() 163 set(lib_dir ${FLATCC_INSTALL_LIB}) 164 endif() 165 166 # The folder of this directory, as apposed to CMAKE_BINARY_DIR 167 # which would usually be the build/Release and build/Debug paths 168 set (dist_dir "${PROJECT_SOURCE_DIR}") 169 # set (dist_dir "${CMAKE_BINARY_DIR}") 170 171 message(STATUS "dist install dir ${dist_dir}") 172 message(STATUS "lib install dir ${dist_dir}/${lib_dir}") 173 174 # Note: for compiling generated C code, warnings of unused functions 175 # and constants should be turned off - those are plentiful. They are 176 # silenced for Clang, GCC and MSVC in generated headers.headers. 177 178 if (CMAKE_C_COMPILER_ID MATCHES "Clang" AND NOT "${CMAKE_CXX_SIMULATE_ID}" STREQUAL "MSVC") 179 # Clang or AppleClang 180 message(STATUS "Setting Clang compiler options") 181 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wstrict-prototypes") 182 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wsign-conversion") 183 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wconversion") 184 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11 -pedantic -Wall -Wextra") 185 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wall -Wextra") 186 # Fix broken C++ alignas - either will do 187 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 188 #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DPORTABLE_PATCH_CPLUSPLUS_STDALIGN") 189 if (FLATCC_ALLOW_WERROR) 190 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror") 191 endif() 192 if (FLATCC_IGNORE_CONST_COND) 193 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-tautological-constant-out-of-range-compare") 194 endif() 195 if (FLATCC_DEBUG_CLANG_SANITIZE) 196 if (CMAKE_BUILD_TYPE MATCHES Debug) 197 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined") 198 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined") 199 endif() 200 endif() 201 # Suppress warning relaxed in clang-6, see https://reviews.llvm.org/D28148 202 if (CMAKE_C_COMPILER_VERSION VERSION_LESS 6) 203 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-missing-field-initializers") 204 endif() 205 206 # To get assembly output 207 # set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -save-temps") 208 209 elseif (CMAKE_C_COMPILER_ID STREQUAL "GNU") 210 execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion 211 OUTPUT_VARIABLE GCC_VERSION) 212 if (GCC_VERSION VERSION_LESS 4.7) 213 message(STATUS "Setting older GNU C compiler options with FLATCC_PORTABLE") 214 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra") 215 # We need stdalign.h 216 set(FLATCC_PORTABLE true) 217 # Disable C++ test for old compilers known to break due to 218 # missing stdalign.h and incomplete stdint.h which is not a 219 # priority to fix in portable library for C++ use case. 220 # Note: we test the C compiler version not the C++ compiler 221 # version, but that is (hopefully) close enough. 222 if (FLATCC_CXX_TEST) 223 message(STATUS "Disabling C++ tests for GCC pre 4.7") 224 set(FLATCC_CXX_TEST false) 225 endif() 226 else() 227 message(STATUS "Setting GNU C compiler options with c11 and Posix") 228 if (GCC_VERSION VERSION_LESS 8.0) 229 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11 -pedantic -Wall -Wextra") 230 elseif (NOT (GCC_VERSION VERSION_LESS 8.0)) 231 # Disable some GCC checks: 232 # (warnings exist since 8.0, but are more aggressive in 9.0) 233 # 234 # -Wstringop-truncation: 235 # GCC 9 warns on truncated strncpy into char arrays in FlatBuffer 236 # structs, but these are valid as zero-paddded, not zero terminated. 237 # 238 # -Wno-format-overflow: 239 # GCC 9 warns on mistakenly assumed NULL string when 240 # printing from a required FlatBuffer string field. 241 # 242 message(STATUS "Disabling -pedantic for GCC >= 8.0") 243 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11 -Wall -Wextra") 244 message(STATUS "Disabling GNU C compiler warnings: -Wstringop-truncation -Wno-format-overflow") 245 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-stringop-truncation -Wno-format-overflow") 246 endif() 247 if (NOT (GCC_VERSION VERSION_LESS 11.0)) 248 # Disable warning on misleading indentation it become more aggressive in 11.0 249 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-misleading-indentation") 250 endif() 251 if (FLATCC_GNU_POSIX_MEMALIGN) 252 # -std=c11 prevents detection of posix_memalign and aligned_alloc might be missing 253 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DPORTABLE_POSIX_MEMALIGN=1") 254 endif() 255 if (FLATCC_ALLOW_WERROR) 256 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror") 257 endif() 258 endif() 259 if (FLATCC_IGNORE_CONST_COND) 260 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-type-limits") 261 endif() 262 263 # Too aggressive, e.g. main() is not permitted and main with 264 # args then yields unused arg warning. 265 # set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wstrict-prototypes") 266 267 # In gcc 4.8 it is not possible to suppress this warning using 268 # #pragma GCC diagnostic ignored "-Wunused-function" 269 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-function") 270 # set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-type-limits") 271 272 if (GCC_VERSION VERSION_LESS 4.8) 273 # -Wsign-conversion broken for GCC 4.7 conditional operator 274 else() 275 # Might be disabled if GCC keeps getting more agressive. 276 # Incorrectly warns on explicit char to uint32_t casts. 277 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wsign-conversion") 278 279 # Too aggressive, warns on `x = x + 1;` or `n = -n;`. 280 # set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wconversion") 281 endif() 282 283 elseif (CMAKE_C_COMPILER_ID STREQUAL "Intel") 284 message(STATUS "Setting Intel C compiler options") 285 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11 -Wall -Wextra") 286 elseif (MSVC) # using STREQUAL here conflicts with string interpretation changes in CMake 287 message(STATUS "Setting MSVC C compiler options") 288 # -DFLATCC_PORTABLE also required, but set earlier 289 # -W3 is the highest warning level that is reasonable. 290 # See include/flatcc/portable/pwarnings.h for disabled warnings. 291 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -W3 -D_CRT_SECURE_NO_WARNINGS") 292 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -W3 -D_CRT_SECURE_NO_WARNINGS") 293 # MSVC 2013 (1800) supports inline variable declations 294 # while MSVC 2010 (1600) does not. 295 if (MSVC_VERSION STRLESS "1800") 296 # Disables monster sample build which uses C99 style variable decls. 297 set (FLATCC_NEED_C89_VAR_DECLS true) 298 endif() 299 set(FLATCC_PORTABLE true) 300 elseif (CMAKE_C_COMPILER_ID STREQUAL "XL") 301 # IBM's native XLC C compiler in extended C99 mode 302 303 message(STATUS "Setting IBM XL C compiler options") 304 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -qlanglvl=extc99") 305 else() 306 # Best effort 307 message(STATUS "Best effort settings for compiler: ${CMAKE_C_COMPILER_ID}") 308 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall") 309 set(FLATCC_PORTABLE true) 310 endif() 311 312 if (FLATCC_PORTABLE) 313 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DFLATCC_PORTABLE") 314 endif() 315 316 if (CLANG_VERSION) 317 message(STATUS "CLANG_VERSION: ${CLANG_VERSION}") 318 endif() 319 if (GCC_VERSION) 320 message(STATUS "GCC_VERSION: ${GCC_VERSION}") 321 endif() 322 message(STATUS "Configured C_FLAGS: ${CMAKE_C_FLAGS}") 323 324 set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/${lib_dir}) 325 326 set(CMAKE_DEBUG_POSTFIX "_d") 327 328 if (CMAKE_BUILD_TYPE MATCHES "Debug") 329 set(CMAKE_EXECUTABLE_SUFFIX "_d${CMAKE_EXECUTABLE_SUFFIX}") 330 endif() 331 332 333 if (FLATCC_RTONLY) 334 # The targets we copy to bin and lib directories, i.e. not tests. 335 set(dist_targets 336 flatccrt 337 ) 338 add_subdirectory(src/runtime) 339 else() 340 # The targets we copy to bin and lib directories, i.e. not tests. 341 set(dist_targets 342 flatcc 343 flatccrt 344 flatcc_cli 345 ) 346 add_subdirectory(src/runtime) 347 add_subdirectory(src/compiler) 348 add_subdirectory(src/cli) 349 endif() 350 351 # disabled by FLATCC_RTONLY 352 if (FLATCC_TEST) 353 add_subdirectory(test) 354 add_subdirectory(samples) 355 endif() 356 357 if (FLATCC_COVERAGE) 358 add_custom_target(coverage 359 COMMAND lcov --capture --directory src --output-file coverage.info 360 COMMAND genhtml coverage.info --output-directory coverage) 361 endif() 362 363 set_target_properties(${dist_targets} 364 PROPERTIES 365 ARCHIVE_OUTPUT_DIRECTORY "${dist_dir}/${lib_dir}" 366 LIBRARY_OUTPUT_DIRECTORY "${dist_dir}/${lib_dir}" 367 RUNTIME_OUTPUT_DIRECTORY "${dist_dir}/bin" 368 ) 369 370 if (FLATCC_INSTALL) 371 install(DIRECTORY include/flatcc DESTINATION include) 372 endif() 373