Use target gtest instead of GTEST_LIB and GTEST_INCLUDE_DIR

This simplifies usage of GoogleTest, since independent handling of
GTEST_LIB and GTEST_INCLUDE_DIR is not necessary anymore.

Additionally CMake creates a dependency now between target gtest and all
test applications using it. This improves build process when building
GoogleTest from source code. When any test application is built,
GoogleTest library is automatically rebuilt if necessary now for
instance.
This commit is contained in:
Christian Gruber 2019-08-18 00:30:42 +02:00
parent 7e71a9724f
commit b303fc6d99
5 changed files with 12 additions and 12 deletions

View File

@ -139,13 +139,9 @@ function(gnc_gtest_configure)
"${GTEST_SRC_DIR}/src/gtest_main.cc"
"${GTEST_SRC_DIR}/src/gtest-all.cc"
PARENT_SCOPE)
set(GTEST_LIB "${CMAKE_BINARY_DIR}/common/test-core/libgtest.a" PARENT_SCOPE)
else()
find_library(GTEST_SHARED_LIB gtest)
find_library(GTEST_MAIN_LIB gtest_main)
if (GTEST_MAIN_LIB AND GTEST_SHARED_LIB)
set(GTEST_LIB "${GTEST_MAIN_LIB};${GTEST_SHARED_LIB}" PARENT_SCOPE)
endif()
endif()
if ((GTEST_SHARED_LIB OR GTEST_SRC_DIR) AND GTEST_INCLUDE_DIR)
set(THREADS_PREFER_PTHREAD_FLAG ON)

View File

@ -82,6 +82,13 @@ if (GTEST_SRC_DIR)
target_compile_options(gtest PRIVATE -Wno-missing-declarations)
endif()
target_include_directories(gtest PUBLIC ${GTEST_INCLUDE_DIR} ${GTEST_SRC_DIR})
else()
add_library(gtest UNKNOWN IMPORTED GLOBAL)
set_target_properties(gtest PROPERTIES
IMPORTED_LOCATION ${GTEST_SHARED_LIB}
INTERFACE_INCLUDE_DIRECTORIES ${GTEST_INCLUDE_DIR}
INTERFACE_LINK_LIBRARIES ${GTEST_MAIN_LIB}
)
endif()
if (GMOCK_SRC_DIR)
set (lib_gmock_SOURCES ${GMOCK_SRC})

View File

@ -12,11 +12,10 @@ set(CSV_IMP_TEST_LIBS gncmod-csv-import gncmod-engine test-core)
# This test does not run in Win32
if (NOT WIN32)
set(MODULEPATH ${CMAKE_SOURCE_DIR}/gnucash/import-export/csv-imp)
set(gtest_csv_imp_LIBS gncmod-csv-import ${GLIB2_LDFLAGS} ${GTEST_LIB})
set(gtest_csv_imp_LIBS gncmod-csv-import ${GLIB2_LDFLAGS} gtest)
set(gtest_csv_imp_INCLUDES
${MODULEPATH}
${CSV_IMP_TEST_INCLUDE_DIRS}
${GTEST_INCLUDE_DIR})
${CSV_IMP_TEST_INCLUDE_DIRS})
set(test_tokenizer_SOURCES
test-tokenizer.cpp)

View File

@ -33,14 +33,13 @@ set(gtest_core_utils_INCLUDES
${CMAKE_SOURCE_DIR}/common # for platform.h
${CMAKE_BINARY_DIR}/libgnucash/core-utils # for gncla-dir.h
${GLIB2_INCLUDE_DIRS}
${GTEST_INCLUDE_DIR}
)
set(gtest_core_utils_LIBS
${GLIB2_LDFLAGS}
${Boost_LIBRARIES}
${GTHREAD_LDFLAGS}
${GTEST_LIB})
gtest)
set(test_gnc_path_util_SOURCES
${MODULEPATH}/gnc-path.c

View File

@ -92,7 +92,7 @@ set(gtest_old_engine_LIBS
gncmod-engine
${GLIB2_LDFLAGS}
${Boost_LIBRARIES}
${GTEST_LIB})
gtest)
set(gtest_qof_LIBS
${GLIB2_LDFLAGS}
@ -101,7 +101,7 @@ set(gtest_qof_LIBS
${GTHREAD_LDFLAGS}
${ICU4C_I18N_LDFLAGS}
${Boost_LIBRARIES}
${GTEST_LIB})
gtest)
set(gtest_engine_INCLUDES
${MODULEPATH}
@ -109,7 +109,6 @@ set(gtest_engine_INCLUDES
${CMAKE_BINARY_DIR}/common # for config.h
${CMAKE_SOURCE_DIR}/common # for platform.h
${GLIB2_INCLUDE_DIRS}
${GTEST_INCLUDE_DIR}
../../../borrowed/libc
)