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