libigl.cmake 13 KB

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