-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ConanCI bot
committed
Mar 6, 2024
1 parent
55518cf
commit fe3e872
Showing
579 changed files
with
4,205 additions
and
589 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
2.1/_sources/examples/tools/meson/build_simple_meson_project.rst.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
.. _examples_tools_meson_toolchain_build_simple_meson_project: | ||
|
||
Build a simple Meson project using Conan | ||
======================================== | ||
|
||
In this example, we are going to create a string compressor application | ||
that uses one of the most popular C++ libraries: `Zlib <https://zlib.net/>`__. | ||
|
||
.. note:: | ||
|
||
This example is based on the main :ref:`Build a simple CMake project using Conan<consuming_packages_build_simple_cmake_project>` | ||
tutorial. So we highly recommend reading it before trying out this one. | ||
|
||
We'll use Meson as build system and pkg-config as helper tool in this case, so you should get them installed | ||
before going forward with this example. | ||
|
||
Please, first clone the sources to recreate this project. You can find them in the | ||
`examples2 repository <https://github.com/conan-io/examples2>`_ in GitHub: | ||
|
||
.. code-block:: bash | ||
$ git clone https://github.com/conan-io/examples2.git | ||
$ cd examples2/examples/tools/meson/mesontoolchain/simple_meson_project | ||
We start from a very simple C language project with this structure: | ||
|
||
.. code-block:: text | ||
. | ||
├── meson.build | ||
└── src | ||
└── main.c | ||
This project contains a basic *meson.build* including the **zlib** dependency and the | ||
source code for the string compressor program in *main.c*. | ||
|
||
Let's have a look at the *main.c* file: | ||
|
||
.. code-block:: cpp | ||
:caption: **main.c** | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <zlib.h> | ||
int main(void) { | ||
char buffer_in [256] = {"Conan is a MIT-licensed, Open Source package manager for C and C++ development " | ||
"for C and C++ development, allowing development teams to easily and efficiently " | ||
"manage their packages and dependencies across platforms and build systems."}; | ||
char buffer_out [256] = {0}; | ||
z_stream defstream; | ||
defstream.zalloc = Z_NULL; | ||
defstream.zfree = Z_NULL; | ||
defstream.opaque = Z_NULL; | ||
defstream.avail_in = (uInt) strlen(buffer_in); | ||
defstream.next_in = (Bytef *) buffer_in; | ||
defstream.avail_out = (uInt) sizeof(buffer_out); | ||
defstream.next_out = (Bytef *) buffer_out; | ||
deflateInit(&defstream, Z_BEST_COMPRESSION); | ||
deflate(&defstream, Z_FINISH); | ||
deflateEnd(&defstream); | ||
printf("Uncompressed size is: %lu\n", strlen(buffer_in)); | ||
printf("Compressed size is: %lu\n", strlen(buffer_out)); | ||
printf("ZLIB VERSION: %s\n", zlibVersion()); | ||
return EXIT_SUCCESS; | ||
} | ||
Also, the contents of *meson.build* are: | ||
|
||
.. code-block:: text | ||
:caption: **meson.build** | ||
project('tutorial', 'c') | ||
zlib = dependency('zlib', version : '1.2.11') | ||
executable('compressor', 'src/main.c', dependencies: zlib) | ||
Let's create a *conanfile.txt* with the following content to install **Zlib**: | ||
|
||
.. code-block:: ini | ||
:caption: **conanfile.txt** | ||
[requires] | ||
zlib/1.2.11 | ||
[generators] | ||
PkgConfigDeps | ||
MesonToolchain | ||
In this case, we will use :ref:`PkgConfigDeps<PkgConfigDeps>` to generate information about where the **Zlib** library | ||
files are installed thanks to the `*.pc` files and :ref:`MesonToolchain<MesonToolchain>` to pass build information | ||
to *Meson* using a `conan_meson_[native|cross].ini` file that describes the native/cross compilation environment, which in | ||
this case is a `conan_meson_native.ini` one. | ||
|
||
We will use Conan to install **Zlib** and generate the files that Meson needs to find this library and build our project. | ||
We will generate those files in the folder *build*. To do that, run: | ||
|
||
.. code-block:: bash | ||
$ conan install . --output-folder=build --build=missing | ||
Now we are ready to build and run our **compressor** app: | ||
|
||
.. code-block:: bash | ||
:caption: Windows | ||
$ cd build | ||
$ meson setup --native-file conan_meson_native.ini .. meson-src | ||
$ meson compile -C meson-src | ||
$ meson-src\compressor.exe | ||
Uncompressed size is: 233 | ||
Compressed size is: 147 | ||
ZLIB VERSION: 1.2.11 | ||
.. code-block:: bash | ||
:caption: Linux, macOS | ||
$ cd build | ||
$ meson setup --native-file conan_meson_native.ini .. meson-src | ||
$ meson compile -C meson-src | ||
$ ./meson-src/compressor | ||
Uncompressed size is: 233 | ||
Compressed size is: 147 | ||
ZLIB VERSION: 1.2.11 |
162 changes: 162 additions & 0 deletions
162
2.1/_sources/examples/tools/meson/create_your_first_package.rst.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
.. _examples_tools_meson_create_first_package: | ||
|
||
Create your first Conan package with Meson | ||
========================================== | ||
|
||
In the :ref:`Create your first Conan package tutorial<creating_packages_create_your_first_conan_package>` | ||
CMake was used as the build system. If you haven't read that section, read it first to familiarize | ||
yourself with the ``conanfile.py`` and ``test_package`` concepts, then come back to read | ||
about the specifics of the ``Meson`` package creation. | ||
|
||
Use the :command:`conan new` command to create a "Hello World" C++ library example project: | ||
|
||
.. code-block:: bash | ||
$ conan new meson_lib -d name=hello -d version=1.0 | ||
This will create a Conan package project with the following structure. | ||
|
||
.. code-block:: text | ||
├── conanfile.py | ||
├── meson.build | ||
├── hello.vcxproj | ||
├── src | ||
│ ├── hello.h | ||
│ └── hello.cpp | ||
└── test_package | ||
├── conanfile.py | ||
├── meson.build | ||
└── src | ||
└── example.cpp | ||
The structure and files are very similar to the previous CMake example: | ||
|
||
- **conanfile.py**: On the root folder, there is a *conanfile.py* which is the main recipe | ||
file, responsible for defining how the package is built and consumed. | ||
- **meson.build**: A Meson build script. This script doesn't need to contain anything Conan-specific, | ||
it is completely agnostic of Conan, because the integration is transparent. | ||
- **src** folder: the folder that contains the simple C++ "hello" library. | ||
- **test_package** folder: contains an *example* application that will require | ||
and link with the created package. In this case the ``test_package`` also contains a | ||
``meson.build``, but it is possible to have the ``test_package`` using | ||
other build system as CMake if desired. It is not mandatory that the test_package is using | ||
the same build system as the package. | ||
|
||
Let's have a look at the package recipe *conanfile.py* (only the relevant new parts): | ||
|
||
.. code-block:: python | ||
exports_sources = "meson.build", "src/*" | ||
def layout(self): | ||
basic_layout(self) | ||
def generate(self): | ||
tc = MesonToolchain(self) | ||
tc.generate() | ||
def build(self): | ||
meson = Meson(self) | ||
meson.configure() | ||
meson.build() | ||
def package(self): | ||
meson = Meson(self) | ||
meson.install() | ||
Let's explain the different sections of the recipe briefly: | ||
|
||
- The ``layout()`` defines a ``basic_layout()``, this is less flexible than a CMake one, so it | ||
doesn't allow any parametrization. | ||
- The ``generate()`` method calls ``MesonToolchain`` that can generate ``conan_meson_native.ini`` | ||
and ``conan_meson_cross.ini`` Meson toolchain files for cross builds. If the project had dependencies | ||
with Conan ``requires``, it should add ``PkgConfigDeps`` too | ||
- The ``build()`` method uses the ``Meson()`` helper to drive the build | ||
- The ``package()`` method uses the ``Meson`` install functionality to define and copy to the package | ||
folder the final artifacts. | ||
|
||
|
||
The **test_package** folder also contains a ``meson.build`` file that declares a dependency to | ||
the tested package, and links an application, to verify the package was correctly created and contains | ||
that library: | ||
|
||
.. code-block:: | ||
:caption: test_package/meson.build | ||
project('Testhello', 'cpp') | ||
hello = dependency('hello', version : '>=0.1') | ||
executable('example', 'src/example.cpp', dependencies: hello) | ||
Note the ``test_package/conanfile.py`` contains also a ``generators = "PkgConfigDeps", "MesonToolchain"``, | ||
because the ``test_package`` has the "hello" package as dependency, and ``PkgConfigDeps`` is necessary to | ||
locate it. | ||
|
||
.. note:: | ||
|
||
This example assumes Meson, Ninja and PkgConfig are installed in the system, which might not always be the case. | ||
If they are not, you can create a profile ``myprofile`` with: | ||
|
||
.. code-block:: | ||
include(default) | ||
[tool_requires] | ||
meson/[*] | ||
pkgconf/[*] | ||
We added `Meson` and `pkg-config` as :ref:`tool requirements to the profile <reference_config_files_profiles_tool_requires>`. By executing ``conan create . -pr=myprofile``, those tools will be installed and made available during the package's build process. | ||
|
||
|
||
Let's build the package from sources with the current default configuration, and then let | ||
the ``test_package`` folder test the package: | ||
|
||
.. code-block:: bash | ||
$ conan create . | ||
... | ||
======== Testing the package: Executing test ======== | ||
hello/1.0 (test package): Running test() | ||
hello/1.0 (test package): RUN: .\example | ||
hello/1.0: Hello World Release! | ||
hello/1.0: _M_X64 defined | ||
hello/1.0: MSVC runtime: MultiThreadedDLL | ||
hello/1.0: _MSC_VER1939 | ||
hello/1.0: _MSVC_LANG201402 | ||
hello/1.0: __cplusplus201402 | ||
hello/1.0 test_package | ||
We can now validate that the recipe and the package binary are in the cache: | ||
|
||
|
||
.. code-block:: bash | ||
$ conan list hello/1.0:* | ||
Local Cache: | ||
hello | ||
hello/1.0 | ||
revisions | ||
856c535669f78da11502a119b7d8a6c9 (2024-03-04 17:52:39 UTC) | ||
packages | ||
c13a22a41ecd72caf9e556f68b406569547e0861 | ||
info | ||
settings | ||
arch: x86_64 | ||
build_type: Release | ||
compiler: msvc | ||
compiler.cppstd: 14 | ||
compiler.runtime: dynamic | ||
compiler.runtime_type: Release | ||
compiler.version: 193 | ||
os: Windows | ||
.. seealso:: | ||
|
||
- :ref:`Meson built-in integrations reference<conan_tools_meson>`. | ||
- :ref:`PkgConfigDeps built-in integrations reference<conan_tools_gnu_pkgconfigdeps>`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.