From 7e2f90e100897536191e5cd053bc049b2b71e3b1 Mon Sep 17 00:00:00 2001 From: Jerome Robert Date: Sun, 18 Jul 2021 13:37:23 +0200 Subject: [PATCH 1/6] 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 --- pybind11/setup_helpers.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pybind11/setup_helpers.py b/pybind11/setup_helpers.py index 2b27f1f5ad..0888ab4872 100644 --- a/pybind11/setup_helpers.py +++ b/pybind11/setup_helpers.py @@ -59,8 +59,7 @@ import distutils.errors import distutils.ccompiler - -WIN = sys.platform.startswith("win32") and sysconfig.get_platform() != "mingw" +WIN = sys.platform.startswith("win32") and "mingw" not in sysconfig.get_platform() PY2 = sys.version_info[0] < 3 MACOS = sys.platform.startswith("darwin") STD_TMPL = "/std:c++{}" if WIN else "-std=c++{}" From 143f9bb88f117003e1587f6ad1f12bf5ebdb051b Mon Sep 17 00:00:00 2001 From: Jerome Robert Date: Mon, 19 Jul 2021 14:17:40 +0200 Subject: [PATCH 2/6] 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 { | ^~~~~~~~~~~~~~~~~ --- include/pybind11/detail/common.h | 14 ++++++++++++-- include/pybind11/pytypes.h | 2 +- tests/test_exceptions.h | 2 +- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 0add6272f5..398566c607 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -89,6 +89,16 @@ # endif #endif +#if !defined(PYBIND11_EXPORT_EXCEPTION) +# ifdef __MINGW32__ +// workaround for: +// error: 'dllexport' implies default visibility, but xxx has already been declared with a different visibility +# define PYBIND11_EXPORT_EXCEPTION +# else +# define PYBIND11_EXPORT_EXCEPTION PYBIND11_EXPORT +# endif +#endif + #if defined(_MSC_VER) # define PYBIND11_NOINLINE __declspec(noinline) #else @@ -740,7 +750,7 @@ PYBIND11_NAMESPACE_END(detail) # 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. #endif /// C++ bindings of builtin Python exceptions -class PYBIND11_EXPORT builtin_exception : public std::runtime_error { +class PYBIND11_EXPORT_EXCEPTION builtin_exception : public std::runtime_error { public: using std::runtime_error::runtime_error; /// Set the error using the Python C API @@ -751,7 +761,7 @@ class PYBIND11_EXPORT builtin_exception : public std::runtime_error { #endif #define PYBIND11_RUNTIME_EXCEPTION(name, type) \ - class PYBIND11_EXPORT name : public builtin_exception { public: \ + class PYBIND11_EXPORT_EXCEPTION name : public builtin_exception { public: \ using builtin_exception::builtin_exception; \ name() : name("") { } \ void set_error() const override { PyErr_SetString(type, what()); } \ diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 161aed06f4..4cf606e8d0 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -327,7 +327,7 @@ PYBIND11_NAMESPACE_END(detail) /// thrown to propagate python-side errors back through C++ which can either be caught manually or /// else falls back to the function dispatcher (which then raises the captured error back to /// python). -class PYBIND11_EXPORT error_already_set : public std::runtime_error { +class PYBIND11_EXPORT_EXCEPTION error_already_set : public std::runtime_error { public: /// Constructs a new exception from the current Python error indicator, if any. The current /// Python error indicator will be cleared. diff --git a/tests/test_exceptions.h b/tests/test_exceptions.h index 5d02d1b35b..9d428312eb 100644 --- a/tests/test_exceptions.h +++ b/tests/test_exceptions.h @@ -4,7 +4,7 @@ // shared exceptions for cross_module_tests -class PYBIND11_EXPORT shared_exception : public pybind11::builtin_exception { +class PYBIND11_EXPORT_EXCEPTION shared_exception : public pybind11::builtin_exception { public: using builtin_exception::builtin_exception; explicit shared_exception() : shared_exception("") {} From d87430795887882e0100b4f603e99abb66006d33 Mon Sep 17 00:00:00 2001 From: Jerome Robert Date: Thu, 29 Jul 2021 10:46:32 +0200 Subject: [PATCH 3/6] GHA: Test Mingw64 build --- .github/workflows/ci.yml | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 03c9225c12..b5837bb9c9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -859,3 +859,34 @@ jobs: - name: Run all checks run: cmake --build build -t check + + mingw: + runs-on: windows-latest + defaults: + run: + shell: msys2 {0} + steps: + - uses: msys2/setup-msys2@v2 + with: + install: >- + mingw-w64-x86_64-gcc + mingw-w64-x86_64-python-pip + mingw-w64-x86_64-cmake + mingw-w64-x86_64-make + mingw-w64-x86_64-python-pytest + mingw-w64-x86_64-eigen3 + mingw-w64-x86_64-boost + mingw-w64-x86_64-catch + + - uses: actions/checkout@v1 + + - name: Configure + # LTO leads to many undefined reference like + # `pybind11::detail::function_call::function_call(pybind11::detail::function_call&&) + run: cmake -DCMAKE_INTERPROCEDURAL_OPTIMIZATION=OFF -G "MinGW Makefiles" -S . -B build + + - name: Build + run: cmake --build build -j 2 + + - name: Python tests + run: cmake --build build --target pytest From f7f6018ccdedea42b95fc49a1f34260803aec4b3 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 29 Jul 2021 08:42:28 -0400 Subject: [PATCH 4/6] fix: avoid thin binaries on mingw --- .github/workflows/ci.yml | 2 +- tools/pybind11Common.cmake | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b5837bb9c9..73a6fd5199 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -883,7 +883,7 @@ jobs: - name: Configure # LTO leads to many undefined reference like # `pybind11::detail::function_call::function_call(pybind11::detail::function_call&&) - run: cmake -DCMAKE_INTERPROCEDURAL_OPTIMIZATION=OFF -G "MinGW Makefiles" -S . -B build + run: cmake -G "MinGW Makefiles" -S . -B build - name: Build run: cmake --build build -j 2 diff --git a/tools/pybind11Common.cmake b/tools/pybind11Common.cmake index 57e42536e1..45fd90ff92 100644 --- a/tools/pybind11Common.cmake +++ b/tools/pybind11Common.cmake @@ -308,7 +308,7 @@ function(_pybind11_generate_lto target prefer_thin_lto) if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND NOT APPLE) # Clang Gold plugin does not support -Os; append -O3 to MinSizeRel builds to override it set(linker_append ";$<$:-O3>") - elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU") + elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND NOT MINGW) set(cxx_append ";-fno-fat-lto-objects") endif() From 597119ce07fbf208b2475c913958155aa9d3cd00 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 29 Jul 2021 10:37:14 -0400 Subject: [PATCH 5/6] fix: drop lto on MinGW --- tools/pybind11Common.cmake | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tools/pybind11Common.cmake b/tools/pybind11Common.cmake index 45fd90ff92..7afb0d0b1a 100644 --- a/tools/pybind11Common.cmake +++ b/tools/pybind11Common.cmake @@ -302,6 +302,11 @@ function(_pybind11_return_if_cxx_and_linker_flags_work result cxxflags linkerfla endfunction() function(_pybind11_generate_lto target prefer_thin_lto) + if(MINGW) + message(STATUS "${target} disabled (problems with undefined symbols for MinGW for now)") + return() + endif() + if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") set(cxx_append "") set(linker_append "") From 12a4d2cb668fcccc745f38617c847cb87ce53b77 Mon Sep 17 00:00:00 2001 From: Jerome Robert Date: Fri, 30 Jul 2021 15:27:12 +0200 Subject: [PATCH 6/6] Mingw64: disable PYBIND11_DEPRECATED It trigger many warnings for unknown reasons --- include/pybind11/detail/common.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 398566c607..d63e3e9c78 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -105,7 +105,11 @@ # define PYBIND11_NOINLINE __attribute__ ((noinline)) #endif -#if defined(PYBIND11_CPP14) +#if defined(__MINGW32__) +// For unknown reasons all PYBIND11_DEPRECATED member trigger a warning when declared +// whether it is used or not +# define PYBIND11_DEPRECATED(reason) +#elif defined(PYBIND11_CPP14) # define PYBIND11_DEPRECATED(reason) [[deprecated(reason)]] #else # define PYBIND11_DEPRECATED(reason) __attribute__((deprecated(reason)))