How can I add external projects in CMake when the project's repo isn't the root of the library I want to use, but in fact contains two directories which are each root directories of repos that I want to use in my project?
I'm working to set up a framework CMake project that uses Google Test and Mock for testing, however when I try to download the google test repo (https://github.com/google/googletest) with ExternalProject_Add, it complains on build that it can't find the source for the project. Well, that's because Google have merged googletest and googlemock into a single project, except it's now two projects.
Some of the repo's file structure:
googletest-master/
├──[...no CMakeFiles.txt exists here...]
├──googletest/
│ ├──src/
│ └──CMakeFiles.txt
└──googlemock/
├──src/
└──CMakeFiles.txt
When I do the following...
ExternalProject_Add(
gtest
GIT_REPOSITORY https://github.com/google/googletest.git
TIMEOUT 10
INSTALL_COMMAND ""
LOG_DOWNLOAD ON
LOG_CONFIGURE ON
LOG_BUILD ON
PREFIX "googletest-master"
)
...it downloads the actual repo to googletest-master/src/gtest because I'm prefixing the repo with "googletest-master" to keep it out of my main source code, and it assumes that I'm downloading a project that is only source and that source is in the root directory.
So I'd like to accomplish two things:
Download the repo into the googletest-master directory, exactly as it would be if I cloned the repo there, or downloaded the zip off GitHub and extracted it.
Build and include both googletest and googlemock in my CMake project
You need single download step, but two build steps. Different ExternalProject_add command calls cannot share steps, but you can arrange all these steps into different calls with appropriate dependencies between them:
# Single download(git clone)
ExternalProject_Add(
googletest-master
DOWNLOAD_DIR "googletest-master/src" # The only dir option which is required
GIT_REPOSITORY https://github.com/google/googletest.git
TIMEOUT 10
LOG_DOWNLOAD ON
# Disable all other steps
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
)
# Build gtest from existing sources
ExternalProject_Add(
gtest
DOWNLOAD_COMMAND "" # No download required
SOURCE_DIR "googletest-master/src/googletest" # Use specific source dir
PREFIX "googletest-master" # But use prefix for compute other dirs
INSTALL_COMMAND ""
LOG_CONFIGURE ON
LOG_BUILD ON
)
# gtest should be build after being downloaded
add_dependencies(gtest googletest-master)
# Build gmock from existing sources
ExternalProject_Add(
gmock
DOWNLOAD_COMMAND "" # No download required
SOURCE_DIR "googletest-master/src/googlemock" # Use specific source dir
PREFIX "googletest-master" # But use prefix for compute other dirs
INSTALL_COMMAND ""
LOG_CONFIGURE ON
LOG_BUILD ON
)
# gmock should be build after being downloaded
add_dependencies(gmock googletest-master)
I have a repository where I import gtest and gmock libraries using CMake, just as you want to, but using the old SVN source repository instead of GIT.
I think the key is getting sources only for gmock, since it includes gtest sources and then export both libraries.
The CMakeLists.txt looks like this:
cmake_minimum_required(VERSION 2.8)
include(ExternalProject)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(GMOCK_VERSION "1.7.0")
set(GMOCK_DIR "${CMAKE_CURRENT_BINARY_DIR}/gmock-${GMOCK_VERSION}")
ExternalProject_Add(project_gmock
SVN_REPOSITORY http://googlemock.googlecode.com/svn/tags/release-${GMOCK_VERSION}
PREFIX ${GMOCK_DIR}
CMAKE_ARGS -DCMAKE_C_COMPILER:PATH=${CMAKE_C_COMPILER} -DCMAKE_CXX_COMPILER:PATH=${CMAKE_CXX_COMPILER}
# Disable update step
UPDATE_COMMAND ""
# Disable install step
INSTALL_COMMAND ""
)
ExternalProject_Get_Property(project_gmock source_dir)
ExternalProject_Get_Property(project_gmock binary_dir)
include_directories(${source_dir}/gtest/include)
add_library(gtest STATIC IMPORTED)
set_property(TARGET gtest PROPERTY IMPORTED_LOCATION ${binary_dir}/gtest/libgtest.a)
add_dependencies(gtest project_gmock)
add_library(gtest_main STATIC IMPORTED)
set_property(TARGET gtest_main PROPERTY IMPORTED_LOCATION ${binary_dir}/gtest/libgtest_main.a)
add_dependencies(gtest_main project_gmock)
include_directories(${source_dir}/include)
add_library(gmock STATIC IMPORTED)
set_property(TARGET gmock PROPERTY IMPORTED_LOCATION ${binary_dir}/libgmock.a)
add_dependencies(gmock project_gmock)
add_library(gmock_main STATIC IMPORTED)
set_property(TARGET gmock_main PROPERTY IMPORTED_LOCATION ${binary_dir}/libgmock_main.a)
add_dependencies(gmock_main project_gmock)
include_directories(${cpp_utest_SOURCE_DIR}/src)
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")
#add_executable(utests )
#target_link_libraries(utests gmock_main gmock gtest pthread)
Related
I am building a flutter plugin which calls native functions from lib.dll file and everything works as expected in my computer.
But I use relative path to link that lib such as
E:/_Projects/mahesabu/client/packages/server/windows/lib.dll
Now I want to move the build process in CI/CD which I believe using relative path such as
./lib.dll would be very easy.
Of cource I am new to cmake configuration. And in one comment it is written
List of absolute paths to libraries that should be bundled with the plugin
I wonder how can I use relative path there, because if I try build fails. The following is CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
set(PROJECT_NAME "server")
project(${PROJECT_NAME} LANGUAGES CXX)
# This value is used when generating builds using this plugin, so it must
# not be changed
set(PLUGIN_NAME "server_plugin")
add_library(${PLUGIN_NAME} SHARED
"server_plugin.cpp"
)
apply_standard_settings(${PLUGIN_NAME})
set_target_properties(${PLUGIN_NAME} PROPERTIES
CXX_VISIBILITY_PRESET hidden)
target_compile_definitions(${PLUGIN_NAME} PRIVATE FLUTTER_PLUGIN_IMPL)
target_include_directories(${PLUGIN_NAME} INTERFACE
"${CMAKE_CURRENT_SOURCE_DIR}/include")
target_link_libraries(${PLUGIN_NAME} PRIVATE flutter flutter_wrapper_plugin)
# List of absolute paths to libraries that should be bundled with the plugin
set(server_bundled_libraries
""
"E:/_Projects/mahesabu/client/packages/server/windows/lib.dll" #USE RELATIVE PATH HERE
PARENT_SCOPE
)
Any help will be appreciated.
Just use:
set(server_bundled_libraries "${CMAKE_CURRENT_SOURCE_DIR}/lib.dll" PARENT_SCOPE)
The CMAKE_CURRENT_SOURCE_DIR variable will expand to current source directory as tracked by add_subdirectory. This is usually, but not always, the directory in which the present CMakeLists.txt resides. Presumably, this is E:/_Projects/mahesabu/client/packages/server/windows on your computer (given your remark that you expect ./lib.dll to work), but will be somewhere else on CI or elsewhere.
I am trying to install a custom application on my Yocto build.
I currently use a Raspberry Pi 4 64 bit setup, for which I want PyQt5 to display an application directly on the frame buffer (so no windowing manager or desktop envoirement).
My current build with Yocto completes and boots on the Raspberry Pi. All the Qt5 libraries are also present in the root fs after the bitbake build.
Although, I'm having problems getting a custom layer, that adds a custom recipe with a custom application to also copy over to the destination root fs.
My custom layer is called 'meta-rpikms' with recipe 'recipes-kms-qt-app' which contains the application bb files. This files is called 'basicquick_0.1.bb' and has the following contents (this test application tries to add a EGLFS friendly Qt5 applicaiton, i'll try PyQt5 later):
SUMMARY = "Simple Qt5 Quick application"
#SECTION = "examples"
LICENSE = "MIT"
#PACKAGE_ARCH = "all"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
DEPENDS += "qtbase qtdeclarative qtquickcontrols2"
SRCREV = "${AUTOREV}"
SRC_URI = "git://github.com/shigmas/BasicQuick.git"
S = "${WORKDIR}/git"
require recipes-qt/qt5/qt5.inc
do_install() {
install -d ${D}${bindir_native}
install -m 0755 BasicQuick ${D}${bindir_native}
}
FILES_${PN} += "${bindir_native}"
When I bitbake the custom layer of my meta-rpikms (called 'qt5-kms-rpi-image'), it does compute. When I take a look at the 'image_initial_manifest' file, my custom application does show up, suggesting that it does compile and try to install the application:
# This file was generated automatically and contains the packages
# passed on to the package manager in order to create the rootfs.
# Format:
# <package_type>,<package_name>
# where:
# <package_type> can be:
# 'mip' = must install package
# 'aop' = attempt only package
# 'mlp' = multilib package
# 'lgp' = language package
mip,basicquick
mip,bluez5
mip,bridge-utils
mip,hostapd
mip, bla bla bla etc etc etc.
And if I take a look in: '~/builds/pyqt5_try1/poky/rpi64-build/tmp/work/cortexa72-poky-linux/basicquick/0.1-r0', the expected files do show up. Also suggesting that it does atleast build the application. This makes the think that the 'install' arguments in my .bb file are pointing to the wrong folder.
In my basicquick_*.bb file this is to make the directory and install (copy) the built files:
install -d ${D}${bindir_native}
install -m 0755 BasicQuick ${D}${bindir_native}
I used the 'bitbake -e' command to trace the variable D and bindir_native:
D="/home/mats/builds/pyqt5_try1/poky/rpi64-build/tmp/work/raspberrypi4_64-poky-linux/qt5-kms-rpi-image/1.0-r0/image"
bindir_native="/usr/bin"
This seems okay at first glance, but when I manually follow the destination of the variable 'D', there is no 'images' folder created. I also wonder why everybody installs their custom applications on ${D}/usr/bin? Should this not be written to the build/tmp/deploy directory? Or am I missing a step here.
So, in this 'qt5-kms-rpi-image/1.0-r0' folder, there is a folder called 'deploy-qt5-kms-rpi-image-image-complete', which contains a rootfs. But there also is a rootfs folder in the 'qt5-kms-rpi-image/1.0-r0' folder. Both of these rootfs's do not contain any mention of my basicquick application, or a BasicQuick folder being created in /usr/bin.
Also, the rootfs found in the "build/tmp/deploy/images/raspberrypi4-64/qt5-kms-rpi-image-raspberrypi4-64.tar.bz2" does not contain any mention of the basicquick application being present in the filesystem.
Does anybody have any clues on what I am missing? Am I just not copying my files to the correct location? Or does the final deploy image end up somewhere else from where I am expecting it?
Thanks in advance.
With kind regards,
Mats de Waard
I need to download an entire GitHub repository using Bazel. Since I'm quite new to this tool I'm not really sure how to achieve that.
My main idea is this:
write a custom repository rule in downloadgithubrepo.bzl (which is located in the project root just like the WORKSPACE file) such as:
def _impl(repository_ctx):
repository_ctx.download("url_to_zipped_github_repo", output='relative_path_to_output_file')
github = repository_rule(
implementation = _impl
and in the WORKSPACE file to write something like this:
load("//:downloadgithubrepo.bzl", "github")
and to invoke a build a BUILD file is needed (also located at the project root)
its contents are the following:
cc_library(
name = "testrun",
srcs = "main.c",
)
I had to add the main.c file otherwise the build is failing - that is one issue and the real issue is that this does not work, as in the build is passing but the GitHub repository is not downloaded.
Am I on the right path at all?? Has anyone done something like this before?
What you're looking might already be implemented in the new_git_repository repository rule, or the git_repository rule if the GitHub project already has Bazel BUILD files wired in.
If the GitHub project does not have BUILD files, a BUILD file is required when using new_git_repository. For example, if you want to depend on a file target (e.g. /foo/bar.txt) or rule target (e.g. a cc_library) in https://github.com/example/repository, and the repository does not have BUILD files, write these lines in your project's WORKSPACE file:
new_git_repository(
name = "example_repository",
remote = "https://github.com/example/repository.git",
build_file_content = """
exports_files(["foo/bar.txt"])
# you can also create targets
cc_library(
name = "remote_cc_library",
srcs = ["..."],
hdrs = ["..."],
)
""",
)
In your BUILD file, reference the external repository's targets using the # prefix:
cc_library(
name = "testrun",
srcs = ["main.c"],
data = ["#example_repository//:foo/bar.txt"],
deps = ["#example_repository//:remote_cc_library"],
)
When you run bazel build //:testrun, Bazel will..
Analyze the dependencies of //:testrun, which include the file main.c and targets from the external repository #example_repository.
Look up the WORKSPACE file for an external repository named example_repository, and finds the new_git_repository declaration.
Perform a git clone on the remote attribute specified in the example_repository declaration.
Write a BUILD file containing the build_file_content string at the project root of the cloned repository.
Analyze the targets #example_repository//:foo/bar.txt and #example_repository//:remote_cc_library
Build the dependencies, and hands them to your //:testrun cc_library.
Build //:testrun.
If the GitHub project does have BUILD files, you do not need to provide an BUILD file. You can refer to the targets directly after specifying the WORKSPACE dependency with git_repository:
git_repository(
name = "example_repository",
remote = "https://github.com/example/repository.git",
)
For more information, check out Bazel's documentation on External Repositories.
I'm trying NetBeans as my new IDE for c++. I would love to use conan.io as package manager.
My conanfile.py looks like this (from the conan site):
class MyConanTestProj(ConanFile):
settings = "os", "compiler", "build_type", "arch"
requires = "Protobuf/3.1.0#inexorgame/stable", "Boost/1.64.0#conan/stable" # comma separated list of requirements
generators = "cmake", "txt"
default_options = "Poco:shared=True", "OpenSSL:shared=True", "Boost:shared=True"
def imports(self):
self.copy("*.dll", dst="bin", src="bin") # From bin to bin
self.copy("*.dylib*", dst="bin", src="lib") # From lib to bin
# self.copy('*.so*', dst='bin', src='lib')
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
My CMakeLists.txt:
project(MyTestProj)
cmake_minimum_required(VERSION 2.8.12)
add_definitions("-std=c++14")
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
add_executable(testProj testProj.cpp)
target_link_libraries(testProj ${CONAN_LIBS})
testProj.cpp: (just to see it compile and link...)
#include <boost/filesystem.hpp>
int main(void) { return 0; }
When I create a build dir and run conan install and so on it works:
mkdir build -p && cd build && conan install .. && cmake .. -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release && cmake --build
But in NetBeans it shows me an unresolved include (netbeans has no idea there conan has stored the boost files).
How to configurate netbeans to use the include paths generated by conan?
Conan created conanbildinfo.cmake and conanbuildinfo.txt with the full paths included but i don't know how to use them in netbeans.
Hope someone could tell me how to setup this correctly (or give me a short example project for netbeans) - Thanks!
Netbeans might be using a particular build folder layout, as previous versions of CLion did in the past, just putting the "temporary" build folders somewhere in the system.
I guess that the CMake execution is complaining about not finding the conanbuildinfo.cmake file, not the Boost headers.
So, the first thing is to know which folder is being used by Netbeans as CMake binary dir. You could add to your CMakeLists.txt:
if(NOT EXISTS ${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
message(FATAL_ERROR "Missing conanbuildinfo. Move to ${CMAKE_BINARY_DIR} folder, and run conan install there")
endif()
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
Then, just cd to that folder, conan install with the respective configuration (note that this process may need to be run for different configurations, like Debug/Release).
I am using CMake to build a project with external libraries by using "Eclipse CDT4 - Unix Makefiles".
Importing in Eclipse leads to a working project, but only all header files and my implemented source files are recognized correctly by the index of Eclipse.
I would also like to navigate through the source files for one external library by using "ctrl+click". I don't know how to add the *.cpp files of that external library in my CMakeList.txt to get them recognized by the indexer without building the library.
You can mark the .cpp files as "header file only" like this:
# find all filenames in the lib path and gather them in $YOUR_LIB
FILE(GLOB YOUR_LIB path_to_library/*.?pp)
# create a seperate sourcegroup so it doesn't clutter up the rest of your code
SOURCE_GROUP(\\lib FILES ${YOUR_LIB})
# mark them as header-file only
SET_SOURCE_FILES_PROPERTIES(${YOUR_LIB} PROPERTIES HEADER_FILE_ONLY TRUE)
# add both your code and the lib-code to the project
ADD_EXECUTABLE(program ${YOUR_CODE} ${YOUR_LIB})
I found a way to attach external library source files to the Eclipse project that is compatible with CMake project generator.
It turns out that to indexing and "ctrl+click" navigation works correctly only when external library sources are direct descendants of the project source folder. Therefore the solution is following:
Scan external library folder for source files.
Create a child folder under project's source folder.
Symlink discovered sources inside the created folder.
I created a CMake function attachExternalSources that performs above steps:
function(attachExternalSources librarySourceLocation folderName)
# Create folder for Geant4 sources
file(MAKE_DIRECTORY ${CMAKE_SOURCE_DIR}/${folderName})
message(STATUS "Searching for C++ sources in \"${librarySourceLocation}\"...")
FILE(GLOB_RECURSE libSources
${librarySourceLocation}/*.c
${librarySourceLocation}/*.cpp
${librarySourceLocation}/*.cxx
${librarySourceLocation}/*.cc
)
message(STATUS "Symlinking sources into\n \"${CMAKE_SOURCE_DIR}/${folderName}\"\n Please wait...")
foreach(source ${libSources})
# Obtain source filename
get_filename_component(source_filename ${source} NAME)
# Create symlink unless it already exists
set(symlink "${CMAKE_SOURCE_DIR}/${folderName}/${source_filename}")
if(NOT EXISTS ${symlink})
execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink ${source} ${symlink})
endif()
endforeach()
# Scan all the symlinks created under the project folder and disable their compilation
FILE(GLOB sources_symlinks ${CMAKE_SOURCE_DIR}/${folderName}/*)
SET_SOURCE_FILES_PROPERTIES(${sources_symlinks} PROPERTIES HEADER_FILE_ONLY TRUE)
endfunction()
The use of the function is following. Paste above function code in your CMakeLists.txt. Next, use it as follows:
attachExternalSources("path/to/external/library/sources" "library-sources")
First parameter is location of the external library source code. Second argument is the name of a folder inside your project that that will contain source symlinks.
P.S. I tested function with Eclipse 4.19 and CMake 3.20.5.