DefineCompilerFlags.cmake 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # (c) 2014 Copyright ownCloud GmbH
  2. # Redistribution and use is allowed according to the terms of the BSD license.
  3. # For details see the accompanying COPYING* file.
  4. # define system dependent compiler flags
  5. include(CheckCCompilerFlag)
  6. include(CheckCCompilerFlagSSP)
  7. #
  8. # Define GNUCC compiler flags
  9. #
  10. if (${CMAKE_C_COMPILER_ID} MATCHES "(GNU|Clang)")
  11. # add -Wconversion ?
  12. # cannot be pedantic with sqlite3 directly linked
  13. # FIXME Can we somehow not use those flags for sqlite3.* but use them for the rest of csync?
  14. if (NOT USE_OUR_OWN_SQLITE3)
  15. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -pedantic -pedantic-errors")
  16. endif()
  17. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wshadow -Wmissing-prototypes")
  18. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wunused -Wfloat-equal -Wpointer-arith -Wwrite-strings -Wformat-security")
  19. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-format-attribute")
  20. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-format-attribute -D_GNU_SOURCE")
  21. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D__STDC_FORMAT_MACROS=1")
  22. if (${CMAKE_C_COMPILER_ID} MATCHES "Clang")
  23. # Disable warning for assert() statements in csync
  24. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-gnu-statement-expression")
  25. endif()
  26. set(CSYNC_STRICT OFF CACHE BOOL "Strict error checking, enabled -Werror and friends")
  27. if (CSYNC_STRICT)
  28. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
  29. endif(CSYNC_STRICT)
  30. # with -fPIC
  31. check_c_compiler_flag("-fPIC" WITH_FPIC)
  32. if (WITH_FPIC AND NOT WIN32)
  33. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
  34. endif (WITH_FPIC AND NOT WIN32)
  35. check_c_compiler_flag_ssp("-fstack-protector" WITH_STACK_PROTECTOR)
  36. if (WITH_STACK_PROTECTOR AND NOT WIN32)
  37. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-protector")
  38. endif (WITH_STACK_PROTECTOR AND NOT WIN32)
  39. if (CMAKE_BUILD_TYPE)
  40. string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER)
  41. if (CMAKE_BUILD_TYPE_LOWER MATCHES "(release|relwithdebinfo|minsizerel)" AND (NOT ${CMAKE_C_FLAGS} MATCHES "FORTIFY_SOURCE=[3-9]"))
  42. check_c_compiler_flag("-Wp,-D_FORTIFY_SOURCE=2" WITH_FORTIFY_SOURCE)
  43. if (WITH_FORTIFY_SOURCE)
  44. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wp,-D_FORTIFY_SOURCE=2")
  45. endif (WITH_FORTIFY_SOURCE)
  46. endif()
  47. endif()
  48. endif (${CMAKE_C_COMPILER_ID} MATCHES "(GNU|Clang)")
  49. if (UNIX AND NOT WIN32)
  50. #
  51. # Check for large filesystem support
  52. #
  53. if (CMAKE_SIZEOF_VOID_P MATCHES "8")
  54. # with large file support
  55. execute_process(
  56. COMMAND
  57. getconf LFS64_CFLAGS
  58. OUTPUT_VARIABLE
  59. _lfs_CFLAGS
  60. ERROR_QUIET
  61. OUTPUT_STRIP_TRAILING_WHITESPACE
  62. )
  63. else (CMAKE_SIZEOF_VOID_P MATCHES "8")
  64. # with large file support
  65. execute_process(
  66. COMMAND
  67. getconf LFS_CFLAGS
  68. OUTPUT_VARIABLE
  69. _lfs_CFLAGS
  70. ERROR_QUIET
  71. OUTPUT_STRIP_TRAILING_WHITESPACE
  72. )
  73. endif (CMAKE_SIZEOF_VOID_P MATCHES "8")
  74. if (_lfs_CFLAGS)
  75. string(REGEX REPLACE "[\r\n]" " " "${_lfs_CFLAGS}" "${${_lfs_CFLAGS}}")
  76. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_lfs_CFLAGS}")
  77. endif (_lfs_CFLAGS)
  78. else(UNIX AND NOT WIN32)
  79. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_FILE_OFFSET_BITS=64")
  80. endif (UNIX AND NOT WIN32)
  81. if (MSVC)
  82. # Use secure functions by default and suppress warnings about
  83. #"deprecated" functions
  84. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES=1")
  85. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_COUNT=1")
  86. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D _CRT_NONSTDC_NO_WARNINGS=1 /D _CRT_SECURE_NO_WARNINGS=1")
  87. endif (MSVC)