libigl.cmake 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. # set(BOOST_ROOT "${LIBIGL_EXTERNAL}/boost")
  113. find_package(CGAL COMPONENTS Core)
  114. if(CGAL_FOUND)
  115. compile_igl_module("cgal")
  116. if(WIN32)
  117. set(Boost_USE_STATIC_LIBS ON) # Favor static Boost libs on Windows
  118. endif()
  119. target_include_directories(igl_cgal ${IGL_SCOPE} "${GMP_INCLUDE_DIR}" "${MPFR_INCLUDE_DIR}")
  120. find_package(Boost 1.48 REQUIRED thread system)
  121. target_include_directories(igl_cgal ${IGL_SCOPE} ${CGAL_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS})
  122. target_link_libraries(igl_cgal ${IGL_SCOPE} CGAL::CGAL CGAL::CGAL_Core ${Boost_LIBRARIES})
  123. else()
  124. set(LIBIGL_WITH_CGAL OFF CACHE BOOL "" FORCE)
  125. endif()
  126. endif()
  127. # Helper function for `igl_copy_cgal_dll()`
  128. function(igl_copy_imported_dll src_target dst_target)
  129. get_target_property(configurations ${src_target} IMPORTED_CONFIGURATIONS)
  130. foreach(config ${configurations})
  131. get_target_property(main_lib ${src_target} IMPORTED_LOCATION_${config})
  132. get_target_property(other_libs ${src_target} IMPORTED_LINK_INTERFACE_LIBRARIES_${config})
  133. set(locations)
  134. list(APPEND locations ${main_lib} ${other_libs})
  135. foreach(location ${locations})
  136. string(REGEX MATCH "^(.*)\\.[^.]*$" dummy ${location})
  137. set(location "${CMAKE_MATCH_1}.dll")
  138. if(EXISTS "${location}" AND location MATCHES "^.*\\.dll$")
  139. add_custom_command(TARGET ${dst_target} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different "${location}" $<TARGET_FILE_DIR:${dst_target}>)
  140. endif()
  141. endforeach()
  142. endforeach()
  143. endfunction()
  144. # Convenient functions to copy CGAL dlls into a target (executable) destination folder (for Windows)
  145. function(igl_copy_cgal_dll target)
  146. if(WIN32 AND LIBIGL_WITH_CGAL)
  147. igl_copy_imported_dll(CGAL::CGAL ${target})
  148. igl_copy_imported_dll(CGAL::CGAL_Core ${target})
  149. endif()
  150. endfunction()
  151. ################################################################################
  152. # Compile CoMISo
  153. # NOTE: this cmakefile works only with the
  154. # comiso available here: https://github.com/libigl/CoMISo
  155. if(LIBIGL_WITH_COMISO)
  156. compile_igl_module("comiso")
  157. if(NOT TARGET CoMISo)
  158. add_subdirectory("${LIBIGL_EXTERNAL}/CoMISo" CoMISo)
  159. endif()
  160. target_link_libraries(igl_comiso ${IGL_SCOPE} CoMISo)
  161. endif()
  162. ################################################################################
  163. ### Compile the cork parts ###
  164. if(LIBIGL_WITH_CORK)
  165. set(CORK_DIR "${LIBIGL_EXTERNAL}/cork")
  166. if(NOT TARGET cork)
  167. # call this "lib-cork" instead of "cork", otherwise cmake gets confused about
  168. # "cork" executable
  169. add_subdirectory("${CORK_DIR}" "lib-cork")
  170. endif()
  171. compile_igl_module("cork")
  172. target_include_directories(igl_cork ${IGL_SCOPE} cork)
  173. target_include_directories(igl_cork ${IGL_SCOPE} "${CORK_DIR}/src")
  174. endif()
  175. ################################################################################
  176. ### Compile the embree part ###
  177. if(LIBIGL_WITH_EMBREE)
  178. set(EMBREE_DIR "${LIBIGL_EXTERNAL}/embree")
  179. set(EMBREE_ISPC_SUPPORT OFF CACHE BOOL " " FORCE)
  180. set(EMBREE_TASKING_SYSTEM "INTERNAL" CACHE BOOL " " FORCE)
  181. set(EMBREE_TUTORIALS OFF CACHE BOOL " " FORCE)
  182. set(EMBREE_MAX_ISA NONE CACHE STRINGS " " FORCE)
  183. # set(ENABLE_INSTALLER OFF CACHE BOOL " " FORCE)
  184. if(MSVC)
  185. # set(EMBREE_STATIC_RUNTIME OFF CACHE BOOL " " FORCE)
  186. set(EMBREE_STATIC_LIB OFF CACHE BOOL " " FORCE)
  187. else()
  188. set(EMBREE_STATIC_LIB ON CACHE BOOL " " FORCE)
  189. endif()
  190. if(NOT TARGET embree)
  191. add_subdirectory("${EMBREE_DIR}" "embree")
  192. endif()
  193. if(MSVC)
  194. add_custom_target(Copy-Embree-DLL ALL # Adds a post-build event
  195. COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E
  196. $<TARGET_FILE:embree> # <--this is in-file
  197. "${CMAKE_BINARY_DIR}" # <--this is out-file path
  198. DEPENDS embree) # Execute after embree target has been built
  199. endif()
  200. compile_igl_module("embree")
  201. target_link_libraries(igl_embree ${IGL_SCOPE} embree)
  202. target_include_directories(igl_embree ${IGL_SCOPE} ${EMBREE_DIR}/include)
  203. if(NOT MSVC)
  204. target_compile_definitions(igl_embree ${IGL_SCOPE} -DENABLE_STATIC_LIB)
  205. endif()
  206. endif()
  207. ################################################################################
  208. ### Compile the lim part ###
  209. if(LIBIGL_WITH_LIM)
  210. set(LIM_DIR "${LIBIGL_EXTERNAL}/lim")
  211. if(NOT TARGET lim)
  212. add_subdirectory("${LIM_DIR}" "lim")
  213. endif()
  214. compile_igl_module("lim")
  215. target_link_libraries(igl_lim ${IGL_SCOPE} lim)
  216. target_include_directories(igl_lim ${IGL_SCOPE} ${LIM_DIR})
  217. endif()
  218. ################################################################################
  219. ### Compile the matlab part ###
  220. if(LIBIGL_WITH_MATLAB)
  221. find_package(Matlab)
  222. if(MATLAB_FOUND)
  223. compile_igl_module("matlab")
  224. target_link_libraries(igl_matlab ${IGL_SCOPE} ${MATLAB_LIBRARIES})
  225. target_include_directories(igl_matlab ${IGL_SCOPE} ${MATLAB_INCLUDE_DIR})
  226. else()
  227. set(LIBIGL_WITH_MATLAB OFF CACHE BOOL "" FORCE)
  228. endif()
  229. endif()
  230. ################################################################################
  231. ### Compile the mosek part ###
  232. if(LIBIGL_WITH_MOSEK)
  233. find_package(MOSEK)
  234. if(MOSEK_FOUND)
  235. compile_igl_module("mosek")
  236. target_link_libraries(igl_mosek ${IGL_SCOPE} ${MOSEK_LIBRARIES})
  237. target_include_directories(igl_mosek ${IGL_SCOPE} ${MOSEK_INCLUDE_DIRS})
  238. target_compile_definitions(igl_mosek ${IGL_SCOPE} -DLIBIGL_WITH_MOSEK)
  239. else()
  240. set(LIBIGL_WITH_MOSEK OFF CACHE BOOL "" FORCE)
  241. endif()
  242. endif()
  243. ################################################################################
  244. ### Compile the opengl parts ###
  245. if(LIBIGL_WITH_OPENGL)
  246. # OpenGL module
  247. find_package(OpenGL REQUIRED)
  248. compile_igl_module("opengl")
  249. target_link_libraries(igl_opengl ${IGL_SCOPE} ${OPENGL_gl_LIBRARY})
  250. target_include_directories(igl_opengl SYSTEM ${IGL_SCOPE} ${OPENGL_INCLUDE_DIR})
  251. # glad module
  252. if(NOT TARGET glad)
  253. add_subdirectory(${LIBIGL_EXTERNAL}/glad glad)
  254. endif()
  255. target_link_libraries(igl_opengl ${IGL_SCOPE} glad)
  256. endif()
  257. ################################################################################
  258. ### Compile the GLFW part ###
  259. if(LIBIGL_WITH_OPENGL_GLFW)
  260. if(TARGET igl::opengl)
  261. # GLFW module
  262. compile_igl_module("opengl/glfw")
  263. if(NOT TARGET glfw)
  264. set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL " " FORCE)
  265. set(GLFW_BUILD_TESTS OFF CACHE BOOL " " FORCE)
  266. set(GLFW_BUILD_DOCS OFF CACHE BOOL " " FORCE)
  267. set(GLFW_INSTALL OFF CACHE BOOL " " FORCE)
  268. add_subdirectory(${LIBIGL_EXTERNAL}/glfw glfw)
  269. endif()
  270. target_link_libraries(igl_opengl_glfw ${IGL_SCOPE} igl_opengl glfw)
  271. else()
  272. message(WARNING "GLFW module could not be compiled")
  273. set(LIBIGL_WITH_OPENGL_GLFW OFF CACHE BOOL "" FORCE)
  274. endif()
  275. endif()
  276. ################################################################################
  277. ### Compile the ImGui part ###
  278. if(LIBIGL_WITH_OPENGL_GLFW_IMGUI)
  279. if(TARGET igl::opengl_glfw)
  280. # ImGui module
  281. compile_igl_module("opengl/glfw/imgui")
  282. if(NOT TARGET imgui)
  283. add_subdirectory(${LIBIGL_EXTERNAL}/imgui imgui)
  284. endif()
  285. target_link_libraries(igl_opengl_glfw_imgui ${IGL_SCOPE} igl_opengl_glfw imgui)
  286. else()
  287. message(WARNING "ImGui module could not be compiled")
  288. set(LIBIGL_WITH_OPENGL_GLFW_IMGUI OFF CACHE BOOL "" FORCE)
  289. endif()
  290. endif()
  291. ################################################################################
  292. ### Compile the png part ###
  293. if(LIBIGL_WITH_PNG)
  294. # png/ module is anomalous because it also depends on opengl it really should
  295. # be moved into the opengl/ directory and namespace ...
  296. if(TARGET igl_opengl)
  297. set(STB_IMAGE_DIR "${LIBIGL_EXTERNAL}/stb_image")
  298. if(NOT TARGET stb_image)
  299. add_subdirectory("${STB_IMAGE_DIR}" "stb_image")
  300. endif()
  301. compile_igl_module("png" "")
  302. target_link_libraries(igl_png ${IGL_SCOPE} igl_stb_image igl_opengl)
  303. else()
  304. set(LIBIGL_WITH_PNG OFF CACHE BOOL "" FORCE)
  305. endif()
  306. endif()
  307. ################################################################################
  308. ### Compile the tetgen part ###
  309. if(LIBIGL_WITH_TETGEN)
  310. set(TETGEN_DIR "${LIBIGL_EXTERNAL}/tetgen")
  311. if(NOT TARGET tetgen)
  312. add_subdirectory("${TETGEN_DIR}" "tetgen")
  313. endif()
  314. compile_igl_module("tetgen")
  315. target_link_libraries(igl_tetgen ${IGL_SCOPE} tetgen)
  316. target_include_directories(igl_tetgen ${IGL_SCOPE} ${TETGEN_DIR})
  317. endif()
  318. ################################################################################
  319. ### Compile the triangle part ###
  320. if(LIBIGL_WITH_TRIANGLE)
  321. set(TRIANGLE_DIR "${LIBIGL_EXTERNAL}/triangle")
  322. if(NOT TARGET triangle)
  323. add_subdirectory("${TRIANGLE_DIR}" "triangle")
  324. endif()
  325. compile_igl_module("triangle")
  326. target_link_libraries(igl_triangle ${IGL_SCOPE} triangle)
  327. target_include_directories(igl_triangle ${IGL_SCOPE} ${TRIANGLE_DIR})
  328. endif()
  329. ################################################################################
  330. ### Compile the xml part ###
  331. if(LIBIGL_WITH_XML)
  332. set(TINYXML2_DIR "${LIBIGL_EXTERNAL}/tinyxml2")
  333. if(NOT TARGET tinyxml2)
  334. add_library(tinyxml2 STATIC ${TINYXML2_DIR}/tinyxml2.cpp ${TINYXML2_DIR}/tinyxml2.h)
  335. target_include_directories(tinyxml2 PUBLIC ${TINYXML2_DIR})
  336. set_target_properties(tinyxml2 PROPERTIES
  337. COMPILE_DEFINITIONS "TINYXML2_EXPORT"
  338. VERSION "3.0.0"
  339. SOVERSION "3")
  340. endif()
  341. compile_igl_module("xml")
  342. target_link_libraries(igl_xml ${IGL_SCOPE} tinyxml2)
  343. endif()