CompilerFlags.cmake 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. ###########################################################################
  2. #
  3. # Copyright 2016 Realm Inc.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. ###########################################################################
  18. include(CheckSymbolExists)
  19. set(CMAKE_CXX_STANDARD 14)
  20. set(CMAKE_CXX_STANDARD_REQUIRED on)
  21. set(CMAKE_CXX_EXTENSIONS off)
  22. set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS
  23. $<$<CONFIG:DEBUG>:REALM_DEBUG>
  24. $<$<CONFIG:COVERAGE>:REALM_DEBUG>
  25. )
  26. if(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang" OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
  27. add_compile_options(
  28. -Wall
  29. -Wextra
  30. -Wno-missing-field-initializers
  31. -Wno-unevaluated-expression
  32. -Wempty-body
  33. -Wparentheses
  34. -Wunknown-pragmas
  35. -Wunreachable-code
  36. -DREALM_HAVE_CONFIG
  37. )
  38. endif()
  39. if(MSVC)
  40. add_definitions(
  41. /D_UNICODE
  42. /DWIN32_LEAN_AND_MEAN
  43. /D_CRT_SECURE_NO_WARNINGS
  44. /D_SCL_SECURE_NO_WARNINGS
  45. /D_ENABLE_EXTENDED_ALIGNED_STORAGE #https://developercommunity.visualstudio.com/comments/279328/view.html
  46. )
  47. add_compile_options(
  48. /MP # Enable multi-processor compilation
  49. )
  50. if(NOT WINDOWS_STORE)
  51. # Statically link the run-time library
  52. # https://docs.microsoft.com/bg-bg/cpp/build/reference/md-mt-ld-use-run-time-library
  53. # https://cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F
  54. foreach(flag_var
  55. CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
  56. CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
  57. if(${flag_var} MATCHES "/MD")
  58. string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
  59. endif()
  60. endforeach()
  61. endif()
  62. endif()
  63. if(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
  64. add_compile_options(
  65. -Wassign-enum
  66. -Wbool-conversion
  67. -Wconditional-uninitialized
  68. -Wconstant-conversion
  69. -Wenum-conversion
  70. -Wimplicit-fallthrough
  71. -Wint-conversion
  72. -Wmissing-prototypes
  73. -Wnewline-eof
  74. -Wshorten-64-to-32
  75. -Wthread-safety
  76. -Wthread-safety-negative
  77. )
  78. endif()
  79. if(${CMAKE_GENERATOR} STREQUAL "Ninja")
  80. if(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
  81. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fcolor-diagnostics")
  82. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fcolor-diagnostics")
  83. elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
  84. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fdiagnostics-color=always")
  85. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdiagnostics-color=always")
  86. endif()
  87. endif()
  88. if(APPLE)
  89. find_library(CF_LIBRARY CoreFoundation)
  90. list(APPEND PLATFORM_LIBRARIES ${CF_LIBRARY})
  91. elseif(REALM_PLATFORM STREQUAL "Android")
  92. find_library(ANDROID_LIBRARY android)
  93. find_library(ANDROID_LOG_LIBRARY log)
  94. list(APPEND PLATFORM_LIBRARIES ${ANDROID_LIBRARY})
  95. list(APPEND PLATFORM_LIBRARIES ${ANDROID_LOG_LIBRARY})
  96. set(PLATFORM_DEFINES "__STDC_CONSTANT_MACROS=1")
  97. endif()
  98. if(NOT REALM_PLATFORM OR REALM_PLATFORM STREQUAL "Node")
  99. find_library(UV_LIBRARY NAMES uv libuv)
  100. if(UV_LIBRARY)
  101. find_path(UV_INCLUDE_DIR uv.h)
  102. list(APPEND PLATFORM_LIBRARIES ${UV_LIBRARY})
  103. add_definitions(-DREALM_HAVE_UV)
  104. endif()
  105. endif()
  106. if(REALM_PLATFORM STREQUAL "Node")
  107. set(PLATFORM_DEFINES "REALM_PLATFORM_NODE=1")
  108. if(NOT UV_LIBRARY)
  109. message(FATAL_ERROR "Platform set to Node but libuv was not found!")
  110. endif()
  111. endif()
  112. check_symbol_exists(epoll_create sys/epoll.h REALM_HAVE_EPOLL)
  113. if(REALM_HAVE_EPOLL)
  114. add_definitions(-DREALM_HAVE_EPOLL)
  115. endif()