libigl.cmake 14 KB

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