Skip to content

Commit c80e059

Browse files
fix: Mingw64 corrected and add a CI job to test it (#3132)
* mingw64 platform string is like mingw_xxx not "mingw" See https://github.com/msys2/MINGW-packages/blob/master/mingw-w64-python/0099-Change-the-get_platform-method-in-sysconfig-and-dist.patch * Mingw: Do not dllexport exceptions This is a fix for errors like: D:/a/pybind11/pybind11/include/pybind11/detail/common.h:735:23: error: 'dllexport' implies default visibility, but 'class pybind11::builtin_exception' has already been declared with a different visibility 735 | class PYBIND11_EXPORT builtin_exception : public std::runtime_error { | ^~~~~~~~~~~~~~~~~ * GHA: Test Mingw64 build * fix: avoid thin binaries on mingw * fix: drop lto on MinGW * Mingw64: disable PYBIND11_DEPRECATED It trigger many warnings for unknown reasons Co-authored-by: Henry Schreiner <[email protected]>
1 parent 46c51fc commit c80e059

File tree

6 files changed

+57
-8
lines changed

6 files changed

+57
-8
lines changed

.github/workflows/ci.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,3 +859,34 @@ jobs:
859859

860860
- name: Run all checks
861861
run: cmake --build build -t check
862+
863+
mingw:
864+
runs-on: windows-latest
865+
defaults:
866+
run:
867+
shell: msys2 {0}
868+
steps:
869+
- uses: msys2/setup-msys2@v2
870+
with:
871+
install: >-
872+
mingw-w64-x86_64-gcc
873+
mingw-w64-x86_64-python-pip
874+
mingw-w64-x86_64-cmake
875+
mingw-w64-x86_64-make
876+
mingw-w64-x86_64-python-pytest
877+
mingw-w64-x86_64-eigen3
878+
mingw-w64-x86_64-boost
879+
mingw-w64-x86_64-catch
880+
881+
- uses: actions/checkout@v1
882+
883+
- name: Configure
884+
# LTO leads to many undefined reference like
885+
# `pybind11::detail::function_call::function_call(pybind11::detail::function_call&&)
886+
run: cmake -G "MinGW Makefiles" -S . -B build
887+
888+
- name: Build
889+
run: cmake --build build -j 2
890+
891+
- name: Python tests
892+
run: cmake --build build --target pytest

include/pybind11/detail/common.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,27 @@
8989
# endif
9090
#endif
9191

92+
#if !defined(PYBIND11_EXPORT_EXCEPTION)
93+
# ifdef __MINGW32__
94+
// workaround for:
95+
// error: 'dllexport' implies default visibility, but xxx has already been declared with a different visibility
96+
# define PYBIND11_EXPORT_EXCEPTION
97+
# else
98+
# define PYBIND11_EXPORT_EXCEPTION PYBIND11_EXPORT
99+
# endif
100+
#endif
101+
92102
#if defined(_MSC_VER)
93103
# define PYBIND11_NOINLINE __declspec(noinline)
94104
#else
95105
# define PYBIND11_NOINLINE __attribute__ ((noinline))
96106
#endif
97107

98-
#if defined(PYBIND11_CPP14)
108+
#if defined(__MINGW32__)
109+
// For unknown reasons all PYBIND11_DEPRECATED member trigger a warning when declared
110+
// whether it is used or not
111+
# define PYBIND11_DEPRECATED(reason)
112+
#elif defined(PYBIND11_CPP14)
99113
# define PYBIND11_DEPRECATED(reason) [[deprecated(reason)]]
100114
#else
101115
# define PYBIND11_DEPRECATED(reason) __attribute__((deprecated(reason)))
@@ -740,7 +754,7 @@ PYBIND11_NAMESPACE_END(detail)
740754
# pragma warning(disable: 4275) // warning C4275: An exported class was derived from a class that wasn't exported. Can be ignored when derived from a STL class.
741755
#endif
742756
/// C++ bindings of builtin Python exceptions
743-
class PYBIND11_EXPORT builtin_exception : public std::runtime_error {
757+
class PYBIND11_EXPORT_EXCEPTION builtin_exception : public std::runtime_error {
744758
public:
745759
using std::runtime_error::runtime_error;
746760
/// Set the error using the Python C API
@@ -751,7 +765,7 @@ class PYBIND11_EXPORT builtin_exception : public std::runtime_error {
751765
#endif
752766

753767
#define PYBIND11_RUNTIME_EXCEPTION(name, type) \
754-
class PYBIND11_EXPORT name : public builtin_exception { public: \
768+
class PYBIND11_EXPORT_EXCEPTION name : public builtin_exception { public: \
755769
using builtin_exception::builtin_exception; \
756770
name() : name("") { } \
757771
void set_error() const override { PyErr_SetString(type, what()); } \

include/pybind11/pytypes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ PYBIND11_NAMESPACE_END(detail)
327327
/// thrown to propagate python-side errors back through C++ which can either be caught manually or
328328
/// else falls back to the function dispatcher (which then raises the captured error back to
329329
/// python).
330-
class PYBIND11_EXPORT error_already_set : public std::runtime_error {
330+
class PYBIND11_EXPORT_EXCEPTION error_already_set : public std::runtime_error {
331331
public:
332332
/// Constructs a new exception from the current Python error indicator, if any. The current
333333
/// Python error indicator will be cleared.

pybind11/setup_helpers.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@
5959
import distutils.errors
6060
import distutils.ccompiler
6161

62-
63-
WIN = sys.platform.startswith("win32") and sysconfig.get_platform() != "mingw"
62+
WIN = sys.platform.startswith("win32") and "mingw" not in sysconfig.get_platform()
6463
PY2 = sys.version_info[0] < 3
6564
MACOS = sys.platform.startswith("darwin")
6665
STD_TMPL = "/std:c++{}" if WIN else "-std=c++{}"

tests/test_exceptions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
// shared exceptions for cross_module_tests
66

7-
class PYBIND11_EXPORT shared_exception : public pybind11::builtin_exception {
7+
class PYBIND11_EXPORT_EXCEPTION shared_exception : public pybind11::builtin_exception {
88
public:
99
using builtin_exception::builtin_exception;
1010
explicit shared_exception() : shared_exception("") {}

tools/pybind11Common.cmake

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,18 @@ function(_pybind11_return_if_cxx_and_linker_flags_work result cxxflags linkerfla
302302
endfunction()
303303

304304
function(_pybind11_generate_lto target prefer_thin_lto)
305+
if(MINGW)
306+
message(STATUS "${target} disabled (problems with undefined symbols for MinGW for now)")
307+
return()
308+
endif()
309+
305310
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
306311
set(cxx_append "")
307312
set(linker_append "")
308313
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND NOT APPLE)
309314
# Clang Gold plugin does not support -Os; append -O3 to MinSizeRel builds to override it
310315
set(linker_append ";$<$<CONFIG:MinSizeRel>:-O3>")
311-
elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
316+
elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND NOT MINGW)
312317
set(cxx_append ";-fno-fat-lto-objects")
313318
endif()
314319

0 commit comments

Comments
 (0)