I am a member of a small group of Chromium developers who are working on adding a unity build setup to Chromium, in order to reduce the project’s long and ever-increasing compile times. We’re calling these “jumbo” builds, because this term is not as overloaded as “unity”.
We’re slowly making progress, but find that a lot of our time is spent renaming things in anonymous namespaces- it would be much simpler if it was possible to automatically treat these as if they were file-local. Jens Widell has put together a proof-of-concept which appears to work reasonably well, it consists of a clang plugin and a small clang patch:
After building clang and the plugin, you generate jumbo source files that look like:
Then, you compile something like this:
clang++ -c jumbo_source_1.cc -Xclang -load -Xclang lib/JumboSupport.so -Xclang -add-plugin -Xclang jumbo-support
The plugin gives unique names to the anonymous namespaces without otherwise changing their semantics, and also #undef’s macros defined in each top-level source file before processing the next top-level source file. That way header files can still define macros that are used in multiple source files in the jumbo translation unit. Collisions between macros defined in header files and names used in other headers and other source files are still possible, but less likely.
To show how much these two changes help, here’s a patch to make Chromium’s network code build in jumbo mode:
https://chromium-review.googlesource.com/c/chromium/src/+/966523 (+352/-377 lines)
And here’s the corresponding patch using the proof-of-concept JumboSupport plugin:
https://chromium-review.googlesource.com/c/chromium/src/+/962062 (+53/-52 lines)
It seems clear that the version using the JumboSupport plugin would require less effort to create, review and merge into the codebase. We have a few other feature ideas, but these two changes seem to do most of the work for us.
So now we’re trying to figure out the best way forward- would a feature like this be welcome to the Clang project? And if so, how would you recommend that we go about it? We would prefer to do this in a way that does not require a locally patched Clang and could live with building a custom plugin, although implementing this entirely in Clang would be even better.
 If you’re not familiar with unity builds, the idea is to compile multiple source files per compiler invocation, reducing the overhead of processing header files (which can be surprisingly high). We do this by taking a list of the source files in a target and generating “jumbo” source files that #include multiple “real” source files, and then we feed these jumbo files to the compiler one at a time. This way, we don’t prevent the usage of valuable build tools like ccache and icecc that only support a single source file on the command line.
 Daniel Bratell has a summary of our progress jumbo-ifying the Chromium codebase here:
 The JumboSupport plugin assigns names to the anonymous namespaces in a given file: foo::(anonymous namespace)::bar is replaced with a symbol name of the form foo::_anonymous::bar where is unique to the file within the jumbo translation unit. Due to the internal linkage of these symbols, does not need to be unique across multiple object files/jumbo source files.