diff --git a/conan/tools/gnu/__init__.py b/conan/tools/gnu/__init__.py index a7c75b32404..bd515ad6128 100644 --- a/conan/tools/gnu/__init__.py +++ b/conan/tools/gnu/__init__.py @@ -1,5 +1,6 @@ from conan.tools.gnu.autotools import Autotools from conan.tools.gnu.autotoolstoolchain import AutotoolsToolchain from conan.tools.gnu.autotoolsdeps import AutotoolsDeps +from conan.tools.gnu.mingw import is_mingw from conan.tools.gnu.pkgconfig import PkgConfig from conan.tools.gnu.pkgconfigdeps import PkgConfigDeps diff --git a/conan/tools/gnu/mingw.py b/conan/tools/gnu/mingw.py new file mode 100644 index 00000000000..335c771346c --- /dev/null +++ b/conan/tools/gnu/mingw.py @@ -0,0 +1,13 @@ +def is_mingw(conanfile): + """ + Validate if current compiler in host setttings is related to MinGW + :param conanfile: ConanFile instance + :return: True, if the host compiler is related to MinGW, otherwise False. + """ + host_os = conanfile.settings.get_safe("os") + is_cygwin = host_os == "Windows" and conanfile.settings.get_safe("os.subsystem") == "cygwin" + host_compiler = conanfile.settings.get_safe("compiler") + is_mingw_gcc = host_os == "Windows" and not is_cygwin and host_compiler == "gcc" + is_mingw_clang = host_os == "Windows" and not is_cygwin and host_compiler == "clang" and \ + not conanfile.settings.get_safe("compiler.runtime") + return is_mingw_gcc or is_mingw_clang diff --git a/conans/test/unittests/tools/gnu/mingw_test.py b/conans/test/unittests/tools/gnu/mingw_test.py new file mode 100644 index 00000000000..e4148ddb7e7 --- /dev/null +++ b/conans/test/unittests/tools/gnu/mingw_test.py @@ -0,0 +1,49 @@ +import pytest +from mock import Mock + +from conan.tools.gnu import is_mingw +from conans import ConanFile, Settings +from conans.model.env_info import EnvValues + + +@pytest.mark.parametrize( + "os,compiler_name,compiler_version,compiler_libcxx,compiler_runtime,compiler_runtime_type,expected", + [ + ("Windows", "gcc", "9", "libstdc++11", None, None, True), + ("Windows", "clang", "16", "libstdc++11", None, None, True), + ("Windows", "Visual Studio", "17", "MD", None, None, False), + ("Windows", "msvc", "193", None, "dynamic", "Release", False), + ("Windows", "clang", "16", None, "MD", None, False), + ("Windows", "clang", "16", None, "dynamic", "Release", False), + ("Linux", "gcc", "9", "libstdc++11", None, None, False), + ("Linux", "clang", "16", "libc++", None, None, False), + ("Macos", "apple-clang", "14", "libc++", None, None, False), + ], +) +def test_is_mingw(os, compiler_name, compiler_version, compiler_libcxx, compiler_runtime, compiler_runtime_type, expected): + compiler = {compiler_name: {"version": [compiler_version]}} + if compiler_libcxx: + compiler[compiler_name].update({"libcxx": [compiler_libcxx]}) + if compiler_runtime: + compiler[compiler_name].update({"runtime": [compiler_runtime]}) + if compiler_runtime_type: + compiler[compiler_name].update({"runtime_type": [compiler_runtime_type]}) + settings = Settings({ + "os": [os], + "arch": ["x86_64"], + "compiler": compiler, + "build_type": ["Release"], + }) + conanfile = ConanFile(Mock(), None) + conanfile.settings = "os", "arch", "compiler", "build_type" + conanfile.initialize(settings, EnvValues()) + conanfile.settings.os = os + conanfile.settings.compiler = compiler_name + conanfile.settings.compiler.version = compiler_version + if compiler_libcxx: + conanfile.settings.compiler.libcxx = compiler_libcxx + if compiler_runtime: + conanfile.settings.compiler.runtime = compiler_runtime + if compiler_runtime_type: + conanfile.settings.compiler.runtime_type = compiler_runtime_type + assert is_mingw(conanfile) == expected