Unity: Refactor implementation to make it easier to extend

This commit is contained in:
Robert Maynard
2020-05-06 15:46:48 -04:00
parent 8406f71eae
commit b00585adcc

View File

@@ -2756,6 +2756,73 @@ void cmLocalGenerator::AddPchDependencies(cmGeneratorTarget* target)
}
}
namespace {
inline void RegisterUnitySources(cmGeneratorTarget* target, cmSourceFile* sf,
std::string const& filename)
{
target->AddSourceFileToUnityBatch(sf->ResolveFullPath());
sf->SetProperty("UNITY_SOURCE_FILE", filename.c_str());
}
inline void IncludeFileInUnitySources(cmGeneratedFileStream& unity_file,
std::string const& sf_full_path,
cmProp beforeInclude,
cmProp afterInclude)
{
if (beforeInclude) {
unity_file << *beforeInclude << "\n";
}
unity_file << "#include \"" << sf_full_path << "\"\n";
if (afterInclude) {
unity_file << *afterInclude << "\n";
}
}
std::vector<std::string> AddUnityFilesModeAuto(
cmGeneratorTarget* target, std::string const& lang,
std::vector<cmSourceFile*> const& filtered_sources, cmProp beforeInclude,
cmProp afterInclude, std::string const& filename_base, size_t batchSize)
{
if (batchSize == 0) {
batchSize = filtered_sources.size();
}
std::vector<std::string> unity_files;
for (size_t itemsLeft = filtered_sources.size(), chunk, batch = 0;
itemsLeft > 0; itemsLeft -= chunk, ++batch) {
chunk = std::min(itemsLeft, batchSize);
std::string filename = cmStrCat(filename_base, "unity_", batch,
(lang == "C") ? "_c.c" : "_cxx.cxx");
const std::string filename_tmp = cmStrCat(filename, ".tmp");
{
size_t begin = batch * batchSize;
size_t end = begin + chunk;
cmGeneratedFileStream file(
filename_tmp, false,
target->GetGlobalGenerator()->GetMakefileEncoding());
file << "/* generated by CMake */\n\n";
for (; begin != end; ++begin) {
cmSourceFile* sf = filtered_sources[begin];
RegisterUnitySources(target, sf, filename);
IncludeFileInUnitySources(file, sf->ResolveFullPath(), beforeInclude,
afterInclude);
}
}
cmSystemTools::MoveFileIfDifferent(filename_tmp, filename);
unity_files.emplace_back(std::move(filename));
}
return unity_files;
}
}
void cmLocalGenerator::AddUnityBuild(cmGeneratorTarget* target)
{
if (!target->GetPropertyAsBool("UNITY_BUILD")) {
@@ -2798,53 +2865,15 @@ void cmLocalGenerator::AddUnityBuild(cmGeneratorTarget* target)
!sf->GetProperty("INCLUDE_DIRECTORIES");
});
size_t batchSize = unityBatchSize;
if (unityBatchSize == 0) {
batchSize = filtered_sources.size();
}
std::vector<std::string> unity_files =
AddUnityFilesModeAuto(target, lang, filtered_sources, beforeInclude,
afterInclude, filename_base, unityBatchSize);
for (size_t itemsLeft = filtered_sources.size(), chunk, batch = 0;
itemsLeft > 0; itemsLeft -= chunk, ++batch) {
chunk = std::min(itemsLeft, batchSize);
std::string filename = cmStrCat(filename_base, "unity_", batch,
(lang == "C") ? "_c.c" : "_cxx.cxx");
const std::string filename_tmp = cmStrCat(filename, ".tmp");
{
size_t begin = batch * batchSize;
size_t end = begin + chunk;
cmGeneratedFileStream file(
filename_tmp, false,
this->GetGlobalGenerator()->GetMakefileEncoding());
file << "/* generated by CMake */\n\n";
for (; begin != end; ++begin) {
cmSourceFile* sf = filtered_sources[begin];
target->AddSourceFileToUnityBatch(sf->ResolveFullPath());
sf->SetProperty("UNITY_SOURCE_FILE", filename.c_str());
if (beforeInclude) {
file << *beforeInclude << "\n";
}
file << "#include \"" << sf->ResolveFullPath() << "\"\n";
if (afterInclude) {
file << *afterInclude << "\n";
}
}
}
cmSystemTools::MoveFileIfDifferent(filename_tmp, filename);
target->AddSource(filename, true);
auto unity = this->Makefile->GetOrCreateSource(filename);
for (auto const& file : unity_files) {
auto unity = this->GetMakefile()->GetOrCreateSource(file);
target->AddSource(file, true);
unity->SetProperty("SKIP_UNITY_BUILD_INCLUSION", "ON");
unity->SetProperty("UNITY_SOURCE_FILE", filename.c_str());
unity->SetProperty("UNITY_SOURCE_FILE", file.c_str());
}
}
}