compiler_setup.cmake 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # Copyright 2018 Google LLC
  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. include(CheckCXXCompilerFlag)
  15. # C++ Compiler setup
  16. # We use C++14
  17. set(CMAKE_CXX_STANDARD 14)
  18. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  19. set(CMAKE_CXX_EXTENSIONS OFF)
  20. if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  21. set(CXX_CLANG ON)
  22. endif()
  23. if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  24. set(CXX_GNU ON)
  25. endif()
  26. if(CMAKE_GENERATOR STREQUAL "Ninja")
  27. set(NINJA ON)
  28. endif()
  29. if(CXX_CLANG OR CXX_GNU)
  30. set(
  31. common_flags
  32. -Wall -Wextra -Werror
  33. # Be super pedantic about format strings
  34. -Wformat
  35. # Avoid use of uninitialized values
  36. -Wuninitialized
  37. -fno-common
  38. # Don't fail the build solely on account of using deprecated things.
  39. -Wno-error=deprecated-declarations
  40. # Delete unused things
  41. -Wunused-function -Wunused-value -Wunused-variable
  42. )
  43. # Disable "redundant-move" warning since it's not really a problem, and is even a valid coding
  44. # style to over-use std::move() in case the type is ever changed to become non-trivially moveable.
  45. CHECK_CXX_COMPILER_FLAG("-Wno-redundant-move" FIREBASE_CXX_COMPILER_FLAG_NO_REDUNDANT_MOVE_SUPPORTED)
  46. if(FIREBASE_CXX_COMPILER_FLAG_NO_REDUNDANT_MOVE_SUPPORTED)
  47. list(APPEND common_flags -Wno-redundant-move)
  48. endif()
  49. set(
  50. cxx_flags
  51. -Wreorder -Werror=reorder
  52. )
  53. set(
  54. c_flags
  55. -Wstrict-prototypes
  56. )
  57. if(CXX_CLANG)
  58. list(
  59. APPEND common_flags
  60. -Wconditional-uninitialized -Werror=return-type -Winfinite-recursion -Wmove
  61. -Wrange-loop-analysis -Wunreachable-code
  62. # Options added to match apple recommended project settings
  63. # TODO(wilhuff): re-enable -Wcomma once Abseil fixes the definition of
  64. # ABSL_ASSERT upstream
  65. -Wno-comma
  66. )
  67. endif()
  68. if(NINJA)
  69. # If building under Ninja, disable tty detection and force color output
  70. if(CXX_CLANG OR CXX_GNU)
  71. list(APPEND common_flags -fdiagnostics-color)
  72. endif()
  73. endif()
  74. endif()
  75. if(APPLE)
  76. # CMake has no special support for Objective-C as a distinct language but
  77. # enabling modules and other clang extensions would apply even to regular C++
  78. # sources which is nonportable. Keep these flags separate to avoid misuse.
  79. set(
  80. FIREBASE_IOS_OBJC_FLAGS
  81. -fobjc-arc
  82. -fno-autolink
  83. )
  84. set(
  85. FIREBASE_IOS_OBJC_FLAGS_STRICT
  86. ${FIREBASE_IOS_OBJC_FLAGS}
  87. -Werror=deprecated-objc-isa-usage
  88. -Werror=non-modular-include-in-framework-module
  89. -Werror=objc-root-class
  90. -Wblock-capture-autoreleasing
  91. -Wimplicit-atomic-properties
  92. -Wnon-modular-include-in-framework-module
  93. )
  94. endif()
  95. if(MSVC)
  96. set(
  97. common_flags
  98. # Cut down on symbol cruft in windows.h
  99. /DWIN32_LEAN_AND_MEAN=1
  100. /DNOMINMAX=1
  101. # Specify at least Windows Vista/Server 2008 (required by gRPC)
  102. /D_WIN32_WINNT=0x600
  103. # Disable warnings that can't be easily addressed or are ignored by
  104. # upstream projects.
  105. # unary minus operator applied to unsigned type, result still unsigned
  106. /wd4146
  107. # character cannot be represented in the current code page
  108. /wd4566
  109. )
  110. endif()
  111. foreach(flag ${common_flags} ${c_flags})
  112. list(APPEND FIREBASE_IOS_C_FLAGS_STRICT ${flag})
  113. endforeach()
  114. foreach(flag ${common_flags} ${cxx_flags})
  115. list(APPEND FIREBASE_IOS_CXX_FLAGS_STRICT ${flag})
  116. endforeach()
  117. if(APPLE)
  118. # When building on Apple platforms, ranlib complains about "file has no
  119. # symbols". Unfortunately, most of our dependencies implement their
  120. # cross-platform build with preprocessor symbols so translation units that
  121. # don't target the current platform end up empty (and trigger this warning).
  122. set(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> Scr <TARGET> <LINK_FLAGS> <OBJECTS>")
  123. set(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> Scr <TARGET> <LINK_FLAGS> <OBJECTS>")
  124. set(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
  125. set(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
  126. endif()