I am using the Unity testing library in my project. Ideally, I would like to automatically download the source code from GitHub and put it in the external/ directory, build the library once, regardless of how many different CMake configurations I have (one Debug and one Release CMake configuration), and link the library with my application.
I have tried to use FetchContent, ExternalProject, and add_subdirectory but none of these seem to work quite right.
The issues I am currently facing right now are:
- The library is installed in the _deps/ subdirectory of my build/ directory. I want it to be downloaded to a directory called external/ in the project root.
- "cmake --build ." builds the library every time. I just want the library to be built once.
- "cmake --install ." installs my application and the Unity library. I only want install to install my application when this command is run. The library would be installed in lib/ and include/ before my application is built.
This is my project structure:
project/ <-- Project root
|-- bin/ <-- Application executable
|-- build/ <-- CMake build files
| |-- _deps/ <-- Where Unity is built
|-- doc/ <-- Documentation from Doxygen
|-- include/ <-- Unity header files
|-- lib/ <-- Unity library file
|-- module/ <-- Application source code
|-- CMakeLists.txt <-- CMake configuration file
This is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.15)
project(Project VERSION 1.0.0)
set(CMAKE_INSTALL_PREFIX ${PROJECT_SOURCE_DIR})
include_directories(${PROJECT_SOURCE_DIR}/include)
add_subdirectory(module)
# Add Unity testing framework from GitHub
include(FetchContent)
FetchContent_Declare(
unity
GIT_REPOSITORY https://github.com/ThrowTheSwitch/Unity.git
GIT_TAG v2.5.1
)
FetchContent_MakeAvailable(unity)
FetchContent_GetProperties(unity)
if (NOT unity_POPULATED)
FetchContent_Populate(unity)
add_subdirectory(${unity_SOURCE_DIR} ${unity_BINARY_DIR})
endif()
enable_testing()
add_subdirectory(tests)
I really have no idea how to accomplish this. I looked at this other question and this link but it didn't seem to do everything I wanted it to do. Any help is appreciated.