Skip to content

Commit

Permalink
CMake: install artifacts reflect the compiled ABI
Browse files Browse the repository at this point in the history
Change `absl/base/options.h` at install time to reflect the ABI used
to compile the libraries, i.e., if Abseil was compiled with
C++ >= 17 then the `ABSL_OPTION_USE_STD_*` macros are set to `1`,
because then the C++ 17 types are embedded in the ABI of the installed
artifacts.

Likewise, set the `target_compile_features()` of Abseil libraries
to reflect the C++ version used to compile them. If they the Abseil
libraries are compiled with C++ >= 17, then all downstream
dependencies must also be compiled with C++ >= 17.

Fixes #696

PiperOrigin-RevId: 472538165
Change-Id: Ic9e346ebe277c882a18ad96e1918c9e3511c91c3
  • Loading branch information
Abseil Team authored and copybara-github committed Sep 6, 2022
1 parent d3c00b0 commit 308bbf3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
27 changes: 24 additions & 3 deletions CMake/AbseilHelpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,27 @@ function(_target_compile_features_if_available TARGET TYPE FEATURE)
endif()
endfunction()

include(CheckCXXSourceCompiles)

check_cxx_source_compiles(
[==[
#ifdef _MSC_VER
# if _MSVC_LANG < 201700L
# error "The compiler defaults or is configured for C++ < 17"
# endif
#elif __cplusplus < 201700L
# error "The compiler defaults or is configured for C++ < 17"
#endif
int main() { return 0; }
]==]
ABSL_INTERNAL_AT_LEAST_CXX17)

if(ABSL_INTERNAL_AT_LEAST_CXX17)
set(ABSL_INTERNAL_CXX_STD_FEATURE cxx_std_17)
else()
set(ABSL_INTERNAL_CXX_STD_FEATURE cxx_std_14)
endif()

# absl_cc_library()
#
# CMake function to imitate Bazel's cc_library rule.
Expand Down Expand Up @@ -279,7 +300,7 @@ Cflags: -I\${includedir}${PC_CFLAGS}\n")
# Abseil libraries require C++14 as the current minimum standard.
# Top-level application CMake projects should ensure a consistent C++
# standard for all compiled sources by setting CMAKE_CXX_STANDARD.
_target_compile_features_if_available(${_NAME} PUBLIC cxx_std_14)
_target_compile_features_if_available(${_NAME} PUBLIC ${ABSL_INTERNAL_CXX_STD_FEATURE})
else()
# Note: This is legacy (before CMake 3.8) behavior. Setting the
# target-level CXX_STANDARD property to ABSL_CXX_STANDARD (which is
Expand Down Expand Up @@ -327,7 +348,7 @@ Cflags: -I\${includedir}${PC_CFLAGS}\n")
# Abseil libraries require C++14 as the current minimum standard.
# Top-level application CMake projects should ensure a consistent C++
# standard for all compiled sources by setting CMAKE_CXX_STANDARD.
_target_compile_features_if_available(${_NAME} INTERFACE cxx_std_14)
_target_compile_features_if_available(${_NAME} INTERFACE ${ABSL_INTERNAL_CXX_STD_FEATURE})

# (INTERFACE libraries can't have the CXX_STANDARD property set, so there
# is no legacy behavior else case).
Expand Down Expand Up @@ -439,7 +460,7 @@ function(absl_cc_test)
# Abseil libraries require C++14 as the current minimum standard.
# Top-level application CMake projects should ensure a consistent C++
# standard for all compiled sources by setting CMAKE_CXX_STANDARD.
_target_compile_features_if_available(${_NAME} PUBLIC cxx_std_14)
_target_compile_features_if_available(${_NAME} PUBLIC ${ABSL_INTERNAL_CXX_STD_FEATURE})
else()
# Note: This is legacy (before CMake 3.8) behavior. Setting the
# target-level CXX_STANDARD property to ABSL_CXX_STANDARD (which is
Expand Down
26 changes: 26 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ if (POLICY CMP0091)
cmake_policy(SET CMP0091 NEW)
endif (POLICY CMP0091)

# try_compile() honors the CMAKE_CXX_STANDARD value
if (POLICY CMP0067)
cmake_policy(SET CMP0067 NEW)
endif (POLICY CMP0067)

project(absl LANGUAGES CXX)
include(CTest)

Expand Down Expand Up @@ -229,4 +234,25 @@ if(ABSL_ENABLE_INSTALL)
PATTERN "copts" EXCLUDE
PATTERN "testdata" EXCLUDE
)

file(READ "absl/base/options.h" ABSL_INTERNAL_OPTIONS_H_CONTENTS)
if (ABSL_INTERNAL_AT_LEAST_CXX17)
string(REGEX REPLACE
"#define ABSL_OPTION_USE_STD_([^ ]*) 2"
"#define ABSL_OPTION_USE_STD_\\1 1"
ABSL_INTERNAL_OPTIONS_H_PINNED
"${ABSL_INTERNAL_OPTIONS_H_CONTENTS}")
else()
string(REGEX REPLACE
"#define ABSL_OPTION_USE_STD_([^ ]*) 2"
"#define ABSL_OPTION_USE_STD_\\1 0"
ABSL_INTERNAL_OPTIONS_H_PINNED
"${ABSL_INTERNAL_OPTIONS_H_CONTENTS}")
endif()
file(WRITE "${CMAKE_BINARY_DIR}/options-pinned.h" "${ABSL_INTERNAL_OPTIONS_H_PINNED}")

install(FILES "${CMAKE_BINARY_DIR}/options-pinned.h"
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/absl/base
RENAME "options.h")

endif() # ABSL_ENABLE_INSTALL

0 comments on commit 308bbf3

Please sign in to comment.