Commit 85226e35 authored by Takafumi Kubota's avatar Takafumi Kubota

Unity Builds: Do more detailed property checks

This patch increases unity-build opportunities.
Currently, the source files that have properties are simply excluded, even if
they have the same properties.
Here, I apply unity builds to the source file that can be bundled
together.
parent b4315cd2
Pipeline #195098 waiting for manual action with stages
in 3 minutes and 34 seconds
......@@ -2879,40 +2879,67 @@ void cmLocalGenerator::AddUnityBuild(cmGeneratorTarget* target)
cmProp unityMode = target->GetProperty("UNITY_BUILD_MODE");
for (std::string lang : { "C", "CXX" }) {
std::vector<cmSourceFile*> filtered_sources;
std::copy_if(sources.begin(), sources.end(),
std::back_inserter(filtered_sources), [&](cmSourceFile* sf) {
return sf->GetLanguage() == lang &&
!sf->GetPropertyAsBool("SKIP_UNITY_BUILD_INCLUSION") &&
!sf->GetPropertyAsBool("HEADER_FILE_ONLY") &&
!sf->GetProperty("COMPILE_OPTIONS") &&
!sf->GetProperty("COMPILE_DEFINITIONS") &&
!sf->GetProperty("COMPILE_FLAGS") &&
!sf->GetProperty("INCLUDE_DIRECTORIES");
});
std::vector<std::string> unity_files;
if (!unityMode || *unityMode == "BATCH") {
unity_files =
AddUnityFilesModeAuto(target, lang, filtered_sources, beforeInclude,
afterInclude, filename_base, unityBatchSize);
} else if (unityMode && *unityMode == "GROUP") {
unity_files =
AddUnityFilesModeGroup(target, lang, filtered_sources, beforeInclude,
afterInclude, filename_base);
} else {
// unity mode is set to an unsupported value
std::string e("Invalid UNITY_BUILD_MODE value of " + *unityMode +
" assigned to target " + target->GetName() +
". Acceptable values are BATCH and GROUP.");
this->IssueMessage(MessageType::FATAL_ERROR, e);
std::unordered_map<std::string, std::vector<cmSourceFile*>>
filtered_source_map;
for (auto* sf : sources) {
if (sf->GetLanguage() != lang)
continue;
if (sf->GetPropertyAsBool("SKIP_UNITY_BUILD_INCLUSION") ||
sf->GetPropertyAsBool("HEADER_FILE_ONLY"))
continue;
std::string key = "";
if (sf->GetProperty("COMPILE_OPTIONS")) {
for (const auto& bt_str : sf->GetCompileOptions()) {
key += bt_str.Value;
}
}
if (auto* flag = sf->GetProperty("COMPILE_FLAGS")) {
key += *(flag);
}
if (sf->GetProperty("INCLUDE_DIRECTORIES")) {
for (const auto& bt_str : sf->GetIncludeDirectories()) {
key += bt_str.Value;
}
}
if (filtered_source_map.find(key) == filtered_source_map.end()) {
filtered_source_map[key] = { sf };
} else {
filtered_source_map[key].push_back(sf);
}
}
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", file.c_str());
for (auto& itr : filtered_source_map) {
auto& filtered_sources = itr.second;
std::vector<std::string> unity_files;
if (!unityMode || *unityMode == "BATCH") {
unity_files =
AddUnityFilesModeAuto(target, lang, filtered_sources, beforeInclude,
afterInclude, filename_base, unityBatchSize);
} else if (unityMode && *unityMode == "GROUP") {
unity_files =
AddUnityFilesModeGroup(target, lang, filtered_sources, beforeInclude,
afterInclude, filename_base);
} else {
// unity mode is set to an unsupported value
std::string e("Invalid UNITY_BUILD_MODE value of " + *unityMode +
" assigned to target " + target->GetName() +
". Acceptable values are BATCH and GROUP.");
this->IssueMessage(MessageType::FATAL_ERROR, e);
}
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", file.c_str());
}
}
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment