Skip to content
Closed
Changes from 20 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
43ceb98
set LD to link & add -nologo
SpaceIm Sep 24, 2022
44ee28c
honor CC, CXX, LD from profile
SpaceIm Sep 24, 2022
bbc8cf7
handle more cases on windows
SpaceIm Sep 24, 2022
73345fa
refactor
SpaceIm Sep 24, 2022
dddfa50
also handle NM
SpaceIm Sep 25, 2022
1eed250
refactor again
SpaceIm Sep 25, 2022
6b69fd2
formatting
SpaceIm Sep 25, 2022
6b7743c
also handle AR
SpaceIm Sep 25, 2022
9977d83
refactor again
SpaceIm Sep 25, 2022
73c2e19
small change
SpaceIm Sep 25, 2022
6e9295b
add more variables
SpaceIm Sep 25, 2022
3ad51c3
add comments
SpaceIm Sep 25, 2022
305783b
add two new attributes to prefix CC, CXX & ARLIB with compatibility w…
SpaceIm Sep 25, 2022
fc3c72c
add cc, cxx, ld, ar, nm, objdump, ranlib & trip attributes
SpaceIm Sep 25, 2022
40fdb20
add compiler_wrapper & arlib_wrapper to arguments of constructor
SpaceIm Sep 25, 2022
f03d98d
default extra_options to None
SpaceIm Sep 25, 2022
c160c7b
typo
SpaceIm Sep 25, 2022
a81f43a
rename to arlib_wrapper to ar_wrapper
SpaceIm Sep 25, 2022
6a415fd
use VirtualBuildEnv to get env vars from profile
SpaceIm Sep 25, 2022
746bff5
do not add -nologo to AR if msvc
SpaceIm Sep 25, 2022
ea62645
don't check existence of wrapper
SpaceIm Sep 25, 2022
a2a2a5d
do not allow spaces in env var paths for autotools
SpaceIm Sep 25, 2022
7d4adf1
minor change
SpaceIm Sep 25, 2022
b539767
improve windows support
SpaceIm Sep 30, 2022
47b8213
move import of win32api where it is used
SpaceIm Sep 30, 2022
a3f77e1
add 4 conf to define build executables
SpaceIm Oct 1, 2022
39001d3
Merge branch 'develop' into autotoolstoolchain-msvc
SpaceIm Dec 1, 2022
49d2cd7
some fixes following merge
SpaceIm Dec 1, 2022
c994932
more fixes
SpaceIm Dec 1, 2022
c902918
minor change
SpaceIm Dec 1, 2022
e393600
less babysitting, only curate path
SpaceIm Dec 1, 2022
49264d8
minor change
SpaceIm Dec 1, 2022
8ab97ab
simplify curated_path
SpaceIm Dec 1, 2022
22abcaa
only manage env vars related to tools which may be provided by tools.…
SpaceIm Dec 2, 2022
e885850
do not compute these paths on instance creation
SpaceIm Dec 2, 2022
3bb2dc9
typo
SpaceIm Dec 2, 2022
3172628
simplify even more
SpaceIm Dec 2, 2022
b878025
Merge branch 'develop' into autotoolstoolchain-msvc
SpaceIm Jan 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 107 additions & 6 deletions conan/tools/gnu/autotoolstoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@
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
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
import os


class AutotoolsToolchain:
def __init__(self, conanfile, namespace=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._ar_wrapper = ar_wrapper

self.configure_args = self._default_configure_shared_flags() + self._default_configure_install_flags()
self.autoreconf_args = self._default_autoreconf_flags()
Expand Down Expand Up @@ -43,6 +46,16 @@ def __init__(self, conanfile, namespace=None):
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.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
self._build = None
Expand Down Expand Up @@ -120,11 +133,99 @@ def defines(self):
ret = [self.ndebug, self.gcc_cxx11_abi] + conf_flags + self.extra_defines
return self._filter_list_empty_fields(ret)

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,
)

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,
)

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 [],
)

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
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 [],
)

def _get_objdump(self):
return self._exe_env_var_to_unix_path(
"OBJDUMP",
":" 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,
)

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.
"""
exe = VirtualBuildEnv(self._conanfile).vars().get(env_var)
Copy link
Contributor Author

@SpaceIm SpaceIm Sep 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested and it's always None, regardless of env var I put under [env] in my profile (tested with 1 & 2 profiles).

I've tried with VirtualBuildEnv and conans.tools.get_env. What's wrong? I see that MesonToolchain also tries to get CC, CXX etc from profile with VirtualBuildEnv, does it work actually?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have much experience with conan 2.0 (yet), but did you use a [buildenv] section in your host profile? See this link.

Copy link
Contributor Author

@SpaceIm SpaceIm Sep 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed thanks, it honors env from profile now.

But now I see two issues:

  • if VirtualBuildEnv is called after AutotoolsToolchain in generate() of a recipe (I'm testing with m4: conan v2 support conan-center-index#13135), it overrides all the CC etc logic of AutotoolsToolchain, so it's very important to call VirtualBuildEnv before any other generator also relying on Environment to set env vars.
  • if there is a space in these paths (which is likely the case for cl, link in Program Files), even unix_path doesn't help (nor protecting spaces with \, or protecting full path with quotes, and I've tested with and without env vars wrapped by compile & ar-lib, errors either happen in those wrappers or later on in configure).
    For example for CC, C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.33.31629\bin\Hostx64\x64\cl.exe in profile is converted to /c/users/spaceim/.conan/data/m4/1.4.19/_/_/build/0a420ff5c47119e668867cdb51baff0eca1fdb68/src/build-aux/compile /c/program files/microsoft visual studio/2022/community/vc/tools/msvc/14.33.31629/bin/hostx64/x64/cl.exe -nologo in m4: conan v2 support conan-center-index#13135.

Copy link
Contributor Author

@SpaceIm SpaceIm Sep 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the second point, the best I can do is display a warning and suggest to set CC etc with short file names.

For example on my computer I can set in profile:

[buildenv]
CC=C:\PROGRA~1\MIB055~1\2022\Community\VC\Tools\MSVC\14.33.31629\bin\Hostx64\x64\cl.exe

then it works and full path is honored

checking for gcc... /c/users/spaceim/.conan/data/m4/1.4.19/_/_/build/0a420ff5c47119e668867cdb51baff0eca1fdb68/src/build-aux/compile /c/progra~1/mib055~1/2022/community/vc/tools/msvc/14.33.31629/bin/hostx64/x64/cl.exe -nologo
checking whether the C compiler works... yes
checking for C compiler default output file name... conftest.exe
checking for suffix of executables... .exe
checking whether we are cross compiling... no
checking for suffix of object files... obj
checking whether the compiler supports GNU C... no
checking whether /c/users/spaceim/.conan/data/m4/1.4.19/_/_/build/0a420ff5c47119e668867cdb51baff0eca1fdb68/src/build-aux/compile /c/progra~1/mib055~1/2022/community/vc/tools/msvc/14.33.31629/bin/hostx64/x64/cl.exe -nologo accepts -g... no
checking for /c/users/spaceim/.conan/data/m4/1.4.19/_/_/build/0a420ff5c47119e668867cdb51baff0eca1fdb68/src/build-aux/compile /c/progra~1/mib055~1/2022/community/vc/tools/msvc/14.33.31629/bin/hostx64/x64/cl.exe -nologo option to enable C11 features... unsupported
checking for /c/users/spaceim/.conan/data/m4/1.4.19/_/_/build/0a420ff5c47119e668867cdb51baff0eca1fdb68/src/build-aux/compile /c/progra~1/mib055~1/2022/community/vc/tools/msvc/14.33.31629/bin/hostx64/x64/cl.exe -nologo option to enable C99 features... unsupported
checking for /c/users/spaceim/.conan/data/m4/1.4.19/_/_/build/0a420ff5c47119e668867cdb51baff0eca1fdb68/src/build-aux/compile /c/progra~1/mib055~1/2022/community/vc/tools/msvc/14.33.31629/bin/hostx64/x64/cl.exe -nologo option to enable C89 features... unsupported
checking whether /c/users/spaceim/.conan/data/m4/1.4.19/_/_/build/0a420ff5c47119e668867cdb51baff0eca1fdb68/src/build-aux/compile /c/progra~1/mib055~1/2022/community/vc/tools/msvc/14.33.31629/bin/hostx64/x64/cl.exe -nologo understands -c and -o together... yes
checking whether the compiler is clang... no
checking for compiler option needed when checking for declarations... none
checking whether make supports the include directive... yes (GNU style)
checking dependency style of /c/users/spaceim/.conan/data/m4/1.4.19/_/_/build/0a420ff5c47119e668867cdb51baff0eca1fdb68/src/build-aux/compile /c/progra~1/mib055~1/2022/community/vc/tools/msvc/14.33.31629/bin/hostx64/x64/cl.exe -nologo... msvc7
checking for stdio.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for strings.h... no
checking for sys/stat.h... yes
checking for sys/types.h... yes
checking for unistd.h... no
checking for wchar.h... yes
checking for minix/config.h... no
checking for pthread.h... no
checking for sys/param.h... no
checking for sys/socket.h... no
checking for dirent.h... no
checking for stdio_ext.h... no
checking for getopt.h... no
checking for sys/cdefs.h... no
checking for threads.h... no
checking for iconv.h... no
checking for limits.h... yes
checking for crtdefs.h... yes
checking for wctype.h... yes
checking for langinfo.h... no
checking for xlocale.h... no
checking for math.h... yes
checking for sys/mman.h... no
checking for malloc.h... yes
checking for spawn.h... no
checking for sys/time.h... no
checking for sys/random.h... no
checking for sys/wait.h... no
checking for features.h... no
checking for arpa/inet.h... no
checking for semaphore.h... no
checking for netdb.h... no
checking for netinet/in.h... no
checking for sys/select.h... no
checking for sys/ioctl.h... no
checking for sys/uio.h... no
checking whether it is safe to define __EXTENSIONS__... yes
checking whether _XOPEN_SOURCE should be defined... no
checking build system type... x86_64-pc-mingw64
checking host system type... x86_64-pc-mingw64
checking how to run the C preprocessor... /c/users/spaceim/.conan/data/m4/1.4.19/_/_/build/0a420ff5c47119e668867cdb51baff0eca1fdb68/src/build-aux/compile /c/progra~1/mib055~1/2022/community/vc/tools/msvc/14.33.31629/bin/hostx64/x64/cl.exe -nologo -E

if exe:
if os.path.exists(exe):
exe = unix_path(self._conanfile, exe)
else:
exe = default
if exe:
if wrapper:
if os.path.exists(wrapper):
wrapper = unix_path(self._conanfile, wrapper)
exe = f"{wrapper} {exe}"
if extra_options:
for option in extra_options:
if option not in exe:
exe += f" {option}"
return exe

def environment(self):
env = Environment()
if is_msvc(self._conanfile):
env.define("CXX", "cl")
env.define("CC", "cl")
build_env = VirtualBuildEnv(self._conanfile).vars()
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 != 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)
env.append("CFLAGS", self.cflags)
Expand Down