Skip to content

Commit

Permalink
Fix conanfile.py tutorial layout (#3234)
Browse files Browse the repository at this point in the history
* wip

* minor changes
  • Loading branch information
czoido authored May 26, 2023
1 parent 11fe4e5 commit 5143a67
Showing 1 changed file with 23 additions and 26 deletions.
49 changes: 23 additions & 26 deletions tutorial/consuming_packages/the_flexibility_of_conanfile_py.rst
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,20 @@ structure we want to follow and also to add some logic using Conan settings and
Use the layout() method
-----------------------

In the previous examples, every time we executed a `conan install` command we had to use
the `--output-folder argument` to define where we wanted to create the files that Conan
generates. Also, note that we used a different folder when building in Windows or in
Linux/macOS depending if we were using a multi-config CMake generator or not. You can
define this directly in the `conanfile.py` inside the `layout()` method and make it work
for every platform without adding more changes:
In the previous examples, every time we executed a `conan install` command, we had to use
the `--output-folder` argument to define where we wanted to create the files that Conan
generates. There's a neater way to decide where we want Conan to generate the files for
the build system that will allow us to decide, for example, if we want different output
folders depending on the type of CMake generator we are using. You can define this
directly in the `conanfile.py` inside the `layout()` method and make it work for every
platform without adding more changes.


.. code-block:: python
:caption: **conanfile.py**
import os
from conan import ConanFile
Expand All @@ -182,23 +186,16 @@ for every platform without adding more changes:
def layout(self):
# We make the assumption that if the compiler is msvc the
# CMake generator is multi-config
if self.settings.get_safe("compiler") == "msvc":
multi = True
multi = True if self.settings.get_safe("compiler") == "msvc" else False
if multi:
self.folders.generators = os.path.join("build", "generators")
else:
multi = False
self.folders.build = "build" if multi else f"build/{str(self.settings.build_type)}"
self.folders.generators = "build"
self.folders.generators = os.path.join("build", str(self.settings.build_type), "generators")
As you can see, we defined two different attributes for the Conanfile in the `layout()` method:

* **self.folders.build** is the folder where the resulting binaries will be placed. The
location depends on the type of CMake generator. For multi-config, they will be located
in a dedicated folder inside the build folder, while for single-config, they will be
located directly in the build folder.
* **self.folders.generators** is the folder where all the auxiliary files generated by
Conan (CMake toolchain and cmake dependencies files) will be placed.
As you can see, we defined the **self.folders.generators** attribute in the `layout()`
method. This is the folder where all the auxiliary files generated by Conan (CMake
toolchain and cmake dependencies files) will be placed.

Note that the definitions of the folders is different if it is a multi-config generator
(like Visual Studio), or a single-config generator (like Unix Makefiles). In the
Expand All @@ -216,9 +213,9 @@ Check that running the same commands as in the previous examples without the
$ conan install . --build=missing
$ cd build
$ conanbuild.bat
$ generators\conanbuild.bat
# assuming Visual Studio 15 2017 is your VS version and that it matches your default profile
$ cmake .. -G "Visual Studio 15 2017" -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake
$ cmake .. -G "Visual Studio 15 2017" -DCMAKE_TOOLCHAIN_FILE=generators\conan_toolchain.cmake
$ cmake --build . --config Release
...
Building with CMake version: 3.22.6
Expand All @@ -229,17 +226,17 @@ Check that running the same commands as in the previous examples without the
Uncompressed size is: 233
Compressed size is: 147
ZLIB VERSION: 1.2.11
$ deactivate_conanbuild.bat
$ generators\deactivate_conanbuild.bat
.. code-block:: bash
:caption: Linux, macOS
$ conan install . --build=missing
$ cd build
$ source conanbuild.sh
$ source ./Release/generators/conanbuild.sh
Capturing current environment in deactivate_conanbuildenv-release-x86_64.sh
Configuring environment variables
$ cmake .. -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release
$ cmake .. -DCMAKE_TOOLCHAIN_FILE=Release/generators/conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release
$ cmake --build .
...
Building with CMake version: 3.22.6
Expand All @@ -250,7 +247,7 @@ Check that running the same commands as in the previous examples without the
Uncompressed size is: 233
Compressed size is: 147
ZLIB VERSION: 1.2.11
$ source deactivate_conanbuild.sh
$ source ./Release/generators/deactivate_conanbuild.sh
There's no need to always write this logic in the `conanfile.py`. There are some
pre-defined layouts you can import and directly use in your recipe. For example, for the
Expand Down

0 comments on commit 5143a67

Please sign in to comment.