CMake Target Unity Builds

I’ve looked at the documentation, but it’s not obvious to me.

Our codebase has multiple libraries. A couple of them have a lot of source files and need to be updated often. Is it possible to enable a unity build just on specific targets?

So, if I have libraries A, B, and C part of this codebase. Can I have B be the only library to use a unity build?

There’s a target property for that, I believe it’s called UNITY_BUILD.

I think I might misunderstand how that property works. The documentation says to disable it if you want to disable unity builds even when a developer passes the unity option.

But, by default does CMake only perform a unity build based on targets?
For some reason, I had in my head it might combine the source for multiple targets. However, while typing this out I realize that decision wouldn’t make much sense at all.

I think you can also use that property to enable unity build for only some targets… But even if not, you could achieve the same effect by explicitly setting the UNITY_BUILD property to false for the targets you don’t want to build as unity.

Tried it. Got stuff to fix in our codebase first. There are a couple specific targets I wanted to try this on, but they are the ones that fail. I’m going to book mark this though. Thanks for the info.

1 Like

you may simply add this line for the target you want to build this way:

set_target_properties(my_target PROPERTIES UNITY_BUILD ON)