File: FindPyBind11.cmake

package info (click to toggle)
stopt 6.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,264 kB
  • sloc: cpp: 75,778; python: 6,012; makefile: 72; sh: 57
file content (82 lines) | stat: -rw-r--r-- 3,120 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# findpybind11.cmake


FIND_PATH(PYBIND11_INCLUDE_DIR
  NAMES pybind11/pybind11.h
  PATHS
    ${PYBIND11_ROOT}
    ${PYBIND11_ROOT}/include
    ${PYBIND11_INCLUDE_DIR}
    ${PYBIND11_INCLUDE_DIRS}
    )

MARK_AS_ADVANCED(
  PYBIND11_INCLUDE_DIR
  )

SET(PYBIND11_INCLUDE_DIRS "${PYBIND11_INCLUDE_DIR}")
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(PyBind11  DEFAULT_MSG   PYBIND11_INCLUDE_DIRS)


INCLUDE_DIRECTORIES(SYSTEM ${PYBIND11_INCLUDE_DIR})


FUNCTION(PYBIND11_ADD_MODULE _NAME)
  add_library( ${_NAME} ${ARGN})

  # Don't add a 'lib' prefix to the shared library
  set_target_properties(${_NAME} PROPERTIES PREFIX "")

  if (WIN32)
    if (MSVC)
      # Enforce size-based optimization and link time code generation
      # on MSVC (~30% smaller binaries in experiments). /bigobj is needed
      # for bigger binding projects due to the limit to 64k addressable sections
      # /MP enables multithreaded builds (relevant when there are many files).
      set_target_properties(${_NAME} PROPERTIES COMPILE_FLAGS "/Os /GL /MP /bigobj")
      set_target_properties(${_NAME} PROPERTIES LINK_FLAGS "/LTCG")
    endif()

    # .PYD file extension on Windows
    set_target_properties(${_NAME} PROPERTIES SUFFIX ".pyd")

    # Link against the Python shared library
    #target_link_libraries(example ${PYTHON_LIBRARY})
  elseif (UNIX)
    # It's quite common to have multiple copies of the same Python version
    # installed on one's system. E.g.: one copy from the OS and another copy
    # that's statically linked into an application like Blender or Maya.
    # If we link our plugin library against the OS Python here and import it
    # into Blender or Maya later on, this will cause segfaults when multiple
    # conflicting Python instances are active at the same time (even when they
    # are of the same version).
  
    # Windows is not affected by this issue since it handles DLL imports 
    # differently. The solution for Linux and Mac OS is simple: we just don't
    # link against the Python library. The resulting shared library will have
    # missing symbols, but that's perfectly fine -- they will be resolved at
    # import time.

    # .SO file extension on Linux/Mac OS
    set_target_properties(${_NAME} PROPERTIES SUFFIX ".so")

    # Optimize for a small binary size
    if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
      set_target_properties(${_NAME} PROPERTIES COMPILE_FLAGS "-Os")
    endif()

    # Strip unnecessary sections of the binary on Linux/Mac OS
    # if(APPLE)
    #   set_target_properties(${_NAME} PROPERTIES MACOSX_RPATH ".")
    #   set_target_properties(${_NAME} PROPERTIES LINK_FLAGS "-undefined dynamic_lookup ")
    #   if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
    #     add_custom_command(TARGET ${_NAME} POST_BUILD COMMAND strip -u -r ${PROJECT_SOURCE_DIR}/${_NAME}/${_NAME}.so)
    #   endif()
    # else()
    #   if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
    #     add_custom_command(TARGET ${_NAME} POST_BUILD COMMAND strip ${PROJECT_SOURCE_DIR}/${_NAME}/${_NAME}.so)
    #   endif()
    # endif()
  endif()
endfunction()