FindNLopt.cmake 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #///////////////////////////////////////////////////////////////////////////
  2. #//-------------------------------------------------------------------------
  3. #//
  4. #// Description:
  5. #// cmake module for finding NLopt installation
  6. #// NLopt installation location is defined by environment variable $NLOPT
  7. #//
  8. #// following variables are defined:
  9. #// NLopt_DIR - NLopt installation directory
  10. #// NLopt_INCLUDE_DIR - NLopt header directory
  11. #// NLopt_LIBRARY_DIR - NLopt library directory
  12. #// NLopt_LIBS - NLopt library files
  13. #//
  14. #// Example usage:
  15. #// find_package(NLopt 1.4 REQUIRED)
  16. #//
  17. #//
  18. #//-------------------------------------------------------------------------
  19. set(NLopt_FOUND FALSE)
  20. set(NLopt_ERROR_REASON "")
  21. set(NLopt_DEFINITIONS "")
  22. unset(NLopt_LIBS CACHE)
  23. set(NLopt_DIR $ENV{NLOPT})
  24. if(NOT NLopt_DIR)
  25. set(NLopt_FOUND TRUE)
  26. set(_NLopt_LIB_NAMES "nlopt")
  27. find_library(NLopt_LIBS
  28. NAMES ${_NLopt_LIB_NAMES})
  29. if(NOT NLopt_LIBS)
  30. set(NLopt_FOUND FALSE)
  31. set(NLopt_ERROR_REASON "${NLopt_ERROR_REASON} Cannot find NLopt library '${_NLopt_LIB_NAMES}'.")
  32. else()
  33. get_filename_component(NLopt_DIR ${NLopt_LIBS} PATH)
  34. endif()
  35. unset(_NLopt_LIB_NAMES)
  36. set(_NLopt_HEADER_FILE_NAME "nlopt.hpp")
  37. find_file(_NLopt_HEADER_FILE
  38. NAMES ${_NLopt_HEADER_FILE_NAME})
  39. if(NOT _NLopt_HEADER_FILE)
  40. set(NLopt_FOUND FALSE)
  41. set(NLopt_ERROR_REASON "${NLopt_ERROR_REASON} Cannot find NLopt header file '${_NLopt_HEADER_FILE_NAME}'.")
  42. endif()
  43. unset(_NLopt_HEADER_FILE_NAME)
  44. if(NOT NLopt_FOUND)
  45. set(NLopt_ERROR_REASON "${NLopt_ERROR_REASON} NLopt not found in system directories (and environment variable NLOPT is not set).")
  46. else()
  47. get_filename_component(NLopt_INCLUDE_DIR ${_NLopt_HEADER_FILE} DIRECTORY )
  48. endif()
  49. unset(_NLopt_HEADER_FILE CACHE)
  50. else()
  51. set(NLopt_FOUND TRUE)
  52. set(NLopt_INCLUDE_DIR "${NLopt_DIR}/include")
  53. if(NOT EXISTS "${NLopt_INCLUDE_DIR}")
  54. set(NLopt_FOUND FALSE)
  55. set(NLopt_ERROR_REASON "${NLopt_ERROR_REASON} Directory '${NLopt_INCLUDE_DIR}' does not exist.")
  56. endif()
  57. set(NLopt_LIBRARY_DIR "${NLopt_DIR}/lib")
  58. if(NOT EXISTS "${NLopt_LIBRARY_DIR}")
  59. set(NLopt_FOUND FALSE)
  60. set(NLopt_ERROR_REASON "${NLopt_ERROR_REASON} Directory '${NLopt_LIBRARY_DIR}' does not exist.")
  61. endif()
  62. set(_NLopt_LIB_NAMES "nlopt_cxx")
  63. find_library(NLopt_LIBS
  64. NAMES ${_NLopt_LIB_NAMES}
  65. PATHS ${NLopt_LIBRARY_DIR}
  66. NO_DEFAULT_PATH)
  67. if(NOT NLopt_LIBS)
  68. set(NLopt_FOUND FALSE)
  69. set(NLopt_ERROR_REASON "${NLopt_ERROR_REASON} Cannot find NLopt library '${_NLopt_LIB_NAMES}' in '${NLopt_LIBRARY_DIR}'.")
  70. endif()
  71. unset(_NLopt_LIB_NAMES)
  72. set(_NLopt_HEADER_FILE_NAME "nlopt.hpp")
  73. find_file(_NLopt_HEADER_FILE
  74. NAMES ${_NLopt_HEADER_FILE_NAME}
  75. PATHS ${NLopt_INCLUDE_DIR}
  76. NO_DEFAULT_PATH)
  77. if(NOT _NLopt_HEADER_FILE)
  78. set(NLopt_FOUND FALSE)
  79. set(NLopt_ERROR_REASON "${NLopt_ERROR_REASON} Cannot find NLopt header file '${_NLopt_HEADER_FILE_NAME}' in '${NLopt_INCLUDE_DIR}'.")
  80. endif()
  81. unset(_NLopt_HEADER_FILE_NAME)
  82. unset(_NLopt_HEADER_FILE CACHE)
  83. endif()
  84. # make variables changeable
  85. mark_as_advanced(
  86. NLopt_INCLUDE_DIR
  87. NLopt_LIBRARY_DIR
  88. NLopt_LIBS
  89. NLopt_DEFINITIONS
  90. )
  91. # report result
  92. if(NLopt_FOUND)
  93. message(STATUS "Found NLopt in '${NLopt_DIR}'.")
  94. message(STATUS "Using NLopt include directory '${NLopt_INCLUDE_DIR}'.")
  95. message(STATUS "Using NLopt library '${NLopt_LIBS}'.")
  96. add_library(NLopt::nlopt INTERFACE IMPORTED)
  97. set_target_properties(NLopt::nlopt PROPERTIES INTERFACE_LINK_LIBRARIES ${NLopt_LIBS})
  98. set_target_properties(NLopt::nlopt PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${NLopt_INCLUDE_DIR})
  99. set_target_properties(NLopt::nlopt PROPERTIES INTERFACE_COMPILE_DEFINITIONS "${NLopt_DEFINITIONS}")
  100. # target_link_libraries(Nlopt::Nlopt INTERFACE ${NLopt_LIBS})
  101. # target_include_directories(Nlopt::Nlopt INTERFACE ${NLopt_INCLUDE_DIR})
  102. # target_compile_definitions(Nlopt::Nlopt INTERFACE ${NLopt_DEFINITIONS})
  103. else()
  104. if(NLopt_FIND_REQUIRED)
  105. message(FATAL_ERROR "Unable to find requested NLopt installation:${NLopt_ERROR_REASON}")
  106. else()
  107. if(NOT NLopt_FIND_QUIETLY)
  108. message(STATUS "NLopt was not found:${NLopt_ERROR_REASON}")
  109. endif()
  110. endif()
  111. endif()