pybind11.cmake 2.79 KB
Newer Older
yuguo960516yuguo's avatar
yuguo960516yuguo committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

include(ExternalProject)

set(PYBIND_PREFIX_DIR ${THIRD_PARTY_PATH}/pybind)
“yuguo”'s avatar
2.5  
“yuguo” committed
18
19
20
21
22
set(PYBIND_SOURCE_DIR ${PYBIND_PREFIX_DIR}/src/extern_pybind)
set(PYBIND_INCLUDE_DIR ${PYBIND_SOURCE_DIR}/include)
set(PYBIND_TAG v2.10.3)
set(SOURCE_DIR ${PADDLE_SOURCE_DIR}/third_party/pybind)
set(SOURCE_INCLUDE_DIR ${SOURCE_DIR}/include)
yuguo960516yuguo's avatar
yuguo960516yuguo committed
23
24
25

include_directories(${PYBIND_INCLUDE_DIR})

“yuguo”'s avatar
2.5  
“yuguo” committed
26
27
28
29
30
31
32
33
34
35
36
37
38
set(PYBIND_PATCH_COMMAND "")
if(NOT WIN32)
  file(TO_NATIVE_PATH ${PADDLE_SOURCE_DIR}/patches/pybind/cast.h.patch
       native_dst)
  # Note: [Why calling some `git` commands before `patch`?]
  # Paddle's CI uses cache to accelarate the make process. However, error might raise when patch codes in two scenarios:
  # 1. Patch to the wrong version: the tag version of CI's cache falls behind PYBIND_TAG, use `git checkout ${PYBIND_TAG}` to solve this.
  # 2. Patch twice: the tag version of cache == PYBIND_TAG, but patch has already applied to cache.
  set(PYBIND_PATCH_COMMAND
      git checkout -- . && git checkout ${PYBIND_TAG} && patch -Nd
      ${SOURCE_INCLUDE_DIR}/pybind11 < ${native_dst})
endif()

yuguo960516yuguo's avatar
yuguo960516yuguo committed
39
40
41
ExternalProject_Add(
  extern_pybind
  ${EXTERNAL_PROJECT_LOG_ARGS} ${SHALLOW_CLONE}
“yuguo”'s avatar
2.5  
“yuguo” committed
42
  SOURCE_DIR ${SOURCE_DIR}
yuguo960516yuguo's avatar
yuguo960516yuguo committed
43
44
45
46
47
48
49
  PREFIX ${PYBIND_PREFIX_DIR}
  # If we explicitly leave the `UPDATE_COMMAND` of the ExternalProject_Add
  # function in CMakeLists blank, it will cause another parameter GIT_TAG
  # to be modified without triggering incremental compilation, and the
  # third-party library version changes cannot be incorporated.
  # reference: https://cmake.org/cmake/help/latest/module/ExternalProject.html
  UPDATE_COMMAND ""
“yuguo”'s avatar
2.5  
“yuguo” committed
50
  PATCH_COMMAND ${PYBIND_PATCH_COMMAND}
yuguo960516yuguo's avatar
yuguo960516yuguo committed
51
  CONFIGURE_COMMAND ""
“yuguo”'s avatar
2.5  
“yuguo” committed
52
53
54
55
56
57
58
59
  # I intentionally preserved an extern_pybind/include/pybind11 directory
  # to site-packages, so that you could discern that you intended to
  # employ not only python, but also CPP and were ready to incorporate header files.
  BUILD_COMMAND
  COMMAND ${CMAKE_COMMAND} -E remove_directory ${PYBIND_SOURCE_DIR}
  COMMAND ${CMAKE_COMMAND} -E make_directory ${PYBIND_SOURCE_DIR}
  COMMAND ${CMAKE_COMMAND} -E copy_directory ${SOURCE_INCLUDE_DIR}
          ${PYBIND_INCLUDE_DIR}
yuguo960516yuguo's avatar
yuguo960516yuguo committed
60
61
62
63
64
65
  INSTALL_COMMAND ""
  TEST_COMMAND "")

add_library(pybind INTERFACE)

add_dependencies(pybind extern_pybind)