compiler_setup.cmake 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. -Wreorder -Werror=reorder
  41. )
  42. set(
  43. c_flags
  44. -Wstrict-prototypes
  45. )
  46. if(CXX_CLANG)
  47. list(
  48. APPEND common_flags
  49. -Wconditional-uninitialized -Werror=return-type -Winfinite-recursion -Wmove
  50. -Wrange-loop-analysis -Wunreachable-code
  51. # Options added to match apple recommended project settings
  52. -Wcomma
  53. )
  54. endif()
  55. if(NINJA)
  56. # If building under Ninja, disable tty detection and force color output
  57. if(CXX_CLANG OR CXX_GNU)
  58. list(APPEND common_flags -fdiagnostics-color)
  59. endif()
  60. endif()
  61. foreach(flag ${common_flags} ${c_flags})
  62. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}")
  63. endforeach()
  64. foreach(flag ${common_flags} ${cxx_flags})
  65. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
  66. endforeach()
  67. endif()
  68. if(APPLE)
  69. # CMake has no special support for Objective-C as a distinct language but
  70. # enabling modules and other clang extensions would apply even to regular C++
  71. # sources which is nonportable. Keep these flags separate to avoid misuse.
  72. set(
  73. OBJC_FLAGS
  74. -Werror=deprecated-objc-isa-usage
  75. -Werror=non-modular-include-in-framework-module
  76. -Werror=objc-root-class
  77. -Wblock-capture-autoreleasing
  78. -Wimplicit-atomic-properties
  79. -Wnon-modular-include-in-framework-module
  80. -fobjc-arc
  81. -fmodules
  82. -fno-autolink
  83. -F${FIREBASE_INSTALL_DIR}/Frameworks
  84. )
  85. endif()
  86. if(MSVC)
  87. set(
  88. common_flags
  89. # Cut down on symbol cruft in windows.h
  90. /DWIN32_LEAN_AND_MEAN=1
  91. /DNOMINMAX=1
  92. # Specify at least Windows Vista/Server 2008 (required by gRPC)
  93. /D_WIN32_WINNT=0x600
  94. # Disable warnings that can't be easily addressed or are ignored by
  95. # upstream projects.
  96. # unary minus operator applied to unsigned type, result still unsigned
  97. /wd4146
  98. )
  99. endif()
  100. foreach(flag ${common_flags} ${c_flags})
  101. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}")
  102. endforeach()
  103. foreach(flag ${common_flags} ${cxx_flags})
  104. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
  105. endforeach()