add specified rule interception switch

https://gitee.com/openharmony/third_party_gn/issues/IA66ZN
Signed-off-by: hosted <chenyadong18@huawei.com>
This commit is contained in:
hosted 2024-06-18 10:06:36 +08:00
parent b2a2179b20
commit 6b9d3c5277
5 changed files with 91 additions and 11 deletions

View File

@ -517,7 +517,8 @@ const OhosComponent *OhosComponents::GetComponentByLabel(const std::string &labe
return mgr->matchComponentByLabel(label.c_str());
}
void OhosComponents::LoadOhosComponentsChecker(const std::string &build_dir, const Value *support, int checkType)
void OhosComponents::LoadOhosComponentsChecker(const std::string &build_dir, const Value *support, int checkType,
unsigned int ruleSwitch)
{
if (!support) {
return;
@ -531,7 +532,7 @@ void OhosComponents::LoadOhosComponentsChecker(const std::string &build_dir, con
InstallInfoGenerator::Init(build_dir, 0);
return;
}
OhosComponentChecker::Init(build_dir, checkType);
OhosComponentChecker::Init(build_dir, checkType, ruleSwitch);
InnerApiPublicInfoGenerator::Init(build_dir, checkType);
InstallInfoGenerator::Init(build_dir, checkType);
return;

View File

@ -73,7 +73,8 @@ public:
const OhosComponent *GetComponentByLabel(const std::string &label) const;
void LoadOhosComponentsChecker(const std::string &build_dir, const Value *support, int checkType);
void LoadOhosComponentsChecker(const std::string &build_dir, const Value *support, int checkType,
unsigned int ruleSwitch);
void LoadOhosComponentsMapping(const std::string& build_dir, const Value *support, const Value *independent);

View File

@ -26,6 +26,7 @@
static const std::string SCAN_RESULT_PATH = "scan_out";
static const std::string WHITELIST_PATH = "component_compilation_whitelist.json";
static const int BASE_BINARY = 1;
static std::vector<std::string> all_deps_config_;
static std::vector<std::string> includes_over_range_;
static std::map<std::string, std::vector<std::string>> innerapi_public_deps_inner_;
@ -201,8 +202,20 @@ static void LoadWhitelist(const std::string &build_dir)
return;
}
static bool IsIntercept(unsigned int switchValue, int shiftLeftNum)
{
if ((switchValue & (BASE_BINARY << (shiftLeftNum - 1))) != 0) {
return true;
}
return false;
}
bool OhosComponentChecker::InterceptAllDepsConfig(const Target *target, const std::string label, Err *err) const
{
if (!IsIntercept(ruleSwitch_, ALL_DEPS_CONFIG_BINARY)) {
return true;
}
auto result = std::find(all_deps_config_.begin(), all_deps_config_.end(), label);
if (result != all_deps_config_.end()) {
return true;
@ -216,6 +229,10 @@ bool OhosComponentChecker::InterceptAllDepsConfig(const Target *target, const st
bool OhosComponentChecker::InterceptIncludesOverRange(const Target *target, const std::string label,
const std::string dir, Err *err) const
{
if (!IsIntercept(ruleSwitch_, INCLUDE_OVER_RANGE_BINARY)) {
return true;
}
auto result = std::find(includes_over_range_.begin(), includes_over_range_.end(), label);
if (result != includes_over_range_.end()) {
return true;
@ -228,6 +245,10 @@ bool OhosComponentChecker::InterceptIncludesOverRange(const Target *target, cons
bool OhosComponentChecker::InterceptInnerApiPublicDepsInner(const Target *target, const std::string label,
const std::string deps, Err *err) const
{
if (!IsIntercept(ruleSwitch_, INNERAPI_PUBLIC_DEPS_INNER_BINARY)) {
return true;
}
if (auto res = innerapi_public_deps_inner_.find(label); res != innerapi_public_deps_inner_.end()) {
std::string deps_str(deps);
auto res_second = std::find(res->second.begin(), res->second.end(), Trim(deps_str));
@ -242,6 +263,10 @@ bool OhosComponentChecker::InterceptInnerApiPublicDepsInner(const Target *target
bool OhosComponentChecker::InterceptInnerApiNotLib(const Item *item, const std::string label, Err *err) const
{
if (!IsIntercept(ruleSwitch_, INNERAPI_NOT_LIB_BINARY)) {
return true;
}
auto result = std::find(innerapi_not_lib_.begin(), innerapi_not_lib_.end(), label);
if (result != innerapi_not_lib_.end()) {
return true;
@ -254,6 +279,10 @@ bool OhosComponentChecker::InterceptInnerApiNotLib(const Item *item, const std::
bool OhosComponentChecker::InterceptDepsNotLib(const Item *item, const std::string label,
const std::string deps, Err *err) const
{
if (!IsIntercept(ruleSwitch_, DEPS_NOT_LIB_BINARY)) {
return true;
}
if (auto res = fuzzy_match_.find("deps_not_lib"); res != fuzzy_match_.end()) {
std::string deps_str(deps);
for (auto res_second : res->second) {
@ -277,6 +306,10 @@ bool OhosComponentChecker::InterceptDepsNotLib(const Item *item, const std::stri
bool OhosComponentChecker::InterceptInnerApiNotDeclare(const Item *item, const std::string label, Err *err) const
{
if (!IsIntercept(ruleSwitch_, INNERAPI_NOT_DECLARE_BINARY)) {
return true;
}
auto result = std::find(innerapi_not_declare_.begin(), innerapi_not_declare_.end(), label);
if (result != innerapi_not_declare_.end()) {
return true;
@ -289,6 +322,10 @@ bool OhosComponentChecker::InterceptInnerApiNotDeclare(const Item *item, const s
bool OhosComponentChecker::InterceptIncludesAbsoluteDepsOther(const Target *target, const std::string label,
const std::string includes, Err *err) const
{
if (!IsIntercept(ruleSwitch_, INCLUDES_ABSOLUTE_DEPS_OTHER_BINARY)) {
return true;
}
if (auto res = fuzzy_match_.find("deps_includes_absolute"); res != fuzzy_match_.end()) {
std::string includes_str(includes);
for (auto res_second : res->second) {
@ -316,6 +353,10 @@ bool OhosComponentChecker::InterceptIncludesAbsoluteDepsOther(const Target *targ
bool OhosComponentChecker::InterceptTargetAbsoluteDepsOther(const Item *item, const std::string label,
const std::string deps, Err *err) const
{
if (!IsIntercept(ruleSwitch_, TARGET_ABSOLUTE_DEPS_OTHER_BINARY)) {
return true;
}
if (auto res = fuzzy_match_.find("deps_component_absolute"); res != fuzzy_match_.end()) {
std::string deps_str(deps);
for (auto res_second : res->second) {
@ -342,6 +383,10 @@ bool OhosComponentChecker::InterceptTargetAbsoluteDepsOther(const Item *item, co
bool OhosComponentChecker::InterceptInnerApiVisibilityDenied(const Item *item, const std::string from_label,
const std::string to_label, Err *err) const
{
if (!IsIntercept(ruleSwitch_, INNERAPI_VISIBILITY_DENIED)) {
return true;
}
*err = Err(item->defined_from(), "InnerApi visibility denied.",
"The item " + from_label + " cannot dependent " + to_label +
"\n"
@ -353,6 +398,10 @@ bool OhosComponentChecker::InterceptInnerApiVisibilityDenied(const Item *item, c
bool OhosComponentChecker::InterceptImportOther(const FunctionCallNode* function, const std::string label,
const std::string deps, Err *err) const
{
if (!IsIntercept(ruleSwitch_, IMPORT_OTHER_BINARY)) {
return true;
}
if (auto res = fuzzy_match_.find("deps_gni"); res != fuzzy_match_.end()) {
std::string deps_str(deps);
for (auto res_second : res->second) {
@ -374,10 +423,11 @@ bool OhosComponentChecker::InterceptImportOther(const FunctionCallNode* function
return false;
}
OhosComponentChecker::OhosComponentChecker(const std::string &build_dir, int checkType)
OhosComponentChecker::OhosComponentChecker(const std::string &build_dir, int checkType, unsigned int ruleSwitch)
{
checkType_ = checkType;
build_dir_ = build_dir;
ruleSwitch_ = ruleSwitch;
if (checkType_ == CheckType::INTERCEPT_IGNORE_TEST || checkType_ == CheckType::INTERCEPT_ALL) {
LoadWhitelist(build_dir_);
}
@ -606,4 +656,4 @@ bool OhosComponentChecker::CheckImportOther(const FunctionCallNode* function, co
}
GenerateScanList("import_other.list", component->subsystem(), component->name(), label, deps);
return true;
}
}

View File

@ -23,13 +23,27 @@ public:
INTERCEPT_IGNORE_TEST,
INTERCEPT_ALL
};
enum BinaryLeftShift {
UNKNOWN = 0,
ALL_DEPS_CONFIG_BINARY,
INCLUDE_OVER_RANGE_BINARY,
INNERAPI_PUBLIC_DEPS_INNER_BINARY,
INNERAPI_NOT_LIB_BINARY,
DEPS_NOT_LIB_BINARY,
INNERAPI_NOT_DECLARE_BINARY,
INCLUDES_ABSOLUTE_DEPS_OTHER_BINARY,
TARGET_ABSOLUTE_DEPS_OTHER_BINARY,
IMPORT_OTHER_BINARY,
INNERAPI_VISIBILITY_DENIED,
ALL
};
static void Init(const std::string &build_dir, int checkType)
static void Init(const std::string &build_dir, int checkType, unsigned int ruleSwitch)
{
if (instance_ != nullptr) {
return;
}
instance_ = new OhosComponentChecker(build_dir, checkType);
instance_ = new OhosComponentChecker(build_dir, checkType, ruleSwitch);
}
bool CheckAllDepsConfigs(const Target *target, const std::string label, Err *err) const;
@ -58,6 +72,7 @@ public:
private:
int checkType_ = NONE;
bool ignoreTest_ = true;
unsigned int ruleSwitch_;
std::string build_dir_;
static OhosComponentChecker *instance_;
bool InterceptAllDepsConfig(const Target *target, const std::string label, Err *err) const;
@ -79,7 +94,7 @@ private:
void GenerateScanList(const std::string path, const std::string subsystem, const std::string component,
const std::string label, const std::string deps) const;
OhosComponentChecker() {}
OhosComponentChecker(const std::string &build_dir, int checkType);
OhosComponentChecker(const std::string &build_dir, int checkType, unsigned int ruleSwitch);
OhosComponentChecker &operator = (const OhosComponentChecker &) = delete;
};

View File

@ -24,6 +24,7 @@
#include "gn/filesystem_utils.h"
#include "gn/input_file.h"
#include "gn/label_pattern.h"
#include "gn/ohos_components_checker.h"
#include "gn/parse_tree.h"
#include "gn/parser.h"
#include "gn/source_dir.h"
@ -450,10 +451,22 @@ bool Setup::FillOhosComponentsInfo(const std::string& build_dir, Err* err)
}
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());
const Value* ruleSwitch = build_settings_.build_args().GetArgOverride("ohos_interception_rule_switch");
if (ruleSwitch && ruleSwitch->type() == Value::INTEGER) {
if (checkType && checkType->type() == Value::INTEGER) {
ohos_components_.LoadOhosComponentsChecker(build_dir, support, checkType->int_value(), ruleSwitch->int_value());
} else {
ohos_components_.LoadOhosComponentsChecker(build_dir, support,
OhosComponentChecker::CheckType::INTERCEPT_IGNORE_TEST, ruleSwitch->int_value());
}
} else {
ohos_components_.LoadOhosComponentsChecker(build_dir, support, 0);
if (checkType && checkType->type() == Value::INTEGER) {
const unsigned int INTERCEPT_ALL_RULE = (1 << (OhosComponentChecker::BinaryLeftShift::ALL - 1)) - 1;
ohos_components_.LoadOhosComponentsChecker(build_dir, support, checkType->int_value(), INTERCEPT_ALL_RULE);
} else {
ohos_components_.LoadOhosComponentsChecker(build_dir, support, OhosComponentChecker::CheckType::NONE,
OhosComponentChecker::BinaryLeftShift::UNKNOWN);
}
}
const Value *independent = build_settings_.build_args().GetArgOverride("ohos_indep_compiler_enable");