CPack Subprojects and Public Header

518 Views Asked by At

I have a question about CPack. I have a project test_lib_1 that depends on 2 other project test_lib_2 and test_lib_3. Both test_lib_2 and 3 define their own CPACK rules, defining PUBLIC_HEADER to be packed. test_lib_1 has the same rule, packaging binaries and some public_headers. using CPack I get a package with the binaries for test_lib_1 and its public headers, the binaries (dynamic libs) for the other two subprojects but also the public headers of the two subprojects: i.e. test_lib_2 and test_lib_3.

here is my CMakeLists.txt for test_lib_1 (for the other two, the file is similar without the inclusion of the two subprojects).

cmake_minimum_required(VERSION 3.10)

set(PROJECT_NAME "test_lib_1")
set(PROJECT_VERSION 1.0.0)
set(CMAKE_INSTALL_PREFIX "${CMAKE_SOURCE_DIR}/opt/company")

project(${PROJECT_NAME} VERSION ${PROJECT_VERSION} DESCRIPTION "test_lib_1 description")

file(GLOB PROJECT_INTERFACE "include/*.h")
file(GLOB PROJECT_HEADERS "src/*.h")
file(GLOB PROJECT_SRCS "src/*.cpp")

add_subdirectory(test_lib_2)
add_subdirectory(test_lib_3)

add_library(${PROJECT_NAME} SHARED ${PROJECT_INTERFACE} ${PROJECT_HEADERS} ${PROJECT_SRCS})

target_include_directories(${PROJECT_NAME} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")

target_link_libraries(${PROJECT_NAME} test_lib_2)
target_link_libraries(${PROJECT_NAME} test_lib_3)

set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_VERSION})

set_target_properties(${PROJECT_NAME} PROPERTIES PUBLIC_HEADER "${PROJECT_INTERFACE}")

install(TARGETS ${PROJECT_NAME} 
    PUBLIC_HEADER DESTINATION "include"
    ARCHIVE DESTINATION "lib"
    RUNTIME DESTINATION "bin"
    LIBRARY DESTINATION "bin"
    )

set(CPACK_PACKAGE_CONTACT "Contact")

if(WIN32)
    set(CPACK_GENERATOR ZIP)
else()
    set(CPACK_GENERATOR "DEB" )
    set(CPACK_SET_DESTDIR TRUE )
endif()

include(CPack)

My current output is: bin -> test_lib_1.dll; test_lib_2.dll; test_lib_3.dll include -> test_lib_1.h; test_lib_2.h; test_lib_3.h ...

expected would be: bin -> test_lib_1.dll; test_lib_2.dll; test_lib_3.dll include -> test_lib_1.h;

without the test_lib_2 and test_lib_3 public header.

any idea?

1

There are 1 best solutions below

0
On

I just found a possible solution by myself. The idea is to enclose the install + cpack part with a

get_directory_property(hasParent PARENT_DIRECTORY)
if(NOT hasParent)
...
endi()

In this way, if the current project is a subproject, it doesn't fill the variables for install and cpack and not participates in install and cpack instructions.

Hope it can help others