Skip to content

Commit

Permalink
[pybind11] Fixed compiling errors for pybind11 `-Wmissing-field-initi…
Browse files Browse the repository at this point in the history
…alizers` under gcc8 and python312 (PaddlePaddle#63949)
  • Loading branch information
gouzil authored Apr 29, 2024
1 parent 4b1e930 commit 898ecab
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
18 changes: 18 additions & 0 deletions cmake/external/pybind11.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@ set(SOURCE_INCLUDE_DIR ${SOURCE_DIR}/include)

include_directories(${PYBIND_INCLUDE_DIR})

# It can be safely removed in gcc9.1+
set(PYBIND_PATCH_COMMAND "")
if(LINUX
AND (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9)
set(PYBIND_TAG v2.12.0)
file(TO_NATIVE_PATH
${PADDLE_SOURCE_DIR}/patches/pybind/detail/internals.h.patch native_dst)
# Note: [Why calling some `git` commands before `patch`?]
# Paddle's CI uses cache to accelerate 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/detail < ${native_dst})
endif()

ExternalProject_Add(
extern_pybind
${EXTERNAL_PROJECT_LOG_ARGS} ${SHALLOW_CLONE}
Expand All @@ -33,6 +50,7 @@ ExternalProject_Add(
# third-party library version changes cannot be incorporated.
# reference: https://cmake.org/cmake/help/latest/module/ExternalProject.html
UPDATE_COMMAND ""
PATCH_COMMAND ${PYBIND_PATCH_COMMAND}
CONFIGURE_COMMAND ""
# I intentionally preserved an extern_pybind/include/pybind11 directory
# to site-packages, so that you could discern that you intended to
Expand Down
22 changes: 22 additions & 0 deletions patches/pybind/detail/internals.h.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
diff --git a/include/pybind11/detail/internals.h b/include/pybind11/detail/internals.h
index c1047e4a..e09a6495 100644
--- a/include/pybind11/detail/internals.h
+++ b/include/pybind11/detail/internals.h
@@ -193,11 +193,18 @@ struct internals {
PyTypeObject *default_metaclass;
PyObject *instance_base;
#if defined(WITH_THREAD)
+#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ <= 8
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
+#endif
// Unused if PYBIND11_SIMPLE_GIL_MANAGEMENT is defined:
PYBIND11_TLS_KEY_INIT(tstate)
# if PYBIND11_INTERNALS_VERSION > 4
PYBIND11_TLS_KEY_INIT(loader_life_support_tls_key)
# endif // PYBIND11_INTERNALS_VERSION > 4
+#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ <= 8
+#pragma GCC diagnostic pop
+#endif
// Unused if PYBIND11_SIMPLE_GIL_MANAGEMENT is defined:
PyInterpreterState *istate = nullptr;

0 comments on commit 898ecab

Please sign in to comment.