forked from mogemimi/daily-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
272 lines (247 loc) · 10 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Copyright (c) 2015 mogemimi. Distributed under the MIT license.
#include "MSBuild.h"
#include "XcodeProject.h"
#include "somera/CommandLineParser.h"
#include "somera/FileSystem.h"
#include "somera/StringHelper.h"
#include "somera/SubprocessHelper.h"
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using somera::CommandLineParser;
namespace StringHelper = somera::StringHelper;
namespace FileSystem = somera::FileSystem;
namespace {
void setupCommandLineParser(CommandLineParser & parser)
{
using somera::CommandLineArgumentType;
parser.setUsageText("nori [options ...] [build file ...]");
parser.addArgument("-h", CommandLineArgumentType::Flag, "Display available options");
parser.addArgument("-help", CommandLineArgumentType::Flag, "Display available options");
parser.addArgument("-I", CommandLineArgumentType::JoinedOrSeparate,
"Add directory to include search path");
parser.addArgument("-L", CommandLineArgumentType::JoinedOrSeparate,
"Add directory to library search path");
parser.addArgument("-D", CommandLineArgumentType::JoinedOrSeparate,
"Add predefined macro");
parser.addArgument("-l", CommandLineArgumentType::JoinedOrSeparate,
"Search the library when linking");
parser.addArgument("-O", CommandLineArgumentType::Flag, "Specify the optimization level");
parser.addArgument("-O0", CommandLineArgumentType::Flag, "Specify the optimization level");
parser.addArgument("-O1", CommandLineArgumentType::Flag, "Specify the optimization level");
parser.addArgument("-O2", CommandLineArgumentType::Flag, "Specify the optimization level");
parser.addArgument("-O3", CommandLineArgumentType::Flag, "Specify the optimization level");
parser.addArgument("-O4", CommandLineArgumentType::Flag, "Specify the optimization level");
parser.addArgument("-Ofast", CommandLineArgumentType::Flag, "Specify the optimization level");
parser.addArgument("-Os", CommandLineArgumentType::Flag, "Specify the optimization level");
parser.addArgument("-Oz", CommandLineArgumentType::Flag, "Specify the optimization level");
parser.addArgument("-Og", CommandLineArgumentType::Flag, "Specify the optimization level");
parser.addArgument("-W", CommandLineArgumentType::JoinedOrSeparate, "Specify a warning option");
parser.addArgument("-fno-exceptions", CommandLineArgumentType::Flag, "no-exceptions");
parser.addArgument("-fno-rtti", CommandLineArgumentType::Flag, "no-rtti");
parser.addArgument("-generator=", CommandLineArgumentType::JoinedOrSeparate,
"The output formats to generate. Supported format\n"
"are \"xcode\", \"msbuild\", \"cmake\", \"gyp\", or \"gn\".");
parser.addArgument("-c", CommandLineArgumentType::JoinedOrSeparate, "TODO");
parser.addArgument("-o", CommandLineArgumentType::JoinedOrSeparate, "Write output to <file>");
parser.addArgument("-generator-output=", CommandLineArgumentType::JoinedOrSeparate,
"Generate build files under the <dir>");
parser.addArgument("-verbose", CommandLineArgumentType::Flag,
"Provide additional status output");
parser.addArgument("-std=", CommandLineArgumentType::JoinedOrSeparate,
"Language standard to compile for");
parser.addArgument("-stdlib=", CommandLineArgumentType::JoinedOrSeparate,
"C++ standard library to use");
}
#if 0
std::string fileToString(const std::string& source)
{
std::ifstream input(source);
std::string line;
std::stringstream stream;
while (input && std::getline(input, line)) {
line = somera::StringHelper::replace(line, "\\", "\\\\");
line = somera::StringHelper::replace(line, "\"", "\\\"");
line = somera::StringHelper::replace(line, "\r", "\\r");
line = somera::StringHelper::replace(line, "\t", " ");
stream << "\"";
stream << line;
stream << "\\n\"\n";
}
return stream.str();
}
#endif
void sortByName(std::vector<std::string>& names)
{
std::sort(std::begin(names), std::end(names),
[](const auto& a, const auto& b) {
return StringHelper::toLower(a) < StringHelper::toLower(b);
});
names.erase(
std::unique(std::begin(names), std::end(names)), std::end(names));
}
std::string getAuthorName()
{
std::error_code err;
std::string result;
std::tie(result, err) = somera::SubprocessHelper::call("git config user.name");
if (err) {
return "";
}
return somera::StringHelper::replace(result, "\n", "");
}
template <class Container, typename Func>
auto eraseIf(Container & container, Func func)
{
return container.erase(
std::remove_if(std::begin(container), std::end(container), func),
std::end(container));
}
} // unnamed namespace
int main(int argc, char *argv[])
{
CommandLineParser parser;
setupCommandLineParser(parser);
parser.parse(argc, argv);
auto printVerbose = [&](const std::string& text) {
static const bool verbose = parser.exists("-verbose");
if (verbose) {
std::cout << text << std::endl;
}
};
if (parser.hasParseError()) {
std::cerr << parser.getErrorMessage() << std::endl;
return 1;
}
if (parser.exists("-h") || parser.exists("-help")) {
std::cout << parser.getHelpText() << std::endl;
return 0;
}
if (parser.getPaths().empty()) {
std::cerr << "error: no input file" << std::endl;
return 1;
}
const auto generator = parser.getValue("-generator=");
somera::CompileOptions options;
for (auto & path : parser.getValues("-I")) {
printVerbose("[-I] "+ path);
options.includeSearchPaths.push_back(path);
}
for (auto & path : parser.getValues("-L")) {
printVerbose("[-L] " + path);
options.librarySearchPaths.push_back(path);
}
for (auto & definition : parser.getValues("-D")) {
printVerbose("[-D] " + definition);
options.preprocessorDefinitions.push_back(definition);
}
for (auto & path : parser.getValues("-l")) {
printVerbose("[-l] " + path);
options.libraries.push_back(path);
}
for (auto & path : parser.getPaths()) {
printVerbose("[Path] " + path);
options.sources.push_back(path);
}
eraseIf(options.sources, [](const std::string& path) {
return somera::StringHelper::startWith(path, "*");
});
if (auto path = parser.getValue("-generator-output=")) {
options.generatorOutputDirectory = *path;
}
options.productName = "MyHoge";
if (auto path = parser.getValue("-o")) {
options.productName = *path;
}
options.targetName = options.productName;
options.author = getAuthorName();
if (auto value = parser.getValue("-std=")) {
options.buildSettings.emplace("-std=", *value);
}
if (auto value = parser.getValue("-stdlib=")) {
options.buildSettings.emplace("-stdlib=", *value);
}
options.enableCppExceptions = !parser.exists("-fno-exceptions");
options.enableCppRtti = !parser.exists("-fno-rtti");
for (auto & path : options.includeSearchPaths) {
if (FileSystem::isAbsolute(path)) {
continue;
}
path = FileSystem::relative(path, options.generatorOutputDirectory);
printVerbose("[-I (Relative)] " + path);
}
for (auto & path : options.librarySearchPaths) {
if (FileSystem::isAbsolute(path)) {
continue;
}
path = FileSystem::relative(path, options.generatorOutputDirectory);
printVerbose("[-L (Relative)] " + path);
}
if (generator && (*generator == "xcode")) {
for (auto & path : options.libraries) {
auto extension = std::get<1>(FileSystem::splitExtension(path));
if (!extension.empty()) {
continue;
}
if (!FileSystem::getDirectoryName(path).empty()) {
continue;
}
for (auto & directory : options.librarySearchPaths) {
auto base = FileSystem::join(directory, "lib" + path);
auto staticLibrary = base + ".a";
if (FileSystem::exists(staticLibrary)) {
path = staticLibrary;
printVerbose("[-l (Found .a file)] " + path);
break;
}
auto dynamicLibrary = base + ".dylib";
if (FileSystem::exists(dynamicLibrary)) {
path = dynamicLibrary;
printVerbose("[-l (Found .dylib file)] " + path);
break;
}
printVerbose("[-l (LDFLAGS)] " + path);
options.otherLinkerFlags.push_back("-l" + path);
path.clear();
}
}
options.libraries.erase(
std::remove(std::begin(options.libraries), std::end(options.libraries), ""),
std::end(options.libraries));
}
for (auto & path : options.libraries) {
if (FileSystem::isAbsolute(path)) {
continue;
}
path = FileSystem::relative(path, options.generatorOutputDirectory);
printVerbose("[-l (Relative)] " + path);
}
for (auto & path : options.sources) {
if (FileSystem::isAbsolute(path)) {
continue;
}
path = FileSystem::relative(path, options.generatorOutputDirectory);
printVerbose("[Path (Relative)] " + path);
}
sortByName(options.libraries);
sortByName(options.sources);
if (generator && (*generator == "xcode")) {
auto error = somera::Xcode::GenerateXcodeProject(options);
if (error.hasError) {
std::cerr << error.description << std::endl;
return 1;
}
std::cout << "Generated." << std::endl;
}
else if (generator && (*generator == "msbuild")) {
auto error = somera::MSBuild::GenerateMSBuildProject(options);
if (error.hasError) {
std::cerr << error.description << std::endl;
return 1;
}
std::cout << "Generated." << std::endl;
}
return 0;
}