Friday, 9 August 2019

The Meson Build System: part #2. Dependencies

This is a second post about Meson. If You have not read the first one, check it out!

Meson dependencies


In the first post about Meson, I have written how to create simple Meson project. But hardly any project can be done without external dependencies. In Meson, You use subprojects to specify external dependencies. If a developer's machine has dependency installed system-wide, Meson will use system version. In the other case, Meson will download dependency sources and build it.

All external dependencies should be declared by placing .wrap file in subprojects subfolder of the root directory of the project. To use dependencies managed by Meson team, You can use meson wrap subcommand. For example, to use gtest library, You will use the following commands:
mkdir subprojects in main dir
meson wrap install gtest

To create an executable using gtest dependency, use following Meson commands:
gtest_dep = dependency('gtest', main : true, fallback : ['gtest', 'gtest_dep'])

tests_executable = executable(
    'name',
    [sources],
    dependencies: [gtest_dep]
)
And that is all!

Meson dependencies vs CMake's

Following CMake code creates executable using gtests.
find_package(GTest REQUIRED)
add_executable(
    tests_executable
    [sources]
)
target_link_libraries(
    tests_executable
    PUBLIC
        GTest::GTest
        GTest::Main
)
The code is similar to one in Meson. But if Your system has not gtest package, using gtests is not straightforward. You can use package managers like vcpkg, but there is no single cross-platform solution.

No comments:

Post a Comment