"src/vscode:/vscode.git/clone" did not exist on "19173aa4370e36cba96ee7049eaaa0dceda5007c"
command.hpp 3.44 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
Paul's avatar
Paul committed
24
25
26
27
28
29
30
#ifndef MIGRAPHX_GUARD_RTGLIB_COMMAND_HPP
#define MIGRAPHX_GUARD_RTGLIB_COMMAND_HPP

#include "argument_parser.hpp"

#include <migraphx/config.hpp>
#include <migraphx/type_name.hpp>
Paul's avatar
Paul committed
31
#include <migraphx/stringutils.hpp>
Paul's avatar
Paul committed
32
33

#include <unordered_map>
Paul's avatar
Paul committed
34
#include <utility>
Paul's avatar
Paul committed
35
36
37
38
39
40
41
42
#include <vector>

namespace migraphx {
namespace driver {
inline namespace MIGRAPHX_INLINE_NS {

inline auto& get_commands()
{
43
    // NOLINTNEXTLINE
44
45
46
47
    static std::unordered_map<
        std::string,
        std::function<void(const std::string& exe_name, std::vector<std::string> args)>>
        m;
Paul's avatar
Paul committed
48
49
50
    return m;
}

Paul's avatar
Paul committed
51
template <class T>
Paul's avatar
Paul committed
52
std::string compute_command_name()
Paul's avatar
Paul committed
53
{
Paul's avatar
Paul committed
54
    static const std::string& tname = get_type_name<T>();
Paul's avatar
Paul committed
55
56
57
58
59
    auto name                       = tname.substr(tname.rfind("::") + 2);
    if(ends_with(name, "_command"))
        name = name.substr(0, name.size() - 8);
    if(ends_with(name, "_cmd"))
        name = name.substr(0, name.size() - 4);
Paul's avatar
Paul committed
60
61
62
63
64
65
66
67
    return name;
}

template <class T>
const std::string& command_name()
{
    static const std::string& name = compute_command_name<T>();
    return name;
Paul's avatar
Paul committed
68
69
}

Paul's avatar
Paul committed
70
template <class T>
71
void run_command(const std::string& exe_name, std::vector<std::string> args, bool add_help = false)
Paul's avatar
Paul committed
72
73
74
{
    T x;
    argument_parser ap;
75
    ap.set_exe_name(exe_name + " " + command_name<T>());
Paul's avatar
Paul committed
76
    if(add_help)
Paul's avatar
Paul committed
77
        ap(nullptr, {"-h", "--help"}, ap.help("Show help"), ap.show_help());
Paul's avatar
Paul committed
78
    x.parse(ap);
Paul's avatar
Paul committed
79
    if(ap.parse(std::move(args)))
Paul's avatar
Paul committed
80
81
82
83
        return;
    x.run();
}

Paul's avatar
Paul committed
84
85
86
template <class T>
int auto_register_command()
{
Paul's avatar
Paul committed
87
    auto& m              = get_commands();
88
89
90
    m[command_name<T>()] = [](const std::string& exe_name, std::vector<std::string> args) {
        run_command<T>(exe_name, args, true);
    };
Paul's avatar
Paul committed
91
92
93
94
95
96
    return 0;
}

template <class T>
struct command
{
97
    static const int static_register;
Paul's avatar
Paul committed
98
99
100
101
102
103
    // This typedef ensures that the static member will be instantiated if
    // the class itself is instantiated
    using static_register_type =
        std::integral_constant<decltype(&static_register), &static_register>;
};

Paul's avatar
Paul committed
104
105
106
107
108
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wglobal-constructors"
#endif

Paul's avatar
Paul committed
109
template <class T>
110
const int command<T>::static_register = auto_register_command<T>(); // NOLINT
Paul's avatar
Paul committed
111
112
113
114
115
116

} // namespace MIGRAPHX_INLINE_NS
} // namespace driver
} // namespace migraphx

#endif