Skip to content

Commit fc59f4e

Browse files
authored
fix(cmake): add required emscripten flags (#5298)
* fix(cmake): add required emscripten flags Signed-off-by: Henry Schreiner <[email protected]> * Update emscripten.yaml * fix(cmake): add required emscripten flags to headers target Signed-off-by: Henry Schreiner <[email protected]> * fix(cmake): incorrect detection of Emscripten Signed-off-by: Henry Schreiner <[email protected]> * fix(cmake): allow pybind11::headers to be modified Signed-off-by: Henry Schreiner <[email protected]> * fix(cmake): hide a warning when building the tests standalone Signed-off-by: Henry Schreiner <[email protected]> * fix(cmake): use explicit variable for is config Signed-off-by: Henry Schreiner <[email protected]> * fix(cmake): go back to ALIAS target Signed-off-by: Henry Schreiner <[email protected]> * chore: reduce overall diff Signed-off-by: Henry Schreiner <[email protected]> * chore: reduce overall diff Signed-off-by: Henry Schreiner <[email protected]> * chore: shorten code a bit Signed-off-by: Henry Schreiner <[email protected]> --------- Signed-off-by: Henry Schreiner <[email protected]>
1 parent 8987944 commit fc59f4e

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

.github/workflows/emscripten.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ on:
55
pull_request:
66
branches:
77
- master
8+
- stable
9+
- v*
810

911
concurrency:
1012
group: ${{ github.workflow }}-${{ github.ref }}
@@ -23,8 +25,6 @@ jobs:
2325
- uses: pypa/[email protected]
2426
env:
2527
PYODIDE_BUILD_EXPORTS: whole_archive
26-
CFLAGS: -fexceptions
27-
LDFLAGS: -fexceptions
2828
with:
2929
package-dir: tests
3030
only: cp312-pyodide_wasm32

tests/pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ name = "pybind11_tests"
1010
version = "0.0.1"
1111
dependencies = ["pytest", "pytest-timeout", "numpy", "scipy"]
1212

13+
[tool.scikit-build]
14+
# Hide a warning while we also support CMake < 3.15
15+
cmake.version = ">=3.15"
16+
1317
[tool.scikit-build.cmake.define]
1418
PYBIND11_FINDPYTHON = true
1519

tools/pybind11Common.cmake

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
Adds the following targets::
44
5-
pybind11::pybind11 - link to headers and pybind11
5+
pybind11::pybind11 - link to Python headers and pybind11::headers
66
pybind11::module - Adds module links
77
pybind11::embed - Adds embed links
88
pybind11::lto - Link time optimizations (only if CMAKE_INTERPROCEDURAL_OPTIMIZATION is not set)
@@ -75,6 +75,32 @@ set_property(
7575
APPEND
7676
PROPERTY INTERFACE_LINK_LIBRARIES pybind11::pybind11)
7777

78+
# -------------- emscripten requires exceptions enabled -------------
79+
# _pybind11_no_exceptions is a private mechanism to disable this addition.
80+
# Please open an issue if you need to use it; it will be removed if no one
81+
# needs it.
82+
if(CMAKE_SYSTEM_NAME MATCHES Emscripten AND NOT _pybind11_no_exceptions)
83+
if(CMAKE_VERSION VERSION_LESS 3.13)
84+
message(WARNING "CMake 3.13+ is required to build for Emscripten. Some flags will be missing")
85+
else()
86+
if(_is_config)
87+
set(_tmp_config_target pybind11::pybind11_headers)
88+
else()
89+
set(_tmp_config_target pybind11_headers)
90+
endif()
91+
92+
set_property(
93+
TARGET ${_tmp_config_target}
94+
APPEND
95+
PROPERTY INTERFACE_LINK_OPTIONS -fexceptions)
96+
set_property(
97+
TARGET ${_tmp_config_target}
98+
APPEND
99+
PROPERTY INTERFACE_COMPILE_OPTIONS -fexceptions)
100+
unset(_tmp_config_target)
101+
endif()
102+
endif()
103+
78104
# --------------------------- link helper ---------------------------
79105

80106
add_library(pybind11::python_link_helper IMPORTED INTERFACE ${optional_global})
@@ -329,7 +355,7 @@ function(_pybind11_generate_lto target prefer_thin_lto)
329355

330356
if(CMAKE_SYSTEM_PROCESSOR MATCHES "ppc64le" OR CMAKE_SYSTEM_PROCESSOR MATCHES "mips64")
331357
# Do nothing
332-
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES emscripten)
358+
elseif(CMAKE_SYSTEM_NAME MATCHES Emscripten)
333359
# This compile is very costly when cross-compiling, so set this without checking
334360
set(PYBIND11_LTO_CXX_FLAGS "-flto${thin}${cxx_append}")
335361
set(PYBIND11_LTO_LINKER_FLAGS "-flto${thin}${linker_append}")

tools/pybind11Config.cmake.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ you can either use the basic targets, or use the FindPython tools:
8484
8585
# Python method:
8686
Python_add_library(MyModule2 src2.cpp)
87-
target_link_libraries(MyModule2 pybind11::headers)
87+
target_link_libraries(MyModule2 PUBLIC pybind11::headers)
8888
set_target_properties(MyModule2 PROPERTIES
8989
INTERPROCEDURAL_OPTIMIZATION ON
9090
CXX_VISIBILITY_PRESET ON

0 commit comments

Comments
 (0)