compiler_setup.cmake 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # Copyright 2018 Google
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # C++ Compiler setup
  15. include(compiler_id)
  16. # We use C++11
  17. set(CMAKE_CXX_STANDARD 11)
  18. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  19. set(CMAKE_CXX_EXTENSIONS OFF)
  20. if(CMAKE_GENERATOR STREQUAL "Ninja")
  21. set(NINJA ON)
  22. endif()
  23. if(CXX_CLANG OR CXX_GNU)
  24. set(
  25. common_flags
  26. -Wall -Wextra -Werror
  27. # Be super pedantic about format strings
  28. -Wformat
  29. # Avoid use of uninitialized values
  30. -Wuninitialized
  31. -fno-common
  32. # Delete unused things
  33. -Wunused-function -Wunused-value -Wunused-variable
  34. )
  35. set(
  36. cxx_flags
  37. # Cut down on symbol clutter
  38. # TODO(wilhuff) try -fvisibility=hidden
  39. -fvisibility-inlines-hidden
  40. )
  41. set(
  42. c_flags
  43. -Wstrict-prototypes
  44. )
  45. if(CXX_CLANG)
  46. list(
  47. APPEND common_flags
  48. -Wconditional-uninitialized -Werror=return-type -Winfinite-recursion -Wmove
  49. -Wrange-loop-analysis -Wunreachable-code
  50. # Options added to match apple recommended project settings
  51. -Wcomma
  52. )
  53. endif()
  54. if(NINJA)
  55. # If building under Ninja, disable tty detection and force color output
  56. if(CXX_CLANG OR CXX_GNU)
  57. list(APPEND common_flags -fdiagnostics-color)
  58. endif()
  59. endif()
  60. foreach(flag ${common_flags} ${c_flags})
  61. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}")
  62. endforeach()
  63. foreach(flag ${common_flags} ${cxx_flags})
  64. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
  65. endforeach()
  66. endif()
  67. if(APPLE)
  68. # CMake has no special support for Objective-C as a distinct language but
  69. # enabling modules and other clang extensions would apply even to regular C++
  70. # sources which is nonportable. Keep these flags separate to avoid misuse.
  71. set(
  72. OBJC_FLAGS
  73. -Werror=deprecated-objc-isa-usage
  74. -Werror=non-modular-include-in-framework-module
  75. -Werror=objc-root-class
  76. -Wblock-capture-autoreleasing
  77. -Wimplicit-atomic-properties
  78. -Wnon-modular-include-in-framework-module
  79. -fobjc-arc
  80. -fmodules
  81. -fno-autolink
  82. -F${FIREBASE_INSTALL_DIR}/Frameworks
  83. )
  84. endif()
  85. if(MSVC)
  86. set(
  87. common_flags
  88. # Cut down on symbol cruft in windows.h
  89. /DWIN32_LEAN_AND_MEAN=1
  90. /DNOMINMAX=1
  91. # Specify at least Windows Vista/Server 2008 (required by gRPC)
  92. /D_WIN32_WINNT=0x600
  93. # Disable warnings that can't be easily addressed or are ignored by
  94. # upstream projects.
  95. # unary minus operator applied to unsigned type, result still unsigned
  96. /wd4146
  97. )
  98. endif()
  99. foreach(flag ${common_flags} ${c_flags})
  100. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}")
  101. endforeach()
  102. foreach(flag ${common_flags} ${cxx_flags})
  103. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
  104. endforeach()