From 8e331a4f0ed9da6f31c14adf82502d8c743b2628 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Sun, 11 Aug 2024 22:34:47 -0400 Subject: [PATCH 01/11] fix(cmake): add required emscripten flags Signed-off-by: Henry Schreiner --- .github/workflows/emscripten.yaml | 2 ++ tools/pybind11Common.cmake | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/.github/workflows/emscripten.yaml b/.github/workflows/emscripten.yaml index cbd7f5d541..ec1429481c 100644 --- a/.github/workflows/emscripten.yaml +++ b/.github/workflows/emscripten.yaml @@ -5,6 +5,8 @@ on: pull_request: branches: - master + - stable + - v* concurrency: group: ${{ github.workflow }}-${{ github.ref }} diff --git a/tools/pybind11Common.cmake b/tools/pybind11Common.cmake index 8467b45d2c..5ac680dcdb 100644 --- a/tools/pybind11Common.cmake +++ b/tools/pybind11Common.cmake @@ -75,6 +75,26 @@ set_property( APPEND PROPERTY INTERFACE_LINK_LIBRARIES pybind11::pybind11) +# -------------- emscripten requires exceptions enabled ------------- +# _pybind11_no_exceptions is a private mechanism to disable this addition. +# Please open an issue if you need to use it; it will be removed if no one +# needs it. +if(CMAKE_SYSTEM_PROCESSOR MATCHES emscripten AND NOT _pybind11_no_exceptions) + if(CMAKE_VERSION VERSION_LESS 3.13) + message(WARNING "CMake 3.13+ is required to build for Emscripten. Some flags will be missing") + else() + set_property( + TARGET pybind11::pybind11 + APPEND + PROPERTY INTERFACE_LINK_OPTIONS -fexceptions) + set_property( + TARGET pybind11::pybind11 + APPEND + PROPERTY INTERFACE_COMPILE_OPTIONS -fexceptions) + endif() + +endif() + # --------------------------- link helper --------------------------- add_library(pybind11::python_link_helper IMPORTED INTERFACE ${optional_global}) From d61a69b37f54cee4a5aa39ab5e1c057045ab4bd6 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 12 Aug 2024 09:05:32 -0400 Subject: [PATCH 02/11] Update emscripten.yaml --- .github/workflows/emscripten.yaml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/emscripten.yaml b/.github/workflows/emscripten.yaml index ec1429481c..14b2b9dc7d 100644 --- a/.github/workflows/emscripten.yaml +++ b/.github/workflows/emscripten.yaml @@ -25,8 +25,6 @@ jobs: - uses: pypa/cibuildwheel@v2.20 env: PYODIDE_BUILD_EXPORTS: whole_archive - CFLAGS: -fexceptions - LDFLAGS: -fexceptions with: package-dir: tests only: cp312-pyodide_wasm32 From 8692a12c8042ce9c13a4cc95091544005aaf7d3e Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 12 Aug 2024 09:36:11 -0400 Subject: [PATCH 03/11] fix(cmake): add required emscripten flags to headers target Signed-off-by: Henry Schreiner --- tools/pybind11Common.cmake | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tools/pybind11Common.cmake b/tools/pybind11Common.cmake index 5ac680dcdb..96a91a43e8 100644 --- a/tools/pybind11Common.cmake +++ b/tools/pybind11Common.cmake @@ -2,7 +2,7 @@ Adds the following targets:: - pybind11::pybind11 - link to headers and pybind11 + pybind11::pybind11 - link to Python headers and pybind11::headers pybind11::module - Adds module links pybind11::embed - Adds embed links pybind11::lto - Link time optimizations (only if CMAKE_INTERPROCEDURAL_OPTIMIZATION is not set) @@ -84,15 +84,14 @@ if(CMAKE_SYSTEM_PROCESSOR MATCHES emscripten AND NOT _pybind11_no_exceptions) message(WARNING "CMake 3.13+ is required to build for Emscripten. Some flags will be missing") else() set_property( - TARGET pybind11::pybind11 + TARGET pybind11::headers APPEND PROPERTY INTERFACE_LINK_OPTIONS -fexceptions) set_property( - TARGET pybind11::pybind11 + TARGET pybind11::headers APPEND PROPERTY INTERFACE_COMPILE_OPTIONS -fexceptions) endif() - endif() # --------------------------- link helper --------------------------- From 7f8287d5a148671534ac3990c5397bb81cd85af9 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 12 Aug 2024 10:57:07 -0400 Subject: [PATCH 04/11] fix(cmake): incorrect detection of Emscripten Signed-off-by: Henry Schreiner --- tools/pybind11Common.cmake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/pybind11Common.cmake b/tools/pybind11Common.cmake index 96a91a43e8..1a42ee7db6 100644 --- a/tools/pybind11Common.cmake +++ b/tools/pybind11Common.cmake @@ -79,7 +79,7 @@ set_property( # _pybind11_no_exceptions is a private mechanism to disable this addition. # Please open an issue if you need to use it; it will be removed if no one # needs it. -if(CMAKE_SYSTEM_PROCESSOR MATCHES emscripten AND NOT _pybind11_no_exceptions) +if(CMAKE_SYSTEM_NAME MATCHES Emscripten AND NOT _pybind11_no_exceptions) if(CMAKE_VERSION VERSION_LESS 3.13) message(WARNING "CMake 3.13+ is required to build for Emscripten. Some flags will be missing") else() @@ -348,7 +348,7 @@ function(_pybind11_generate_lto target prefer_thin_lto) if(CMAKE_SYSTEM_PROCESSOR MATCHES "ppc64le" OR CMAKE_SYSTEM_PROCESSOR MATCHES "mips64") # Do nothing - elseif(CMAKE_SYSTEM_PROCESSOR MATCHES emscripten) + elseif(CMAKE_SYSTEM_NAME MATCHES Emscripten) # This compile is very costly when cross-compiling, so set this without checking set(PYBIND11_LTO_CXX_FLAGS "-flto${thin}${cxx_append}") set(PYBIND11_LTO_LINKER_FLAGS "-flto${thin}${linker_append}") From 613fb0f25f6db54cfe7f63b6f6f1b9b870331d92 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 12 Aug 2024 11:12:28 -0400 Subject: [PATCH 05/11] fix(cmake): allow pybind11::headers to be modified Signed-off-by: Henry Schreiner --- CMakeLists.txt | 3 ++- tools/pybind11Common.cmake | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6e5b8c8f31..ea14ee2dcb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -229,7 +229,8 @@ if(NOT TARGET pybind11_headers) # (long name used here to keep this from clashing in subdirectory mode) add_library(pybind11_headers INTERFACE) add_library(pybind11::pybind11_headers ALIAS pybind11_headers) # to match exported target - add_library(pybind11::headers ALIAS pybind11_headers) # easier to use/remember + add_library(pybind11::headers IMPORTED INTERFACE) + set_target_properties(pybind11::headers PROPERTIES INTERFACE_LINK_LIBRARIES pybind11_headers) target_include_directories( pybind11_headers ${pybind11_system} INTERFACE $ diff --git a/tools/pybind11Common.cmake b/tools/pybind11Common.cmake index 1a42ee7db6..0cbec2b2b3 100644 --- a/tools/pybind11Common.cmake +++ b/tools/pybind11Common.cmake @@ -30,7 +30,7 @@ endif() # which might be simpler than this check. get_property( is_config - TARGET pybind11::headers + TARGET pybind11::pybind11_headers PROPERTY IMPORTED) if(NOT is_config) set(optional_global GLOBAL) From 6a3ca2d998835f2b0a8359e208c85a13706b05ce Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 12 Aug 2024 11:48:16 -0400 Subject: [PATCH 06/11] fix(cmake): hide a warning when building the tests standalone Signed-off-by: Henry Schreiner --- tests/pyproject.toml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/pyproject.toml b/tests/pyproject.toml index 469c145dfd..044bf15c05 100644 --- a/tests/pyproject.toml +++ b/tests/pyproject.toml @@ -10,6 +10,10 @@ name = "pybind11_tests" version = "0.0.1" dependencies = ["pytest", "pytest-timeout", "numpy", "scipy"] +[tool.scikit-build] +# Hide a warning while we also support CMake < 3.15 +cmake.version = ">=3.15" + [tool.scikit-build.cmake.define] PYBIND11_FINDPYTHON = true From 3a3ab40bae0f0e25ac7b28b227e9b311f89c7afa Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 12 Aug 2024 14:14:42 -0400 Subject: [PATCH 07/11] fix(cmake): use explicit variable for is config Signed-off-by: Henry Schreiner --- CMakeLists.txt | 1 + tools/pybind11Common.cmake | 6 +----- tools/pybind11Config.cmake.in | 4 ++-- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ea14ee2dcb..81e0d85987 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -247,6 +247,7 @@ else() set(PYBIND11_INSTALL OFF) endif() +set(_pybind11_is_config OFF) include("${CMAKE_CURRENT_SOURCE_DIR}/tools/pybind11Common.cmake") # https://github.com/jtojnar/cmake-snips/#concatenating-paths-when-building-pkg-config-files # TODO: cmake 3.20 adds the cmake_path() function, which obsoletes this snippet diff --git a/tools/pybind11Common.cmake b/tools/pybind11Common.cmake index 0cbec2b2b3..d8ed0c8878 100644 --- a/tools/pybind11Common.cmake +++ b/tools/pybind11Common.cmake @@ -28,11 +28,7 @@ endif() # are in CONFIG mode, they should be "normal" targets instead. # In CMake 3.11+ you can promote a target to global after you create it, # which might be simpler than this check. -get_property( - is_config - TARGET pybind11::pybind11_headers - PROPERTY IMPORTED) -if(NOT is_config) +if(NOT _pybind11_is_config) set(optional_global GLOBAL) endif() diff --git a/tools/pybind11Config.cmake.in b/tools/pybind11Config.cmake.in index 304f1d9077..1f73dde34f 100644 --- a/tools/pybind11Config.cmake.in +++ b/tools/pybind11Config.cmake.in @@ -84,7 +84,7 @@ you can either use the basic targets, or use the FindPython tools: # Python method: Python_add_library(MyModule2 src2.cpp) - target_link_libraries(MyModule2 pybind11::headers) + target_link_libraries(MyModule2 PUBLIC pybind11::headers) set_target_properties(MyModule2 PROPERTIES INTERPROCEDURAL_OPTIMIZATION ON CXX_VISIBILITY_PRESET ON @@ -222,7 +222,7 @@ include("${CMAKE_CURRENT_LIST_DIR}/pybind11Targets.cmake") add_library(pybind11::headers IMPORTED INTERFACE) set_target_properties(pybind11::headers PROPERTIES INTERFACE_LINK_LIBRARIES pybind11::pybind11_headers) - +set(_pybind11_is_config ON) include("${CMAKE_CURRENT_LIST_DIR}/pybind11Common.cmake") if(NOT pybind11_FIND_QUIETLY) From f7951b9b5e25551a39db3df8266522eede7481a0 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 12 Aug 2024 14:32:31 -0400 Subject: [PATCH 08/11] fix(cmake): go back to ALIAS target Signed-off-by: Henry Schreiner --- CMakeLists.txt | 3 +-- tools/pybind11Common.cmake | 27 +++++++++++++++++++-------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 81e0d85987..359d510292 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -229,8 +229,7 @@ if(NOT TARGET pybind11_headers) # (long name used here to keep this from clashing in subdirectory mode) add_library(pybind11_headers INTERFACE) add_library(pybind11::pybind11_headers ALIAS pybind11_headers) # to match exported target - add_library(pybind11::headers IMPORTED INTERFACE) - set_target_properties(pybind11::headers PROPERTIES INTERFACE_LINK_LIBRARIES pybind11_headers) + add_library(pybind11::headers ALIAS pybind11_headers) target_include_directories( pybind11_headers ${pybind11_system} INTERFACE $ diff --git a/tools/pybind11Common.cmake b/tools/pybind11Common.cmake index d8ed0c8878..48c09e2c4e 100644 --- a/tools/pybind11Common.cmake +++ b/tools/pybind11Common.cmake @@ -79,14 +79,25 @@ if(CMAKE_SYSTEM_NAME MATCHES Emscripten AND NOT _pybind11_no_exceptions) if(CMAKE_VERSION VERSION_LESS 3.13) message(WARNING "CMake 3.13+ is required to build for Emscripten. Some flags will be missing") else() - set_property( - TARGET pybind11::headers - APPEND - PROPERTY INTERFACE_LINK_OPTIONS -fexceptions) - set_property( - TARGET pybind11::headers - APPEND - PROPERTY INTERFACE_COMPILE_OPTIONS -fexceptions) + if(_pybind11_is_config) + set_property( + TARGET pybind11::pybind11_headers + APPEND + PROPERTY INTERFACE_LINK_OPTIONS -fexceptions) + set_property( + TARGET pybind11::pybind11_headers + APPEND + PROPERTY INTERFACE_COMPILE_OPTIONS -fexceptions) + else() + set_property( + TARGET pybind11_headers + APPEND + PROPERTY INTERFACE_LINK_OPTIONS -fexceptions) + set_property( + TARGET pybind11_headers + APPEND + PROPERTY INTERFACE_COMPILE_OPTIONS -fexceptions) + endif() endif() endif() From 1223a98289a6bcd091678f9b42d54c893357d0ac Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 12 Aug 2024 14:40:36 -0400 Subject: [PATCH 09/11] chore: reduce overall diff Signed-off-by: Henry Schreiner --- CMakeLists.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 359d510292..6e5b8c8f31 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -229,7 +229,7 @@ if(NOT TARGET pybind11_headers) # (long name used here to keep this from clashing in subdirectory mode) add_library(pybind11_headers INTERFACE) add_library(pybind11::pybind11_headers ALIAS pybind11_headers) # to match exported target - add_library(pybind11::headers ALIAS pybind11_headers) + add_library(pybind11::headers ALIAS pybind11_headers) # easier to use/remember target_include_directories( pybind11_headers ${pybind11_system} INTERFACE $ @@ -246,7 +246,6 @@ else() set(PYBIND11_INSTALL OFF) endif() -set(_pybind11_is_config OFF) include("${CMAKE_CURRENT_SOURCE_DIR}/tools/pybind11Common.cmake") # https://github.com/jtojnar/cmake-snips/#concatenating-paths-when-building-pkg-config-files # TODO: cmake 3.20 adds the cmake_path() function, which obsoletes this snippet From 9c89f7a681c387a0a5af99de5fcf791e414aa7d8 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 12 Aug 2024 14:40:42 -0400 Subject: [PATCH 10/11] chore: reduce overall diff Signed-off-by: Henry Schreiner --- tools/pybind11Common.cmake | 8 ++++++-- tools/pybind11Config.cmake.in | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/tools/pybind11Common.cmake b/tools/pybind11Common.cmake index 48c09e2c4e..5c4f5c1cdf 100644 --- a/tools/pybind11Common.cmake +++ b/tools/pybind11Common.cmake @@ -28,7 +28,11 @@ endif() # are in CONFIG mode, they should be "normal" targets instead. # In CMake 3.11+ you can promote a target to global after you create it, # which might be simpler than this check. -if(NOT _pybind11_is_config) +get_property( + is_config + TARGET pybind11::headers + PROPERTY IMPORTED) +if(NOT is_config) set(optional_global GLOBAL) endif() @@ -79,7 +83,7 @@ if(CMAKE_SYSTEM_NAME MATCHES Emscripten AND NOT _pybind11_no_exceptions) if(CMAKE_VERSION VERSION_LESS 3.13) message(WARNING "CMake 3.13+ is required to build for Emscripten. Some flags will be missing") else() - if(_pybind11_is_config) + if(_is_config) set_property( TARGET pybind11::pybind11_headers APPEND diff --git a/tools/pybind11Config.cmake.in b/tools/pybind11Config.cmake.in index 1f73dde34f..2d9fa94f67 100644 --- a/tools/pybind11Config.cmake.in +++ b/tools/pybind11Config.cmake.in @@ -222,7 +222,7 @@ include("${CMAKE_CURRENT_LIST_DIR}/pybind11Targets.cmake") add_library(pybind11::headers IMPORTED INTERFACE) set_target_properties(pybind11::headers PROPERTIES INTERFACE_LINK_LIBRARIES pybind11::pybind11_headers) -set(_pybind11_is_config ON) + include("${CMAKE_CURRENT_LIST_DIR}/pybind11Common.cmake") if(NOT pybind11_FIND_QUIETLY) From 7273ca5844918fcfceda257ccd21fefd59b7412b Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 12 Aug 2024 14:48:36 -0400 Subject: [PATCH 11/11] chore: shorten code a bit Signed-off-by: Henry Schreiner --- tools/pybind11Common.cmake | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/tools/pybind11Common.cmake b/tools/pybind11Common.cmake index 5c4f5c1cdf..585ed3d213 100644 --- a/tools/pybind11Common.cmake +++ b/tools/pybind11Common.cmake @@ -84,24 +84,20 @@ if(CMAKE_SYSTEM_NAME MATCHES Emscripten AND NOT _pybind11_no_exceptions) message(WARNING "CMake 3.13+ is required to build for Emscripten. Some flags will be missing") else() if(_is_config) - set_property( - TARGET pybind11::pybind11_headers - APPEND - PROPERTY INTERFACE_LINK_OPTIONS -fexceptions) - set_property( - TARGET pybind11::pybind11_headers - APPEND - PROPERTY INTERFACE_COMPILE_OPTIONS -fexceptions) + set(_tmp_config_target pybind11::pybind11_headers) else() - set_property( - TARGET pybind11_headers - APPEND - PROPERTY INTERFACE_LINK_OPTIONS -fexceptions) - set_property( - TARGET pybind11_headers - APPEND - PROPERTY INTERFACE_COMPILE_OPTIONS -fexceptions) + set(_tmp_config_target pybind11_headers) endif() + + set_property( + TARGET ${_tmp_config_target} + APPEND + PROPERTY INTERFACE_LINK_OPTIONS -fexceptions) + set_property( + TARGET ${_tmp_config_target} + APPEND + PROPERTY INTERFACE_COMPILE_OPTIONS -fexceptions) + unset(_tmp_config_target) endif() endif()