libigl.cmake 14 KB

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