How do I tell meson setup about the location of a dependency? - cmake

I'm trying to build celluloid, which uses meson. I ran meson, but it failed to find an appropriate version of mpv:
Determining dependency 'mpv' with pkg-config executable '/usr/bin/pkg-config'
Called `/usr/bin/pkg-config --modversion mpv` -> 1
Found CMake: /usr/bin/cmake (3.13.4)
Determining dependency 'mpv' with CMake executable '/usr/bin/cmake'
Try CMake generator: auto
Called `/usr/bin/cmake --trace-expand -DNAME=mpv .` in /tmp/celluloid-0.20/build/meson-private/cmake_mpv -> 0
Dependency mpv found: NO (tried pkgconfig and cmake)
src/meson.build:125:0: ERROR: Dependency "mpv" not found, tried pkgconfig and cmake
so I downloaded and built the latest mpv release (0.33.0), built and installed it at /opt/mpv.
Now - how do I tell meson to take mpv from this new path?
Note: The relevant snippet of the meson files seems to be:
executable('celluloid', sources,
dependencies: [
libgtk,
libgio,
meson.get_compiler('c').find_library('m', required: false),
dependency('mpv', version: '>= 1.107'),
dependency('epoxy')
],
link_with: extra_libs,
include_directories: includes,
c_args: cflags,
install: true
)

You tell meson about your dependency by letting pkgconfig know about your dependency...
and that can be done by adding your dependency's path to the PKG_CONFIG_PATH environment variable; it is delimited by colons, just like PATH, e.g. /opt/foo:/opt/extra/baz.
Remember that you may also need to add associated paths to LD_LIBRARY_PATH after building and installing with a custom-built directory.

Related

pkg_config_modules dependency fails because version in "Uncontrolled"

The Problem
I've got a CMakeLists.txt file with this content:
pkg_check_modules(FOO REQUIRED foo>=0.1.0.1)
When I run Cmake v3.17.2 with cmake3 -G Ninja . in that directory:
Checking for module 'foo>=0.1.0.1'
Requested 'foo >= 0.1.0.1' but version of foo is Uncontrolled
Details
This is running inside RHEL7
yum info foo | grep Version returns Version : 0.1.0.1.20200417git602d018
The foo module is created by the team I'm on
The Question
How can I tell CMake what version my foo library is so that it isn't "Uncontrolled"?
In the output of the foo project, inside of the generated lib64 directory, there's a pkgconfig directory which contains foo.pc.
Inside of that file, version info is as follows:
Version: HEAD
Change this to be the intended version. In my case this was automated by the build process of foo, so what was required was to add a git tag for the current version and rebuild.

Installing gtest with conan

I am about to change to conan, in the hope that is will simplify installing my package by my users. It was OK, until I started to add gtest to my package.
During install, I receive messages
gtest/1.8.1#bincrafters/stable: Package installed
conanfile.txt imports(): Copied 4 '.a' files: libgmockd.a, libgtestd.a, libgmock_maind.a, libgtest_maind.a
However, during build I receive:
/usr/bin/ld: cannot find -lgmock_maind
/usr/bin/ld: cannot find -lgmockd
/usr/bin/ld: cannot find -lgtestd
My CMakeLists.txt file contains
target_link_libraries(
${PROJECT_NAME}_GTEST
Modules
${CONAN_LIBS}
)
What is missing? Shall I provide some
link_directories(?)
argument?
(In the meantime, after some trials, I succeeded: Not only
link_directories(${CONAN_LIB_DIRS_GTEST})
is needed, but also conan's .data must be cleared.)
What generator are you using?
I have this in my conanfile.txt requires section
gtest/[~=1.8]#bincrafters/stable
This is what I have for generators in that section
cmake_find_package
cmake_paths
And in the CMakeLists.txt
include(${CMAKE_BINARY_DIR}/conan_paths.cmake)
find_package(gtest REQUIRED)
add_dependencies(something gtest::gtest)
target_link_libraries(something gtest::gtest)
Note that FindGTest is a built in module, but Findgtest.cmake is a file generated by conan in the build directory.
The Bincrafters package for gtest is marked as obsolete, you should use the one in the conan center.
For that, simply add the conan recipe to the conanfile.txt/py.
Let's say you use a plain conanfile.txt:
# conanfile.txt
[requires]
gtest/1.10.0
[generators]
cmake
Then you can run conan install
Then add the conan instructions to your project's CMakeLists.txt:
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
target_link_libraries(${PROJECT_NAME} ${CONAN_LIBS})
You may print a message to see the included libraries:
message("-- Conan libs: ${CONAN_LIBS}").
This should include both gtest and gmock.
Finally, just include the header, and use the framework:
#include "gtest/gtest.h"
TEST(TestName, Foo)
{
EXPECT_TRUE(true);
}

Catkin cannot find gtest

Since yesterday none of my packages containing tests build. Catkin complains it cannot find gtest when using catkin_add_gtests(), since GTEST_FOUND is FALSE. You can see this in the error msg below, with the custom output I added to my CMakeLists. Up to yesterday, GTEST_FOUND was TRUE when catkin_add_gtests() was called.
This is the error I always get. In this case I'm trying to build a mockup package on a clean catkin workspace:
Errors << silly_pkg:cmake /home/paco/catkin_ws2/logs/silly_pkg/build.cmake.002.log
Not searching for unused variables given on the command line.
Re-run cmake no build system arguments
-- Using CATKIN_DEVEL_PREFIX: /home/paco/catkin_ws2/devel/.private/silly_pkg
-- Using CMAKE_PREFIX_PATH: /home/paco/catkin_ws2/devel;/opt/ros/kinetic
-- This workspace overlays: /home/paco/catkin_ws2/devel;/opt/ros/kinetic
-- Using PYTHON_EXECUTABLE: /usr/bin/python
-- Using Debian Python package layout
-- Using empy: /usr/bin/empy
-- Using CATKIN_ENABLE_TESTING: ON
-- Call enable_testing()
-- Using CATKIN_TEST_RESULTS_DIR: /home/paco/catkin_ws2/build/silly_pkg/test_results
-- Using Python nosetests: /usr/bin/nosetests-2.7
-- catkin 0.7.11
-- GTEST_FOUND: FALSE
CMake Warning at /opt/ros/kinetic/share/catkin/cmake/test/gtest.cmake:149 (message):
skipping gtest 'test_silly_pkg' in project 'silly_pkg' because gtest was
not found
Call Stack (most recent call first):
/opt/ros/kinetic/share/catkin/cmake/test/gtest.cmake:79 (_catkin_add_executable_with_google_test)
/opt/ros/kinetic/share/catkin/cmake/test/gtest.cmake:28 (_catkin_add_google_test)
CMakeLists.txt:28 (catkin_add_gtest)
CMake Error at /home/paco/catkin_ws2/src/silly_pkg/CMakeLists.txt:33 (target_link_libraries):
Cannot specify link libraries for target "test_silly_pkg" which is not
built by this project.
-- Configuring incomplete, errors occurred!
See also "/home/paco/catkin_ws2/build/silly_pkg/CMakeFiles/CMakeOutput.log".
See also "/home/paco/catkin_ws2/build/silly_pkg/CMakeFiles/CMakeError.log".
cd /home/paco/catkin_ws2/build/silly_pkg; catkin build --get-env silly_pkg | catkin env -si /usr/bin/cmake /home/paco/catkin_ws2/src/silly_pkg --no-warn-unused-cli -DCATKIN_DEVEL_PREFIX=/home/paco/catkin_ws2/devel/.private/silly_pkg -DCMAKE_INSTALL_PREFIX=/home/paco/catkin_ws2/install; cd -
I am using catkin 0.7.11, libgtest-dev 1.7.0 and cmake 3.5.1. I use ROS Kinetic with Ubuntu 16.04. The only thing I did yesterday was reinstalling ROS Kinetic, but the package versions are exactly the same. Did anybody have this problem? Do you have any ideas on what could be happening?
EDIT 3/09/18:
By comparing with a functional catkin+gtest workspace in a different computer, I found out that the main difference is in the results of /opt/ros/kinetic/share/catkin/cmake/test/gtest.cmake. In the functional workspace, line 292 evaluates to TRUE (gtest/gmock is not a target) while in my workspace it evaluates to FALSE. This is because in my workspace running find_package(GMock QUIET) (line 287) sets gmock and gtest as imported targets, which does not happen in the other computer. Why is this different?
Thanks TikO for your help!
Since you wrote that cmake does not find the libraries and that you have reinstalled Kinetic, I assume that you have a freshly installed machine or wiped out gtest libraries by accident.
If you install libgtest-dev, you only get the sources which you need to build and install like this:
sudo apt-get install libgtest-dev
mkdir /tmp/gtest_build && cd /tmp/gtest_build
cmake /usr/src/gtest
make
#copy or symlink libgtest.a and ligtest_main.a to /usr/lib folder
sudo cp *.a /usr/lib
After this routine, you should be able to build again without cmake complaining.
Optional
If you have limited rights on your machine and you are not allowed to install the libraries in that way, just copy them into some home folder like
mkdir ~/lib && cp *.a ~/lib
But be aware of the fact, that you have to call catkin in the following way:
LIBRARY_PATH=~/lib GTEST_ROOT=~/lib catkin_make
LIBRARY_PATH tells the linker where to find the libraries, while GTEST_ROOT gives cmake the location hints for it's checks.
Reference: https://github.com/tik0/gtest_ros_example
SOLUTION FOUND
gmock and gtest were being set to imported target because the suggested manual compilation of libgtest had created a FindGMock.cmake file inside /usr/share/cmake-3.5/Modules. This file was being called by the find(GMock QUIET)
in catkin_add_gtests(), therefore setting the imported target. Just deleting FindGMock.cmake solved the issue.

CMake: Set zlib path

I'm trying to compile libzip on Windows using CMake to generate makefiles. Libzip needs zlib so I'm doing the following:
mkdir build
cd build
cmake -DZLIB_LIBRARY=../../../zlib-1.2.11 -DZLIB_INCLUDE_DIR=../../../zlib-1.2.11 ..
This doesn't work, however. I get the following error:
CMake Error at C:/Program Files (x86)/CMake/share/cmake-3.2/Modules/FindPackageHandleStandardArgs.cmake:138 (message):
Could NOT find ZLIB (missing: ZLIB_LIBRARY ZLIB_INCLUDE_DIR)
Why is that? I have defined ZLIB_LIBRARY and ZLIB_INCLUDE_DIR to point to the latest zlib.
I've also tried the following:
mkdir build
cd build
cmake .. -DZLIB_LIBRARY=../../../zlib-1.2.11 -DZLIB_INCLUDE_DIR=../../../zlib-1.2.11
When I run CMake like this, I get the following two warnings:
CMake Warning:
Manually-specified variables were not used by the project:
ZLIB_INCLUDE_DIR
ZLIB_LIBRARY
In comparison to the first approach, however, build files are now written to my build directory. When running nmake, however, only zlib seems to get built. libzip itself isn't built at all.
I'm out of ideas here. What am I doing wrong?
To answer my own question, using relative paths for ZLIB_LIBRARY and ZLIB_INCLUDE_DIR was the problem. When using absolute paths, it works just fine. Furthermore, ZLIB_LIBRARY needs to point to the library itself. Here is my final build line which works:
cmake -DZLIB_INCLUDE_DIR=d:\mystuff\zlib-1.2.11 -DZLIB_LIBRARY=d:\mystuff\zlib-1.2.11\build\zlibstatic.lib -DBUILD_SHARED_LIBS=OFF -DCMAKE_BUILD_TYPE=Release ..

cmake could not find required package TIFF

I'm trying to build an application via cmake 3.9.0. Cmake keeps complaining about the inability to find the tiff library: CMake error at CMakeModules/FindPackageHandleStandardArgs.cmake:51 (Message): Could not find REQUIRED package TIFF). I tried to install the library via sudo apt-get install libtiff5-dev but was still getting the same message. Then I checked-out the source code for libtiff 4 and built it from the source. Now I think we can hint the cmake with the location where to look for the libtiff via setting the variables TIFF_INCLUDE_DIR, TIFF_INCLUDE_DIRS, etc as described here: https://cmake.org/cmake/help/v3.6/module/FindTIFF.html. However I have failed in wiring the right values for the variables. Can somebody show me an example of sample libtiff instalation and the sample values for the configuration variable in order cmake would find the TIFF. Or is here another option how to show CMake where does the TIFF library lie?
cd build
cmake -DTIFF_INCLUDE_DIR=<dir> -DTIFF_LIBRARY=<filename> -GNinja ..
cmake --build .
Alternatively, you can modify the variables in your CMakeLists.txt before calling find_package():
set(TIFF_INCLUDE_DIR "<dir>")
set(TIFF_LIBRARY "<filename>")
find_package(TIFF)
add_executable(myexe TIFF::TIFF)
where <dir> is the include directory path and <filename> is the exact file path to the library.