From af94fe403cdf1f507aad992582c7346f608b48bb Mon Sep 17 00:00:00 2001 From: memsharded Date: Tue, 1 Jul 2025 15:13:26 +0200 Subject: [PATCH 1/2] adding conan build in the tutorial --- .../the_flexibility_of_conanfile_py.rst | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tutorial/consuming_packages/the_flexibility_of_conanfile_py.rst b/tutorial/consuming_packages/the_flexibility_of_conanfile_py.rst index 2f06efb80b47..025f6c9338f5 100644 --- a/tutorial/consuming_packages/the_flexibility_of_conanfile_py.rst +++ b/tutorial/consuming_packages/the_flexibility_of_conanfile_py.rst @@ -394,6 +394,42 @@ demonstrates how to import bindings for the library depending on the graphics AP tools` without the need to copy them to the local folder. + +Use the build() method and ``conan build`` command +-------------------------------------------------- + +If you have a recipe that implements a ``build()`` method, then it is possible to automatically call +the full ``conan install + cmake + cmake `` flow with a single command. Though this +might not be the typical developer flow, it can be a convenient shortcut in some cases. + +You can try it with: + +.. code-block:: bash + + $ mkdir myproj && cd myproj + $ conan new cmake_lib + $ conan build . + + +The ``conan new`` command creates a simple project containing a ``conanfile.py``, a ``CMakeLists.txt`` and some C++ code. +If you inspect the ``conanfile.py``, you can see it contains a ``build()`` method, with the instructions how to +configure and build this project, using some Conan helpers such as ``cmake.configure()`` and ``cmake.build()``. + +It is not important to fully understand the ``build()`` method now, it will explained later in the tutorial. +The ``conan build .`` command will automatically call ``conan install .`` (``conan build`` can receive all the same +arguments, profiles, settings, etc. as ``install``) to install the dependencies and call the ``generarators``, +and after it finishes, it will call the ``build()`` method, which in turns call the CMake configure and build steps, +doing a local build of the project. + +.. note:: + + **Best practices** + + The ``conan build`` command does not intend to replace or change the typical developer flow using CMake and + other build tools, and using their IDEs. It is just a convenient shortcut for cases in which we want to locally + build a project easily without having to type several commands or use the IDE. + + .. seealso:: - :ref:`Using "cmake_layout" + "CMakeToolchain" + "CMakePresets feature" to build your project`. From 0a5c95db7d88165e7b925634a5d48aadbfb2eff1 Mon Sep 17 00:00:00 2001 From: memsharded Date: Wed, 2 Jul 2025 11:01:04 +0200 Subject: [PATCH 2/2] review, continuing example --- .../the_flexibility_of_conanfile_py.rst | 55 ++++++++++++++----- 1 file changed, 41 insertions(+), 14 deletions(-) diff --git a/tutorial/consuming_packages/the_flexibility_of_conanfile_py.rst b/tutorial/consuming_packages/the_flexibility_of_conanfile_py.rst index 025f6c9338f5..e2faf793532f 100644 --- a/tutorial/consuming_packages/the_flexibility_of_conanfile_py.rst +++ b/tutorial/consuming_packages/the_flexibility_of_conanfile_py.rst @@ -399,27 +399,54 @@ Use the build() method and ``conan build`` command -------------------------------------------------- If you have a recipe that implements a ``build()`` method, then it is possible to automatically call -the full ``conan install + cmake + cmake `` flow with a single command. Though this +the full ``conan install + cmake + cmake `` (or call the build system that the ``build()`` +method uses) flow with a single command. Though this might not be the typical developer flow, it can be a convenient shortcut in some cases. -You can try it with: +Let's add this ``build()`` method to our recipe: + +.. code-block:: python + + from conan import ConanFile + from conan.tools.cmake import CMake + + class CompressorRecipe(ConanFile): + settings = "os", "compiler", "build_type", "arch" + generators = "CMakeToolchain", "CMakeDeps" + + ... + + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build() + + +So now we can just call ``conan build .``: .. code-block:: bash - $ mkdir myproj && cd myproj - $ conan new cmake_lib $ conan build . + ... + Graph root + conanfile.py: ...\conanfile.py + Requirements + zlib/1.2.11#bfceb3f8904b735f75c2b0df5713b1e6 - Downloaded (conancenter) + Build requirements + cmake/3.22.6#32cced101c6df0fab43e8d00bd2483eb - Downloaded (conancenter) + + ======== Calling build() ======== + conanfile.py: Calling build() + conanfile.py: Running CMake.configure() + conanfile.py: RUN: cmake -G "Visual Studio 17 2022" -DCMAKE_TOOLCHAIN_FILE="conan_toolchain.cmake" + ... + conanfile.py: Running CMake.build() + conanfile.py: RUN: cmake --build "...\conanfile_py" --config Release - -The ``conan new`` command creates a simple project containing a ``conanfile.py``, a ``CMakeLists.txt`` and some C++ code. -If you inspect the ``conanfile.py``, you can see it contains a ``build()`` method, with the instructions how to -configure and build this project, using some Conan helpers such as ``cmake.configure()`` and ``cmake.build()``. - -It is not important to fully understand the ``build()`` method now, it will explained later in the tutorial. -The ``conan build .`` command will automatically call ``conan install .`` (``conan build`` can receive all the same -arguments, profiles, settings, etc. as ``install``) to install the dependencies and call the ``generarators``, -and after it finishes, it will call the ``build()`` method, which in turns call the CMake configure and build steps, -doing a local build of the project. +And we will see how it manages first to install the dependencies, then it will be calling the ``build()`` +method, that calls CMake configure and build step for us. Because the ``conan build .`` does internally +a ``conan install``, it can receive the same arguments (profile, settings, options, lockfile, etc.) as +``conan install``. .. note::