libigl.cmake 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. cmake_minimum_required(VERSION 3.1)
  2. ### Find packages to populate default options ###
  3. #
  4. # COMPONENTS should match subsequent calls
  5. find_package(CGAL COMPONENTS Core) # --> CGAL_FOUND
  6. find_package(Boost 1.48 COMPONENTS thread system) # --> BOOST_FOUND
  7. if(CGAL_FOUND AND BOOST_FOUND)
  8. set(CGAL_AND_BOOST_FOUND TRUE)
  9. endif()
  10. find_package(Matlab COMPONENTS MEX_COMPILER MX_LIBRARY ENG_LIBRARY) # --> Matlab_FOUND
  11. find_package(MOSEK) # --> MOSEK_FOUND
  12. find_package(OpenGL) # --> OPENGL_FOUND
  13. ### Available options ###
  14. option(LIBIGL_USE_STATIC_LIBRARY "Use libigl as static library" OFF)
  15. option(LIBIGL_WITH_ANTTWEAKBAR "Use AntTweakBar" OFF)
  16. option(LIBIGL_WITH_CGAL "Use CGAL" "${CGAL_AND_BOOST_FOUND}")
  17. option(LIBIGL_WITH_COMISO "Use CoMiso" ON)
  18. option(LIBIGL_WITH_CORK "Use Cork" OFF)
  19. option(LIBIGL_WITH_EMBREE "Use Embree" OFF)
  20. option(LIBIGL_WITH_LIM "Use LIM" ON)
  21. option(LIBIGL_WITH_MATLAB "Use Matlab" "${Matlab_FOUND}")
  22. option(LIBIGL_WITH_MOSEK "Use MOSEK" "${MOSEK_FOUND}")
  23. option(LIBIGL_WITH_NANOGUI "Use Nanogui menu" OFF)
  24. option(LIBIGL_WITH_OPENGL "Use OpenGL" "${OPENGL_FOUND}")
  25. option(LIBIGL_WITH_OPENGL_GLFW "Use GLFW" "${OPENGL_FOUND}")
  26. option(LIBIGL_WITH_OPENGL_GLFW_IMGUI "Use IMGUI" OFF)
  27. option(LIBIGL_WITH_PNG "Use PNG" ON)
  28. option(LIBIGL_WITH_TETGEN "Use Tetgen" ON)
  29. option(LIBIGL_WITH_TRIANGLE "Use Triangle" ON)
  30. option(LIBIGL_WITH_VIEWER "Use OpenGL viewer" "${OPENGL_FOUND}")
  31. option(LIBIGL_WITH_XML "Use XML" ON)
  32. option(LIBIGL_WITH_PYTHON "Use Python" OFF)
  33. if(LIBIGL_WITH_VIEWER AND (NOT LIBIGL_WITH_OPENGL_GLFW OR NOT LIBIGL_WITH_OPENGL) )
  34. message(FATAL_ERROR "LIBIGL_WITH_VIEWER=ON requires LIBIGL_WITH_OPENGL_GLFW=ON and LIBIGL_WITH_OPENGL=ON")
  35. endif()
  36. ################################################################################
  37. ### Configuration
  38. set(LIBIGL_ROOT "${CMAKE_CURRENT_LIST_DIR}/../..")
  39. set(LIBIGL_SOURCE_DIR "${LIBIGL_ROOT}/include")
  40. set(LIBIGL_EXTERNAL "${LIBIGL_ROOT}/external")
  41. # Dependencies are linked as INTERFACE targets unless libigl is compiled as a static library
  42. if(LIBIGL_USE_STATIC_LIBRARY)
  43. set(IGL_SCOPE PUBLIC)
  44. else()
  45. set(IGL_SCOPE INTERFACE)
  46. endif()
  47. ################################################################################
  48. ### IGL Common
  49. ################################################################################
  50. add_library(igl_common INTERFACE)
  51. target_include_directories(igl_common SYSTEM INTERFACE ${LIBIGL_SOURCE_DIR})
  52. if(LIBIGL_USE_STATIC_LIBRARY)
  53. target_compile_definitions(igl_common INTERFACE -DIGL_STATIC_LIBRARY)
  54. endif()
  55. # Transitive C++11 flags
  56. include(CXXFeatures)
  57. target_compile_features(igl_common INTERFACE ${CXX11_FEATURES})
  58. # Other compilation flags
  59. if(MSVC)
  60. # Enable parallel compilation for Visual Studio
  61. target_compile_options(igl_common INTERFACE /MP /bigobj)
  62. if(LIBIGL_WITH_CGAL)
  63. target_compile_options(igl_common INTERFACE "/MD$<$<CONFIG:Debug>:d>")
  64. endif()
  65. endif()
  66. if(BUILD_SHARED_LIBS)
  67. # Generate position independent code
  68. set_target_properties(igl_common PROPERTIES INTERFACE_POSITION_INDEPENDENT_CODE ON)
  69. endif()
  70. # Eigen
  71. if(TARGET Eigen3::Eigen)
  72. # If an imported target already exists, use it
  73. target_link_libraries(igl_common INTERFACE Eigen3::Eigen)
  74. else()
  75. target_include_directories(igl_common SYSTEM INTERFACE ${LIBIGL_EXTERNAL}/eigen)
  76. endif()
  77. # C++11 Thread library
  78. find_package(Threads REQUIRED)
  79. target_link_libraries(igl_common INTERFACE ${CMAKE_THREAD_LIBS_INIT})
  80. ################################################################################
  81. function(compile_igl_module module_dir)
  82. string(REPLACE "/" "_" module_name "${module_dir}")
  83. if(LIBIGL_USE_STATIC_LIBRARY)
  84. file(GLOB SOURCES_IGL_${module_name}
  85. "${LIBIGL_SOURCE_DIR}/igl/${module_dir}/*.cpp"
  86. "${LIBIGL_SOURCE_DIR}/igl/copyleft/${module_dir}/*.cpp")
  87. add_library(igl_${module_name} STATIC ${SOURCES_IGL_${module_name}} ${ARGN})
  88. if(MSVC)
  89. target_compile_options(igl_${module_name} PRIVATE /w) # disable all warnings (not ideal but...)
  90. else()
  91. #target_compile_options(igl_${module_name} PRIVATE -w) # disable all warnings (not ideal but...)
  92. endif()
  93. else()
  94. add_library(igl_${module_name} INTERFACE)
  95. endif()
  96. target_link_libraries(igl_${module_name} ${IGL_SCOPE} igl_common)
  97. if(NOT module_name STREQUAL "core")
  98. target_link_libraries(igl_${module_name} ${IGL_SCOPE} igl_core)
  99. endif()
  100. # Alias target because it looks nicer
  101. message(STATUS "Creating target: igl::${module_name}")
  102. add_library(igl::${module_name} ALIAS igl_${module_name})
  103. endfunction()
  104. ################################################################################
  105. ### IGL Core
  106. ################################################################################
  107. if(LIBIGL_USE_STATIC_LIBRARY)
  108. file(GLOB SOURCES_IGL
  109. "${LIBIGL_SOURCE_DIR}/igl/*.cpp"
  110. "${LIBIGL_SOURCE_DIR}/igl/copyleft/*.cpp")
  111. endif()
  112. compile_igl_module("core" ${SOURCES_IGL})
  113. ################################################################################
  114. ## Compile the AntTweakBar part ###
  115. if(LIBIGL_WITH_ANTTWEAKBAR)
  116. set(ANTTWEAKBAR_DIR "${LIBIGL_EXTERNAL}/AntTweakBar")
  117. if(NOT TARGET AntTweakBar)
  118. add_subdirectory("${ANTTWEAKBAR_DIR}" AntTweakBar)
  119. endif()
  120. compile_igl_module("anttweakbar")
  121. target_link_libraries(igl_anttweakbar ${IGL_SCOPE} AntTweakBar)
  122. endif()
  123. ################################################################################
  124. ### Compile the cgal parts ###
  125. if(LIBIGL_WITH_CGAL)
  126. # CGAL Core is needed for
  127. # `Exact_predicates_exact_constructions_kernel_with_sqrt`
  128. find_package(CGAL REQUIRED COMPONENTS Core)
  129. compile_igl_module("cgal")
  130. if(WIN32)
  131. set(Boost_USE_STATIC_LIBS ON) # Favor static Boost libs on Windows
  132. endif()
  133. target_include_directories(igl_cgal ${IGL_SCOPE} "${GMP_INCLUDE_DIR}" "${MPFR_INCLUDE_DIR}")
  134. find_package(Boost 1.48 REQUIRED thread system)
  135. target_include_directories(igl_cgal ${IGL_SCOPE} ${CGAL_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS})
  136. target_link_libraries(igl_cgal ${IGL_SCOPE} CGAL::CGAL CGAL::CGAL_Core ${Boost_LIBRARIES})
  137. endif()
  138. # Helper function for `igl_copy_cgal_dll()`
  139. function(igl_copy_imported_dll src_target dst_target)
  140. get_target_property(configurations ${src_target} IMPORTED_CONFIGURATIONS)
  141. foreach(config ${configurations})
  142. get_target_property(location ${src_target} IMPORTED_LOCATION_${config})
  143. if(EXISTS "${location}" AND location MATCHES "^.*\\.dll$")
  144. add_custom_command(TARGET ${dst_target} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different "${location}" $<TARGET_FILE_DIR:${dst_target}>)
  145. endif()
  146. endforeach()
  147. endfunction()
  148. # Convenient functions to copy CGAL dlls into a target (executable) destination folder (for Windows)
  149. function(igl_copy_cgal_dll target)
  150. if(WIN32 AND LIBIGL_WITH_CGAL)
  151. igl_copy_imported_dll(CGAL::CGAL ${target})
  152. igl_copy_imported_dll(CGAL::CGAL_Core ${target})
  153. endif()
  154. endfunction()
  155. ################################################################################
  156. # Compile CoMISo
  157. # NOTE: this cmakefile works only with the
  158. # comiso available here: https://github.com/libigl/CoMISo
  159. if(LIBIGL_WITH_COMISO)
  160. compile_igl_module("comiso")
  161. if(NOT TARGET CoMISo)
  162. add_subdirectory("${LIBIGL_EXTERNAL}/CoMISo" CoMISo)
  163. endif()
  164. target_link_libraries(igl_comiso ${IGL_SCOPE} CoMISo)
  165. endif()
  166. ################################################################################
  167. ### Compile the cork parts ###
  168. if(LIBIGL_WITH_CORK)
  169. set(CORK_DIR "${LIBIGL_EXTERNAL}/cork")
  170. if(NOT TARGET cork)
  171. # call this "lib-cork" instead of "cork", otherwise cmake gets confused about
  172. # "cork" executable
  173. add_subdirectory("${CORK_DIR}" "lib-cork")
  174. endif()
  175. compile_igl_module("cork")
  176. target_include_directories(igl_cork ${IGL_SCOPE} cork)
  177. target_include_directories(igl_cork ${IGL_SCOPE} "${CORK_DIR}/src")
  178. endif()
  179. ################################################################################
  180. ### Compile the embree part ###
  181. if(LIBIGL_WITH_EMBREE)
  182. set(EMBREE_DIR "${LIBIGL_EXTERNAL}/embree")
  183. set(EMBREE_ISPC_SUPPORT OFF CACHE BOOL " " FORCE)
  184. set(EMBREE_TASKING_SYSTEM "INTERNAL" CACHE BOOL " " FORCE)
  185. set(EMBREE_TUTORIALS OFF CACHE BOOL " " FORCE)
  186. set(EMBREE_MAX_ISA NONE CACHE STRINGS " " FORCE)
  187. # set(ENABLE_INSTALLER OFF CACHE BOOL " " FORCE)
  188. if(MSVC)
  189. # set(EMBREE_STATIC_RUNTIME OFF CACHE BOOL " " FORCE)
  190. set(EMBREE_STATIC_LIB OFF CACHE BOOL " " FORCE)
  191. else()
  192. set(EMBREE_STATIC_LIB ON CACHE BOOL " " FORCE)
  193. endif()
  194. if(NOT TARGET embree)
  195. add_subdirectory("${EMBREE_DIR}" "embree")
  196. endif()
  197. if(MSVC)
  198. add_custom_target(Copy-Embree-DLL ALL # Adds a post-build event
  199. COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E
  200. $<TARGET_FILE:embree> # <--this is in-file
  201. "${CMAKE_BINARY_DIR}" # <--this is out-file path
  202. DEPENDS embree) # Execute after embree target has been built
  203. endif()
  204. compile_igl_module("embree")
  205. target_link_libraries(igl_embree ${IGL_SCOPE} embree)
  206. target_include_directories(igl_embree ${IGL_SCOPE} ${EMBREE_DIR}/include)
  207. if(NOT MSVC)
  208. target_compile_definitions(igl_embree ${IGL_SCOPE} -DENABLE_STATIC_LIB)
  209. endif()
  210. endif()
  211. ################################################################################
  212. ### Compile the lim part ###
  213. if(LIBIGL_WITH_LIM)
  214. set(LIM_DIR "${LIBIGL_EXTERNAL}/lim")
  215. if(NOT TARGET lim)
  216. add_subdirectory("${LIM_DIR}" "lim")
  217. endif()
  218. compile_igl_module("lim")
  219. target_link_libraries(igl_lim ${IGL_SCOPE} lim)
  220. target_include_directories(igl_lim ${IGL_SCOPE} ${LIM_DIR})
  221. endif()
  222. ################################################################################
  223. ### Compile the matlab part ###
  224. if(LIBIGL_WITH_MATLAB)
  225. find_package(Matlab REQUIRED COMPONENTS MEX_COMPILER MX_LIBRARY ENG_LIBRARY)
  226. compile_igl_module("matlab")
  227. target_link_libraries(igl_matlab ${IGL_SCOPE} ${Matlab_LIBRARIES})
  228. target_include_directories(igl_matlab ${IGL_SCOPE} ${Matlab_INCLUDE_DIRS})
  229. endif()
  230. ################################################################################
  231. ### Compile the mosek part ###
  232. if(LIBIGL_WITH_MOSEK)
  233. find_package(MOSEK REQUIRED)
  234. compile_igl_module("mosek")
  235. target_link_libraries(igl_mosek ${IGL_SCOPE} ${MOSEK_LIBRARIES})
  236. target_include_directories(igl_mosek ${IGL_SCOPE} ${MOSEK_INCLUDE_DIRS})
  237. target_compile_definitions(igl_mosek ${IGL_SCOPE} -DLIBIGL_WITH_MOSEK)
  238. endif()
  239. ################################################################################
  240. ### Compile the opengl parts ###
  241. if(LIBIGL_WITH_OPENGL)
  242. # OpenGL module
  243. find_package(OpenGL REQUIRED)
  244. compile_igl_module("opengl")
  245. target_link_libraries(igl_opengl ${IGL_SCOPE} ${OPENGL_gl_LIBRARY})
  246. target_include_directories(igl_opengl SYSTEM ${IGL_SCOPE} ${OPENGL_INCLUDE_DIR})
  247. # glad module
  248. if(NOT TARGET glad)
  249. add_subdirectory(${LIBIGL_EXTERNAL}/glad glad)
  250. endif()
  251. target_link_libraries(igl_opengl ${IGL_SCOPE} glad)
  252. endif()
  253. ################################################################################
  254. ### Compile the GLFW part ###
  255. if(LIBIGL_WITH_OPENGL_GLFW)
  256. if(TARGET igl::opengl)
  257. # GLFW module
  258. compile_igl_module("opengl/glfw")
  259. if(NOT TARGET glfw)
  260. set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL " " FORCE)
  261. set(GLFW_BUILD_TESTS OFF CACHE BOOL " " FORCE)
  262. set(GLFW_BUILD_DOCS OFF CACHE BOOL " " FORCE)
  263. set(GLFW_INSTALL OFF CACHE BOOL " " FORCE)
  264. add_subdirectory(${LIBIGL_EXTERNAL}/glfw glfw)
  265. endif()
  266. target_link_libraries(igl_opengl_glfw ${IGL_SCOPE} igl_opengl glfw)
  267. endif()
  268. endif()
  269. ################################################################################
  270. ### Compile the ImGui part ###
  271. if(LIBIGL_WITH_OPENGL_GLFW_IMGUI)
  272. if(TARGET igl::opengl_glfw)
  273. # ImGui module
  274. compile_igl_module("opengl/glfw/imgui")
  275. if(NOT TARGET imgui)
  276. add_subdirectory(${LIBIGL_EXTERNAL}/imgui imgui)
  277. endif()
  278. target_link_libraries(igl_opengl_glfw_imgui ${IGL_SCOPE} igl_opengl_glfw imgui)
  279. endif()
  280. endif()
  281. ################################################################################
  282. ### Compile the png part ###
  283. if(LIBIGL_WITH_PNG)
  284. # png/ module is anomalous because it also depends on opengl it really should
  285. # be moved into the opengl/ directory and namespace ...
  286. if(TARGET igl_opengl)
  287. set(STB_IMAGE_DIR "${LIBIGL_EXTERNAL}/stb_image")
  288. if(NOT TARGET stb_image)
  289. add_subdirectory("${STB_IMAGE_DIR}" "stb_image")
  290. endif()
  291. compile_igl_module("png" "")
  292. target_link_libraries(igl_png ${IGL_SCOPE} igl_stb_image igl_opengl)
  293. endif()
  294. endif()
  295. ################################################################################
  296. ### Compile the tetgen part ###
  297. if(LIBIGL_WITH_TETGEN)
  298. set(TETGEN_DIR "${LIBIGL_EXTERNAL}/tetgen")
  299. if(NOT TARGET tetgen)
  300. add_subdirectory("${TETGEN_DIR}" "tetgen")
  301. endif()
  302. compile_igl_module("tetgen")
  303. target_link_libraries(igl_tetgen ${IGL_SCOPE} tetgen)
  304. target_include_directories(igl_tetgen ${IGL_SCOPE} ${TETGEN_DIR})
  305. endif()
  306. ################################################################################
  307. ### Compile the triangle part ###
  308. if(LIBIGL_WITH_TRIANGLE)
  309. set(TRIANGLE_DIR "${LIBIGL_EXTERNAL}/triangle")
  310. if(NOT TARGET triangle)
  311. add_subdirectory("${TRIANGLE_DIR}" "triangle")
  312. endif()
  313. compile_igl_module("triangle")
  314. target_link_libraries(igl_triangle ${IGL_SCOPE} triangle)
  315. target_include_directories(igl_triangle ${IGL_SCOPE} ${TRIANGLE_DIR})
  316. endif()
  317. ################################################################################
  318. ### Compile the xml part ###
  319. if(LIBIGL_WITH_XML)
  320. set(TINYXML2_DIR "${LIBIGL_EXTERNAL}/tinyxml2")
  321. if(NOT TARGET tinyxml2)
  322. add_library(tinyxml2 STATIC ${TINYXML2_DIR}/tinyxml2.cpp ${TINYXML2_DIR}/tinyxml2.h)
  323. target_include_directories(tinyxml2 PUBLIC ${TINYXML2_DIR})
  324. set_target_properties(tinyxml2 PROPERTIES
  325. COMPILE_DEFINITIONS "TINYXML2_EXPORT"
  326. VERSION "3.0.0"
  327. SOVERSION "3")
  328. endif()
  329. compile_igl_module("xml")
  330. target_link_libraries(igl_xml ${IGL_SCOPE} tinyxml2)
  331. endif()