Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake target depends on installed target

Tags:

cmake

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!

like image 557
user1150609 Avatar asked Nov 08 '22 03:11

user1150609


1 Answers

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

like image 136
Iddo Avatar answered Dec 22 '22 14:12

Iddo