libigl.cmake 14 KB

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