Blame googletest/cmake/internal_utils.cmake

Packit bd1cd8
# Defines functions and macros useful for building Google Test and
Packit bd1cd8
# Google Mock.
Packit bd1cd8
#
Packit bd1cd8
# Note:
Packit bd1cd8
#
Packit bd1cd8
# - This file will be run twice when building Google Mock (once via
Packit bd1cd8
#   Google Test's CMakeLists.txt, and once via Google Mock's).
Packit bd1cd8
#   Therefore it shouldn't have any side effects other than defining
Packit bd1cd8
#   the functions and macros.
Packit bd1cd8
#
Packit bd1cd8
# - The functions/macros defined in this file may depend on Google
Packit bd1cd8
#   Test and Google Mock's option() definitions, and thus must be
Packit bd1cd8
#   called *after* the options have been defined.
Packit bd1cd8
Packit bd1cd8
# Tweaks CMake's default compiler/linker settings to suit Google Test's needs.
Packit bd1cd8
#
Packit bd1cd8
# This must be a macro(), as inside a function string() can only
Packit bd1cd8
# update variables in the function scope.
Packit bd1cd8
macro(fix_default_compiler_settings_)
Packit bd1cd8
  if (MSVC)
Packit bd1cd8
    # For MSVC, CMake sets certain flags to defaults we want to override.
Packit bd1cd8
    # This replacement code is taken from sample in the CMake Wiki at
Packit bd1cd8
    # http://www.cmake.org/Wiki/CMake_FAQ#Dynamic_Replace.
Packit bd1cd8
    foreach (flag_var
Packit bd1cd8
             CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
Packit bd1cd8
             CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
Packit bd1cd8
      if (NOT BUILD_SHARED_LIBS AND NOT gtest_force_shared_crt)
Packit bd1cd8
        # When Google Test is built as a shared library, it should also use
Packit bd1cd8
        # shared runtime libraries.  Otherwise, it may end up with multiple
Packit bd1cd8
        # copies of runtime library data in different modules, resulting in
Packit bd1cd8
        # hard-to-find crashes. When it is built as a static library, it is
Packit bd1cd8
        # preferable to use CRT as static libraries, as we don't have to rely
Packit bd1cd8
        # on CRT DLLs being available. CMake always defaults to using shared
Packit bd1cd8
        # CRT libraries, so we override that default here.
Packit bd1cd8
        string(REPLACE "/MD" "-MT" ${flag_var} "${${flag_var}}")
Packit bd1cd8
      endif()
Packit bd1cd8
Packit bd1cd8
      # We prefer more strict warning checking for building Google Test.
Packit bd1cd8
      # Replaces /W3 with /W4 in defaults.
Packit bd1cd8
      string(REPLACE "/W3" "/W4" ${flag_var} "${${flag_var}}")
Packit bd1cd8
    endforeach()
Packit bd1cd8
  endif()
Packit bd1cd8
endmacro()
Packit bd1cd8
Packit bd1cd8
# Defines the compiler/linker flags used to build Google Test and
Packit bd1cd8
# Google Mock.  You can tweak these definitions to suit your need.  A
Packit bd1cd8
# variable's value is empty before it's explicitly assigned to.
Packit bd1cd8
macro(config_compiler_and_linker)
Packit bd1cd8
  if (NOT gtest_disable_pthreads)
Packit bd1cd8
    # Defines CMAKE_USE_PTHREADS_INIT and CMAKE_THREAD_LIBS_INIT.
Packit bd1cd8
    find_package(Threads)
Packit bd1cd8
  endif()
Packit bd1cd8
Packit bd1cd8
  fix_default_compiler_settings_()
Packit bd1cd8
  if (MSVC)
Packit bd1cd8
    # Newlines inside flags variables break CMake's NMake generator.
Packit bd1cd8
    # TODO(vladl@google.com): Add -RTCs and -RTCu to debug builds.
Packit bd1cd8
    set(cxx_base_flags "-GS -W4 -WX -wd4251 -wd4275 -nologo -J -Zi")
Packit bd1cd8
    if (MSVC_VERSION LESS 1400)  # 1400 is Visual Studio 2005
Packit bd1cd8
      # Suppress spurious warnings MSVC 7.1 sometimes issues.
Packit bd1cd8
      # Forcing value to bool.
Packit bd1cd8
      set(cxx_base_flags "${cxx_base_flags} -wd4800")
Packit bd1cd8
      # Copy constructor and assignment operator could not be generated.
Packit bd1cd8
      set(cxx_base_flags "${cxx_base_flags} -wd4511 -wd4512")
Packit bd1cd8
      # Compatibility warnings not applicable to Google Test.
Packit bd1cd8
      # Resolved overload was found by argument-dependent lookup.
Packit bd1cd8
      set(cxx_base_flags "${cxx_base_flags} -wd4675")
Packit bd1cd8
    endif()
Packit bd1cd8
    if (MSVC_VERSION LESS 1500)  # 1500 is Visual Studio 2008
Packit bd1cd8
      # Conditional expression is constant.
Packit bd1cd8
      # When compiling with /W4, we get several instances of C4127
Packit bd1cd8
      # (Conditional expression is constant). In our code, we disable that
Packit bd1cd8
      # warning on a case-by-case basis. However, on Visual Studio 2005,
Packit bd1cd8
      # the warning fires on std::list. Therefore on that compiler and earlier,
Packit bd1cd8
      # we disable the warning project-wide.
Packit bd1cd8
      set(cxx_base_flags "${cxx_base_flags} -wd4127")
Packit bd1cd8
    endif()
Packit bd1cd8
    if (NOT (MSVC_VERSION LESS 1700))  # 1700 is Visual Studio 2012.
Packit bd1cd8
      # Suppress "unreachable code" warning on VS 2012 and later.
Packit bd1cd8
      # http://stackoverflow.com/questions/3232669 explains the issue.
Packit bd1cd8
      set(cxx_base_flags "${cxx_base_flags} -wd4702")
Packit bd1cd8
    endif()
Packit bd1cd8
    if (NOT (MSVC_VERSION GREATER 1900))  # 1900 is Visual Studio 2015
Packit bd1cd8
      # BigObj required for tests.
Packit bd1cd8
      set(cxx_base_flags "${cxx_base_flags} -bigobj")
Packit bd1cd8
    endif()
Packit bd1cd8
Packit bd1cd8
    set(cxx_base_flags "${cxx_base_flags} -D_UNICODE -DUNICODE -DWIN32 -D_WIN32")
Packit bd1cd8
    set(cxx_base_flags "${cxx_base_flags} -DSTRICT -DWIN32_LEAN_AND_MEAN")
Packit bd1cd8
    set(cxx_exception_flags "-EHsc -D_HAS_EXCEPTIONS=1")
Packit bd1cd8
    set(cxx_no_exception_flags "-D_HAS_EXCEPTIONS=0")
Packit bd1cd8
    set(cxx_no_rtti_flags "-GR-")
Packit bd1cd8
  elseif (CMAKE_COMPILER_IS_GNUCXX)
Packit bd1cd8
    set(cxx_base_flags "-Wall -Wshadow")
Packit bd1cd8
    set(cxx_exception_flags "-fexceptions")
Packit bd1cd8
    set(cxx_no_exception_flags "-fno-exceptions")
Packit bd1cd8
    # Until version 4.3.2, GCC doesn't define a macro to indicate
Packit bd1cd8
    # whether RTTI is enabled.  Therefore we define GTEST_HAS_RTTI
Packit bd1cd8
    # explicitly.
Packit bd1cd8
    set(cxx_no_rtti_flags "-fno-rtti -DGTEST_HAS_RTTI=0")
Packit bd1cd8
    set(cxx_strict_flags
Packit bd1cd8
      "-Wextra -Wno-unused-parameter -Wno-missing-field-initializers")
Packit bd1cd8
  elseif (CMAKE_CXX_COMPILER_ID STREQUAL "SunPro")
Packit bd1cd8
    set(cxx_exception_flags "-features=except")
Packit bd1cd8
    # Sun Pro doesn't provide macros to indicate whether exceptions and
Packit bd1cd8
    # RTTI are enabled, so we define GTEST_HAS_* explicitly.
Packit bd1cd8
    set(cxx_no_exception_flags "-features=no%except -DGTEST_HAS_EXCEPTIONS=0")
Packit bd1cd8
    set(cxx_no_rtti_flags "-features=no%rtti -DGTEST_HAS_RTTI=0")
Packit bd1cd8
  elseif (CMAKE_CXX_COMPILER_ID STREQUAL "VisualAge" OR
Packit bd1cd8
      CMAKE_CXX_COMPILER_ID STREQUAL "XL")
Packit bd1cd8
    # CMake 2.8 changes Visual Age's compiler ID to "XL".
Packit bd1cd8
    set(cxx_exception_flags "-qeh")
Packit bd1cd8
    set(cxx_no_exception_flags "-qnoeh")
Packit bd1cd8
    # Until version 9.0, Visual Age doesn't define a macro to indicate
Packit bd1cd8
    # whether RTTI is enabled.  Therefore we define GTEST_HAS_RTTI
Packit bd1cd8
    # explicitly.
Packit bd1cd8
    set(cxx_no_rtti_flags "-qnortti -DGTEST_HAS_RTTI=0")
Packit bd1cd8
  elseif (CMAKE_CXX_COMPILER_ID STREQUAL "HP")
Packit bd1cd8
    set(cxx_base_flags "-AA -mt")
Packit bd1cd8
    set(cxx_exception_flags "-DGTEST_HAS_EXCEPTIONS=1")
Packit bd1cd8
    set(cxx_no_exception_flags "+noeh -DGTEST_HAS_EXCEPTIONS=0")
Packit bd1cd8
    # RTTI can not be disabled in HP aCC compiler.
Packit bd1cd8
    set(cxx_no_rtti_flags "")
Packit bd1cd8
  endif()
Packit bd1cd8
Packit bd1cd8
  if (CMAKE_USE_PTHREADS_INIT)  # The pthreads library is available and allowed.
Packit bd1cd8
    set(cxx_base_flags "${cxx_base_flags} -DGTEST_HAS_PTHREAD=1")
Packit bd1cd8
  else()
Packit bd1cd8
    set(cxx_base_flags "${cxx_base_flags} -DGTEST_HAS_PTHREAD=0")
Packit bd1cd8
  endif()
Packit bd1cd8
Packit bd1cd8
  # For building gtest's own tests and samples.
Packit bd1cd8
  set(cxx_exception "${CMAKE_CXX_FLAGS} ${cxx_base_flags} ${cxx_exception_flags}")
Packit bd1cd8
  set(cxx_no_exception
Packit bd1cd8
    "${CMAKE_CXX_FLAGS} ${cxx_base_flags} ${cxx_no_exception_flags}")
Packit bd1cd8
  set(cxx_default "${cxx_exception}")
Packit bd1cd8
  set(cxx_no_rtti "${cxx_default} ${cxx_no_rtti_flags}")
Packit bd1cd8
  set(cxx_use_own_tuple "${cxx_default} -DGTEST_USE_OWN_TR1_TUPLE=1")
Packit bd1cd8
Packit bd1cd8
  # For building the gtest libraries.
Packit bd1cd8
  set(cxx_strict "${cxx_default} ${cxx_strict_flags}")
Packit bd1cd8
endmacro()
Packit bd1cd8
Packit bd1cd8
# Defines the gtest & gtest_main libraries.  User tests should link
Packit bd1cd8
# with one of them.
Packit bd1cd8
function(cxx_library_with_type name type cxx_flags)
Packit bd1cd8
  # type can be either STATIC or SHARED to denote a static or shared library.
Packit bd1cd8
  # ARGN refers to additional arguments after 'cxx_flags'.
Packit bd1cd8
  add_library(${name} ${type} ${ARGN})
Packit bd1cd8
  set_target_properties(${name}
Packit bd1cd8
    PROPERTIES
Packit bd1cd8
    COMPILE_FLAGS "${cxx_flags}")
Packit bd1cd8
  if (BUILD_SHARED_LIBS OR type STREQUAL "SHARED")
Packit bd1cd8
    set_target_properties(${name}
Packit bd1cd8
      PROPERTIES
Packit bd1cd8
      COMPILE_DEFINITIONS "GTEST_CREATE_SHARED_LIBRARY=1")
Packit bd1cd8
  endif()
Packit bd1cd8
  if (CMAKE_USE_PTHREADS_INIT)
Packit bd1cd8
    target_link_libraries(${name} ${CMAKE_THREAD_LIBS_INIT})
Packit bd1cd8
  endif()
Packit bd1cd8
endfunction()
Packit bd1cd8
Packit bd1cd8
########################################################################
Packit bd1cd8
#
Packit bd1cd8
# Helper functions for creating build targets.
Packit bd1cd8
Packit bd1cd8
function(cxx_shared_library name cxx_flags)
Packit bd1cd8
  cxx_library_with_type(${name} SHARED "${cxx_flags}" ${ARGN})
Packit bd1cd8
endfunction()
Packit bd1cd8
Packit bd1cd8
function(cxx_library name cxx_flags)
Packit bd1cd8
  cxx_library_with_type(${name} "" "${cxx_flags}" ${ARGN})
Packit bd1cd8
endfunction()
Packit bd1cd8
Packit bd1cd8
# cxx_executable_with_flags(name cxx_flags libs srcs...)
Packit bd1cd8
#
Packit bd1cd8
# creates a named C++ executable that depends on the given libraries and
Packit bd1cd8
# is built from the given source files with the given compiler flags.
Packit bd1cd8
function(cxx_executable_with_flags name cxx_flags libs)
Packit bd1cd8
  add_executable(${name} ${ARGN})
Packit bd1cd8
  if (cxx_flags)
Packit bd1cd8
    set_target_properties(${name}
Packit bd1cd8
      PROPERTIES
Packit bd1cd8
      COMPILE_FLAGS "${cxx_flags}")
Packit bd1cd8
  endif()
Packit bd1cd8
  if (BUILD_SHARED_LIBS)
Packit bd1cd8
    set_target_properties(${name}
Packit bd1cd8
      PROPERTIES
Packit bd1cd8
      COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1")
Packit bd1cd8
  endif()
Packit bd1cd8
  # To support mixing linking in static and dynamic libraries, link each
Packit bd1cd8
  # library in with an extra call to target_link_libraries.
Packit bd1cd8
  foreach (lib "${libs}")
Packit bd1cd8
    target_link_libraries(${name} ${lib})
Packit bd1cd8
  endforeach()
Packit bd1cd8
endfunction()
Packit bd1cd8
Packit bd1cd8
# cxx_executable(name dir lib srcs...)
Packit bd1cd8
#
Packit bd1cd8
# creates a named target that depends on the given libs and is built
Packit bd1cd8
# from the given source files.  dir/name.cc is implicitly included in
Packit bd1cd8
# the source file list.
Packit bd1cd8
function(cxx_executable name dir libs)
Packit bd1cd8
  cxx_executable_with_flags(
Packit bd1cd8
    ${name} "${cxx_default}" "${libs}" "${dir}/${name}.cc" ${ARGN})
Packit bd1cd8
endfunction()
Packit bd1cd8
Packit bd1cd8
# Sets PYTHONINTERP_FOUND and PYTHON_EXECUTABLE.
Packit bd1cd8
find_package(PythonInterp)
Packit bd1cd8
Packit bd1cd8
# cxx_test_with_flags(name cxx_flags libs srcs...)
Packit bd1cd8
#
Packit bd1cd8
# creates a named C++ test that depends on the given libs and is built
Packit bd1cd8
# from the given source files with the given compiler flags.
Packit bd1cd8
function(cxx_test_with_flags name cxx_flags libs)
Packit bd1cd8
  cxx_executable_with_flags(${name} "${cxx_flags}" "${libs}" ${ARGN})
Packit bd1cd8
  add_test(${name} ${name})
Packit bd1cd8
endfunction()
Packit bd1cd8
Packit bd1cd8
# cxx_test(name libs srcs...)
Packit bd1cd8
#
Packit bd1cd8
# creates a named test target that depends on the given libs and is
Packit bd1cd8
# built from the given source files.  Unlike cxx_test_with_flags,
Packit bd1cd8
# test/name.cc is already implicitly included in the source file list.
Packit bd1cd8
function(cxx_test name libs)
Packit bd1cd8
  cxx_test_with_flags("${name}" "${cxx_default}" "${libs}"
Packit bd1cd8
    "test/${name}.cc" ${ARGN})
Packit bd1cd8
endfunction()
Packit bd1cd8
Packit bd1cd8
# py_test(name)
Packit bd1cd8
#
Packit bd1cd8
# creates a Python test with the given name whose main module is in
Packit bd1cd8
# test/name.py.  It does nothing if Python is not installed.
Packit bd1cd8
function(py_test name)
Packit bd1cd8
  # We are not supporting Python tests on Linux yet as they consider
Packit bd1cd8
  # all Linux environments to be google3 and try to use google3 features.
Packit bd1cd8
  if (PYTHONINTERP_FOUND)
Packit bd1cd8
    # ${CMAKE_BINARY_DIR} is known at configuration time, so we can
Packit bd1cd8
    # directly bind it from cmake. ${CTEST_CONFIGURATION_TYPE} is known
Packit bd1cd8
    # only at ctest runtime (by calling ctest -c <Configuration>), so
Packit bd1cd8
    # we have to escape $ to delay variable substitution here.
Packit bd1cd8
    if (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 3.1)
Packit bd1cd8
      add_test(
Packit bd1cd8
        NAME ${name}
Packit bd1cd8
        COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
Packit bd1cd8
            --build_dir=${CMAKE_CURRENT_BINARY_DIR}/$<CONFIGURATION>)
Packit bd1cd8
    else (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 3.1)
Packit bd1cd8
      add_test(
Packit bd1cd8
        ${name}
Packit bd1cd8
        ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
Packit bd1cd8
          --build_dir=${CMAKE_CURRENT_BINARY_DIR}/\${CTEST_CONFIGURATION_TYPE})
Packit bd1cd8
    endif (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 3.1)
Packit bd1cd8
  endif()
Packit bd1cd8
endfunction()