CompilerFlags.cmake 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # Compiler flags for SmartBotic
  2. # Warning flags
  3. if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
  4. add_compile_options(
  5. -Wall
  6. -Wextra
  7. -Wpedantic
  8. -Wno-unused-parameter
  9. -Wno-missing-field-initializers
  10. )
  11. # Debug flags
  12. if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  13. add_compile_options(-g -O0 -DDEBUG)
  14. # Note: _GLIBCXX_DEBUG is disabled because it causes ABI incompatibility
  15. # with pre-compiled system libraries (gRPC, etc.)
  16. # add_compile_definitions(_GLIBCXX_DEBUG)
  17. endif()
  18. # Release flags
  19. if(CMAKE_BUILD_TYPE STREQUAL "Release")
  20. add_compile_options(-O3 -DNDEBUG)
  21. add_compile_options(-flto)
  22. add_link_options(-flto)
  23. endif()
  24. # RelWithDebInfo flags
  25. if(CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
  26. add_compile_options(-O2 -g -DNDEBUG)
  27. endif()
  28. endif()
  29. # MSVC flags (for potential Windows support)
  30. if(MSVC)
  31. add_compile_options(/W4 /permissive-)
  32. add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
  33. if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  34. add_compile_options(/Od /Zi)
  35. else()
  36. add_compile_options(/O2)
  37. endif()
  38. endif()
  39. # Enable colored output
  40. if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
  41. add_compile_options(-fdiagnostics-color=always)
  42. elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  43. add_compile_options(-fcolor-diagnostics)
  44. endif()
  45. # Position independent code
  46. set(CMAKE_POSITION_INDEPENDENT_CODE ON)
  47. # Address Sanitizer. CLAUDE.md documents
  48. # cmake -DCMAKE_BUILD_TYPE=Debug -DENABLE_ASAN=ON ..
  49. # as how to build this project with ASAN - this option is what makes that
  50. # command actually do it, rather than silently produce an ordinary
  51. # non-sanitized build (which is exactly what happened before this option
  52. # existed: the flag was documented but had no matching cmake option, so it
  53. # was ignored with no error). -fno-omit-frame-pointer keeps ASAN's stack
  54. # traces readable; leak detection rides along with ASAN on Linux/macOS
  55. # rather than needing -fsanitize=leak separately.
  56. option(ENABLE_ASAN "Build with AddressSanitizer (and leak detection) enabled" OFF)
  57. if(ENABLE_ASAN)
  58. if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
  59. add_compile_options(-fsanitize=address -fno-omit-frame-pointer)
  60. add_link_options(-fsanitize=address)
  61. else()
  62. message(WARNING "ENABLE_ASAN was requested but the compiler (${CMAKE_CXX_COMPILER_ID}) "
  63. "is not GNU or Clang; AddressSanitizer was not enabled")
  64. endif()
  65. endif()