/
InstallHeaders.cmake
76 lines (67 loc) · 2.63 KB
/
InstallHeaders.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Installs headers for the named target
# Syntax:
# install_headers TARGET <target> DESTINATION <destination> ...
#
# NOEXCLUDE_STDAFX Also install the precompiled header file
# <target> The target whose sources are to be installed
# <destination> The root of the destination folder where files will be copied
#
# Additional options are passed after FILES to the cmake install command
include(DefaultValue)
function(install_headers)
set(options NOEXCLUDE_STDAFX)
set(oneValueArgs TARGET DESTINATION)
set(multiValueArgs)
cmake_parse_arguments(opt "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
string(TOLOWER LOWERNAME CMAKE_PROJECT_NAME)
default_value(opt_DESTINATION include/${LOWERNAME})
if(NOT opt_TARGET)
message(FATAL_ERROR "Cannot install files for a nonexistent target")
endif()
get_target_property(target_SRCS ${opt_TARGET} SOURCES)
foreach(src IN LISTS target_SRCS)
if(NOT ${opt_NOEXCLUDE_STDAFX} AND ${src} STREQUAL "stdafx.h")
continue()
endif()
get_filename_component(src_ext ${src} EXT)
if(NOT src_ext STREQUAL ".h")
continue()
endif()
# Need to make the path absolute. We first look in the source directory,
# then we look in the binary directory, but only if the path is relative.
if(NOT IS_ABSOLUTE ${src})
get_filename_component(src_src ${src} ABSOLUTE BASE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
get_filename_component(src_bin ${src} ABSOLUTE BASE_DIR ${CMAKE_CURRENT_BINARY_DIR})
if(EXISTS ${src_src})
set(src ${src_src})
elseif(EXISTS ${src_bin})
set(src ${src_bin})
else()
message(FATAL_ERROR "Could not find input header file ${src}")
endif()
endif()
# We find out what directory part the file is in, we want the shortest
# relative path on the assumption that the file will be most properly
# located in the directory whose relative address is the shortest.
file(RELATIVE_PATH src_rel ${CMAKE_CURRENT_SOURCE_DIR} ${src})
get_filename_component(src_rel ${src_rel} DIRECTORY)
if(src_rel)
string(LENGTH ${src_rel} src_rel_len)
set(actual_rel ${src_rel})
endif()
file(RELATIVE_PATH bin_rel ${CMAKE_CURRENT_BINARY_DIR} ${src})
get_filename_component(bin_rel ${bin_rel} DIRECTORY)
if(bin_rel)
string(LENGTH ${bin_rel} bin_rel_len)
if(src_rel AND bin_rel_len LESS src_rel_len)
set(actual_rel ${bin_rel})
endif()
endif()
get_filename_component(src_ext ${src} EXT)
install(
FILES ${src}
DESTINATION ${opt_DESTINATION}/${actual_rel}
${opt_UNPARSED_ARGUMENTS}
)
endforeach()
endfunction()