CMake doesn’t provide a dedicated way to install header files (except for mac). What I wanted to do was to install all headers of my project using the same directory structure as in the source tree. This isn’t as easy as it sounds. Assume you have a list of header files:
SET(HS folder/test.h folder/other/test2.h)
A simple call to INSTALL
doesn’t preserve the folder structure:
INSTALL(FILES ${HS} DESTINATION include)
This results in all files being directly under $prefix/include
.
To preserve the structure you can use this simple macro:
MACRO(INSTALL_HEADERS_WITH_DIRECTORY HEADER_LIST)
FOREACH(HEADER ${${HEADER_LIST}})
STRING(REGEX MATCH "(.\\\*)\\\[/\\\]" DIR ${HEADER})
INSTALL(FILES ${HEADER} DESTINATION include/${DIR})
ENDFOREACH(HEADER)
ENDMACRO(INSTALL_HEADERS_WITH_DIRECTORY)
INSTALL_HEADERS_WITH_DIRECTORY(HS)