diff options
author | ahedberg <ahedberg@google.com> | 2018-10-22T17·10-0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-22T17·10-0400 |
commit | 5fbde92571a269675543b2c513747adb711b0ea1 (patch) | |
tree | 073afd2209e6f9d6f82f77619764dca75f7fd87e /CMake | |
parent | 45221ccc4ed643e4209b0cc5798e97203f108fa8 (diff) | |
parent | bd2d9a420747665ff7a85257b2d72df4c97e4cd6 (diff) |
Merge pull request #136 from rongjiecomputer/cc_library
[CMake] Implement absl_cc_library as Bazel's cc_library
Diffstat (limited to 'CMake')
-rw-r--r-- | CMake/AbseilHelpers.cmake | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/CMake/AbseilHelpers.cmake b/CMake/AbseilHelpers.cmake index 0c9341747512..96813b083d01 100644 --- a/CMake/AbseilHelpers.cmake +++ b/CMake/AbseilHelpers.cmake @@ -62,7 +62,110 @@ function(absl_library) endif() endfunction() +# +# CMake function to imitate Bazel's cc_library rule. +# +# Parameters: +# NAME: name of target (see Note) +# HDRS: List of public header files for the library +# SRCS: List of source files for the library +# DEPS: List of other libraries to be linked in to the binary targets +# COPTS: List of private compile options +# DEFINES: List of public defines +# LINKOPTS: List of link options +# PUBLIC: Add this so that this library will be exported under absl:: (see Note). +# TESTONLY: When added, this target will only be built if user passes -DABSL_RUN_TESTS=ON to CMake. +# +# Note: +# +# By default, absl_cc_library will always create a library named absl_internal_${NAME}, +# which means other targets can only depend this library as absl_internal_${NAME}, not ${NAME}. +# This is to reduce namespace pollution. +# +# absl_cc_library( +# NAME +# awesome_lib +# HDRS +# "a.h" +# SRCS +# "a.cc" +# ) +# absl_cc_library( +# NAME +# fantastic_lib +# SRCS +# "b.cc" +# DEPS +# absl_internal_awesome_lib # not "awesome_lib"! +# ) +# +# If PUBLIC is set, absl_cc_library will instead create a target named +# absl_${NAME} and an alias absl::${NAME}. +# +# absl_cc_library( +# NAME +# main_lib +# ... +# PUBLIC +# ) +# +# User can then use the library as absl::main_lib (although absl_main_lib is defined too). +# +# TODO: Implement "ALWAYSLINK" +function(absl_cc_library) + cmake_parse_arguments(ABSL_CC_LIB + "DISABLE_INSTALL;PUBLIC;TESTONLY" + "NAME" + "HDRS;SRCS;COPTS;DEFINES;LINKOPTS;DEPS" + ${ARGN} + ) + + if (NOT ABSL_CC_LIB_TESTONLY OR ABSL_RUN_TESTS) + if (ABSL_CC_LIB_PUBLIC) + set(_NAME "absl_${ABSL_CC_LIB_NAME}") + else() + set(_NAME "absl_internal_${ABSL_CC_LIB_NAME}") + endif() + + # Check if this is a header-only library + if ("${ABSL_CC_LIB_SRCS}" STREQUAL "") + set(ABSL_CC_LIB_IS_INTERFACE 1) + else() + set(ABSL_CC_LIB_IS_INTERFACE 0) + endif() + + if(NOT ABSL_CC_LIB_IS_INTERFACE) + add_library(${_NAME} STATIC "") + target_sources(${_NAME} PRIVATE ${ABSL_CC_LIB_SRCS} ${ABSL_CC_LIB_HDRS}) + target_include_directories(${_NAME} + PUBLIC ${ABSL_COMMON_INCLUDE_DIRS}) + # TODO(rongjiecomputer): Revisit ABSL_COMPILE_CXXFLAGS when fixing GH#123 + target_compile_options(${_NAME} + PRIVATE ${ABSL_COMPILE_CXXFLAGS} ${ABSL_CC_LIB_COPTS}) + target_link_libraries(${_NAME} + PUBLIC ${ABSL_CC_LIB_DEPS} + PRIVATE ${ABSL_CC_LIB_LINKOPTS} + ) + target_compile_definitions(${_NAME} PUBLIC ${ABSL_CC_LIB_DEFINES}) + + # Add all Abseil targets to a a folder in the IDE for organization. + set_property(TARGET ${_NAME} PROPERTY FOLDER ${ABSL_IDE_FOLDER}) + else() + # Generating header-only library + add_library(${_NAME} INTERFACE) + target_include_directories(${_NAME} INTERFACE ${ABSL_COMMON_INCLUDE_DIRS}) + target_link_libraries(${_NAME} + INTERFACE ${ABSL_CC_LIB_DEPS} ${ABSL_CC_LIB_LINKOPTS} + ) + target_compile_definitions(${_NAME} INTERFACE ${ABSL_CC_LIB_DEFINES}) + endif() + + if(ABSL_CC_LIB_PUBLIC) + add_library(absl::${ABSL_CC_LIB_NAME} ALIAS ${_NAME}) + endif() + endif() +endfunction() # # header only virtual target creation |