CompilerFlags.cmake 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # Smartbotic-MicroBit Compiler Flags
  2. # Common warnings for all builds
  3. add_compile_options(
  4. -Wall
  5. -Wextra
  6. -Wpedantic
  7. -Werror=return-type
  8. -Werror=unused-result
  9. -Wno-unused-parameter
  10. )
  11. # Debug flags
  12. if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  13. add_compile_options(
  14. -g3
  15. -O0
  16. -fno-omit-frame-pointer
  17. )
  18. option(ENABLE_SANITIZERS "Enable ASan and UBSan" OFF)
  19. if(ENABLE_SANITIZERS)
  20. add_compile_options(-fsanitize=address,undefined)
  21. add_link_options(-fsanitize=address,undefined)
  22. endif()
  23. endif()
  24. # Release flags
  25. if(CMAKE_BUILD_TYPE STREQUAL "Release")
  26. add_compile_options(
  27. -O3
  28. -DNDEBUG
  29. -march=native
  30. )
  31. endif()
  32. # RelWithDebInfo flags
  33. if(CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
  34. add_compile_options(
  35. -O2
  36. -g
  37. -DNDEBUG
  38. )
  39. endif()
  40. # LTO for release builds
  41. if(CMAKE_BUILD_TYPE STREQUAL "Release" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
  42. include(CheckIPOSupported)
  43. check_ipo_supported(RESULT IPO_SUPPORTED OUTPUT IPO_ERROR)
  44. if(IPO_SUPPORTED)
  45. message(STATUS "IPO/LTO enabled")
  46. set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON)
  47. else()
  48. message(STATUS "IPO/LTO not supported: ${IPO_ERROR}")
  49. endif()
  50. endif()
  51. # Position independent code
  52. set(CMAKE_POSITION_INDEPENDENT_CODE ON)