"Fossies" - the Fresh Open Source Software Archive 
Member "abseil-cpp-20230802.1/CMake/AbseilHelpers.cmake" (18 Sep 2023, 14425 Bytes) of package /linux/misc/abseil-cpp-20230802.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) CMake source code syntax highlighting (style:
standard) with prefixed line numbers.
Alternatively you can here
view or
download the uninterpreted source code file.
See also the last
Fossies "Diffs" side-by-side code changes report for "AbseilHelpers.cmake":
20230125.3_vs_20230802.0.
1 #
2 # Copyright 2017 The Abseil Authors.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # https://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16
17 include(CMakeParseArguments)
18 include(AbseilConfigureCopts)
19 include(AbseilDll)
20
21 # The IDE folder for Abseil that will be used if Abseil is included in a CMake
22 # project that sets
23 # set_property(GLOBAL PROPERTY USE_FOLDERS ON)
24 # For example, Visual Studio supports folders.
25 if(NOT DEFINED ABSL_IDE_FOLDER)
26 set(ABSL_IDE_FOLDER Abseil)
27 endif()
28
29 if(ABSL_USE_SYSTEM_INCLUDES)
30 set(ABSL_INTERNAL_INCLUDE_WARNING_GUARD SYSTEM)
31 else()
32 set(ABSL_INTERNAL_INCLUDE_WARNING_GUARD "")
33 endif()
34
35 # absl_cc_library()
36 #
37 # CMake function to imitate Bazel's cc_library rule.
38 #
39 # Parameters:
40 # NAME: name of target (see Note)
41 # HDRS: List of public header files for the library
42 # SRCS: List of source files for the library
43 # DEPS: List of other libraries to be linked in to the binary targets
44 # COPTS: List of private compile options
45 # DEFINES: List of public defines
46 # LINKOPTS: List of link options
47 # PUBLIC: Add this so that this library will be exported under absl::
48 # Also in IDE, target will appear in Abseil folder while non PUBLIC will be in Abseil/internal.
49 # TESTONLY: When added, this target will only be built if both
50 # BUILD_TESTING=ON and ABSL_BUILD_TESTING=ON.
51 #
52 # Note:
53 # By default, absl_cc_library will always create a library named absl_${NAME},
54 # and alias target absl::${NAME}. The absl:: form should always be used.
55 # This is to reduce namespace pollution.
56 #
57 # absl_cc_library(
58 # NAME
59 # awesome
60 # HDRS
61 # "a.h"
62 # SRCS
63 # "a.cc"
64 # )
65 # absl_cc_library(
66 # NAME
67 # fantastic_lib
68 # SRCS
69 # "b.cc"
70 # DEPS
71 # absl::awesome # not "awesome" !
72 # PUBLIC
73 # )
74 #
75 # absl_cc_library(
76 # NAME
77 # main_lib
78 # ...
79 # DEPS
80 # absl::fantastic_lib
81 # )
82 #
83 # TODO: Implement "ALWAYSLINK"
84 function(absl_cc_library)
85 cmake_parse_arguments(ABSL_CC_LIB
86 "DISABLE_INSTALL;PUBLIC;TESTONLY"
87 "NAME"
88 "HDRS;SRCS;COPTS;DEFINES;LINKOPTS;DEPS"
89 ${ARGN}
90 )
91
92 if(ABSL_CC_LIB_TESTONLY AND
93 NOT ((BUILD_TESTING AND ABSL_BUILD_TESTING) OR
94 (ABSL_BUILD_TEST_HELPERS AND ABSL_CC_LIB_PUBLIC)))
95 return()
96 endif()
97
98 if(ABSL_ENABLE_INSTALL)
99 set(_NAME "${ABSL_CC_LIB_NAME}")
100 else()
101 set(_NAME "absl_${ABSL_CC_LIB_NAME}")
102 endif()
103
104 # Check if this is a header-only library
105 # Note that as of February 2019, many popular OS's (for example, Ubuntu
106 # 16.04 LTS) only come with cmake 3.5 by default. For this reason, we can't
107 # use list(FILTER...)
108 set(ABSL_CC_SRCS "${ABSL_CC_LIB_SRCS}")
109 foreach(src_file IN LISTS ABSL_CC_SRCS)
110 if(${src_file} MATCHES ".*\\.(h|inc)")
111 list(REMOVE_ITEM ABSL_CC_SRCS "${src_file}")
112 endif()
113 endforeach()
114
115 if(ABSL_CC_SRCS STREQUAL "")
116 set(ABSL_CC_LIB_IS_INTERFACE 1)
117 else()
118 set(ABSL_CC_LIB_IS_INTERFACE 0)
119 endif()
120
121 # Determine this build target's relationship to the DLL. It's one of four things:
122 # 1. "dll" -- This target is part of the DLL
123 # 2. "dll_dep" -- This target is not part of the DLL, but depends on the DLL.
124 # Note that we assume any target not in the DLL depends on the
125 # DLL. This is not a technical necessity but a convenience
126 # which happens to be true, because nearly every target is
127 # part of the DLL.
128 # 3. "shared" -- This is a shared library, perhaps on a non-windows platform
129 # where DLL doesn't make sense.
130 # 4. "static" -- This target does not depend on the DLL and should be built
131 # statically.
132 if (${ABSL_BUILD_DLL})
133 if(ABSL_ENABLE_INSTALL)
134 absl_internal_dll_contains(TARGET ${_NAME} OUTPUT _in_dll)
135 absl_internal_test_dll_contains(TARGET ${_NAME} OUTPUT _in_test_dll)
136 else()
137 absl_internal_dll_contains(TARGET ${ABSL_CC_LIB_NAME} OUTPUT _in_dll)
138 absl_internal_test_dll_contains(TARGET ${ABSL_CC_LIB_NAME} OUTPUT _in_test_dll)
139 endif()
140 if (${_in_dll} OR ${_in_test_dll})
141 # This target should be replaced by the DLL
142 set(_build_type "dll")
143 set(ABSL_CC_LIB_IS_INTERFACE 1)
144 else()
145 # Building a DLL, but this target is not part of the DLL
146 set(_build_type "dll_dep")
147 endif()
148 elseif(BUILD_SHARED_LIBS)
149 set(_build_type "shared")
150 else()
151 set(_build_type "static")
152 endif()
153
154 # Generate a pkg-config file for every library:
155 if(ABSL_ENABLE_INSTALL)
156 if(absl_VERSION)
157 set(PC_VERSION "${absl_VERSION}")
158 else()
159 set(PC_VERSION "head")
160 endif()
161 if(NOT _build_type STREQUAL "dll")
162 set(LNK_LIB "${LNK_LIB} -labsl_${_NAME}")
163 endif()
164 foreach(dep ${ABSL_CC_LIB_DEPS})
165 if(${dep} MATCHES "^absl::(.*)")
166 # for DLL builds many libs are not created, but add
167 # the pkgconfigs nevertheless, pointing to the dll.
168 if(_build_type STREQUAL "dll")
169 # hide this MATCHES in an if-clause so it doesn't overwrite
170 # the CMAKE_MATCH_1 from (${dep} MATCHES "^absl::(.*)")
171 if(NOT PC_DEPS MATCHES "abseil_dll")
172 # Join deps with commas.
173 if(PC_DEPS)
174 set(PC_DEPS "${PC_DEPS},")
175 endif()
176 # don't duplicate dll-dep if it exists already
177 set(PC_DEPS "${PC_DEPS} abseil_dll = ${PC_VERSION}")
178 set(LNK_LIB "${LNK_LIB} -labseil_dll")
179 endif()
180 else()
181 # Join deps with commas.
182 if(PC_DEPS)
183 set(PC_DEPS "${PC_DEPS},")
184 endif()
185 set(PC_DEPS "${PC_DEPS} absl_${CMAKE_MATCH_1} = ${PC_VERSION}")
186 endif()
187 endif()
188 endforeach()
189 foreach(cflag ${ABSL_CC_LIB_COPTS})
190 if(${cflag} MATCHES "^(-Wno|/wd)")
191 # These flags are needed to suppress warnings that might fire in our headers.
192 set(PC_CFLAGS "${PC_CFLAGS} ${cflag}")
193 elseif(${cflag} MATCHES "^(-W|/w[1234eo])")
194 # Don't impose our warnings on others.
195 elseif(${cflag} MATCHES "^-m")
196 # Don't impose CPU instruction requirements on others, as
197 # the code performs feature detection on runtime.
198 else()
199 set(PC_CFLAGS "${PC_CFLAGS} ${cflag}")
200 endif()
201 endforeach()
202 string(REPLACE ";" " " PC_LINKOPTS "${ABSL_CC_LIB_LINKOPTS}")
203 FILE(GENERATE OUTPUT "${CMAKE_BINARY_DIR}/lib/pkgconfig/absl_${_NAME}.pc" CONTENT "\
204 prefix=${CMAKE_INSTALL_PREFIX}\n\
205 exec_prefix=\${prefix}\n\
206 libdir=${CMAKE_INSTALL_FULL_LIBDIR}\n\
207 includedir=${CMAKE_INSTALL_FULL_INCLUDEDIR}\n\
208 \n\
209 Name: absl_${_NAME}\n\
210 Description: Abseil ${_NAME} library\n\
211 URL: https://abseil.io/\n\
212 Version: ${PC_VERSION}\n\
213 Requires:${PC_DEPS}\n\
214 Libs: -L\${libdir} $<$<NOT:$<BOOL:${ABSL_CC_LIB_IS_INTERFACE}>>:${LNK_LIB}> ${PC_LINKOPTS}\n\
215 Cflags: -I\${includedir}${PC_CFLAGS}\n")
216 INSTALL(FILES "${CMAKE_BINARY_DIR}/lib/pkgconfig/absl_${_NAME}.pc"
217 DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
218 endif()
219
220 if(NOT ABSL_CC_LIB_IS_INTERFACE)
221 if(_build_type STREQUAL "dll_dep")
222 # This target depends on the DLL. When adding dependencies to this target,
223 # any depended-on-target which is contained inside the DLL is replaced
224 # with a dependency on the DLL.
225 add_library(${_NAME} STATIC "")
226 target_sources(${_NAME} PRIVATE ${ABSL_CC_LIB_SRCS} ${ABSL_CC_LIB_HDRS})
227 absl_internal_dll_targets(
228 DEPS ${ABSL_CC_LIB_DEPS}
229 OUTPUT _dll_deps
230 )
231 target_link_libraries(${_NAME}
232 PUBLIC ${_dll_deps}
233 PRIVATE
234 ${ABSL_CC_LIB_LINKOPTS}
235 ${ABSL_DEFAULT_LINKOPTS}
236 )
237
238 if (ABSL_CC_LIB_TESTONLY)
239 set(_gtest_link_define "GTEST_LINKED_AS_SHARED_LIBRARY=1")
240 else()
241 set(_gtest_link_define)
242 endif()
243
244 target_compile_definitions(${_NAME}
245 PUBLIC
246 ABSL_CONSUME_DLL
247 "${_gtest_link_define}"
248 )
249
250 elseif(_build_type STREQUAL "static" OR _build_type STREQUAL "shared")
251 add_library(${_NAME} "")
252 target_sources(${_NAME} PRIVATE ${ABSL_CC_LIB_SRCS} ${ABSL_CC_LIB_HDRS})
253 target_link_libraries(${_NAME}
254 PUBLIC ${ABSL_CC_LIB_DEPS}
255 PRIVATE
256 ${ABSL_CC_LIB_LINKOPTS}
257 ${ABSL_DEFAULT_LINKOPTS}
258 )
259 else()
260 message(FATAL_ERROR "Invalid build type: ${_build_type}")
261 endif()
262
263 # Linker language can be inferred from sources, but in the case of DLLs we
264 # don't have any .cc files so it would be ambiguous. We could set it
265 # explicitly only in the case of DLLs but, because "CXX" is always the
266 # correct linker language for static or for shared libraries, we set it
267 # unconditionally.
268 set_property(TARGET ${_NAME} PROPERTY LINKER_LANGUAGE "CXX")
269
270 target_include_directories(${_NAME} ${ABSL_INTERNAL_INCLUDE_WARNING_GUARD}
271 PUBLIC
272 "$<BUILD_INTERFACE:${ABSL_COMMON_INCLUDE_DIRS}>"
273 $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
274 )
275 target_compile_options(${_NAME}
276 PRIVATE ${ABSL_CC_LIB_COPTS})
277 target_compile_definitions(${_NAME} PUBLIC ${ABSL_CC_LIB_DEFINES})
278
279 # Add all Abseil targets to a a folder in the IDE for organization.
280 if(ABSL_CC_LIB_PUBLIC)
281 set_property(TARGET ${_NAME} PROPERTY FOLDER ${ABSL_IDE_FOLDER})
282 elseif(ABSL_CC_LIB_TESTONLY)
283 set_property(TARGET ${_NAME} PROPERTY FOLDER ${ABSL_IDE_FOLDER}/test)
284 else()
285 set_property(TARGET ${_NAME} PROPERTY FOLDER ${ABSL_IDE_FOLDER}/internal)
286 endif()
287
288 if(ABSL_PROPAGATE_CXX_STD)
289 # Abseil libraries require C++14 as the current minimum standard. When
290 # compiled with C++17 (either because it is the compiler's default or
291 # explicitly requested), then Abseil requires C++17.
292 target_compile_features(${_NAME} PUBLIC ${ABSL_INTERNAL_CXX_STD_FEATURE})
293 endif()
294
295 # When being installed, we lose the absl_ prefix. We want to put it back
296 # to have properly named lib files. This is a no-op when we are not being
297 # installed.
298 if(ABSL_ENABLE_INSTALL)
299 set_target_properties(${_NAME} PROPERTIES
300 OUTPUT_NAME "absl_${_NAME}"
301 SOVERSION "2308.0.0"
302 )
303 endif()
304 else()
305 # Generating header-only library
306 add_library(${_NAME} INTERFACE)
307 target_include_directories(${_NAME} ${ABSL_INTERNAL_INCLUDE_WARNING_GUARD}
308 INTERFACE
309 "$<BUILD_INTERFACE:${ABSL_COMMON_INCLUDE_DIRS}>"
310 $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
311 )
312
313 if (_build_type STREQUAL "dll")
314 set(ABSL_CC_LIB_DEPS abseil_dll)
315 endif()
316
317 target_link_libraries(${_NAME}
318 INTERFACE
319 ${ABSL_CC_LIB_DEPS}
320 ${ABSL_CC_LIB_LINKOPTS}
321 ${ABSL_DEFAULT_LINKOPTS}
322 )
323 target_compile_definitions(${_NAME} INTERFACE ${ABSL_CC_LIB_DEFINES})
324
325 if(ABSL_PROPAGATE_CXX_STD)
326 # Abseil libraries require C++14 as the current minimum standard.
327 # Top-level application CMake projects should ensure a consistent C++
328 # standard for all compiled sources by setting CMAKE_CXX_STANDARD.
329 target_compile_features(${_NAME} INTERFACE ${ABSL_INTERNAL_CXX_STD_FEATURE})
330 endif()
331 endif()
332
333 if(ABSL_ENABLE_INSTALL)
334 install(TARGETS ${_NAME} EXPORT ${PROJECT_NAME}Targets
335 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
336 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
337 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
338 )
339 endif()
340
341 add_library(absl::${ABSL_CC_LIB_NAME} ALIAS ${_NAME})
342 endfunction()
343
344 # absl_cc_test()
345 #
346 # CMake function to imitate Bazel's cc_test rule.
347 #
348 # Parameters:
349 # NAME: name of target (see Usage below)
350 # SRCS: List of source files for the binary
351 # DEPS: List of other libraries to be linked in to the binary targets
352 # COPTS: List of private compile options
353 # DEFINES: List of public defines
354 # LINKOPTS: List of link options
355 #
356 # Note:
357 # By default, absl_cc_test will always create a binary named absl_${NAME}.
358 # This will also add it to ctest list as absl_${NAME}.
359 #
360 # Usage:
361 # absl_cc_library(
362 # NAME
363 # awesome
364 # HDRS
365 # "a.h"
366 # SRCS
367 # "a.cc"
368 # PUBLIC
369 # )
370 #
371 # absl_cc_test(
372 # NAME
373 # awesome_test
374 # SRCS
375 # "awesome_test.cc"
376 # DEPS
377 # absl::awesome
378 # GTest::gmock
379 # GTest::gtest_main
380 # )
381 function(absl_cc_test)
382 if(NOT (BUILD_TESTING AND ABSL_BUILD_TESTING))
383 return()
384 endif()
385
386 cmake_parse_arguments(ABSL_CC_TEST
387 ""
388 "NAME"
389 "SRCS;COPTS;DEFINES;LINKOPTS;DEPS"
390 ${ARGN}
391 )
392
393 set(_NAME "absl_${ABSL_CC_TEST_NAME}")
394
395 add_executable(${_NAME} "")
396 target_sources(${_NAME} PRIVATE ${ABSL_CC_TEST_SRCS})
397 target_include_directories(${_NAME}
398 PUBLIC ${ABSL_COMMON_INCLUDE_DIRS}
399 PRIVATE ${absl_gtest_src_dir}/googletest/include ${absl_gtest_src_dir}/googlemock/include
400 )
401
402 if (${ABSL_BUILD_DLL})
403 target_compile_definitions(${_NAME}
404 PUBLIC
405 ${ABSL_CC_TEST_DEFINES}
406 ABSL_CONSUME_DLL
407 ABSL_CONSUME_TEST_DLL
408 GTEST_LINKED_AS_SHARED_LIBRARY=1
409 )
410
411 # Replace dependencies on targets inside the DLL with abseil_dll itself.
412 absl_internal_dll_targets(
413 DEPS ${ABSL_CC_TEST_DEPS}
414 OUTPUT ABSL_CC_TEST_DEPS
415 )
416 absl_internal_dll_targets(
417 DEPS ${ABSL_CC_TEST_LINKOPTS}
418 OUTPUT ABSL_CC_TEST_LINKOPTS
419 )
420 else()
421 target_compile_definitions(${_NAME}
422 PUBLIC
423 ${ABSL_CC_TEST_DEFINES}
424 )
425 endif()
426 target_compile_options(${_NAME}
427 PRIVATE ${ABSL_CC_TEST_COPTS}
428 )
429
430 target_link_libraries(${_NAME}
431 PUBLIC ${ABSL_CC_TEST_DEPS}
432 PRIVATE ${ABSL_CC_TEST_LINKOPTS}
433 )
434 # Add all Abseil targets to a folder in the IDE for organization.
435 set_property(TARGET ${_NAME} PROPERTY FOLDER ${ABSL_IDE_FOLDER}/test)
436
437 if(ABSL_PROPAGATE_CXX_STD)
438 # Abseil libraries require C++14 as the current minimum standard.
439 # Top-level application CMake projects should ensure a consistent C++
440 # standard for all compiled sources by setting CMAKE_CXX_STANDARD.
441 target_compile_features(${_NAME} PUBLIC ${ABSL_INTERNAL_CXX_STD_FEATURE})
442 endif()
443
444 add_test(NAME ${_NAME} COMMAND ${_NAME})
445 endfunction()