-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
157 lines (126 loc) · 3.26 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
#include "chibi.h"
#include <limits.h>
#include <set>
#include <stdarg.h>
#include <stdio.h>
#include <string>
#include <string.h>
#ifdef _MSC_VER
#include <Windows.h>
#ifndef PATH_MAX
#define PATH_MAX _MAX_PATH
#endif
#endif
#if defined(__GNUC__)
#define sprintf_s(s, ss, f, ...) snprintf(s, ss, f, __VA_ARGS__)
#define vsprintf_s(s, ss, f, a) vsnprintf(s, ss, f, a)
#define strcpy_s(d, ds, s) strcpy(d, s)
#define sscanf_s sscanf
#endif
static bool eat_arg(int & argc, const char **& argv, const char *& arg)
{
if (argc == 0)
return false;
arg = *argv;
argc -= 1;
argv += 1;
return true;
}
static void report_error(const char * format, ...)
{
char text[1024];
va_list ap;
va_start(ap, format);
vsprintf_s(text, sizeof(text), format, ap);
va_end(ap);
//
printf("error: %s\n", text);
}
static void show_chibi_cli()
{
printf("usage: chibi -g <source_path> <destination_path> ..[-target <wildcard>] [-platform <name>]\n");
printf("\t<source_path> the path where to begin looking for the chibi root file\n");
printf("\t<destination_path> the path where to output the generated cmake file\n");
printf("\t-target sets an optional filter for the <app_name> or <library_name> to limit the scope of the generated cmake file to only the specific target(s). <wildcard> may specify either the complete target name or a wildcard. when used more than once, multiple targets can be set\n");
printf("\t-platform sets an optional platform for which to generate build files. supported platforms: macos, windows, linux, linux.raspberry-pi, ios, android\n");
}
int main(int argc, const char * argv[])
{
char cwd[PATH_MAX];
cwd[0] = 0;
const char * src_path = nullptr;
const char * dst_path = nullptr;
argc -= 1;
argv += 1;
if (argc == 0)
{
show_chibi_cli();
printf("\n");
show_chibi_syntax();
return -1;
}
enum Mode
{
kMode_Unknown,
kMode_Generate,
};
Mode mode = kMode_Unknown;
std::set<std::string> build_targets;
const char * platform = nullptr;
while (argc > 0)
{
const char * option;
if (!eat_arg(argc, argv, option))
break;
if (!strcmp(option, "-generate") || !strcmp(option, "-g"))
{
mode = kMode_Generate;
if (!eat_arg(argc, argv, src_path))
{
report_error("missing source path");
return -1;
}
else if (!eat_arg(argc, argv, dst_path))
{
report_error("missing destination path");
return -1;
}
}
else if (!strcmp(option, "-target"))
{
const char * target;
if (!eat_arg(argc, argv, target))
{
report_error("missing target name: %s", option);
return -1;
}
build_targets.insert(target);
}
else if (!strcmp(option, "-platform"))
{
if (!eat_arg(argc, argv, platform))
{
report_error("missing platform name: %s", option);
return -1;
}
}
else
{
report_error("unknown command line option: %s", option);
return -1;
}
}
if (mode == kMode_Unknown)
{
report_error("nothing to do");
return -1;
}
const int numTargets = (int)build_targets.size();
const char ** targets = (const char**)alloca(numTargets * sizeof(char*));
int index = 0;
for (auto & target : build_targets)
targets[index++] = target.c_str();
if (chibi_generate(cwd, src_path, dst_path, targets, numTargets, platform) == false)
return -1;
return 0;
}