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