1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * 4 * HDF is dual licensed: you can use it either under the terms of 5 * the GPL, or the BSD license, at your option. 6 * See the LICENSE file in the root of this repository for complete details. 7 */ 8 9 #ifndef OHOS_HDI_OPTION_H 10 #define OHOS_HDI_OPTION_H 11 12 #include <string> 13 #include <unordered_map> 14 #include <vector> 15 16 namespace OHOS { 17 namespace HDI { 18 class Options { 19 public: 20 using PkgPathMap = std::unordered_map<std::string, std::string>; 21 22 enum class Language { 23 C, 24 CPP, 25 JAVA, 26 }; 27 28 static Options &GetInstance(); 29 30 Options(const Options &other) = delete; 31 Options operator=(const Options &other) = delete; 32 33 Options &Parse(int argc, char *argv[]); 34 35 ~Options() = default; 36 DoShowUsage()37 inline bool DoShowUsage() const 38 { 39 return doShowUsage_; 40 } 41 DoShowVersion()42 inline bool DoShowVersion() const 43 { 44 return doShowVersion_; 45 } 46 DoCompile()47 inline bool DoCompile() const 48 { 49 return doCompile_; 50 } 51 DoDumpAST()52 inline bool DoDumpAST() const 53 { 54 return doDumpAST_; 55 } 56 DoGetHashKey()57 inline bool DoGetHashKey() const 58 { 59 return doGetHashKey_; 60 } 61 DoGenerateCode()62 inline bool DoGenerateCode() const 63 { 64 return doGenerateCode_; 65 } 66 DoGenerateKernelCode()67 inline bool DoGenerateKernelCode() const 68 { 69 return doModeKernel_; 70 } 71 DoPassthrough()72 inline bool DoPassthrough() const 73 { 74 return doPassthrough_; 75 } 76 HasErrors()77 inline bool HasErrors() const 78 { 79 return !errors_.empty(); 80 } 81 GetSourceFiles()82 inline std::vector<std::string> GetSourceFiles() const 83 { 84 return sourceFiles_; 85 } 86 GetPackagePathMap()87 inline PkgPathMap GetPackagePathMap() const 88 { 89 return packagePath_; 90 } 91 GetTargetLanguage()92 inline Language GetTargetLanguage() const 93 { 94 return targetLanguage_; 95 } 96 GetCodePart()97 inline std::string GetCodePart() const 98 { 99 return codePart_; 100 } 101 GetModuleName()102 inline std::string GetModuleName() const 103 { 104 return doSetModuleName_ ? moduleName_ : "sample"; 105 } 106 GetGenerationDirectory()107 inline std::string GetGenerationDirectory() const 108 { 109 return generationDirectory_; 110 } 111 112 void ShowErrors() const; 113 114 void ShowVersion() const; 115 116 void ShowUsage() const; 117 118 std::string GetRootPackage(const std::string &package); 119 120 std::string GetRootPath(const std::string &package); 121 122 std::string GetSubPackage(const std::string &package); 123 124 std::string GetPackagePath(const std::string &package); 125 126 std::string GetImportFilePath(const std::string &import); 127 128 private: Options()129 Options() 130 : program_(), 131 sourceFiles_(0), 132 targetLanguage_(Language::C), 133 codePart_("all"), 134 generationDirectory_(), 135 illegalOptions_(), 136 errors_(), 137 doShowUsage_(false), 138 doShowVersion_(false), 139 doCompile_(false), 140 doDumpAST_(false), 141 doGetHashKey_(false), 142 doGenerateCode_(false), 143 doModeKernel_(false), 144 doGeneratePart_(false), 145 doSetModuleName_(false), 146 doOutDir_(false), 147 doPassthrough_(false) 148 { 149 } 150 151 void SetOptionData(char op); 152 153 void AddPackagePath(const std::string &packagePath); 154 155 void AddSources(const std::string &sourceFile); 156 157 void SetOutDir(const std::string &dir); 158 159 void SetModuleName(const std::string &moduleName); 160 161 void SetLanguage(Language language); 162 163 void SetCodePart(const std::string &part); 164 165 void CheckOptions(); 166 167 static const char *optSupportArgs; 168 static constexpr int OPT_END = -1; 169 170 static constexpr int VERSION_MAJOR = 0; 171 static constexpr int VERSION_MINOR = 1; 172 173 std::string program_; 174 std::vector<std::string> sourceFiles_; 175 Language targetLanguage_; 176 std::string codePart_; 177 std::string moduleName_; 178 std::string generationDirectory_; 179 std::string illegalOptions_; 180 std::vector<std::string> errors_; 181 PkgPathMap packagePath_; 182 183 bool doShowUsage_; 184 bool doShowVersion_; 185 bool doCompile_; 186 bool doDumpAST_; 187 bool doGetHashKey_; 188 bool doGenerateCode_; 189 bool doModeKernel_; 190 bool doGeneratePart_; 191 bool doSetModuleName_; 192 bool doOutDir_; 193 bool doPassthrough_; 194 }; 195 } // namespace HDI 196 } // namespace OHOS 197 198 #endif // OHOS_HDIL_OPTION_H