From 3542b19304d7d615e41378d62032a4bb34a8e056 Mon Sep 17 00:00:00 2001 From: cmlmakahts_ Date: Tue, 9 Sep 2025 20:42:10 +0800 Subject: [PATCH] precise build support root target Signed-off-by: cmlmakahts_ --- src/gn/precise/precise.cc | 76 ++++++++++++++++++++++++++++++++++----- src/gn/precise/precise.h | 2 +- src/gn/visibility.cc | 12 +++---- 3 files changed, 75 insertions(+), 15 deletions(-) diff --git a/src/gn/precise/precise.cc b/src/gn/precise/precise.cc index 8cb1f57..b09f945 100644 --- a/src/gn/precise/precise.cc +++ b/src/gn/precise/precise.cc @@ -40,6 +40,8 @@ static std::vector modifyGnFileList_; static std::vector modifyGnModuleList_; static std::vector ignoreList_; static std::vector maxRangeList_; +static std::unordered_set rootTargets_; +static std::unordered_map filter_cache; static bool ReadFile(base::FilePath path, std::string& content) { @@ -142,6 +144,13 @@ static void LoadTargetTypeList(const base::Value& list) } } +static void LoadRootTarget(const base::Value& list) +{ + for (const base::Value& value : list.GetList()) { + rootTargets_.insert(value.GetString()); + } +} + static std::map> modifyMap_ = { { "h_file", LoadHFileList }, { "c_file", LoadCFileList }, @@ -161,6 +170,7 @@ static std::map> conf { "modify_files_path", LoadModifyFilesPath }, { "precise_result_path", LoadPreciseResultPath }, { "precise_log_path", LoadPreciseLogPath }, + { "root_target", LoadRootTarget } }; static void LoadModifyList() @@ -469,7 +479,7 @@ bool PreciseManager::IsFirstRecord(const std::vector& result, const return false; } -void PreciseManager::PreciseSearch(const Node* node, std::vector& result, std::vector& log, +void PreciseManager::PreciseSearch(const Node* node, std::vector& result, std::vector& module_list, std::vector& log, bool forGn, int depth, int maxDepth) { Module* module = (Module* )node; @@ -491,6 +501,7 @@ void PreciseManager::PreciseSearch(const Node* node, std::vector& r && !IsIgnore(name) && IsInMaxRange(name)) { log.push_back("OK:" + name); result.push_back(name); + module_list.push_back(module); return; } @@ -499,7 +510,7 @@ void PreciseManager::PreciseSearch(const Node* node, std::vector& r const Item* itemParent = moduleParent->GetItem(); std::string nameParent = itemParent->label().GetUserVisibleName(false); log.push_back("Check Parent:" + nameParent + "->" + name); - PreciseSearch(parent, result, log, forGn, depth + 1, maxDepth); + PreciseSearch(parent, result, module_list, log, forGn, depth + 1, maxDepth); } } @@ -553,10 +564,54 @@ void PreciseManager::WritePreciseTargets(const std::vector& result, WriteFile(preciseResultPath_, resultInfo); } +bool CheckModuleRootTargets(Module* module, std::string origin_label, std::vector cache_list) +{ + if (module == nullptr) { + cache_list.clear(); + return false; + } + + std::string label = module->GetItem()->label().GetUserVisibleName(false); + if (rootTargets_.find(label) != rootTargets_.end() || filter_cache[label]) { + if (!cache_list.empty()) { + for (auto &label : cache_list) { + filter_cache[label] = true; + } + } + return true; + } + cache_list.push_back(label); + std::vector from_list = module->GetFromList(); + if (!from_list.empty()) { + for (Node* parent : module->GetFromList()) { + Module* moduleParent = (Module* )parent; + std::string parent_label = moduleParent->GetItem()->label().GetUserVisibleName(false); + return CheckModuleRootTargets(moduleParent, parent_label, cache_list); + } + } + cache_list.clear(); + return false; +} + +void FilterWithRootTargets(std::vector& result, std::vector& module_list) +{ + for (int i = result.size() - 1; i >= 0; --i) + { + Module* module = module_list[i]; + std::string label = module->GetItem()->label().GetUserVisibleName(false); + if (!CheckModuleRootTargets(module, label, {})) { + std::cout << "Delete target not from root targets:" << result[i] << "(index:" << i << ")" << std::endl; + result.erase(result.begin() + i); + module_list.erase(module_list.begin() + i); + } + } +} + void PreciseManager::GeneratPreciseTargets() { - std::cout << "GeneratPreciseTargets Begain." << std::endl; + std::cout << "GeneratPreciseTargets Begin." << std::endl; std::vector result; + std::vector module_list; std::vector log; log.push_back("Init Precise depth:" + std::to_string(hFileDepth_) + " " + std::to_string(cFileDepth_) + " " + std::to_string(gnFileDepth_) + " " + std::to_string(gnModuleDepth_)); @@ -571,18 +626,23 @@ void PreciseManager::GeneratPreciseTargets() if (CheckSourceInTarget(item)) { log.push_back("Hit C:"); - PreciseSearch(pair.second, result, log, false, 0, cFileDepth_); + PreciseSearch(pair.second, result, module_list, log, false, 0, cFileDepth_); } else if (CheckIncludeInTarget(item) || CheckPrivateConfigs(item) || CheckPublicConfigs(item) || CheckAllDepConfigs(item)) { log.push_back("Hit H:"); - PreciseSearch(pair.second, result, log, false, 0, hFileDepth_); + PreciseSearch(pair.second, result, module_list, log, false, 0, hFileDepth_); } else if (CheckModuleInGn(label)) { log.push_back("Hit GN:"); - PreciseSearch(pair.second, result, log, true, 0, gnFileDepth_); + PreciseSearch(pair.second, result, module_list, log, true, 0, gnFileDepth_); } else if (CheckModuleMatch(label)) { log.push_back("Hit Module:"); - PreciseSearch(pair.second, result, log, true, 0, gnModuleDepth_); + PreciseSearch(pair.second, result, module_list, log, true, 0, gnModuleDepth_); } } + if (!rootTargets_.empty()){ + std::cout << "Module target count:" << result.size() << std::endl; + FilterWithRootTargets(result, module_list); + std::cout << "Module target count filtered by root targets:" << result.size() << std::endl; + } WritePreciseTargets(result, log); -} \ No newline at end of file +} diff --git a/src/gn/precise/precise.h b/src/gn/precise/precise.h index f088306..83c778b 100644 --- a/src/gn/precise/precise.h +++ b/src/gn/precise/precise.h @@ -69,7 +69,7 @@ private: bool IsFirstRecord(const std::vector& result, const std::string& name); bool IsTargetTypeMatch(const Item* item); bool IsTestOnlyMatch(const Item* item); - void PreciseSearch(const Node* node, std::vector& result, std::vector& log, + void PreciseSearch(const Node* node, std::vector& result, std::vector& module_list, std::vector& log, bool forGn, int depth, int maxDepth); void WriteFile(const std::string& path, const std::string& info); void WritePreciseTargets(const std::vector& result, const std::vector& log); diff --git a/src/gn/visibility.cc b/src/gn/visibility.cc index 5cdc7f2..6111435 100644 --- a/src/gn/visibility.cc +++ b/src/gn/visibility.cc @@ -110,20 +110,20 @@ bool Visibility::CheckItemVisibility(const Item *from, const Item *to, bool is_e const OhosComponent *from_component = from->ohos_component(); const OhosComponent *to_component = to->ohos_component(); - PreciseManager* precisehManager = PreciseManager::GetInstance(); - if (precisehManager != nullptr) { - Node *fromNode = precisehManager->GetModule(from_label); + PreciseManager* preciseManager = PreciseManager::GetInstance(); + if (preciseManager != nullptr) { + Node *fromNode = preciseManager->GetModule(from_label); if (!fromNode) { fromNode = new Module(from_label, from_label, from); } - Node *toNode = precisehManager->GetModule(to_label); + Node *toNode = preciseManager->GetModule(to_label); if (!toNode) { toNode = new Module(to_label, to_label, to); } fromNode->AddTo(toNode); toNode->AddFrom(fromNode); - precisehManager->AddModule(from_label, fromNode); - precisehManager->AddModule(to_label, toNode); + preciseManager->AddModule(from_label, fromNode); + preciseManager->AddModule(to_label, toNode); } if ((from_component == nullptr) || (to_component == nullptr)) { -- Gitee