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

[TODO/Planning] Better CMake support #40

Open
sacceus opened this issue Jan 28, 2021 · 0 comments
Open

[TODO/Planning] Better CMake support #40

sacceus opened this issue Jan 28, 2021 · 0 comments

Comments

@sacceus
Copy link

sacceus commented Jan 28, 2021

It would be nice if the library could be downloaded, configured and installed as dependency for user's own projects in more automated way. The current CMakeLists.txt does not export and install package that would be easily consumed by other projects.

I had a few tries at it, but without success so far.

set(PACKAGE_PUBLISHER   "chronoxor")
set(PACKAGE_REPOSITORY  "CppServer")
set(PACKAGE_NAME        "${PACKAGE_PUBLISHER}_${PACKAGE_REPOSITORY}")
set(PACKAGE_PROJECT     "${PACKAGE_NAME}_project")
set(PACKAGE_VERSION     1.0.0)

FetchContent_Declare(${PACKAGE_PROJECT}
    VERSION           ${PACKAGE_VERSION}
    GIT_REPOSITORY    https://github.com/chronoxor/CppServer
    GIT_TAG           50f2deb26342a8a4de819a30a35b450db51af45e # 1.0.0+ (Jan 10, 2021)
    )
FetchContent_GetProperties(${PACKAGE_PROJECT})
if(NOT ${PACKAGE_PROJECT}_POPULATED)
    message(STATUS "Downloading external dependency: ${PACKAGE_NAME}")
    FetchContent_Populate(${PACKAGE_PROJECT})

    set(CPPSERVER_MODULE TRUE)
    add_subdirectory(
        "${${PACKAGE_PROJECT}_SOURCE_DIR}"
        "${${PACKAGE_PROJECT}_BINARY_DIR}"
        )
endif()

The install and export commands are also rather complicated to get right. A library such as PackageProject.cmake could help with this. In theory if CppServer's CMakeLists.txt would be perfect, the other project CPM.cmake by TheLartians should work for configuration step also. A custom approach might suit this project better.
https://github.com/TheLartians/CPM.cmake
https://github.com/TheLartians/PackageProject.cmake

# PackageProject.cmake will be used to make our target installable.
CPMAddPackage(
    NAME PackageProject.cmake
    GITHUB_REPOSITORY TheLartians/PackageProject.cmake
    VERSION 1.4
)
packageProject(                                                                               
    NAME ${REF_LIBRARY}                                                                       
    VERSION ${PACKAGE_VERSION}                                                                
    NAMESPACE ${REF_LIBRARY}                                                                  
    BINARY_DIR ${${PACKAGE_PROJECT}_BINARY_DIR}/build                                         
    INCLUDE_DIR ${${PACKAGE_PROJECT}_SOURCE_DIR}/include                                      
    INCLUDE_DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PACKAGE_PUBLISHER}/${PACKAGE_REPOSITORY}                                                 
    ) 

Custom install should have some commands like following

install(
    TARGETS
        ${INSTALL_TARGETS}
    EXPORT
        ${PROJECT_NAME}Targets
    ...
    )
install(
    DIRECTORY
        "${CMAKE_CURRENT_LIST_DIR}/include/"
    DESTINATION
        "${CMAKE_INSTALL_INCLUDEDIR}
    ...
    )

Exporting depends on whether using normal Targets or Component approach for project.

export(
    TARGETS
        ${INSTALL_TARGETS}
    NAMESPACE
        ${PROJECT_NAME}::
    FILE
        ${CMAKE_CURRENT_BINARY_DIR}/${TARGETS_EXPORT_NAME}.cmake
    )
install(
    EXPORT
        ${TARGETS_EXPORT_NAME}
    DESTINATION
        ${CMAKE_INSTALL_CMAKEDIR}
    NAMESPACE
        ${PROJECT_NAME}::
    )

And somehow finally when that works, it is possible to use CppServer in own project in more straightforward way.

find_package(${PACKAGE_NAME}
    1.0.0
    REQUIRED
    CONFIG
    HINTS
        "C:/Codes/chronoxor/CppServer/install"
    )
target_link_libraries(${PROJECT_NAME} PRIVATE chronoxor::CppServer)

CppServer/install/lib/cmake/CppServer
CppServerTargets.cmake
CppServerConfig.cmake
CppServerConfigVersion.cmake
CppServer/install/include
And so on...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant