/src/WasmEdge/lib/driver/uniTool.cpp
Line | Count | Source |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: Copyright The WasmEdge Authors |
3 | | |
4 | | #include "driver/unitool.h" |
5 | | #include "common/spdlog.h" |
6 | | #include "driver/compiler.h" |
7 | | #include "driver/tool.h" |
8 | | #include "po/argument_parser.h" |
9 | | |
10 | | #include <string_view> |
11 | | |
12 | | namespace WasmEdge { |
13 | | namespace Driver { |
14 | | |
15 | 0 | int UniTool(int Argc, const char *Argv[], const ToolType ToolSelect) noexcept { |
16 | 0 | using namespace std::literals; |
17 | |
|
18 | 0 | std::ios::sync_with_stdio(false); |
19 | |
|
20 | 0 | auto Parser = PO::ArgumentParser(); |
21 | |
|
22 | 0 | PO::SubCommand ToolSubCommand( |
23 | 0 | PO::Description("Wasmedge runtime tool subcommand"sv)); |
24 | 0 | PO::SubCommand CompilerSubCommand( |
25 | 0 | PO::Description("Wasmedge compiler subcommand"sv)); |
26 | 0 | PO::SubCommand ParseSubCommand( |
27 | 0 | PO::Description("Wasmedge parse tool subcommand"sv)); |
28 | 0 | PO::SubCommand InstantiateSubCommand( |
29 | 0 | PO::Description("Wasmedge instantiate tool subcommand"sv)); |
30 | 0 | PO::SubCommand ValidateSubCommand( |
31 | 0 | PO::Description("Wasmedge validate tool subcommand"sv)); |
32 | 0 | struct DriverToolOptions ToolOptions; |
33 | 0 | struct DriverCompilerOptions CompilerOptions; |
34 | 0 | struct DriverToolOptions ParseOptions; |
35 | 0 | struct DriverToolOptions InstantiateOptions; |
36 | 0 | struct DriverToolOptions ValidateOptions; |
37 | | |
38 | | // Construct Parser Subcommands and Options |
39 | 0 | if (ToolSelect == ToolType::All) { |
40 | 0 | ToolOptions.addOptions(Parser); |
41 | |
|
42 | 0 | Parser.begin_subcommand(CompilerSubCommand, "compile"sv); |
43 | 0 | CompilerOptions.addOptions(Parser); |
44 | 0 | Parser.end_subcommand(); |
45 | 0 | Parser.begin_subcommand(ToolSubCommand, "run"sv); |
46 | 0 | ToolOptions.addOptions(Parser); |
47 | 0 | Parser.end_subcommand(); |
48 | 0 | Parser.begin_subcommand(ParseSubCommand, "parse"sv); |
49 | 0 | ParseOptions.addParserOptions(Parser); |
50 | 0 | Parser.end_subcommand(); |
51 | 0 | Parser.begin_subcommand(InstantiateSubCommand, "instantiate"sv); |
52 | 0 | InstantiateOptions.addLinkerOptions(Parser); |
53 | 0 | Parser.end_subcommand(); |
54 | 0 | Parser.begin_subcommand(ValidateSubCommand, "validate"sv); |
55 | 0 | ValidateOptions.addParserOptions(Parser); |
56 | 0 | Parser.end_subcommand(); |
57 | 0 | } else if (ToolSelect == ToolType::Tool) { |
58 | 0 | ToolOptions.addOptions(Parser); |
59 | 0 | } else if (ToolSelect == ToolType::Compiler) { |
60 | 0 | CompilerOptions.addOptions(Parser); |
61 | 0 | } else if (ToolSelect == ToolType::Parse) { |
62 | 0 | ParseOptions.addParserOptions(Parser); |
63 | 0 | } else if (ToolSelect == ToolType::Validate) { |
64 | 0 | ValidateOptions.addParserOptions(Parser); |
65 | 0 | } else if (ToolSelect == ToolType::Instantiate) { |
66 | 0 | InstantiateOptions.addLinkerOptions(Parser); |
67 | 0 | } else { |
68 | 0 | return EXIT_FAILURE; |
69 | 0 | } |
70 | | |
71 | | // Parse |
72 | 0 | if (!Parser.parse(stdout, Argc, Argv)) { |
73 | 0 | return EXIT_FAILURE; |
74 | 0 | } |
75 | 0 | if (Parser.isVersion()) { |
76 | 0 | fmt::print("{} version {}\n"sv, Argv[0], kVersionString); |
77 | 0 | for (const auto &Plugin : Plugin::Plugin::plugins()) { |
78 | 0 | auto PluginVersion = Plugin.version(); |
79 | 0 | fmt::print("{} (plugin \"{}\") version {}.{}.{}.{}\n"sv, |
80 | 0 | Plugin.path().string(), Plugin.name(), PluginVersion.Major, |
81 | 0 | PluginVersion.Minor, PluginVersion.Patch, PluginVersion.Build); |
82 | 0 | } |
83 | 0 | return EXIT_SUCCESS; |
84 | 0 | } |
85 | 0 | if (Parser.isHelp()) { |
86 | 0 | return EXIT_SUCCESS; |
87 | 0 | } |
88 | | |
89 | 0 | auto ApplyLogLevel = [](const std::string &Level) { |
90 | 0 | if (!Level.empty() && !Log::setLoggingLevelFromString(Level)) { |
91 | 0 | spdlog::warn( |
92 | 0 | "Invalid log level: {}. Valid values are: off, trace, debug, " |
93 | 0 | "info, warning, error, fatal. Falling back to info level."sv, |
94 | 0 | Level); |
95 | 0 | Log::setInfoLoggingLevel(); |
96 | 0 | } |
97 | 0 | }; |
98 | |
|
99 | 0 | if (ToolSelect == ToolType::All) { |
100 | 0 | if (!ParseSubCommand.is_selected() && !ValidateSubCommand.is_selected() && |
101 | 0 | !InstantiateSubCommand.is_selected()) { |
102 | 0 | ApplyLogLevel(ToolOptions.LogLevel.value()); |
103 | 0 | } |
104 | 0 | } else if (ToolSelect == ToolType::Tool) { |
105 | 0 | ApplyLogLevel(ToolOptions.LogLevel.value()); |
106 | 0 | } else if (ToolSelect == ToolType::Instantiate) { |
107 | 0 | ApplyLogLevel(InstantiateOptions.LogLevel.value()); |
108 | 0 | } |
109 | | |
110 | | // Forward Results |
111 | 0 | if (ToolSubCommand.is_selected() || ToolSelect == ToolType::Tool) { |
112 | 0 | return Tool(ToolOptions); |
113 | 0 | } else if (CompilerSubCommand.is_selected() || |
114 | 0 | ToolSelect == ToolType::Compiler) { |
115 | 0 | return Compiler(CompilerOptions); |
116 | 0 | } else if (ParseSubCommand.is_selected() || ToolSelect == ToolType::Parse) { |
117 | 0 | return ParseTool(ParseOptions); |
118 | 0 | } else if (ValidateSubCommand.is_selected() || |
119 | 0 | ToolSelect == ToolType::Validate) { |
120 | 0 | return ValidateTool(ValidateOptions); |
121 | 0 | } else if (InstantiateSubCommand.is_selected() || |
122 | 0 | ToolSelect == ToolType::Instantiate) { |
123 | 0 | return InstantiateTool(InstantiateOptions); |
124 | 0 | } else { |
125 | 0 | return Tool(ToolOptions); |
126 | 0 | } |
127 | 0 | } |
128 | | } // namespace Driver |
129 | | } // namespace WasmEdge |