1#include <new>
2#include <iostream>
3#include <vector>
4#include <string>
5#include <utility> /// pair
6
7#if __has_include("config_tools.h")
8#include "config_tools.h"
9#endif
10#if __has_include(<common/config_common.h>) /// "Arcadia" build system lacks configure files.
11#include <common/config_common.h>
12#endif
13#if __has_include("config_core.h")
14#include "config_core.h"
15#endif
16
17#include <Common/StringUtils/StringUtils.h>
18
19#include <common/phdr_cache.h>
20#include <ext/scope_guard.h>
21
22
23/// Universal executable for various clickhouse applications
24#if ENABLE_CLICKHOUSE_SERVER || !defined(ENABLE_CLICKHOUSE_SERVER)
25int mainEntryClickHouseServer(int argc, char ** argv);
26#endif
27#if ENABLE_CLICKHOUSE_CLIENT || !defined(ENABLE_CLICKHOUSE_CLIENT)
28int mainEntryClickHouseClient(int argc, char ** argv);
29#endif
30#if ENABLE_CLICKHOUSE_LOCAL || !defined(ENABLE_CLICKHOUSE_LOCAL)
31int mainEntryClickHouseLocal(int argc, char ** argv);
32#endif
33#if ENABLE_CLICKHOUSE_BENCHMARK || !defined(ENABLE_CLICKHOUSE_BENCHMARK)
34int mainEntryClickHouseBenchmark(int argc, char ** argv);
35#endif
36#if ENABLE_CLICKHOUSE_PERFORMANCE_TEST || !defined(ENABLE_CLICKHOUSE_PERFORMANCE_TEST)
37int mainEntryClickHousePerformanceTest(int argc, char ** argv);
38#endif
39#if ENABLE_CLICKHOUSE_EXTRACT_FROM_CONFIG || !defined(ENABLE_CLICKHOUSE_EXTRACT_FROM_CONFIG)
40int mainEntryClickHouseExtractFromConfig(int argc, char ** argv);
41#endif
42#if ENABLE_CLICKHOUSE_COMPRESSOR || !defined(ENABLE_CLICKHOUSE_COMPRESSOR)
43int mainEntryClickHouseCompressor(int argc, char ** argv);
44#endif
45#if ENABLE_CLICKHOUSE_FORMAT || !defined(ENABLE_CLICKHOUSE_FORMAT)
46int mainEntryClickHouseFormat(int argc, char ** argv);
47#endif
48#if ENABLE_CLICKHOUSE_COPIER || !defined(ENABLE_CLICKHOUSE_COPIER)
49int mainEntryClickHouseClusterCopier(int argc, char ** argv);
50#endif
51#if ENABLE_CLICKHOUSE_OBFUSCATOR || !defined(ENABLE_CLICKHOUSE_OBFUSCATOR)
52int mainEntryClickHouseObfuscator(int argc, char ** argv);
53#endif
54
55
56namespace
57{
58
59using MainFunc = int (*)(int, char**);
60
61
62/// Add an item here to register new application
63std::pair<const char *, MainFunc> clickhouse_applications[] =
64{
65#if ENABLE_CLICKHOUSE_LOCAL || !defined(ENABLE_CLICKHOUSE_LOCAL)
66 {"local", mainEntryClickHouseLocal},
67#endif
68#if ENABLE_CLICKHOUSE_CLIENT || !defined(ENABLE_CLICKHOUSE_CLIENT)
69 {"client", mainEntryClickHouseClient},
70#endif
71#if ENABLE_CLICKHOUSE_BENCHMARK || !defined(ENABLE_CLICKHOUSE_BENCHMARK)
72 {"benchmark", mainEntryClickHouseBenchmark},
73#endif
74#if ENABLE_CLICKHOUSE_SERVER || !defined(ENABLE_CLICKHOUSE_SERVER)
75 {"server", mainEntryClickHouseServer},
76#endif
77#if ENABLE_CLICKHOUSE_PERFORMANCE_TEST || !defined(ENABLE_CLICKHOUSE_PERFORMANCE_TEST)
78 {"performance-test", mainEntryClickHousePerformanceTest},
79#endif
80#if ENABLE_CLICKHOUSE_EXTRACT_FROM_CONFIG || !defined(ENABLE_CLICKHOUSE_EXTRACT_FROM_CONFIG)
81 {"extract-from-config", mainEntryClickHouseExtractFromConfig},
82#endif
83#if ENABLE_CLICKHOUSE_COMPRESSOR || !defined(ENABLE_CLICKHOUSE_COMPRESSOR)
84 {"compressor", mainEntryClickHouseCompressor},
85#endif
86#if ENABLE_CLICKHOUSE_FORMAT || !defined(ENABLE_CLICKHOUSE_FORMAT)
87 {"format", mainEntryClickHouseFormat},
88#endif
89#if ENABLE_CLICKHOUSE_COPIER || !defined(ENABLE_CLICKHOUSE_COPIER)
90 {"copier", mainEntryClickHouseClusterCopier},
91#endif
92#if ENABLE_CLICKHOUSE_OBFUSCATOR || !defined(ENABLE_CLICKHOUSE_OBFUSCATOR)
93 {"obfuscator", mainEntryClickHouseObfuscator},
94#endif
95};
96
97
98int printHelp(int, char **)
99{
100 std::cerr << "Use one of the following commands:" << std::endl;
101 for (auto & application : clickhouse_applications)
102 std::cerr << "clickhouse " << application.first << " [args] " << std::endl;
103 return -1;
104}
105
106
107bool isClickhouseApp(const std::string & app_suffix, std::vector<char *> & argv)
108{
109 /// Use app if the first arg 'app' is passed (the arg should be quietly removed)
110 if (argv.size() >= 2)
111 {
112 auto first_arg = argv.begin() + 1;
113
114 /// 'clickhouse --client ...' and 'clickhouse client ...' are Ok
115 if (*first_arg == "--" + app_suffix || *first_arg == app_suffix)
116 {
117 argv.erase(first_arg);
118 return true;
119 }
120 }
121
122 /// Use app if clickhouse binary is run through symbolic link with name clickhouse-app
123 std::string app_name = "clickhouse-" + app_suffix;
124 return !argv.empty() && (app_name == argv[0] || endsWith(argv[0], "/" + app_name));
125}
126
127}
128
129
130/// This allows to implement assert to forbid initialization of a class in static constructors.
131/// Usage:
132///
133/// extern bool inside_main;
134/// class C { C() { assert(inside_main); } };
135bool inside_main = false;
136
137
138int main(int argc_, char ** argv_)
139{
140 inside_main = true;
141 SCOPE_EXIT({ inside_main = false; });
142
143 /// Reset new handler to default (that throws std::bad_alloc)
144 /// It is needed because LLVM library clobbers it.
145 std::set_new_handler(nullptr);
146
147 /// PHDR cache is required for query profiler to work reliably
148 /// It also speed up exception handling, but exceptions from dynamically loaded libraries (dlopen)
149 /// will work only after additional call of this function.
150 updatePHDRCache();
151
152 std::vector<char *> argv(argv_, argv_ + argc_);
153
154 /// Print a basic help if nothing was matched
155 MainFunc main_func = printHelp;
156
157 for (auto & application : clickhouse_applications)
158 {
159 if (isClickhouseApp(application.first, argv))
160 {
161 main_func = application.second;
162 break;
163 }
164 }
165
166 return main_func(static_cast<int>(argv.size()), argv.data());
167}
168