libigl.cmake 14 KB

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