media/webrtc/trunk/testing/gtest/cmake/internal_utils.cmake

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/webrtc/trunk/testing/gtest/cmake/internal_utils.cmake	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,227 @@
     1.4 +# Defines functions and macros useful for building Google Test and
     1.5 +# Google Mock.
     1.6 +#
     1.7 +# Note:
     1.8 +#
     1.9 +# - This file will be run twice when building Google Mock (once via
    1.10 +#   Google Test's CMakeLists.txt, and once via Google Mock's).
    1.11 +#   Therefore it shouldn't have any side effects other than defining
    1.12 +#   the functions and macros.
    1.13 +#
    1.14 +# - The functions/macros defined in this file may depend on Google
    1.15 +#   Test and Google Mock's option() definitions, and thus must be
    1.16 +#   called *after* the options have been defined.
    1.17 +
    1.18 +# Tweaks CMake's default compiler/linker settings to suit Google Test's needs.
    1.19 +#
    1.20 +# This must be a macro(), as inside a function string() can only
    1.21 +# update variables in the function scope.
    1.22 +macro(fix_default_compiler_settings_)
    1.23 +  if (MSVC)
    1.24 +    # For MSVC, CMake sets certain flags to defaults we want to override.
    1.25 +    # This replacement code is taken from sample in the CMake Wiki at
    1.26 +    # http://www.cmake.org/Wiki/CMake_FAQ#Dynamic_Replace.
    1.27 +    foreach (flag_var
    1.28 +             CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
    1.29 +             CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
    1.30 +      if (NOT BUILD_SHARED_LIBS AND NOT gtest_force_shared_crt)
    1.31 +        # When Google Test is built as a shared library, it should also use
    1.32 +        # shared runtime libraries.  Otherwise, it may end up with multiple
    1.33 +        # copies of runtime library data in different modules, resulting in
    1.34 +        # hard-to-find crashes. When it is built as a static library, it is
    1.35 +        # preferable to use CRT as static libraries, as we don't have to rely
    1.36 +        # on CRT DLLs being available. CMake always defaults to using shared
    1.37 +        # CRT libraries, so we override that default here.
    1.38 +        string(REPLACE "/MD" "-MT" ${flag_var} "${${flag_var}}")
    1.39 +      endif()
    1.40 +
    1.41 +      # We prefer more strict warning checking for building Google Test.
    1.42 +      # Replaces /W3 with /W4 in defaults.
    1.43 +      string(REPLACE "/W3" "-W4" ${flag_var} "${${flag_var}}")
    1.44 +    endforeach()
    1.45 +  endif()
    1.46 +endmacro()
    1.47 +
    1.48 +# Defines the compiler/linker flags used to build Google Test and
    1.49 +# Google Mock.  You can tweak these definitions to suit your need.  A
    1.50 +# variable's value is empty before it's explicitly assigned to.
    1.51 +macro(config_compiler_and_linker)
    1.52 +  if (NOT gtest_disable_pthreads)
    1.53 +    # Defines CMAKE_USE_PTHREADS_INIT and CMAKE_THREAD_LIBS_INIT.
    1.54 +    find_package(Threads)
    1.55 +  endif()
    1.56 +
    1.57 +  fix_default_compiler_settings_()
    1.58 +  if (MSVC)
    1.59 +    # Newlines inside flags variables break CMake's NMake generator.
    1.60 +    # TODO(vladl@google.com): Add -RTCs and -RTCu to debug builds.
    1.61 +    set(cxx_base_flags "-GS -W4 -WX -wd4127 -wd4251 -wd4275 -nologo -J -Zi")
    1.62 +    if (MSVC_VERSION LESS 1400)
    1.63 +      # Suppress spurious warnings MSVC 7.1 sometimes issues.
    1.64 +      # Forcing value to bool.
    1.65 +      set(cxx_base_flags "${cxx_base_flags} -wd4800")
    1.66 +      # Copy constructor and assignment operator could not be generated.
    1.67 +      set(cxx_base_flags "${cxx_base_flags} -wd4511 -wd4512")
    1.68 +      # Compatibility warnings not applicable to Google Test.
    1.69 +      # Resolved overload was found by argument-dependent lookup.
    1.70 +      set(cxx_base_flags "${cxx_base_flags} -wd4675")
    1.71 +    endif()
    1.72 +    set(cxx_base_flags "${cxx_base_flags} -D_UNICODE -DUNICODE -DWIN32 -D_WIN32")
    1.73 +    set(cxx_base_flags "${cxx_base_flags} -DSTRICT -DWIN32_LEAN_AND_MEAN")
    1.74 +    set(cxx_exception_flags "-EHsc -D_HAS_EXCEPTIONS=1")
    1.75 +    set(cxx_no_exception_flags "-D_HAS_EXCEPTIONS=0")
    1.76 +    set(cxx_no_rtti_flags "-GR-")
    1.77 +  elseif (CMAKE_COMPILER_IS_GNUCXX)
    1.78 +    set(cxx_base_flags "-Wall -Wshadow")
    1.79 +    set(cxx_exception_flags "-fexceptions")
    1.80 +    set(cxx_no_exception_flags "-fno-exceptions")
    1.81 +    # Until version 4.3.2, GCC doesn't define a macro to indicate
    1.82 +    # whether RTTI is enabled.  Therefore we define GTEST_HAS_RTTI
    1.83 +    # explicitly.
    1.84 +    set(cxx_no_rtti_flags "-fno-rtti -DGTEST_HAS_RTTI=0")
    1.85 +    set(cxx_strict_flags
    1.86 +      "-Wextra -Wno-unused-parameter -Wno-missing-field-initializers")
    1.87 +  elseif (CMAKE_CXX_COMPILER_ID STREQUAL "SunPro")
    1.88 +    set(cxx_exception_flags "-features=except")
    1.89 +    # Sun Pro doesn't provide macros to indicate whether exceptions and
    1.90 +    # RTTI are enabled, so we define GTEST_HAS_* explicitly.
    1.91 +    set(cxx_no_exception_flags "-features=no%except -DGTEST_HAS_EXCEPTIONS=0")
    1.92 +    set(cxx_no_rtti_flags "-features=no%rtti -DGTEST_HAS_RTTI=0")
    1.93 +  elseif (CMAKE_CXX_COMPILER_ID STREQUAL "VisualAge" OR
    1.94 +      CMAKE_CXX_COMPILER_ID STREQUAL "XL")
    1.95 +    # CMake 2.8 changes Visual Age's compiler ID to "XL".
    1.96 +    set(cxx_exception_flags "-qeh")
    1.97 +    set(cxx_no_exception_flags "-qnoeh")
    1.98 +    # Until version 9.0, Visual Age doesn't define a macro to indicate
    1.99 +    # whether RTTI is enabled.  Therefore we define GTEST_HAS_RTTI
   1.100 +    # explicitly.
   1.101 +    set(cxx_no_rtti_flags "-qnortti -DGTEST_HAS_RTTI=0")
   1.102 +  elseif (CMAKE_CXX_COMPILER_ID STREQUAL "HP")
   1.103 +    set(cxx_base_flags "-AA -mt")
   1.104 +    set(cxx_exception_flags "-DGTEST_HAS_EXCEPTIONS=1")
   1.105 +    set(cxx_no_exception_flags "+noeh -DGTEST_HAS_EXCEPTIONS=0")
   1.106 +    # RTTI can not be disabled in HP aCC compiler.
   1.107 +    set(cxx_no_rtti_flags "")
   1.108 +  endif()
   1.109 +
   1.110 +  if (CMAKE_USE_PTHREADS_INIT)  # The pthreads library is available and allowed.
   1.111 +    set(cxx_base_flags "${cxx_base_flags} -DGTEST_HAS_PTHREAD=1")
   1.112 +  else()
   1.113 +    set(cxx_base_flags "${cxx_base_flags} -DGTEST_HAS_PTHREAD=0")
   1.114 +  endif()
   1.115 +
   1.116 +  # For building gtest's own tests and samples.
   1.117 +  set(cxx_exception "${CMAKE_CXX_FLAGS} ${cxx_base_flags} ${cxx_exception_flags}")
   1.118 +  set(cxx_no_exception
   1.119 +    "${CMAKE_CXX_FLAGS} ${cxx_base_flags} ${cxx_no_exception_flags}")
   1.120 +  set(cxx_default "${cxx_exception}")
   1.121 +  set(cxx_no_rtti "${cxx_default} ${cxx_no_rtti_flags}")
   1.122 +  set(cxx_use_own_tuple "${cxx_default} -DGTEST_USE_OWN_TR1_TUPLE=1")
   1.123 +
   1.124 +  # For building the gtest libraries.
   1.125 +  set(cxx_strict "${cxx_default} ${cxx_strict_flags}")
   1.126 +endmacro()
   1.127 +
   1.128 +# Defines the gtest & gtest_main libraries.  User tests should link
   1.129 +# with one of them.
   1.130 +function(cxx_library_with_type name type cxx_flags)
   1.131 +  # type can be either STATIC or SHARED to denote a static or shared library.
   1.132 +  # ARGN refers to additional arguments after 'cxx_flags'.
   1.133 +  add_library(${name} ${type} ${ARGN})
   1.134 +  set_target_properties(${name}
   1.135 +    PROPERTIES
   1.136 +    COMPILE_FLAGS "${cxx_flags}")
   1.137 +  if (BUILD_SHARED_LIBS OR type STREQUAL "SHARED")
   1.138 +    set_target_properties(${name}
   1.139 +      PROPERTIES
   1.140 +      COMPILE_DEFINITIONS "GTEST_CREATE_SHARED_LIBRARY=1")
   1.141 +  endif()
   1.142 +  if (CMAKE_USE_PTHREADS_INIT)
   1.143 +    target_link_libraries(${name} ${CMAKE_THREAD_LIBS_INIT})
   1.144 +  endif()
   1.145 +endfunction()
   1.146 +
   1.147 +########################################################################
   1.148 +#
   1.149 +# Helper functions for creating build targets.
   1.150 +
   1.151 +function(cxx_shared_library name cxx_flags)
   1.152 +  cxx_library_with_type(${name} SHARED "${cxx_flags}" ${ARGN})
   1.153 +endfunction()
   1.154 +
   1.155 +function(cxx_library name cxx_flags)
   1.156 +  cxx_library_with_type(${name} "" "${cxx_flags}" ${ARGN})
   1.157 +endfunction()
   1.158 +
   1.159 +# cxx_executable_with_flags(name cxx_flags libs srcs...)
   1.160 +#
   1.161 +# creates a named C++ executable that depends on the given libraries and
   1.162 +# is built from the given source files with the given compiler flags.
   1.163 +function(cxx_executable_with_flags name cxx_flags libs)
   1.164 +  add_executable(${name} ${ARGN})
   1.165 +  if (cxx_flags)
   1.166 +    set_target_properties(${name}
   1.167 +      PROPERTIES
   1.168 +      COMPILE_FLAGS "${cxx_flags}")
   1.169 +  endif()
   1.170 +  if (BUILD_SHARED_LIBS)
   1.171 +    set_target_properties(${name}
   1.172 +      PROPERTIES
   1.173 +      COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1")
   1.174 +  endif()
   1.175 +  # To support mixing linking in static and dynamic libraries, link each
   1.176 +  # library in with an extra call to target_link_libraries.
   1.177 +  foreach (lib "${libs}")
   1.178 +    target_link_libraries(${name} ${lib})
   1.179 +  endforeach()
   1.180 +endfunction()
   1.181 +
   1.182 +# cxx_executable(name dir lib srcs...)
   1.183 +#
   1.184 +# creates a named target that depends on the given libs and is built
   1.185 +# from the given source files.  dir/name.cc is implicitly included in
   1.186 +# the source file list.
   1.187 +function(cxx_executable name dir libs)
   1.188 +  cxx_executable_with_flags(
   1.189 +    ${name} "${cxx_default}" "${libs}" "${dir}/${name}.cc" ${ARGN})
   1.190 +endfunction()
   1.191 +
   1.192 +# Sets PYTHONINTERP_FOUND and PYTHON_EXECUTABLE.
   1.193 +find_package(PythonInterp)
   1.194 +
   1.195 +# cxx_test_with_flags(name cxx_flags libs srcs...)
   1.196 +#
   1.197 +# creates a named C++ test that depends on the given libs and is built
   1.198 +# from the given source files with the given compiler flags.
   1.199 +function(cxx_test_with_flags name cxx_flags libs)
   1.200 +  cxx_executable_with_flags(${name} "${cxx_flags}" "${libs}" ${ARGN})
   1.201 +  add_test(${name} ${name})
   1.202 +endfunction()
   1.203 +
   1.204 +# cxx_test(name libs srcs...)
   1.205 +#
   1.206 +# creates a named test target that depends on the given libs and is
   1.207 +# built from the given source files.  Unlike cxx_test_with_flags,
   1.208 +# test/name.cc is already implicitly included in the source file list.
   1.209 +function(cxx_test name libs)
   1.210 +  cxx_test_with_flags("${name}" "${cxx_default}" "${libs}"
   1.211 +    "test/${name}.cc" ${ARGN})
   1.212 +endfunction()
   1.213 +
   1.214 +# py_test(name)
   1.215 +#
   1.216 +# creates a Python test with the given name whose main module is in
   1.217 +# test/name.py.  It does nothing if Python is not installed.
   1.218 +function(py_test name)
   1.219 +  # We are not supporting Python tests on Linux yet as they consider
   1.220 +  # all Linux environments to be google3 and try to use google3 features.
   1.221 +  if (PYTHONINTERP_FOUND)
   1.222 +    # ${CMAKE_BINARY_DIR} is known at configuration time, so we can
   1.223 +    # directly bind it from cmake. ${CTEST_CONFIGURATION_TYPE} is known
   1.224 +    # only at ctest runtime (by calling ctest -c <Configuration>), so
   1.225 +    # we have to escape $ to delay variable substitution here.
   1.226 +    add_test(${name}
   1.227 +      ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
   1.228 +          --build_dir=${CMAKE_CURRENT_BINARY_DIR}/\${CTEST_CONFIGURATION_TYPE})
   1.229 +  endif()
   1.230 +endfunction()

mercurial