From f82a2287d8e60190b9c6dcb6d0fbafe178912521 Mon Sep 17 00:00:00 2001 From: memsharded Date: Thu, 3 Nov 2022 13:59:49 +0100 Subject: [PATCH 1/4] automatically add msys2/usr/bin path --- conans/client/subsystems.py | 3 ++ .../toolchains/gnu/autotools/test_win_bash.py | 37 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/conans/client/subsystems.py b/conans/client/subsystems.py index 7b961d8154b..191a24163e9 100644 --- a/conans/client/subsystems.py +++ b/conans/client/subsystems.py @@ -77,6 +77,9 @@ def _windows_bash_wrapper(conanfile, command, env, envfiles_folder): _msystem = {"x86": "MINGW32"}.get(conanfile.settings.get_safe("arch"), "MINGW64") msys2_mode_env.define("MSYSTEM", _msystem) msys2_mode_env.define("MSYS2_PATH_TYPE", "inherit") + # Automatically add the current bash.exe path to the PATH + if os.path.isabs(shell_path): + msys2_mode_env.prepend_path("PATH", os.path.dirname(shell_path)) path = os.path.join(conanfile.generators_folder, "msys2_mode.bat") msys2_mode_env.vars(conanfile, "build").save_bat(path) env.append(path) diff --git a/conans/test/functional/toolchains/gnu/autotools/test_win_bash.py b/conans/test/functional/toolchains/gnu/autotools/test_win_bash.py index d0af58717f2..a4f96f7e94b 100644 --- a/conans/test/functional/toolchains/gnu/autotools/test_win_bash.py +++ b/conans/test/functional/toolchains/gnu/autotools/test_win_bash.py @@ -61,3 +61,40 @@ def build(self): bat_contents = client.load("conanbuild.bat") assert "conanvcvars.bat" in bat_contents + +@pytest.mark.skipif(platform.system() != "Windows", reason="Requires Windows") +def test_add_msys2_path_automatically(): + """ Check that commands like ar, autoconf, etc, that are in the /usr/bin folder together + with the bash.exe, can be automaticallly used when running in windows bash, without user + extra addition to [buildenv] of that msys64/usr/bin path + + # https://github.com/conan-io/conan/issues/12110 + """ + client = TestClient(path_with_spaces=False) + try: + bash_path = tools_locations["msys2"]["system"]["path"]["Windows"] + "/bash.exe" + except KeyError: + pytest.skip("msys2 path not defined") + + save(client.cache.new_config_path, textwrap.dedent(""" + tools.microsoft.bash:subsystem=msys2 + tools.microsoft.bash:path={} + """.format(bash_path))) + + conanfile = textwrap.dedent(""" + from conan import ConanFile + + class HelloConan(ConanFile): + name = "hello" + version = "0.1" + + win_bash = True + + def build(self): + self.run("ar -h") + """) + + client.save({"conanfile.py": conanfile}) + client.run("create .") + assert "Usage: ar" in client.out + From 0f88879c7f53992a91eed67f922fa6a3af5539f8 Mon Sep 17 00:00:00 2001 From: memsharded Date: Thu, 3 Nov 2022 19:22:45 +0100 Subject: [PATCH 2/4] fixed --- conans/client/subsystems.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/conans/client/subsystems.py b/conans/client/subsystems.py index 191a24163e9..b25f4bb22d7 100644 --- a/conans/client/subsystems.py +++ b/conans/client/subsystems.py @@ -81,7 +81,10 @@ def _windows_bash_wrapper(conanfile, command, env, envfiles_folder): if os.path.isabs(shell_path): msys2_mode_env.prepend_path("PATH", os.path.dirname(shell_path)) path = os.path.join(conanfile.generators_folder, "msys2_mode.bat") + # Make sure we save pure .bat files, without sh stuff + wb, conanfile.win_bash = conanfile.win_bash, None msys2_mode_env.vars(conanfile, "build").save_bat(path) + conanfile.win_bash = wb env.append(path) wrapped_shell = '"%s"' % shell_path if " " in shell_path else shell_path From 1195b571401bfa332e54ceba9ade9b2970ae8552 Mon Sep 17 00:00:00 2001 From: memsharded Date: Mon, 14 Nov 2022 15:54:54 +0100 Subject: [PATCH 3/4] CHERE_INVOKING --- conans/client/subsystems.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/conans/client/subsystems.py b/conans/client/subsystems.py index 6c74e110eb9..e076e100102 100644 --- a/conans/client/subsystems.py +++ b/conans/client/subsystems.py @@ -77,6 +77,8 @@ def _windows_bash_wrapper(conanfile, command, env, envfiles_folder): _msystem = {"x86": "MINGW32"}.get(conanfile.settings.get_safe("arch"), "MINGW64") msys2_mode_env.define("MSYSTEM", _msystem) msys2_mode_env.define("MSYS2_PATH_TYPE", "inherit") + # So --login do not change automatically to the user home + msys2_mode_env.define("CHERE_INVOKING", "1") path = os.path.join(conanfile.generators_folder, "msys2_mode.bat") # Make sure we save pure .bat files, without sh stuff wb, conanfile.win_bash = conanfile.win_bash, None From f7f60ba2bc3693feda11e3d823e36072322cf714 Mon Sep 17 00:00:00 2001 From: memsharded Date: Mon, 14 Nov 2022 17:55:09 +0100 Subject: [PATCH 4/4] comments --- conans/client/subsystems.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/conans/client/subsystems.py b/conans/client/subsystems.py index e076e100102..aea3bf1d500 100644 --- a/conans/client/subsystems.py +++ b/conans/client/subsystems.py @@ -75,6 +75,9 @@ def _windows_bash_wrapper(conanfile, command, env, envfiles_folder): # Configure MSYS2 to inherith the PATH msys2_mode_env = Environment() _msystem = {"x86": "MINGW32"}.get(conanfile.settings.get_safe("arch"), "MINGW64") + # https://www.msys2.org/wiki/Launchers/ dictates that the shell should be launched with + # - MSYSTEM defined + # - CHERE_INVOKING is necessary to keep the CWD and not change automatically to the user home msys2_mode_env.define("MSYSTEM", _msystem) msys2_mode_env.define("MSYS2_PATH_TYPE", "inherit") # So --login do not change automatically to the user home @@ -96,6 +99,8 @@ def _windows_bash_wrapper(conanfile, command, env, envfiles_folder): accepted_extensions=("sh", )) wrapped_user_cmd = _escape_windows_cmd(wrapped_user_cmd) + # according to https://www.msys2.org/wiki/Launchers/, it is necessary to use --login shell + # running without it is discouraged final_command = '{} --login -c {}'.format(wrapped_shell, wrapped_user_cmd) return final_command