CompileFlags.cmake 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #-----------------------------------------------------------------------------
  4. # set some special flags for different compilers
  5. #
  6. if(WIN32 AND CMAKE_C_COMPILER_ID STREQUAL "Intel")
  7. set(_INTEL_WINDOWS 1)
  8. endif()
  9. if(WIN32 AND CMAKE_C_COMPILER_ID STREQUAL "Clang"
  10. AND "x${CMAKE_CXX_SIMULATE_ID}" STREQUAL "xMSVC")
  11. set(_CLANG_MSVC_WINDOWS 1)
  12. endif()
  13. # Disable deprecation warnings for standard C functions.
  14. # really only needed for newer versions of VS, but should
  15. # not hurt other versions, and this will work into the
  16. # future
  17. if(MSVC OR _INTEL_WINDOWS OR _CLANG_MSVC_WINDOWS)
  18. add_definitions(-D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE)
  19. else()
  20. endif()
  21. if(MSVC)
  22. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${CMAKE_CXX_LINKER_WRAPPER_FLAG}-stack:10000000")
  23. endif()
  24. # MSVC 14.28 enables C5105, but the Windows SDK 10.0.18362.0 triggers it.
  25. if(CMAKE_C_COMPILER_ID STREQUAL "MSVC" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 19.28)
  26. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -wd5105")
  27. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -wd5105")
  28. endif()
  29. if(_CLANG_MSVC_WINDOWS AND "x${CMAKE_CXX_COMPILER_FRONTEND_VARIANT}" STREQUAL "xGNU")
  30. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Xlinker -stack:20000000")
  31. endif()
  32. #silence duplicate symbol warnings on AIX
  33. if(CMAKE_SYSTEM_NAME MATCHES "AIX")
  34. if(NOT CMAKE_COMPILER_IS_GNUCXX)
  35. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -bhalt:5 ")
  36. endif()
  37. endif()
  38. if(CMAKE_SYSTEM MATCHES "OSF1-V")
  39. if(NOT CMAKE_COMPILER_IS_GNUCXX)
  40. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -timplicit_local -no_implicit_include ")
  41. endif()
  42. endif()
  43. # Workaround for short jump tables on PA-RISC
  44. if(CMAKE_SYSTEM_PROCESSOR MATCHES "^parisc")
  45. if(CMAKE_COMPILER_IS_GNUCC)
  46. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mlong-calls")
  47. endif()
  48. if(CMAKE_COMPILER_IS_GNUCXX)
  49. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mlong-calls")
  50. endif()
  51. endif()
  52. # Use 64-bit off_t on 32-bit Linux
  53. if (CMAKE_SYSTEM_NAME STREQUAL "Linux" AND CMAKE_SIZEOF_VOID_P EQUAL 4)
  54. # ensure 64bit offsets are used for filesystem accesses for 32bit compilation
  55. add_definitions(-D_FILE_OFFSET_BITS=64)
  56. endif()
  57. # Workaround for TOC Overflow on ppc64
  58. set(bigTocFlag "")
  59. if(CMAKE_SYSTEM_NAME STREQUAL "AIX" AND
  60. CMAKE_SYSTEM_PROCESSOR MATCHES "powerpc")
  61. set(bigTocFlag "-Wl,-bbigtoc")
  62. elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND
  63. CMAKE_SYSTEM_PROCESSOR MATCHES "ppc64")
  64. set(bigTocFlag "-Wl,--no-multi-toc")
  65. endif()
  66. if(bigTocFlag)
  67. include(CheckCXXLinkerFlag)
  68. check_cxx_linker_flag(${bigTocFlag} BIG_TOC_FLAG_SUPPORTED)
  69. if(BIG_TOC_FLAG_SUPPORTED)
  70. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${bigTocFlag}")
  71. endif()
  72. endif()
  73. if (CMAKE_CXX_COMPILER_ID STREQUAL SunPro AND
  74. NOT DEFINED CMAKE_CXX${CMAKE_CXX_STANDARD}_STANDARD_COMPILE_OPTION)
  75. if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.13)
  76. if (NOT CMAKE_CXX_STANDARD OR CMAKE_CXX_STANDARD EQUAL 98)
  77. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++03")
  78. elseif(CMAKE_VERSION VERSION_LESS 3.8.20170502)
  79. # CMake knows how to add this flag for compilation as C++11,
  80. # but has not been taught that SunPro needs it for linking too.
  81. # Add it in a place that will be used for both.
  82. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
  83. endif()
  84. else()
  85. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -library=stlport4")
  86. endif()
  87. endif()
  88. foreach(lang C CXX)
  89. # Suppress warnings from PGI compiler.
  90. if (CMAKE_${lang}_COMPILER_ID STREQUAL "PGI")
  91. set(CMAKE_${lang}_FLAGS "${CMAKE_${lang}_FLAGS} -w")
  92. endif()
  93. endforeach()
  94. # use the ansi CXX compile flag for building cmake
  95. if (CMAKE_ANSI_CXXFLAGS)
  96. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_ANSI_CXXFLAGS}")
  97. endif ()
  98. if (CMAKE_ANSI_CFLAGS)
  99. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_ANSI_CFLAGS}")
  100. endif ()
  101. # Allow per-translation-unit parallel builds when using MSVC
  102. if(CMAKE_GENERATOR MATCHES "Visual Studio" AND
  103. (CMAKE_C_COMPILER_ID MATCHES "MSVC|Intel" OR
  104. CMAKE_CXX_COMPILER_ID MATCHES "MSVC|Intel"))
  105. set(CMake_MSVC_PARALLEL ON CACHE STRING "\
  106. Enables /MP flag for parallel builds using MSVC. Specify an \
  107. integer value to control the number of threads used (Only \
  108. works on some older versions of Visual Studio). Setting to \
  109. ON lets the toolchain decide how many threads to use. Set to \
  110. OFF to disable /MP completely." )
  111. if(CMake_MSVC_PARALLEL)
  112. if(CMake_MSVC_PARALLEL GREATER 0)
  113. string(APPEND CMAKE_C_FLAGS " /MP${CMake_MSVC_PARALLEL}")
  114. string(APPEND CMAKE_CXX_FLAGS " /MP${CMake_MSVC_PARALLEL}")
  115. else()
  116. string(APPEND CMAKE_C_FLAGS " /MP")
  117. string(APPEND CMAKE_CXX_FLAGS " /MP")
  118. endif()
  119. endif()
  120. endif()
  121. # Get rid of excess -Wunused-but-set-variable on release builds with LCC >= 1.26
  122. foreach(l C CXX)
  123. if(CMAKE_${l}_COMPILER_ID STREQUAL "LCC" AND NOT CMAKE_${l}_COMPILER_VERSION VERSION_LESS 1.26)
  124. foreach(c MINSIZEREL RELEASE RELWITHDEBINFO)
  125. string(APPEND "CMAKE_${l}_FLAGS_${c}" " -Wno-unused-but-set-variable")
  126. endforeach()
  127. endif()
  128. endforeach()