Skip to content

Commit 82eab16

Browse files
memshardedAbrilRBS
andauthored
example for library wrapper (#4357)
* example for library wrapper * Update examples/tools/system/system_package/package_manager.rst * Update examples/tools/system/system_package/package_manager.rst * Update examples/tools/system/system_package/package_manager.rst * Update examples/tools/system/system_package/package_manager.rst * Update examples/tools/system/system_package/package_manager.rst --------- Co-authored-by: Abril Rincón Blanco <5364255+AbrilRBS@users.noreply.github.com>
1 parent f9554a9 commit 82eab16

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

examples/tools/system/system_package/package_manager.rst

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,90 @@ Don't worry if the displayed version is different from the one shown here or the
234234
It depends on the version installed in your system and where you built the application.
235235

236236
That's it! You have successfully packaged a system library and consumed it in a Conan package.
237+
238+
239+
.. _examples_tools_system_package_manager_wrapper:
240+
241+
Wrapping a library installed in the system as a Conan package
242+
-------------------------------------------------------------
243+
244+
As a variant of the above case, it is also possible to apply the above strategy to libraries that
245+
are installed in the system, but not necessarily installed by the system package manager, nor
246+
necessarily in the common system locations where the compilers will find them by default.
247+
248+
Suppose that there is an existing library, already compiled in a user folder such as:
249+
250+
.. code-block:: text
251+
252+
/home/myuser/mymath
253+
└── include
254+
├── mymath.h
255+
└── lib
256+
├── mymath.lib
257+
258+
And ``/home/myuser/mymath`` is not added to the compilers default paths or anything like that.
259+
260+
In general, a more recommended approach is to create a full package from those precompiled binaries,
261+
and upload that package, and then manage it as any other regular package. See the tutorial about
262+
:ref:`creating packages from pre-compiled binaries here<creating_packages_other_prebuilt>`.
263+
264+
But in some scenarios, it might still be desirable to use that library from its installed location
265+
``/home/myuser/mymath`` without putting the artifacts inside a Conan package. This can be done
266+
with a "wrapper" recipe, similar to the one above, but which does not have any ``system_requirements()``
267+
method.
268+
269+
It could be something like:
270+
271+
272+
.. code-block:: python
273+
274+
from conan import ConanFile
275+
276+
class MyMath(ConanFile):
277+
name = "mymath"
278+
version = "1.2" # In this case an actual version might make more sense
279+
package_type = "static-library"
280+
281+
def package_info(self):
282+
self.cpp_info.bindirs = []
283+
# Absolute paths are allowed here
284+
self.cpp_info.includedirs = ["/home/myuser/mymath/include"]
285+
self.cpp_info.libdirs = ["/home/myuser/mymath/lib"]
286+
self.cpp_info.libs = ["mymath"]
287+
288+
289+
Note that it is also possible to still do conditions based on settings, in case that the library
290+
is installed in the system in different locations based on the platform:
291+
292+
.. code-block:: python
293+
294+
settings = "os"
295+
296+
def package_info(self):
297+
self.cpp_info.bindirs = []
298+
# Absolute paths are allowed here
299+
if self.settings.os == "Windows":
300+
self.cpp_info.includedirs = ["C:/Users/myuser/mymath/include"]
301+
self.cpp_info.libdirs = ["C:/Users/myuser/mymath/lib"]
302+
else:
303+
self.cpp_info.includedirs = ["/home/myuser/mymath/include"]
304+
self.cpp_info.libdirs = ["/home/myuser/mymath/lib"]
305+
self.cpp_info.libs = ["mymath"]
306+
307+
308+
It might even be possible to parametrize those absolute paths with some environment variable
309+
specific for that platform too.
310+
311+
.. note::
312+
313+
**Best practices**
314+
315+
- The use of "wrapper" recipes like this one should be minimized, as it makes reproducibility and
316+
traceability harder. Creating a real package putting the headers and libraries inside it, uploading
317+
it to the server, makes it possible to achieve such traceability and reproducibility.
318+
- This type of "wrapper" recipe can be convenient together with the ``[replace_requires]`` feature,
319+
for specific platform constraints, like a platform that mandates that some ``openssl`` library must
320+
be the one contained in a sysroot, not the one from the Conan package ``openssl/version``, but in
321+
general, such a dependency to ``openssl/version`` is required by other packages. In those cases, writing
322+
a wrapper recipe around the sysroot ``openssl`` and using ``[replace_requires]`` to force the dependency
323+
graph to resolve to it could make sense.

reference/conanfile/methods/package_info.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ Directories:
7070
- **builddirs**: List of relative paths (starting from package root) of directories that can contain build scripts that could be used by the consumers. Empty by default.
7171
- **frameworkdirs**: List of relative paths (starting from the package root), of directories containing OSX frameworks.
7272

73+
There is one exception that would allow using absolute paths in ``self.cpp_info.xxxxdirs`` fields: for recipes that serve as a "wrapper" of a library installed in the system, but not by Conan. See the :ref:`example of a recipe wrapping a library installed in the system<examples_tools_system_package_manager_wrapper>`.
74+
7375
Flags:
7476

7577
- **defines**: Ordered list of preprocessor directives. It is common that the consumers have to specify some sort of defines in some cases,

tutorial/creating_packages/other_types_of_packages/package_prebuilt_binaries.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ Now you can try to generate a binary package for ``build_type=Debug`` running th
140140
You can repeat this process any number of times for different configurations.
141141

142142

143-
Packaging already pre-built Binaries
143+
Packaging already pre-built binaries
144144
------------------------------------
145145

146146
Please, first clone the sources to recreate this project. You can find them in the

0 commit comments

Comments
 (0)