DefineCompilerFlags.cmake 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # define system dependent compiler flags
  2. include(CheckCCompilerFlag)
  3. include(MacroCheckCCompilerFlagSSP)
  4. #
  5. # Define GNUCC compiler flags
  6. #
  7. if (${CMAKE_C_COMPILER_ID} MATCHES GNU)
  8. # add -Wconversion ?
  9. #set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -pedantic -pedantic-errors")
  10. #set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wshadow -Wmissing-prototypes -Wdeclaration-after-statement")
  11. #set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wunused -Wfloat-equal -Wpointer-arith -Wwrite-strings -Wformat-security")
  12. #set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-format-attribute")
  13. #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -pedantic-errors")
  14. #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wshadow")
  15. #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wunused -Wfloat-equal -Wpointer-arith -Wwrite-strings -Wformat-security")
  16. #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wmissing-format-attribute")
  17. if (UNIX AND NOT WIN32)
  18. # with -fPIC
  19. check_c_compiler_flag("-fPIC" WITH_FPIC)
  20. if (WITH_FPIC)
  21. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
  22. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
  23. endif (WITH_FPIC)
  24. endif(UNIX AND NOT WIN32)
  25. check_c_compiler_flag_ssp("-fstack-protector" WITH_STACK_PROTECTOR)
  26. if (WITH_STACK_PROTECTOR)
  27. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-protector")
  28. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstack-protector")
  29. set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -fstack-protector")
  30. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKDER_FLAGS} -fstack-protector")
  31. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKDER_FLAGS} -fstack-protector")
  32. endif (WITH_STACK_PROTECTOR)
  33. check_c_compiler_flag("-D_FORTIFY_SOURCE=2" WITH_FORTIFY_SOURCE)
  34. if (WITH_FORTIFY_SOURCE)
  35. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_FORTIFY_SOURCE=2")
  36. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_FORTIFY_SOURCE=2")
  37. endif (WITH_FORTIFY_SOURCE)
  38. endif (${CMAKE_C_COMPILER_ID} MATCHES GNU)
  39. if (UNIX AND NOT WIN32)
  40. #
  41. # Check for large filesystem support
  42. #
  43. if (CMAKE_SIZEOF_VOID_P MATCHES "8")
  44. # with large file support
  45. execute_process(
  46. COMMAND
  47. getconf LFS64_CFLAGS
  48. OUTPUT_VARIABLE
  49. _lfs_CFLAGS
  50. ERROR_QUIET
  51. OUTPUT_STRIP_TRAILING_WHITESPACE
  52. )
  53. else (CMAKE_SIZEOF_VOID_P MATCHES "8")
  54. # with large file support
  55. execute_process(
  56. COMMAND
  57. getconf LFS_CFLAGS
  58. OUTPUT_VARIABLE
  59. _lfs_CFLAGS
  60. ERROR_QUIET
  61. OUTPUT_STRIP_TRAILING_WHITESPACE
  62. )
  63. endif (CMAKE_SIZEOF_VOID_P MATCHES "8")
  64. if (_lfs_CFLAGS)
  65. string(REGEX REPLACE "[\r\n]" " " "${_lfs_CFLAGS}" "${${_lfs_CFLAGS}}")
  66. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_lfs_CFLAGS}")
  67. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${_lfs_CFLAGS}")
  68. endif (_lfs_CFLAGS)
  69. endif (UNIX AND NOT WIN32)
  70. if (MSVC)
  71. # Use secure functions by defaualt and suppress warnings about
  72. #"deprecated" functions
  73. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES=1")
  74. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_COUNT=1")
  75. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D _CRT_NONSTDC_NO_WARNINGS=1 /D _CRT_SECURE_NO_WARNINGS=1")
  76. set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS}")
  77. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_C_FLAGS}")
  78. endif (MSVC)