CMakeLists.txt 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. # ----------------------------------------------------------------------------
  2. # Root CMake file for nanoflann
  3. # ----------------------------------------------------------------------------
  4. cmake_minimum_required(VERSION 3.5)
  5. # Extract library version into "NANOFLANN_VERSION"
  6. # -----------------------------------------------------
  7. # Look for: "#define NANOFLANN_VERSION 0xABC"
  8. file(READ "${CMAKE_CURRENT_SOURCE_DIR}/include/nanoflann.hpp" STR_HPP)
  9. string(REGEX MATCHALL "NANOFLANN_VERSION.*0x[0-9,A-F]+" CMAKE_VERSION_LINE "${STR_HPP}")
  10. string(REGEX MATCHALL "0x[0-9,A-F]+" NANOFLANN_VERSION_HEX "${CMAKE_VERSION_LINE}")
  11. string(REGEX REPLACE "0x(.).*" "\\1" NANOFLANN_VERSION_MAJOR "${NANOFLANN_VERSION_HEX}" )
  12. string(REGEX REPLACE "0x.(.).*" "\\1" NANOFLANN_VERSION_MINOR "${NANOFLANN_VERSION_HEX}" )
  13. string(REGEX REPLACE "0x..(.).*" "\\1" NANOFLANN_VERSION_PATCH "${NANOFLANN_VERSION_HEX}" )
  14. mark_as_advanced(STR_HPP CMAKE_VERSION_LINE NANOFLANN_VERSION_HEX NANOFLANN_VERSION_MAJOR NANOFLANN_VERSION_MINOR NANOFLANN_VERSION_PATCH)
  15. project(nanoflann VERSION "${NANOFLANN_VERSION_MAJOR}.${NANOFLANN_VERSION_MINOR}.${NANOFLANN_VERSION_PATCH}")
  16. message(STATUS "nanoflann version: ${NANOFLANN_VERSION_MAJOR}.${NANOFLANN_VERSION_MINOR}.${NANOFLANN_VERSION_PATCH}")
  17. file(WRITE "${nanoflann_BINARY_DIR}/version" "${NANOFLANN_VERSION_MAJOR}.${NANOFLANN_VERSION_MINOR}.${NANOFLANN_VERSION_PATCH}")
  18. # Enable a high level of warnings.
  19. if (CMAKE_COMPILER_IS_GNUCXX)
  20. # The -Wno-long-long is required in 64bit systems when including sytem headers.
  21. # The -Wno-variadic-macros was needed for Eigen3, StdVector.h
  22. add_compile_options(-Wall -Wshadow -Wno-long-long -Wno-variadic-macros)
  23. if (NOT "${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
  24. add_compile_options(-O2 -mtune=native)
  25. endif()
  26. # Workaround: Eigen <3.4 produces *tons* of warnings in GCC >=6. See http://eigen.tuxfamily.org/bz/show_bug.cgi?id=1221
  27. if (NOT ${CMAKE_CXX_COMPILER_VERSION} LESS "6.0")
  28. add_compile_options(-Wno-ignored-attributes -Wno-int-in-bool-context)
  29. endif()
  30. endif()
  31. if(MSVC)
  32. add_definitions( "/W3 /D_CRT_SECURE_NO_WARNINGS /nologo" )
  33. endif()
  34. # Solution Folder options:
  35. if (${CMAKE_SOURCE_DIR} STREQUAL ${PROJECT_SOURCE_DIR})
  36. set_property(GLOBAL PROPERTY USE_FOLDERS ON)
  37. set_property(GLOBAL PROPERTY PREDEFINED_TARGETS_FOLDER "CMakeTargets")
  38. endif()
  39. add_definitions ( -DNANOFLANN_PATH="${CMAKE_SOURCE_DIR}" )
  40. include(GNUInstallDirs)
  41. if ($ENV{VERBOSE})
  42. message(STATUS "CMAKE_INSTALL_INCLUDEDIR: ${CMAKE_INSTALL_INCLUDEDIR}")
  43. message(STATUS "CMAKE_INSTALL_DATADIR : ${CMAKE_INSTALL_DATADIR}")
  44. message(STATUS "CMAKE_INSTALL_LIBDIR : ${CMAKE_INSTALL_LIBDIR}")
  45. message(STATUS "CMAKE_INSTALL_DOCDIR : ${CMAKE_INSTALL_DOCDIR}")
  46. message(STATUS "CMAKE_INSTALL_PREFIX : ${CMAKE_INSTALL_PREFIX}")
  47. endif()
  48. # Set relative install directories
  49. set(INSTALL_INCLUDE_DIR "${CMAKE_INSTALL_INCLUDEDIR}")
  50. set(INSTALL_CMAKE_DIR "${CMAKE_INSTALL_DATADIR}/cmake/${PROJECT_NAME}")
  51. set(INSTALL_COPYRIGHT_DIR "${CMAKE_INSTALL_DOCDIR}")
  52. if(NOT DEFINED PKGCONFIG_INSTALL_DIR)
  53. set(PKGCONFIG_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
  54. endif()
  55. if (${CMAKE_CURRENT_SOURCE_DIR} STREQUAL ${CMAKE_SOURCE_DIR})
  56. # This is the root project
  57. set(MASTER_PROJECT ON)
  58. else()
  59. set(MASTER_PROJECT OFF)
  60. endif()
  61. if (MASTER_PROJECT)
  62. # Save all executables (unit tests & examples) in the same place:
  63. set(EXECUTABLE_OUTPUT_PATH ${${PROJECT_NAME}_BINARY_DIR}/bin CACHE PATH "Output directory for executables")
  64. endif()
  65. # Define nanoflann lib (header-only)
  66. add_library(nanoflann INTERFACE)
  67. # Tell CMake which C++ features we need
  68. target_compile_features(nanoflann
  69. INTERFACE
  70. cxx_auto_type
  71. cxx_decltype
  72. cxx_deleted_functions
  73. )
  74. target_include_directories(nanoflann
  75. INTERFACE
  76. $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  77. $<INSTALL_INTERFACE:${INSTALL_INCLUDE_DIR}>)
  78. install(TARGETS nanoflann
  79. EXPORT nanoflannTargets)
  80. # Since 2023-March, the parallel KD tree construction feature
  81. # needs pthreads for gcc:
  82. find_package(Threads)
  83. target_link_libraries(nanoflann INTERFACE Threads::Threads)
  84. add_library(nanoflann::nanoflann ALIAS nanoflann)
  85. # Examples
  86. option(NANOFLANN_BUILD_EXAMPLES "Build examples" ON)
  87. if(NANOFLANN_BUILD_EXAMPLES)
  88. add_subdirectory(examples)
  89. endif()
  90. # Tests
  91. option(NANOFLANN_BUILD_TESTS "Build unit tests" ON)
  92. option(NANOFLANN_USE_SYSTEM_GTEST "Use system GTest dependency" OFF)
  93. if(NANOFLANN_BUILD_TESTS)
  94. enable_testing()
  95. add_subdirectory(tests)
  96. endif()
  97. # --------------------------------------------------------------------
  98. # Install/uninstall targets
  99. # --------------------------------------------------------------------
  100. # Variable for pkgconfig file:
  101. set(nanoflann_pkgconfig_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}")
  102. # Generate the pkg-config file:
  103. configure_file(
  104. "${nanoflann_SOURCE_DIR}/scripts/nanoflann.pc.in"
  105. "${nanoflann_BINARY_DIR}/nanoflann.pc" @ONLY IMMEDIATE )
  106. # Generate the cmake config and cmake config-version file:
  107. include(CMakePackageConfigHelpers)
  108. configure_package_config_file(
  109. "${nanoflann_SOURCE_DIR}/scripts/nanoflannConfig.cmake.in"
  110. "${nanoflann_BINARY_DIR}/nanoflannConfig.cmake"
  111. INSTALL_DESTINATION ${INSTALL_CMAKE_DIR}
  112. PATH_VARS INSTALL_INCLUDE_DIR)
  113. # Setting CMAKE_SIZEOF_VOID_P to the empty string has the same
  114. # effect as the ARCH_INDEPENDENT option of
  115. # write_basic_package_version_file(), but works with older CMake
  116. # versions before 3.14
  117. set(backup_of_CMAKE_SIZEOF_VOID_P "${CMAKE_SIZEOF_VOID_P}")
  118. set(CMAKE_SIZEOF_VOID_P "")
  119. write_basic_package_version_file(
  120. "${nanoflann_BINARY_DIR}/nanoflannConfigVersion.cmake"
  121. VERSION ${nanoflann_VERSION}
  122. COMPATIBILITY AnyNewerVersion)
  123. set(CMAKE_SIZEOF_VOID_P "${backup_of_CMAKE_SIZEOF_VOID_P}")
  124. # Uninstall target, for "make uninstall"
  125. configure_file(
  126. "${nanoflann_SOURCE_DIR}/scripts/cmake_uninstall.cmake.in"
  127. "${nanoflann_BINARY_DIR}/cmake_uninstall.cmake"
  128. @ONLY IMMEDIATE)
  129. option(MASTER_PROJECT_HAS_TARGET_UNINSTALL "uninstall target to master project CMakeLists.txt" OFF)
  130. if(NOT MASTER_PROJECT_HAS_TARGET_UNINSTALL OR NOT TARGET uninstall)
  131. add_custom_target(uninstall
  132. "${CMAKE_COMMAND}" -P "${nanoflann_BINARY_DIR}/cmake_uninstall.cmake")
  133. else()
  134. add_custom_target(nanoflann_uninstall
  135. "${CMAKE_COMMAND}" -P "${nanoflann_BINARY_DIR}/cmake_uninstall.cmake")
  136. add_dependencies(uninstall nanoflann_uninstall)
  137. endif()
  138. export(EXPORT nanoflannTargets
  139. NAMESPACE nanoflann::
  140. FILE "${nanoflann_BINARY_DIR}/nanoflannTargets.cmake")
  141. export(PACKAGE nanoflann)
  142. install(EXPORT nanoflannTargets
  143. NAMESPACE nanoflann::
  144. DESTINATION "${INSTALL_CMAKE_DIR}")
  145. install(
  146. FILES "${nanoflann_BINARY_DIR}/nanoflann.pc"
  147. DESTINATION "${PKGCONFIG_INSTALL_DIR}" )
  148. install(
  149. FILES "${nanoflann_BINARY_DIR}/nanoflannConfig.cmake"
  150. "${nanoflann_BINARY_DIR}/nanoflannConfigVersion.cmake"
  151. DESTINATION "${INSTALL_CMAKE_DIR}" )
  152. install(
  153. FILES "${nanoflann_SOURCE_DIR}/include/nanoflann.hpp"
  154. DESTINATION "${INSTALL_INCLUDE_DIR}" )