From-source shared dependency

SuperProject consumes my project, MyProject, via CMake. Both projects have a common dependency CommonDep. CommonDep isn’t a CMake project, so MyProject provides a FindCommonDep.cmake that uses find_path/find_library to make sure CommonDep exists and is a usable version. Importantly, it respects the user-provided CommonDep_ROOT_DIR.

The problem is that SuperProject wants to build CommonDep from source and share it with MyProject. However, SuperProject is using external_project_add and setting CommonDep_ROOT_DIR to the respective directory in their build tree. This makes FindCommonDep.cmake fail because CommonDep isn’t available at config time even though CommonDep_ROOT_DIR is set.

I can’t find any suggestions on what to do in this case. I have considered

  1. Change MyProject to not perform find_path/find_library verification when CommonDep_ROOT_DIR is provided
  2. Update SuperProject to use FetchContent

I don’t like (1) because an invalid version of CommonDep will cause build failures instead of CMake errors (or will build and have wrong behavior). I also don’t like (2) because it’s intrusive to SuperProject.

Suggestions are very much welcome.