libigl.cmake 14 KB

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