CMAKE retain dir path for header includes from a header only built library

61 Views Asked by At

I'm trying to restructure my CMAKE project by placing all headers in a separate cmake header only project and add that using target_link_libraries to my sources cmake project.

Is it possible to retain folder path while including headers in cpp source file.

Headers in interface library should also be able to access other headers from different directory(within same cmake interface project) using header folder name.

expected

#include "A/A.hpp"
#include "AGen/AGen.hpp"
#include "json/json.hpp"

current working impl

#include "A.hpp"
#include "AGen.hpp"
#include "json.hpp"

my interface cmake configuration

    set(project_includes_internal
    ${CMAKE_CURRENT_LIST_DIR}/A
    ${CMAKE_CURRENT_LIST_DIR}/B
    ${CMAKE_CURRENT_LIST_DIR}/C
    )
    
    set(project_gen_includes
    ${CMAKE_CURRENT_BINARY_DIR}/include/AGen
    ${CMAKE_CURRENT_BINARY_DIR}/include/BGen
    ${CMAKE_CURRENT_BINARY_DIR}/include/CGen
    )
    
    set(project_dep_inludes
    ${CMAKE_HOME_DIRECTORY}/dep/nlohmann/json
    )
set(PROJECT_INCLUDES
        ${project_includes_internal}
        ${project_gen_includes}
        ${project_dep_inludes})

add_library(${PROJECT_NAME} INTERFACE)
target_include_directories(${PROJECT_NAME}
        INTERFACE
        $<BUILD_INTERFACE:${PROJECT_INCLUDES}>
        $<INSTALL_INTERFACE:include>
        )

my sources cmake config

add_executable(${PROJECT_NAME}-bin ${PROJECT_TARGET_SOURCES})
target_link_libraries(${PROJECT_NAME}-bin
        PUBLIC
        ${ACTIVATION_PROJECT_INCLUDES_NAME})

Also is it possible to copy all required include files in a build directory so those can be copied/tar published while installing on other machines.

0

There are 0 best solutions below