CMakeLists.txt 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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(PythonInterp REQUIRED)
  11. find_package(PythonLibs 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/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_OPENGL "Use viewer" ON)
  26. option(LIBIGL_WITH_OPENGL_GLFW "Use GLFW" ON)
  27. option(LIBIGL_WITH_OPENGL_GLFW_IMGUI "Use ImGui" OFF)
  28. option(LIBIGL_WITH_CGAL "Use CGAL" OFF)
  29. option(LIBIGL_WITH_BOOLEAN "Use Cork boolean" OFF)
  30. option(LIBIGL_WITH_COMISO "Use CoMiso" ON)
  31. option(LIBIGL_WITH_EMBREE "Use Embree" ON)
  32. option(LIBIGL_WITH_LIM "Use LIM" ON)
  33. option(LIBIGL_WITH_MATLAB "Use Matlab" OFF)
  34. option(LIBIGL_WITH_MOSEK "Use MOSEK" OFF)
  35. option(LIBIGL_WITH_PNG "Use PNG" ON)
  36. option(LIBIGL_WITH_TETGEN "Use Tetgen" ON)
  37. option(LIBIGL_WITH_TRIANGLE "Use Triangle" ON)
  38. option(LIBIGL_WITH_XML "Use XML" ON)
  39. option(LIBIGL_WITH_PYTHON "Use Python" ON)
  40. option(LIBIGL_WITH_COPYLEFT "Use Copyleft" ON)
  41. ## libigl
  42. if(NOT TARGET igl::core)
  43. list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/../shared/cmake")
  44. include(libigl)
  45. endif()
  46. ## Prepare the python library
  47. pybind11_add_module(pyigl
  48. python_shared.cpp
  49. modules/py_vector.cpp
  50. py_igl.cpp
  51. py_doc.cpp
  52. )
  53. ## Add dependencies
  54. target_include_directories(pyigl PUBLIC igl::core)
  55. ## Optional modules
  56. if(LIBIGL_WITH_OPENGL_GLFW)
  57. target_sources(pyigl PRIVATE "modules/py_igl_opengl_glfw.cpp")
  58. target_compile_definitions(pyigl PUBLIC -DPY_GLFW)
  59. target_link_libraries(pyigl PUBLIC igl::opengl igl::opengl_glfw)
  60. endif()
  61. if(LIBIGL_WITH_COMISO)
  62. target_sources(pyigl PRIVATE "modules/copyleft/py_igl_comiso.cpp")
  63. target_compile_definitions(pyigl PUBLIC -DPY_COMISO)
  64. target_link_libraries(pyigl PUBLIC igl::comiso)
  65. endif()
  66. if(LIBIGL_WITH_TETGEN)
  67. target_sources(pyigl PRIVATE "modules/copyleft/py_igl_tetgen.cpp")
  68. target_compile_definitions(pyigl PUBLIC -DPY_TETGEN)
  69. target_link_libraries(pyigl PUBLIC igl::tetgen)
  70. endif()
  71. if(LIBIGL_WITH_EMBREE)
  72. target_sources(pyigl PRIVATE "modules/py_igl_embree.cpp")
  73. target_compile_definitions(pyigl PUBLIC -DPY_EMBREE)
  74. target_link_libraries(pyigl PUBLIC igl::embree)
  75. endif()
  76. if(LIBIGL_WITH_TRIANGLE)
  77. target_sources(pyigl PRIVATE "modules/py_igl_triangle.cpp")
  78. target_compile_definitions(pyigl PUBLIC -DPY_TRIANGLE)
  79. target_link_libraries(pyigl PUBLIC igl::triangle)
  80. endif()
  81. if(LIBIGL_WITH_CGAL)
  82. target_sources(pyigl PRIVATE "modules/copyleft/py_igl_cgal.cpp")
  83. target_compile_definitions(pyigl PUBLIC -DPY_CGAL)
  84. target_link_libraries(pyigl PUBLIC igl::cgal)
  85. endif()
  86. if(LIBIGL_WITH_COPYLEFT)
  87. target_sources(pyigl PRIVATE "modules/copyleft/py_igl_copyleft.cpp")
  88. target_compile_definitions(pyigl PUBLIC -DPY_COPYLEFT)
  89. endif()
  90. if(LIBIGL_WITH_PNG)
  91. target_sources(pyigl PRIVATE "modules/py_igl_png.cpp")
  92. target_compile_definitions(pyigl PUBLIC -DPY_PNG)
  93. target_link_libraries(pyigl PUBLIC igl::png)
  94. endif()
  95. set_target_properties(pyigl PROPERTIES PREFIX "")
  96. set_target_properties(pyigl PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR})
  97. if(WIN32)
  98. if(MSVC)
  99. # Enforce size-based optimization and link time code generation on MSVC (~30% smaller binaries in experiments)
  100. set_target_properties(pyigl PROPERTIES COMPILE_FLAGS "/Os /GL")
  101. set_target_properties(pyigl PROPERTIES LINK_FLAGS "/LTCG")
  102. endif()
  103. # .PYD file extension on Windows
  104. set_target_properties(pyigl PROPERTIES SUFFIX ".pyd")
  105. # Link against the Python shared library
  106. # message(FATAL_ERROR ${PYTHON_LIBRARY})
  107. # target_link_libraries(igl ${PYTHON_LIBRARY})
  108. target_link_libraries(pyigl PRIVATE ${PYTHON_LIBRARIES})
  109. elseif(UNIX)
  110. # It's quite common to have multiple copies of the same Python version
  111. # installed on one's system. E.g.: one copy from the OS and another copy
  112. # that's statically linked into an application like Blender or Maya.
  113. # If we link our plugin library against the OS Python here and import it
  114. # into Blender or Maya later on, this will cause segfaults when multiple
  115. # conflicting Python instances are active at the same time.
  116. # Windows does not seem to be affected by this issue. The solution for Linux
  117. # and Mac OS is simple: we just don't link against the Python library. The
  118. # resulting shared library will have missing symbols, but that's perfectly
  119. # fine -- they will be resolved at import time.
  120. # .SO file extension on Linux/Mac OS
  121. set_target_properties(pyigl PROPERTIES SUFFIX ".so")
  122. # Enable flag if undefined symbols appear on pyigl module import to get notified about the missing symbols at link time
  123. option(CHECK_UNDEFINED "Check for undefined symbols" OFF)
  124. # Strip unnecessary sections of the binary on Linux/Mac OS
  125. if(APPLE)
  126. set_target_properties(pyigl PROPERTIES MACOSX_RPATH ".")
  127. if(NOT CHECK_UNDEFINED)
  128. set_target_properties(pyigl PROPERTIES LINK_FLAGS "-undefined dynamic_lookup -dead_strip")
  129. endif()
  130. if(NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
  131. add_custom_command(TARGET pyigl POST_BUILD COMMAND strip -u -r ${PROJECT_SOURCE_DIR}/pyigl.so)
  132. endif()
  133. else()
  134. if(CHECK_UNDEFINED)
  135. target_link_libraries(pyigl PRIVATE ${PYTHON_LIBRARIES})
  136. set_target_properties(pyigl PROPERTIES LINK_FLAGS "-Wl,--no-undefined")
  137. endif()
  138. if(NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
  139. add_custom_command(TARGET pyigl POST_BUILD COMMAND strip ${PROJECT_SOURCE_DIR}/pyigl.so)
  140. endif()
  141. endif()
  142. endif()