From 43ceb98d77b016b3aad4db7a8a2b9307a7c2d834 Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Sat, 24 Sep 2022 23:09:35 +0200 Subject: [PATCH 01/36] set LD to link & add -nologo --- conan/tools/gnu/autotoolstoolchain.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index 7269fa2f702..492828f0929 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -123,8 +123,9 @@ def defines(self): def environment(self): env = Environment() if is_msvc(self._conanfile): - env.define("CXX", "cl") - env.define("CC", "cl") + env.define("CXX", "cl -nologo") + env.define("CC", "cl -nologo") + env.define("LD", "link -nologo") env.append("CPPFLAGS", ["-D{}".format(d) for d in self.defines]) env.append("CXXFLAGS", self.cxxflags) env.append("CFLAGS", self.cflags) From 44ee28cca66e7e02ecdef1073176fad64ef6e96b Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Sat, 24 Sep 2022 23:31:38 +0200 Subject: [PATCH 02/36] honor CC, CXX, LD from profile --- conan/tools/gnu/autotoolstoolchain.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index 492828f0929..30cc4223ce5 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -7,9 +7,10 @@ from conan.tools.env import Environment from conan.tools.files.files import save_toolchain_args from conan.tools.gnu.get_gnu_triplet import _get_gnu_triplet -from conan.tools.microsoft import VCVars, is_msvc, msvc_runtime_flag +from conan.tools.microsoft import VCVars, is_msvc, msvc_runtime_flag, unix_path from conans.errors import ConanException -from conans.tools import args_to_string +from conans.tools import args_to_string, get_env +import os class AutotoolsToolchain: @@ -123,9 +124,24 @@ def defines(self): def environment(self): env = Environment() if is_msvc(self._conanfile): - env.define("CXX", "cl -nologo") - env.define("CC", "cl -nologo") - env.define("LD", "link -nologo") + cc = get_env("CC") + if cc and os.path.exists(cc): + cc = f"{unix_path(self._conanfile, cc)} -nologo" + else: + cc = "cl -nologo" + env.define("CC", cc) + cxx = get_env("CXX") + if cxx and os.path.exists(cxx): + cxx = f"{unix_path(self._conanfile, cxx)} -nologo" + else: + cxx = "cl -nologo" + env.define("CXX", cxx) + ld = get_env("CXX") + if ld and os.path.exists(ld): + ld = f"{unix_path(self._conanfile, ld)} -nologo" + else: + ld = "link -nologo" + env.define("LD", ld) env.append("CPPFLAGS", ["-D{}".format(d) for d in self.defines]) env.append("CXXFLAGS", self.cxxflags) env.append("CFLAGS", self.cflags) From bbc8cf7a38d98b880d552a55537f17b93458cc13 Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Sun, 25 Sep 2022 00:00:10 +0200 Subject: [PATCH 03/36] handle more cases on windows --- conan/tools/gnu/autotoolstoolchain.py | 41 ++++++++++++++++++++------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index 30cc4223ce5..6031647af8a 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -123,25 +123,46 @@ def defines(self): def environment(self): env = Environment() + + # Specific compiler & linker handling on Windows + if hasattr(self._conanfile, "settings_build"): + os_build = self._conanfile.settings_build.get_safe("os") + else: + os_build = self._conanfile.settings.get_safe("os") if is_msvc(self._conanfile): cc = get_env("CC") if cc and os.path.exists(cc): - cc = f"{unix_path(self._conanfile, cc)} -nologo" + cc = unix_path(self._conanfile, cc) else: - cc = "cl -nologo" - env.define("CC", cc) + cc = "cl" + env.define("CC", f"{cc} -nologo") + cxx = get_env("CXX") if cxx and os.path.exists(cxx): - cxx = f"{unix_path(self._conanfile, cxx)} -nologo" + cxx = unix_path(self._conanfile, cxx) else: - cxx = "cl -nologo" - env.define("CXX", cxx) - ld = get_env("CXX") + cxx = "cl" + env.define("CXX", f"{cxx} -nologo") + + ld = get_env("LD") if ld and os.path.exists(ld): - ld = f"{unix_path(self._conanfile, ld)} -nologo" + ld = unix_path(self._conanfile, ld) else: - ld = "link -nologo" - env.define("LD", ld) + ld = "link" + env.define("LD", f"{ld} -nologo") + elif os_build == "Windows": + cc = get_env("CC") + if cc and os.path.exists(cc): + env.define("CC", unix_path(self._conanfile, cc)) + + cxx = get_env("CXX") + if cxx and os.path.exists(cxx): + env.define("CXX", unix_path(self._conanfile, cxx)) + + ld = get_env("LD") + if ld and os.path.exists(ld): + env.define("LD", unix_path(self._conanfile, ld)) + env.append("CPPFLAGS", ["-D{}".format(d) for d in self.defines]) env.append("CXXFLAGS", self.cxxflags) env.append("CFLAGS", self.cflags) From 73345fad6ba9fc25c3c87ef3c72e4c63a1f0bd34 Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Sun, 25 Sep 2022 01:22:40 +0200 Subject: [PATCH 04/36] refactor --- conan/tools/gnu/autotoolstoolchain.py | 67 +++++++++++++-------------- 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index 6031647af8a..0e0cb88c8b1 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -121,47 +121,46 @@ def defines(self): ret = [self.ndebug, self.gcc_cxx11_abi] + conf_flags + self.extra_defines return self._filter_list_empty_fields(ret) + def _exe_env_var_to_unix_path(self, env_var, default=None, extra_options=[]): + """ + Convenient method to convert env vars like CC, CXX or LD to values compatible with autotools. + If env var doesn't exist, returns default. + """ + exe = get_env(env_var) + if exe: + if os.path.exists(exe): + exe = unix_path(self._conanfile, exe) + else: + exe = default + if exe: + for option in extra_options: + if option not in exe: + exe = f"{exe} {option}" + return exe + def environment(self): env = Environment() - # Specific compiler & linker handling on Windows + # On Windows or if compiler is msvc, ensure to properly set CC, CXX and LD: + # - convert values from profile (if set) to compatible values + # - otherwise set to a good default if compiler is not a first class citizen in autotools if hasattr(self._conanfile, "settings_build"): os_build = self._conanfile.settings_build.get_safe("os") else: os_build = self._conanfile.settings.get_safe("os") - if is_msvc(self._conanfile): - cc = get_env("CC") - if cc and os.path.exists(cc): - cc = unix_path(self._conanfile, cc) - else: - cc = "cl" - env.define("CC", f"{cc} -nologo") - - cxx = get_env("CXX") - if cxx and os.path.exists(cxx): - cxx = unix_path(self._conanfile, cxx) - else: - cxx = "cl" - env.define("CXX", f"{cxx} -nologo") - - ld = get_env("LD") - if ld and os.path.exists(ld): - ld = unix_path(self._conanfile, ld) - else: - ld = "link" - env.define("LD", f"{ld} -nologo") - elif os_build == "Windows": - cc = get_env("CC") - if cc and os.path.exists(cc): - env.define("CC", unix_path(self._conanfile, cc)) - - cxx = get_env("CXX") - if cxx and os.path.exists(cxx): - env.define("CXX", unix_path(self._conanfile, cxx)) - - ld = get_env("LD") - if ld and os.path.exists(ld): - env.define("LD", unix_path(self._conanfile, ld)) + if is_msvc(self._conanfile) or os_build == "Windows": + default_compiler = "cl" if is_msvc(self._conanfile) else None + default_linker = "link" if is_msvc(self._conanfile) else None + extra_options = ["-nologo"] if is_msvc(self._conanfile) else [] + cc = self._exe_env_var_to_unix_path("CC", default_compiler, extra_options) + if cc: + env.define("CC", cc) + cxx = self._exe_env_var_to_unix_path("CXX", default_compiler, extra_options) + if cxx: + env.define("CXX", cxx) + ld = self._exe_env_var_to_unix_path("LD", default_linker, extra_options) + if ld: + env.define("LD", ld) env.append("CPPFLAGS", ["-D{}".format(d) for d in self.defines]) env.append("CXXFLAGS", self.cxxflags) From dddfa507354d99bb955a96c1d8edcbb006e49b13 Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Sun, 25 Sep 2022 10:33:06 +0200 Subject: [PATCH 05/36] also handle NM --- conan/tools/gnu/autotoolstoolchain.py | 32 ++++++++++++++++++--------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index 0e0cb88c8b1..41b2930f6cd 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -141,7 +141,7 @@ def _exe_env_var_to_unix_path(self, env_var, default=None, extra_options=[]): def environment(self): env = Environment() - # On Windows or if compiler is msvc, ensure to properly set CC, CXX and LD: + # On Windows or if compiler is msvc, ensure to properly set CC, CXX, LD and NM: # - convert values from profile (if set) to compatible values # - otherwise set to a good default if compiler is not a first class citizen in autotools if hasattr(self._conanfile, "settings_build"): @@ -149,18 +149,30 @@ def environment(self): else: os_build = self._conanfile.settings.get_safe("os") if is_msvc(self._conanfile) or os_build == "Windows": - default_compiler = "cl" if is_msvc(self._conanfile) else None - default_linker = "link" if is_msvc(self._conanfile) else None - extra_options = ["-nologo"] if is_msvc(self._conanfile) else [] - cc = self._exe_env_var_to_unix_path("CC", default_compiler, extra_options) - if cc: + default = { + "CC": "cl" if is_msvc(self._conanfile) else None, + "CXX": "cl" if is_msvc(self._conanfile) else None, + "LD": "link" if is_msvc(self._conanfile) else None, + "NM": "dumpbin" if is_msvc(self._conanfile) else None, + } + extra_options = { + "CC": ["-nologo"] if is_msvc(self._conanfile) else [], + "CXX": ["-nologo"] if is_msvc(self._conanfile) else [], + "LD": ["-nologo"] if is_msvc(self._conanfile) else [], + "NM": ["-symbols"] if is_msvc(self._conanfile) else [] + } + cc = self._exe_env_var_to_unix_path("CC", default["CC"], extra_options["CC"]) + if cc and cc != get_env("CC"): env.define("CC", cc) - cxx = self._exe_env_var_to_unix_path("CXX", default_compiler, extra_options) - if cxx: + cxx = self._exe_env_var_to_unix_path("CXX", default["CXX"], extra_options["CXX"]) + if cxx and cxx != get_env("CC"): env.define("CXX", cxx) - ld = self._exe_env_var_to_unix_path("LD", default_linker, extra_options) - if ld: + ld = self._exe_env_var_to_unix_path("LD", default["LD"], extra_options["LD"]) + if ld and ld != get_env("CC"): env.define("LD", ld) + nm = self._exe_env_var_to_unix_path("NM", default["NM"], extra_options["NM"]) + if nm and nm != get_env("NM"): + env.define("NM", nm) env.append("CPPFLAGS", ["-D{}".format(d) for d in self.defines]) env.append("CXXFLAGS", self.cxxflags) From 1eed250448a6585c4a94a1958806cce894ae29fb Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Sun, 25 Sep 2022 10:36:35 +0200 Subject: [PATCH 06/36] refactor again --- conan/tools/gnu/autotoolstoolchain.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index 41b2930f6cd..a6bc500da55 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -161,18 +161,14 @@ def environment(self): "LD": ["-nologo"] if is_msvc(self._conanfile) else [], "NM": ["-symbols"] if is_msvc(self._conanfile) else [] } - cc = self._exe_env_var_to_unix_path("CC", default["CC"], extra_options["CC"]) - if cc and cc != get_env("CC"): - env.define("CC", cc) - cxx = self._exe_env_var_to_unix_path("CXX", default["CXX"], extra_options["CXX"]) - if cxx and cxx != get_env("CC"): - env.define("CXX", cxx) - ld = self._exe_env_var_to_unix_path("LD", default["LD"], extra_options["LD"]) - if ld and ld != get_env("CC"): - env.define("LD", ld) - nm = self._exe_env_var_to_unix_path("NM", default["NM"], extra_options["NM"]) - if nm and nm != get_env("NM"): - env.define("NM", nm) + for env_var in ["CC", "CXX", "LD", "NM"]: + new_env_var = self._exe_env_var_to_unix_path( + env_var, + default.get(env_var), + extra_options.get(env_var, []), + ) + if new_env_var and new_env_var != get_env(env_var): + env.define(env_var, new_env_var) env.append("CPPFLAGS", ["-D{}".format(d) for d in self.defines]) env.append("CXXFLAGS", self.cxxflags) From 6b69fd25533ac8dd49dcff207cf0edb72f3fa0f2 Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Sun, 25 Sep 2022 10:56:04 +0200 Subject: [PATCH 07/36] formatting --- conan/tools/gnu/autotoolstoolchain.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index a6bc500da55..99c755ec4fa 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -159,7 +159,7 @@ def environment(self): "CC": ["-nologo"] if is_msvc(self._conanfile) else [], "CXX": ["-nologo"] if is_msvc(self._conanfile) else [], "LD": ["-nologo"] if is_msvc(self._conanfile) else [], - "NM": ["-symbols"] if is_msvc(self._conanfile) else [] + "NM": ["-symbols"] if is_msvc(self._conanfile) else [], } for env_var in ["CC", "CXX", "LD", "NM"]: new_env_var = self._exe_env_var_to_unix_path( From 6b7743c7f3989748aa0fcce2dd5af22813efee30 Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Sun, 25 Sep 2022 11:12:36 +0200 Subject: [PATCH 08/36] also handle AR --- conan/tools/gnu/autotoolstoolchain.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index 99c755ec4fa..e1276a6539d 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -141,7 +141,7 @@ def _exe_env_var_to_unix_path(self, env_var, default=None, extra_options=[]): def environment(self): env = Environment() - # On Windows or if compiler is msvc, ensure to properly set CC, CXX, LD and NM: + # On Windows or if compiler is msvc, ensure to properly set vars like CC & CXX: # - convert values from profile (if set) to compatible values # - otherwise set to a good default if compiler is not a first class citizen in autotools if hasattr(self._conanfile, "settings_build"): @@ -153,15 +153,17 @@ def environment(self): "CC": "cl" if is_msvc(self._conanfile) else None, "CXX": "cl" if is_msvc(self._conanfile) else None, "LD": "link" if is_msvc(self._conanfile) else None, + "AR": "lib" if is_msvc(self._conanfile) else None, "NM": "dumpbin" if is_msvc(self._conanfile) else None, } extra_options = { "CC": ["-nologo"] if is_msvc(self._conanfile) else [], "CXX": ["-nologo"] if is_msvc(self._conanfile) else [], "LD": ["-nologo"] if is_msvc(self._conanfile) else [], - "NM": ["-symbols"] if is_msvc(self._conanfile) else [], + "AR": ["-nologo"] if is_msvc(self._conanfile) else [], + "NM": ["-nologo", "-symbols"] if is_msvc(self._conanfile) else [], } - for env_var in ["CC", "CXX", "LD", "NM"]: + for env_var in ["CC", "CXX", "LD", "AR", "NM"]: new_env_var = self._exe_env_var_to_unix_path( env_var, default.get(env_var), From 9977d8335ec3f72f5524b500e5bd5c192a5a160e Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Sun, 25 Sep 2022 12:17:10 +0200 Subject: [PATCH 09/36] refactor again --- conan/tools/gnu/autotoolstoolchain.py | 40 ++++++++++++++++----------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index e1276a6539d..8b58f04e475 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -149,25 +149,33 @@ def environment(self): else: os_build = self._conanfile.settings.get_safe("os") if is_msvc(self._conanfile) or os_build == "Windows": - default = { - "CC": "cl" if is_msvc(self._conanfile) else None, - "CXX": "cl" if is_msvc(self._conanfile) else None, - "LD": "link" if is_msvc(self._conanfile) else None, - "AR": "lib" if is_msvc(self._conanfile) else None, - "NM": "dumpbin" if is_msvc(self._conanfile) else None, + env_vars = { + "CC": { + "default": "cl" if is_msvc(self._conanfile) else None, + "extra_options": ["-nologo"] if is_msvc(self._conanfile) else [], + }, + "CXX": { + "default": "cl" if is_msvc(self._conanfile) else None, + "extra_options": ["-nologo"] if is_msvc(self._conanfile) else [], + }, + "LD": { + "default": "link" if is_msvc(self._conanfile) else None, + "extra_options": ["-nologo"] if is_msvc(self._conanfile) else [], + }, + "AR": { + "default": "lib" if is_msvc(self._conanfile) else None, + "extra_options": ["-nologo"] if is_msvc(self._conanfile) else [], + }, + "NM": { + "default": "dumpbin" if is_msvc(self._conanfile) else None, + "extra_options": ["-nologo", "-symbols"] if is_msvc(self._conanfile) else [], + }, } - extra_options = { - "CC": ["-nologo"] if is_msvc(self._conanfile) else [], - "CXX": ["-nologo"] if is_msvc(self._conanfile) else [], - "LD": ["-nologo"] if is_msvc(self._conanfile) else [], - "AR": ["-nologo"] if is_msvc(self._conanfile) else [], - "NM": ["-nologo", "-symbols"] if is_msvc(self._conanfile) else [], - } - for env_var in ["CC", "CXX", "LD", "AR", "NM"]: + for env_var, env_var_values in env_vars.items(): new_env_var = self._exe_env_var_to_unix_path( env_var, - default.get(env_var), - extra_options.get(env_var, []), + env_var_values.get("default"), + env_var_values.get("extra_options", []), ) if new_env_var and new_env_var != get_env(env_var): env.define(env_var, new_env_var) From 73c2e196b98ed14761a556c33c35d84ab45e7612 Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Sun, 25 Sep 2022 12:39:16 +0200 Subject: [PATCH 10/36] small change --- conan/tools/gnu/autotoolstoolchain.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index 8b58f04e475..e13962f5923 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -135,7 +135,7 @@ def _exe_env_var_to_unix_path(self, env_var, default=None, extra_options=[]): if exe: for option in extra_options: if option not in exe: - exe = f"{exe} {option}" + exe += f" {option}" return exe def environment(self): From 6e9295b571801e83f3d423cbfea0e572a8b0fdb4 Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Sun, 25 Sep 2022 13:34:15 +0200 Subject: [PATCH 11/36] add more variables --- conan/tools/gnu/autotoolstoolchain.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index e13962f5923..b67862d0196 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -144,6 +144,7 @@ def environment(self): # On Windows or if compiler is msvc, ensure to properly set vars like CC & CXX: # - convert values from profile (if set) to compatible values # - otherwise set to a good default if compiler is not a first class citizen in autotools + # TODO: handle clang-cl if hasattr(self._conanfile, "settings_build"): os_build = self._conanfile.settings_build.get_safe("os") else: @@ -170,6 +171,15 @@ def environment(self): "default": "dumpbin" if is_msvc(self._conanfile) else None, "extra_options": ["-nologo", "-symbols"] if is_msvc(self._conanfile) else [], }, + "OBJDUMP": { + "default": ":" if is_msvc(self._conanfile) else None, + }, + "RANLIB": { + "default": ":" if is_msvc(self._conanfile) else None, + }, + "STRIP": { + "default": ":" if is_msvc(self._conanfile) else None, + }, } for env_var, env_var_values in env_vars.items(): new_env_var = self._exe_env_var_to_unix_path( From 3ad51c3ba50872c0a56fae10b715f5331a1a9e29 Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Sun, 25 Sep 2022 14:41:01 +0200 Subject: [PATCH 12/36] add comments --- conan/tools/gnu/autotoolstoolchain.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index b67862d0196..fbc38c72ab1 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -144,6 +144,11 @@ def environment(self): # On Windows or if compiler is msvc, ensure to properly set vars like CC & CXX: # - convert values from profile (if set) to compatible values # - otherwise set to a good default if compiler is not a first class citizen in autotools + # msvc: see https://lists.gnu.org/archive/html/automake/2011-09/msg00002.html + # TODO: for msvc, allow users to prefix: + # - CC & CXX with path of compile script from automake + # - AR with with path of ar-lib script from automake + # Or provide these scripts in conan client? # TODO: handle clang-cl if hasattr(self._conanfile, "settings_build"): os_build = self._conanfile.settings_build.get_safe("os") From 305783bafc5a49ad89213db3710551057ec2f8ad Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Sun, 25 Sep 2022 17:54:36 +0200 Subject: [PATCH 13/36] add two new attributes to prefix CC, CXX & ARLIB with compatibility wrappers --- conan/tools/gnu/autotoolstoolchain.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index fbc38c72ab1..e457b67e538 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -44,6 +44,10 @@ def __init__(self, conanfile, namespace=None): self.fpic = self._conanfile.options.get_safe("fPIC") self.msvc_runtime_flag = self._get_msvc_runtime_flag() + # Wrappers paths for windows tools not compatible with autotools, like msvc + self.compile_wrapper = None + self.arlib_wrapper = None + # Cross build self._host = None self._build = None @@ -121,7 +125,7 @@ def defines(self): ret = [self.ndebug, self.gcc_cxx11_abi] + conf_flags + self.extra_defines return self._filter_list_empty_fields(ret) - def _exe_env_var_to_unix_path(self, env_var, default=None, extra_options=[]): + def _exe_env_var_to_unix_path(self, env_var, default=None, extra_options=[], wrapper=None): """ Convenient method to convert env vars like CC, CXX or LD to values compatible with autotools. If env var doesn't exist, returns default. @@ -133,6 +137,10 @@ def _exe_env_var_to_unix_path(self, env_var, default=None, extra_options=[]): else: exe = default if exe: + if wrapper: + if os.path.exists(wrapper): + wrapper = unix_path(self._conanfile, wrapper) + exe = f"{wrapper} {exe}" for option in extra_options: if option not in exe: exe += f" {option}" @@ -145,10 +153,6 @@ def environment(self): # - convert values from profile (if set) to compatible values # - otherwise set to a good default if compiler is not a first class citizen in autotools # msvc: see https://lists.gnu.org/archive/html/automake/2011-09/msg00002.html - # TODO: for msvc, allow users to prefix: - # - CC & CXX with path of compile script from automake - # - AR with with path of ar-lib script from automake - # Or provide these scripts in conan client? # TODO: handle clang-cl if hasattr(self._conanfile, "settings_build"): os_build = self._conanfile.settings_build.get_safe("os") @@ -159,10 +163,12 @@ def environment(self): "CC": { "default": "cl" if is_msvc(self._conanfile) else None, "extra_options": ["-nologo"] if is_msvc(self._conanfile) else [], + "wrapper": self.compile_wrapper, }, "CXX": { "default": "cl" if is_msvc(self._conanfile) else None, "extra_options": ["-nologo"] if is_msvc(self._conanfile) else [], + "wrapper": self.compile_wrapper, }, "LD": { "default": "link" if is_msvc(self._conanfile) else None, @@ -171,6 +177,7 @@ def environment(self): "AR": { "default": "lib" if is_msvc(self._conanfile) else None, "extra_options": ["-nologo"] if is_msvc(self._conanfile) else [], + "wrapper": self.arlib_wrapper, }, "NM": { "default": "dumpbin" if is_msvc(self._conanfile) else None, @@ -191,6 +198,7 @@ def environment(self): env_var, env_var_values.get("default"), env_var_values.get("extra_options", []), + env_var_values.get("wrapper"), ) if new_env_var and new_env_var != get_env(env_var): env.define(env_var, new_env_var) From fc3c72c6abda1da24951483bf251cf444b28d237 Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Sun, 25 Sep 2022 19:36:38 +0200 Subject: [PATCH 14/36] add cc, cxx, ld, ar, nm, objdump, ranlib & trip attributes --- conan/tools/gnu/autotoolstoolchain.py | 143 ++++++++++++++++---------- 1 file changed, 87 insertions(+), 56 deletions(-) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index e457b67e538..61d5880c0ad 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -44,7 +44,17 @@ def __init__(self, conanfile, namespace=None): self.fpic = self._conanfile.options.get_safe("fPIC") self.msvc_runtime_flag = self._get_msvc_runtime_flag() - # Wrappers paths for windows tools not compatible with autotools, like msvc + # standard build toolchains env vars + self.cc = self._cc + self.cxx = self._cxx + self.ld = self._ld + self.ar = self._ar + self.nm = self._nm + self.objdump = self._objdump + self.ranlib = self._ranlib + self.strip = self._strip + + # Wrappers paths for tools not compatible with autotools, like msvc self.compile_wrapper = None self.arlib_wrapper = None @@ -125,6 +135,70 @@ def defines(self): ret = [self.ndebug, self.gcc_cxx11_abi] + conf_flags + self.extra_defines return self._filter_list_empty_fields(ret) + @property + def _cc(self): + return self._exe_env_var_to_unix_path( + "CC", + "cl" if is_msvc(self._conanfile) else None, + ["-nologo"] if is_msvc(self._conanfile) else [], + self.compile_wrapper, + ) + + @property + def _cxx(self): + return self._exe_env_var_to_unix_path( + "CXX", + "cl" if is_msvc(self._conanfile) else None, + ["-nologo"] if is_msvc(self._conanfile) else [], + self.compile_wrapper, + ) + + @property + def _ld(self): + return self._exe_env_var_to_unix_path( + "LD", + "link" if is_msvc(self._conanfile) else None, + ["-nologo"] if is_msvc(self._conanfile) else [], + ) + + @property + def _ar(self): + return self._exe_env_var_to_unix_path( + "AR", + "lib" if is_msvc(self._conanfile) else None, + ["-nologo"] if is_msvc(self._conanfile) else [], + self.arlib_wrapper, + ) + + @property + def _nm(self): + return self._exe_env_var_to_unix_path( + "NM", + "dumpbin" if is_msvc(self._conanfile) else None, + ["-nologo", "-symbols"] if is_msvc(self._conanfile) else [], + ) + + @property + def _objdump(self): + return self._exe_env_var_to_unix_path( + "OBJDUMP", + ":" if is_msvc(self._conanfile) else None, + ) + + @property + def _ranlib(self): + return self._exe_env_var_to_unix_path( + "RANLIB", + ":" if is_msvc(self._conanfile) else None, + ) + + @property + def _strip(self): + return self._exe_env_var_to_unix_path( + "STRIP", + ":" if is_msvc(self._conanfile) else None, + ) + def _exe_env_var_to_unix_path(self, env_var, default=None, extra_options=[], wrapper=None): """ Convenient method to convert env vars like CC, CXX or LD to values compatible with autotools. @@ -148,61 +222,18 @@ def _exe_env_var_to_unix_path(self, env_var, default=None, extra_options=[], wra def environment(self): env = Environment() - - # On Windows or if compiler is msvc, ensure to properly set vars like CC & CXX: - # - convert values from profile (if set) to compatible values - # - otherwise set to a good default if compiler is not a first class citizen in autotools - # msvc: see https://lists.gnu.org/archive/html/automake/2011-09/msg00002.html - # TODO: handle clang-cl - if hasattr(self._conanfile, "settings_build"): - os_build = self._conanfile.settings_build.get_safe("os") - else: - os_build = self._conanfile.settings.get_safe("os") - if is_msvc(self._conanfile) or os_build == "Windows": - env_vars = { - "CC": { - "default": "cl" if is_msvc(self._conanfile) else None, - "extra_options": ["-nologo"] if is_msvc(self._conanfile) else [], - "wrapper": self.compile_wrapper, - }, - "CXX": { - "default": "cl" if is_msvc(self._conanfile) else None, - "extra_options": ["-nologo"] if is_msvc(self._conanfile) else [], - "wrapper": self.compile_wrapper, - }, - "LD": { - "default": "link" if is_msvc(self._conanfile) else None, - "extra_options": ["-nologo"] if is_msvc(self._conanfile) else [], - }, - "AR": { - "default": "lib" if is_msvc(self._conanfile) else None, - "extra_options": ["-nologo"] if is_msvc(self._conanfile) else [], - "wrapper": self.arlib_wrapper, - }, - "NM": { - "default": "dumpbin" if is_msvc(self._conanfile) else None, - "extra_options": ["-nologo", "-symbols"] if is_msvc(self._conanfile) else [], - }, - "OBJDUMP": { - "default": ":" if is_msvc(self._conanfile) else None, - }, - "RANLIB": { - "default": ":" if is_msvc(self._conanfile) else None, - }, - "STRIP": { - "default": ":" if is_msvc(self._conanfile) else None, - }, - } - for env_var, env_var_values in env_vars.items(): - new_env_var = self._exe_env_var_to_unix_path( - env_var, - env_var_values.get("default"), - env_var_values.get("extra_options", []), - env_var_values.get("wrapper"), - ) - if new_env_var and new_env_var != get_env(env_var): - env.define(env_var, new_env_var) - + for env_var, new_env_var_value in [ + ("CC", self.cc), + ("CXX", self.cxx), + ("LD", self.ld), + ("AR", self.ar), + ("NM", self.nm), + ("OBJDUMP", self.objdump), + ("RANLIB", self.ranlib), + ("STRIP", self.strip), + ]: + if new_env_var_value and new_env_var_value != get_env(env_var): + env.define(env_var, new_env_var_value) env.append("CPPFLAGS", ["-D{}".format(d) for d in self.defines]) env.append("CXXFLAGS", self.cxxflags) env.append("CFLAGS", self.cflags) From 40fdb2000fd727a4f2f1309c3fb848910b2cb0d8 Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Sun, 25 Sep 2022 20:09:43 +0200 Subject: [PATCH 15/36] add compiler_wrapper & arlib_wrapper to arguments of constructor --- conan/tools/gnu/autotoolstoolchain.py | 54 +++++++++++---------------- 1 file changed, 22 insertions(+), 32 deletions(-) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index 61d5880c0ad..e28e68c0559 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -14,9 +14,11 @@ class AutotoolsToolchain: - def __init__(self, conanfile, namespace=None): + def __init__(self, conanfile, namespace=None, compiler_wrapper=None, arlib_wrapper=None): self._conanfile = conanfile self._namespace = namespace + self._compile_wrapper = compiler_wrapper + self._arlib_wrapper = arlib_wrapper self.configure_args = self._default_configure_shared_flags() + self._default_configure_install_flags() self.autoreconf_args = self._default_autoreconf_flags() @@ -45,18 +47,14 @@ def __init__(self, conanfile, namespace=None): self.msvc_runtime_flag = self._get_msvc_runtime_flag() # standard build toolchains env vars - self.cc = self._cc - self.cxx = self._cxx - self.ld = self._ld - self.ar = self._ar - self.nm = self._nm - self.objdump = self._objdump - self.ranlib = self._ranlib - self.strip = self._strip - - # Wrappers paths for tools not compatible with autotools, like msvc - self.compile_wrapper = None - self.arlib_wrapper = None + self.cc = self._get_cc() + self.cxx = self._get_cxx() + self.ld = self._get_ld() + self.ar = self._get_ar() + self.nm = self._get_nm() + self.objdump = self._get_objdump() + self.ranlib = self._get_ranlib() + self.strip = self._get_strip() # Cross build self._host = None @@ -135,65 +133,57 @@ def defines(self): ret = [self.ndebug, self.gcc_cxx11_abi] + conf_flags + self.extra_defines return self._filter_list_empty_fields(ret) - @property - def _cc(self): + def _get_cc(self): return self._exe_env_var_to_unix_path( "CC", "cl" if is_msvc(self._conanfile) else None, ["-nologo"] if is_msvc(self._conanfile) else [], - self.compile_wrapper, + self._compile_wrapper, ) - @property - def _cxx(self): + def _get_cxx(self): return self._exe_env_var_to_unix_path( "CXX", "cl" if is_msvc(self._conanfile) else None, ["-nologo"] if is_msvc(self._conanfile) else [], - self.compile_wrapper, + self._compile_wrapper, ) - @property - def _ld(self): + def _get_ld(self): return self._exe_env_var_to_unix_path( "LD", "link" if is_msvc(self._conanfile) else None, ["-nologo"] if is_msvc(self._conanfile) else [], ) - @property - def _ar(self): + def _get_ar(self): return self._exe_env_var_to_unix_path( "AR", "lib" if is_msvc(self._conanfile) else None, ["-nologo"] if is_msvc(self._conanfile) else [], - self.arlib_wrapper, + self._arlib_wrapper, ) - @property - def _nm(self): + def _get_nm(self): return self._exe_env_var_to_unix_path( "NM", "dumpbin" if is_msvc(self._conanfile) else None, ["-nologo", "-symbols"] if is_msvc(self._conanfile) else [], ) - @property - def _objdump(self): + def _get_objdump(self): return self._exe_env_var_to_unix_path( "OBJDUMP", ":" if is_msvc(self._conanfile) else None, ) - @property - def _ranlib(self): + def _get_ranlib(self): return self._exe_env_var_to_unix_path( "RANLIB", ":" if is_msvc(self._conanfile) else None, ) - @property - def _strip(self): + def _get_strip(self): return self._exe_env_var_to_unix_path( "STRIP", ":" if is_msvc(self._conanfile) else None, From f03d98dbdf0e399f0555416f5de7eece3d52fcd4 Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Sun, 25 Sep 2022 20:41:56 +0200 Subject: [PATCH 16/36] default extra_options to None --- conan/tools/gnu/autotoolstoolchain.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index e28e68c0559..f6bd1f392eb 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -189,7 +189,7 @@ def _get_strip(self): ":" if is_msvc(self._conanfile) else None, ) - def _exe_env_var_to_unix_path(self, env_var, default=None, extra_options=[], wrapper=None): + def _exe_env_var_to_unix_path(self, env_var, default=None, extra_options=None, wrapper=None): """ Convenient method to convert env vars like CC, CXX or LD to values compatible with autotools. If env var doesn't exist, returns default. @@ -205,9 +205,10 @@ def _exe_env_var_to_unix_path(self, env_var, default=None, extra_options=[], wra if os.path.exists(wrapper): wrapper = unix_path(self._conanfile, wrapper) exe = f"{wrapper} {exe}" - for option in extra_options: - if option not in exe: - exe += f" {option}" + if extra_options: + for option in extra_options: + if option not in exe: + exe += f" {option}" return exe def environment(self): From c160c7bb3eaa928cc4fec6f3df983b10207341f3 Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Sun, 25 Sep 2022 21:07:23 +0200 Subject: [PATCH 17/36] typo --- conan/tools/gnu/autotoolstoolchain.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index f6bd1f392eb..05a18459651 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -14,10 +14,10 @@ class AutotoolsToolchain: - def __init__(self, conanfile, namespace=None, compiler_wrapper=None, arlib_wrapper=None): + def __init__(self, conanfile, namespace=None, compile_wrapper=None, arlib_wrapper=None): self._conanfile = conanfile self._namespace = namespace - self._compile_wrapper = compiler_wrapper + self._compile_wrapper = compile_wrapper self._arlib_wrapper = arlib_wrapper self.configure_args = self._default_configure_shared_flags() + self._default_configure_install_flags() From a81f43adbe2f59d42b916579687d4a62b7e3180c Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Sun, 25 Sep 2022 21:13:20 +0200 Subject: [PATCH 18/36] rename to arlib_wrapper to ar_wrapper --- conan/tools/gnu/autotoolstoolchain.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index 05a18459651..82203da158c 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -14,11 +14,11 @@ class AutotoolsToolchain: - def __init__(self, conanfile, namespace=None, compile_wrapper=None, arlib_wrapper=None): + def __init__(self, conanfile, namespace=None, compile_wrapper=None, ar_wrapper=None): self._conanfile = conanfile self._namespace = namespace self._compile_wrapper = compile_wrapper - self._arlib_wrapper = arlib_wrapper + self._ar_wrapper = ar_wrapper self.configure_args = self._default_configure_shared_flags() + self._default_configure_install_flags() self.autoreconf_args = self._default_autoreconf_flags() @@ -161,7 +161,7 @@ def _get_ar(self): "AR", "lib" if is_msvc(self._conanfile) else None, ["-nologo"] if is_msvc(self._conanfile) else [], - self._arlib_wrapper, + self._ar_wrapper, ) def _get_nm(self): From 6a415fd41fc08279b929a6bf2498eddfc376ca89 Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Sun, 25 Sep 2022 22:13:29 +0200 Subject: [PATCH 19/36] use VirtualBuildEnv to get env vars from profile --- conan/tools/gnu/autotoolstoolchain.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index 82203da158c..e95a6adf40f 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -4,12 +4,12 @@ from conan.tools.apple.apple import apple_min_version_flag, to_apple_arch, \ apple_sdk_path from conan.tools.build.cross_building import cross_building, get_cross_building_settings -from conan.tools.env import Environment +from conan.tools.env import Environment, VirtualBuildEnv from conan.tools.files.files import save_toolchain_args from conan.tools.gnu.get_gnu_triplet import _get_gnu_triplet from conan.tools.microsoft import VCVars, is_msvc, msvc_runtime_flag, unix_path from conans.errors import ConanException -from conans.tools import args_to_string, get_env +from conans.tools import args_to_string import os @@ -194,7 +194,7 @@ def _exe_env_var_to_unix_path(self, env_var, default=None, extra_options=None, w Convenient method to convert env vars like CC, CXX or LD to values compatible with autotools. If env var doesn't exist, returns default. """ - exe = get_env(env_var) + exe = VirtualBuildEnv(self._conanfile).vars().get(env_var) if exe: if os.path.exists(exe): exe = unix_path(self._conanfile, exe) @@ -213,6 +213,7 @@ def _exe_env_var_to_unix_path(self, env_var, default=None, extra_options=None, w def environment(self): env = Environment() + build_env = VirtualBuildEnv(self._conanfile).vars() for env_var, new_env_var_value in [ ("CC", self.cc), ("CXX", self.cxx), @@ -223,7 +224,7 @@ def environment(self): ("RANLIB", self.ranlib), ("STRIP", self.strip), ]: - if new_env_var_value and new_env_var_value != get_env(env_var): + if new_env_var_value and new_env_var_value != build_env.get(env_var): env.define(env_var, new_env_var_value) env.append("CPPFLAGS", ["-D{}".format(d) for d in self.defines]) env.append("CXXFLAGS", self.cxxflags) From 746bff5cde1a3cfb3d0fd07bac4605fdc7da8d12 Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Sun, 25 Sep 2022 22:14:09 +0200 Subject: [PATCH 20/36] do not add -nologo to AR if msvc ar-lib wrapper doesn't seem to like extra options --- conan/tools/gnu/autotoolstoolchain.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index e95a6adf40f..d2387aa4999 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -160,7 +160,7 @@ def _get_ar(self): return self._exe_env_var_to_unix_path( "AR", "lib" if is_msvc(self._conanfile) else None, - ["-nologo"] if is_msvc(self._conanfile) else [], + None, # do not add any option, ar wrapper may not like this self._ar_wrapper, ) From ea62645100a12826ef75a86372aadf5bc8542bd2 Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Sun, 25 Sep 2022 22:34:47 +0200 Subject: [PATCH 21/36] don't check existence of wrapper --- conan/tools/gnu/autotoolstoolchain.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index d2387aa4999..8cf169f7417 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -202,9 +202,7 @@ def _exe_env_var_to_unix_path(self, env_var, default=None, extra_options=None, w exe = default if exe: if wrapper: - if os.path.exists(wrapper): - wrapper = unix_path(self._conanfile, wrapper) - exe = f"{wrapper} {exe}" + exe = f"{unix_path(self._conanfile, wrapper)} {exe}" if extra_options: for option in extra_options: if option not in exe: From a2a2a5db4e2f1d2811c69256191b21c2ff5ac143 Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Mon, 26 Sep 2022 00:00:29 +0200 Subject: [PATCH 22/36] do not allow spaces in env var paths for autotools --- conan/tools/gnu/autotoolstoolchain.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index 8cf169f7417..4c6e2d653b9 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -198,6 +198,12 @@ def _exe_env_var_to_unix_path(self, env_var, default=None, extra_options=None, w if exe: if os.path.exists(exe): exe = unix_path(self._conanfile, exe) + if " " in exe: + self._conanfile.output.warn( + f"AutotoolsToolchain {env_var} management: {exe} contains spaces, " + f"sorry it can't work with autotools, fallback to {default}" + ) + exe = default else: exe = default if exe: From 7d4adf187021d7d13bdccdcc71bacd7879799c67 Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Mon, 26 Sep 2022 00:00:47 +0200 Subject: [PATCH 23/36] minor change --- conan/tools/gnu/autotoolstoolchain.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index 4c6e2d653b9..a5ef8394539 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -160,7 +160,7 @@ def _get_ar(self): return self._exe_env_var_to_unix_path( "AR", "lib" if is_msvc(self._conanfile) else None, - None, # do not add any option, ar wrapper may not like this + [], # do not add any option, ar wrapper may not like this self._ar_wrapper, ) From b539767444805a0aa5f6c83c38c12731623ed624 Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Fri, 30 Sep 2022 20:12:17 +0200 Subject: [PATCH 24/36] improve windows support --- conan/tools/gnu/autotoolstoolchain.py | 34 ++++++++++++++-------- conan/tools/microsoft/win32api/__init__.py | 1 + conan/tools/microsoft/win32api/win32api.py | 22 ++++++++++++++ 3 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 conan/tools/microsoft/win32api/__init__.py create mode 100644 conan/tools/microsoft/win32api/win32api.py diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index a5ef8394539..6e95860b6f1 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -8,6 +8,8 @@ from conan.tools.files.files import save_toolchain_args from conan.tools.gnu.get_gnu_triplet import _get_gnu_triplet from conan.tools.microsoft import VCVars, is_msvc, msvc_runtime_flag, unix_path +if os.name == "nt": + from conan.tools.microsoft.win32api import win32api from conans.errors import ConanException from conans.tools import args_to_string import os @@ -160,7 +162,7 @@ def _get_ar(self): return self._exe_env_var_to_unix_path( "AR", "lib" if is_msvc(self._conanfile) else None, - [], # do not add any option, ar wrapper may not like this + ["-nologo"], self._ar_wrapper, ) @@ -194,25 +196,26 @@ def _exe_env_var_to_unix_path(self, env_var, default=None, extra_options=None, w Convenient method to convert env vars like CC, CXX or LD to values compatible with autotools. If env var doesn't exist, returns default. """ - exe = VirtualBuildEnv(self._conanfile).vars().get(env_var) + env_var_value = VirtualBuildEnv(self._conanfile).vars().get(env_var) + exe = env_var_value if exe: if os.path.exists(exe): + if " " in exe and os.name == "nt": + exe = win32api.get_short_path_name(exe) exe = unix_path(self._conanfile, exe) - if " " in exe: - self._conanfile.output.warn( - f"AutotoolsToolchain {env_var} management: {exe} contains spaces, " - f"sorry it can't work with autotools, fallback to {default}" - ) - exe = default + if " " in exe: + exe = default else: exe = default if exe: - if wrapper: - exe = f"{unix_path(self._conanfile, wrapper)} {exe}" if extra_options: for option in extra_options: if option not in exe: exe += f" {option}" + if wrapper: + if extra_options: + exe = f"\"{exe}\"" + exe = f"{unix_path(self._conanfile, wrapper)} {exe}" return exe def environment(self): @@ -228,8 +231,15 @@ def environment(self): ("RANLIB", self.ranlib), ("STRIP", self.strip), ]: - if new_env_var_value and new_env_var_value != build_env.get(env_var): - env.define(env_var, new_env_var_value) + if new_env_var_value: + previous_env_var_value = build_env.get(env_var) + if new_env_var_value != previous_env_var_value: + env.define(env_var, new_env_var_value) + if previous_env_var_value: + self._conanfile.output.info( + f"{self.__class__.__name__}: {env_var} env var set to " + f"{new_env_var_value}, instead of {previous_env_var_value}" + ) env.append("CPPFLAGS", ["-D{}".format(d) for d in self.defines]) env.append("CXXFLAGS", self.cxxflags) env.append("CFLAGS", self.cflags) diff --git a/conan/tools/microsoft/win32api/__init__.py b/conan/tools/microsoft/win32api/__init__.py new file mode 100644 index 00000000000..b2ffd846363 --- /dev/null +++ b/conan/tools/microsoft/win32api/__init__.py @@ -0,0 +1 @@ +from conan.tools.microsoft.win32api import win32api diff --git a/conan/tools/microsoft/win32api/win32api.py b/conan/tools/microsoft/win32api/win32api.py new file mode 100644 index 00000000000..2fbe3feab6a --- /dev/null +++ b/conan/tools/microsoft/win32api/win32api.py @@ -0,0 +1,22 @@ +import os + +if os.name == "nt": + import ctypes + from ctypes import wintypes + + + def get_short_path_name(path): + try: + _GetShortPathNameW = ctypes.windll.kernel32.GetShortPathNameW + _GetShortPathNameW.argtypes = [wintypes.LPCWSTR, wintypes.LPWSTR, wintypes.DWORD] + _GetShortPathNameW.restype = wintypes.DWORD + output_buf_size = 0 + while True: + output_buf = ctypes.create_unicode_buffer(output_buf_size) + needed = _GetShortPathNameW(path, output_buf, output_buf_size) + if output_buf_size >= needed: + return output_buf.value + else: + output_buf_size = needed + except: + return path From 47b821375b0a1d1a262a1ff414edcbfa71ee5877 Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Fri, 30 Sep 2022 20:15:30 +0200 Subject: [PATCH 25/36] move import of win32api where it is used --- conan/tools/gnu/autotoolstoolchain.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index 6e95860b6f1..ef14f173ede 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -8,8 +8,6 @@ from conan.tools.files.files import save_toolchain_args from conan.tools.gnu.get_gnu_triplet import _get_gnu_triplet from conan.tools.microsoft import VCVars, is_msvc, msvc_runtime_flag, unix_path -if os.name == "nt": - from conan.tools.microsoft.win32api import win32api from conans.errors import ConanException from conans.tools import args_to_string import os @@ -201,6 +199,7 @@ def _exe_env_var_to_unix_path(self, env_var, default=None, extra_options=None, w if exe: if os.path.exists(exe): if " " in exe and os.name == "nt": + from conan.tools.microsoft.win32api import win32api exe = win32api.get_short_path_name(exe) exe = unix_path(self._conanfile, exe) if " " in exe: From a3f77e193d900747eee6622e1e7218394524d1b5 Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Sat, 1 Oct 2022 11:39:27 +0200 Subject: [PATCH 26/36] add 4 conf to define build executables tools.build:c_compiler for C compiler tools.build:cxx_compiler for C++ compiler tools.build:archiver for archiver tools.build:linker for linker --- conan/tools/cmake/toolchain/blocks.py | 7 +- conan/tools/gnu/autotoolstoolchain.py | 104 ++++++++++---------------- conan/tools/meson/toolchain.py | 12 +-- conans/model/conf.py | 4 + 4 files changed, 51 insertions(+), 76 deletions(-) diff --git a/conan/tools/cmake/toolchain/blocks.py b/conan/tools/cmake/toolchain/blocks.py index 74b27472722..33c0714b841 100644 --- a/conan/tools/cmake/toolchain/blocks.py +++ b/conan/tools/cmake/toolchain/blocks.py @@ -647,14 +647,10 @@ class GenericSystemBlock(Block): set(CMAKE_GENERATOR_TOOLSET "{{ toolset }}" CACHE STRING "" FORCE) {% endif %} {% if compiler %} - if(NOT DEFINED ENV{CC}) set(CMAKE_C_COMPILER {{ compiler }}) - endif() {% endif %} {% if compiler_cpp %} - if(NOT DEFINED ENV{CXX}) set(CMAKE_CXX_COMPILER {{ compiler_cpp }}) - endif() {% endif %} {% if compiler_rc %} if(NOT DEFINED ENV{RC}) @@ -735,6 +731,9 @@ def _get_compiler(self, generator): compiler_c = "clang" compiler_cpp = "clang++" + compiler_c = self._conanfile.conf.get("tools.build:c_compiler", default=compiler_c) + compiler_cpp = self._conanfile.conf.get("tools.build:cxx_compiler", default=compiler_cpp) + return compiler_c, compiler_cpp, compiler_rc def _get_generic_system_name(self): diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index ef14f173ede..2059446492e 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -4,7 +4,7 @@ from conan.tools.apple.apple import apple_min_version_flag, to_apple_arch, \ apple_sdk_path from conan.tools.build.cross_building import cross_building, get_cross_building_settings -from conan.tools.env import Environment, VirtualBuildEnv +from conan.tools.env import Environment from conan.tools.files.files import save_toolchain_args from conan.tools.gnu.get_gnu_triplet import _get_gnu_triplet from conan.tools.microsoft import VCVars, is_msvc, msvc_runtime_flag, unix_path @@ -133,94 +133,73 @@ def defines(self): ret = [self.ndebug, self.gcc_cxx11_abi] + conf_flags + self.extra_defines return self._filter_list_empty_fields(ret) + def _tools_build_exe_to_unix_path(self, conf, default=None, extra_options=None, wrapper=None): + exe = self._conanfile.conf.get(conf, default=default) + if exe: + if os.path.exists(exe): + if " " in exe and os.name == "nt": + from conan.tools.microsoft.win32api import win32api + exe = win32api.get_short_path_name(exe) + exe = unix_path(self._conanfile, exe) + if " " in exe: + exe = default + if exe: + if extra_options: + for option in extra_options: + if option not in exe: + exe += f" {option}" + if wrapper: + if extra_options: + exe = f"\"{exe}\"" + exe = f"{unix_path(self._conanfile, wrapper)} {exe}" + return exe + def _get_cc(self): - return self._exe_env_var_to_unix_path( - "CC", + return self._tools_build_exe_to_unix_path( + "tools.build:c_compiler", "cl" if is_msvc(self._conanfile) else None, ["-nologo"] if is_msvc(self._conanfile) else [], self._compile_wrapper, ) def _get_cxx(self): - return self._exe_env_var_to_unix_path( - "CXX", + return self._tools_build_exe_to_unix_path( + "tools.build:cxx_compiler", "cl" if is_msvc(self._conanfile) else None, ["-nologo"] if is_msvc(self._conanfile) else [], self._compile_wrapper, ) def _get_ld(self): - return self._exe_env_var_to_unix_path( - "LD", + return self._tools_build_exe_to_unix_path( + "tools.build:linker", "link" if is_msvc(self._conanfile) else None, ["-nologo"] if is_msvc(self._conanfile) else [], ) def _get_ar(self): - return self._exe_env_var_to_unix_path( - "AR", + return self._tools_build_exe_to_unix_path( + "tools.build:archiver", "lib" if is_msvc(self._conanfile) else None, - ["-nologo"], + ["-nologo"] if is_msvc(self._conanfile) else [], self._ar_wrapper, ) def _get_nm(self): - return self._exe_env_var_to_unix_path( - "NM", - "dumpbin" if is_msvc(self._conanfile) else None, - ["-nologo", "-symbols"] if is_msvc(self._conanfile) else [], - ) + return "dumpbin -nologo -symbols" if is_msvc(self._conanfile) else None def _get_objdump(self): - return self._exe_env_var_to_unix_path( - "OBJDUMP", - ":" if is_msvc(self._conanfile) else None, - ) + return ":" if is_msvc(self._conanfile) else None def _get_ranlib(self): - return self._exe_env_var_to_unix_path( - "RANLIB", - ":" if is_msvc(self._conanfile) else None, - ) + return ":" if is_msvc(self._conanfile) else None def _get_strip(self): - return self._exe_env_var_to_unix_path( - "STRIP", - ":" if is_msvc(self._conanfile) else None, - ) - - def _exe_env_var_to_unix_path(self, env_var, default=None, extra_options=None, wrapper=None): - """ - Convenient method to convert env vars like CC, CXX or LD to values compatible with autotools. - If env var doesn't exist, returns default. - """ - env_var_value = VirtualBuildEnv(self._conanfile).vars().get(env_var) - exe = env_var_value - if exe: - if os.path.exists(exe): - if " " in exe and os.name == "nt": - from conan.tools.microsoft.win32api import win32api - exe = win32api.get_short_path_name(exe) - exe = unix_path(self._conanfile, exe) - if " " in exe: - exe = default - else: - exe = default - if exe: - if extra_options: - for option in extra_options: - if option not in exe: - exe += f" {option}" - if wrapper: - if extra_options: - exe = f"\"{exe}\"" - exe = f"{unix_path(self._conanfile, wrapper)} {exe}" - return exe + return ":" if is_msvc(self._conanfile) else None def environment(self): env = Environment() - build_env = VirtualBuildEnv(self._conanfile).vars() - for env_var, new_env_var_value in [ + for env_var, env_var_value in [ ("CC", self.cc), ("CXX", self.cxx), ("LD", self.ld), @@ -230,15 +209,8 @@ def environment(self): ("RANLIB", self.ranlib), ("STRIP", self.strip), ]: - if new_env_var_value: - previous_env_var_value = build_env.get(env_var) - if new_env_var_value != previous_env_var_value: - env.define(env_var, new_env_var_value) - if previous_env_var_value: - self._conanfile.output.info( - f"{self.__class__.__name__}: {env_var} env var set to " - f"{new_env_var_value}, instead of {previous_env_var_value}" - ) + if env_var_value: + env.define(env_var, env_var_value) env.append("CPPFLAGS", ["-D{}".format(d) for d in self.defines]) env.append("CXXFLAGS", self.cxxflags) env.append("CFLAGS", self.cflags) diff --git a/conan/tools/meson/toolchain.py b/conan/tools/meson/toolchain.py index d97116943d3..fcf3cb6d69e 100644 --- a/conan/tools/meson/toolchain.py +++ b/conan/tools/meson/toolchain.py @@ -163,13 +163,13 @@ def __init__(self, conanfile, backend=None): default_comp = "gcc" default_comp_cpp = "g++" - # Read the VirtualBuildEnv to update the variables + # Read the VirtualBuildEnv to update the variables. Values from conf have precedence. build_env = VirtualBuildEnv(self._conanfile).vars() - self.c = build_env.get("CC") or default_comp - self.cpp = build_env.get("CXX") or default_comp_cpp - self.c_ld = build_env.get("CC_LD") or build_env.get("LD") - self.cpp_ld = build_env.get("CXX_LD") or build_env.get("LD") - self.ar = build_env.get("AR") + self.c = self._conanfile.conf.get("tools.build:c_compiler", default=build_env.get("CC") or default_comp) + self.cpp = self._conanfile.conf.get("tools.build:cxx_compiler", default=build_env.get("CXX") or default_comp_cpp) + self.c_ld = self._conanfile.conf.get("tools.build:linker", default=build_env.get("CC_LD") or build_env.get("LD")) + self.cpp_ld = self._conanfile.conf.get("tools.build:linker", default=build_env.get("CXX_LD") or build_env.get("LD")) + self.ar = self._conanfile.conf.get("tools.build:archiver", default=build_env.get("AR")) self.strip = build_env.get("STRIP") self.as_ = build_env.get("AS") self.windres = build_env.get("WINDRES") diff --git a/conans/model/conf.py b/conans/model/conf.py index 4df28790e10..78dfd33bb5d 100644 --- a/conans/model/conf.py +++ b/conans/model/conf.py @@ -46,6 +46,10 @@ "tools.apple:enable_bitcode": "(boolean) Enable/Disable Bitcode Apple Clang flags", "tools.apple:enable_arc": "(boolean) Enable/Disable ARC Apple Clang flags", "tools.apple:enable_visibility": "(boolean) Enable/Disable Visibility Apple Clang flags", + "tools.build:archiver": "Define the archiver executable name or full path", + "tools.build:c_compiler": "Define the C compiler executable name or full path", + "tools.build:cxx_compiler": "Define the C++ compiler executable name or full path", + "tools.build:linker": "Define the linker executable name or full path", "tools.build:cxxflags": "List of extra CXX flags used by different toolchains like CMakeToolchain, AutotoolsToolchain and MesonToolchain", "tools.build:cflags": "List of extra C flags used by different toolchains like CMakeToolchain, AutotoolsToolchain and MesonToolchain", "tools.build:defines": "List of extra definition flags used by different toolchains like CMakeToolchain and AutotoolsToolchain", From 49d2cd7b6ea5860809ea9b86244b2ba19bf7c6e5 Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Thu, 1 Dec 2022 23:08:32 +0100 Subject: [PATCH 27/36] some fixes following merge --- conan/tools/gnu/autotoolstoolchain.py | 2 +- conans/model/conf.py | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index 56096824676..f5d78c1e55f 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -14,7 +14,7 @@ class AutotoolsToolchain: - def __init__(self, conanfile, namespace=None, compile_wrapper=None, ar_wrapper=None): + def __init__(self, conanfile, namespace=None, prefix="/"): self._conanfile = conanfile self._namespace = namespace self._prefix = prefix diff --git a/conans/model/conf.py b/conans/model/conf.py index 31b94b50b2d..62f91da8d24 100644 --- a/conans/model/conf.py +++ b/conans/model/conf.py @@ -48,10 +48,6 @@ "tools.apple:enable_bitcode": "(boolean) Enable/Disable Bitcode Apple Clang flags", "tools.apple:enable_arc": "(boolean) Enable/Disable ARC Apple Clang flags", "tools.apple:enable_visibility": "(boolean) Enable/Disable Visibility Apple Clang flags", - "tools.build:archiver": "Define the archiver executable name or full path", - "tools.build:c_compiler": "Define the C compiler executable name or full path", - "tools.build:cxx_compiler": "Define the C++ compiler executable name or full path", - "tools.build:linker": "Define the linker executable name or full path", "tools.build:cxxflags": "List of extra CXX flags used by different toolchains like CMakeToolchain, AutotoolsToolchain and MesonToolchain", "tools.build:cflags": "List of extra C flags used by different toolchains like CMakeToolchain, AutotoolsToolchain and MesonToolchain", "tools.build:defines": "List of extra definition flags used by different toolchains like CMakeToolchain and AutotoolsToolchain", From c9949325968ef733262094533c5315ad3d806f1d Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Thu, 1 Dec 2022 23:14:31 +0100 Subject: [PATCH 28/36] more fixes --- conan/tools/gnu/autotoolstoolchain.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index f5d78c1e55f..001f091f56c 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -148,15 +148,17 @@ def _curated_path(self, exe, default=None): def _get_cc(self): cc = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("c") - return self._curated_path(cc, "cl" if is_msvc(self._conanfile) else None) + default_cc = "cl" if is_msvc(self._conanfile) else None + return self._curated_path(cc or default_cc, default_cc) def _get_cxx(self): cxx = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("cpp") - return self._curated_path(cxx, "cl" if is_msvc(self._conanfile) else None) + default_cxx = "cl" if is_msvc(self._conanfile) else None + return self._curated_path(cxx or default_cxx, default_cxx) def _get_cuda(self): cuda = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("cuda") - return self._curated_path(cxx, None) + return self._curated_path(cuda, None) def _get_fortran(self): fc = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("fortran") From c902918782f15f206a79b6ec412cbbd772f9904c Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Thu, 1 Dec 2022 23:15:58 +0100 Subject: [PATCH 29/36] minor change --- conan/tools/gnu/autotoolstoolchain.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index 001f091f56c..b2d2cc38c7e 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -156,14 +156,14 @@ def _get_cxx(self): default_cxx = "cl" if is_msvc(self._conanfile) else None return self._curated_path(cxx or default_cxx, default_cxx) - def _get_cuda(self): - cuda = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("cuda") - return self._curated_path(cuda, None) - def _get_fortran(self): fc = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("fortran") return self._curated_path(fc, None) + def _get_cuda(self): + cuda = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("cuda") + return self._curated_path(cuda, None) + def _get_ld(self): return "link" if is_msvc(self._conanfile) else None From e39360066103dd199d83250c06ab72a676ec1219 Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Thu, 1 Dec 2022 23:18:14 +0100 Subject: [PATCH 30/36] less babysitting, only curate path --- conan/tools/gnu/autotoolstoolchain.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index b2d2cc38c7e..96cbf5334a8 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -135,34 +135,32 @@ def defines(self): ret = [self.ndebug, self.gcc_cxx11_abi] + conf_flags + self.extra_defines return self._filter_list_empty_fields(ret) - def _curated_path(self, exe, default=None): + def _curated_path(self, exe): if exe: if os.path.exists(exe): if " " in exe and os.name == "nt": from conan.tools.microsoft.win32api import win32api exe = win32api.get_short_path_name(exe) exe = unix_path(self._conanfile, exe) - if " " in exe: - exe = default return exe def _get_cc(self): cc = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("c") default_cc = "cl" if is_msvc(self._conanfile) else None - return self._curated_path(cc or default_cc, default_cc) + return self._curated_path(cc or default_cc) def _get_cxx(self): cxx = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("cpp") default_cxx = "cl" if is_msvc(self._conanfile) else None - return self._curated_path(cxx or default_cxx, default_cxx) + return self._curated_path(cxx or default_cxx) def _get_fortran(self): fc = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("fortran") - return self._curated_path(fc, None) + return self._curated_path(fc) def _get_cuda(self): cuda = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("cuda") - return self._curated_path(cuda, None) + return self._curated_path(cuda) def _get_ld(self): return "link" if is_msvc(self._conanfile) else None From 49264d8d6520da6770aab79bddfe391be54d33d3 Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Thu, 1 Dec 2022 23:23:36 +0100 Subject: [PATCH 31/36] minor change --- conan/tools/gnu/autotoolstoolchain.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index 96cbf5334a8..33b7b4b539a 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -146,17 +146,19 @@ def _curated_path(self, exe): def _get_cc(self): cc = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("c") - default_cc = "cl" if is_msvc(self._conanfile) else None - return self._curated_path(cc or default_cc) + if not cc and is_msvc(self._conanfile): + cc = "cl" + return self._curated_path(cc) def _get_cxx(self): cxx = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("cpp") - default_cxx = "cl" if is_msvc(self._conanfile) else None - return self._curated_path(cxx or default_cxx) + if not cxx and is_msvc(self._conanfile): + cxx = "cl" + return self._curated_path(cxx) def _get_fortran(self): - fc = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("fortran") - return self._curated_path(fc) + fortran = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("fortran") + return self._curated_path(fortran) def _get_cuda(self): cuda = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("cuda") From 8ab97ab71125368b16de4c4ce626aee05f7cdb56 Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Thu, 1 Dec 2022 23:35:04 +0100 Subject: [PATCH 32/36] simplify curated_path --- conan/tools/gnu/autotoolstoolchain.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index 33b7b4b539a..de1f399e6b0 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -135,14 +135,13 @@ def defines(self): ret = [self.ndebug, self.gcc_cxx11_abi] + conf_flags + self.extra_defines return self._filter_list_empty_fields(ret) - def _curated_path(self, exe): - if exe: - if os.path.exists(exe): - if " " in exe and os.name == "nt": - from conan.tools.microsoft.win32api import win32api - exe = win32api.get_short_path_name(exe) - exe = unix_path(self._conanfile, exe) - return exe + def _curated_path(self, path): + if path and os.path.exists(path): + if " " in path and os.name == "nt": + from conan.tools.microsoft.win32api import win32api + path = win32api.get_short_path_name(path) + path = unix_path(self._conanfile, path) + return path def _get_cc(self): cc = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("c") From 22abcaa5a43413a63b901181c040d73a47c1ba1d Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Fri, 2 Dec 2022 02:27:58 +0100 Subject: [PATCH 33/36] only manage env vars related to tools which may be provided by tools.build:compiler_executables --- conan/tools/gnu/autotoolstoolchain.py | 65 ++++++++++----------------- 1 file changed, 24 insertions(+), 41 deletions(-) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index de1f399e6b0..84efcfafb24 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -45,17 +45,14 @@ def __init__(self, conanfile, namespace=None, prefix="/"): self.fpic = self._conanfile.options.get_safe("fPIC") self.msvc_runtime_flag = self._get_msvc_runtime_flag() - # standard build toolchains env vars - self.cc = self._get_cc() - self.cxx = self._get_cxx() - self.cuda = self._get_cuda() - self.fortran = self._get_fortran() - self.ld = self._get_ld() - self.ar = self._get_ar() - self.nm = self._get_nm() - self.objdump = self._get_objdump() - self.ranlib = self._get_ranlib() - self.strip = self._get_strip() + self._toolchain_env_vars = { + "ccas": self._get_ccas(), + "cc": self._get_cc(), + "cxx": self._get_cxx(), + "cuda": self._get_cuda(), + "fortran": self._get_fortran(), + "rc": self._get_rc(), + } # Cross build triplets self._host = self._conanfile.conf.get("tools.gnu:host_triplet") @@ -143,6 +140,10 @@ def _curated_path(self, path): path = unix_path(self._conanfile, path) return path + def _get_ccas(self): + ccas = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("asm") + return self._curated_path(ccas) + def _get_cc(self): cc = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("c") if not cc and is_msvc(self._conanfile): @@ -155,45 +156,27 @@ def _get_cxx(self): cxx = "cl" return self._curated_path(cxx) - def _get_fortran(self): - fortran = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("fortran") - return self._curated_path(fortran) - def _get_cuda(self): cuda = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("cuda") return self._curated_path(cuda) - def _get_ld(self): - return "link" if is_msvc(self._conanfile) else None - - def _get_ar(self): - return "lib" if is_msvc(self._conanfile) else None - - def _get_nm(self): - return "dumpbin -symbols" if is_msvc(self._conanfile) else None - - def _get_objdump(self): - return ":" if is_msvc(self._conanfile) else None - - def _get_ranlib(self): - return ":" if is_msvc(self._conanfile) else None + def _get_fortran(self): + fortran = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("fortran") + return self._curated_path(fortran) - def _get_strip(self): - return ":" if is_msvc(self._conanfile) else None + def _get_rc(self): + rc = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("rc") + return self._curated_path(rc) def environment(self): env = Environment() for env_var, env_var_value in [ - ("CC", self.cc), - ("CXX", self.cxx), - ("FC", self.fortran), - ("NVCC", self.cuda), - ("LD", self.ld), - ("AR", self.ar), - ("NM", self.nm), - ("OBJDUMP", self.objdump), - ("RANLIB", self.ranlib), - ("STRIP", self.strip), + ("CCAS", self._toolchain_env_vars.get("ccas")) + ("CC", self._toolchain_env_vars.get("cc")), + ("CXX", self._toolchain_env_vars.get("cxx")), + ("NVCC", self._toolchain_env_vars.get("cuda")), + ("FC", self._toolchain_env_vars.get("fortran")), + ("RC", self._toolchain_env_vars.get("rc")), ]: if env_var_value: env.define(env_var, env_var_value) From e885850fdc721fbf9698f9ac02a9a17e1da27307 Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Fri, 2 Dec 2022 02:38:02 +0100 Subject: [PATCH 34/36] do not compute these paths on instance creation --- conan/tools/gnu/autotoolstoolchain.py | 39 +++++++++++++-------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index 84efcfafb24..04662090ff3 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -45,15 +45,6 @@ def __init__(self, conanfile, namespace=None, prefix="/"): self.fpic = self._conanfile.options.get_safe("fPIC") self.msvc_runtime_flag = self._get_msvc_runtime_flag() - self._toolchain_env_vars = { - "ccas": self._get_ccas(), - "cc": self._get_cc(), - "cxx": self._get_cxx(), - "cuda": self._get_cuda(), - "fortran": self._get_fortran(), - "rc": self._get_rc(), - } - # Cross build triplets self._host = self._conanfile.conf.get("tools.gnu:host_triplet") self._build = None @@ -140,43 +131,49 @@ def _curated_path(self, path): path = unix_path(self._conanfile, path) return path - def _get_ccas(self): + @property + def _ccas(self): ccas = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("asm") return self._curated_path(ccas) - def _get_cc(self): + @property + def _cc(self): cc = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("c") if not cc and is_msvc(self._conanfile): cc = "cl" return self._curated_path(cc) - def _get_cxx(self): + @property + def _cxx(self): cxx = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("cpp") if not cxx and is_msvc(self._conanfile): cxx = "cl" return self._curated_path(cxx) - def _get_cuda(self): + @property + def _cuda(self): cuda = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("cuda") return self._curated_path(cuda) - def _get_fortran(self): + @property + def _fortran(self): fortran = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("fortran") return self._curated_path(fortran) - def _get_rc(self): + @property + def _rc(self): rc = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("rc") return self._curated_path(rc) def environment(self): env = Environment() for env_var, env_var_value in [ - ("CCAS", self._toolchain_env_vars.get("ccas")) - ("CC", self._toolchain_env_vars.get("cc")), - ("CXX", self._toolchain_env_vars.get("cxx")), - ("NVCC", self._toolchain_env_vars.get("cuda")), - ("FC", self._toolchain_env_vars.get("fortran")), - ("RC", self._toolchain_env_vars.get("rc")), + ("CCAS", self._ccas) + ("CC", self._cc), + ("CXX", self._cxx), + ("NVCC", self._cuda), + ("FC", self._fortran), + ("RC", self._rc), ]: if env_var_value: env.define(env_var, env_var_value) From 3bb2dc956f0ad252c8ffd1b83074b12e1096d997 Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Fri, 2 Dec 2022 09:11:14 +0100 Subject: [PATCH 35/36] typo --- conan/tools/gnu/autotoolstoolchain.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index 04662090ff3..beccd79dcf0 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -168,7 +168,7 @@ def _rc(self): def environment(self): env = Environment() for env_var, env_var_value in [ - ("CCAS", self._ccas) + ("CCAS", self._ccas), ("CC", self._cc), ("CXX", self._cxx), ("NVCC", self._cuda), From 31726280f623b406fa91b0a0cd8c24387762f658 Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Fri, 2 Dec 2022 09:48:33 +0100 Subject: [PATCH 36/36] simplify even more --- conan/tools/gnu/autotoolstoolchain.py | 50 ++++----------------------- 1 file changed, 6 insertions(+), 44 deletions(-) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index beccd79dcf0..911c080f9f7 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -131,52 +131,14 @@ def _curated_path(self, path): path = unix_path(self._conanfile, path) return path - @property - def _ccas(self): - ccas = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("asm") - return self._curated_path(ccas) - - @property - def _cc(self): - cc = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("c") - if not cc and is_msvc(self._conanfile): - cc = "cl" - return self._curated_path(cc) - - @property - def _cxx(self): - cxx = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("cpp") - if not cxx and is_msvc(self._conanfile): - cxx = "cl" - return self._curated_path(cxx) - - @property - def _cuda(self): - cuda = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("cuda") - return self._curated_path(cuda) - - @property - def _fortran(self): - fortran = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("fortran") - return self._curated_path(fortran) - - @property - def _rc(self): - rc = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict).get("rc") - return self._curated_path(rc) - def environment(self): env = Environment() - for env_var, env_var_value in [ - ("CCAS", self._ccas), - ("CC", self._cc), - ("CXX", self._cxx), - ("NVCC", self._cuda), - ("FC", self._fortran), - ("RC", self._rc), - ]: - if env_var_value: - env.define(env_var, env_var_value) + compilers_by_conf = self._conanfile.conf.get("tools.build:compiler_executables", default={}, check_type=dict) + if compilers_by_conf: + compilers_mapping = {"asm": "CCAS", "c": "CC", "cpp": "CXX", "cuda": "NVCC", "fortran": "FC"} + for comp, env_var in compilers_mapping.items(): + if comp in compilers_by_conf: + env.define(env_var, self._curated_path(compilers_by_conf[comp])) env.append("CPPFLAGS", ["-D{}".format(d) for d in self.defines]) env.append("CXXFLAGS", self.cxxflags) env.append("CFLAGS", self.cflags)