Is it possible to add_subdirectory() after another target has finished installing ( configure second subdirectory after first subdirectory is finished installing )

I tried add_custom_target with POST_BUILD and manually installing it there then adding a dependency to the library build by the second subdirectory on that target but it doesn’t do it and i can’t force it to not be configured until the first subdirectory is finished installing.

in the configuring/generating of the second subdirectory i need the executable from the first subdirectory to compile things in the second and also for find_package so configuring gives errors and i cant make it wait for installation. i’m not sure if there’s a way to make the add_subdirectory() wait for that. I tried externalproject but linking the libraries to my target doesn’t work for some reason and alot of variables don’t get set when passing them as -D in the externalproject_add() im not sure why.

Yes, it is i.e.:

# test if the targets are usable from the install directory
add_test(
    NAME find-package-test
    COMMAND
        ${CMAKE_CTEST_COMMAND}
        # --verbose
        --output-on-failure -C $<CONFIG> --build-and-test #
        "${CMAKE_CURRENT_SOURCE_DIR}/test-FindOpenSSL" "${CMAKE_CURRENT_BINARY_DIR}/find-package-test" #
        --build-generator ${CMAKE_GENERATOR} --build-makeprogram ${CMAKE_MAKE_PROGRAM} #
        --build-options #
        "-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}" #
        "-DCMAKE_C_STANDARD=${CMAKE_C_STANDARD}" #
        "-DCMAKE_BUILD_TYPE=$<CONFIG>" #
        "-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}" #
        "-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}"
)

Or as a post build command for target install like this:

if(ZLIB_DEBUG_POSTFIX)
    add_custom_command(
        TARGET openssl
        POST_BUILD
        COMMAND
            ${CMAKE_COMMAND} -E copy_if_different libssl${_CMAKE_LINK_LIBRARY_SUFFIX}
            libssl${ZLIB_DEBUG_POSTFIX}${_CMAKE_LINK_LIBRARY_SUFFIX}
        COMMAND
            ${CMAKE_COMMAND} -E copy_if_different libcrypto${_CMAKE_LINK_LIBRARY_SUFFIX}
            libcrypto${ZLIB_DEBUG_POSTFIX}${_CMAKE_LINK_LIBRARY_SUFFIX}
        VERBATIM
        WORKING_DIRECTORY ${OPENSSL_LIB_DIR}
    )
endif()

but how do i add_subdirectory() after the installation is done?

Not sure what you mean. add_subdirectory() happens at configure time (ie before the build), and installation happens after configure & build.

sorry for not properly explaining,
i have 2 subdirectories, 1 builds an executable needed by the second subdirectory so i want to configure the second subdirectory after the first subdirectory is installed so it can find the executable to use in configuration/generation.
im trying to make the question as general as possible but if it helps it’s protobuf and the executable is protoc

right now i have:

add_subdirectory(./proto)
add_custom_target(install_proto
    COMMAND ${CMAKE_COMMAND} -P instproto.cmake
    DEPENDS libprotobuf
)
add_custom_target(link_gns
    COMMAND ${CMAKE_COMMAND} -P addgns.cmake
    DEPENDS install_proto
)
add_dependancies(main linkgns)

instproto.cmake:include("./proto/cmake_install.cmake")
addgns.cmake:add_subdirectory(./gns)

but still it doesn’t wait

You cannot add_subdirectory from a script or a custom target. The way to solve this is to make the build/install of your libprotobuf execute during configure of the main project, such as via execute_process

thank you, execute process works every time. i also want to add ExternalProject works too but sometimes variables don’t get set when they are passed to the externalproject through CMAKE_ARGS without changing any files and i don’t know of a way to force variables like i would with set( [FORCE]) but for external project so im not sure why that happens.