Debian Codegraph @

src/mpt/main/main.hpp

raw
1 /* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
2
3 #ifndef MPT_MAIN_MAIN_HPP
4 #define MPT_MAIN_MAIN_HPP
5
6
7
8 #include "mpt/base/check_platform.hpp"
9 #include "mpt/base/detect.hpp"
10 #include "mpt/base/integer.hpp"
11 #include "mpt/base/macros.hpp"
12 #include "mpt/base/namespace.hpp"
13 #include "mpt/string/types.hpp"
14 #include "mpt/string_transcode/transcode.hpp"
15
16 #include <type_traits>
17 #include <vector>
18
19 #if MPT_OS_DJGPP
20 #include <cassert>
21 #endif // MPT_OS_DJGPP
22
23 #if MPT_OS_DJGPP
24 #include <crt0.h>
25 #endif // MPT_OS_DJGPP
26
27 #if MPT_OS_WINDOWS
28 #include <windows.h>
29 #endif // MPT_OS_WINDOWS
30
31
32
33 namespace mpt {
34 inline namespace MPT_INLINE_NS {
35
36
37
38 namespace main {
39
40
41
42 #if MPT_OS_DJGPP
43 /* Work-around <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=45977> */
44 /* clang-format off */
45 #define MPT_MAIN_PREFIX \
46 extern "C" { \
47 int _crt0_startup_flags = 0 \
48 | _CRT0_FLAG_NONMOVE_SBRK /* force interrupt compatible allocation */ \
49 | _CRT0_DISABLE_SBRK_ADDRESS_WRAP /* force NT compatible allocation */ \
50 | _CRT0_FLAG_LOCK_MEMORY /* lock all code and data at program startup */ \
51 | 0; \
52 } \
53 /* clang-format on */
54 #endif /* MPT_OS_DJGPP */
55
56 #if !defined(MPT_MAIN_PREFIX)
57 #define MPT_MAIN_PREFIX
58 #endif
59
60
61
62 #if MPT_OS_WINDOWS && defined(UNICODE)
63 #define MPT_MAIN_NAME wmain
64 #elif defined(MPT_OS_WINDOWS)
65 #define MPT_MAIN_NAME main
66 #endif
67
68 #if !defined(MPT_MAIN_NAME)
69 #define MPT_MAIN_NAME main
70 #endif
71
72
73
74 #if MPT_OS_WINDOWS && defined(UNICODE)
75 #define MPT_MAIN_ARGV_TYPE wchar_t
76 #elif defined(MPT_OS_WINDOWS)
77 #define MPT_MAIN_ARGV_TYPE char
78 #endif
79
80 #if !defined(MPT_MAIN_NAME)
81 #define MPT_MAIN_ARGV_TYPE char
82 #endif
83
84
85
86 #if MPT_OS_WINDOWS && (MPT_COMPILER_GCC || MPT_COMPILER_CLANG)
87 #if defined(UNICODE)
88 // mingw64 does only default to special C linkage for "main", but not for "wmain".
89 #define MPT_MAIN_DECL extern "C" int wmain(int argc, wchar_t * argv[]);
90 #endif
91 #endif
92
93 #if !defined(MPT_MAIN_DECL)
94 #define MPT_MAIN_DECL
95 #endif
96
97
98
99 #if MPT_OS_DJGPP
100 /* clang-format off */
101 #define MPT_MAIN_PROLOG() \
102 do { \
103 assert(mpt::platform::libc().is_ok()); \
104 _crt0_startup_flags &= ~_CRT0_FLAG_LOCK_MEMORY; \
105 } while (0) \
106 /**/
107 /* clang-format on */
108 #endif // MPT_OS_DJGPP
109
110 #if !defined(MPT_MAIN_PROLOG)
111 /* clang-format off */
112 #define MPT_MAIN_PROLOG() do { } while(0)
113 /* clang-format on */
114 #endif
115
116
117
118 #if MPT_OS_WINDOWS && (MPT_COMPILER_GCC || MPT_COMPILER_CLANG)
119 #if defined(UNICODE)
120 #define MPT_MAIN_DEF_PREFIX extern "C"
121 #endif
122 #endif // MPT_OS_WINDOWS && (MPT_COMPILER_GCC || MPT_COMPILER_CLANG)
123
124 #if !defined(MPT_MAIN_DEF_PREFIX)
125 #define MPT_MAIN_DEF_PREFIX
126 #endif
127
128
129
130 inline mpt::ustring transcode_arg(char * arg) {
131 return mpt::transcode<mpt::ustring>(mpt::logical_encoding::locale, arg);
132 }
133
134 #if !defined(MPT_COMPILER_QUIRK_NO_WCHAR)
135 inline mpt::ustring transcode_arg(wchar_t * arg) {
136 return mpt::transcode<mpt::ustring>(arg);
137 }
138 #endif
139
140 template <typename Tchar>
141 inline std::vector<mpt::ustring> transcode_argv(int argc, Tchar * argv[]) {
142 std::vector<mpt::ustring> args;
143 args.reserve(argc);
144 for (int arg = 0; arg < argc; ++arg) {
145 args.push_back(transcode_arg(argv[arg]));
146 }
147 return args;
148 }
149
150
151
152 #if !defined(MPT_MAIN_POSTFIX)
153 #define MPT_MAIN_POSTFIX
154 #endif
155
156
157
158 /* clang-format off */
159 #define MPT_MAIN_IMPLEMENT_MAIN(ns) \
160 MPT_MAIN_PREFIX \
161 MPT_MAIN_DECL \
162 MPT_MAIN_DEF_PREFIX int MPT_MAIN_NAME(int argc, MPT_MAIN_ARGV_TYPE * argv[]) { \
163 MPT_MAIN_PROLOG(); \
164 static_assert(std::is_same<decltype(ns::main), mpt::uint8(std::vector<mpt::ustring>)>::value); \
165 return static_cast<int>(ns::main(mpt::main::transcode_argv(argc, argv))); \
166 } \
167 MPT_MAIN_POSTFIX \
168 /**/
169 /* clang-format on */
170
171
172
173 /* clang-format off */
174 #define MPT_MAIN_IMPLEMENT_MAIN_NO_ARGS(ns) \
175 MPT_MAIN_PREFIX \
176 MPT_MAIN_DECL \
177 MPT_MAIN_DEF_PREFIX int MPT_MAIN_NAME(int argc, MPT_MAIN_ARGV_TYPE * argv[]) { \
178 MPT_MAIN_PROLOG(); \
179 static_assert(std::is_same<decltype(ns::main), mpt::uint8(void)>::value); \
180 MPT_UNUSED(argc); \
181 MPT_UNUSED(argv); \
182 return static_cast<int>(ns::main()); \
183 } \
184 MPT_MAIN_POSTFIX \
185 /**/
186 /* clang-format on */
187
188
189
190 } // namespace main
191
192
193
194 } // namespace MPT_INLINE_NS
195 } // namespace mpt
196
197
198
199 #endif // MPT_MAIN_MAIN_HPP