diff --git a/build/gen.py b/build/gen.py index 60142964616b6412a6449b412499b47d41b8d8f3..9e1a41bff1a86f0983063477cab1b2adb9f4da16 100755 --- a/build/gen.py +++ b/build/gen.py @@ -651,6 +651,7 @@ def WriteGNNinja(path, platform, host, options, args_list): 'src/gn/ninja_writer.cc', 'src/gn/ohos_components.cc', "src/gn/ohos_components_checker.cc", + "src/gn/ohos_components_mapping.cc", 'src/gn/ohos_variables.cc', 'src/gn/operators.cc', 'src/gn/output_conversion.cc', diff --git a/src/gn/functions.cc b/src/gn/functions.cc index 79aad15fe1ed4946c64aef382975d6e081cc7920..a6c033ce32d17101d00356d8649036ed4be2b18a 100644 --- a/src/gn/functions.cc +++ b/src/gn/functions.cc @@ -17,6 +17,7 @@ #include "gn/err.h" #include "gn/input_file.h" #include "gn/ohos_components_checker.h" +#include "gn/ohos_components_mapping.h" #include "gn/parse_node_value_adapter.h" #include "gn/parse_tree.h" #include "gn/pool.h" @@ -663,16 +664,33 @@ Value RunImport(Scope* scope, const SourceDir& input_dir = scope->GetSourceDir(); SourceFile import_file = input_dir.ResolveRelativeFile( args[0], err, scope->settings()->build_settings()->root_path_utf8()); + + const OhosComponentChecker *checker = OhosComponentChecker::getInstance(); + if (checker != nullptr) { + checker->CheckImportOther(function, scope->settings()->build_settings(), + input_dir.value(), import_file.value(), err); + } + + std::string mapping_file = ""; + const OhosComponentMapping *mapping = OhosComponentMapping::getInstance(); + if (mapping != nullptr) { + mapping_file = mapping->MappingImportOther(scope->settings()->build_settings(), + input_dir.value(), import_file.value()); + } + + if (mapping_file != "") { + SourceFile real_file(mapping_file); + scope->AddBuildDependencyFile(real_file); + if (!err->has_error()) { + scope->settings()->import_manager().DoImport(real_file, function, scope, err); + } + return Value(); + } scope->AddBuildDependencyFile(import_file); if (!err->has_error()) { scope->settings()->import_manager().DoImport(import_file, function, scope, err); } - const OhosComponentChecker *checker = OhosComponentChecker::getInstance(); - if (checker != nullptr) { - checker->CheckImportOther(function, scope->settings()->build_settings(), - input_dir.value(), import_file.value(), err); - } return Value(); } diff --git a/src/gn/ohos_components.cc b/src/gn/ohos_components.cc index ace5ad38acb48957dfc1783a15eac8e9566c9ec4..9d6b811ed434c29123cf1b3ce79202b17eefe948 100644 --- a/src/gn/ohos_components.cc +++ b/src/gn/ohos_components.cc @@ -17,6 +17,7 @@ #include "gn/innerapis_publicinfo_generator.h" #include "gn/ohos_components_checker.h" #include "gn/ohos_components_impl.h" +#include "gn/ohos_components_mapping.h" /** * Ohos Component API @@ -45,8 +46,18 @@ OhosComponent::OhosComponent(const char *name, const char *subsystem, const char void OhosComponent::addInnerApi(const std::string &name, const std::string &label) { - innerapi_names_[name] = label; - innerapi_labels_[label] = name; + std::string la = label; + size_t pos = label.find(":"); + if (pos > 0) { + if ((label[pos - 1]) == '/') { // some are like this : "//components/foo/libfoo/:libfoo" + unsigned long indexToRemove = pos - 1; + if (indexToRemove >= 0 && indexToRemove <= la.length()) { + la.erase(la.begin() + indexToRemove); + } + } + } + innerapi_names_[name] = la; + innerapi_labels_[la] = name; } @@ -97,38 +108,31 @@ bool OhosComponentsImpl::ReadBuildConfigFile(const std::string &build_dir, const return true; } -bool OhosComponentsImpl::LoadComponentSubsystemAndPaths(const std::string &paths, const std::string &override_map, - const std::string &subsystems, std::string &err_msg_out) +bool OhosComponentsImpl::LoadComponentInfo(const std::string &components_content, std::string &err_msg_out) { - const base::DictionaryValue *paths_dict; - - std::unique_ptr paths_value = base::JSONReader::ReadAndReturnError(paths, - base::JSONParserOptions::JSON_PARSE_RFC, nullptr, &err_msg_out, nullptr, nullptr); - if (!paths_value) { - return false; - } - if (!paths_value->GetAsDictionary(&paths_dict)) { - return false; - } - - const base::DictionaryValue *subsystems_dict; - std::unique_ptr subsystems_value = base::JSONReader::ReadAndReturnError(subsystems, + const base::DictionaryValue *components_dict; + std::unique_ptr components_value = base::JSONReader::ReadAndReturnError(components_content, base::JSONParserOptions::JSON_PARSE_RFC, nullptr, &err_msg_out, nullptr, nullptr); - if (!subsystems_value) { + if (!components_value) { return false; } - if (!subsystems_value->GetAsDictionary(&subsystems_dict)) { + if (!components_value->GetAsDictionary(&components_dict)) { return false; } - const base::Value *subsystem; - for (const auto com : paths_dict->DictItems()) { - subsystem = subsystems_dict->FindKey(com.first); - if (!subsystem) { + for (const auto com : components_dict->DictItems()) { + const base::Value *subsystem = com.second.FindKey("subsystem"); + const base::Value *path = com.second.FindKey("path"); + if (!subsystem || !path) { continue; } components_[com.first] = - new OhosComponent(com.first.c_str(), subsystem->GetString().c_str(), com.second.GetString().c_str()); + new OhosComponent(com.first.c_str(), subsystem->GetString().c_str(), path->GetString().c_str()); + const base::Value *innerapis = com.second.FindKey("innerapis"); + if (!innerapis) { + continue; + } + LoadInnerApi(com.first, innerapis->GetList()); } setupComponentsTree(); return true; @@ -259,91 +263,45 @@ void OhosComponentsImpl::setupComponentsTree() } } -void OhosComponentsImpl::LoadInnerApi(const base::DictionaryValue *innerapis) -{ - OhosComponent *component; - for (const auto kv : innerapis->DictItems()) { - for (const auto inner : kv.second.DictItems()) { - component = (OhosComponent *)GetComponentByName(kv.first); - if (!component) { - break; - } - for (const auto info : inner.second.DictItems()) { - if (info.first == "label") { - component->addInnerApi(inner.first, info.second.GetString()); - break; - } - } - for (const auto info : inner.second.DictItems()) { - if (info.first == "visibility") { - component->addInnerApiVisibility(inner.first, info.second.GetList()); - break; - } - } - } - } -} - -bool OhosComponentsImpl::LoadOhosInnerApis_(const std::string innerapi_content, std::string &err_msg_out) +void OhosComponentsImpl::LoadInnerApi(const std::string &component_name, const std::vector &innerapis) { - const base::DictionaryValue *innerapis_dict; - - std::unique_ptr innerapis = base::JSONReader::ReadAndReturnError(innerapi_content, - base::JSONParserOptions::JSON_PARSE_RFC, nullptr, &err_msg_out, nullptr, nullptr); - if (!innerapis) { - return false; + OhosComponent *component = (OhosComponent *)GetComponentByName(component_name); + if (!component) { + return; } - if (!innerapis->GetAsDictionary(&innerapis_dict)) { - return false; + for (const base::Value &kv : innerapis) { + const base::Value *label = kv.FindKey("label"); + const base::Value *name = kv.FindKey("name"); + + if (!label || !name) { + continue; + } + component->addInnerApi(name->GetString(), label->GetString()); + const base::Value *visibility = kv.FindKey("visibility"); + if (!visibility) { + continue; + } + component->addInnerApiVisibility(name->GetString(), visibility->GetList()); } - LoadInnerApi(innerapis_dict); - return true; } bool OhosComponentsImpl::LoadOhosComponents(const std::string &build_dir, const Value *enable, Err *err) { - const char *paths_file = "parts_info/parts_path_info.json"; - std::string paths_content; - if (!ReadBuildConfigFile(build_dir, paths_file, paths_content)) { - *err = Err(*enable, "Your .gn file has enabled \"ohos_components_support\", but " - "OpenHarmony build config file (" + - std::string(paths_file) + ") does not exists.\n"); - return false; - } - const char *subsystems_file = "parts_info/part_subsystem.json"; - std::string subsystems_content; - if (!ReadBuildConfigFile(build_dir, subsystems_file, subsystems_content)) { - *err = Err(*enable, "Your .gn file has enabled \"ohos_components_support\", but " - "OpenHarmony build config file (" + - std::string(subsystems_file) + ") does not exists.\n"); - return false; - } - const char *innerapis_file = "parts_info/inner_kits_info.json"; - std::string innerapis_content; - if (!ReadBuildConfigFile(build_dir, innerapis_file, innerapis_content)) { + const char *components_file = "parts_info/components.json"; + std::string components_content; + if (!ReadBuildConfigFile(build_dir, components_file, components_content)) { *err = Err(*enable, "Your .gn file has enabled \"ohos_components_support\", but " "OpenHarmony build config file (" + - std::string(innerapis_file) + ") does not exists.\n"); + std::string(components_file) + ") does not exists.\n"); return false; } - const char *override_file = "component_override_map.json"; - std::string override_map; - if (!ReadBuildConfigFile(build_dir, override_file, override_map)) { - override_map = EMPTY_INNERAPI; - } std::string err_msg_out; - if (!LoadComponentSubsystemAndPaths(paths_content, override_map, subsystems_content, err_msg_out)) { + if (!LoadComponentInfo(components_content, err_msg_out)) { *err = Err(*enable, "Your .gn file has enabled \"ohos_components_support\", but " "OpenHarmony build config file parsing failed:\n" + err_msg_out + "\n"); return false; } - if (!LoadOhosInnerApis_(innerapis_content, err_msg_out)) { - *err = Err(*enable, "Your .gn file has enabled \"ohos_components_support\", but " - "OpenHarmony build config file " + - std::string(innerapis_file) + " parsing failed:\n" + err_msg_out + "\n"); - return false; - } return true; } @@ -483,3 +441,20 @@ void OhosComponents::LoadOhosComponentsChecker(const std::string &build_dir, con InnerApiPublicInfoGenerator::Init(build_dir, checkType); return; } + +void OhosComponents::LoadOhosComponentsMapping(const Value *support, const Value *independent) +{ + if (!support) { + return; + } + if (!support->boolean_value()) { + return; + } + + if (!independent || !independent->boolean_value()) { + return; + } + + OhosComponentMapping::Init(); + return; +} diff --git a/src/gn/ohos_components.h b/src/gn/ohos_components.h index 29e7287ec733e63886cd40190b94f2ccbccfdc46..b25b83b62de39d0281f6162690b53c5bd42bc53a 100644 --- a/src/gn/ohos_components.h +++ b/src/gn/ohos_components.h @@ -75,6 +75,8 @@ public: void LoadOhosComponentsChecker(const std::string &build_dir, const Value *support, int checkType); + void LoadOhosComponentsMapping(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 f07b2852cc0962a8a675bb96b6bbb4c735ef7c81..b0adb66516086c55fb3b98c4fe8b38a5c95c8ad1 100644 --- a/src/gn/ohos_components_checker.cc +++ b/src/gn/ohos_components_checker.cc @@ -25,8 +25,6 @@ #include "gn/target.h" #include "gn/value.h" -namespace fs = std::filesystem; - static const std::string SCAN_RESULT_PATH = "scan_out"; static const std::string WHITELIST_PATH = "build/component_compilation_whitelist.json"; static std::vector all_deps_config_; @@ -37,6 +35,7 @@ static std::vector innerapi_not_declare_; static std::map> includes_absolute_deps_other_; static std::map> target_absolute_deps_other_; static std::map> import_other_; +static std::map> deps_not_lib_; OhosComponentChecker *OhosComponentChecker::instance_ = nullptr; @@ -57,8 +56,8 @@ static bool StartWith(const std::string &str, const std::string prefix) static void CreateScanOutDir(const std::string &dir) { - fs::path path(dir); - fs::create_directories(path); + base::FilePath path(dir); + base::CreateDirectory(path); return; } @@ -67,19 +66,14 @@ static void RemoveScanOutDir(const std::string& dir) if (access(dir.c_str(), F_OK) == -1) { return; } - for (auto& entry : fs::directory_iterator(dir)) { - if (entry.is_regular_file()) { - fs::remove(entry); - } else if (entry.is_directory()) { - RemoveScanOutDir(entry.path().string()); - } - } - fs::remove(dir); + base::FilePath path(dir); + base::DeleteFile(path, true); + return; } -static bool ReadBuildConfigFile(std::string &content) +static bool ReadBuildConfigFile(base::FilePath path, std::string &content) { - if (!base::ReadFileToString(base::FilePath(WHITELIST_PATH), &content)) { + if (!base::ReadFileToString(path, &content)) { return false; } return true; @@ -149,6 +143,15 @@ static void LoadImportOtherWhitelist(const base::Value &value) } } +static void LoadDepsNotLibWhitelist(const base::Value &value) +{ + for (auto info : value.DictItems()) { + for (const base::Value &value_tmp : info.second.GetList()) { + deps_not_lib_[info.first].push_back(value_tmp.GetString()); + } + } +} + static std::map> whitelist_map_ = { { "all_dependent_configs", LoadAllDepsConfigWhitelist }, { "includes_over_range", LoadIncludesOverRangeWhitelist }, @@ -157,13 +160,14 @@ static std::map> whit { "innerapi_public_deps_inner", LoadInnerApiPublicDepsInnerWhitelist }, { "includes_absolute_deps_other", LoadIncludesAbsoluteDepsOtherWhitelist }, { "target_absolute_deps_other", LoadAbsoluteDepsOtherWhitelist }, - { "import_other", LoadImportOtherWhitelist } + { "import_other", LoadImportOtherWhitelist }, + { "deps_not_lib", LoadDepsNotLibWhitelist } }; static void LoadWhitelist() { std::string whitelistContent; - if (!ReadBuildConfigFile(whitelistContent)) { + if (!ReadBuildConfigFile(base::FilePath(WHITELIST_PATH), whitelistContent)) { return; } const base::DictionaryValue *whitelist_dict; @@ -235,6 +239,21 @@ bool OhosComponentChecker::InterceptInnerApiNotLib(const Item *item, const std:: return false; } +bool OhosComponentChecker::InterceptDepsNotLib(const Item *item, const std::string label, + const std::string deps, Err *err) const +{ + if (auto res = deps_not_lib_.find(label); res != deps_not_lib_.end()) { + std::string deps_str(deps); + auto res_second = std::find(res->second.begin(), res->second.end(), Trim(deps_str)); + if (res_second != res->second.end()) { + return true; + } + } + *err = Err(item->defined_from(), "Depend a non-lib target.", + "The item " + label + " cannot depend on a non-lib target " + deps); + return false; +} + bool OhosComponentChecker::InterceptInnerApiNotDeclare(const Item *item, const std::string label, Err *err) const { auto result = std::find(innerapi_not_declare_.begin(), innerapi_not_declare_.end(), label); @@ -395,7 +414,7 @@ bool OhosComponentChecker::CheckInnerApiPublicDepsInner(const Target *target, co } bool OhosComponentChecker::CheckInnerApiNotLib(const Item *item, const OhosComponent *component, - const std::string label, Err *err) const + const std::string label, const std::string deps, Err *err) const { if (checkType_ <= CheckType::NONE || item == nullptr || item->AsTarget() == nullptr || (ignoreTest_ && item->testonly()) || component == nullptr) { @@ -408,9 +427,12 @@ bool OhosComponentChecker::CheckInnerApiNotLib(const Item *item, const OhosCompo } if (checkType_ >= CheckType::INTERCEPT_IGNORE_TEST) { - return InterceptInnerApiNotLib(item, label, err); + return InterceptDepsNotLib(item, label, deps, err) && InterceptInnerApiNotLib(item, deps, err); } - GenerateScanList("innerapi_not_lib.list", component->subsystem(), component->name(), label, ""); + + std::string type_str(Target::GetStringForOutputType(type)); + GenerateScanList("innerapi_not_lib.list", component->subsystem(), component->name(), deps, type_str); + GenerateScanList("deps_not_lib.list", component->subsystem(), component->name(), label, deps); return true; } @@ -496,9 +518,6 @@ bool OhosComponentChecker::CheckTargetAbsoluteDepsOther(const Item *item, const if (checkType_ <= CheckType::NONE || component == nullptr || item == nullptr || (ignoreTest_ && item->testonly())) { return true; } - if (!component->isInnerApi(deps)) { - return true; - } if (is_external_deps) { return true; diff --git a/src/gn/ohos_components_checker.h b/src/gn/ohos_components_checker.h index 423ba4d119950505fe1737c3677df7dd29f96ab7..92457724404c95859a145b0ad3f0574d2510171a 100644 --- a/src/gn/ohos_components_checker.h +++ b/src/gn/ohos_components_checker.h @@ -37,7 +37,8 @@ public: Err *err) const; bool CheckInnerApiPublicDepsInner(const Target *target, const std::string label, const std::string deps, Err *err) const; - bool CheckInnerApiNotLib(const Item *item, const OhosComponent *component, const std::string label, Err *err) const; + bool CheckInnerApiNotLib(const Item *item, const OhosComponent *component, const std::string label, + const std::string deps, Err *err) const; bool CheckInnerApiNotDeclare(const Item *item, const OhosComponent *component, const std::string label, Err *err) const; bool CheckIncludesAbsoluteDepsOther(const Target *target, const std::string label, const std::string includes, @@ -65,6 +66,7 @@ private: bool InterceptInnerApiPublicDepsInner(const Target *target, const std::string label, const std::string deps, Err *err) const; bool InterceptInnerApiNotLib(const Item *item, const std::string label, Err *err) const; + bool InterceptDepsNotLib(const Item *item, const std::string label, const std::string deps, Err *err) const; bool InterceptInnerApiNotDeclare(const Item *item, const std::string label, Err *err) const; bool InterceptIncludesAbsoluteDepsOther(const Target *target, const std::string label, const std::string includes, Err *err) const; diff --git a/src/gn/ohos_components_mapping.cc b/src/gn/ohos_components_mapping.cc new file mode 100644 index 0000000000000000000000000000000000000000..15a4319d636cfb809745275629134e16fc6972fe --- /dev/null +++ b/src/gn/ohos_components_mapping.cc @@ -0,0 +1,73 @@ +// Copyright (c) 2024 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "gn/ohos_components_mapping.h" + +#include +#include +#include +#include +#include + +#include "base/values.h" +#include "gn/build_settings.h" +#include "gn/config.h" +#include "gn/functions.h" +#include "gn/ohos_components.h" +#include "gn/parse_tree.h" +#include "gn/settings.h" +#include "gn/substitution_writer.h" +#include "gn/target.h" +#include "gn/value.h" + +OhosComponentMapping *OhosComponentMapping::instance_ = nullptr; + +static bool StartWith(const std::string &str, const std::string prefix) +{ + return (str.rfind(prefix, 0) == 0); +} + +const std::string OhosComponentMapping::MappingTargetAbsoluteDpes(const BuildSettings* build_settings, + const std::string label, const std::string deps) const +{ + if (build_settings == nullptr) { + return ""; + } + + const OhosComponent *component = build_settings->GetOhosComponent(label); + if (component == nullptr) { + return ""; + } + if (StartWith(deps, component->path())) { + return ""; + } + + const OhosComponent *deps_component = build_settings->GetOhosComponent(deps); + if (deps_component == nullptr) { + return ""; + } + + size_t pos = deps.find(":"); + if (pos <= 0) { + return ""; + } + return deps_component->getInnerApi(deps.substr(pos + 1)); +} + +const std::string OhosComponentMapping::MappingImportOther(const BuildSettings* build_settings, + const std::string label, const std::string deps) const +{ + if (build_settings == nullptr) { + return ""; + } + + const OhosComponent *component = build_settings->GetOhosComponent(label); + if (component == nullptr) { + return ""; + } + if (StartWith(deps, component->path()) || StartWith(deps, "//build/") || StartWith(deps, "//out/")) { + return ""; + } + return "//build/indep_configs/gni" + deps.substr(1); +} diff --git a/src/gn/ohos_components_mapping.h b/src/gn/ohos_components_mapping.h new file mode 100644 index 0000000000000000000000000000000000000000..fcfcf73c629c6fcf4c76f7b0dc34dde3e573b163 --- /dev/null +++ b/src/gn/ohos_components_mapping.h @@ -0,0 +1,43 @@ +// Copyright (c) 2024 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef OHOS_COMPONENTS_MAPPING_H_ +#define OHOS_COMPONENTS_MAPPING_H_ + +#include "gn/build_settings.h" +#include "gn/config.h" +#include "gn/functions.h" +#include "gn/parse_tree.h" +#include "gn/settings.h" +#include "gn/substitution_writer.h" +#include "gn/target.h" +#include "gn/value.h" + +class OhosComponentMapping { +public: + static void Init() + { + if (instance_ != nullptr) { + return; + } + instance_ = new OhosComponentMapping(); + } + + const std::string MappingTargetAbsoluteDpes(const BuildSettings* build_settings, + const std::string label, const std::string deps) const; + const std::string MappingImportOther(const BuildSettings* build_settings, + const std::string label, const std::string deps) const; + + static OhosComponentMapping *getInstance() + { + return instance_; + } + +private: + static OhosComponentMapping *instance_; + OhosComponentMapping() {} + OhosComponentMapping &operator = (const OhosComponentMapping &) = delete; +}; + +#endif // OHOS_COMPONENTS_MAPPING_H_ diff --git a/src/gn/setup.cc b/src/gn/setup.cc index c0c99a30d22783b8bc302d727651fb3e4369bea9..3988c605be06fbf1e85f966fd7ac6d40053bbd9e 100644 --- a/src/gn/setup.cc +++ b/src/gn/setup.cc @@ -413,13 +413,18 @@ bool Setup::FillOhosComponentsInfo(const std::string& build_dir, Err* err) if (ohos_components_.isOhosComponentsLoaded()) { build_settings_.SetOhosComponentsInfo(&ohos_components_); } - + const Value* checkType = build_settings_.build_args().GetArgOverride("ohos_components_checktype"); if (checkType && checkType->type() == Value::INTEGER) { ohos_components_.LoadOhosComponentsChecker(build_dir, support, checkType->int_value()); } else { ohos_components_.LoadOhosComponentsChecker(build_dir, support, 0); } + + const Value *independent = build_settings_.build_args().GetArgOverride("ohos_indep_compiler_enable"); + if (independent) { + ohos_components_.LoadOhosComponentsMapping(support, independent); + } return true; } diff --git a/src/gn/target_generator.cc b/src/gn/target_generator.cc index 2d3af051f2a2fd1d9d608242f3c5568431f5ac41..4383112d4ece50f4ec6cd7028e62717ef3d5f249 100644 --- a/src/gn/target_generator.cc +++ b/src/gn/target_generator.cc @@ -441,9 +441,8 @@ bool TargetGenerator::FillGenericDeps(const char* var_name, LabelTargetVector* dest) { const Value* value = scope_->GetValue(var_name, true); if (value) { - ExtractListOfLabels(scope_->settings()->build_settings(), *value, - scope_->GetSourceDir(), ToolchainLabelForScope(scope_), - dest, err_); + ExtractListOfLabelsMapping(target_->label().GetUserVisibleName(false), scope_->settings()->build_settings(), + *value, scope_->GetSourceDir(), ToolchainLabelForScope(scope_), dest, err_); } return !err_->has_error(); } diff --git a/src/gn/value_extractors.cc b/src/gn/value_extractors.cc index 4f268d483b1f5496538f96962d1791cf982e979d..0fb46e47446ccd63de27c88cd10dbd4fc5c48ec7 100644 --- a/src/gn/value_extractors.cc +++ b/src/gn/value_extractors.cc @@ -10,6 +10,7 @@ #include "gn/err.h" #include "gn/frameworks_utils.h" #include "gn/label.h" +#include "gn/ohos_components_mapping.h" #include "gn/source_dir.h" #include "gn/source_file.h" #include "gn/target.h" @@ -190,6 +191,46 @@ struct LabelResolver { const Label& current_toolchain; }; +// Fills the label part of a LabelPtrPair, if it is a cross-component dependency, mapping is required. +template +struct LabelPtrResolverMapping { + LabelPtrResolverMapping(const std::string &label_in, + const BuildSettings* build_settings_in, + const SourceDir& current_dir_in, + const Label& current_toolchain_in) + : label(label_in), + build_settings(build_settings_in), + current_dir(current_dir_in), + current_toolchain(current_toolchain_in) {} + bool operator()(const Value& v, LabelPtrPair* out, Err* err) const { + if (!v.VerifyTypeIs(Value::STRING, err)) { + return false; + } + + + std::string map_label = ""; + OhosComponentMapping *mapping = OhosComponentMapping::getInstance(); + if (mapping != nullptr) { + map_label = mapping->MappingTargetAbsoluteDpes(build_settings, label, v.string_value()); + } + if (map_label != "") { + Value map_dep(v.origin(), map_label); + out->label = Label::Resolve(current_dir, build_settings->root_path_utf8(), + current_toolchain, map_dep, err); + out->origin = map_dep.origin(); + } else { + out->label = Label::Resolve(current_dir, build_settings->root_path_utf8(), + current_toolchain, v, err); + out->origin = v.origin(); + } + return !err->has_error(); + } + const std::string &label; + const BuildSettings* build_settings; + const SourceDir& current_dir; + const Label& current_toolchain; +}; + // Fills the label part of a LabelPtrPair, leaving the pointer null. template struct LabelPtrResolver { @@ -311,6 +352,18 @@ bool ExtractListOfLabels(const BuildSettings* build_settings, LabelPtrResolver(build_settings, current_dir, current_toolchain)); } +bool ExtractListOfLabelsMapping(const std::string& label, + const BuildSettings* build_settings, + const Value& value, + const SourceDir& current_dir, + const Label& current_toolchain, + LabelTargetVector* dest, + Err* err) { + return ListValueExtractor( + value, dest, err, + LabelPtrResolverMapping(label, build_settings, current_dir, current_toolchain)); +} + bool ExtractListOfExternalDeps(const BuildSettings* build_settings, const Value& value, const SourceDir& current_dir, diff --git a/src/gn/value_extractors.h b/src/gn/value_extractors.h index 057fa7eb734bf773d28fee989293d991031896f1..5248aa0b57ac68354414a5b860f87963d50aedf9 100644 --- a/src/gn/value_extractors.h +++ b/src/gn/value_extractors.h @@ -56,6 +56,14 @@ bool ExtractListOfLabels(const BuildSettings* build_settings, LabelTargetVector* dest, Err* err); +bool ExtractListOfLabelsMapping(const std::string& label, + const BuildSettings* build_settings, + const Value& value, + const SourceDir& current_dir, + const Label& current_toolchain, + LabelTargetVector* dest, + Err* err); + bool ExtractListOfExternalDeps(const BuildSettings* build_settings, const Value& value, const SourceDir& current_dir, diff --git a/src/gn/visibility.cc b/src/gn/visibility.cc index 480c9937a7719a8f7a38968ddfbd4020e3120c7a..e03f283c6fdce3b56b116a1c0723bb8e6857dc0b 100644 --- a/src/gn/visibility.cc +++ b/src/gn/visibility.cc @@ -122,7 +122,7 @@ bool Visibility::CheckItemVisibility(const Item *from, const Item *to, bool is_e OhosComponentChecker *checker = OhosComponentChecker::getInstance(); if (checker != nullptr) { - if (!checker->CheckInnerApiNotLib(to, to_component, to_label, err) || + if (!checker->CheckInnerApiNotLib(to, to_component, from_label, to_label, err) || !checker->CheckInnerApiNotDeclare(to, to_component, to_label, err) || !checker->CheckTargetAbsoluteDepsOther(from, to_component, from_label, to_label, is_external_deps, err) || !checker->CheckInnerApiVisibilityDenied(from, to_component, from_label, to_label, err)) {