Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceIm committed Sep 24, 2022
1 parent bbc8cf7 commit 134a4a3
Showing 1 changed file with 33 additions and 34 deletions.
67 changes: 33 additions & 34 deletions conan/tools/gnu/autotoolstoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 is not defined or does not point to an existing path, 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) else None
default_linker = "link" if is_msvc(self) else None
extra_options = ["-nologo"] if is_msvc(self) 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)
Expand Down

0 comments on commit 134a4a3

Please sign in to comment.