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, Err *err); 69 70 bool GetExternalDepsLabel(const Value &external_dep, std::string &label, 71 const Label& current_toolchain, int &whole_status, Err *err) const; 72 bool GetPrivateDepsLabel(const Value &dep, std::string &label, 73 const Label& current_toolchain, int &whole_status, Err *err) const; 74 bool GetSubsystemName(const Value &component_name, std::string &subsystem_name, Err *err) const; 75 76 const OhosComponent *GetComponentByName(const std::string &component_name) const; 77 78 private: 79 bool ReadBuildConfigFile(const std::string &build_dir, 80 const char *subfile, std::string &content); 81 82 std::map<std::string, OhosComponent *> components_; 83 84 std::map<std::string, std::string> override_map_; 85 86 bool is_indep_compiler_enable_ = false; 87 88 std::string toolchain_; 89 90 struct OhosComponentTree *pathTree = nullptr; 91 void setupComponentsTree(); 92 const struct OhosComponentTree *findChildByPath(const struct OhosComponentTree *current, 93 const char *path, size_t len); 94 void addComponentToTree(struct OhosComponentTree *current, OhosComponent *component); 95 96 void LoadOverrideMap(const std::string &override_map); 97 98 // For unittest 99 public: 100 const OhosComponent *matchComponentByLabel(const char *label); 101 102 bool LoadComponentInfo(const std::string &components_content, std::string &err_msg_out); 103 104 void LoadInnerApi(const std::string &component_name, const std::vector<base::Value> &innerapis); 105 }; 106 107 #endif // TOOLS_GN_OHOS_COMPONENTS_MGR_H_