Skip to content

Commit bc7795a

Browse files
workspace packages (#4325)
* workspace packages * Update reference/workspace_files.rst Co-authored-by: Francisco Ramírez <franchuti688@gmail.com> * Update reference/workspace_files.rst Co-authored-by: Francisco Ramírez <franchuti688@gmail.com> * review --------- Co-authored-by: Francisco Ramírez <franchuti688@gmail.com>
1 parent 387ea1e commit bc7795a

File tree

1 file changed

+125
-5
lines changed

1 file changed

+125
-5
lines changed

reference/workspace_files.rst

Lines changed: 125 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,133 @@ methods, using the ``Workspace.load_conanfile()`` helper:
7979
return result
8080
8181
82+
conanws.py super-build ``ConanFile``
83+
++++++++++++++++++++++++++++++++++++
84+
85+
The ``conanws.py`` file can contain the definition of a ``ConanFile`` that represents the super-build. When the workspace dependency graph
86+
is computed, all packages in the workspace are collapsed into a single node in the dependency graph, and that node will have dependencies
87+
to the other packages external to the workspace, that is, installed in the Conan cache.
88+
89+
The ``ConanFile`` that represents the workspace super-build project is defined as:
90+
91+
.. code-block:: python
92+
:caption: conanws.py
93+
94+
from conan import ConanFile, Workspace
95+
from conan.tools.cmake import cmake_layout
96+
97+
class MyWs(ConanFile):
98+
settings = "os", "compiler", "arch", "build_type"
99+
generators = "CMakeToolchain", "CMakeDeps"
100+
101+
def layout(self):
102+
cmake_layout(self)
103+
104+
105+
class Ws(Workspace):
106+
def root_conanfile(self):
107+
return MyWs
108+
109+
110+
It defines that our super-build project will be a CMake project that uses the ``CMakeToolchain`` and ``CMakeDeps`` generators to integrate
111+
and find the external package dependencies.
112+
It is not necessary that the ``ConanFile`` defines ``requires`` at all, they will be computed by aggregating the requires of all packages
113+
in the workspace.
114+
115+
.. important::
116+
117+
The ``ConanFile`` inside ``conanws.py`` is a special conanfile, used exclusively for the workspace super-build definition of layout and
118+
generators. It shouldn't have any kind of requirements, not regular ``requires``, ``tool_requires`` or ``test_requires``. It obtains
119+
its dependencies collecting and aggregating the workspace packages requirements.
120+
It shouldn't have ``build()`` or ``package()`` methods either.
121+
122+
123+
conanws.py super-build workspace packages
124+
+++++++++++++++++++++++++++++++++++++++++
125+
126+
With the ``workspace_packages`` attribute, the ``conanws.py`` super-build ``ConanFile`` can have access to the workspace packages, to
127+
be able to reuse their functionality.
128+
For example, if we wanted to collect the behavior of workspace packages toolchain definitions we could do the following.
129+
Let's imagine that we have a workspace with 2 packages, ``pkga/1.2.3`` and ``pkgb/2.3.4``
130+
131+
.. code-block:: python
132+
:caption: pkga/conanfile.py
133+
134+
from conan import ConanFile
135+
136+
class PkgA(ConanFile):
137+
name = "pkga"
138+
version = "1.2.3"
139+
140+
def configure_toolchain(self, tc):
141+
tc.preprocessor_definitions["PKGA_SOME_DEFINITION"] = self.version
142+
143+
def generate(self):
144+
tc = CMakeToolchain(self)
145+
self.configure_toolchain(tc)
146+
tc.generate()
147+
148+
149+
.. code-block:: python
150+
:caption: pkgb/conanfile.py
151+
152+
from conan import ConanFile
153+
class PkgB(ConanFile):
154+
name = "pkgb"
155+
version = "2.3.4"
156+
157+
def configure_toolchain(self, tc):
158+
tc.preprocessor_definitions["SOME_PKGB_DEFINE"] = self.version
159+
160+
def generate(self):
161+
tc = CMakeToolchain(self)
162+
self.configure_toolchain(tc)
163+
tc.generate()
164+
165+
166+
.. code-block:: python
167+
:caption: conanws.py
168+
169+
from conan import ConanFile
170+
from conan import Workspace
171+
from conan.tools.cmake import CMakeToolchain
172+
173+
class MyWs(ConanFile):
174+
settings = "arch", "build_type"
175+
def generate(self):
176+
tc = CMakeToolchain(self)
177+
for ref, dep in self.workspace_packages.items():
178+
dep.configure_toolchain(tc)
179+
tc.generate()
180+
181+
class Ws(Workspace):
182+
def root_conanfile(self):
183+
return MyWs
184+
185+
186+
Then, the ``workspace_packages.items()`` iteration will be able to call every package in the workspace ``configure_toolchain()`` and
187+
collect all their behavior in the current super-build ``CMakeToolchain``. The resulting toolchain will contain the definitions for
188+
``SOME_PKGB_DEFINE=2.3.4`` and ``PKGA_SOME_DEFINITION=1.2.3``.
189+
190+
.. warning::
191+
192+
**Important**
193+
194+
The access of ``workspace_packages`` to the workspace packages ``ConanFiles`` must be **read-only** and **pure**. It cannot modify
195+
the workspace ``pkga`` and ``pkgb`` packages data, and it cannot have any side effect. For example it is forbidden to call any method such as ``.build()``
196+
or even the ``.generate()`` method.
197+
If there is logic to be reused, it is the responsibility of the developer to define some convention, such as the ``configure_toolchain()``
198+
method that if called from the ``conanws.py`` will not modify at all (pure) the ``pkga`` or ``pkgb`` data.
199+
82200

83201
conanws.py super-build options
84202
++++++++++++++++++++++++++++++
85203

204+
A particular case of the above ``workspace_packages`` access could be reading the individual workspace packages options.
86205
A ``conanws.py`` used for a super-build workspaces file can manage options in two different ways:
87206

88207
- It can define its own ``options`` with the normal ``conanfile.py`` syntax, so the generated ``conan_toolchain.cmake`` for the super-project uses those inputs.
89-
- It can collect the options of the workspace's packages with the ``workspace_packages_options`` and process them in any user-custom way.
208+
- It can collect the options of the workspace's packages with the ``workspace_packages`` and process them in any user-custom way.
90209

91210

92211
**super-project options**
@@ -117,9 +236,10 @@ Then, options can be provided with the usual syntax, via profiles or command lin
117236
118237
Note there can be overlap with the ``options`` defined in the workspace packages, as for super-projects those options are simply ignored, and only the options of the super-project are taken into account to generate the ``conan_toolchain.cmake``. For example, the ``conanws.py`` can define a ``shared`` option if it is desired that the ``conan_toolchain.cmake`` will correctly define ``BUILD_SHARED_LIBS`` or not when defining something like ``-o "*:shared=True"``, as the workspace packages having the ``shared`` option information is discarded when the workspace packages are collapsed in the dependency graph to model the super-project.
119238

239+
120240
**packages options**
121241

122-
The second alternative is to collect the ``options`` of the workspace packages that have been collapsed. Recall that in the final dependency graph, the workspace packages are no longer represented, as they are no longer individual packages but part as the current super-build. The way to access their options information is via the ``workspace_packages_options``, and that information can be used in the ``generate()`` method to do any desired action at the super-build project level.
242+
The second alternative is to collect the ``options`` of the workspace packages that have been collapsed. Recall that in the final dependency graph, the workspace packages are no longer represented, as they are no longer individual packages but part as the current super-build. The way to access their options information is via the ``workspace_packages``, and that information can be used in the ``generate()`` method to do any desired action at the super-build project level.
123243

124244

125245
So let's say that a workspace containing a ``dep/0.1`` package that contains the standard ``shared`` options defines the following super-build ``ConanFile``:
@@ -130,9 +250,9 @@ So let's say that a workspace containing a ``dep/0.1`` package that contains the
130250
131251
class MyWs(ConanFile):
132252
def generate(self):
133-
for pkg, options in self.workspace_packages_options.items():
134-
for k, v in options.items():
135-
self.output.info(f"Generating with opt {pkg}:{k}={v}!!!!")
253+
for pkg, dep in self.workspace_packages.items():
254+
for k, v in dep.options.items():
255+
self.output.info(f"Generating with opt {pkg}:{k}={v}!!!!")
136256
137257
class Ws(Workspace):
138258
def root_conanfile(self):

0 commit comments

Comments
 (0)