Environment

Warning

This is a very experimental feature and it will have breaking changes in future releases.

Environment is a class that helps defining modifications to the environment variables. This class is used by other tools like the conan.tools.gnu autotools helpers.

It allows different operations like:

from conan.tools.env import Environment

env = Environment()
env.define("MYVAR1", "MyValue1")  # Overwrite previously existing MYVAR1 with new value
env.append("MYVAR2", "MyValue2")  # Append to existing MYVAR2 the new value
env.prepend("MYVAR3", "MyValue3") # Prepend to existing MYVAR3 the new value
env.unset("MYVAR4")               # Remove MYVAR4 definition from environment

# And the equivalent with paths
env.define_path("MYPATH1", "path/one")  # Overwrite previously existing MYPATH1 with new value
env.append_path("MYPATH2", "path/two")  # Append to existing MYPATH2 the new value
env.prepend_path("MYPATH3", "path/three") # Prepend to existing MYPATH3 the new value

Normal variables will be appended by default with space, but separator argument can be provided to define a custom one. Path variables will be appended with the default system path separator, either : or ;, but it also allows defining which one.

Environments can compose:

from conan.tools.env import Environment

env1 = Environment()
env1.define(...)
env2 = Environment()
env2.append(...)

env1.compose(env2) # env1 has priority, and its modifications will prevail

There are some places where this Environment is used:

  • In recipes package_info() method, in new self.buildenv_info and self.runenv_info.

  • In other generators like AutootoolsDeps and AutotoolsToolchain that need to define environment

  • In profiles new [buildenv] and [runenv] sections.

The definition in package_info() is as follow, taking into account that both self.buildenv_info and self.runenv_info are objects of Environment() class.

from conans import ConanFile

class App(ConanFile):
    name = "mypkg"
    version = "1.0"
    settings = "os", "arch", "compiler", "build_type"

    def package_info(self):
        # This is information needed by consumers to build using this package
        self.buildenv_info.append("MYVAR", "MyValue")
        self.buildenv_info.prepend_path("MYPATH", "some/path/folder")

        # This is information needed by consumers to run apps that depends on this package
        # at runtime
        self.runenv_info.define("MYPKG_DATA_DIR", os.path.join(self.package_folder,
                                                               "datadir"))