In a CMake project:
I have a directory "A" for an external project. Its CMakeLists.txt file first attempts find_package (A-PKG ...).
If A-PKG_FOUND, it extracts some paths, and defines some variables according to what it found.
Else, it sets up and calls ExternalProject_Add (A-PRJ ...) to (try to) download, build, and install that project, and defines some variables according to what it built.
I now want directory "B" to configure variously, depending on the outcome of either finding or building "A" -- specifically, to set target_compile_definitions and target_include_directories in "B" depending on whether and what the build step produced in "A".
CMake dependencies seem to affect only the build sequence, not the configure sequence. Everything is configured before anything is built. So "B" is configured for the non-existence of "A"'s output. In the build step, "A" does produce something that "B" would use, but it's too late: "B" has already been configured to not rely on "A".
Of course, I could change the configuration of "B" to be optimistic and assume that "A" will successfully find or build something, but I'm trying to make this smarter. If "A" fails, or if we choose to not include "A", I would prefer to not have to manually change the configuration of "B" ("C", "D", ...)
I can force the result I want by touching "B"'s CMakeLists.txt file after the first configure & build cycle, to force B to reconfigure, and then "build" again. I have not found a way to force "A" to build before "B" configures.
What I have tried:
In the "A" CMakeLists.txt, in ExternalProject_Add (A-PRJ ...), I've set the BUILD_BYPRODUCTS, and STEP_TARGETS build install; I have an add_custom_target (A-TARG ALL DEPENDS A-PRJ-build ...); I've looked for and tried various ways to set dependencies. As far as I can tell, everything is configured before anything is built, and dependencies affect only the build order. I haven't found a way to make a configuration of one directory depend on the build of another.
I've tried setting an A_FOUND cache variable, but it gets set during "A"'s configuration before the thing in question actually exists. "B" then tries to include a header that doesn't exist yet, and the whole build dies. I guess there might be some clue lurking in there, because if the build of "B" properly depended on the build of "A", the header would have been there to include. But again, this would require the configuration "B" to be optimistic about the build of "A" producing that thing. I'm trying to avoid that optimism :-O And I want "B" to build whether or not "A" does, but to build differently, so a hard dependence of "B" on "A" (as implemented in the explicit CMake dependencies) doesn't quite seem to accomplish what I'm after.