How to build eclipse paho with cmake - cmake

im very new to cmake.
I want to add eclipse paho to my project, therefor I use "ExternalProject_add". Here is an extract of my CMakeLists.txt:
ExternalProject_add(
libressl
URL ${CMAKE_SOURCE_DIR}/externals/libressl-2.5.0.tar.gz
CONFIGURE_COMMAND ./configure --disable-hardening --prefix=${CMAKE_BINARY_DIR}
BUILD_IN_SOURCE 1)
ExternalProject_add(
paho
DEPENDS libressl
URL ${CMAKE_SOURCE_DIR}/externals/eclipse-paho-mqtt-c-src-1.1.0.tar.gz
CMAKE_ARGS -DPAHO_WITH_SSL -DOPENSSL_INC_SEARCH_PATH=${CMAKE_SOURCE_DIR}/include -DOPENSSL_LIB_SEARCH_PATH=${CMAKE_SOURCE_DIR}/lib
BUILD_COMMAND make
BUILD_IN_SOURCE 1)
The thing is libressl is building just fine. As you can see it uses autoconf-tools.
My Problem is that paho throws an error at the "configure step".
~/projects/cmake_test/CL_Test/build$ cmake ..
-- Configuring done
-- Generating done
-- Build files have been written to: /projects/cmake_test/CL_Test/build
:~/projects/cmake_test/CL_Test/build$ make
[ 50%] Built target libressl
[ 56%] Performing configure step for 'paho'
CMake Error at /projects/cmake_test/CL_Test/build/paho-prefix/src/paho-stamp/paho-configure-.cmake:16 (message):
Command failed: 1
'/usr/bin/cmake' '-DPAHO_WITH_SSL' '-DOPENSSL_INC_SEARCH_PATH=/home/hbaumann/projects/cmake_test/CL_Test/include' '-DOPENSSL_LIB_SEARCH_PATH=/home/hbaumann/projects/cmake_test/CL_Test/lib' '-GUnix Makefiles' '/projects/cmake_test/CL_Test/build/paho-prefix/src/paho'
CMakeFiles/paho.dir/build.make:107: recipe for target 'paho-prefix/src/paho-stamp/paho-configure' failed
make[2]: *** [paho-prefix/src/paho-stamp/paho-configure] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/paho.dir/all' failed
make[1]: *** [CMakeFiles/paho.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
I thought, since paho is build with the use of CMake by default, there is nothing I really need to add. Here you can see, my knowledge about CMake is really low.
What makes me wonder is that in the paho tar-ball, there is only a Makefile in the main folder and not an CMakeLists.txt.
What do i miss... is there a special configure command I need to set ?
thx in advance

you can use the GIT repository, it has a CMakeList.txt.
ExternalProject_add(
paho
DEPENDS libressl
GIT_REPOSITORY "https://github.com/eclipse/paho.mqtt.c.git"
GIT_TAG "master"
UPDATE_COMMAND ""
PATCH_COMMAND ""
SOURCE_DIR "${CMAKE_BINARY_DIR}/paho_sourcedir"
CMAKE_ARGS -DPAHO_WITH_SSL=TRUE -DOPENSSL_INC_SEARCH_PATH=${CMAKE_BINARY_DIR}/include -DOPENSSL_LIB_SEARCH_PATH=${CMAKE_BINARY_DIR}/lib -DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/paho_prefix
)

Related

Catkin and CMake integration question for cross-compilation ROS node

Trying to cross-compile a ros node which relays on some arm64 libraries located in a different path other than /usr/lib/arm64-
Error received is:
Errors << gps_confidence_module:make /gitlab/soads-ros/DAS/ros/logs/gps_confidence_module/build.make.013.log
make[2]: *** No rule to make target '/usr/lib/aarch64-linux-gnu/libconsole_bridge.so.0.4', needed by '/gitlab/soads-ros/DAS/ros/devel/.private/gps_confidence_module/lib/gps_confidence_module/gps_confidence_module'. Stop.
make[1]: *** [CMakeFiles/gps_confidence_module.dir/all] Error 2
make: *** [all] Error 2
cd /gitlab/soads-ros/DAS/ros/build/gps_confidence_module; catkin build --get-env gps_confidence_module | catkin env -si /usr/bin/make --jobserver-fds=6,7 -j; cd -
.....................................................................................................................................................................................
The error goes away when I add the path inside the CMakelist.txt with:
link_directories(
${PDK_PATH}/targetfs/usr/lib/aarch64-linux-gnu
)
Is there a way to make this link_directories global to my catkin environment?
trying to avoid to insert the link_directories code on every single CMakelists.txt of my ROS source code.
You should not do this in your CMakeLists.txt files yourself. This kind of info should go into your toolchain file (see https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html).
I haven't worked with your kind of setup, but setting CMAKE_SYSROOT should work in your case.
toolchain-arm64.cmake
set(PDK_PATH ...)
set(CMAKE_SYSTEM_NAME ...)
set(CMAKE_SYSTEM_PROCESSOR arm64)
set(CMAKE_CXX_COMPILER /path/to/c++/compiler)
set(CMAKE_C_COMPILER /path/to/c/compiler)
set(CMAKE_SYSROOT "${PDK_PATH}/targetfs")
...
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

ExternalProject_Add for Makefile project error during build

I am trying to add Postgresql as a dependency for my project for which I am using ExternalProject module to download the source from github and build, but the build step fails when running from cmake (cmake --build .). Configure step seems to succeed and if I go to the Build directory under EP_BASE and do a make it runs successfully. I get the following error during build:
<...>/Source/postgresql_external/src/common/relpath.c:21:10: fatal error: catalog/pg_tablespace_d.h: No such file or directory
21 | #include "catalog/pg_tablespace_d.h"
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make[5]: *** [<builtin>: relpath.o] Error 1
make[4]: *** [Makefile:42: all-common-recurse] Error 2
make[3]: *** [GNUmakefile:11: all-src-recurse] Error 2
My external project add looks like the following:
ExternalProject_Add(postgresql_external
GIT_REPOSITORY https://github.com/postgres/postgres.git
GIT_TAG REL_12_4
CONFIGURE_COMMAND <SOURCE_DIR>/configure --prefix=<INSTALL_DIR>
LOG_CONFIGURE 1
LOG_BUILD 1
LOG_INSTALL 1
)
This is running on Ubuntu 20.04 LTS, with cmake 3.16.3, gcc 9.3.0
try
ExternalProject_Get_Property(postgresql_external install_dir)
include_directories(${install_dir}/include)
I guess, you haven't propagate include directory to your target yet, but it is evtl. known to your system (thus successful call of manually called make)
Try the following code, it works for me. PotgreSQL uses MAKELEVEL variable to generate header files via perl. When you call make directly it works as expected. But it seems that cmake adds more levels to PotgreSQL's root make, so headers are not generated.
CONFIGURE_COMMAND ./configure <your options>
BUILD_IN_SOURCE 1
BUILD_COMMAND $(MAKE) MAKELEVEL=0

How to add Ziplib library in Clion on Ubuntu

I'm trying to add ZipLip into my project using Clion on ubuntu, but I have this output:
====================[ Build | TryZip | Debug ]==================================
/home/david/Snap/clion-2019.2.4/bin/cmake/linux/bin/cmake --build
/home/david/CLionProjects/TryZip/cmake-build-debug --target TryZip -- -j 2
[ 13%] Built target bzip2
[ 31%] Built target zlib
[ 83%] Built target lzma
[ 95%] Built target ZipLib
Scanning dependencies of target TryZip
[ 97%] Linking CXX executable ../bin/TryZip
/usr/bin/ld: cannot find -lExternalLibrary/ZipLib
collect2: error: ld returned 1 exit status
CMakeFiles/TryZip.dir/build.make:102: recipe for target '../bin/TryZip' failed
make[3]: *** [../bin/TryZip] Error 1
CMakeFiles/Makefile2:109: recipe for target 'CMakeFiles/TryZip.dir/all' failed
make[2]: *** [CMakeFiles/TryZip.dir/all] Error 2
CMakeFiles/Makefile2:116: recipe for target 'CMakeFiles/TryZip.dir/rule' failed
make[1]: *** [CMakeFiles/TryZip.dir/rule] Error 2
Makefile:131: recipe for target 'TryZip' failed
make: *** [TryZip] Error 2
This is my Cmakefile.txt
cmake_minimum_required(VERSION 3.15)
project(TryZip)
if(BOOST_FILESYSTEM)
include_directories(${BOOST_INCLUDE_DIR})
link_directories(${BOOST_LIB_DIR})
add_definitions(-DUSE_BOOST_FILESYSTEM)
else()
if(MSVC)
add_definitions(-DFILESYSTEM_EXPERIMENTAL)
endif()
endif()
if(BOOST_FILESYSTEM)
if(UNIX)
find_package(Boost COMPONENTS system filesystem REQUIRED)
target_link_libraries(${Boost_FILESYSTEM_LIBRARY}
${Boost_SYSTEM_LIBRARY})
endif()
endif()
add_subdirectory(ExternalLibrary/ZipLib)
link_libraries(ExternalLibrary/ZipLib)
include_directories(ExternalLibrary/ZipLib)
set(CMAKE_CXX_STANDARD 17)
add_executable(TryZip main.cpp ExternalLibrary/ZipLib/ZipFile.cpp)
target_link_libraries(TryZip ZipLib)
Can someone help me to solve this please?
My ZipLib folder is in the same folder as my cmakefile.txt file.
The call to link_libraries() appears to accept the wrong arguments in this case. The link_libraries() command takes arguments of existing CMake targets, or library names. It is also redundant with your target_link_libraries() call, as this already links ZipLib to TryZip.
Try removing the call to link_libraries(), as this CMake function is deprecated and its use is highly discouraged. The include_directories() call is similarly deprecated, in favor of the target-specific command, so consider using target_include_directories() instead.
Assuming your added sub-directory ExternalLibrary/ZipLib contains an additional CMakeLists.txt file for configuring the ZipLib target, you should not need to add the ZipFile.cpp file again. If this file is already compiled in the sub-directory into the target ZipLib, you do not need to compile it again into TryZip.
add_subdirectory(ExternalLibrary/ZipLib)
set(CMAKE_CXX_STANDARD 17)
add_executable(TryZip main.cpp)
target_include_directories(TryZip PRIVATE ExternalLibrary/ZipLib)
target_link_libraries(TryZip PRIVATE ZipLib)
EDIT: Based on your feedback, it appears ZipLib also depends on pthread but somehow it is not getting linked correctly. You might try to add the following to your ExternalLibrary/ZipLib/CMakeLists.txt file (if it doesn't already exist), to utilize CMake's FindThreads module:
find_package(Threads REQUIRED)
...
target_link_libraries(ZipLib PUBLIC Threads::Threads)

CMakeLists.txt not in root directory, can't do an automatic build

I have an issue with a repository not having its CMakeLists in the root directory, namely https://github.com/lz4/lz4
The CMakeLists.txt is in the subfolder contrib/cmake_unofficial.
I already checked similar questions on SO (Is it possible to have cmake build file (CMakeLists.txt) not in root in CLion, cmake - CMakeLists.txt is not in root folder (but is included in source)), but they only provide alternatives, and not a solution applicable to my situation.
Heres the cmake module I came up with:
if(ENABLE_LZ4)
message(STATUS "Using LZ4.")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DENABLE_LZ4")
# Enable ExternalProject CMake module
include(ExternalProject)
set(LZ4_SOURCE_DIR ${CMAKE_CURRENT_BINARY_DIR}/lz4)
set(LZ4_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/lib)
# Download and install lz4
ExternalProject_Add(
lz4
GIT_REPOSITORY https://github.com/lz4/lz4.git
GIT_TAG dev
SOURCE_DIR ${LZ4_SOURCE_DIR}
BINARY_DIR ${LZ4_BINARY_DIR}
INSTALL_COMMAND ""
CMAKE_ARGS
${LZ4_SOURCE_DIR}/contrib/cmake_unofficial
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
)
# Get lz4 source and binary directories from CMake project
ExternalProject_Get_Property(lz4 source_dir binary_dir)
# Create a liblz4 target to be used as a dependency by the program
add_library(liblz4 IMPORTED SHARED GLOBAL)
add_dependencies(liblz4 lz4)
include_directories(
${LZ4_SOURCE_DIR}/lib
)
set(LZ4_LIB ${LZ4_BINARY_DIR}/liblz4.so)
else()
message(STATUS "Not using LZ4.")
set(LZ4_LIB "")
endif()
Here the complete error output:
[ 0%] Performing update step for 'lz4'
Current branch dev is up to date.
[ 1%] Performing configure step for 'lz4'
CMake Error: The source directory "/****/build/lz4" does not appear to contain CMakeLists.txt.
Specify --help for usage, or press the help button on the CMake GUI.
CMakeFiles/lz4.dir/build.make:105: recipe for target 'lz4-prefix/src/lz4-stamp/lz4-configure' failed
make[2]: *** [lz4-prefix/src/lz4-stamp/lz4-configure] Error 1
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/lz4.dir/all' failed
make[1]: *** [CMakeFiles/lz4.dir/all] Error 2
Makefile:94: recipe for target 'all' failed
make: *** [all] Error 2
I tried adding the path 'contrib/cmake_unofficial' to the CMAKE_ARGS variable (as seen in the module above), but it does not work (seems to be ignored?).
I also tried using PATCH_COMMAND to copy the CMakeLists.txt to the root before the build starts, but the relative paths of the file get messed up.
In other words, i need the cmake command to be called to build the library to be : cmake contrib/cmake_unofficial.
I also tried using CONFIGURE_COMMAND for this, but keep getting a file not found error for some reason (even though the path is correct).
The module has some other issues too, but I'm only interested in the non-root CMakeLists.
Thanks in advance!
ExternalProject separates download and source directories:
DOWNLOAD_DIR - a directory where downloading step is performed
SOURCE_DIR - a directory used as a source one when configuration step is performed
When use git for extract the project, note that git clone is called from the download directory, where it creates new directory with a project sources.
set(LZ4_DOWNLOAD_DIR ${CMAKE_CURRENT_BINARY_DIR}/lz4)
# Set a source dir based on the download one.
set(LZ4_SOURCE_DIR ${LZ4_DOWNLOAD_DIR}/lz4/contrib/cmake_unofficial)
set(LZ4_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/lib)
# Download and install lz4
ExternalProject_Add(
lz4
DOWNLOAD_DIR ${LZ4_DOWNLOAD_DIR} # Set download directory explicitely
GIT_REPOSITORY https://github.com/lz4/lz4.git
GIT_TAG dev
SOURCE_DIR ${LZ4_SOURCE_DIR}
BINARY_DIR ${LZ4_BINARY_DIR}
...
)
Probably not the correct way to do it, but this seems to work for me.
I used the CONFIGURE_COMMAND to call cmake on the correct directory.
Then use BUILD_COMMAND to call make
So essentially, it breaks down to this:
ExternalProject_Add(
lz4
GIT_REPOSITORY https://github.com/lz4/lz4.git
GIT_TAG dev
SOURCE_DIR ${LZ4_SOURCE_DIR}
BINARY_DIR ${LZ4_BINARY_DIR}
CONFIGURE_COMMAND cmake ${LZ4_SOURCE_DIR}/contrib/cmake_unofficial
BUILD_COMMAND make
INSTALL_COMMAND ""
CMAKE_ARGS
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
)

How to not make install step when building external project with cmake

I'm building dependency project with cmake ExternalProject_Add command:
include(ExternalProject)
...
set(COMMON_BASE_PROJECT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../CommonBase)
ExternalProject_Add(CommonBaseProject
SOURCE_DIR ${COMMON_BASE_PROJECT_DIR}
BINARY_DIR ${COMMON_BASE_PROJECT_DIR}/build
INSTALL_COMMMAND ""
)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
include_directories(${COMMON_BASE_PROJECT_DIR}/include)
add_library(
${LIBRARY_NAME}
SHARED
${SRC_FILES}
${INCLUDE_FILES}
)
target_link_libraries (Bios ${COMMON_BASE_PROJECT_DIR}/build/libCommonBase.dll)
add_dependencies(Bios CommonBaseProject)
but i get error:
[100%] Linking CXX shared library libCommonBase.dll
[100%] Built target CommonBase
[ 50%] Performing install step for 'CommonBaseProject'
make[3]: *** No rule to make target 'install'. Stop.
I don't need to make install step, so my question is: how to disable it?
You almost had it: Instead of INSTALL_COMMAND "" put something like
INSTALL_COMMAND cmake -E echo "Skipping install step."
You can generate a target for the build step with STEP_TARGETS build and add dependency on this particular target. The step targets are named <external-project-name>-<step-name> so in this case the target representing the build step will be named CommonBaseProject-build.
You probably also want to exclude the CommonBaseProject from the "all" target with EXCLUDE_FROM_ALL TRUE.
ExternalProject_Add(CommonBaseProject
SOURCE_DIR ${COMMON_BASE_PROJECT_DIR}
BINARY_DIR ${COMMON_BASE_PROJECT_DIR}/build
STEP_TARGETS build
EXCLUDE_FROM_ALL TRUE
)
add_dependencies(Bios CommonBaseProject-build)
Not relevant to your question, which it was already answered, but in my case I had the following ExternalProject_Add directive:
ExternalProject_Add(external_project
# [...]
# Override build/install command
BUILD_COMMAND ""
INSTALL_COMMAND
"${CMAKE_COMMAND}"
--build .
--target INSTALL # Wrong casing for "install" target
--config ${CMAKE_BUILD_TYPE}
)
In this case cmake quits with very similar error (*** No rule to make target 'INSTALL'), but in this case it's the external project that is looking for incorrect uppercase INSTALL target: correct case is install instead. Apparently, that worked in Windows with MSVC but fails in unix operating systems.
Since at least CMake 3.10 the empty string is sufficient to suppress the install step:
Passing an empty string as the <cmd> makes the install step do nothing.
The same goes for the other stages; see the docs for more.
If you're still building with CMake <3.10 then you need to update CMake ;)