draco_sanitizer.cmake 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # Copyright 2021 The Draco Authors
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not
  4. # use this file except in compliance with the License. You may obtain a copy of
  5. # the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. # License for the specific language governing permissions and limitations under
  13. # the License.
  14. if(DRACO_CMAKE_DRACO_SANITIZER_CMAKE_)
  15. return()
  16. endif() # DRACO_CMAKE_DRACO_SANITIZER_CMAKE_
  17. set(DRACO_CMAKE_DRACO_SANITIZER_CMAKE_ 1)
  18. # Handles the details of enabling sanitizers.
  19. macro(draco_configure_sanitizer)
  20. if(DRACO_SANITIZE
  21. AND NOT EMSCRIPTEN
  22. AND NOT MSVC)
  23. if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  24. if(DRACO_SANITIZE MATCHES "cfi")
  25. list(APPEND SAN_CXX_FLAGS "-flto" "-fno-sanitize-trap=cfi")
  26. list(APPEND SAN_LINKER_FLAGS "-flto" "-fno-sanitize-trap=cfi"
  27. "-fuse-ld=gold")
  28. endif()
  29. if(${CMAKE_SIZEOF_VOID_P} EQUAL 4 AND DRACO_SANITIZE MATCHES
  30. "integer|undefined")
  31. list(APPEND SAN_LINKER_FLAGS "--rtlib=compiler-rt" "-lgcc_s")
  32. endif()
  33. endif()
  34. list(APPEND SAN_CXX_FLAGS "-fsanitize=${DRACO_SANITIZE}")
  35. list(APPEND SAN_LINKER_FLAGS "-fsanitize=${DRACO_SANITIZE}")
  36. # Make sanitizer callstacks accurate.
  37. list(APPEND SAN_CXX_FLAGS "-fno-omit-frame-pointer")
  38. list(APPEND SAN_CXX_FLAGS "-fno-optimize-sibling-calls")
  39. draco_test_cxx_flag(FLAG_LIST_VAR_NAMES SAN_CXX_FLAGS FLAG_REQUIRED)
  40. draco_test_exe_linker_flag(FLAG_LIST_VAR_NAME SAN_LINKER_FLAGS)
  41. endif()
  42. endmacro()