5
5
# you may not use this file except in compliance with the License.
6
6
# You may obtain a copy of the License at
7
7
#
8
- # http ://www.apache.org/licenses/LICENSE-2.0
8
+ # https ://www.apache.org/licenses/LICENSE-2.0
9
9
#
10
10
# Unless required by applicable law or agreed to in writing, software
11
11
# distributed under the License is distributed on an "AS IS" BASIS,
15
15
#
16
16
17
17
include (CMakeParseArguments )
18
+ include (AbseilConfigureCopts )
19
+ include (AbseilInstallDirs )
18
20
19
21
# The IDE folder for Abseil that will be used if Abseil is included in a CMake
20
22
# project that sets
21
23
# set_property(GLOBAL PROPERTY USE_FOLDERS ON)
22
24
# For example, Visual Studio supports folders.
23
25
set (ABSL_IDE_FOLDER Abseil )
24
26
25
- #
26
- # create a library in the absl namespace
27
- #
28
- # parameters
29
- # SOURCES : sources files for the library
30
- # PUBLIC_LIBRARIES: targets and flags for linking phase
31
- # PRIVATE_COMPILE_FLAGS: compile flags for the library. Will not be exported.
32
- # EXPORT_NAME: export name for the absl:: target export
33
- # TARGET: target name
34
- #
35
- # create a target associated to <NAME>
36
- # libraries are installed under CMAKE_INSTALL_FULL_LIBDIR by default
37
- #
38
- function (absl_library )
39
- cmake_parse_arguments (ABSL_LIB
40
- "DISABLE_INSTALL" # keep that in case we want to support installation one day
41
- "TARGET;EXPORT_NAME"
42
- "SOURCES;PUBLIC_LIBRARIES;PRIVATE_COMPILE_FLAGS"
43
- ${ARGN}
44
- )
45
-
46
- set (_NAME ${ABSL_LIB_TARGET} )
47
- string (TOUPPER ${_NAME} _UPPER_NAME )
48
-
49
- add_library (${_NAME} STATIC ${ABSL_LIB_SOURCES} )
50
-
51
- target_compile_options (${_NAME} PRIVATE ${ABSL_LIB_PRIVATE_COMPILE_FLAGS} )
52
- target_link_libraries (${_NAME} PUBLIC ${ABSL_LIB_PUBLIC_LIBRARIES} )
53
- target_include_directories (${_NAME}
54
- PUBLIC ${ABSL_COMMON_INCLUDE_DIRS} ${ABSL_LIB_PUBLIC_INCLUDE_DIRS}
55
- PRIVATE ${ABSL_LIB_PRIVATE_INCLUDE_DIRS}
56
- )
57
- # Add all Abseil targets to a a folder in the IDE for organization.
58
- set_property (TARGET ${_NAME} PROPERTY FOLDER ${ABSL_IDE_FOLDER} )
59
-
60
- if (ABSL_LIB_EXPORT_NAME )
61
- add_library (absl::${ABSL_LIB_EXPORT_NAME} ALIAS ${_NAME} )
62
- endif ()
63
- endfunction ()
64
-
27
+ # absl_cc_library()
65
28
#
66
29
# CMake function to imitate Bazel's cc_library rule.
67
30
#
@@ -73,18 +36,18 @@ endfunction()
73
36
# COPTS: List of private compile options
74
37
# DEFINES: List of public defines
75
38
# LINKOPTS: List of link options
76
- # PUBLIC: Add this so that this library will be exported under absl:: (see Note).
39
+ # PUBLIC: Add this so that this library will be exported under absl::
40
+ # Also in IDE, target will appear in Abseil folder while non PUBLIC will be in Abseil/internal.
77
41
# TESTONLY: When added, this target will only be built if user passes -DABSL_RUN_TESTS=ON to CMake.
78
42
#
79
43
# Note:
80
- #
81
- # By default, absl_cc_library will always create a library named absl_internal_${NAME},
82
- # which means other targets can only depend this library as absl_internal_${NAME}, not ${NAME}.
44
+ # By default, absl_cc_library will always create a library named absl_${NAME},
45
+ # and alias target absl::${NAME}. The absl:: form should always be used.
83
46
# This is to reduce namespace pollution.
84
47
#
85
48
# absl_cc_library(
86
49
# NAME
87
- # awesome_lib
50
+ # awesome
88
51
# HDRS
89
52
# "a.h"
90
53
# SRCS
@@ -96,23 +59,19 @@ endfunction()
96
59
# SRCS
97
60
# "b.cc"
98
61
# DEPS
99
- # absl_internal_awesome_lib # not "awesome_lib"!
62
+ # absl::awesome # not "awesome" !
63
+ # PUBLIC
100
64
# )
101
65
#
102
- # If PUBLIC is set, absl_cc_library will instead create a target named
103
- # absl_${NAME} and an alias absl::${NAME}.
104
- #
105
66
# absl_cc_library(
106
67
# NAME
107
68
# main_lib
108
69
# ...
109
- # PUBLIC
70
+ # DEPS
71
+ # absl::fantastic_lib
110
72
# )
111
73
#
112
- # User can then use the library as absl::main_lib (although absl_main_lib is defined too).
113
- #
114
74
# TODO: Implement "ALWAYSLINK"
115
-
116
75
function (absl_cc_library )
117
76
cmake_parse_arguments (ABSL_CC_LIB
118
77
"DISABLE_INSTALL;PUBLIC;TESTONLY"
@@ -121,15 +80,24 @@ function(absl_cc_library)
121
80
${ARGN}
122
81
)
123
82
124
- if (NOT ABSL_CC_LIB_TESTONLY OR ABSL_RUN_TESTS )
125
- if ( ABSL_CC_LIB_PUBLIC )
126
- set (_NAME "absl_ ${ABSL_CC_LIB_NAME} " )
83
+ if (NOT ABSL_CC_LIB_TESTONLY OR ABSL_RUN_TESTS )
84
+ if ( ABSL_ENABLE_INSTALL )
85
+ set (_NAME "${ABSL_CC_LIB_NAME} " )
127
86
else ()
128
- set (_NAME "absl_internal_ ${ABSL_CC_LIB_NAME} " )
87
+ set (_NAME "absl_ ${ABSL_CC_LIB_NAME} " )
129
88
endif ()
130
89
131
90
# Check if this is a header-only library
132
- if ("${ABSL_CC_LIB_SRCS} " STREQUAL "" )
91
+ # Note that as of February 2019, many popular OS's (for example, Ubuntu
92
+ # 16.04 LTS) only come with cmake 3.5 by default. For this reason, we can't
93
+ # use list(FILTER...)
94
+ set (ABSL_CC_SRCS "${ABSL_CC_LIB_SRCS} " )
95
+ foreach (src_file IN LISTS ABSL_CC_SRCS )
96
+ if (${src_file} MATCHES ".*\\ .(h|inc)" )
97
+ list (REMOVE_ITEM ABSL_CC_SRCS "${src_file} " )
98
+ endif ()
99
+ endforeach ()
100
+ if ("${ABSL_CC_SRCS} " STREQUAL "" )
133
101
set (ABSL_CC_LIB_IS_INTERFACE 1 )
134
102
else ()
135
103
set (ABSL_CC_LIB_IS_INTERFACE 0 )
@@ -139,134 +107,149 @@ function(absl_cc_library)
139
107
add_library (${_NAME} STATIC "" )
140
108
target_sources (${_NAME} PRIVATE ${ABSL_CC_LIB_SRCS} ${ABSL_CC_LIB_HDRS} )
141
109
target_include_directories (${_NAME}
142
- PUBLIC ${ABSL_COMMON_INCLUDE_DIRS} )
110
+ PUBLIC
111
+ $< BUILD_INTERFACE:${ABSL_COMMON_INCLUDE_DIRS} >
112
+ $< INSTALL_INTERFACE:${ABSL_INSTALL_INCLUDEDIR} >
113
+ )
143
114
target_compile_options (${_NAME}
144
115
PRIVATE ${ABSL_CC_LIB_COPTS} )
145
116
target_link_libraries (${_NAME}
146
117
PUBLIC ${ABSL_CC_LIB_DEPS}
147
- PRIVATE ${ABSL_CC_LIB_LINKOPTS}
118
+ PRIVATE
119
+ ${ABSL_CC_LIB_LINKOPTS}
120
+ ${ABSL_DEFAULT_LINKOPTS}
148
121
)
149
122
target_compile_definitions (${_NAME} PUBLIC ${ABSL_CC_LIB_DEFINES} )
150
123
151
124
# Add all Abseil targets to a a folder in the IDE for organization.
152
- set_property (TARGET ${_NAME} PROPERTY FOLDER ${ABSL_IDE_FOLDER} )
125
+ if (ABSL_CC_LIB_PUBLIC )
126
+ set_property (TARGET ${_NAME} PROPERTY FOLDER ${ABSL_IDE_FOLDER} )
127
+ elseif (ABSL_CC_LIB_TESTONLY )
128
+ set_property (TARGET ${_NAME} PROPERTY FOLDER ${ABSL_IDE_FOLDER} /test )
129
+ else ()
130
+ set_property (TARGET ${_NAME} PROPERTY FOLDER ${ABSL_IDE_FOLDER} /internal )
131
+ endif ()
132
+
133
+ # INTERFACE libraries can't have the CXX_STANDARD property set
134
+ set_property (TARGET ${_NAME} PROPERTY CXX_STANDARD ${ABSL_CXX_STANDARD} )
135
+ set_property (TARGET ${_NAME} PROPERTY CXX_STANDARD_REQUIRED ON )
136
+
137
+ # When being installed, we lose the absl_ prefix. We want to put it back
138
+ # to have properly named lib files. This is a no-op when we are not being
139
+ # installed.
140
+ set_target_properties (${_NAME} PROPERTIES
141
+ OUTPUT_NAME "absl_${_NAME} "
142
+ )
153
143
else ()
154
144
# Generating header-only library
155
145
add_library (${_NAME} INTERFACE )
156
- target_include_directories (${_NAME} INTERFACE ${ABSL_COMMON_INCLUDE_DIRS} )
146
+ target_include_directories (${_NAME}
147
+ INTERFACE
148
+ $< BUILD_INTERFACE:${ABSL_COMMON_INCLUDE_DIRS} >
149
+ $< INSTALL_INTERFACE:${ABSL_INSTALL_INCLUDEDIR} >
150
+ )
157
151
target_link_libraries (${_NAME}
158
- INTERFACE ${ABSL_CC_LIB_DEPS} ${ABSL_CC_LIB_LINKOPTS}
152
+ INTERFACE
153
+ ${ABSL_CC_LIB_DEPS}
154
+ ${ABSL_CC_LIB_LINKOPTS}
155
+ ${ABSL_DEFAULT_LINKOPTS}
159
156
)
160
157
target_compile_definitions (${_NAME} INTERFACE ${ABSL_CC_LIB_DEFINES} )
161
158
endif ()
162
159
163
- if (ABSL_CC_LIB_PUBLIC )
164
- add_library (absl::${ABSL_CC_LIB_NAME} ALIAS ${_NAME} )
160
+ # TODO currently we don't install googletest alongside abseil sources, so
161
+ # installed abseil can't be tested.
162
+ if (NOT ABSL_CC_LIB_TESTONLY AND ABSL_ENABLE_INSTALL )
163
+ install (TARGETS ${_NAME} EXPORT ${PROJECT_NAME} Targets
164
+ RUNTIME DESTINATION ${ABSL_INSTALL_BINDIR}
165
+ LIBRARY DESTINATION ${ABSL_INSTALL_LIBDIR}
166
+ ARCHIVE DESTINATION ${ABSL_INSTALL_LIBDIR}
167
+ )
165
168
endif ()
166
- endif ()
167
- endfunction ()
168
-
169
- #
170
- # header only virtual target creation
171
- #
172
- function (absl_header_library )
173
- cmake_parse_arguments (ABSL_HO_LIB
174
- "DISABLE_INSTALL"
175
- "EXPORT_NAME;TARGET"
176
- "PUBLIC_LIBRARIES;PRIVATE_COMPILE_FLAGS;PUBLIC_INCLUDE_DIRS;PRIVATE_INCLUDE_DIRS"
177
- ${ARGN}
178
- )
179
-
180
- set (_NAME ${ABSL_HO_LIB_TARGET} )
181
-
182
- set (__dummy_header_only_lib_file "${CMAKE_CURRENT_BINARY_DIR} /${_NAME} _header_only_dummy.cc" )
183
-
184
- if (NOT EXISTS ${__dummy_header_only_lib_file} )
185
- file (WRITE ${__dummy_header_only_lib_file}
186
- "/* generated file for header-only cmake target */
187
-
188
- namespace absl {
189
-
190
- // single meaningless symbol
191
- void ${_NAME} __header_fakesym() {}
192
- } // namespace absl
193
- "
194
- )
195
- endif ()
196
-
197
169
198
- add_library (${_NAME} ${__dummy_header_only_lib_file} )
199
- target_link_libraries (${_NAME} PUBLIC ${ABSL_HO_LIB_PUBLIC_LIBRARIES} )
200
- target_include_directories (${_NAME}
201
- PUBLIC ${ABSL_COMMON_INCLUDE_DIRS} ${ABSL_HO_LIB_PUBLIC_INCLUDE_DIRS}
202
- PRIVATE ${ABSL_HO_LIB_PRIVATE_INCLUDE_DIRS}
203
- )
204
-
205
- # Add all Abseil targets to a a folder in the IDE for organization.
206
- set_property (TARGET ${_NAME} PROPERTY FOLDER ${ABSL_IDE_FOLDER} )
207
-
208
- if (ABSL_HO_LIB_EXPORT_NAME )
209
- add_library (absl::${ABSL_HO_LIB_EXPORT_NAME} ALIAS ${_NAME} )
170
+ add_library (absl::${ABSL_CC_LIB_NAME} ALIAS ${_NAME} )
210
171
endif ()
211
-
212
172
endfunction ()
213
173
214
-
215
- #
216
- # create an abseil unit_test and add it to the executed test list
174
+ # absl_cc_test()
217
175
#
218
- # parameters
219
- # TARGET: target name prefix
220
- # SOURCES: sources files for the tests
221
- # PUBLIC_LIBRARIES: targets and flags for linking phase.
222
- # PRIVATE_COMPILE_FLAGS: compile flags for the test. Will not be exported.
176
+ # CMake function to imitate Bazel's cc_test rule.
223
177
#
224
- # create a target associated to <NAME>_bin
178
+ # Parameters:
179
+ # NAME: name of target (see Usage below)
180
+ # SRCS: List of source files for the binary
181
+ # DEPS: List of other libraries to be linked in to the binary targets
182
+ # COPTS: List of private compile options
183
+ # DEFINES: List of public defines
184
+ # LINKOPTS: List of link options
225
185
#
226
- # all tests will be register for execution with add_test()
186
+ # Note:
187
+ # By default, absl_cc_test will always create a binary named absl_${NAME}.
188
+ # This will also add it to ctest list as absl_${NAME}.
227
189
#
228
- # test compilation and execution is disable when ABSL_RUN_TESTS=OFF
190
+ # Usage:
191
+ # absl_cc_library(
192
+ # NAME
193
+ # awesome
194
+ # HDRS
195
+ # "a.h"
196
+ # SRCS
197
+ # "a.cc"
198
+ # PUBLIC
199
+ # )
229
200
#
230
- function (absl_test )
201
+ # absl_cc_test(
202
+ # NAME
203
+ # awesome_test
204
+ # SRCS
205
+ # "awesome_test.cc"
206
+ # DEPS
207
+ # absl::awesome
208
+ # gmock
209
+ # gtest_main
210
+ # )
211
+ function (absl_cc_test )
212
+ if (NOT ABSL_RUN_TESTS )
213
+ return ()
214
+ endif ()
231
215
232
- cmake_parse_arguments (ABSL_TEST
216
+ cmake_parse_arguments (ABSL_CC_TEST
233
217
""
234
- "TARGET "
235
- "SOURCES;PUBLIC_LIBRARIES;PRIVATE_COMPILE_FLAGS;PUBLIC_INCLUDE_DIRS "
218
+ "NAME "
219
+ "SRCS;COPTS;DEFINES;LINKOPTS;DEPS "
236
220
${ARGN}
237
221
)
238
222
223
+ set (_NAME "absl_${ABSL_CC_TEST_NAME} " )
224
+ add_executable (${_NAME} "" )
225
+ target_sources (${_NAME} PRIVATE ${ABSL_CC_TEST_SRCS} )
226
+ target_include_directories (${_NAME}
227
+ PUBLIC ${ABSL_COMMON_INCLUDE_DIRS}
228
+ PRIVATE ${GMOCK_INCLUDE_DIRS} ${GTEST_INCLUDE_DIRS}
229
+ )
230
+ target_compile_definitions (${_NAME}
231
+ PUBLIC ${ABSL_CC_TEST_DEFINES}
232
+ )
233
+ target_compile_options (${_NAME}
234
+ PRIVATE ${ABSL_CC_TEST_COPTS}
235
+ )
236
+ target_link_libraries (${_NAME}
237
+ PUBLIC ${ABSL_CC_TEST_DEPS}
238
+ PRIVATE ${ABSL_CC_TEST_LINKOPTS}
239
+ )
240
+ # Add all Abseil targets to a a folder in the IDE for organization.
241
+ set_property (TARGET ${_NAME} PROPERTY FOLDER ${ABSL_IDE_FOLDER} /test )
239
242
240
- if (ABSL_RUN_TESTS )
241
-
242
- set (_NAME ${ABSL_TEST_TARGET} )
243
- string (TOUPPER ${_NAME} _UPPER_NAME )
244
-
245
- add_executable (${_NAME} _bin ${ABSL_TEST_SOURCES} )
246
-
247
- target_compile_options (${_NAME} _bin PRIVATE ${ABSL_TEST_PRIVATE_COMPILE_FLAGS} )
248
- target_link_libraries (${_NAME} _bin PUBLIC ${ABSL_TEST_PUBLIC_LIBRARIES} ${ABSL_TEST_COMMON_LIBRARIES} )
249
- target_include_directories (${_NAME} _bin
250
- PUBLIC ${ABSL_COMMON_INCLUDE_DIRS} ${ABSL_TEST_PUBLIC_INCLUDE_DIRS}
251
- PRIVATE ${GMOCK_INCLUDE_DIRS} ${GTEST_INCLUDE_DIRS}
252
- )
253
-
254
- # Add all Abseil targets to a a folder in the IDE for organization.
255
- set_property (TARGET ${_NAME} _bin PROPERTY FOLDER ${ABSL_IDE_FOLDER} )
256
-
257
- add_test (${_NAME} ${_NAME} _bin )
258
- endif (ABSL_RUN_TESTS )
243
+ set_property (TARGET ${_NAME} PROPERTY CXX_STANDARD ${ABSL_CXX_STANDARD} )
244
+ set_property (TARGET ${_NAME} PROPERTY CXX_STANDARD_REQUIRED ON )
259
245
246
+ add_test (NAME ${_NAME} COMMAND ${_NAME} )
260
247
endfunction ()
261
248
262
249
263
-
264
-
265
250
function (check_target my_target )
266
-
267
251
if (NOT TARGET ${my_target} )
268
252
message (FATAL_ERROR " ABSL: compiling absl requires a ${my_target} CMake target in your project,
269
253
see CMake/README.md for more details" )
270
254
endif (NOT TARGET ${my_target} )
271
-
272
255
endfunction ()
0 commit comments