Skip to content

Commit b0d08fe

Browse files
AbrilRBSmemshardedSSE4franramirez688
authored
Add docu for deployers and a simple example (#2946)
* Add deployer documentation and simple example For #2686 * Fix note formatting and typo * Use root conanfile for copy * Update reference/extensions/deployers.rst Co-authored-by: James <[email protected]> * Address review comments * Extra changes * Update examples/extensions/deployers/sources/custom_deployer_sources.rst Co-authored-by: SSE4 <[email protected]> * Update reference/extensions/deployers.rst Co-authored-by: SSE4 <[email protected]> * Update examples/extensions/deployers/sources/custom_deployer_sources.rst Co-authored-by: SSE4 <[email protected]> * Update examples/extensions/deployers/sources/custom_deployer_sources.rst Co-authored-by: SSE4 <[email protected]> * Update examples/extensions/deployers/sources/custom_deployer_sources.rst Co-authored-by: SSE4 <[email protected]> * Update reference/extensions/deployers.rst Co-authored-by: Francisco Ramírez <[email protected]> * Update reference/extensions/deployers.rst Co-authored-by: Francisco Ramírez <[email protected]> * Update examples/extensions/deployers/sources/custom_deployer_sources.rst Co-authored-by: Francisco Ramírez <[email protected]> * Update examples/extensions/deployers/sources/custom_deployer_sources.rst Co-authored-by: Francisco Ramírez <[email protected]> * Update reference/extensions/deployers.rst Co-authored-by: Francisco Ramírez <[email protected]> * Update examples/extensions/deployers/sources/custom_deployer_sources.rst Co-authored-by: Francisco Ramírez <[email protected]> * Update reference/extensions/deployers.rst Co-authored-by: Francisco Ramírez <[email protected]> * Remove fn parenthesis to full deployers * Update reference/extensions/deployers.rst --------- Co-authored-by: James <[email protected]> Co-authored-by: SSE4 <[email protected]> Co-authored-by: Francisco Ramírez <[email protected]>
1 parent ef2641c commit b0d08fe

File tree

4 files changed

+167
-1
lines changed

4 files changed

+167
-1
lines changed

examples.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Examples
99

1010
examples/conanfile/conanfile
1111
examples/extensions/commands/custom_commands
12+
examples/extensions/deployers/custom_deployers
1213
examples/tools/cmake/cmake
1314
examples/tools/files/files
1415
examples/tools/meson/meson
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.. _examples_extensions_deployers:
2+
3+
4+
5+
6+
Custom deployers
7+
================
8+
9+
10+
.. toctree::
11+
:maxdepth: 2
12+
13+
14+
sources/custom_deployer_sources
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
.. examples_extensions_deployers_sources:
2+
3+
Copy sources from all your dependencies
4+
=======================================
5+
6+
7+
8+
Please, first of all, clone the sources to recreate this project. You can find them in the
9+
`examples2.0 repository <https://github.com/conan-io/examples2>`_ in GitHub:
10+
11+
.. code-block:: bash
12+
13+
$ git clone https://github.com/conan-io/examples2.git
14+
$ cd examples2/examples/extensions/deployers/sources
15+
16+
17+
In this example we are going to see how to create and use a custom deployer.
18+
This deployer copies all the source files from your dependencies and puts them into a specific output folder
19+
20+
.. note::
21+
22+
To better understand this example, it is highly recommended to have previously read the :ref:`Deployers <reference_extensions_deployer_direct_deploy>` reference.
23+
24+
25+
Locate the deployer
26+
-------------------
27+
28+
In this case, the deployer is located in the same directory as our example conanfile,
29+
but as shown in :ref:`Deployers <reference_extensions_deployer_direct_deploy>` reference,
30+
Conan will look for the specified deployer in a few extra places in order, namely:
31+
32+
#. Absolute paths
33+
#. Relative to cwd
34+
#. In the ``[CONAN_HOME]/extensions/deploy`` folder
35+
#. Built-in deployers
36+
37+
38+
Run it
39+
------
40+
41+
For our example, we have a simple recipe that lists both ``zlib`` and ``mcap`` as requirements.
42+
With the help of the ``tools.build:download_source=True`` conf, we can force the invocation of its ``source()`` method,
43+
which will ensure that sources are available even if no build needs to be carried out.
44+
45+
Now, you should be able to use the new deployer in both ``conan install`` and ``conan graph`` commands for any given recipe:
46+
47+
.. code-block:: bash
48+
49+
$ conan graph info . -c tools.build:download_source=True --deploy=sources_deploy
50+
51+
52+
Inspecting the command output we can see that it copied the sources of our direct dependencies ``zlib`` and ``mcap``,
53+
**plus** the sources of our transitive dependencies, ``zstd``and ``lz4`` to a ``dependencies_sources`` folder.
54+
After this is done, extra preprocessing could be done to accomplish more specific needs.
55+
56+
Code tour
57+
---------
58+
59+
The **source_deploy.py** file has the following code:
60+
61+
62+
63+
.. code-block:: python
64+
:caption: **sources_deploy.py**
65+
66+
from conan.tools.files import copy
67+
import os
68+
69+
70+
def deploy(graph, output_folder):
71+
for name, dep in graph.root.conanfile.dependencies.items():
72+
copy(graph.root.conanfile, "*", dep.folders.source_folder, os.path.join(output_folder, "dependency_sources", str(dep)))
73+
74+
75+
deploy()
76+
++++++++
77+
78+
The ``deploy()`` method is called by Conan, and gets both a dependency graph and an output folder path as arguments.
79+
It iterates all the dependencies of our recipe and copies every source file to their respective folders
80+
under ``dependencies_sources`` using :ref:`conan.tools.copy<conan_tools_files_copy>`.

reference/extensions/deployers.rst

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,76 @@
11
.. _reference_extensions_deployers:
22

33
Deployers
4-
---------
4+
=========
55

6+
Deployers are a mechanism to facilitate copying files form one folder, usually the Conan cache, to user folders.
7+
While Conan provides two built-in ones (``full_deploy`` and ``direct_deploy``), users can easily manage their own
8+
with ``conan config install``.
9+
10+
Deployers run before generators, and they can change the target folders.
11+
For example, if the ``--deploy=full_deploy`` deployer runs before ``CMakeDeps``,
12+
the files generated by ``CMakeDeps`` will point to the local copy in the user folder done by the ``full_deploy`` deployer,
13+
and not to the Conan cache. Multiple deployers can be specified by supplying more than one ``--deploy=`` argument,
14+
and they will be ran in order of appearance.
15+
16+
Deployers can be multi-configuration. Running ``conan install . --deploy=full_deploy`` repeatedly for different profiles
17+
can achieve a fully self-contained project, including all the artifacts, binaries, and build files.
18+
This project will be completely independent of Conan and no longer require it at all to build.
19+
20+
21+
Built-in deployers
22+
------------------
23+
24+
.. _reference_extensions_deployer_full_deploy:
25+
26+
full_deploy
27+
^^^^^^^^^^^
28+
29+
Deploys each package folder of every dependency to your recipe's ``output_folder`` in a subfolder tree based on:
30+
31+
#. The build context
32+
#. The dependency name and version
33+
#. The build type
34+
#. The build arch
35+
36+
Then every dependency will end up in a folder such as:
37+
38+
``[OUTPUT_FOLDER]/host/dep/0.1/Release/x86_64``
39+
40+
41+
.. _reference_extensions_deployer_direct_deploy:
42+
43+
direct_deploy
44+
^^^^^^^^^^^^^
45+
46+
Same as ``full_deploy``, but only processes your recipe's *direct* dependencies.
47+
48+
49+
Custom deployers
50+
----------------
51+
52+
Custom deployers can be managed via ``conan config install``. When looking for a specific deployer,
53+
Conan will look in these locations for the deployer in the following order:
54+
55+
#. Absolute paths
56+
#. Relative to cwd
57+
#. In the ``[CONAN_HOME]/extensions/deploy`` folder
58+
#. As built-in deployers
59+
60+
Conan will look for a ``deploy()`` method to call for each installed file.
61+
The function signature of your custom deployers should be as follows:
62+
63+
64+
.. code-block:: python
65+
:caption: **my_custom_deployer.py**
66+
67+
def deploy(graph, output_folder: str):
68+
69+
(Note that the arguments are passed as named parameters, so both the ``graph`` and ``output_folder`` names are mandatory)
70+
71+
You can access your conanfile object with ``graph.root.conanfile``.
72+
See :ref:`ConanFile.dependencies<conan_conanfile_model_dependencies>` for information on how to iterate over its dependencies.
73+
Your custom deployer can now be invoked as if it were a built-in deployer using the filename in which it's found,
74+
in this case ``conan install . --deploy=my_custom_deployer``. Note that supplying the **.py** extension is optional.
75+
76+
See the :ref:`custom deployers<examples_extensions_deployers>` section for examples on how to implement your own deployers.

0 commit comments

Comments
 (0)