CMakeLists.txt 5.4 KB

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