1#!/bin/bash 2# 3# Copyright 2019 The Abseil Authors. 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# https://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17# "Unit" and integration tests for Absl CMake installation 18 19# TODO(absl-team): This script isn't fully hermetic because 20# -DABSL_USE_GOOGLETEST_HEAD=ON means that this script isn't pinned to a fixed 21# version of GoogleTest. This means that an upstream change to GoogleTest could 22# break this test. Fix this by allowing this script to pin to a known-good 23# version of GoogleTest. 24 25# Fail on any error. Treat unset variables an error. Print commands as executed. 26set -euox pipefail 27 28install_absl() { 29 pushd "${absl_build_dir}" 30 if [[ "${#}" -eq 1 ]]; then 31 cmake -DCMAKE_INSTALL_PREFIX="${1}" "${absl_dir}" 32 else 33 cmake "${absl_dir}" 34 fi 35 cmake --build . --target install -- -j 36 popd 37} 38 39uninstall_absl() { 40 xargs rm < "${absl_build_dir}"/install_manifest.txt 41 rm -rf "${absl_build_dir}" 42 mkdir -p "${absl_build_dir}" 43} 44 45lts_install="" 46 47while getopts ":l" lts; do 48 case "${lts}" in 49 l ) 50 lts_install="true" 51 ;; 52 esac 53done 54 55absl_dir=/abseil-cpp 56absl_build_dir=/buildfs/absl-build 57project_dir="${absl_dir}"/CMake/install_test_project 58project_build_dir=/buildfs/project-build 59 60mkdir -p "${absl_build_dir}" 61mkdir -p "${project_build_dir}" 62 63if [[ "${lts_install}" ]]; then 64 install_dir="/usr/local" 65else 66 install_dir="${project_build_dir}"/install 67fi 68mkdir -p "${install_dir}" 69 70# Test build, install, and link against installed abseil 71pushd "${project_build_dir}" 72if [[ "${lts_install}" ]]; then 73 install_absl 74 cmake "${project_dir}" 75else 76 install_absl "${install_dir}" 77 cmake "${project_dir}" -DCMAKE_PREFIX_PATH="${install_dir}" 78fi 79 80cmake --build . --target simple 81 82output="$(${project_build_dir}/simple "printme" 2>&1)" 83if [[ "${output}" != *"Arg 1: printme"* ]]; then 84 echo "Faulty output on simple project:" 85 echo "${output}" 86 exit 1 87fi 88 89popd 90 91# Test that we haven't accidentally made absl::abslblah 92pushd "${install_dir}" 93 94# Starting in CMake 3.12 the default install dir is lib$bit_width 95if [[ -d lib64 ]]; then 96 libdir="lib64" 97elif [[ -d lib ]]; then 98 libdir="lib" 99else 100 echo "ls *, */*, */*/*:" 101 ls * 102 ls */* 103 ls */*/* 104 echo "unknown lib dir" 105fi 106 107if [[ "${lts_install}" ]]; then 108 # LTS versions append the date of the release to the subdir. 109 # 9999/99/99 is the dummy date used in the local_lts workflow. 110 absl_subdir="absl_99999999" 111else 112 absl_subdir="absl" 113fi 114 115if ! grep absl::strings "${libdir}/cmake/${absl_subdir}/abslTargets.cmake"; then 116 cat "${libdir}"/cmake/absl/abslTargets.cmake 117 echo "CMake targets named incorrectly" 118 exit 1 119fi 120 121pushd "${HOME}" 122cat > hello-abseil.cc << EOF 123#include <cstdlib> 124 125#include "absl/strings/str_format.h" 126 127int main(int argc, char **argv) { 128 absl::PrintF("Hello Abseil!\n"); 129 return EXIT_SUCCESS; 130} 131EOF 132export PKG_CONFIG_PATH="${install_dir}/${libdir}/pkgconfig" 133pc_args=($(pkg-config --cflags --libs --static absl_str_format)) 134g++ -static -o hello-abseil hello-abseil.cc "${pc_args[@]}" 135hello="$(./hello-abseil)" 136[[ "${hello}" == "Hello Abseil!" ]] 137popd 138 139uninstall_absl 140popd 141 142if [[ ! "${lts_install}" ]]; then 143 # Test that we warn if installed without a prefix or a system prefix 144 output="$(install_absl 2>&1)" 145 if [[ "${output}" != *"Please set CMAKE_INSTALL_PREFIX"* ]]; then 146 echo "Install without prefix didn't warn as expected. Output:" 147 echo "${output}" 148 exit 1 149 fi 150 uninstall_absl 151 152 output="$(install_absl /usr 2>&1)" 153 if [[ "${output}" != *"Please set CMAKE_INSTALL_PREFIX"* ]]; then 154 echo "Install with /usr didn't warn as expected. Output:" 155 echo "${output}" 156 exit 1 157 fi 158 uninstall_absl 159fi 160 161echo "Install test complete!" 162exit 0 163