-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add conan.tools.gnu.is_mingw() helper
#19963
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HYUEHFJKhfjklkej
wants to merge
1
commit into
conan-io:develop2
Choose a base branch
from
HYUEHFJKhfjklkej:feature/15648-is-mingw
base: develop2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+114
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| def is_mingw(conanfile, build_context=False): | ||
| """ | ||
| Validate if the current compiler is a MinGW toolchain (host context by default). | ||
|
|
||
| A MinGW toolchain is detected by the following Conan settings: | ||
|
|
||
| - ``os == "Windows"`` (a non-Windows host is never MinGW) | ||
| - ``os.subsystem != "cygwin"`` (Cygwin uses a POSIX layer, not MinGW) | ||
| - ``compiler == "gcc"``, OR | ||
| ``compiler == "clang"`` with ``compiler.runtime`` unset (MinGW Clang). | ||
| ``clang-cl`` is detected via ``compiler.runtime`` and is not considered MinGW. | ||
|
|
||
| Reference: | ||
| https://blog.conan.io/2022/10/13/Different-flavors-Clang-compiler-Windows.html | ||
|
|
||
| :param conanfile: ``< ConanFile object >`` The current recipe object. Always use ``self``. | ||
| :param build_context: If True, will use the settings from the build context, not host ones. | ||
| :return: ``bool`` True if the selected context targets a MinGW toolchain, otherwise False. | ||
| """ | ||
| settings = conanfile.settings_build if build_context else conanfile.settings | ||
| if settings.get_safe("os") != "Windows": | ||
| return False | ||
| if settings.get_safe("os.subsystem") == "cygwin": | ||
| return False | ||
| compiler = settings.get_safe("compiler") | ||
| if compiler == "gcc": | ||
| return True | ||
| if compiler == "clang" and settings.get_safe("compiler.runtime") is None: | ||
| return True | ||
| return False | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import pytest | ||
|
|
||
| from conan import ConanFile | ||
| from conan.tools.gnu import is_mingw | ||
| from conan.internal.model.settings import Settings | ||
|
|
||
|
|
||
| def _make_conanfile(os_, compiler, version, *, libcxx=None, runtime=None, | ||
| runtime_type=None, subsystem=None): | ||
| compiler_def = {compiler: {"version": [version]}} | ||
| if libcxx is not None: | ||
| compiler_def[compiler]["libcxx"] = [libcxx] | ||
| if runtime is not None: | ||
| compiler_def[compiler]["runtime"] = [runtime] | ||
| if runtime_type is not None: | ||
| compiler_def[compiler]["runtime_type"] = [runtime_type] | ||
| os_def = {os_: {"subsystem": [subsystem]}} if subsystem else [os_] | ||
| settings = Settings({ | ||
| "os": os_def, | ||
| "arch": ["x86_64"], | ||
| "compiler": compiler_def, | ||
| "build_type": ["Release"], | ||
| }) | ||
| conanfile = ConanFile() | ||
| conanfile.settings = settings | ||
| conanfile.settings.os = os_ | ||
| if subsystem is not None: | ||
| conanfile.settings.os.subsystem = subsystem | ||
| conanfile.settings.compiler = compiler | ||
| conanfile.settings.compiler.version = version | ||
| if libcxx is not None: | ||
| conanfile.settings.compiler.libcxx = libcxx | ||
| if runtime is not None: | ||
| conanfile.settings.compiler.runtime = runtime | ||
| if runtime_type is not None: | ||
| conanfile.settings.compiler.runtime_type = runtime_type | ||
| return conanfile | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("os_,compiler,version,kwargs,expected", [ | ||
| # MinGW with gcc — the canonical case | ||
| ("Windows", "gcc", "13", {"libcxx": "libstdc++11"}, True), | ||
| # MinGW with Clang — `compiler.runtime` is unset | ||
| ("Windows", "clang", "17", {"libcxx": "libstdc++11"}, True), | ||
| # clang-cl on Windows — `compiler.runtime` is set, not MinGW | ||
| ("Windows", "clang", "17", {"runtime": "dynamic", "runtime_type": "Release"}, False), | ||
| # MSVC — never MinGW | ||
| ("Windows", "msvc", "193", {"runtime": "dynamic", "runtime_type": "Release"}, False), | ||
| # Cygwin uses a POSIX layer, not MinGW | ||
| ("Windows", "gcc", "13", {"subsystem": "cygwin", "libcxx": "libstdc++11"}, False), | ||
| # Non-Windows hosts are never MinGW | ||
| ("Linux", "gcc", "13", {"libcxx": "libstdc++11"}, False), | ||
| ("Linux", "clang", "17", {"libcxx": "libc++"}, False), | ||
| ("Macos", "apple-clang", "15", {"libcxx": "libc++"}, False), | ||
| ]) | ||
| def test_is_mingw(os_, compiler, version, kwargs, expected): | ||
| conanfile = _make_conanfile(os_, compiler, version, **kwargs) | ||
| assert is_mingw(conanfile) is expected | ||
|
|
||
|
|
||
| def test_is_mingw_build_context(): | ||
| """`build_context=True` must consult settings_build, not the host settings.""" | ||
| host = _make_conanfile("Linux", "gcc", "13", libcxx="libstdc++11") | ||
| build = _make_conanfile("Windows", "gcc", "13", libcxx="libstdc++11") | ||
| host.settings_build = build.settings | ||
| assert is_mingw(host) is False | ||
| assert is_mingw(host, build_context=True) is True | ||
|
|
||
|
|
||
| def test_is_mingw_libcxx_not_required(): | ||
| """`compiler.libcxx` is removed in pure-C recipes; detection must still work without it.""" | ||
| settings = Settings({ | ||
| "os": ["Windows"], | ||
| "arch": ["x86_64"], | ||
| "compiler": {"gcc": {"version": ["13"]}}, | ||
| "build_type": ["Release"], | ||
| }) | ||
| conanfile = ConanFile() | ||
| conanfile.settings = settings | ||
| conanfile.settings.os = "Windows" | ||
| conanfile.settings.compiler = "gcc" | ||
| conanfile.settings.compiler.version = "13" | ||
| assert is_mingw(conanfile) is True |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't necessarily true. The
msys2clang compiler is its own "environment", not a MinGW environment https://www.msys2.org/docs/environments/the most common definition of
is_mingwin ConanCenter recipes is:So changing and doing it for
clangtoo might be breaking some recipes or use cases?