5

I have searched for this, but maybe I am using wrong wording; I want a CMake target to be built after another target is installed.

With a concrete example, I want my tests to include from and link with a version of the library, whose directory structure resembles actual install. Directory structure:

project
  lib
    first_library
      header1.hpp
      source1.cpp # this includes "first_library/header1.hpp"
    second_library
      header2.hpp
      source2.cpp # likewise, #include "second_library/header2.hpp"
  tests
    lib1_tests
      test1.cpp # this must include "first_library/header1.hpp"
    lib2_tests
      test2.cpp # likewise, #include "second_library/header2.hpp"

Neither of these worked for me. In the CMakeLists.txt in lib directory, I have:

add_library(lib1 STATIC ${lib1_SOURCES} ${lib1_HEADERS})
set_property(TARGET lib1 APPEND
    PROPERTY INCLUDE_DIRECTORIES "${CMAKE_CURRENT_LIST_DIR}/../")
install(TARGETS lib1 EXPORT lib1
    ARCHIVE DESTINATION lib
    INCLUDES DESTINATION include)
install(FILES ${lib1_HEADERS}
    DESTINATION include/my_lib_collection/first_library)

and tests have

add_executable(tests "${TEST_SOURCES}")
add_dependencies(tests lib1)
add_dependencies(tests lib2)
target_link_libraries(tests ${GTEST_BOTH_LIBRARIES}
    lib1 lib2)
target_include_directories(tests
    INTERFACE $<INSTALL_INTERFACE:INTERFACE_INCLUDE_DIRECTORIES:include/my_lib_collection>)
set_property(TARGET tests APPEND
    PROPERTY INCLUDE_DIRECTORIES ${GTEST_INCLUDE_DIRS})

Ultimately, what I want is a directory structure that is compatible with the installed state, and being able to use these while building tests.

Thanks!

user1150609
  • 347
  • 2
  • 10
  • You cannot install *particular* target - you may only **install whole project**l. And CMake doesn't provide target which express installation. However, you may perform installation as a part of other target, e.g. with `COMMAND cmake --build . --target install`. `Ultimately, what I want is a directory structure that is compatible with the installed state` - You may create that "install-compatible" structure within build directory during configure or build stage. Just copy header files into common directory. – Tsyvarev Jan 19 '17 at 21:14
  • Yes, you're right. I think I couldn't see the forest for the trees. I'll simply tune my flags to match my current directory structure, which isn't far away from the installed structure. Thanks for the tip! – user1150609 Jan 19 '17 at 23:19
  • 1
    Possible duplicate of [Postpone making custom target until install](http://stackoverflow.com/questions/8636479/postpone-making-custom-target-until-install) – arrowd Jan 20 '17 at 07:22

1 Answers1

1

I was facing the same problem you are facing now. can't say I found a text-book solution, but I did manage to work around it

I had multiple sub directories with a CmakeList.txt file for each of them.

there was one branch that all the rest of the branched depended on it being installed what I did was using add_custom_target (instead of add_subdirectory) and the commands in that custom target where the command lines for building the subdirectory

SHR
  • 7,940
  • 9
  • 38
  • 57
Iddo
  • 31
  • 3