Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmake: add aliases so exported target names are available in tree. #5206

Closed
wants to merge 1 commit into from

Conversation

teknoman117
Copy link
Contributor

@teknoman117 teknoman117 commented Apr 9, 2020

One of the common patterns in CMake 3.x is the "superbuild" pattern, where the source trees of a dependencies are nested under a master project. In order for the projects to be modular (be agnostic to whether or not the dependencies are nested or not), the in-tree build process needs to generate the same targets as finding the dependencies out of tree. For example:

cmake_minimum_required(VERSION 3.14)
project(curl-example VERSION 0.0.1 LANGUAGES CXX)

option(USE_SYSTEM_CURL "Use the system cURL" OFF)

if (USE_SYSTEM_CURL)
    find_package(CURL REQUIRED)
else ()
    add_subdirectory(${PROJECT_SOURCE_DIR}/dependencies/curl)
endif()

add_executable(example ${PROJECT_SOURCE_DIR}/src/example.cpp)
target_link_libraries(example PRIVATE CURL::libcurl)

Using find_package to find cURL will create the CURL::libcurl target, but adding the source code directly only adds libcurl. Aliasing libcurl to CURL::libcurl lets you keep the target_link_libraries line the same regardless of the source of the dependency.

lib/CMakeLists.txt Show resolved Hide resolved
@bagder bagder closed this in 7fa1578 Apr 13, 2020
@bagder
Copy link
Member

bagder commented Apr 13, 2020

Thanks, both of you!

@teknoman117 teknoman117 deleted the cmake-add-alias branch April 13, 2020 22:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

cmake: add library and executable aliases to support superbuild pattern.
3 participants