I work on a very large C++ project that takes 20-30 minutes to fully build with all cores enabled. The problem is that there are some very specific source files that take up an enormous amount of memory to build (around 5 GB), so what happens is that, whenever the build reaches those source files and tries to build them all at the same time, the memory usage skyrockets and if the machine doesn't have enough memory, the system starts swapping and the compilation considerably slows. However, if I try to reduce the amount of threads, the compilation run time could end up reaching a couple of hours or more -- and most of the source files don't really need that much memory to compile.
The ideal scenario would be: go on with the multi-process compilation, then fall back to a single compilation process once the offending files start to compile. So how do I tell CMake that I want a specific source file to be compiled without any other compilations in parallel?
EDIT: Linking is fine, the memory spike happens while compiling the offending source cpp files.