CMakeLists.txt 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #
  2. # EnTT
  3. #
  4. cmake_minimum_required(VERSION 3.7.2)
  5. #
  6. # Building in-tree is not allowed (we take care of your craziness).
  7. #
  8. if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
  9. message(FATAL_ERROR "Prevented in-tree built. Please create a build directory outside of the source code and call cmake from there. Thank you.")
  10. endif()
  11. #
  12. # Project configuration
  13. #
  14. project(EnTT VERSION 2.7.1)
  15. include(GNUInstallDirs)
  16. if(NOT CMAKE_BUILD_TYPE)
  17. set(CMAKE_BUILD_TYPE Debug)
  18. endif()
  19. set(SETTINGS_ORGANIZATION "Michele Caini")
  20. set(SETTINGS_APPLICATION ${PROJECT_NAME})
  21. set(PROJECT_AUTHOR "Michele Caini")
  22. set(PROJECT_AUTHOR_EMAIL "michele.caini@gmail.com")
  23. message("*")
  24. message("* ${PROJECT_NAME} v${PROJECT_VERSION} (${CMAKE_BUILD_TYPE})")
  25. message("* Copyright (c) 2018 ${PROJECT_AUTHOR} <${PROJECT_AUTHOR_EMAIL}>")
  26. message("*")
  27. option(USE_LIBCPP "Use libc++ by adding -stdlib=libc++ flag if availbale." ON)
  28. option(USE_ASAN "Use address sanitizer by adding -fsanitize=address -fno-omit-frame-pointer flags" OFF)
  29. option(USE_COMPILE_OPTIONS "Use compile options from EnTT." ON)
  30. #
  31. # Compiler stuff
  32. #
  33. if(NOT MSVC AND USE_LIBCPP)
  34. include(CheckCXXSourceCompiles)
  35. include(CMakePushCheckState)
  36. cmake_push_check_state()
  37. set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -stdlib=libc++")
  38. check_cxx_source_compiles("
  39. #include<type_traits>
  40. int main() { return std::is_same<int, int>::value ? 0 : 1; }
  41. " HAS_LIBCPP)
  42. if(NOT HAS_LIBCPP)
  43. message(WARNING "The option USE_LIBCPP is set (by default) but libc++ is not available. The flag will not be added to the target.")
  44. endif()
  45. cmake_pop_check_state()
  46. endif()
  47. #
  48. # Add EnTT target
  49. #
  50. add_library(EnTT INTERFACE)
  51. target_include_directories(
  52. EnTT INTERFACE
  53. $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>
  54. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
  55. )
  56. target_compile_definitions(
  57. EnTT
  58. INTERFACE $<$<AND:$<CONFIG:Debug>,$<NOT:$<CXX_COMPILER_ID:MSVC>>>:DEBUG>
  59. INTERFACE $<$<AND:$<CONFIG:Release>,$<NOT:$<CXX_COMPILER_ID:MSVC>>>:RELEASE>
  60. )
  61. if(USE_ASAN)
  62. target_compile_options(EnTT INTERFACE $<$<AND:$<CONFIG:Debug>,$<NOT:$<CXX_COMPILER_ID:MSVC>>>:-fsanitize=address -fno-omit-frame-pointer>)
  63. target_link_libraries(EnTT INTERFACE $<$<AND:$<CONFIG:Debug>,$<NOT:$<CXX_COMPILER_ID:MSVC>>>:-fsanitize=address -fno-omit-frame-pointer>)
  64. endif()
  65. if(USE_COMPILE_OPTIONS)
  66. target_compile_options(
  67. EnTT
  68. INTERFACE $<$<AND:$<CONFIG:Debug>,$<NOT:$<CXX_COMPILER_ID:MSVC>>>:-O0 -g>
  69. # it seems that -O3 ruins a bit the performance when using clang ...
  70. INTERFACE $<$<AND:$<CONFIG:Release>,$<CXX_COMPILER_ID:Clang>>:-O2>
  71. # ... on the other side, GCC is incredibly comfortable with it.
  72. INTERFACE $<$<AND:$<CONFIG:Release>,$<CXX_COMPILER_ID:GNU>>:-O3>
  73. )
  74. endif()
  75. if(HAS_LIBCPP)
  76. target_compile_options(EnTT BEFORE INTERFACE -stdlib=libc++)
  77. endif()
  78. target_compile_features(EnTT INTERFACE cxx_std_14)
  79. #
  80. # Install EnTT
  81. #
  82. if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
  83. set(CUSTOM_INSTALL_CONFIGDIR cmake)
  84. else()
  85. set(CUSTOM_INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/entt)
  86. endif()
  87. install(DIRECTORY src/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
  88. install(TARGETS EnTT EXPORT EnTTTargets)
  89. export(EXPORT EnTTTargets FILE ${EnTT_BINARY_DIR}/EnTTTargets.cmake)
  90. install(
  91. EXPORT EnTTTargets
  92. FILE EnTTTargets.cmake
  93. DESTINATION ${CUSTOM_INSTALL_CONFIGDIR}
  94. )
  95. #
  96. # Build tree package config file
  97. #
  98. configure_file(cmake/in/EnTTBuildConfig.cmake.in EnTTConfig.cmake @ONLY)
  99. include(CMakePackageConfigHelpers)
  100. #
  101. # Install tree package config file
  102. #
  103. configure_package_config_file(
  104. cmake/in/EnTTConfig.cmake.in
  105. ${CUSTOM_INSTALL_CONFIGDIR}/EnTTConfig.cmake
  106. INSTALL_DESTINATION ${CUSTOM_INSTALL_CONFIGDIR}
  107. PATH_VARS CMAKE_INSTALL_INCLUDEDIR
  108. NO_CHECK_REQUIRED_COMPONENTS_MACRO
  109. )
  110. write_basic_package_version_file(
  111. ${EnTT_BINARY_DIR}/EnTTConfigVersion.cmake
  112. VERSION ${PROJECT_VERSION}
  113. COMPATIBILITY AnyNewerVersion
  114. )
  115. install(
  116. FILES
  117. ${EnTT_BINARY_DIR}/${CUSTOM_INSTALL_CONFIGDIR}/EnTTConfig.cmake
  118. ${EnTT_BINARY_DIR}/EnTTConfigVersion.cmake
  119. DESTINATION ${CUSTOM_INSTALL_CONFIGDIR}
  120. )
  121. export(PACKAGE EnTT)
  122. #
  123. # Tests
  124. #
  125. option(BUILD_TESTING "Enable testing with ctest." ON)
  126. if(BUILD_TESTING)
  127. set(THREADS_PREFER_PTHREAD_FLAG ON)
  128. find_package(Threads REQUIRED)
  129. option(FIND_GTEST_PACKAGE "Enable finding gtest package." OFF)
  130. if(FIND_GTEST_PACKAGE)
  131. find_package(GTest REQUIRED)
  132. else()
  133. # gtest, gtest_main, gmock and gmock_main targets are available from now on
  134. set(GOOGLETEST_DEPS_DIR ${EnTT_SOURCE_DIR}/deps/googletest)
  135. configure_file(${EnTT_SOURCE_DIR}/cmake/in/googletest.in ${GOOGLETEST_DEPS_DIR}/CMakeLists.txt)
  136. execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . WORKING_DIRECTORY ${GOOGLETEST_DEPS_DIR})
  137. execute_process(COMMAND ${CMAKE_COMMAND} --build . WORKING_DIRECTORY ${GOOGLETEST_DEPS_DIR})
  138. set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
  139. add_subdirectory(${GOOGLETEST_DEPS_DIR}/src ${GOOGLETEST_DEPS_DIR}/build)
  140. add_library(GTest::Main ALIAS gtest_main)
  141. endif()
  142. option(BUILD_BENCHMARK "Build benchmark." OFF)
  143. option(BUILD_MOD "Build mod example." OFF)
  144. option(BUILD_SNAPSHOT "Build snapshot example." OFF)
  145. enable_testing()
  146. add_subdirectory(test)
  147. endif()
  148. #
  149. # Documentation
  150. #
  151. option(BUILD_DOCS "Enable building with documentation." OFF)
  152. if(BUILD_DOCS)
  153. find_package(Doxygen 1.8)
  154. if(DOXYGEN_FOUND)
  155. add_subdirectory(docs)
  156. endif()
  157. endif()
  158. #
  159. # AOB
  160. #
  161. add_custom_target(
  162. entt_aob
  163. SOURCES
  164. appveyor.yml
  165. AUTHORS
  166. LICENSE
  167. README.md
  168. TODO
  169. .travis.yml
  170. docs/CONTRIBUTING.md
  171. docs/extra.dox
  172. )