libigl.cmake 15 KB

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