CMakeLists.txt 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. cmake_minimum_required(VERSION 2.8.12)
  2. project(pyigl)
  3. # Force a specific python version
  4. # set(PYTHON_LIBRARIES "D:/Python34/libs/python34.lib")
  5. # set(PYTHON_INCLUDE_DIR "D:/Python34/include")
  6. # Force a specific python version
  7. # set(PYTHON_LIBRARIES "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/libpython3.5m.dylib")
  8. # set(PYTHON_INCLUDE_DIR "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/include/python3.5m")
  9. set(Python_ADDITIONAL_VERSIONS 3.4 3.5 3.6)
  10. find_package(PythonLibs REQUIRED)
  11. find_package(PythonInterp REQUIRED)
  12. string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
  13. if(UNIX)
  14. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fPIC")
  15. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
  16. if(NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
  17. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden -flto")
  18. endif()
  19. endif()
  20. ## include pybind
  21. set(PYBIND11_DIR ${PROJECT_SOURCE_DIR}/../external/nanogui/ext/pybind11 CACHE PATH "Path to pybind11")
  22. add_subdirectory(${PYBIND11_DIR} pybind11)
  23. ## include libigl
  24. option(LIBIGL_USE_STATIC_LIBRARY "Use LibIGL as static library" OFF)
  25. option(LIBIGL_WITH_VIEWER "Use OpenGL viewer" ON)
  26. option(LIBIGL_WITH_OPENGL "Use viewer" ON)
  27. option(LIBIGL_WITH_OPENGL_GLFW "Use GLFW" ON)
  28. option(LIBIGL_WITH_NANOGUI "Use Nanogui menu" OFF)
  29. option(LIBIGL_WITH_CGAL "Use CGAL" OFF)
  30. option(LIBIGL_WITH_BOOLEAN "Use Cork boolean" OFF)
  31. option(LIBIGL_WITH_COMISO "Use CoMiso" ON)
  32. option(LIBIGL_WITH_EMBREE "Use Embree" ON)
  33. option(LIBIGL_WITH_LIM "Use LIM" ON)
  34. option(LIBIGL_WITH_MATLAB "Use Matlab" OFF)
  35. option(LIBIGL_WITH_MOSEK "Use MOSEK" OFF)
  36. option(LIBIGL_WITH_PNG "Use PNG" ON)
  37. option(LIBIGL_WITH_TETGEN "Use Tetgen" ON)
  38. option(LIBIGL_WITH_TRIANGLE "Use Triangle" ON)
  39. option(LIBIGL_WITH_XML "Use XML" ON)
  40. option(LIBIGL_WITH_PYTHON "Use Python" ON)
  41. option(LIBIGL_WITH_COPYLEFT "Use Copyleft" ON)
  42. ## libigl
  43. if(NOT TARGET igl::core)
  44. list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/../shared/cmake")
  45. include(libigl)
  46. endif()
  47. ## Prepare the python library
  48. pybind11_add_module(pyigl
  49. python_shared.cpp
  50. modules/py_vector.cpp
  51. py_igl.cpp
  52. py_doc.cpp
  53. )
  54. ## Add dependencies
  55. target_include_directories(pyigl PUBLIC igl::core)
  56. ## Optional modules
  57. if(LIBIGL_WITH_VIEWER)
  58. target_sources(pyigl PRIVATE "modules/py_igl_viewer.cpp")
  59. target_compile_definitions(pyigl PUBLIC -DPY_VIEWER)
  60. target_link_libraries(pyigl PUBLIC igl::viewer)
  61. endif()
  62. if(LIBIGL_WITH_COMISO)
  63. target_sources(pyigl PRIVATE "modules/copyleft/py_igl_comiso.cpp")
  64. target_compile_definitions(pyigl PUBLIC -DPY_COMISO)
  65. target_link_libraries(pyigl PUBLIC igl::comiso)
  66. endif()
  67. if(LIBIGL_WITH_TETGEN)
  68. target_sources(pyigl PRIVATE "modules/copyleft/py_igl_tetgen.cpp")
  69. target_compile_definitions(pyigl PUBLIC -DPY_TETGEN)
  70. target_link_libraries(pyigl PUBLIC igl::tetgen)
  71. endif()
  72. if(LIBIGL_WITH_EMBREE)
  73. target_sources(pyigl PRIVATE "modules/py_igl_embree.cpp")
  74. target_compile_definitions(pyigl PUBLIC -DPY_EMBREE)
  75. target_link_libraries(pyigl PUBLIC igl::embree)
  76. endif()
  77. if(LIBIGL_WITH_TRIANGLE)
  78. target_sources(pyigl PRIVATE "modules/py_igl_triangle.cpp")
  79. target_compile_definitions(pyigl PUBLIC -DPY_TRIANGLE)
  80. target_link_libraries(pyigl PUBLIC igl::triangle)
  81. endif()
  82. if(LIBIGL_WITH_CGAL)
  83. target_sources(pyigl PRIVATE "modules/copyleft/py_igl_cgal.cpp")
  84. target_compile_definitions(pyigl PUBLIC -DPY_CGAL)
  85. target_link_libraries(pyigl PUBLIC igl::cgal)
  86. endif()
  87. if(LIBIGL_WITH_COPYLEFT)
  88. target_sources(pyigl PRIVATE "modules/copyleft/py_igl_copyleft.cpp")
  89. target_compile_definitions(pyigl PUBLIC -DPY_COPYLEFT)
  90. endif()
  91. if(LIBIGL_WITH_PNG)
  92. target_sources(pyigl PRIVATE "modules/py_igl_png.cpp")
  93. target_compile_definitions(pyigl PUBLIC -DPY_PNG)
  94. target_link_libraries(pyigl PUBLIC igl::png)
  95. endif()
  96. set_target_properties(pyigl PROPERTIES PREFIX "")
  97. set_target_properties(pyigl PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR})
  98. # Copy the nanogui bindings
  99. #get_target_property(NANOGUI_LIB nanogui_python LOCATION)
  100. #file(COPY ${NANOGUI_LIB} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/../)
  101. if(WIN32)
  102. if(MSVC)
  103. # Enforce size-based optimization and link time code generation on MSVC (~30% smaller binaries in experiments)
  104. set_target_properties(pyigl PROPERTIES COMPILE_FLAGS "/Os /GL")
  105. set_target_properties(pyigl PROPERTIES LINK_FLAGS "/LTCG")
  106. endif()
  107. # .PYD file extension on Windows
  108. set_target_properties(pyigl PROPERTIES SUFFIX ".pyd")
  109. # Link against the Python shared library
  110. # message(FATAL_ERROR ${PYTHON_LIBRARY})
  111. # target_link_libraries(igl ${PYTHON_LIBRARY})
  112. target_link_libraries(pyigl ${PYTHON_LIBRARIES})
  113. elseif(UNIX)
  114. # It's quite common to have multiple copies of the same Python version
  115. # installed on one's system. E.g.: one copy from the OS and another copy
  116. # that's statically linked into an application like Blender or Maya.
  117. # If we link our plugin library against the OS Python here and import it
  118. # into Blender or Maya later on, this will cause segfaults when multiple
  119. # conflicting Python instances are active at the same time.
  120. # Windows does not seem to be affected by this issue. The solution for Linux
  121. # and Mac OS is simple: we just don't link against the Python library. The
  122. # resulting shared library will have missing symbols, but that's perfectly
  123. # fine -- they will be resolved at import time.
  124. # .SO file extension on Linux/Mac OS
  125. set_target_properties(pyigl PROPERTIES SUFFIX ".so")
  126. #Enable flag if undefined symbols appear on pyigl module import to get notified about the missing symbols at link time
  127. option(CHECK_UNDEFINED "Check for undefined symbols" OFF)
  128. # Strip unnecessary sections of the binary on Linux/Mac OS
  129. if(APPLE)
  130. set_target_properties(pyigl PROPERTIES MACOSX_RPATH ".")
  131. if(NOT CHECK_UNDEFINED)
  132. set_target_properties(pyigl PROPERTIES LINK_FLAGS "-undefined dynamic_lookup -dead_strip")
  133. endif()
  134. if(NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
  135. add_custom_command(TARGET pyigl POST_BUILD COMMAND strip -u -r ${PROJECT_SOURCE_DIR}/pyigl.so)
  136. endif()
  137. else()
  138. if(CHECK_UNDEFINED)
  139. target_link_libraries(pyigl ${PYTHON_LIBRARIES})
  140. set_target_properties(pyigl PROPERTIES LINK_FLAGS "-Wl,--no-undefined")
  141. endif()
  142. if(NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
  143. add_custom_command(TARGET pyigl POST_BUILD COMMAND strip ${PROJECT_SOURCE_DIR}/pyigl.so)
  144. endif()
  145. endif()
  146. endif()
  147. if(LIBIGL_WITH_PYTHON AND LIBIGL_WITH_NANOGUI)
  148. # Copy the nanogui python lib after compilation
  149. get_target_property(NANOGUI_LIB nanogui-python LOCATION)
  150. add_custom_target(copy ALL)
  151. add_custom_command(
  152. TARGET copy
  153. COMMAND ${CMAKE_COMMAND} -E copy ${NANOGUI_LIB} ${PROJECT_SOURCE_DIR}
  154. )
  155. add_dependencies(copy pyigl)
  156. endif()