Link test to target being tested?

I have a project with multiple optional components and, sometimes, two of those components have code in common. So we have a structure like this

  • lib/common
  • src/comp1
  • src/comp2
  • src/comp3

With let’s say lib/common used by comp1 and comp2.

I don’t want to spend time building lib/common for a platform using only comp3. The solution seems to be to use the EXCLUDE_FROM_ALL directory property in lib/.
This would all be good, but I may have unit tests for lib/common and, since I’m now using EXCLUDE_FROM_ALL they are not being built.

So I want a way to build the tests under lib/common when something depending on the “main” target in lib/common is built.
I guess this could be

  • Declaring a target the “main directory target”. When that target build is triggered, all the other targets in that directory are also triggered / the effect of EXCLUDE_FROM_ALL disappears?
  • Having a way to say “this test is testing this other target” so that, when the target is built, the test is also built.

There is any way to handle this situation?