Prevent FetchContent from being re-configured if already configured - cmake

I have several FetchContents Github repos in my CMakeLists.txt and they get reconfigured by CMake every time I modify my CMakeLists.txt:
FetchContent_Declare(fmt GIT_REPOSITORY https://github.com/fmtlib/fmt)
FetchContent_MakeAvailable(fmt)
FetchContent_Declare(glfw GIT_REPOSITORY https://github.com/glfw/glfw)
FetchContent_MakeAvailable(glfw)
# Several more GitHub repositories using FetchContent
# ...
Is it possible to set FetchContent to skip the library that is already FetchContented?

Related

Cmake Fetchcontent not finding dependency

Im trying to use two external git projects in my project, where one depends on the other. However I cannot make it work.
FetchContent_Declare(
spdlog
GIT_REPOSITORY https://github.com/gabime/spdlog.git
GIT_TAG v1.11.0
)
FetchContent_MakeAvailable(spdlog)
FetchContent_Declare(
spdlog_setup
GIT_REPOSITORY https://github.com/guangie88/spdlog_setup.git
GIT_TAG v0.3.2
)
FetchContent_MakeAvailable(spdlog_setup)
The second FetchContent fails because it cannot find the spdlog lib.
If I look at the CMakeLists of that depenency I see:
if (EXISTS ${CMAKE_SOURCE_DIR}/deps/spdlog/CMakeLists.txt)
add_subdirectory(deps/spdlog)
else()
# allow usage of installed dependency
find_package(spdlog ${SPDLOG_MIN_VERSION} REQUIRED)
add_library(${PROJECT_NAME}_spdlog INTERFACE IMPORTED)
endif()
I fails in the else statement. How can I fix this?
I found that OVERRIDE_FIND_PACKAGE does the job. If specifying this for spdlog, cmake uses this target in the cmake file of the spdlog_setup project.

Using third-party libraries with CMake

I want anyone who cloned the repository can build it immediately, and don't need to install the dependencies.
Therefore, I found several ways:
Use git submodule and add_subdirectory.
Use find_package to find the built libraries and the headers.
The first one takes much time to build, so I think the second might be better. To make people be able to build the project instantly, I put the the files in the project, but it saied it doesn't know the linker language. What's this? And how to solve?
Direstories:
Project Root
lib
SDL2
(generated files when install)
include
(headers)
src
(sources)
CMakeLists.txt
CMakeLists.txt:
# ...
list(APPEND CMAKE_PREFIX_PATH lib)
find_package(SDL2)
# ...

Cannot create SDL2_ttf library correctly using local SDL2 repository using cmake

I am making a project that has a deps folder with all its dependencies, currently it includes a git clone of SDL2 and SDL2_ttf. I'd like to get this project to work without having to install SDL2 or SDL2_ttf separately using home brew for example. My current approach is doing the follow, however, it seems like SDL2_ttf is failing to find the directory of SDL2, how can I fix it?
CMakelists.txt
cmake_minimum_required(VERSION 3.19)
project(app)
add_executable(app MACOSX_BUNDLE src/main.cpp)
# SDL2
add_subdirectory(deps/SDL2)
add_dependencies(app SDL2)
# SDL2_ttf
add_subdirectory(deps/SDL2_ttf)
add_dependencies(app SDL2_ttf)
Error message
CMake Error at deps/SDL2_ttf/CMakeLists.txt:12 (find_package):
By not providing "FindSDL2.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "SDL2", but
CMake did not find one.
Could not find a package configuration file provided by "SDL2" with any of
the following names:
SDL2Config.cmake
sdl2-config.cmake
Add the installation prefix of "SDL2" to CMAKE_PREFIX_PATH or set
"SDL2_DIR" to a directory containing one of the above files. If "SDL2"
provides a separate development package or SDK, be sure it has been
installed.
P.S. I tried doing SET(SDL2_DIR deps/SDL2) in my CMakeLists.txt as suggested in the error but that doesn't seem to be working, I get the same error message.
SDL2 does contain SDL2Config.cmake, I'm using the latest master of these repos:
https://github.com/libsdl-org/SDL
https://github.com/libsdl-org/SDL_ttf

Minimal protobuf c++ installation for CMake use

I'm trying to use the CMake functions protobuf_generate in one of my projects. However, I'd like to the build as lightweight as possible. If at all possible I'd like to do this without having to compile protobuf's source code, or install some package on my (windows) machine.
Is there a way to just download binaries and headers and tell CMake: "Here is everything you need", so I can swap new proto versions whenever I feel like?
# CMakeList.txt : Top-level CMake project file, do global configuration
# and include sub-projects here.
#
cmake_minimum_required (VERSION 3.5)
project ("CMakeProject1")
set(TARGET_NAME CMakeProject1)
SET(PROTOBUF_INCLUDE_DIR "/path/to/include")
SET(PROTOBUF_LIBRARY "/path/to/libprotobuf.lib")
SET(PROTOBUF_PROTOC_EXECUTABLE "/path/to/protoc.exe")
find_package(Protobuf)
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS path/to/some_object.proto)
add_library(${TARGET_NAME} STATIC ${PROTO_SRCS} ${PROTO_HDRS})

How to point cmake at specific directory for library?

I have a CMake project where I am using a library and now I want to test my code with a different version of that library. I can set INCLUDE_DIRECTORIES (and possibly later also linking) in the below example. But because I only want to do this temporarily, I'd like to manually set this path with ccmake/cmake-gui.
How do I do this?
project(min_example)
cmake_minimum_required(VERSION 2.8)
find_package(OpenCV REQUIRED)
# Without the following line please:
INCLUDE_DIRECTORIES("/home/me/src/opencv/install/include")
add_executable(min_example main.cpp)
target_link_libraries(min_example ${OpenCV_LIBS})
This should be possible by setting the CMAKE_PREFIX_PATH variable upon configuring your build. In your project directory generate a test_build directory and run:
mkdir test_build
cd test_build
cmake -DCMAKE_PREFIX_PATH=/home/me/src/opencv/install ..
Setting the CMAKE_PREFIX_PATH variable will make the find_package(OpenCV REQUIRED) command pick your OpenCV installation in /home/me/src/opencv and set the OpenCV_LIBS and OpenCV_INCLUDE_DIR variables accordingly.
Alternatively you can edit a CMakeCache.txt file of an existing build directory with the CMake GUI editor and add the CMAKE_PREFIX_PATH definition there. You have to re-configure your project then.
Using config in find_package will restrict search path to OpenCV_DIR. This will use the cmake config that opencv generates at build time to setup paths to include and libs
set(OpenCV_DIR "<cusompath>" CACHE PATH '' ${SHOULD_FORCE_CACHE})
find_package(OpenCV REQUIRED CONFIG)