CMake FetchContent show download progress - cmake

Is there some way to show download progress while downloading git repository using FetchContent?
I tried to clear FETCHCONTENT_QUIET, but it did nothing. I have no idea if it is even possible.

Clarification: To see git clone progress, you need to set both FetchContent_Quiet and GIT_PROGRESS as in the below example
Set(FETCHCONTENT_QUIET FALSE)
FetchContent_Declare(
someTarget
GIT_REPOSITORY "https://github.com/someone/someTarget.git"
GIT_TAG "tag"
GIT_PROGRESS TRUE
)

I just figured out that I have to use GIT_PROGRESS <bool> in FetchContent_Declare.
FetchContent_Declare(
someTarget
GIT_REPOSITORY "https://github.com/someone/someTarget.git"
GIT_TAG "tag"
GIT_PROGRESS TRUE
)

Related

Is it possible to include protobuf using cmake's FetchContent?

I want to use protobuf in my C++ library. All dependencies so far are included using cmake's FetchContent module. I want to do the same with protobuf. However, I run into the following problem: Unknown CMake command "protobuf_generate_cpp". Any hints on how to solve this?
Excerpt of my CMakeLists.txt:
FetchContent_Declare(fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
GIT_TAG 9.0.0)
FetchContent_Declare(protobuf
GIT_REPOSITORY https://github.com/protocolbuffers/protobuf.git
GIT_TAG v21.4)
FetchContent_MakeAvailable(fmt protobuf)
include_directories(${Protobuf_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_BINARY_DIR})
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS message.proto)
This works for me:
FetchContent_Declare(protobuf
GIT_REPOSITORY https://github.com/protocolbuffers/protobuf.git
GIT_TAG v21.4
SOURCE_SUBDIR cmake
FIND_PACKAGE_ARGS NAMES protobuf
)
FetchContent_MakeAvailable(protobuf)
On the consumer end do this
include(FindProtobuf)
find_package(protobuf CONFIG REQUIRED)
Note: This has only been tested on CMake v3.25
protobuf_generate_cpp is from FindProtobuf. It doesn't seem to work with a protoc that was build in the project with FetchContent. You'll have to call the protoc binary explicitly.
set(GENERATED_CODE_DIR ${CMAKE_CURRENT_BINARY_DIR}/generated)
set(PROTO_SRCS ${GENERATED_CODE_DIR}/message.pb.cc)
set(PROTO_HDRS ${GENERATED_CODE_DIR}/message.pb.h)
set(PROTOC ${protobuf_BINARY_DIR}/protoc)
set(PROTO_DIR ${CMAKE_CURRENT_SOURCE_DIR})
add_custom_command(
OUTPUT ${PROTO_SRCS} ${PROTO_HDRS}
COMMAND ${PROTOC} --proto_path ${PROTO_DIR} message.proto --cpp_out ${GENERATED_CODE_DIR}
DEPENDS ${PROTOC} ${PROTO_DIR}/message.proto
)

How to propagate -Wno-dev to cmake using FetchContent_Declare?

I am using the FetchContent feature from CMake (3.12) and declaring it like this:
FetchContent_Declare(libsndfile
GIT_REPOSITORY ${LIBSNDFILE_GIT_REPO}
GIT_TAG ${LIBSNDFILE_GIT_TAG}
GIT_CONFIG advice.detachedHead=false
SOURCE_DIR "${CMAKE_BINARY_DIR}/libsndfile"
BINARY_DIR "${CMAKE_BINARY_DIR}/libsndfile-build"
CMAKE_ARGS "-Wno-dev"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
According to the CMake documentation:
FetchContent_Declare: The <contentOptions> can be any of the download or update/patch options that the ExternalProject_Add() command understands
And according to the ExternalProject_Add documentation, "The specified arguments are passed to the cmake command line" when using CMAKE_ARGS.
The -Wno-dev option does not seem to be passed along as I continue to see this warning messages in the output:
CMake Warning (dev) at /Volumes/Vault/misc/src/libsndfile/CMakeLists.txt:446 (add_executable):
Policy CMP0063 is not set: Honor visibility properties for all target
types. Run "cmake --help-policy CMP0063" for policy details. Use the
cmake_policy command to set the policy and suppress this warning.
Target "sndfile-interleave" of type "EXECUTABLE" has the following
visibility properties set for C:
C_VISIBILITY_PRESET
For compatibility CMake is not honoring them for this target.
This warning is for project developers. Use -Wno-dev to suppress it.
I believe I am following the documentation but it seems I must be doing something wrong. Any idea what could be wrong?
Edit: As requested in comment, here is a complete example:
File CMakeLists.txt
cmake_minimum_required(VERSION 3.12)
project(self_contained_libsndfile_example)
set(CMAKE_CXX_STANDARD 14)
# This is in order to trigger the warnings in FetchContent
set(CMAKE_C_VISIBILITY_PRESET hidden)
include(FetchContent)
set(LIBSNDFILE_GIT_REPO "https://github.com/erikd/libsndfile" CACHE STRING "libsndfile git repository url" FORCE)
set(LIBSNDFILE_GIT_TAG b4bd397ca74f4c72b9cabaae66fef0c3d5a8c527 CACHE STRING "libsndfile git tag" FORCE)
FetchContent_Declare(libsndfile
GIT_REPOSITORY ${LIBSNDFILE_GIT_REPO}
GIT_TAG ${LIBSNDFILE_GIT_TAG}
GIT_CONFIG advice.detachedHead=false
SOURCE_DIR "${CMAKE_BINARY_DIR}/libsndfile"
BINARY_DIR "${CMAKE_BINARY_DIR}/libsndfile-build"
CMAKE_ARGS "-Wno-dev"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
FetchContent_GetProperties(libsndfile)
if(NOT libsndfile_POPULATED)
FetchContent_Populate(libsndfile)
endif()
set(LIBSNDFILE_ROOT_DIR ${libsndfile_SOURCE_DIR})
set(LIBSNDFILE_INCLUDE_DIR "${libsndfile_BINARY_DIR}/src")
add_subdirectory(${libsndfile_SOURCE_DIR} ${libsndfile_BINARY_DIR} EXCLUDE_FROM_ALL)
file(COPY "${libsndfile_SOURCE_DIR}/src/sndfile.hh" DESTINATION ${LIBSNDFILE_INCLUDE_DIR})
include_directories(${LIBSNDFILE_INCLUDE_DIR})
set(target self_contained_libsndfile_example)
add_executable(${target} main.cpp)
target_link_libraries(${target} PRIVATE sndfile)
With the fix of this CMake-issue, which will go into CMake 3.17, you could point variable CMAKE_PROJECT_sndfile_INCLUDE_BEFORE to a file which sets the CMake-policy CMP0063 appropriately and which will automatically be included before the call to project(sndfile). As a result you won't get this warning for your fetched project.
This is a misunderstanding of the CMake documentation. The CMAKE_ARGS is part of the Configure Step options not download or update/patch options of the ExternalProject_Add() and is ignored.
Looking at the documentation for CMake (3.12) [https://cmake.org/cmake/help/v3.12/module/FetchContent.html]
The contentOptions can be any of the download or update/patch
options that the ExternalProject_Add() command understands. The
configure, build, install and test steps are explicitly disabled and
therefore options related to them will be ignored.
To avoid the messages you see you need to invoke cmake as cmake -Wno-dev on the command line when building your project.

ExternalProject_Add autogen project prevent configure on rebuild

I have a CMake project on linux and I'm using ExternalProject to build Google Protobuf. It works great, however any subsequent builds still call the configure step in the external project (which is annoying because protobuf is an autogen project with a rather long step). I used the UPDATE_DISCONNECTED argument so it wouldn't re-clone which helps some, but you'd think if it didn't re-clone, it wouldn't need to re-configure or re-build/install. How can I get CMake to just build it the one time and skip subsequent builds (i.e. my next make from the build directory)?
Here's my CMakeLists.txt
cmake_minimum_required(VERSION 2.8.11)
project(pbuf_test)
include(${PROJECT_SOURCE_DIR}/cmake/protogen.cmake)
set(PBUF_DIR ${PROJECT_BINARY_DIR}/protobuf)
include(ExternalProject)
ExternalProject_Add(protobuf
PREFIX ${PROTOBUF_DIR}
GIT_REPOSITORY https://github.com/google/protobuf.git
GIT_TAG v3.4.1
UPDATE_DISCONNECTED 1
BUILD_IN_SOURCE 1
CONFIGURE_COMMAND ./autogen.sh COMMAND ./configure --prefix=${PBUF_DIR}
)
set(PBUF_INCLUDE_DIR ${PBUF_DIR}/include)
set(PBUF_LIBRARY ${PBUF_DIR}/lib/libprotobuf.so)
set(PBUF_PROTOC ${PBUF_DIR}/bin/protoc)
file(GLOB PBUF_FILES "${PROJECT_SOURCE_DIR}/msg/*.proto")
custom_protobuf_generate_cpp(PBUF_SRCS PBUF_HDRS ${PBUF_FILES})
include_directories(
${PROJECT_BINARY_DIR}
${PBUF_INCLUDE_DIR}
)
add_executable(${PROJECT_NAME} main.cpp ${PBUF_SRCS} ${PBUF_HDRS})
add_dependencies(${PROJECT_NAME} protobuf)
target_link_libraries(${PROJECT_NAME}
${PBUF_LIBRARY}
)
Full example project here
The only way I know is to move the steps to build the external project into a script called by add_command. And then add an dependency between your project and the external project's output/lib/binary.
Pro: Avoids unnecessary rebuilds
Pro: Avoids unexpected updates of the external lib
Con: Does not rebuild in case the external repository was updated (and this is required)
Con: Additional work to write such a script
BTW: Within my project, we do it the hard way. But we do not want any unexpected updates of external libs.
It seems the solution to this for now that still uses ExternalProject is to move the configure step into the download step.
Something like this. Note the name of the target is the directory it expects the download to create. This might only work on certain operating systems because of the use of the && joiner: I'm a little new to cmake.
ExternalProject_Add(protobuf
PREFIX ${PROTOBUF_DIR}
DOWNLOAD_COMMAND git clone https://github.com/google/protobuf.git protobuf &&
cd protobuf &&
git checkout v3.4.1 &&
./autogen.sh &&
./configure --prefix=${PBUF_DIR}
UPDATE_COMMAND git pull
UPDATE_DISCONNECTED 1
BUILD_IN_SOURCE 1
CONFIGURE_COMMAND ""
)

cmake's ExternalProject_Add: how to set source, build and install directories

I want to use ExternalProject_Add function to install cmake enabled projects. I need to control the building and installation processes. More specifically, I want that cmake build and install in specific directories.
There is an option to select a path where to put the source directory: SOURCE_DIR. Is there something equivalent for BUILD_DIR and INSTALL_DIR? I did not see anything alike.
There is a PREFIX option:
Root dir for entire project
What does it mean exactly? An how does cmake's ExternalProject_Add do the installation?
Edit:
This work, but I have no control over source directory and it does not install the library:
ExternalProject_Add(
wjelement-project
GIT_REPOSITORY "https://github.com/netmail-open/wjelement.git"
GIT_TAG "v1.2"
UPDATE_COMMAND ""
PATCH_COMMAND ""
TEST_COMMAND ""
INSTALL_COMMAND ""
#SOURCE_DIR "${MY_SOURCE_ROOT}/wjelement"
BINARY_DIR "${MY_BUILD_DIR}/wjelement"
INSTALL_DIR "${CCT_INSTALL_DIR}/wjelement"
)
If I uncomment SOURCE_DIR, it does not clone from GIT_CLONE but try to get source from SOURCE_DIR (and it fails because that's not what I expected...)
If I comment INSTALL_COMMAND "", then it try to install in C:/Program Files/wjelement, the default (apparently) and not in INSTALL_DIR
default path for source files seem to be ${CMAKE_BINARY_DIR}/wjelement-project-prefix/src
Another Stackoverflow question report problem (or at least non intuitive behavior) with INSTALL_DIR.

ExternalProject_Add doesn't install the Release configuration of the project

EDIT: I have found a probable cause but I do not understand why: the last line in the below script Project(Externals) when removed fixes my issue. So the question now why??
cmake_minimum_required(VERSION 2.8)
include(ExternalProject)
MACRO(EXTERNAL_DEF aNewTargetName aPathToSource)
ExternalProject_Add(
${aNewTargetName}
PREFIX ${CMAKE_INSTALL_PREFIX}
SOURCE_DIR ${aPathToSource}
TMP_DIR "${CMAKE_INSTALL_PREFIX}/tmp/${CMAKE_BUILD_TYPE}"
DOWNLOAD_DIR "${CMAKE_INSTALL_PREFIX}/src/${CMAKE_BUILD_TYPE}"
BINARY_DIR "${CMAKE_INSTALL_PREFIX}/src/${CMAKE_BUILD_TYPE}/${aNewTargetName}-build"
STAMP_DIR "${CMAKE_INSTALL_PREFIX}/src/${CMAKE_BUILD_TYPE}/${aNewTargetName}-stamp"
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}
--debug-output
BUILD_COMMAND "${CMAKE_COMMAND}" --build
"${CMAKE_INSTALL_PREFIX}/src/${CMAKE_BUILD_TYPE}/${aNewTargetName}-build" --config "${CMAKE_BUILD_TYPE}"
#INSTALL_DIR "${CMAKE_INSTALL_PREFIX}"
)
ENDMACRO()
get_filename_component(zlibAbsPath "./zlib" ABSOLUTE)
EXTERNAL_DEF(zlib_external ${zlibAbsPath})
Project(Externals)
I invoke cmake on the above CMakeLists.txt file with CMAKE_INSTALL_PREFIX set to let's say "d:\externals" and CMAKE_BUILD_TYPE set to "Release"
Expectation:
I would expect only the Release configuration to get built. And after it gets built I would expect it to get installed in D:\externals\bin\zlib.dll.
Problem:
In reality, ExternalProject_Add builds both a Debug and a Release, and installs the debug version of the dll in D:\externals\bin\zlibd.dll
Isn't my build script correct? What am I doing wrong?
EDIT:
some more info. I just noticed. In the generated D:\externals\src\Release\zlib_external-build\zlib.sln, the INSTALL target is not selected to build at all. If I check it to build for the Release configuration then hit "Build" from visual studio, the INSTALL target builds and installs the files where I expect them. I have no idea what's going on...
CMAKE_BUILD_TYPE only work with single-configuration projects
you can change to:
BUILD_COMMAND ""
INSTALL_COMMAND
${CMAKE_COMMAND}
--build .
--target install
--config Release
BUILD_ALWAYS 1
Use DCMAKE_CFG_INTDIR variable.
...
CMAKE_ARGS
...
-DCMAKE_CFG_INTDIR=Release