Browse Source

CPP: Update CMake rules for Google Test to not require a pre-compiled static

library but be able to build the library from source as current practice is.
Review URL: https://codereview.appspot.com/6602047
pull/567/head
Fredrik Roubert 13 years ago
committed by Mihaela Rosca
parent
commit
2e074af243
3 changed files with 52 additions and 26 deletions
  1. +3
    -1
      cpp/CMakeLists.txt
  2. +2
    -25
      tools/cpp/CMakeLists.txt
  3. +47
    -0
      tools/cpp/gtest.cmake

+ 3
- 1
cpp/CMakeLists.txt View File

@ -23,6 +23,8 @@ set (libphonenumber_VERSION_MINOR 1)
# Helper functions dealing with finding libraries and programs this library
# depends on.
include (../tools/cpp/gtest.cmake)
function (print_error DESCRIPTION FILE)
message (FATAL_ERROR
"Can't find ${DESCRIPTION}: can't locate ${FILE}. Please read the README.")
@ -82,7 +84,7 @@ if (NOT Boost_FOUND)
endif ()
include_directories (${Boost_INCLUDE_DIRS})
find_required_library (GTEST gtest/gtest.h gtest "Google Test framework")
find_or_build_gtest ()
if (${USE_RE2} STREQUAL "ON")
find_required_library (RE2 re2/re2.h re2 "Google RE2")


+ 2
- 25
tools/cpp/CMakeLists.txt View File

@ -21,32 +21,9 @@ project (generate_geocoding_data)
# Helper functions dealing with finding libraries and programs this library
# depends on.
function (print_error DESCRIPTION FILE)
message (FATAL_ERROR
"Can't find ${DESCRIPTION}: can't locate ${FILE}. Please read the README.")
endfunction ()
include (gtest.cmake)
# Find a library. If it has not been found, stop CMake with a fatal error
# message.
function (find_required_library NAME HEADER LIBRARY DESCRIPTION)
# Check the header.
find_path (${NAME}_INCLUDE_DIR ${HEADER})
set (INCLUDE_DIR ${${NAME}_INCLUDE_DIR})
if (${INCLUDE_DIR} STREQUAL "${INCLUDE_DIR}-NOTFOUND")
print_error (${DESCRIPTION} ${HEADER})
endif ()
include_directories (${INCLUDE_DIR})
# Check the binary.
find_library (${NAME}_LIB ${LIBRARY})
set (LIB ${NAME}_LIB)
if (${LIB} STREQUAL "${LIB}-NOTFOUND")
print_error (${DESCRIPTION} ${LIBRARY})
endif ()
endfunction (find_required_library)
find_required_library (GTEST gtest/gtest.h gtest "Google Test framework")
find_or_build_gtest ()
set (
SOURCES


+ 47
- 0
tools/cpp/gtest.cmake View File

@ -0,0 +1,47 @@
# Copyright (C) 2012 The Libphonenumber 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
#
# http://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.
# Author: Fredrik Roubert
# It used to be common to provide pre-compiled Google Test libraries, but this
# practice is not recommended and is increasingly rare today:
#
# http://code.google.com/p/googletest/wiki/FAQ#Why_is_it_not_recommended_to_install_a_pre-compiled_copy_of_Goog
#
# This helper function will either find a pre-compiled library or else find the
# source code and add a library target to build the library from source.
function (find_or_build_gtest)
# Find header files.
find_path (GTEST_INCLUDE_DIR gtest/gtest.h)
if (${GTEST_INCLUDE_DIR} STREQUAL "GTEST_INCLUDE_DIR-NOTFOUND")
message (FATAL_ERROR
"Can't find Google C++ Testing Framework: can't locate gtest/gtest.h. "
"Please read the README and also take a look at tools/cpp/gtest.cmake.")
endif ()
include_directories (${GTEST_INCLUDE_DIR})
# Check for a pre-compiled library.
find_library (GTEST_LIB gtest)
if (${GTEST_LIB} STREQUAL "GTEST_LIB-NOTFOUND")
# No pre-compiled library found, attempt building it from source.
find_path (GTEST_SOURCE_DIR src/gtest-all.cc
HINTS /usr/src/gtest /usr/local/src/gtest)
add_library (gtest STATIC ${GTEST_SOURCE_DIR}/src/gtest-all.cc)
include_directories (${GTEST_SOURCE_DIR})
set (GTEST_LIB gtest PARENT_SCOPE)
endif ()
endfunction ()

Loading…
Cancel
Save