Skip to content

Commit b9aa376

Browse files
danimtbmemsharded
authored andcommitted
Updated virtualrunenv generator and some fixes (conan-io#677)
* updated virtualrunenv generator * rewrite using virtualrunenv and runenvironment helper * fix * mac fix
1 parent 9186cfa commit b9aa376

File tree

3 files changed

+29
-78
lines changed

3 files changed

+29
-78
lines changed

‎howtos/custom_generators.rst‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ that file. The **name of the generator** itself will be taken from the class nam
4242
def content(self):
4343
return "whatever contents the generator produces"
4444
45-
This class is just included in a ``conanfile.py`` that must contain also a ``Conanfile`` class
45+
This class is just included in a ``conanfile.py`` that must contain also a ``ConanFile`` class
4646
that implements the package itself, with the name of the package, the version, etc. This
4747
class typically has no ``source()``, ``build()``, ``package()``, and even the ``package_info()`` method is
48-
overriden as it doesn't have to define any include paths or library paths.
48+
overridden as it doesn't have to define any include paths or library paths.
4949

5050
If you want to create a generator that creates more than one file, you can leave the ``filename()`` empty, and return a dictionary of
5151
filenames->contents in the ``content()`` method:

‎howtos/manage_shared_libraries/env_vars.rst‎

Lines changed: 25 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@ the right environment variable set, so they will be able to locate the (A) share
2424
Similarly if you use the :ref:`virtualenv generator<virtual_environment_generator>` and you
2525
activate it, you will get the paths needed to locate the shared libraries in your terminal.
2626

27-
2827
Example
2928
-------
3029

31-
3230
We are packaging a tool called ``toolA`` with a library and an executable that, for example, compress data.
3331

3432
The package offers two flavors, shared library or static library (embedded in the executable of the tool and
@@ -49,7 +47,6 @@ you will need to have the shared library available.
4947
options = {"shared": [True, False]}
5048
default_options = "shared=False"
5149
52-
5350
def build(self):
5451
# build your shared library
5552
@@ -65,40 +62,28 @@ you will need to have the shared library available.
6562
else:
6663
...
6764
68-
69-
def package_info(self):
70-
self.env_info.PATH.append(os.path.join(self.package_folder, "bin"))
71-
if self.options.shared:
72-
self.env_info.LD_LIBRARY_PATH.append(os.path.join(self.package_folder, "lib"))
73-
self.env_info.DYLD_LIBRARY_PATH.append(os.path.join(self.package_folder, "lib"))
74-
75-
76-
7765
Using the tool from a different package
7866
---------------------------------------
7967

8068
If we are creating now a package that uses the ``ToolA`` executable to compress some data. You can
81-
call directly to the ``toolA.exe``, the required environment variables to locate both the executable
82-
and the shared libraries are automatically available:
69+
execute directly ``toolA`` using RunEnvironment build helper to set the environment variables accordingly:
8370

8471
.. code-block:: python
8572
8673
import os
8774
from conans import tools, ConanFile
8875
8976
class PackageB(ConanFile):
90-
....
9177
name = "packageB"
9278
version = "1.0"
9379
requires = "toolA/1.0@myuser/stable"
9480
95-
9681
def build(self):
97-
...
98-
# we can call directly the ``toolA`` executable. the shared library will be located too
9982
exe_name = "toolA.exe" if self.settings.os == "Windows" else "toolA"
100-
self.run("%s --someparams" % exe_name)
101-
...
83+
env_build = RunEnvironment(self)
84+
with tools.environment_append(env_build.vars):
85+
self.run("%s --someparams" % exe_name)
86+
...
10287
10388
Building an application using the shared library from ``toolA``
10489
---------------------------------------------------------------
@@ -107,9 +92,8 @@ As we are building a final application, probably we will want to distribute it t
10792
shared library from the ``toolA``, so we can use the :ref:`Imports<imports_txt>` to import the required
10893
shared libraries to our user space.
10994

110-
**conanfile.txt**
111-
11295
.. code-block:: python
96+
:caption: *conanfile.txt*
11397
11498
[requires]
11599
toolA/1.0@myuser/stable
@@ -125,8 +109,7 @@ shared libraries to our user space.
125109
lib, *.dylib* -> ./bin # Copies all dylib files from packages lib folder to my "bin" folder
126110
lib, *.so* -> ./bin # Copies all dylib files from packages lib folder to my "bin" folder
127111
128-
129-
**In the terminal window and build the project:**
112+
Now you can build the project:
130113

131114
.. code-block:: bash
132115
@@ -146,67 +129,35 @@ In Linux you still need to set the ``LD_LIBRARY_PATH``, or in OSX, the ``DYLD_LI
146129
147130
$ cd bin && LD_LIBRARY_PATH=$(pwd) && ./mytool
148131
149-
150132
Using shared libraries from dependencies
151-
------------------------------------------
133+
----------------------------------------
152134

153135
If you are executing something that depends on shared libraries belonging to your dependencies, such shared libraries have to be found at
154136
runtime. In Windows, it is enough if the package added its binary folder to the system ``PATH``. In Linux and OSX, it is necessary that the
155137
``LD_LIBRARY_PATH`` and ``DYLD_LIBRARY_PATH`` environment variables are used.
156138

157139
Security restrictions might apply in OSX
158140
(`read this thread <https://stackoverflow.com/questions/35568122/why-isnt-dyld-library-path-being-propagated-here>`_), so the
159-
``DYLD_LIBRARY_PATH`` environment variable is not directly transferred to the child process. In that case, you have to use it explicitely in
160-
your conanfile.py:
141+
``DYLD_LIBRARY_PATH`` environment variable is not directly transferred to the child process. In that case, you have to use it explicitly in
142+
your *conanfile.py*:
161143

162144
.. code-block:: python
163145
164146
def test(self):
165147
# self.run('./myexe") # won't work, even if 'DYLD_LIBRARY_PATH' is in the env
166-
self.run('DYLD_LIBRARY_PATH=%s ./myexe" % os.environ['DYLD_LIBRARY_PATH'])
167-
168-
169-
170-
Using the **virtualenv** generator
171-
----------------------------------
172-
173-
We could also use a :ref:`virtualenv generator<virtual_environment_generator>` to get the
174-
``toolA`` executable available:
175-
176-
**conanfile.txt**
148+
with tools.environment_append({"DYLD_LIBRARY_PATH": [self.deps_cpp_info["toolA"].lib_paths]}):
149+
self.run('DYLD_LIBRARY_PATH=%s ./myexe" % os.environ['DYLD_LIBRARY_PATH'])
177150
178-
.. code-block:: python
179-
180-
[requires]
181-
toolA/1.0@myuser/stable
182-
183-
[options]
184-
toolA:shared=True
151+
Or you could use ``RunEnvironment`` helper described above.
185152
186-
[generators]
187-
virtualenv
153+
Using ``virtualrunenv`` generator
154+
---------------------------------
188155
189-
190-
**In the terminal window:**
191-
192-
.. code-block:: bash
193-
194-
conan install .
195-
source activate
196-
toolA --someparams
197-
198-
199-
Using the **virtualrunenv** generator
200-
-------------------------------------
201-
202-
Even if ``toolA`` doesn't declare the variables in the ``package_info`` method, you can use
203-
the :ref:`virtualrunenv generator<virtual_run_environment_generator>`. It will set automatically
204-
the environment variables poiting to the "lib" and "bin" folders.
205-
206-
207-
**conanfile.txt**
156+
:ref:`virtualrunenv generator<virtual_run_environment_generator>` will set the environment variables ``PATH``, ``LD_LIBRARY_PATH``,
157+
``DYLD_LIBRARY_PATH`` pointing to *lib* and *bin* folders automatically.
208158
209159
.. code-block:: python
160+
:caption: *conanfile.txt*
210161
211162
[requires]
212163
toolA/1.0@myuser/stable
@@ -215,14 +166,14 @@ the environment variables poiting to the "lib" and "bin" folders.
215166
toolA:shared=True
216167
217168
[generators]
218-
virtualenv
169+
virtualrunenv
219170
220-
221-
**In the terminal window:**
171+
In the terminal window:
222172
223173
.. code-block:: bash
224174
225-
conan install .
226-
source activate
227-
toolA --someparams
228-
175+
$ conan install .
176+
$ source activate_run
177+
$ toolA --someparams
178+
# Only For Mac OS users to avoid restrictions:
179+
$ DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH toolA --someparams

‎reference/conanfile/methods.rst‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,8 @@ consumers can use those executables easily:
289289
# assuming the binaries are in the "bin" subfolder
290290
self.env_info.PATH.append(os.path.join(self.package_folder, "bin")
291291
292-
The :ref:`virtualenv<virtual_environment_generator>` generator will use the self.env_info variables to prepare a script to activate/deactive
293-
a virtual environment.
292+
The :ref:`virtualenv<virtual_environment_generator>` generator will use the ``self.env_info`` variables to prepare a script to
293+
activate/deactive a virtual environment. However, this could be directly done using the :ref:`virtualrunenv_generator` generator.
294294
295295
They will be automatically applied before calling the consumer *conanfile.py* methods ``source()``, ``build()``, ``package()`` and
296296
``imports()``.

0 commit comments

Comments
 (0)