compiler_setup.cmake 3.8 KB

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