Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 1 | # Ceres Solver - A fast non-linear least squares minimizer |
| 2 | # Copyright 2019 Google Inc. All rights reserved. |
| 3 | # http://ceres-solver.org/ |
| 4 | # |
| 5 | # Redistribution and use in source and binary forms, with or without |
| 6 | # modification, are permitted provided that the following conditions are met: |
| 7 | # |
| 8 | # * Redistributions of source code must retain the above copyright notice, |
| 9 | # this list of conditions and the following disclaimer. |
| 10 | # * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | # this list of conditions and the following disclaimer in the documentation |
| 12 | # and/or other materials provided with the distribution. |
| 13 | # * Neither the name of Google Inc. nor the names of its contributors may be |
| 14 | # used to endorse or promote products derived from this software without |
| 15 | # specific prior written permission. |
| 16 | # |
| 17 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 21 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | # POSSIBILITY OF SUCH DAMAGE. |
| 28 | # |
| 29 | # Author: alexs.mac@gmail.com (Alex Stewart) |
| 30 | |
| 31 | # Usage: enable_sanitizer(REQUIRED_SANITIZERS) where REQUIRED_SANITIZERS should |
| 32 | # contain the list of sanitizers to enable by updating CMAKE_CXX_FLAGS and |
| 33 | # CMAKE_EXE_LINKER_FLAGS. |
| 34 | # |
| 35 | # The specified sanitizers will be checked both for compatibility with the |
| 36 | # current compiler and with each other as some sanitizers are mutually |
| 37 | # exclusive. |
| 38 | macro(enable_sanitizer) |
| 39 | # According to the Clang documentation [1] the following sanitizers are |
| 40 | # mututally exclusive. |
| 41 | # [1]: https://clang.llvm.org/docs/UsersManual.html#controlling-code-generation |
| 42 | set(INCOMPATIBLE_SANITIZERS address thread memory) |
| 43 | # Set the recommended additional common compile flags for any sanitizer to |
| 44 | # get the best possible output, e.g [2] but make them visible in the cache |
| 45 | # so that the user can edit them if required. |
| 46 | # [2]: https://clang.llvm.org/docs/AddressSanitizer.html#usage |
| 47 | set(COMMON_SANITIZER_COMPILE_OPTIONS |
| 48 | "-g -fno-omit-frame-pointer -fno-optimize-sibling-calls" |
| 49 | CACHE STRING "Common compile flags enabled for any sanitizer") |
| 50 | |
| 51 | # Check that the specified list of sanitizers to enable does not include |
| 52 | # multiple entries from the incompatible list. |
| 53 | set(MERGED_SANITIZERS ${ARGN} ${INCOMPATIBLE_SANITIZERS}) |
| 54 | list(LENGTH MERGED_SANITIZERS COMBINED_LENGTH) |
| 55 | list(REMOVE_DUPLICATES MERGED_SANITIZERS) |
| 56 | list(LENGTH MERGED_SANITIZERS COMBINED_LENGTH_NO_DUPLICATES) |
| 57 | math(EXPR VALID_LENGTH "${COMBINED_LENGTH} - 1") |
| 58 | if (COMBINED_LENGTH_NO_DUPLICATES LESS VALID_LENGTH) |
| 59 | include(PrettyPrintCMakeList) |
| 60 | pretty_print_cmake_list(REQUESTED_SANITIZERS ${ARGN}) |
| 61 | pretty_print_cmake_list( |
| 62 | PRETTY_INCOMPATIBLE_SANITIZERS ${INCOMPATIBLE_SANITIZERS}) |
| 63 | message(FATAL_ERROR "Found incompatible sanitizers in requested set: " |
| 64 | "${REQUESTED_SANITIZERS}. The following sanitizers are mutually " |
| 65 | "exclusive: ${PRETTY_INCOMPATIBLE_SANITIZERS}") |
| 66 | endif() |
| 67 | |
| 68 | # Until CMake 3.14 and CMAKE_REQUIRED_LINK_OPTIONS there was no equivalent to |
| 69 | # CMAKE_REQUIRED_FLAGS for try_compile() for linker flags. However, in CMake |
| 70 | # 3.2 CMP0056 was introduced that when enabled passes CMAKE_EXE_LINKER_FLAGS |
| 71 | # to try_compile() which allows us to achieve the same effect. |
| 72 | cmake_policy(SET CMP0056 NEW) |
| 73 | include(CheckCXXCompilerFlag) |
| 74 | |
| 75 | unset(ADDED_SANITIZER) |
| 76 | foreach(REQUESTED_SANITIZER ${ARGN}) |
| 77 | set(SANITIZER_FLAG -fsanitize=${REQUESTED_SANITIZER}) |
| 78 | # Save the current CMAKE_EXE_LINKER_FLAGS before modifying it to test for |
| 79 | # the existence of the sanitizer flag so that we can revert after the test. |
| 80 | set(INITIAL_CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}") |
| 81 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${SANITIZER_FLAG}") |
| 82 | check_cxx_compiler_flag(${SANITIZER_FLAG} HAVE_SANITIZER) |
| 83 | set(CMAKE_EXE_LINKER_FLAGS "${INITIAL_CMAKE_EXE_LINKER_FLAGS}") |
| 84 | if (NOT HAVE_SANITIZER) |
| 85 | message(FATAL_ERROR "Specified sanitizer: ${REQUESTED_SANITIZER} is not " |
| 86 | "supported by the compiler.") |
| 87 | endif() |
| 88 | message(STATUS "Enabling sanitizer: ${REQUESTED_SANITIZER}") |
| 89 | set(ADDED_SANITIZER TRUE) |
| 90 | # As per the Clang documentation, the sanitizer flags must be added to both |
| 91 | # the compiler and linker flags. |
| 92 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SANITIZER_FLAG}") |
| 93 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${SANITIZER_FLAG}") |
| 94 | endforeach() |
| 95 | if (ADDED_SANITIZER) |
| 96 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMMON_SANITIZER_COMPILE_OPTIONS}") |
| 97 | endif() |
| 98 | endmacro() |