From c35207a4230fae1f4eab3a4787e64362e7ee6095 Mon Sep 17 00:00:00 2001 From: ring_king <1300547600@qq.com> Date: Sat, 17 Feb 2024 12:21:20 +0000 Subject: [PATCH 1/2] add overried components and modify gni mapping Signed-off-by: ring_king <1300547600@qq.com> --- src/gn/ohos_components.cc | 35 +++++++++++++++- src/gn/ohos_components.h | 2 +- src/gn/ohos_components_checker.cc | 18 ++++---- src/gn/ohos_components_impl.h | 4 ++ src/gn/ohos_components_mapping.cc | 68 ++++++++++++++++++++++++++++--- src/gn/ohos_components_mapping.h | 6 ++- src/gn/setup.cc | 2 +- 7 files changed, 117 insertions(+), 18 deletions(-) diff --git a/src/gn/ohos_components.cc b/src/gn/ohos_components.cc index 9d6b811..6d5ebcf 100644 --- a/src/gn/ohos_components.cc +++ b/src/gn/ohos_components.cc @@ -285,6 +285,24 @@ void OhosComponentsImpl::LoadInnerApi(const std::string &component_name, const s } } +void OhosComponentsImpl::LoadOverrideMap(const std::string &override_map) +{ + const base::DictionaryValue *override_dict; + std::unique_ptr override_value = base::JSONReader::ReadAndReturnError(override_map, + base::JSONParserOptions::JSON_PARSE_RFC, nullptr, nullptr, nullptr, nullptr); + if (!override_value) { + return; + } + if (!override_value->GetAsDictionary(&override_dict)) { + return; + } + + for (const auto com : override_dict->DictItems()) { + override_map_[com.first] = com.second.GetString(); + } + return; +} + bool OhosComponentsImpl::LoadOhosComponents(const std::string &build_dir, const Value *enable, Err *err) { const char *components_file = "parts_info/components.json"; @@ -295,6 +313,12 @@ bool OhosComponentsImpl::LoadOhosComponents(const std::string &build_dir, const std::string(components_file) + ") does not exists.\n"); return false; } + + std::string override_map; + if (ReadBuildConfigFile(build_dir, "component_override_map.json", override_map)) { + LoadOverrideMap(override_map); + } + std::string err_msg_out; if (!LoadComponentInfo(components_content, err_msg_out)) { *err = Err(*enable, "Your .gn file has enabled \"ohos_components_support\", but " @@ -324,6 +348,12 @@ bool OhosComponentsImpl::GetExternalDepsLabel(const Value &external_dep, std::st return false; } std::string component_name = str_val.substr(0, sep); + for (const auto& pair : override_map_) { + if (pair.first == component_name) { + component_name = pair.second; + break; + } + } const OhosComponent *component = GetComponentByName(component_name); if (component == nullptr) { *err = Err(external_dep, "OHOS component : (" + component_name + ") not found."); @@ -442,7 +472,8 @@ void OhosComponents::LoadOhosComponentsChecker(const std::string &build_dir, con return; } -void OhosComponents::LoadOhosComponentsMapping(const Value *support, const Value *independent) +void OhosComponents::LoadOhosComponentsMapping(const std::string& build_dir, + const Value *support, const Value *independent) { if (!support) { return; @@ -455,6 +486,6 @@ void OhosComponents::LoadOhosComponentsMapping(const Value *support, const Value return; } - OhosComponentMapping::Init(); + OhosComponentMapping::Init(build_dir); return; } diff --git a/src/gn/ohos_components.h b/src/gn/ohos_components.h index b25b83b..86ccf0c 100644 --- a/src/gn/ohos_components.h +++ b/src/gn/ohos_components.h @@ -75,7 +75,7 @@ public: void LoadOhosComponentsChecker(const std::string &build_dir, const Value *support, int checkType); - void LoadOhosComponentsMapping(const Value *support, const Value *independent); + void LoadOhosComponentsMapping(const std::string& build_dir, const Value *support, const Value *independent); private: OhosComponentsImpl *mgr = nullptr; diff --git a/src/gn/ohos_components_checker.cc b/src/gn/ohos_components_checker.cc index b0adb66..a93eb2b 100644 --- a/src/gn/ohos_components_checker.cc +++ b/src/gn/ohos_components_checker.cc @@ -26,7 +26,7 @@ #include "gn/value.h" static const std::string SCAN_RESULT_PATH = "scan_out"; -static const std::string WHITELIST_PATH = "build/component_compilation_whitelist.json"; +static const std::string WHITELIST_PATH = "component_compilation_whitelist.json"; static std::vector all_deps_config_; static std::vector includes_over_range_; static std::map> innerapi_public_deps_inner_; @@ -164,11 +164,13 @@ static std::map> whit { "deps_not_lib", LoadDepsNotLibWhitelist } }; -static void LoadWhitelist() +static void LoadWhitelist(const std::string &build_dir) { std::string whitelistContent; - if (!ReadBuildConfigFile(base::FilePath(WHITELIST_PATH), whitelistContent)) { - return; + if (!ReadBuildConfigFile(base::FilePath(build_dir + "/" + WHITELIST_PATH), whitelistContent)) { + if (!ReadBuildConfigFile(base::FilePath("build/" + WHITELIST_PATH), whitelistContent)) { + return; + } } const base::DictionaryValue *whitelist_dict; std::unique_ptr whitelist = base::JSONReader::ReadAndReturnError(whitelistContent, @@ -331,7 +333,7 @@ OhosComponentChecker::OhosComponentChecker(const std::string &build_dir, int che checkType_ = checkType; build_dir_ = build_dir; if (checkType_ == CheckType::INTERCEPT_IGNORE_TEST || checkType_ == CheckType::INTERCEPT_ALL) { - LoadWhitelist(); + LoadWhitelist(build_dir_); } if (checkType_ == CheckType::SCAN_ALL || checkType_ == CheckType::INTERCEPT_ALL) { ignoreTest_ = false; @@ -460,7 +462,8 @@ bool OhosComponentChecker::CheckIncludesAbsoluteDepsOther(const Target *target, return true; } - if (!StartWith(includes, "//") || StartWith(includes, "//out/") || StartWith(includes, "////out/")) { + if (!StartWith(includes, "//") || StartWith(includes, "//out/") || StartWith(includes, "////out/") + || StartWith(includes, "//prebuilts/")) { return true; } @@ -546,7 +549,8 @@ bool OhosComponentChecker::CheckImportOther(const FunctionCallNode* function, co if (component == nullptr) { return true; } - if (StartWith(deps, component->path()) || StartWith(deps, "//build/") || StartWith(deps, "//out/")) { + if (StartWith(deps, component->path()) || StartWith(deps, "//build/") || StartWith(deps, "//out/") + || StartWith(deps, "//prebuilts/")) { return true; } diff --git a/src/gn/ohos_components_impl.h b/src/gn/ohos_components_impl.h index 2cceae7..7f3a239 100644 --- a/src/gn/ohos_components_impl.h +++ b/src/gn/ohos_components_impl.h @@ -68,12 +68,16 @@ private: std::map components_; + std::map override_map_; + struct OhosComponentTree *pathTree = nullptr; void setupComponentsTree(); const struct OhosComponentTree *findChildByPath(const struct OhosComponentTree *current, const char *path, size_t len); void addComponentToTree(struct OhosComponentTree *current, OhosComponent *component); + void LoadOverrideMap(const std::string &override_map); + // For unittest public: const OhosComponent *matchComponentByLabel(const char *label); diff --git a/src/gn/ohos_components_mapping.cc b/src/gn/ohos_components_mapping.cc index 15a4319..bcb4c2c 100644 --- a/src/gn/ohos_components_mapping.cc +++ b/src/gn/ohos_components_mapping.cc @@ -10,9 +10,13 @@ #include #include +#include "base/files/file_path.h" +#include "base/files/file_util.h" +#include "base/json/json_reader.h" #include "base/values.h" #include "gn/build_settings.h" #include "gn/config.h" +#include "gn/filesystem_utils.h" #include "gn/functions.h" #include "gn/ohos_components.h" #include "gn/parse_tree.h" @@ -21,17 +25,71 @@ #include "gn/target.h" #include "gn/value.h" +static const std::string MAPPING_FILE_PATH = "component_mapping.json"; + OhosComponentMapping *OhosComponentMapping::instance_ = nullptr; +static std::map gni_mapping_file_map_; + static bool StartWith(const std::string &str, const std::string prefix) { return (str.rfind(prefix, 0) == 0); } +static bool ReadBuildConfigFile(base::FilePath path, std::string &content) +{ + if (!base::ReadFileToString(path, &content)) { + return false; + } + return true; +} + +static void LoadGniMappingFileMap(const base::Value &value) +{ + for (auto info : value.DictItems()) { + gni_mapping_file_map_[info.first] = info.second.GetString(); + } +} + +static std::map> mapping_map_ = { + { "gni_mapping_file", LoadGniMappingFileMap } +}; + +static void LoadMappingFile(const std::string &build_dir) +{ + std::string mappingContent; + if (!ReadBuildConfigFile(base::FilePath(build_dir + "/build_configs/" + MAPPING_FILE_PATH), mappingContent)) { + return; + } + const base::DictionaryValue *mapping_dict; + std::unique_ptr mapping = base::JSONReader::ReadAndReturnError(mappingContent, + base::JSONParserOptions::JSON_PARSE_RFC, nullptr, nullptr, nullptr, nullptr); + if (!mapping) { + return; + } + if (!mapping->GetAsDictionary(&mapping_dict)) { + return; + } + for (const auto kv : mapping_dict->DictItems()) { + auto iter = mapping_map_.find(kv.first); + if (iter != mapping_map_.end()) { + iter->second(kv.second); + } + } + return; +} + +OhosComponentMapping::OhosComponentMapping(const std::string &build_dir) +{ + build_dir_ = build_dir; + LoadMappingFile(build_dir); +} + const std::string OhosComponentMapping::MappingTargetAbsoluteDpes(const BuildSettings* build_settings, const std::string label, const std::string deps) const { - if (build_settings == nullptr) { + if (build_settings == nullptr || StartWith(deps, "//build/") || + StartWith(deps, "//out/") || StartWith(deps, "//prebuilts/")) { return ""; } @@ -58,16 +116,16 @@ const std::string OhosComponentMapping::MappingTargetAbsoluteDpes(const BuildSet const std::string OhosComponentMapping::MappingImportOther(const BuildSettings* build_settings, const std::string label, const std::string deps) const { - if (build_settings == nullptr) { + if (build_settings == nullptr || StartWith(deps, "//build/") || + StartWith(deps, "//out/") || StartWith(deps, "//prebuilts/")) { return ""; } - const OhosComponent *component = build_settings->GetOhosComponent(label); if (component == nullptr) { return ""; } - if (StartWith(deps, component->path()) || StartWith(deps, "//build/") || StartWith(deps, "//out/")) { + if (StartWith(deps, component->path())) { return ""; } - return "//build/indep_configs/gni" + deps.substr(1); + return gni_mapping_file_map_[deps]; } diff --git a/src/gn/ohos_components_mapping.h b/src/gn/ohos_components_mapping.h index fcfcf73..d144dcc 100644 --- a/src/gn/ohos_components_mapping.h +++ b/src/gn/ohos_components_mapping.h @@ -16,12 +16,12 @@ class OhosComponentMapping { public: - static void Init() + static void Init(const std::string& build_dir) { if (instance_ != nullptr) { return; } - instance_ = new OhosComponentMapping(); + instance_ = new OhosComponentMapping(build_dir); } const std::string MappingTargetAbsoluteDpes(const BuildSettings* build_settings, @@ -35,8 +35,10 @@ public: } private: + std::string build_dir_; static OhosComponentMapping *instance_; OhosComponentMapping() {} + OhosComponentMapping(const std::string &build_dir); OhosComponentMapping &operator = (const OhosComponentMapping &) = delete; }; diff --git a/src/gn/setup.cc b/src/gn/setup.cc index 3988c60..da4bb6f 100644 --- a/src/gn/setup.cc +++ b/src/gn/setup.cc @@ -423,7 +423,7 @@ bool Setup::FillOhosComponentsInfo(const std::string& build_dir, Err* err) const Value *independent = build_settings_.build_args().GetArgOverride("ohos_indep_compiler_enable"); if (independent) { - ohos_components_.LoadOhosComponentsMapping(support, independent); + ohos_components_.LoadOhosComponentsMapping(build_dir, support, independent); } return true; } -- Gitee From 046d8edb0adb645f82db41743dbba8d4dfd21d53 Mon Sep 17 00:00:00 2001 From: RingKing <1300547600@qq.com> Date: Sat, 17 Feb 2024 12:31:59 +0000 Subject: [PATCH 2/2] update src/gn/ohos_components.cc. Signed-off-by: RingKing <1300547600@qq.com> --- src/gn/ohos_components.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gn/ohos_components.cc b/src/gn/ohos_components.cc index 6d5ebcf..706439c 100644 --- a/src/gn/ohos_components.cc +++ b/src/gn/ohos_components.cc @@ -287,7 +287,7 @@ void OhosComponentsImpl::LoadInnerApi(const std::string &component_name, const s void OhosComponentsImpl::LoadOverrideMap(const std::string &override_map) { - const base::DictionaryValue *override_dict; + const base::DictionaryValue *override_dict; std::unique_ptr override_value = base::JSONReader::ReadAndReturnError(override_map, base::JSONParserOptions::JSON_PARSE_RFC, nullptr, nullptr, nullptr, nullptr); if (!override_value) { -- Gitee