6

With CMake I use add_subdirectory(gtest) to build gtest for testing my project. I also use CPack to make an installer.

The problem is the gtest repo (which is a git submodule) runs install() on some files so they end up in my installer! Obviously I don't want that. Is there a way to disable install() commands for a given subdirectory without just removing them from the CMakeLists?

languitar
  • 6,554
  • 2
  • 37
  • 62
Timmmm
  • 88,195
  • 71
  • 364
  • 509
  • Never having used gtest, so this might be a stupid question, but... why are you building a tool not directly related to your project *in* your project? Why don't you build / install gtest seperately and be done with it? (I wouldn't build Boost.Test inside my project either.) – DevSolar Feb 08 '17 at 16:51
  • It simplifies building the project & linking with cmake. If you build it in your tree using `add_directory` then you can just do `target_link_libraries(mytest gtest)` without having to faff around finding it. It's not super elegant but apart from this issue it works really well. – Timmmm Feb 08 '17 at 17:05
  • 1
    But the point is, you are using `add_subdirectory()` for something `find_package()` is intended for, found an issue, and now want `add_subdirectory()` to *not* do something it *is* intended for... I wanted to hint that "faffing around" with `find_package()` (which isn't that hard to do) and dropping the `add_subdirectory()` *would* solve your problem. ;-) – DevSolar Feb 08 '17 at 17:07
  • Yes but it would introduce other problems. I have found a reasonable workaround for this issue anyway - explicitly set `CPACK_COMPONENTS_ALL` which excludes the `Unspecified` component. – Timmmm Feb 08 '17 at 17:12
  • Ah that is a better solution. – Timmmm Feb 09 '17 at 11:14
  • Why not build gtest once and use this within your different projects? – Th. Thielemann Feb 09 '17 at 19:42
  • Well... I only have one project. But mainly because it simplifies setup if it is automatically downloaded, and also because this way imports the `gtest` target so you can just do `target_link_libraries(gtest)`. There's an `ExternalProject_Add` function that's meant to help but [it has issues](http://stackoverflow.com/q/15175318/265521). I think this is just one of the hacky areas of CMake (which is basically all of it but y'know...) – Timmmm Feb 10 '17 at 09:56

1 Answers1

6

You can use this to prevent Google Test from being installed:

add_directory(gtest EXCLUDE_FROM_ALL)
rdb
  • 1,488
  • 1
  • 14
  • 24
  • Correct. I [already discovered this](https://stackoverflow.com/a/43043052/265521) but for some reason didn't answer my own question... :-S – Timmmm Jun 07 '17 at 10:39