'CMake one build directory for multiple projects with seperate context

I'm trying to build multiple projects within one build directory with the following structure:

  |------ CMakeLists.txt (The main Cmake)
  |
  |------ ProjectAPP
  |          |----- .c/h files
  |          |----- sdh_config.h
  |          |----- CMakeList.txt
  |
  |------ ProjectDFU
  |          |----- .c/h files
  |          |----- sdh_config.h
  |          |----- CMakeList.txt
  | 
  |-------- SDK
  |          |---- SDK used by both projects

The idea would be to build two independent projects, both built on top of one single SDK. Note that both projects rely on a different configuration of the SDK, done by their respective sdk_config.h.

The main CMakeList.txt looks like this:

cmake_minimum_required(VERSION 3.22)

project(project)

add_dependency(ProjectAPP)
add_dependency(ProjectDFU)

add_custom_target(app DEPENDS ${exec_target_app} 
    ...
)

add_custom_target(dfu DEPENDS ${exec_target_dfu} 
    ...
)

add_custom_target(merge DEPENDS app dfu
    ...
)

Basically my only use of building the two projects in the same place is that I'm then able to have targets depending on both executables so I can then do something with that.

My problem:

The SDK, cmake based, is fragmented in hundreds of small libraries like this:

add_library(lib INTERFACE
    "file1.c"
    "file2.c"
)

Most of theses libraries will be used by both projects but with different build parameters (cf. sdk_config.h).

Where I'm at right now, I'm getting the following error:

add_library cannot create target "lib" because another target with the same name already exists. 
The existing target is an interface library created in source directory "ProjectAPP". 
See documentation for policy CMP0002 for more details.

My question:

What would be the best way to isolate both projects in two different build contexts while still being able to have dependencies on each project at root level?

Thank you in advance for any help.



Solution 1:[1]

Answering my own question, I'm not sure if it'ss the best way to handle this but cmake ExternalProject solved my problem.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 will_tm