-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathFindCTargets.cmake
219 lines (187 loc) · 7.67 KB
/
FindCTargets.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# -*- coding: utf-8 -*-
# ----------------------------------------------------------------------
# Copyright © 2015, RedJack, LLC.
# All rights reserved.
#
# Please see the COPYING file in this distribution for license details.
# ----------------------------------------------------------------------
#-----------------------------------------------------------------------
# Configuration options that control all of the below
set(ENABLE_SHARED YES CACHE BOOL "Whether to build a shared library")
set(ENABLE_SHARED_EXECUTABLES YES CACHE BOOL
"Whether to link executables using shared libraries")
set(ENABLE_STATIC YES CACHE BOOL "Whether to build a static library")
#-----------------------------------------------------------------------
# Library, with options to build both shared and static versions
function(target_add_shared_libraries TARGET_NAME LIBRARIES LOCAL_LIBRARIES)
foreach(lib ${LIBRARIES})
string(REPLACE "-" "_" lib ${lib})
string(TOUPPER ${lib} upperlib)
target_link_libraries(
${TARGET_NAME}
${${upperlib}_LDFLAGS}
)
endforeach(lib)
foreach(lib ${LOCAL_LIBRARIES})
target_link_libraries(${TARGET_NAME} ${lib}-shared)
endforeach(lib)
endfunction(target_add_shared_libraries)
function(target_add_static_libraries TARGET_NAME LIBRARIES LOCAL_LIBRARIES)
foreach(lib ${LIBRARIES})
string(REPLACE "-" "_" lib ${lib})
string(TOUPPER ${lib} upperlib)
target_link_libraries(
${TARGET_NAME}
${${upperlib}_STATIC_LDFLAGS}
)
endforeach(lib)
foreach(lib ${LOCAL_LIBRARIES})
target_link_libraries(${TARGET_NAME} ${lib}-static)
endforeach(lib)
endfunction(target_add_static_libraries)
set_property(GLOBAL PROPERTY ALL_LOCAL_LIBRARIES "")
function(add_c_library __TARGET_NAME)
set(options)
set(one_args OUTPUT_NAME PKGCONFIG_NAME VERSION_INFO)
set(multi_args LIBRARIES LOCAL_LIBRARIES SOURCES)
cmake_parse_arguments(_ "${options}" "${one_args}" "${multi_args}" ${ARGN})
if (__VERSION_INFO MATCHES "^([0-9]+):([0-9]+):([0-9]+)(-dev)?$")
set(__VERSION_CURRENT "${CMAKE_MATCH_1}")
set(__VERSION_REVISION "${CMAKE_MATCH_2}")
set(__VERSION_AGE "${CMAKE_MATCH_3}")
else (__VERSION_INFO MATCHES "^([0-9]+):([0-9]+):([0-9]+)(-dev)?$")
message(FATAL_ERROR "Invalid library version info: ${__VERSION_INFO}")
endif (__VERSION_INFO MATCHES "^([0-9]+):([0-9]+):([0-9]+)(-dev)?$")
# Mimic libtool's behavior in calculating SONAME and VERSION from
# version-info.
# http://git.savannah.gnu.org/cgit/libtool.git/tree/build-aux/ltmain.in?id=722b6af0fad19b3d9f21924ae5aa6dfae5957378#n7042
math(EXPR __SOVERSION "${__VERSION_CURRENT} - ${__VERSION_AGE}")
set(__VERSION "${__SOVERSION}.${__VERSION_AGE}.${__VERSION_REVISION}")
get_property(ALL_LOCAL_LIBRARIES GLOBAL PROPERTY ALL_LOCAL_LIBRARIES)
list(APPEND ALL_LOCAL_LIBRARIES ${__TARGET_NAME})
set_property(GLOBAL PROPERTY ALL_LOCAL_LIBRARIES "${ALL_LOCAL_LIBRARIES}")
if (ENABLE_SHARED OR ENABLE_SHARED_EXECUTABLES)
add_library(${__TARGET_NAME}-shared SHARED ${__SOURCES})
set_target_properties(
${__TARGET_NAME}-shared PROPERTIES
OUTPUT_NAME ${__OUTPUT_NAME}
CLEAN_DIRECT_OUTPUT 1
VERSION ${__VERSION}
SOVERSION ${__SOVERSION}
)
if (CMAKE_VERSION VERSION_GREATER "2.8.11")
target_include_directories(
${__TARGET_NAME}-shared PUBLIC
${CMAKE_SOURCE_DIR}/include
${CMAKE_BINARY_DIR}/include
)
else (CMAKE_VERSION VERSION_GREATER "2.8.11")
include_directories(
${CMAKE_SOURCE_DIR}/include
${CMAKE_BINARY_DIR}/include
)
endif (CMAKE_VERSION VERSION_GREATER "2.8.11")
target_add_shared_libraries(
${__TARGET_NAME}-shared
"${__LIBRARIES}"
"${__LOCAL_LIBRARIES}"
)
# We have to install the shared library if the user asked us to, or if
# the user asked us to link our programs with the shared library.
install(TARGETS ${__TARGET_NAME}-shared
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif (ENABLE_SHARED OR ENABLE_SHARED_EXECUTABLES)
if (ENABLE_STATIC OR NOT ENABLE_SHARED_EXECUTABLES)
add_library(${__TARGET_NAME}-static STATIC ${__SOURCES})
set_target_properties(
${__TARGET_NAME}-static PROPERTIES
OUTPUT_NAME ${__OUTPUT_NAME}
CLEAN_DIRECT_OUTPUT 1
)
if (CMAKE_VERSION VERSION_GREATER "2.8.11")
target_include_directories(
${__TARGET_NAME}-static PUBLIC
${CMAKE_SOURCE_DIR}/include
${CMAKE_BINARY_DIR}/include
)
else (CMAKE_VERSION VERSION_GREATER "2.8.11")
include_directories(
${CMAKE_SOURCE_DIR}/include
${CMAKE_BINARY_DIR}/include
)
endif (CMAKE_VERSION VERSION_GREATER "2.8.11")
target_add_static_libraries(
${__TARGET_NAME}-static
"${__LIBRARIES}"
"${__LOCAL_LIBRARIES}"
)
endif (ENABLE_STATIC OR NOT ENABLE_SHARED_EXECUTABLES)
if (ENABLE_STATIC)
# We DON'T have to install the static library if the user asked us to
# link our programs statically.
install(TARGETS ${__TARGET_NAME}-static
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif (ENABLE_STATIC)
set(prefix ${CMAKE_INSTALL_PREFIX})
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/${__PKGCONFIG_NAME}.pc.in
${CMAKE_CURRENT_BINARY_DIR}/${__PKGCONFIG_NAME}.pc
@ONLY
)
install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/${__PKGCONFIG_NAME}.pc
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
)
endfunction(add_c_library)
#-----------------------------------------------------------------------
# Executable
function(add_c_executable __TARGET_NAME)
set(options SKIP_INSTALL)
set(one_args OUTPUT_NAME)
set(multi_args LIBRARIES LOCAL_LIBRARIES SOURCES)
cmake_parse_arguments(_ "${options}" "${one_args}" "${multi_args}" ${ARGN})
add_executable(${__TARGET_NAME} ${__SOURCES})
if (CMAKE_VERSION VERSION_GREATER "2.8.11")
target_include_directories(
${__TARGET_NAME} PUBLIC
${CMAKE_SOURCE_DIR}/include
${CMAKE_BINARY_DIR}/include
)
else (CMAKE_VERSION VERSION_GREATER "2.8.11")
include_directories(
${CMAKE_SOURCE_DIR}/include
${CMAKE_BINARY_DIR}/include
)
endif (CMAKE_VERSION VERSION_GREATER "2.8.11")
if (ENABLE_SHARED_EXECUTABLES)
target_add_shared_libraries(
${__TARGET_NAME}
"${__LIBRARIES}"
"${__LOCAL_LIBRARIES}"
)
else (ENABLE_SHARED_EXECUTABLES)
target_add_static_libraries(
${__TARGET_NAME}
"${__LIBRARIES}"
"${__LOCAL_LIBRARIES}"
)
endif (ENABLE_SHARED_EXECUTABLES)
if (NOT __SKIP_INSTALL)
install(TARGETS ${__TARGET_NAME} RUNTIME DESTINATION bin)
endif (NOT __SKIP_INSTALL)
endfunction(add_c_executable)
#-----------------------------------------------------------------------
# Test case
pkgconfig_prereq(check OPTIONAL)
function(add_c_test TEST_NAME)
get_property(ALL_LOCAL_LIBRARIES GLOBAL PROPERTY ALL_LOCAL_LIBRARIES)
add_c_executable(
${TEST_NAME}
SKIP_INSTALL
OUTPUT_NAME ${TEST_NAME}
SOURCES ${TEST_NAME}.c
LIBRARIES check
LOCAL_LIBRARIES ${ALL_LOCAL_LIBRARIES}
)
add_test(${TEST_NAME} ${TEST_NAME})
endfunction(add_c_test)