Skip to content

Conversation

chenshuo
Copy link
Contributor

@chenshuo chenshuo commented Mar 6, 2019

This allows a project to include both googletest and benchmark as top-level git submodule.

This allows incorporating Benchmark to an existing CMake project that already incorporates googletest.
https://github.com/google/googletest/blob/master/googletest/README.md#incorporating-into-an-existing-cmake-project
https://github.com/abseil/abseil-cpp/tree/master/CMake#incorporating-abseil-into-a-cmake-project

@coveralls
Copy link

coveralls commented Mar 6, 2019

Coverage Status

Coverage increased (+0.004%) to 91.714% when pulling 017dfe5 on chenshuo:patch-1 into 823d246 on google:master.

# This allows a project to include both googletest and benchmark
# as top-level git submodule.
set(GTEST_BOTH_LIBRARIES gtest gmock gmock_main)
elseif (IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/googletest)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should happen when you've explicitly checked out googletest into benchmark/, but also have already defined the targets? It's not clear what version we should use.

Also, what happens the second time this CMake file is processed? AKA, the first pass we create the targets ourselves. The second pass we detect that the targets are present?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If gtest/gmock targets are already defined, we should not use benchmark/googletest. Otherwise CMake complains about duplicated targets, as it requires target names being globally unique [https://cmake.org/cmake/help/v3.9/policy/CMP0002.html].

CMake Error at benchmark/googletest/googletest/cmake/internal_utils.cmake:145 (add_library):
  add_library cannot create target "gtest" because another target with the
  same name already exists.  The existing target is a static library created
  in source directory
  "/home/schen/git/recipes/benchmark/googletest/googletest".
  See documentation for policy CMP0002 for more details.
Call Stack (most recent call first):
  benchmark/googletest/googletest/cmake/internal_utils.cmake:197 (cxx_library_with_type)
  benchmark/googletest/googletest/CMakeLists.txt:125 (cxx_library)

I don't quite understand how this file could be processed twice. It's only included by root/CMakeLists.txt. If root/CMakeLists.txt is added twice, I am pretty sure we will have the same duplicate targets error as above.

@LebedevRI
Copy link
Collaborator

I think this should be ok to have, but needs a rebase.

@LebedevRI
Copy link
Collaborator

In particular, the entirely of the patch should now simply be

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 856bc98..74b9934 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -267,7 +267,9 @@ add_subdirectory(src)
 
 if (BENCHMARK_ENABLE_TESTING)
   enable_testing()
-  if (BENCHMARK_ENABLE_GTEST_TESTS)
+  if (BENCHMARK_ENABLE_GTEST_TESTS AND
+      NOT (TARGET gtest AND TARGET gtest_main AND
+           TARGET gmock AND TARGET gmock_main))
     include(GoogleTest)
   endif()
   add_subdirectory(test)
(

^ i'd merge that

@dmah42 dmah42 added the next-release PRs or Issues that should be included in the next release label May 1, 2019
This allows a project to include both googletest and benchmark as top-level git submodule.

This allows incorporating Benchmark to an existing CMake project that already incorporates googletest.
https://github.com/google/googletest/blob/master/googletest/README.md#incorporating-into-an-existing-cmake-project
https://github.com/abseil/abseil-cpp/tree/master/CMake#incorporating-abseil-into-a-cmake-project
@chenshuo
Copy link
Contributor Author

chenshuo commented May 1, 2019

Done rebasing.

Copy link
Collaborator

@LebedevRI LebedevRI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool

@LebedevRI LebedevRI merged commit 7d856b0 into google:master May 1, 2019
JBakamovic pushed a commit to JBakamovic/benchmark that referenced this pull request Sep 11, 2020
This allows a project to include both googletest and benchmark as top-level git submodule.

This allows incorporating Benchmark to an existing CMake project that already incorporates googletest.
https://github.com/google/googletest/blob/master/googletest/README.md#incorporating-into-an-existing-cmake-project
https://github.com/abseil/abseil-cpp/tree/master/CMake#incorporating-abseil-into-a-cmake-project
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes next-release PRs or Issues that should be included in the next release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants