forked from rraju1/cmake-buildscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CopyTarget.cmake
145 lines (124 loc) · 7.15 KB
/
CopyTarget.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# Provides the copy_target() function, which is used to make duplicate MPI and OpenMP versions of targets.
# copies the value of a property from one target to another. Does nothing if the property is not defined on the SOURCE.
function(copy_property PROPERTY SOURCE DESTINATION)
get_property(PROP_IS_DEFINED TARGET ${SOURCE} PROPERTY ${PROPERTY} SET)
if(PROP_IS_DEFINED)
get_property(PROP_VALUE TARGET ${SOURCE} PROPERTY ${PROPERTY})
#message("Copying ${PROPERTY} with value ${PROP_VALUE}")
set_property(TARGET ${DESTINATION} PROPERTY ${PROPERTY} "${PROP_VALUE}")
endif()
endfunction(copy_property)
# Copies a target. Creates a new target with a different name and the same sources and (important) properties. Doesn't work on aliases or imported libraries.
# Allows switching out sources if needed.
# usage: copy_target(<target> <new name> [SWAP_SOURCES <source 1...> TO <replacement source 1...>])
function(copy_target SOURCE DESTINATION)
#message("Copying target ${SOURCE} -> ${DESTINATION} --------------------------------------")
# parse arguments
# --------------------------------------------------------------------
cmake_parse_arguments(COPY_TARGET "" "" "LANGUAGES;SWAP_SOURCES;TO" ${ARGN})
# set up source list
# --------------------------------------------------------------------
get_property(TARGET_SOURCES TARGET ${SOURCE} PROPERTY SOURCES)
if(NOT "${COPY_TARGET_SWAP_SOURCES}" STREQUAL "")
list(REMOVE_ITEM TARGET_SOURCES ${COPY_TARGET_SWAP_SOURCES})
endif()
if(NOT "${COPY_TARGET_TO}" STREQUAL "")
list(APPEND TARGET_SOURCES ${COPY_TARGET_TO})
endif()
# create target according to type
# --------------------------------------------------------------------
get_property(TARGET_TYPE TARGET ${SOURCE} PROPERTY TYPE)
if("${TARGET_TYPE}" STREQUAL "SHARED_LIBRARY")
add_library(${DESTINATION} SHARED ${TARGET_SOURCES})
elseif("${TARGET_TYPE}" STREQUAL "STATIC_LIBRARY")
add_library(${DESTINATION} STATIC ${TARGET_SOURCES})
elseif("${TARGET_TYPE}" STREQUAL "MODULE_LIBRARY")
add_library(${DESTINATION} MODULE ${TARGET_SOURCES})
elseif("${TARGET_TYPE}" STREQUAL "OBJECT_LIBRARY")
add_library(${DESTINATION} OBJECT ${TARGET_SOURCES})
elseif("${TARGET_TYPE}" STREQUAL "INTERFACE_LIBRARY")
add_library(${DESTINATION} INTERFACE)
elseif("${TARGET_TYPE}" STREQUAL "EXECUTABLE")
add_executable(${DESTINATION} ${TARGET_SOURCES})
else()
message(FATAL_ERROR "copy_target(): cannot copy target ${SOURCE}: unknown target type ${TARGET_TYPE}")
endif()
# copy properties (feel free to add more if some are missing from this list)
# this *should* be every target property that isn't obscure (ex: only used for QT support) or deprecated
# --------------------------------------------------------------------
copy_property(BUILD_WITH_INSTALL_RPATH ${SOURCE} ${DESTINATION})
copy_property(C_EXTENSIONS ${SOURCE} ${DESTINATION})
copy_property(C_STANDARD ${SOURCE} ${DESTINATION})
copy_property(COMPILE_DEFINITIONS ${SOURCE} ${DESTINATION})
copy_property(COMPILE_FEATURES ${SOURCE} ${DESTINATION})
copy_property(COMPILE_FLAGS ${SOURCE} ${DESTINATION})
copy_property(COMPILE_OPTIONS ${SOURCE} ${DESTINATION})
copy_property(CXX_EXTENSIONS ${SOURCE} ${DESTINATION})
copy_property(CXX_STANDARD ${SOURCE} ${DESTINATION})
copy_property(DEBUG_POSTFIX ${SOURCE} ${DESTINATION})
copy_property(DEFINE_SYMBOL ${SOURCE} ${DESTINATION})
copy_property(ENABLE_EXPORTS ${SOURCE} ${DESTINATION})
copy_property(EXCLUDE_FROM_ALL ${SOURCE} ${DESTINATION})
copy_property(FRAMEWORK ${SOURCE} ${DESTINATION})
copy_property(FOLDER ${SOURCE} ${DESTINATION})
copy_property(Fortran_FORMAT ${SOURCE} ${DESTINATION})
copy_property(Fortran_MODULE_DIRECTORY ${SOURCE} ${DESTINATION})
copy_property(GNUtoMS ${SOURCE} ${DESTINATION})
copy_property(IMPORT_PREFIX ${SOURCE} ${DESTINATION})
copy_property(INCLUDE_DIRECTORIES ${SOURCE} ${DESTINATION})
copy_property(INSTALL_NAME_DIR ${SOURCE} ${DESTINATION})
copy_property(INSTALL_RPATH ${SOURCE} ${DESTINATION})
copy_property(INSTALL_RPATH_USE_LINK_PATH ${SOURCE} ${DESTINATION})
copy_property(INTERFACE_COMPILE_DEFINITIONS ${SOURCE} ${DESTINATION})
copy_property(INTERFACE_COMPILE_FEATURES ${SOURCE} ${DESTINATION})
copy_property(INTERFACE_COMPILE_OPTIONS ${SOURCE} ${DESTINATION})
copy_property(INTERFACE_INCLUDE_DIRECTORIES ${SOURCE} ${DESTINATION})
copy_property(INTERFACE_LINK_LIBRARIES ${SOURCE} ${DESTINATION})
copy_property(INTERFACE_POSITION_INDEPENDENT_CODE ${SOURCE} ${DESTINATION})
copy_property(INTERFACE_SOURCES ${SOURCE} ${DESTINATION})
copy_property(INTERFACE_SYSTEM_INCLUDE_DIRECTORIES ${SOURCE} ${DESTINATION})
copy_property(INTERPROCEDURAL_OPTIMIZATION ${SOURCE} ${DESTINATION})
copy_property(LABELS ${SOURCE} ${DESTINATION})
copy_property(LIBRARY_OUTPUT_DIRECTORY ${SOURCE} ${DESTINATION})
copy_property(LIBRARY_OUTPUT_NAME ${SOURCE} ${DESTINATION})
copy_property(LINKER_LANGUAGE ${SOURCE} ${DESTINATION})
copy_property(LINK_FLAGS ${SOURCE} ${DESTINATION})
copy_property(LINK_LIBRARIES ${SOURCE} ${DESTINATION})
copy_property(MACOSX_BUNDLE_INFO_PLIST ${SOURCE} ${DESTINATION})
copy_property(MACOSX_BUNDLE ${SOURCE} ${DESTINATION})
copy_property(MACOSX_FRAMEWORK_INFO_PLIST ${SOURCE} ${DESTINATION})
copy_property(MACOSX_RPATH ${SOURCE} ${DESTINATION})
copy_property(NO_SONAME ${SOURCE} ${DESTINATION})
copy_property(NO_SYSTEM_FROM_IMPORTED ${SOURCE} ${DESTINATION})
copy_property(OSX_ARCHITECTURES ${SOURCE} ${DESTINATION})
copy_property(OUTPUT_NAME ${SOURCE} ${DESTINATION})
copy_property(POSITION_INDEPENDENT_CODE ${SOURCE} ${DESTINATION})
copy_property(PREFIX ${SOURCE} ${DESTINATION})
copy_property(PRIVATE_HEADER ${SOURCE} ${DESTINATION})
copy_property(PROJECT_LABEL ${SOURCE} ${DESTINATION})
copy_property(PUBLIC_HEADER ${SOURCE} ${DESTINATION})
copy_property(RESOURCE ${SOURCE} ${DESTINATION})
copy_property(RUNTIME_OUTPUT_DIRECTORY ${SOURCE} ${DESTINATION})
copy_property(RUNTIME_OUTPUT_NAME ${SOURCE} ${DESTINATION})
copy_property(SKIP_BUILD_RPATH ${SOURCE} ${DESTINATION})
copy_property(SOVERSION ${SOURCE} ${DESTINATION})
copy_property(STATIC_LIBRARY_FLAGS ${SOURCE} ${DESTINATION})
copy_property(SUFFIX ${SOURCE} ${DESTINATION})
copy_property(VERSION ${SOURCE} ${DESTINATION})
copy_property(VISIBILITY_INLINES_HIDDEN ${SOURCE} ${DESTINATION})
copy_property(VS_GLOBAL_KEYWORD ${SOURCE} ${DESTINATION})
copy_property(VS_GLOBAL_PROJECT_TYPES ${SOURCE} ${DESTINATION})
copy_property(VS_GLOBAL_ROOTNAMESPACE ${SOURCE} ${DESTINATION})
copy_property(VS_KEYWORD ${SOURCE} ${DESTINATION})
copy_property(VS_WINRT_EXTENSIONS ${SOURCE} ${DESTINATION})
copy_property(WIN32_EXECUTABLE ${SOURCE} ${DESTINATION})
endfunction(copy_target)
# useful function when duplicating targets:
function(remove_link_libraries TARGET) # ARGN: LIBRARIES...
get_property(TARGET_LINK_LIBRARIES TARGET ${TARGET} PROPERTY LINK_LIBRARIES)
get_property(TARGET_INTERFACE_LINK_LIBRARIES TARGET ${TARGET} PROPERTY INTERFACE_LINK_LIBRARIES)
list(REMOVE_ITEM TARGET_LINK_LIBRARIES ${ARGN})
list(REMOVE_ITEM TARGET_INTERFACE_LINK_LIBRARIES ${ARGN})
set_property(TARGET ${TARGET} PROPERTY LINK_LIBRARIES ${TARGET_LINK_LIBRARIES})
set_property(TARGET ${TARGET} PROPERTY INTERFACE_LINK_LIBRARIES ${TARGET_INTERFACE_LINK_LIBRARIES})
endfunction(remove_link_libraries)