Skip to content

Commit 2cef576

Browse files
authored
adding conan build in the tutorial (#4152)
* adding conan build in the tutorial * review, continuing example
1 parent 162a951 commit 2cef576

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

tutorial/consuming_packages/the_flexibility_of_conanfile_py.rst

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,69 @@ demonstrates how to import bindings for the library depending on the graphics AP
394394
tools<conan_tools_env_virtualrunenv>` without the need to copy them to the local
395395
folder.
396396

397+
398+
Use the build() method and ``conan build`` command
399+
--------------------------------------------------
400+
401+
If you have a recipe that implements a ``build()`` method, then it is possible to automatically call
402+
the full ``conan install + cmake <configure> + cmake <build>`` (or call the build system that the ``build()``
403+
method uses) flow with a single command. Though this
404+
might not be the typical developer flow, it can be a convenient shortcut in some cases.
405+
406+
Let's add this ``build()`` method to our recipe:
407+
408+
.. code-block:: python
409+
410+
from conan import ConanFile
411+
from conan.tools.cmake import CMake
412+
413+
class CompressorRecipe(ConanFile):
414+
settings = "os", "compiler", "build_type", "arch"
415+
generators = "CMakeToolchain", "CMakeDeps"
416+
417+
...
418+
419+
def build(self):
420+
cmake = CMake(self)
421+
cmake.configure()
422+
cmake.build()
423+
424+
425+
So now we can just call ``conan build .``:
426+
427+
.. code-block:: bash
428+
429+
$ conan build .
430+
...
431+
Graph root
432+
conanfile.py: ...\conanfile.py
433+
Requirements
434+
zlib/1.2.11#bfceb3f8904b735f75c2b0df5713b1e6 - Downloaded (conancenter)
435+
Build requirements
436+
cmake/3.22.6#32cced101c6df0fab43e8d00bd2483eb - Downloaded (conancenter)
437+
438+
======== Calling build() ========
439+
conanfile.py: Calling build()
440+
conanfile.py: Running CMake.configure()
441+
conanfile.py: RUN: cmake -G "Visual Studio 17 2022" -DCMAKE_TOOLCHAIN_FILE="conan_toolchain.cmake"
442+
...
443+
conanfile.py: Running CMake.build()
444+
conanfile.py: RUN: cmake --build "...\conanfile_py" --config Release
445+
446+
And we will see how it manages first to install the dependencies, then it will be calling the ``build()``
447+
method, that calls CMake configure and build step for us. Because the ``conan build .`` does internally
448+
a ``conan install``, it can receive the same arguments (profile, settings, options, lockfile, etc.) as
449+
``conan install``.
450+
451+
.. note::
452+
453+
**Best practices**
454+
455+
The ``conan build`` command does not intend to replace or change the typical developer flow using CMake and
456+
other build tools, and using their IDEs. It is just a convenient shortcut for cases in which we want to locally
457+
build a project easily without having to type several commands or use the IDE.
458+
459+
397460
.. seealso::
398461

399462
- :ref:`Using "cmake_layout" + "CMakeToolchain" + "CMakePresets feature" to build your project<examples-tools-cmake-toolchain-build-project-presets>`.

0 commit comments

Comments
 (0)