libigl.cmake 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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" ON)
  5. option(LIBIGL_WITH_CGAL "Use CGAL" ON)
  6. option(LIBIGL_WITH_COMISO "Use CoMiso" ON)
  7. option(LIBIGL_WITH_CORK "Use Cork" ON)
  8. option(LIBIGL_WITH_EMBREE "Use Embree" ON)
  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 prefix)
  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/${prefix}/${module_dir}/*.cpp")
  70. add_library(igl_${module_name} STATIC ${SOURCES_IGL_${module_name}} ${ARGN})
  71. target_link_libraries(igl_${module_name} PUBLIC igl_common)
  72. else()
  73. add_library(igl_${module_name} INTERFACE)
  74. target_link_libraries(igl_${module_name} INTERFACE igl_common)
  75. endif()
  76. # Alias target because it looks nicer
  77. message(STATUS "Creating target: igl::${module_name}")
  78. add_library(igl::${module_name} ALIAS igl_${module_name})
  79. endfunction()
  80. ################################################################################
  81. ### IGL Core
  82. ################################################################################
  83. if(LIBIGL_USE_STATIC_LIBRARY)
  84. file(GLOB SOURCES_IGL
  85. "${LIBIGL_SOURCE_DIR}/igl/*.cpp"
  86. "${LIBIGL_SOURCE_DIR}/igl/copyleft/*.cpp")
  87. endif()
  88. compile_igl_module("core" "" ${SOURCES_IGL})
  89. ################################################################################
  90. ### Compile the AntTweakBar part ###
  91. # if(LIBIGL_WITH_ANTTWEAKBAR)
  92. # set(ANTTWEAKBAR_DIR "${LIBIGL_EXTERNAL}/AntTweakBar")
  93. # set(ANTTWEAKBAR_INCLUDE_DIR "${ANTTWEAKBAR_DIR}/include")
  94. # set(ANTTWEAKBAR_C_SRC_FILES
  95. # "${ANTTWEAKBAR_DIR}/src/TwEventGLFW.c"
  96. # "${ANTTWEAKBAR_DIR}/src/TwEventGLUT.c"
  97. # "${ANTTWEAKBAR_DIR}/src/TwEventSDL.c"
  98. # "${ANTTWEAKBAR_DIR}/src/TwEventSDL12.c"
  99. # "${ANTTWEAKBAR_DIR}/src/TwEventSDL13.c"
  100. # )
  101. # set(ANTTWEAKBAR_CPP_SRC_FILES
  102. # "${ANTTWEAKBAR_DIR}/src/LoadOGL.cpp"
  103. # "${ANTTWEAKBAR_DIR}/src/LoadOGLCore.cpp"
  104. # "${ANTTWEAKBAR_DIR}/src/TwBar.cpp"
  105. # "${ANTTWEAKBAR_DIR}/src/TwColors.cpp"
  106. # "${ANTTWEAKBAR_DIR}/src/TwEventSFML.cpp"
  107. # "${ANTTWEAKBAR_DIR}/src/TwFonts.cpp"
  108. # "${ANTTWEAKBAR_DIR}/src/TwMgr.cpp"
  109. # "${ANTTWEAKBAR_DIR}/src/TwOpenGL.cpp"
  110. # "${ANTTWEAKBAR_DIR}/src/TwOpenGLCore.cpp"
  111. # "${ANTTWEAKBAR_DIR}/src/TwPrecomp.cpp"
  112. # )
  113. # # These are probably needed for windows/Linux, should append if
  114. # # windows/Linux
  115. # #"${ANTTWEAKBAR_DIR}/src/TwEventWin.c"
  116. # #"${ANTTWEAKBAR_DIR}/src/TwEventX11.c"
  117. # #"${ANTTWEAKBAR_DIR}/src/TwDirect3D10.cpp"
  118. # #"${ANTTWEAKBAR_DIR}/src/TwDirect3D11.cpp"
  119. # #"${ANTTWEAKBAR_DIR}/src/TwDirect3D9.cpp"
  120. # list(
  121. # APPEND
  122. # ANTTWEAKBAR_SRC_FILES
  123. # "${ANTTWEAKBAR_C_SRC_FILES}"
  124. # "${ANTTWEAKBAR_CPP_SRC_FILES}")
  125. # add_library(AntTweakBar STATIC "${ANTTWEAKBAR_SRC_FILES}")
  126. # target_include_directories(AntTweakBar PUBLIC "${ANTTWEAKBAR_INCLUDE_DIR}")
  127. # if(APPLE)
  128. # set_target_properties(
  129. # AntTweakBar
  130. # PROPERTIES
  131. # COMPILE_FLAGS
  132. # "-fPIC -fno-strict-aliasing -x objective-c++")
  133. # target_compile_definitions(
  134. # AntTweakBar PUBLIC _MACOSX __PLACEMENT_NEW_INLINE)
  135. # endif()
  136. # list(APPEND LIBIGL_INCLUDE_DIRS "${ANTTWEAKBAR_INCLUDE_DIR}")
  137. # set(LIBIGL_ANTTWEAKBAR_EXTRA_LIBRARIES "AntTweakBar")
  138. # list(APPEND LIBIGL_EXTRA_LIBRARIES ${LIBIGL_ANTTWEAKBAR_EXTRA_LIBRARIES})
  139. # if(LIBIGL_USE_STATIC_LIBRARY)
  140. # compile_igl_module("anttweakbar" "")
  141. # target_include_directories(igl_anttweakbar PRIVATE ${ANTTWEAKBAR_INCLUDE_DIR})
  142. # endif()
  143. # endif()
  144. ################################################################################
  145. ### Compile the cgal parts ###
  146. if(LIBIGL_WITH_CGAL)
  147. # CGAL Core is needed for
  148. # `Exact_predicates_exact_constructions_kernel_with_sqrt`
  149. find_package(CGAL COMPONENTS Core)
  150. if(CGAL_FOUND)
  151. compile_igl_module("cgal" "copyleft/")
  152. find_package(Boost 1.48 REQUIRED thread system)
  153. target_link_libraries(igl_cgal ${IGL_SCOPE} CGAL::CGAL CGAL::CGAL_Core ${Boost_LIBRARIES})
  154. else()
  155. set(LIBIGL_WITH_CGAL OFF CACHE BOOL "" FORCE)
  156. endif()
  157. endif()
  158. ################################################################################
  159. # Compile CoMISo
  160. # NOTE: this cmakefile works only with the
  161. # comiso available here: https://github.com/libigl/CoMISo
  162. if(LIBIGL_WITH_COMISO)
  163. compile_igl_module("comiso" "copyleft/")
  164. add_subdirectory(${LIBIGL_EXTERNAL}/CoMISo CoMISo)
  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. # set(CORK_INCLUDE_DIR "${CORK_DIR}/src")
  172. # # call this "lib-cork" instead of "cork", otherwise cmake gets confused about
  173. # # "cork" executable
  174. # add_subdirectory("${CORK_DIR}" "lib-cork")
  175. # list(APPEND LIBIGL_INCLUDE_DIRS "${CORK_INCLUDE_DIR}")
  176. # list(APPEND LIBIGL_CORK_EXTRA_LIBRARIES "cork")
  177. # list(APPEND LIBIGL_EXTRA_LIBRARIES ${LIBIGL_CORK_EXTRA_LIBRARIES})
  178. # if(LIBIGL_USE_STATIC_LIBRARY)
  179. # compile_igl_module("cork" "copyleft/")
  180. # target_include_directories(igl_cork PRIVATE ${CORK_INCLUDE_DIR})
  181. # endif()
  182. # endif()
  183. ################################################################################
  184. ### Compile the embree part ###
  185. # if(LIBIGL_WITH_EMBREE)
  186. # set(EMBREE_DIR "${LIBIGL_EXTERNAL}/embree")
  187. # set(EMBREE_ISPC_SUPPORT OFF CACHE BOOL " " FORCE)
  188. # set(EMBREE_TASKING_SYSTEM "INTERNAL" CACHE BOOL " " FORCE)
  189. # set(EMBREE_TUTORIALS OFF CACHE BOOL " " FORCE)
  190. # set(EMBREE_MAX_ISA NONE CACHE STRINGS " " FORCE)
  191. # # set(ENABLE_INSTALLER OFF CACHE BOOL " " FORCE)
  192. # if(MSVC)
  193. # # set(EMBREE_STATIC_RUNTIME OFF CACHE BOOL " " FORCE)
  194. # set(EMBREE_STATIC_LIB OFF CACHE BOOL " " FORCE)
  195. # else()
  196. # set(EMBREE_STATIC_LIB ON CACHE BOOL " " FORCE)
  197. # endif()
  198. # add_subdirectory("${EMBREE_DIR}" "embree")
  199. # list(APPEND LIBIGL_INCLUDE_DIRS "${EMBREE_DIR}/include")
  200. # list(APPEND LIBIGL_EMBREE_EXTRA_LIBRARIES "embree")
  201. # list(APPEND LIBIGL_EXTRA_LIBRARIES ${LIBIGL_EMBREE_EXTRA_LIBRARIES})
  202. # if(NOT MSVC)
  203. # list(APPEND LIBIGL_DEFINITIONS "-DENABLE_STATIC_LIB")
  204. # endif()
  205. # if(MSVC)
  206. # add_custom_target(Copy-Embree-DLL ALL # Adds a post-build event to MyTest
  207. # COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E copy_if_different..."
  208. # "${CMAKE_BINARY_DIR}/libigl/embree/$<CONFIGURATION>/embree.dll" # <--this is in-file
  209. # "${CMAKE_BINARY_DIR}/embree.dll") # <--this is out-file path endif()
  210. # endif()
  211. # if(LIBIGL_USE_STATIC_LIBRARY)
  212. # compile_igl_module("embree" "")
  213. # target_include_directories(igl_embree PRIVATE ${EMBREE_DIR}/include)
  214. # if(NOT MSVC)
  215. # target_compile_definitions(igl_embree PRIVATE -DENABLE_STATIC_LIB)
  216. # endif()
  217. # endif()
  218. # endif()
  219. ################################################################################
  220. ### Compile the lim part ###
  221. # if(LIBIGL_WITH_LIM)
  222. # set(LIM_DIR "${LIBIGL_EXTERNAL}/lim")
  223. # add_subdirectory("${LIM_DIR}" "lim")
  224. # list(APPEND LIBIGL_INCLUDE_DIRS ${LIM_DIR})
  225. # ## it depends on ligigl, so placing it here solve linking problems
  226. # #list(APPEND LIBIGL_LIBRARIES "lim")
  227. # # ^--- Alec: I don't understand this comment. Does lim need to come before
  228. # # libigl libraries? Why can't lim be placed where it belongs in
  229. # # LIBIGL_EXTRA_LIBRARIES?
  230. # set(LIBIGL_LIM_EXTRA_LIBRARIES "lim")
  231. # list(APPEND LIBIGL_EXTRA_LIBRARIES "${LIBIGL_LIM_EXTRA_LIBRARIES}")
  232. # if(LIBIGL_USE_STATIC_LIBRARY)
  233. # compile_igl_module("lim" "")
  234. # target_include_directories(igl_lim PRIVATE ${LIM_DIR})
  235. # endif()
  236. # endif()
  237. ################################################################################
  238. ### Compile the matlab part ###
  239. # if(LIBIGL_WITH_MATLAB)
  240. # find_package(MATLAB REQUIRED)
  241. # list(APPEND LIBIGL_INCLUDE_DIRS ${MATLAB_INCLUDE_DIR})
  242. # list(APPEND LIBIGL_MATLAB_EXTRA_LIBRARIES ${MATLAB_LIBRARIES})
  243. # list(APPEND LIBIGL_EXTRA_LIBRARIES ${LIBIGL_MATLAB_EXTRA_LIBRARIES})
  244. # if(LIBIGL_USE_STATIC_LIBRARY)
  245. # compile_igl_module("matlab" "")
  246. # target_include_directories(igl_matlab PRIVATE ${MATLAB_INCLUDE_DIR})
  247. # endif()
  248. # endif()
  249. ################################################################################
  250. ### Compile the mosek part ###
  251. if(LIBIGL_WITH_MOSEK)
  252. find_package(MOSEK)
  253. if(MOSEK_FOUND)
  254. compile_igl_module("mosek" "")
  255. target_link_libraries(igl_mosek ${IGL_SCOPE} ${MOSEK_LIBRARIES})
  256. target_include_directories(igl_mosek ${IGL_SCOPE} ${MOSEK_INCLUDE_DIRS})
  257. target_compile_definitions(igl_mosek ${IGL_SCOPE} -DLIBIGL_WITH_MOSEK)
  258. else()
  259. set(LIBIGL_WITH_MOSEK OFF CACHE BOOL "" FORCE)
  260. endif()
  261. endif()
  262. ################################################################################
  263. ### Compile the opengl parts ###
  264. if(LIBIGL_WITH_OPENGL)
  265. # OpenGL modules
  266. find_package(OpenGL REQUIRED)
  267. compile_igl_module("opengl" "")
  268. compile_igl_module("opengl2" "")
  269. target_link_libraries(igl_opengl ${IGL_SCOPE} ${OPENGL_gl_LIBRARY})
  270. target_link_libraries(igl_opengl2 ${IGL_SCOPE} ${OPENGL_gl_LIBRARY})
  271. target_include_directories(igl_opengl SYSTEM ${IGL_SCOPE} ${OPENGL_INCLUDE_DIR})
  272. target_include_directories(igl_opengl2 SYSTEM ${IGL_SCOPE} ${OPENGL_INCLUDE_DIR})
  273. # GLEW for linux and windows
  274. if(NOT TARGET glew)
  275. add_library(glew STATIC ${NANOGUI_DIR}/ext/glew/src/glew.c)
  276. target_include_directories(glew SYSTEM PUBLIC ${NANOGUI_DIR}/ext/glew/include)
  277. target_compile_definitions(glew PUBLIC -DGLEW_BUILD -DGLEW_NO_GLU)
  278. endif()
  279. target_link_libraries(igl_opengl ${IGL_SCOPE} glew)
  280. target_link_libraries(igl_opengl2 ${IGL_SCOPE} glew)
  281. # Nanogui
  282. if(LIBIGL_WITH_NANOGUI)
  283. if(LIBIGL_WITH_PYTHON)
  284. set(NANOGUI_BUILD_PYTHON ON CACHE BOOL " " FORCE)
  285. else()
  286. set(NANOGUI_BUILD_PYTHON OFF CACHE BOOL " " FORCE)
  287. endif()
  288. set(NANOGUI_BUILD_EXAMPLE OFF CACHE BOOL " " FORCE)
  289. set(NANOGUI_BUILD_SHARED OFF CACHE BOOL " " FORCE)
  290. add_subdirectory(${NANOGUI_DIR} nanogui)
  291. target_include_directories(nanogui PUBLIC
  292. "${NANOGUI_DIR}/include"
  293. "${NANOGUI_DIR}/ext/nanovg/src")
  294. endif()
  295. # GLFW module
  296. if(LIBIGL_WITH_OPENGL_GLFW)
  297. compile_igl_module("opengl/glfw" "")
  298. if(NOT TARGET glfw)
  299. set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL " " FORCE)
  300. set(GLFW_BUILD_TESTS OFF CACHE BOOL " " FORCE)
  301. set(GLFW_BUILD_DOCS OFF CACHE BOOL " " FORCE)
  302. set(GLFW_BUILD_INSTALL OFF CACHE BOOL " " FORCE)
  303. add_subdirectory(${NANOGUI_DIR}/ext/glfw glfw)
  304. target_include_directories(glfw ${IGL_SCOPE} ${NANOGUI_DIR}/ext/glfw/include)
  305. endif()
  306. target_link_libraries(igl_opengl_glfw ${IGL_SCOPE} igl_opengl glfw)
  307. endif()
  308. # Viewer module
  309. if(LIBIGL_WITH_VIEWER)
  310. compile_igl_module("viewer" "")
  311. target_link_libraries(igl_viewer ${IGL_SCOPE} igl_core glfw glew ${OPENGL_gl_LIBRARY})
  312. target_include_directories(igl_viewer SYSTEM ${IGL_SCOPE} ${OPENGL_INCLUDE_DIR})
  313. if(TARGET nanogui)
  314. target_link_libraries(igl_viewer ${IGL_SCOPE} nanogui)
  315. target_compile_definitions(igl_viewer ${IGL_SCOPE} -DIGL_VIEWER_WITH_NANOGUI)
  316. endif()
  317. endif()
  318. endif()
  319. ################################################################################
  320. ### Compile the png parts ###
  321. if(LIBIGL_WITH_PNG)
  322. set(STB_IMAGE_DIR "${LIBIGL_EXTERNAL}/stb_image")
  323. if(NOT TARGET stb_image)
  324. add_subdirectory("${STB_IMAGE_DIR}" "stb_image")
  325. endif()
  326. compile_igl_module("png" "")
  327. target_link_libraries(igl_png ${IGL_SCOPE} igl_stb_image)
  328. endif()
  329. ################################################################################
  330. ### Compile the tetgen part ###
  331. if(LIBIGL_WITH_TETGEN)
  332. set(TETGEN_DIR "${LIBIGL_EXTERNAL}/tetgen")
  333. if(NOT TARGET tetgen)
  334. add_subdirectory("${TETGEN_DIR}" "tetgen")
  335. endif()
  336. compile_igl_module("tetgen" "copyleft/")
  337. target_link_libraries(igl_tetgen ${IGL_SCOPE} tetgen)
  338. target_include_directories(igl_tetgen ${IGL_SCOPE} ${TETGEN_DIR})
  339. endif()
  340. ################################################################################
  341. ### Compile the triangle part ###
  342. if(LIBIGL_WITH_TRIANGLE)
  343. set(TRIANGLE_DIR "${LIBIGL_EXTERNAL}/triangle")
  344. if(NOT TARGET triangle)
  345. add_subdirectory("${TRIANGLE_DIR}" "triangle")
  346. endif()
  347. compile_igl_module("triangle" "")
  348. target_link_libraries(igl_triangle ${IGL_SCOPE} triangle)
  349. target_include_directories(igl_triangle ${IGL_SCOPE} ${TRIANGLE_DIR})
  350. endif()
  351. ################################################################################
  352. ### Compile the xml part ###
  353. if(LIBIGL_WITH_XML)
  354. set(TINYXML2_DIR "${LIBIGL_EXTERNAL}/tinyxml2")
  355. if(NOT TARGET tinyxml2)
  356. add_library(tinyxml2 STATIC ${TINYXML2_DIR}/tinyxml2.cpp ${TINYXML2_DIR}/tinyxml2.h)
  357. target_include_directories(tinyxml2 PUBLIC ${TINYXML2_DIR})
  358. set_target_properties(tinyxml2 PROPERTIES
  359. COMPILE_DEFINITIONS "TINYXML2_EXPORT"
  360. VERSION "3.0.0"
  361. SOVERSION "3")
  362. endif()
  363. compile_igl_module("xml" "")
  364. target_link_libraries(igl_xml ${IGL_SCOPE} tinyxml2)
  365. endif()