Build helpers

There are several helpers that can assist to automate the build() method for popular build systems:

CMake

The CMake class helps us to invoke cmake command with the generator, flags and definitions, reflecting the specified Conan settings.

There are two ways to invoke your cmake tools:

  • Using the helper attributes cmake.command_line and cmake.build_config:
def build(self):
   cmake = CMake(self)
   self.run('cmake "%s" %s' % (self.conanfile_directory, cmake.command_line))
   self.run('cmake --build . %s' % cmake.build_config)
  • Using the helper methods:
def build(self):
   cmake = CMake(self)
   cmake.configure(source_dir=self.conanfile_directory, build_dir="./")
   cmake.build()

See also

Check the section Reference/Build Helpers/CMake to find out more.

Autotools: configure / make

If you are using configure/make you can use AutoToolsBuildEnvironment helper. This helper sets LIBS, LDFLAGS, CFLAGS, CXXFLAGS and CPPFLAGS environment variables based on your requirements.

It works using the environment_append context manager applied to your configure and make commands:

from conans import ConanFile, AutoToolsBuildEnvironment

class ExampleConan(ConanFile):
   settings = "os", "compiler", "build_type", "arch"
   requires = "Poco/1.7.8p3@pocoproject/stable"
   default_options = "Poco:shared=True", "OpenSSL:shared=True"

   def imports(self):
      self.copy("*.dll", dst="bin", src="bin")
      self.copy("*.dylib*", dst="bin", src="lib")

   def build(self):
      env_build = AutoToolsBuildEnvironment(self)
      with tools.environment_append(env_build.vars):
         self.run("./configure")
         self.run("make")

For Windows users:

from conans import ConanFile, AutoToolsBuildEnvironment

class ExampleConan(ConanFile):
   settings = "os", "compiler", "build_type", "arch"
   requires = "Poco/1.7.8p3@pocoproject/stable"
   default_options = "Poco:shared=True", "OpenSSL:shared=True"

   def _run_cmd(self, command):
     if self.settings.os == "Windows":
         tools.run_in_windows_bash(self, command)
     else:
         self.run(command)

   def imports(self):
     self.copy("*.dll", dst="bin", src="bin")
     self.copy("*.dylib*", dst="bin", src="lib")

   def build(self):
      env_build = AutoToolsBuildEnvironment(self)
      with tools.environment_append(env_build.vars):
         self._run_cmd("./configure")
         self._run_cmd("make")

You can change some variables like .fpic, .libs, .include_paths, defines before accessing the vars to override an automatic value or add new values:

from conans import ConanFile, AutoToolsBuildEnvironment

class ExampleConan(ConanFile):
   ...

   def build(self):
      env_build = AutoToolsBuildEnvironment(self)
      env_build.fpic = True
      env_build.libs.append("pthread")
      env_build.defines.append("NEW_DEFINE=23")

      with tools.environment_append(env_build.vars):
         self.run("./configure")
         self.run("make")

See also

Check the Reference/Build Helpers/AutoToolsBuildEnvironment to see the complete reference.

Visual Studio

You can invoke your Visual Studio compiler from command line using the VisualStudioBuildEnvironment and the vcvars_command tool, that will point to your Visual Studio installation.

Example:

 from conans import ConanFile, VisualStudioBuildEnvironment, tools

 class ExampleConan(ConanFile):
   ...

   def build(self):
      if self.settings.compiler == "Visual Studio":
         env_build = VisualStudioBuildEnvironment(self)
         with tools.environment_append(env_build.vars):
             vcvars = tools.vcvars_command(self.settings)
             self.run('%s && cl /c /EHsc hello.cpp' % vcvars)
             self.run('%s && lib hello.obj -OUT:hello.lib' % vcvars

See also

Check the Reference/Build Helpers/VisualStudioBuildEnvironment to see the complete reference.

GCC or Clang

You could use the gcc generator directly to build your source code. It’s valid to invoke both gcc and clang compilers.

from conans import ConanFile

class PocoTimerConan(ConanFile):
   settings = "os", "compiler", "build_type", "arch"
   requires = "Poco/1.7.8p3@pocoproject/stable"
   generators = "gcc"
   default_options = "Poco:shared=True", "OpenSSL:shared=True"

   def imports(self):
      self.copy("*.dll", dst="bin", src="bin") # From bin to bin
      self.copy("*.dylib*", dst="bin", src="lib") # From lib to bin

   def build(self):
      self.run("mkdir -p bin")
      command = 'g++ timer.cpp @conanbuildinfo.gcc -o bin/timer'
      self.run(command)

See also

Check the Reference/Generators/gcc for the complete reference.

RunEnvironment

The RunEnvironment helper prepare PATH, LD_LIBRARY_PATH and DYLIB_LIBRARY_PATH environment variables to locate shared libraries and executables of your requirements at runtime.

This helper is specially useful:

  • If you are requiring packages with shared libraries and you are running some executable that needs those libraries.
  • If you have a requirement with some tool (executable) and you need it in the path.

Example:

from conans import ConanFile, AutoToolsBuildEnvironment

class ExampleConan(ConanFile):
   ...

   def build(self):
      env_build = RunEnvironment(self)
      with tools.environment_append(env_build.vars):
         self.run("....")
         # All the requirements bin folder will be available at PATH
         # All the lib folders will be available in LD_LIBRARY_PATH and DYLIB_LIBRARY_PATH

See also

Check the Reference/Build Helpers/RunEnvironment to see the complete reference.