1 // Copyright (c) 2024 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef TOOLS_GN_OHOS_COMPONENTS_MGR_H_ 6 #define TOOLS_GN_OHOS_COMPONENTS_MGR_H_ 7 8 #include <map> 9 #include <mutex> 10 11 #include "base/files/file_path.h" 12 #include "base/values.h" 13 #include "gn/err.h" 14 #include "gn/value.h" 15 16 class OhosComponent; 17 18 struct OhosComponentTree { 19 const char *dirName; 20 struct OhosComponentTree *next; 21 struct OhosComponentTree *child; 22 const OhosComponent *component; 23 24 public: 25 OhosComponentTree(const char *dirName, const OhosComponent *component = nullptr) 26 { 27 this->dirName = strdup(dirName); 28 this->component = component; 29 this->next = nullptr; 30 this->child = nullptr; 31 } 32 33 OhosComponentTree(const char *dirName, size_t len, const OhosComponent *component = nullptr) 34 { 35 this->dirName = (char *)malloc(len + 1); 36 if (this->dirName) { 37 strncpy((char *)this->dirName, dirName, len); 38 ((char *)this->dirName)[len] = '\0'; 39 } 40 this->component = component; 41 this->next = nullptr; 42 this->child = nullptr; 43 } 44 ~OhosComponentTreeOhosComponentTree45 ~OhosComponentTree() 46 { 47 if (!this->dirName) { 48 free((void *)this->dirName); 49 } 50 } 51 }; 52 53 class OhosComponentsImpl { 54 public: 55 OhosComponentsImpl(); 56 isOhosIndepCompilerEnable()57 bool isOhosIndepCompilerEnable() const { 58 return is_indep_compiler_enable_; 59 } 60 GetTargetToolchain()61 std::string GetTargetToolchain() const { 62 return toolchain_; 63 } 64 65 void LoadToolchain(const Value *product); 66 67 bool LoadOhosComponents(const std::string &build_dir, 68 const Value *enable, const Value *indep, const Value *product, 69 bool special_parts_switch, Err *err); 70 71 bool GetExternalDepsLabel(const Value &external_dep, std::string &label, 72 const Label& current_toolchain, int &whole_status, Err *err) const; 73 bool GetPrivateDepsLabel(const Value &dep, std::string &label, 74 const Label& current_toolchain, int &whole_status, Err *err) const; 75 bool GetSubsystemName(const Value &component_name, std::string &subsystem_name, Err *err) const; 76 77 const OhosComponent *GetComponentByName(const std::string &component_name) const; 78 79 private: 80 bool ReadBuildConfigFile(const std::string &build_dir, 81 const char *subfile, std::string &content); 82 83 std::map<std::string, OhosComponent *> components_; 84 85 std::map<std::string, std::string> override_map_; 86 87 bool is_indep_compiler_enable_ = false; 88 89 std::string toolchain_; 90 91 struct OhosComponentTree *pathTree = nullptr; 92 void setupComponentsTree(); 93 const struct OhosComponentTree *findChildByPath(const struct OhosComponentTree *current, 94 const char *path, size_t len); 95 void addComponentToTree(struct OhosComponentTree *current, OhosComponent *component); 96 97 void LoadOverrideMap(const std::string &override_map); 98 99 // For unittest 100 public: 101 const OhosComponent *matchComponentByLabel(const char *label); 102 103 bool LoadComponentInfo(const std::string &components_content, bool special_parts_switch, std::string &err_msg_out); 104 105 void LoadInnerApi(const std::string &component_name, const std::vector<base::Value> &innerapis); 106 }; 107 108 #endif // TOOLS_GN_OHOS_COMPONENTS_MGR_H_