From eb317a701b83bf9a4f2a035d75747a3d76a48324 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Wed, 21 Oct 2020 00:01:29 -0700 Subject: Export of internal Abseil changes -- d09230db053c544c2dc7fd7d95d1ebe4302071e9 by Abseil Team : Move testonly_uses_linear_node_search to BtreeNodePeer PiperOrigin-RevId: 338210523 -- 7b11c945dbba7a354103c194877eba240f7f0cbc by Derek Mauro : Allow pinning to a GoogleTest commit to make the build reproducible Allow using a cached copy of the commit to avoid depending on GitHub PiperOrigin-RevId: 338115715 -- 8414f496c570a6398744da8324e158b39a2e3d92 by Andy Getzendanner : Generate a pkg-config file per absl_cc_library. PiperOrigin-RevId: 337986219 GitOrigin-RevId: d09230db053c544c2dc7fd7d95d1ebe4302071e9 Change-Id: Iae398ab8ad5c0c6833abd01aa5198315f5b6fa99 --- CMake/AbseilHelpers.cmake | 39 ++++++++++++++++++++ CMake/Googletest/CMakeLists.txt.in | 30 +++++---------- CMake/install_test_project/test.sh | 18 +++++++++ CMakeLists.txt | 17 +++++++-- WORKSPACE | 1 + absl/container/btree_test.cc | 61 +++++++++++++++++-------------- absl/container/internal/btree.h | 13 ------- absl/container/internal/btree_container.h | 1 + ci/cmake_common.sh | 25 +++++++++++++ ci/cmake_install_test.sh | 2 + ci/linux_gcc-latest_libstdcxx_cmake.sh | 16 +++----- ci/linux_gcc_alpine_cmake.sh | 14 +++---- ci/macos_xcode_cmake.sh | 12 ++++-- 13 files changed, 160 insertions(+), 89 deletions(-) create mode 100755 ci/cmake_common.sh diff --git a/CMake/AbseilHelpers.cmake b/CMake/AbseilHelpers.cmake index 8b2925c5..e63db3a8 100644 --- a/CMake/AbseilHelpers.cmake +++ b/CMake/AbseilHelpers.cmake @@ -137,6 +137,45 @@ function(absl_cc_library) set(_build_type "static") endif() + # Generate a pkg-config file for every library: + if(${_build_type} STREQUAL "static" OR ${_build_type} STREQUAL "shared") + if(NOT ABSL_CC_LIB_TESTONLY) + if(absl_VERSION) + set(PC_VERSION "${absl_VERSION}") + else() + set(PC_VERSION "head") + endif() + foreach(dep ${ABSL_CC_LIB_DEPS}) + if(${dep} MATCHES "^absl::(.*)") + set(PC_DEPS "${PC_DEPS} absl_${CMAKE_MATCH_1} = ${PC_VERSION}") + endif() + endforeach() + foreach(cflag ${ABSL_CC_LIB_COPTS}) + if(${cflag} MATCHES "^(-Wno|/wd)") + # These flags are needed to suppress warnings that might fire in our headers. + set(PC_CFLAGS "${PC_CFLAGS} ${cflag}") + elseif(${cflag} MATCHES "^(-W|/w[1234eo])") + # Don't impose our warnings on others. + else() + set(PC_CFLAGS "${PC_CFLAGS} ${cflag}") + endif() + endforeach() + FILE(GENERATE OUTPUT "${CMAKE_INSTALL_PREFIX}/lib/pkgconfig/absl_${_NAME}.pc" CONTENT "\ +prefix=${CMAKE_INSTALL_PREFIX}\n\ +exec_prefix=\${prefix}\n\ +libdir=\${prefix}/lib\n\ +includedir=\${prefix}/include\n\ +\n\ +Name: absl_${_NAME}\n\ +Description: Abseil ${_NAME} library\n\ +URL: https://abseil.io/\n\ +Version: ${PC_VERSION}\n\ +Requires.private:${PC_DEPS}\n\ +Libs: -L\${libdir} $ $<$>:-labsl_${_NAME}>\n\ +Cflags: -I\${includedir}${PC_CFLAGS}\n") + endif() + endif() + if(NOT ABSL_CC_LIB_IS_INTERFACE) if(${_build_type} STREQUAL "dll_dep") # This target depends on the DLL. When adding dependencies to this target, diff --git a/CMake/Googletest/CMakeLists.txt.in b/CMake/Googletest/CMakeLists.txt.in index 994dac0b..5769e3a9 100644 --- a/CMake/Googletest/CMakeLists.txt.in +++ b/CMake/Googletest/CMakeLists.txt.in @@ -3,24 +3,12 @@ cmake_minimum_required(VERSION 2.8.2) project(googletest-external NONE) include(ExternalProject) -if(${ABSL_USE_GOOGLETEST_HEAD}) - ExternalProject_Add(googletest - GIT_REPOSITORY https://github.com/google/googletest.git - GIT_TAG master - SOURCE_DIR "${absl_gtest_src_dir}" - BINARY_DIR "${absl_gtest_build_dir}" - CONFIGURE_COMMAND "" - BUILD_COMMAND "" - INSTALL_COMMAND "" - TEST_COMMAND "" - ) -else() - ExternalProject_Add(googletest - SOURCE_DIR "${absl_gtest_src_dir}" - BINARY_DIR "${absl_gtest_build_dir}" - CONFIGURE_COMMAND "" - BUILD_COMMAND "" - INSTALL_COMMAND "" - TEST_COMMAND "" - ) -endif() \ No newline at end of file +ExternalProject_Add(googletest + URL "${absl_gtest_download_url}" # May be empty + SOURCE_DIR "${absl_gtest_src_dir}" + BINARY_DIR "${absl_gtest_build_dir}" + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" + TEST_COMMAND "" +) diff --git a/CMake/install_test_project/test.sh b/CMake/install_test_project/test.sh index 99989b03..ddc7726b 100755 --- a/CMake/install_test_project/test.sh +++ b/CMake/install_test_project/test.sh @@ -118,6 +118,24 @@ if ! grep absl::strings "${libdir}/cmake/${absl_subdir}/abslTargets.cmake"; then exit 1 fi +pushd "${HOME}" +cat > hello-abseil.cc << EOF +#include + +#include "absl/strings/str_format.h" + +int main(int argc, char **argv) { + absl::PrintF("Hello Abseil!\n"); + return EXIT_SUCCESS; +} +EOF +export PKG_CONFIG_PATH="${install_dir}/${libdir}/pkgconfig" +pc_args=($(pkg-config --cflags --libs --static absl_str_format)) +g++ -static -o hello-abseil hello-abseil.cc "${pc_args[@]}" +hello="$(./hello-abseil)" +[[ "${hello}" == "Hello Abseil!" ]] +popd + uninstall_absl popd diff --git a/CMakeLists.txt b/CMakeLists.txt index bc195e04..c1ae8d56 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -95,12 +95,13 @@ find_package(Threads REQUIRED) option(ABSL_USE_EXTERNAL_GOOGLETEST "If ON, Abseil will assume that the targets for GoogleTest are already provided by the including project. This makes sense when Abseil is used with add_subproject." OFF) - option(ABSL_USE_GOOGLETEST_HEAD - "If ON, abseil will download HEAD from googletest at config time." OFF) + "If ON, abseil will download HEAD from GoogleTest at config time." OFF) + +set(ABSL_GOOGLETEST_DOWNLOAD_URL "" CACHE STRING "If set, download GoogleTest from this URL") set(ABSL_LOCAL_GOOGLETEST_DIR "/usr/src/googletest" CACHE PATH - "If ABSL_USE_GOOGLETEST_HEAD is OFF, specifies the directory of a local googletest checkout." + "If ABSL_USE_GOOGLETEST_HEAD is OFF and ABSL_GOOGLETEST_URL is not set, specifies the directory of a local GoogleTest checkout." ) option(ABSL_RUN_TESTS "If ON, Abseil tests will be run." OFF) @@ -113,7 +114,15 @@ if(${ABSL_RUN_TESTS}) ## check targets if (NOT ABSL_USE_EXTERNAL_GOOGLETEST) set(absl_gtest_build_dir ${CMAKE_BINARY_DIR}/googletest-build) - if(${ABSL_USE_GOOGLETEST_HEAD}) + if(ABSL_USE_GOOGLETEST_HEAD AND ABSL_GOOGLETEST_DOWNLOAD_URL) + message(FATAL_ERROR "Do not set both ABSL_USE_GOOGLETEST_HEAD and ABSL_GOOGLETEST_DOWNLOAD_URL") + endif() + if(ABSL_USE_GOOGLETEST_HEAD) + set(absl_gtest_download_url "https://github.com/google/googletest/archive/master.zip") + elseif(ABSL_GOOGLETEST_DOWNLOAD_URL) + set(absl_gtest_download_url ${ABSL_GOOGLETEST_DOWNLOAD_URL}) + endif() + if(absl_gtest_download_url) set(absl_gtest_src_dir ${CMAKE_BINARY_DIR}/googletest-src) else() set(absl_gtest_src_dir ${ABSL_LOCAL_GOOGLETEST_DIR}) diff --git a/WORKSPACE b/WORKSPACE index 0b533562..ed90d2ba 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -20,6 +20,7 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") # GoogleTest/GoogleMock framework. Used by most unit-tests. http_archive( name = "com_google_googletest", + # Keep this URL in sync with ABSL_GOOGLETEST_COMMIT in ci/cmake_common.sh. urls = ["https://github.com/google/googletest/archive/8567b09290fe402cf01923e2131c5635b8ed851b.zip"], # 2020-06-12T22:24:28Z strip_prefix = "googletest-8567b09290fe402cf01923e2131c5635b8ed851b", sha256 = "9a8a166eb6a56c7b3d7b19dc2c946fe4778fd6f21c7a12368ad3b836d8f1be48", diff --git a/absl/container/btree_test.cc b/absl/container/btree_test.cc index b6c11ea1..c2f098f0 100644 --- a/absl/container/btree_test.cc +++ b/absl/container/btree_test.cc @@ -1183,6 +1183,39 @@ TEST(Btree, RangeCtorSanity) { EXPECT_EQ(1, tmap.size()); } +} // namespace + +class BtreeNodePeer { + public: + // Yields the size of a leaf node with a specific number of values. + template + constexpr static size_t GetTargetNodeSize(size_t target_values_per_node) { + return btree_node< + set_params, std::allocator, + /*TargetNodeSize=*/256, // This parameter isn't used here. + /*Multi=*/false>>::SizeWithNValues(target_values_per_node); + } + + // Yields the number of values in a (non-root) leaf node for this btree. + template + constexpr static size_t GetNumValuesPerNode() { + return btree_node::kNodeValues; + } + + template + constexpr static size_t GetMaxFieldType() { + return std::numeric_limits< + typename btree_node::field_type>::max(); + } + + template + constexpr static bool UsesLinearNodeSearch() { + return btree_node::use_linear_search::value; + } +}; + +namespace { + TEST(Btree, BtreeMapCanHoldMoveOnlyTypes) { absl::btree_map> m; @@ -1328,34 +1361,6 @@ TEST(Btree, RValueInsert) { EXPECT_EQ(tracker.swaps(), 0); } -} // namespace - -class BtreeNodePeer { - public: - // Yields the size of a leaf node with a specific number of values. - template - constexpr static size_t GetTargetNodeSize(size_t target_values_per_node) { - return btree_node< - set_params, std::allocator, - /*TargetNodeSize=*/256, // This parameter isn't used here. - /*Multi=*/false>>::SizeWithNValues(target_values_per_node); - } - - // Yields the number of values in a (non-root) leaf node for this set. - template - constexpr static size_t GetNumValuesPerNode() { - return btree_node::kNodeValues; - } - - template - constexpr static size_t GetMaxFieldType() { - return std::numeric_limits< - typename btree_node::field_type>::max(); - } -}; - -namespace { - // A btree set with a specific number of values per node. template > class SizedBtreeSet diff --git a/absl/container/internal/btree.h b/absl/container/internal/btree.h index 55d69572..cadbeab1 100644 --- a/absl/container/internal/btree.h +++ b/absl/container/internal/btree.h @@ -803,12 +803,6 @@ class btree_node { // Deletes a node and all of its children. static void clear_and_delete(btree_node *node, allocator_type *alloc); - public: - // Exposed only for tests. - static bool testonly_uses_linear_node_search() { - return use_linear_search::value; - } - private: template void value_init(const field_type i, allocator_type *alloc, Args &&... args) { @@ -1496,13 +1490,6 @@ class btree { return res; } - public: - // Exposed only for tests. - static bool testonly_uses_linear_node_search() { - return node_type::testonly_uses_linear_node_search(); - } - - private: // We use compressed tuple in order to save space because key_compare and // allocator_type are usually empty. absl::container_internal::CompressedTuple class btree_map_container : public btree_set_container { using super_type = btree_set_container; using params_type = typename Tree::params_type; + friend class BtreeNodePeer; private: template diff --git a/ci/cmake_common.sh b/ci/cmake_common.sh new file mode 100755 index 00000000..15ffde2e --- /dev/null +++ b/ci/cmake_common.sh @@ -0,0 +1,25 @@ +# Copyright 2020 The Abseil Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# The commit of GoogleTest to be used in the CMake tests in this directory. +# Keep this in sync with the commit in the WORKSPACE file. +readonly ABSL_GOOGLETEST_COMMIT="8567b09290fe402cf01923e2131c5635b8ed851b" + +# Avoid depending on GitHub by looking for a cached copy of the commit first. +if [[ -r "${KOKORO_GFILE_DIR}/distdir/${ABSL_GOOGLETEST_COMMIT}.zip" ]]; then + DOCKER_EXTRA_ARGS="--mount type=bind,source=${KOKORO_GFILE_DIR}/distdir,target=/distdir,readonly ${DOCKER_EXTRA_ARGS:-}" + ABSL_GOOGLETEST_DOWNLOAD_URL="file:///distdir/${ABSL_GOOGLETEST_COMMIT}.zip" +else + ABSL_GOOGLETEST_DOWNLOAD_URL="https://github.com/google/googletest/archive/${ABSL_GOOGLETEST_COMMIT}.zip" +fi diff --git a/ci/cmake_install_test.sh b/ci/cmake_install_test.sh index 78eb41d9..5bf540c5 100755 --- a/ci/cmake_install_test.sh +++ b/ci/cmake_install_test.sh @@ -20,6 +20,8 @@ if [[ -z ${ABSEIL_ROOT:-} ]]; then ABSEIL_ROOT="$(realpath $(dirname ${0})/..)" fi +source "${ABSEIL_ROOT}/ci/cmake_common.sh" + source "${ABSEIL_ROOT}/ci/linux_docker_containers.sh" readonly DOCKER_CONTAINER=${LINUX_GCC_LATEST_CONTAINER} diff --git a/ci/linux_gcc-latest_libstdcxx_cmake.sh b/ci/linux_gcc-latest_libstdcxx_cmake.sh index 1bf5fdab..ed9cfa38 100755 --- a/ci/linux_gcc-latest_libstdcxx_cmake.sh +++ b/ci/linux_gcc-latest_libstdcxx_cmake.sh @@ -14,18 +14,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -# TODO(absl-team): This script isn't fully hermetic because -# -DABSL_USE_GOOGLETEST_HEAD=ON means that this script isn't pinned to a fixed -# version of GoogleTest. This means that an upstream change to GoogleTest could -# break this test. Fix this by allowing this script to pin to a known-good -# version of GoogleTest. - set -euox pipefail if [[ -z ${ABSEIL_ROOT:-} ]]; then ABSEIL_ROOT="$(realpath $(dirname ${0})/..)" fi +source "${ABSEIL_ROOT}/ci/cmake_common.sh" + if [[ -z ${ABSL_CMAKE_CXX_STANDARDS:-} ]]; then ABSL_CMAKE_CXX_STANDARDS="11 14 17 20" fi @@ -46,17 +42,17 @@ for std in ${ABSL_CMAKE_CXX_STANDARDS}; do for build_shared in ${ABSL_CMAKE_BUILD_SHARED}; do time docker run \ --mount type=bind,source="${ABSEIL_ROOT}",target=/abseil-cpp,readonly \ - --workdir=/abseil-cpp \ --tmpfs=/buildfs:exec \ + --workdir=/buildfs \ --cap-add=SYS_PTRACE \ --rm \ -e CFLAGS="-Werror" \ -e CXXFLAGS="-Werror" \ - ${DOCKER_CONTAINER} \ + ${DOCKER_EXTRA_ARGS:-} \ + "${DOCKER_CONTAINER}" \ /bin/bash -c " - cd /buildfs && \ cmake /abseil-cpp \ - -DABSL_USE_GOOGLETEST_HEAD=ON \ + -DABSL_GOOGLETEST_DOWNLOAD_URL=${ABSL_GOOGLETEST_DOWNLOAD_URL} \ -DABSL_RUN_TESTS=ON \ -DBUILD_SHARED_LIBS=${build_shared} \ -DCMAKE_BUILD_TYPE=${compilation_mode} \ diff --git a/ci/linux_gcc_alpine_cmake.sh b/ci/linux_gcc_alpine_cmake.sh index c8f173da..31310ac7 100755 --- a/ci/linux_gcc_alpine_cmake.sh +++ b/ci/linux_gcc_alpine_cmake.sh @@ -14,18 +14,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -# TODO(absl-team): This script isn't fully hermetic because -# -DABSL_USE_GOOGLETEST_HEAD=ON means that this script isn't pinned to a fixed -# version of GoogleTest. This means that an upstream change to GoogleTest could -# break this test. Fix this by allowing this script to pin to a known-good -# version of GoogleTest. - set -euox pipefail if [[ -z ${ABSEIL_ROOT:-} ]]; then ABSEIL_ROOT="$(realpath $(dirname ${0})/..)" fi +source "${ABSEIL_ROOT}/ci/cmake_common.sh" + if [[ -z ${ABSL_CMAKE_CXX_STANDARDS:-} ]]; then ABSL_CMAKE_CXX_STANDARDS="11 14 17" fi @@ -46,17 +42,17 @@ for std in ${ABSL_CMAKE_CXX_STANDARDS}; do for build_shared in ${ABSL_CMAKE_BUILD_SHARED}; do time docker run \ --mount type=bind,source="${ABSEIL_ROOT}",target=/abseil-cpp,readonly \ - --workdir=/abseil-cpp \ --tmpfs=/buildfs:exec \ + --workdir=/buildfs \ --cap-add=SYS_PTRACE \ --rm \ -e CFLAGS="-Werror" \ -e CXXFLAGS="-Werror" \ + ${DOCKER_EXTRA_ARGS:-} \ "${DOCKER_CONTAINER}" \ /bin/sh -c " - cd /buildfs && \ cmake /abseil-cpp \ - -DABSL_USE_GOOGLETEST_HEAD=ON \ + -DABSL_GOOGLETEST_DOWNLOAD_URL=${ABSL_GOOGLETEST_DOWNLOAD_URL} \ -DABSL_RUN_TESTS=ON \ -DCMAKE_BUILD_TYPE=${compilation_mode} \ -DCMAKE_CXX_STANDARD=${std} \ diff --git a/ci/macos_xcode_cmake.sh b/ci/macos_xcode_cmake.sh index d90e273a..0847b3ea 100755 --- a/ci/macos_xcode_cmake.sh +++ b/ci/macos_xcode_cmake.sh @@ -14,9 +14,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -# This script is invoked on Kokoro to test Abseil on macOS. -# It is not hermetic and may break when Kokoro is updated. - set -euox pipefail if [[ -z ${ABSEIL_ROOT:-} ]]; then @@ -24,6 +21,13 @@ if [[ -z ${ABSEIL_ROOT:-} ]]; then fi ABSEIL_ROOT=$(realpath ${ABSEIL_ROOT}) +source "${ABSEIL_ROOT}/ci/cmake_common.sh" + +# The MacOS build doesn't run in a docker container, so we have to override ABSL_GOOGLETEST_DOWNLOAD_URL. +if [[ -r "${KOKORO_GFILE_DIR}/distdir/${ABSL_GOOGLETEST_COMMIT}.zip" ]]; then + ABSL_GOOGLETEST_DOWNLOAD_URL="file://${KOKORO_GFILE_DIR}/distdir/${ABSL_GOOGLETEST_COMMIT}.zip" +fi + if [[ -z ${ABSL_CMAKE_BUILD_TYPES:-} ]]; then ABSL_CMAKE_BUILD_TYPES="Debug" fi @@ -44,7 +48,7 @@ for compilation_mode in ${ABSL_CMAKE_BUILD_TYPES}; do -DCMAKE_BUILD_TYPE=${compilation_mode} \ -DCMAKE_CXX_STANDARD=11 \ -DCMAKE_MODULE_LINKER_FLAGS="-Wl,--no-undefined" \ - -DABSL_USE_GOOGLETEST_HEAD=ON \ + -DABSL_GOOGLETEST_DOWNLOAD_URL="${ABSL_GOOGLETEST_DOWNLOAD_URL}" \ -DABSL_RUN_TESTS=ON time cmake --build . time ctest -C ${compilation_mode} --output-on-failure -- cgit v1.2.3