AutotoolsToolchain

The AutotoolsToolchain is the toolchain generator for Autotools. It will generate shell scripts containing environment variable definitions that the autotools build system can understand.

This generator can be used by name in conanfiles:

conanfile.py

  1. class Pkg(ConanFile):
  2. generators = "AutotoolsToolchain"

conanfile.txt

  1. [generators]
  2. AutotoolsToolchain

And it can also be fully instantiated in the conanfile generate() method:

  1. from conan import ConanFile
  2. from conan.tools.gnu import AutotoolsToolchain
  3. class App(ConanFile):
  4. settings = "os", "arch", "compiler", "build_type"
  5. def generate(self):
  6. tc = AutotoolsToolchain(self)
  7. tc.generate()

Generated files

It will generate the file conanautotoolstoolchain.sh or conanautotoolstoolchain.bat files:

  1. $ conan install conanfile.py # default is Release
  2. $ source conanautotoolstoolchain.sh
  3. # or in Windows
  4. $ conanautotoolstoolchain.bat

This launchers will append information to the CPPFLAGS, LDFLAGS, CXXFLAGS, CFLAGS environment variables that translate the settings and options to the corresponding build flags like -stdlib=libstdc++, -std=gnu14, architecture flags, etc. It will also append the folder where the Conan generators are located to the PKG_CONFIG_PATH environment variable.

Since Conan 2.4.0, in a cross-building context, the environment variables CC_FOR_BUILD and CXX_FOR_BUILD are also set if the build profile defines the c and cpp values in the configuration variable tools.build:compiler_executables. See more info in the conf section.

This generator will also generate a file called conanbuild.conf containing two keys:

  • configure_args: Arguments to call the configure script.

  • make_args: Arguments to call the make script.

  • autoreconf_args: Arguments to call the autoreconf script.

The Autotools build helper will use that conanbuild.conf file to seamlessly call the configure and make script using these precalculated arguments.

Customization

You can change some attributes before calling the generate() method if you want to change some of the precalculated values:

  1. from conan import ConanFile
  2. from conan.tools.gnu import AutotoolsToolchain
  3. class App(ConanFile):
  4. settings = "os", "arch", "compiler", "build_type"
  5. def generate(self):
  6. tc = AutotoolsToolchain(self)
  7. tc.configure_args.append("--my_argument")
  8. tc.generate()
  • configure_args: Additional arguments to be passed to the configure script.

    • By default the following arguments are passed:

      • --prefix: Takes / as default value.

      • --bindir=${prefix}/bin

      • --sbindir=${prefix}/bin

      • --libdir=${prefix}/lib

      • --includedir=${prefix}/include

      • --oldincludedir=${prefix}/include

      • --datarootdir=${prefix}/res

  1. - Also if the shared option exists it will add by default:
  2. - `--enable-shared`, `--disable-static` if `shared==True`
  3. - `--disable-shared`, `--enable-static` if `shared==False`
  • make_args (Defaulted to []): Additional arguments to be passed to he make script.

  • autoreconf_args (Defaulted to ["--force", "--install"]): Additional arguments to be passed to he make script.

  • extra_defines (Defaulted to []): Additional defines.

  • extra_cxxflags (Defaulted to []): Additional cxxflags.

  • extra_cflags (Defaulted to []): Additional cflags.

  • extra_ldflags (Defaulted to []): Additional ldflags.

  • ndebug: “NDEBUG” if the settings.build_type != Debug.

  • gcc_cxx11_abi: “_GLIBCXX_USE_CXX11_ABI” if gcc/libstdc++.

  • libcxx: Flag calculated from settings.compiler.libcxx.

  • fpic: True/False from options.fpic if defined.

  • cppstd: Flag from settings.compiler.cppstd

  • arch_flag: Flag from settings.arch

  • build_type_flags: Flags from settings.build_type

  • sysroot_flag: To pass the --sysroot flag to the compiler.

  • apple_arch_flag: Only when cross-building with Apple systems. Flags from settings.arch.

  • apple_isysroot_flag: Only when cross-building with Apple systems. Path to the root sdk.

  • msvc_runtime_flag: Flag from settings.compiler.runtime_type when compiler is msvc or settings.compiler.runtime when using the deprecated Visual Studio.

The following attributes are ready-only and will contain the calculated values for the current configuration and customized attributes. Some recipes might need to read them to generate custom build files (not strictly Autotools) with the configuration:

  • defines

  • cxxflags

  • cflags

  • ldflags

  1. from conan import ConanFile
  2. from conan.tools.gnu import AutotoolsToolchain
  3. class App(ConanFile):
  4. settings = "os", "arch", "compiler", "build_type"
  5. def generate(self):
  6. tc = AutotoolsToolchain(self)
  7. # Customize the flags
  8. tc.extra_cxxflags = ["MyFlag"]
  9. # Read the computed flags and use them (write custom files etc)
  10. tc.defines
  11. tc.cxxflags
  12. tc.cflags
  13. tc.ldflags

If you want to change the default values for configure_args, adjust the cpp.package object at the layout() method:

  1. def layout(self):
  2. ...
  3. # For bindir and sbindir takes the first value:
  4. self.cpp.package.bindirs = ["mybin"]
  5. # For libdir takes the first value:
  6. self.cpp.package.libdirs = ["mylib"]
  7. # For includedir and oldincludedir takes the first value:
  8. self.cpp.package.includedirs = ["myinclude"]
  9. # For datarootdir takes the first value:
  10. self.cpp.package.resdirs = ["myres"]

Note

It is not valid to change the self.cpp_info at the package_info() method.

Customizing the environment

If your Makefile or configure scripts need some other environment variable rather than CPPFLAGS, LDFLAGS, CXXFLAGS or CFLAGS, you can customize it before calling the generate() method. Call the environment() method to calculate the mentioned variables and then add the variables that you need. The environment() method returns an Environment object:

  1. from conan import ConanFile
  2. from conan.tools.gnu import AutotoolsToolchain
  3. class App(ConanFile):
  4. settings = "os", "arch", "compiler", "build_type"
  5. def generate(self):
  6. at = AutotoolsToolchain(self)
  7. env = at.environment()
  8. env.define("FOO", "BAR")
  9. at.generate(env)

The AutotoolsToolchain also sets CXXFLAGS, CFLAGS, LDFLAGS and CPPFLAGS reading variables from the [conf] section in the profiles. See the conf reference below.

Managing the configure_args, make_args and autoreconf_args attributes

AutotoolsToolchain provides some help methods so users can add/update/remove values defined in configure_args, make_args and autoreconf_args (all of them lists of strings). Those methods are:

  • update_configure_args(updated_flags): will change AutotoolsToolchain.configure_args.

  • update_make_args(updated_flags): will change AutotoolsToolchain.make_args.

  • update_autoreconf_args(updated_flags): will change AutotoolsToolchain.autoreconf_args.

Where updated_flags is a dict-like Python object defining all the flags to change. It follows the next rules:

  • Key-value are the flags names and their values, e.g., {"--enable-tools": no} will be translated as --enable-tools=no.

  • If that key has no value, then it will be an empty string, e.g., {"--disable-verbose": ""} will be translated as --disable-verbose.

  • If the key value is None, it means that you want to remove that flag from the xxxxxx_args (notice that it could be configure_args, make_args or autoreconf_args), e.g., {"--force": None} will remove that flag from the final result.

In a nutshell, you will:

  • Add arguments: if the given flag in updated_flags does not already exist in xxxxxx_args.

  • Update arguments: if the given flag in updated_flags already exists in attribute xxxxxx_args.

  • Remove arguments: if the given flag in updated_flags already exists in xxxxxx_args and it’s passed with None as value.

For instance:

  1. from conan import ConanFile
  2. from conan.tools.gnu import AutotoolsToolchain
  3. class App(ConanFile):
  4. settings = "os", "arch", "compiler", "build_type"
  5. def generate(self):
  6. at = AutotoolsToolchain(self)
  7. at.update_configure_args({
  8. "--new-super-flag": "", # add new flag '--new-super-flag'
  9. "--host": "my-gnu-triplet", # update flag '--host=my-gnu-triplet'
  10. "--force": None # remove existing '--force' flag
  11. })
  12. at.generate()

Reference

class AutotoolsToolchain(conanfile, namespace=None, prefix=’/‘)

  • Parameters:

    • conanfile – The current recipe object. Always use self.

    • namespace – This argument avoids collisions when you have multiple toolchain calls in the same recipe. By setting this argument, the conanbuild.conf file used to pass information to the build helper will be named as <namespace>_conanbuild.conf. The default value is None meaning that the name of the generated file is conanbuild.conf. This namespace must be also set with the same value in the constructor of the Autotools build helper so that it reads the information from the proper file.

    • prefix – Folder to use for --prefix argument (“/” by default).

  • update_configure_args(updated_flags)

    Helper to update/prune flags from self.configure_args.

    • Parameters:

      updated_flagsdict with arguments as keys and their argument values. Notice that if argument value is None, this one will be pruned.

  • update_make_args(updated_flags)

    Helper to update/prune arguments from self.make_args.

    • Parameters:

      updated_flagsdict with arguments as keys and their argument values. Notice that if argument value is None, this one will be pruned.

  • update_autoreconf_args(updated_flags)

    Helper to update/prune arguments from self.autoreconf_args.

    • Parameters:

      updated_flagsdict with arguments as keys and their argument values. Notice that if argument value is None, this one will be pruned.

conf

  • tools.build:cxxflags list of extra C++ flags that will be used by CXXFLAGS.

  • tools.build:cflags list of extra of pure C flags that will be used by CFLAGS.

  • tools.build:sharedlinkflags list of extra linker flags that will be used by LDFLAGS.

  • tools.build:exelinkflags list of extra linker flags that will be used by LDFLAGS.

  • tools.build:defines list of preprocessor definitions that will be used by CPPFLAGS.

  • tools.build:linker_scripts list of linker scripts, each of which will be prefixed with -T and added to LDFLAGS. Only use this flag with linkers that supports specifying linker scripts with the -T flag, such as ld, gold, and lld.

  • tools.build:sysroot defines the --sysroot flag to the compiler.

  • tools.android:ndk_path (new since Conan 2.5.0) argument for NDK path in case of Android cross-compilation. It is used to set some environment variables like CC, CXX, LD, STRIP, RANLIB, AS, and AR in the conanautotoolstoolchain.sh|bat script.

  • tools.build:compiler_executables dict-like Python object which specifies the compiler as key and the compiler executable path as value. Those keys will be mapped as follows:

    • c: will set CC (and CC_FOR_BUILD if cross-building) in conanautotoolstoolchain.sh|bat script.

    • cpp: will set CXX (and CXX_FOR_BUILD if cross-building) in conanautotoolstoolchain.sh|bat script.

    • rc: will set RC in conanautotoolstoolchain.sh|bat script.

    • cuda: will set NVCC in conanautotoolstoolchain.sh|bat script.

    • fortran: will set FC in conanautotoolstoolchain.sh|bat script.

Note

flags order of preference: Flags specified in the tools.build configuration, such as cxxflags, cflags, sharedlinkflags, exelinkflags, and defines, will always take precedence over those set by the AutotoolsToolchain attributes.