libigl.cmake 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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_VIEWER "Use OpenGL viewer" ON)
  19. option(LIBIGL_WITH_XML "Use XML" ON)
  20. option(LIBIGL_WITH_PYTHON "Use Python" OFF)
  21. if(LIBIGL_WITH_VIEWER AND (NOT LIBIGL_WITH_OPENGL_GLFW OR NOT LIBIGL_WITH_OPENGL) )
  22. message(FATAL_ERROR "LIBIGL_WITH_VIEWER=ON requires LIBIGL_WITH_OPENGL_GLFW=ON and LIBIGL_WITH_OPENGL=ON")
  23. endif()
  24. ################################################################################
  25. ### Configuration
  26. set(LIBIGL_ROOT "${CMAKE_CURRENT_LIST_DIR}/../..")
  27. set(LIBIGL_SOURCE_DIR "${LIBIGL_ROOT}/include")
  28. set(LIBIGL_EXTERNAL "${LIBIGL_ROOT}/external")
  29. ### Multiple dependencies are buried in Nanogui
  30. set(NANOGUI_DIR "${LIBIGL_EXTERNAL}/nanogui")
  31. # Dependencies are linked as INTERFACE targets unless libigl is compiled as a static library
  32. if(LIBIGL_USE_STATIC_LIBRARY)
  33. set(IGL_SCOPE PUBLIC)
  34. else()
  35. set(IGL_SCOPE INTERFACE)
  36. endif()
  37. ################################################################################
  38. ### IGL Common
  39. ################################################################################
  40. add_library(igl_common INTERFACE)
  41. target_include_directories(igl_common SYSTEM INTERFACE ${LIBIGL_SOURCE_DIR})
  42. if(LIBIGL_USE_STATIC_LIBRARY)
  43. target_compile_definitions(igl_common INTERFACE -DIGL_STATIC_LIBRARY)
  44. endif()
  45. # Transitive C++11 flags
  46. include(CXXFeatures)
  47. target_compile_features(igl_common INTERFACE ${CXX11_FEATURES})
  48. # Other compilation flags
  49. if(MSVC)
  50. # Enable parallel compilation for Visual Studio
  51. target_compile_options(igl_common INTERFACE /MP /bigobj)
  52. endif()
  53. if(BUILD_SHARED_LIBS)
  54. # Generate position independent code
  55. set_target_properties(igl_common PROPERTIES INTERFACE_POSITION_INDEPENDENT_CODE ON)
  56. endif()
  57. # Eigen
  58. if(TARGET Eigen3::Eigen)
  59. # If an imported target already exists, use it
  60. target_link_libraries(igl_common INTERFACE Eigen3::Eigen)
  61. else()
  62. target_include_directories(igl_common SYSTEM INTERFACE ${NANOGUI_DIR}/ext/eigen)
  63. endif()
  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. find_package(CGAL COMPONENTS Core)
  113. if(CGAL_FOUND)
  114. compile_igl_module("cgal")
  115. find_package(Boost 1.48 REQUIRED thread system)
  116. target_include_directories(igl_cgal ${IGL_SCOPE} ${CGAL_INCLUDE_DIRS})
  117. target_link_libraries(igl_cgal ${IGL_SCOPE} CGAL::CGAL CGAL::CGAL_Core ${Boost_LIBRARIES})
  118. else()
  119. set(LIBIGL_WITH_CGAL OFF CACHE BOOL "" FORCE)
  120. endif()
  121. endif()
  122. ################################################################################
  123. # Compile CoMISo
  124. # NOTE: this cmakefile works only with the
  125. # comiso available here: https://github.com/libigl/CoMISo
  126. if(LIBIGL_WITH_COMISO)
  127. compile_igl_module("comiso")
  128. if(NOT TARGET CoMISo)
  129. add_subdirectory("${LIBIGL_EXTERNAL}/CoMISo" CoMISo)
  130. endif()
  131. target_link_libraries(igl_comiso ${IGL_SCOPE} CoMISo)
  132. endif()
  133. ################################################################################
  134. ### Compile the cork parts ###
  135. if(LIBIGL_WITH_CORK)
  136. set(CORK_DIR "${LIBIGL_EXTERNAL}/cork")
  137. if(NOT TARGET cork)
  138. # call this "lib-cork" instead of "cork", otherwise cmake gets confused about
  139. # "cork" executable
  140. add_subdirectory("${CORK_DIR}" "lib-cork")
  141. endif()
  142. compile_igl_module("cork")
  143. target_include_directories(igl_cork ${IGL_SCOPE} cork)
  144. target_include_directories(igl_cork ${IGL_SCOPE} "${CORK_DIR}/src")
  145. endif()
  146. ################################################################################
  147. ### Compile the embree part ###
  148. if(LIBIGL_WITH_EMBREE)
  149. set(EMBREE_DIR "${LIBIGL_EXTERNAL}/embree")
  150. set(EMBREE_ISPC_SUPPORT OFF CACHE BOOL " " FORCE)
  151. set(EMBREE_TASKING_SYSTEM "INTERNAL" CACHE BOOL " " FORCE)
  152. set(EMBREE_TUTORIALS OFF CACHE BOOL " " FORCE)
  153. set(EMBREE_MAX_ISA NONE CACHE STRINGS " " FORCE)
  154. # set(ENABLE_INSTALLER OFF CACHE BOOL " " FORCE)
  155. if(MSVC)
  156. # set(EMBREE_STATIC_RUNTIME OFF CACHE BOOL " " FORCE)
  157. set(EMBREE_STATIC_LIB OFF CACHE BOOL " " FORCE)
  158. else()
  159. set(EMBREE_STATIC_LIB ON CACHE BOOL " " FORCE)
  160. endif()
  161. if(NOT TARGET embree)
  162. add_subdirectory("${EMBREE_DIR}" "embree")
  163. endif()
  164. if(MSVC)
  165. add_custom_target(Copy-Embree-DLL ALL # Adds a post-build event
  166. COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E
  167. $<TARGET_FILE:embree> # <--this is in-file
  168. "${CMAKE_BINARY_DIR}" # <--this is out-file path
  169. DEPENDS embree) # Execute after embree target has been built
  170. endif()
  171. compile_igl_module("embree")
  172. target_link_libraries(igl_embree ${IGL_SCOPE} embree)
  173. target_include_directories(igl_embree ${IGL_SCOPE} ${EMBREE_DIR}/include)
  174. if(NOT MSVC)
  175. target_compile_definitions(igl_embree ${IGL_SCOPE} -DENABLE_STATIC_LIB)
  176. endif()
  177. endif()
  178. ################################################################################
  179. ### Compile the lim part ###
  180. if(LIBIGL_WITH_LIM)
  181. set(LIM_DIR "${LIBIGL_EXTERNAL}/lim")
  182. if(NOT TARGET lim)
  183. add_subdirectory("${LIM_DIR}" "lim")
  184. endif()
  185. compile_igl_module("lim")
  186. target_link_libraries(igl_lim ${IGL_SCOPE} lim)
  187. target_include_directories(igl_lim ${IGL_SCOPE} ${LIM_DIR})
  188. endif()
  189. ################################################################################
  190. ### Compile the matlab part ###
  191. if(LIBIGL_WITH_MATLAB)
  192. find_package(MATLAB)
  193. if(MATLAB_FOUND)
  194. compile_igl_module("matlab")
  195. target_link_libraries(igl_matlab ${IGL_SCOPE} ${MATLAB_LIBRARIES})
  196. target_include_directories(igl_matlab ${IGL_SCOPE} ${MATLAB_INCLUDE_DIR})
  197. else()
  198. set(LIBIGL_WITH_MATLAB OFF CACHE BOOL "" FORCE)
  199. endif()
  200. endif()
  201. ################################################################################
  202. ### Compile the mosek part ###
  203. if(LIBIGL_WITH_MOSEK)
  204. find_package(MOSEK)
  205. if(MOSEK_FOUND)
  206. compile_igl_module("mosek")
  207. target_link_libraries(igl_mosek ${IGL_SCOPE} ${MOSEK_LIBRARIES})
  208. target_include_directories(igl_mosek ${IGL_SCOPE} ${MOSEK_INCLUDE_DIRS})
  209. target_compile_definitions(igl_mosek ${IGL_SCOPE} -DLIBIGL_WITH_MOSEK)
  210. else()
  211. set(LIBIGL_WITH_MOSEK OFF CACHE BOOL "" FORCE)
  212. endif()
  213. endif()
  214. ################################################################################
  215. ### Compile the opengl parts ###
  216. if(LIBIGL_WITH_OPENGL)
  217. # OpenGL modules
  218. find_package(OpenGL REQUIRED)
  219. compile_igl_module("opengl")
  220. compile_igl_module("opengl2")
  221. target_link_libraries(igl_opengl ${IGL_SCOPE} ${OPENGL_gl_LIBRARY})
  222. target_link_libraries(igl_opengl2 ${IGL_SCOPE} ${OPENGL_gl_LIBRARY})
  223. target_include_directories(igl_opengl SYSTEM ${IGL_SCOPE} ${OPENGL_INCLUDE_DIR})
  224. target_include_directories(igl_opengl2 SYSTEM ${IGL_SCOPE} ${OPENGL_INCLUDE_DIR})
  225. # GLEW for linux and windows
  226. if(NOT TARGET glew)
  227. add_library(glew STATIC ${NANOGUI_DIR}/ext/glew/src/glew.c)
  228. target_include_directories(glew SYSTEM PUBLIC ${NANOGUI_DIR}/ext/glew/include)
  229. target_compile_definitions(glew PUBLIC -DGLEW_BUILD -DGLEW_NO_GLU)
  230. endif()
  231. target_link_libraries(igl_opengl ${IGL_SCOPE} glew)
  232. target_link_libraries(igl_opengl2 ${IGL_SCOPE} glew)
  233. # Nanogui
  234. if(LIBIGL_WITH_NANOGUI)
  235. if(LIBIGL_WITH_PYTHON)
  236. set(NANOGUI_BUILD_PYTHON ON CACHE BOOL " " FORCE)
  237. else()
  238. set(NANOGUI_BUILD_PYTHON OFF CACHE BOOL " " FORCE)
  239. endif()
  240. set(NANOGUI_BUILD_EXAMPLE OFF CACHE BOOL " " FORCE)
  241. set(NANOGUI_BUILD_SHARED OFF CACHE BOOL " " FORCE)
  242. add_subdirectory(${NANOGUI_DIR} nanogui)
  243. target_include_directories(nanogui PUBLIC
  244. "${NANOGUI_DIR}/include"
  245. "${NANOGUI_DIR}/ext/nanovg/src")
  246. endif()
  247. # GLFW module
  248. if(LIBIGL_WITH_OPENGL_GLFW)
  249. compile_igl_module("opengl/glfw")
  250. if(NOT TARGET glfw)
  251. set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL " " FORCE)
  252. set(GLFW_BUILD_TESTS OFF CACHE BOOL " " FORCE)
  253. set(GLFW_BUILD_DOCS OFF CACHE BOOL " " FORCE)
  254. set(GLFW_BUILD_INSTALL OFF CACHE BOOL " " FORCE)
  255. add_subdirectory(${NANOGUI_DIR}/ext/glfw glfw)
  256. endif()
  257. target_include_directories(glfw ${IGL_SCOPE} ${NANOGUI_DIR}/ext/glfw/include)
  258. target_link_libraries(igl_opengl_glfw ${IGL_SCOPE} igl_opengl glfw)
  259. endif()
  260. # Viewer module
  261. if(LIBIGL_WITH_VIEWER)
  262. compile_igl_module("viewer")
  263. target_link_libraries(igl_viewer ${IGL_SCOPE} igl_core glfw glew ${OPENGL_gl_LIBRARY})
  264. target_include_directories(igl_viewer SYSTEM ${IGL_SCOPE} ${OPENGL_INCLUDE_DIR})
  265. if(TARGET nanogui)
  266. target_link_libraries(igl_viewer ${IGL_SCOPE} nanogui)
  267. target_compile_definitions(igl_viewer ${IGL_SCOPE} -DIGL_VIEWER_WITH_NANOGUI)
  268. endif()
  269. endif()
  270. endif()
  271. ################################################################################
  272. ### Compile the png parts ###
  273. if(LIBIGL_WITH_PNG)
  274. # png/ module is anomalous because it also depends on opengl it really should
  275. # be moved into the opengl/ directory and namespace ...
  276. if(TARGET igl_opengl)
  277. set(STB_IMAGE_DIR "${LIBIGL_EXTERNAL}/stb_image")
  278. if(NOT TARGET stb_image)
  279. add_subdirectory("${STB_IMAGE_DIR}" "stb_image")
  280. endif()
  281. compile_igl_module("png" "")
  282. target_link_libraries(igl_png ${IGL_SCOPE} igl_stb_image igl_opengl)
  283. else()
  284. set(LIBIGL_WITH_PNG OFF CACHE BOOL "" FORCE)
  285. endif()
  286. endif()
  287. ################################################################################
  288. ### Compile the tetgen part ###
  289. if(LIBIGL_WITH_TETGEN)
  290. set(TETGEN_DIR "${LIBIGL_EXTERNAL}/tetgen")
  291. if(NOT TARGET tetgen)
  292. add_subdirectory("${TETGEN_DIR}" "tetgen")
  293. endif()
  294. compile_igl_module("tetgen")
  295. target_link_libraries(igl_tetgen ${IGL_SCOPE} tetgen)
  296. target_include_directories(igl_tetgen ${IGL_SCOPE} ${TETGEN_DIR})
  297. endif()
  298. ################################################################################
  299. ### Compile the triangle part ###
  300. if(LIBIGL_WITH_TRIANGLE)
  301. set(TRIANGLE_DIR "${LIBIGL_EXTERNAL}/triangle")
  302. if(NOT TARGET triangle)
  303. add_subdirectory("${TRIANGLE_DIR}" "triangle")
  304. endif()
  305. compile_igl_module("triangle")
  306. target_link_libraries(igl_triangle ${IGL_SCOPE} triangle)
  307. target_include_directories(igl_triangle ${IGL_SCOPE} ${TRIANGLE_DIR})
  308. endif()
  309. ################################################################################
  310. ### Compile the xml part ###
  311. if(LIBIGL_WITH_XML)
  312. set(TINYXML2_DIR "${LIBIGL_EXTERNAL}/tinyxml2")
  313. if(NOT TARGET tinyxml2)
  314. add_library(tinyxml2 STATIC ${TINYXML2_DIR}/tinyxml2.cpp ${TINYXML2_DIR}/tinyxml2.h)
  315. target_include_directories(tinyxml2 PUBLIC ${TINYXML2_DIR})
  316. set_target_properties(tinyxml2 PROPERTIES
  317. COMPILE_DEFINITIONS "TINYXML2_EXPORT"
  318. VERSION "3.0.0"
  319. SOVERSION "3")
  320. endif()
  321. compile_igl_module("xml")
  322. target_link_libraries(igl_xml ${IGL_SCOPE} tinyxml2)
  323. endif()