[CMake] Convenience version of CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE for INTERFACE_SOURCES

Robert Dailey rcdailey.lists at gmail.com
Sun Jul 10 14:23:46 EDT 2016


I am exporting/installing a header-only INTERFACE library and I run
into difficulty with target_sources() because I can't use relative
paths to the files. So I do this:



set(project_name fubar)

set(source_files
    myfile.h
)

# Include dir for installed targets
set(INCLUDE_DIR include/${project_name}-${upstream_version})

foreach(source_file ${source_files})
    get_filename_component(abs_source_file ${source_file} ABSOLUTE)
    list(APPEND absolute_source_files
        $<BUILD_INTERFACE:${abs_source_file}>
        $<INSTALL_INTERFACE:${INCLUDE_DIR}/${source_file}>
    )
endforeach()

add_library(${project_name} INTERFACE)
target_sources(${project_name} INTERFACE ${absolute_source_files})



Without the separate generator expressions for BUILD_INTERFACE and
INSTALL_INTERFACE, CMake complains that absolute paths are not inside
INSTALL_PREFIX in my install() command later:



CMake Error in CMakeLists.txt:
Target "better-enums" INTERFACE_SOURCES property contains path:

"C:/code/fubar/myfile.h"

which is prefixed in the source directory.



Corresponding install() command is:


install(TARGETS ${project_name} EXPORT ${project_name}-targets
    INCLUDES DESTINATION ${INCLUDE_DIR}
)

export(EXPORT ${project_name}-targets
    FILE ${CMAKE_CURRENT_BINARY_DIR}/${project_name}-targets.cmake
)

install(EXPORT ${project_name}-targets
    FILE ${project_name}-targets.cmake
    DESTINATION ${package_config_dir}
)

For INTERFACE_INCLUDE_DIRECTORIES, this same problem exists except
CMake provides a convenience toggle called
INTERFACE_SYSTEM_INCLUDE_DIRECTORIES. Is there a similar convenience
for interface sources? Or maybe I'm going about this all the wrong
way?

Honestly the only reason interface sources are specified is because I
have a header-only library and I want that header to show up in only
MY targets during development so I can edit those header files through
my IDE. I do not expect the header files to appear in targets that
simply import my target as a dependency.

So it's possible I'm going about this the wrong way. Advice is greatly
appreciated.


More information about the CMake mailing list