CMakeLists.txt 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. cmake_minimum_required(VERSION 2.8.12)
  2. project(pyigl)
  3. ### Adding libIGL: choose the path to your local copy libIGL
  4. if(NOT TARGET igl::core)
  5. ### Prefer header-only mode for compiling Python bindings
  6. if(NOT LIBIGL_WITH_PYTHON OR NOT LIBIGL_USE_STATIC_LIBRARY)
  7. message(FATAL_ERROR
  8. "Trying to compile Python bindings without -DLIBIGL_WITH_PYTHON=ON. "
  9. "Either enable manually all the necessary options, or compile from "
  10. "the root folder with -DLIBIGL_USE_STATIC_LIBRARY=OFF")
  11. endif()
  12. list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/../cmake")
  13. include(libigl)
  14. endif()
  15. # Force a specific python version
  16. # set(PYTHON_LIBRARIES "D:/Python34/libs/python34.lib")
  17. # set(PYTHON_INCLUDE_DIR "D:/Python34/include")
  18. # Force a specific python version
  19. # set(PYTHON_LIBRARIES "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/libpython3.5m.dylib")
  20. # set(PYTHON_INCLUDE_DIR "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/include/python3.5m")
  21. set(Python_ADDITIONAL_VERSIONS 3.4 3.5 3.6 3.7)
  22. find_package(PythonInterp 3.4 REQUIRED)
  23. find_package(PythonLibs 3.4 REQUIRED)
  24. ## libigl
  25. if(NOT TARGET igl::core)
  26. list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/../cmake")
  27. include(libigl)
  28. endif()
  29. string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
  30. if(UNIX)
  31. if(NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
  32. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden -flto")
  33. endif()
  34. endif()
  35. ## include pybind
  36. set(PYBIND11_DIR ${PROJECT_SOURCE_DIR}/../external/pybind11 CACHE PATH "Path to pybind11")
  37. add_subdirectory(${PYBIND11_DIR} pybind11)
  38. ## Prepare the python library
  39. pybind11_add_module(pyigl
  40. python_shared.cpp
  41. modules/py_vector.cpp
  42. py_igl.cpp
  43. py_doc.cpp
  44. )
  45. ## Add dependencies
  46. target_link_libraries(pyigl PUBLIC igl::core)
  47. ## Optional modules
  48. if(LIBIGL_WITH_OPENGL_GLFW)
  49. target_sources(pyigl PRIVATE "modules/py_igl_opengl_glfw.cpp")
  50. target_compile_definitions(pyigl PUBLIC -DPY_GLFW)
  51. target_link_libraries(pyigl PUBLIC igl::opengl igl::opengl_glfw)
  52. endif()
  53. if(LIBIGL_WITH_COMISO)
  54. target_sources(pyigl PRIVATE "modules/copyleft/py_igl_comiso.cpp")
  55. target_compile_definitions(pyigl PUBLIC -DPY_COMISO)
  56. target_link_libraries(pyigl PUBLIC igl::comiso)
  57. endif()
  58. if(LIBIGL_WITH_TETGEN)
  59. target_sources(pyigl PRIVATE "modules/copyleft/py_igl_tetgen.cpp")
  60. target_compile_definitions(pyigl PUBLIC -DPY_TETGEN)
  61. target_link_libraries(pyigl PUBLIC igl::tetgen)
  62. endif()
  63. if(LIBIGL_WITH_EMBREE)
  64. target_sources(pyigl PRIVATE "modules/py_igl_embree.cpp")
  65. target_compile_definitions(pyigl PUBLIC -DPY_EMBREE)
  66. target_link_libraries(pyigl PUBLIC igl::embree)
  67. endif()
  68. if(LIBIGL_WITH_TRIANGLE)
  69. target_sources(pyigl PRIVATE "modules/py_igl_triangle.cpp")
  70. target_compile_definitions(pyigl PUBLIC -DPY_TRIANGLE)
  71. target_link_libraries(pyigl PUBLIC igl::triangle)
  72. endif()
  73. if(LIBIGL_WITH_CGAL)
  74. target_sources(pyigl PRIVATE "modules/copyleft/py_igl_cgal.cpp")
  75. target_compile_definitions(pyigl PUBLIC -DPY_CGAL)
  76. target_link_libraries(pyigl PUBLIC igl::cgal)
  77. endif()
  78. if(NOT LIBIGL_WITHOUT_COPYLEFT)
  79. target_sources(pyigl PRIVATE "modules/copyleft/py_igl_copyleft.cpp")
  80. target_compile_definitions(pyigl PUBLIC -DPY_COPYLEFT)
  81. endif()
  82. if(LIBIGL_WITH_PNG)
  83. target_sources(pyigl PRIVATE "modules/py_igl_png.cpp")
  84. target_compile_definitions(pyigl PUBLIC -DPY_PNG)
  85. target_link_libraries(pyigl PUBLIC igl::png)
  86. endif()
  87. set_target_properties(pyigl PROPERTIES PREFIX "")
  88. set_target_properties(pyigl PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR})
  89. if(WIN32)
  90. if(MSVC)
  91. # Enforce size-based optimization and link time code generation on MSVC (~30% smaller binaries in experiments)
  92. set_target_properties(pyigl PROPERTIES COMPILE_FLAGS "/Os /GL")
  93. set_target_properties(pyigl PROPERTIES LINK_FLAGS "/LTCG")
  94. endif()
  95. # .PYD file extension on Windows
  96. set_target_properties(pyigl PROPERTIES SUFFIX ".pyd")
  97. # Link against the Python shared library
  98. # message(FATAL_ERROR ${PYTHON_LIBRARY})
  99. # target_link_libraries(igl ${PYTHON_LIBRARY})
  100. target_link_libraries(pyigl PRIVATE ${PYTHON_LIBRARIES})
  101. elseif(UNIX)
  102. # It's quite common to have multiple copies of the same Python version
  103. # installed on one's system. E.g.: one copy from the OS and another copy
  104. # that's statically linked into an application like Blender or Maya.
  105. # If we link our plugin library against the OS Python here and import it
  106. # into Blender or Maya later on, this will cause segfaults when multiple
  107. # conflicting Python instances are active at the same time.
  108. # Windows does not seem to be affected by this issue. The solution for Linux
  109. # and Mac OS is simple: we just don't link against the Python library. The
  110. # resulting shared library will have missing symbols, but that's perfectly
  111. # fine -- they will be resolved at import time.
  112. # .SO file extension on Linux/Mac OS
  113. set_target_properties(pyigl PROPERTIES SUFFIX ".so")
  114. # Enable flag if undefined symbols appear on pyigl module import to get notified about the missing symbols at link time
  115. option(LIBIGL_CHECK_UNDEFINED "Check for undefined symbols" OFF)
  116. # Strip unnecessary sections of the binary on Linux/Mac OS
  117. if(APPLE)
  118. set_target_properties(pyigl PROPERTIES MACOSX_RPATH ".")
  119. if(NOT LIBIGL_CHECK_UNDEFINED)
  120. set_target_properties(pyigl PROPERTIES LINK_FLAGS "-undefined dynamic_lookup -dead_strip")
  121. endif()
  122. if(NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
  123. add_custom_command(TARGET pyigl POST_BUILD COMMAND strip -u -r ${PROJECT_SOURCE_DIR}/pyigl.so)
  124. endif()
  125. else()
  126. if(LIBIGL_CHECK_UNDEFINED)
  127. target_link_libraries(pyigl PRIVATE ${PYTHON_LIBRARIES})
  128. set_target_properties(pyigl PROPERTIES LINK_FLAGS "-Wl,--no-undefined")
  129. endif()
  130. if(NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
  131. add_custom_command(TARGET pyigl POST_BUILD COMMAND strip ${PROJECT_SOURCE_DIR}/pyigl.so)
  132. endif()
  133. endif()
  134. endif()