-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmakeautoargs.c
421 lines (399 loc) · 9.88 KB
/
cmakeautoargs.c
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#include "cmakeauto.h"
bool print_templates(const char *abspath, const char *relpath, const char *name, bool isfolder /* 0 = file, 1 = folder */, void *userdata)
{
if (isfolder)
printf("\t%s\n", name);
return true;
}
void cma_print_usage()
{
printf("Usage: cmakeauto help|build|configure|template -b <build path> -s <source path> [-g <Generator>] [-m <Mode>] [-a <Architecture>] [-ei <Extra Init Params>] [-eb <Extra Build Params>] [-w <Directories>] [-t <Template>]\n");
printf("\thelp: print help message\n");
printf("\tbuild: build project\n");
printf("\tconfigure: configure project\n");
printf("\ttemplate: create a template project\n");
printf("\nParameters:\n\t-b <build path>: (Default: build) This specifies where the project would be built.\n");
printf("\t-s <source path>: (Default: src) This specifies where the project source is.\n");
printf("\t-g <Generator Name>: (Optional) This specifies the generator to be used.\n");
printf("\t-m: (Default: release) This specifies the build mode to be Debug.\n");
printf("\t\tdebug: Debug mode\n");
printf("\t\trelease: Release mode\n");
printf("\t-a: (Default: x64) This specifies the architecture to be built.\n");
printf("\t\tx86: 32-bit\n");
printf("\t\tx64: 64-bit\n");
printf("\t-ei: (Optional) This specifies extra init params to be passed to cmake.\n");
printf("\t-eb: (Optional) This specifies extra build params to be passed to cmake.\n");
printf("\t-w: (Optional) This specifies the directories to be watched and will auto rebuild if a change was found.\n");
printf("\t-t: (Only for Template Action) This specifies the template to be used when creating a template project.\n");
char buf[FILE_MAX_PATH + 1];
memset(buf, 0, FILE_MAX_PATH + 1);
if (!cma_get_current_process_absfilepath(buf, FILE_MAX_PATH) || !strlen(buf))
return;
strrchr(buf, FILE_SEPERATOR_CHAR)[1] = 0;
strcat_s(buf, FILE_MAX_PATH, "templates");
printf("\nTemplates:\n");
cma_iterate_dir(buf, ".", 0, false, print_templates);
}
bool cma_parse_args(int argc, char **argv, CMakeAutoConfig *config)
{
if (argc < 2)
{
config->action = CMAKE_AUTO_ACTION_UNKNOWN;
return config;
}
if (stricmp(argv[1], "build") == 0)
{
config->action = CMAKE_AUTO_ACTION_BUILD;
config->mode = CMAKE_AUTO_MODE_RELEASE;
config->arch = CMAKE_AUTO_ARCH_UNKONWN;
if (!cma_abspath(config->builddir, FILE_MAX_PATH, "."))
memset(config->builddir, 0, FILE_MAX_PATH);
else
strcat_s(config->builddir, FILE_MAX_PATH, FILE_SEPERATOR "build");
if (!cma_abspath(config->srcdir, FILE_MAX_PATH, "."))
memset(config->srcdir, 0, FILE_MAX_PATH);
else
strcat_s(config->srcdir, FILE_MAX_PATH, FILE_SEPERATOR "src");
for (int i = 2; i < argc; i++)
{
if (stricmp(argv[i], "-s") == 0)
{
if (i + 1 < argc)
{
if (!cma_abspath(config->srcdir, FILE_MAX_PATH, argv[i + 1]))
{
printf("invalid or error happened when parsing source dir\n");
return false;
}
i++;
}
else
{
printf("no source dir specified\n");
return false;
}
}
else if (stricmp(argv[i], "-b") == 0)
{
if (i + 1 < argc)
{
if (!cma_abspath(config->builddir, FILE_MAX_PATH, argv[i + 1]))
{
printf("invalid or error happened when parsing build dir\n");
return false;
}
i++;
}
else
{
printf("no build dir specified\n");
return false;
}
}
else if (stricmp(argv[i], "-g") == 0)
{
if (i + 1 < argc)
{
if (strcpy_s(config->generator, GERNERATOR_MAX_LEN, argv[i + 1]))
{
printf("invalid or error happened when parsing generator\n");
return false;
}
i++;
}
}
else if (stricmp(argv[i], "-m") == 0)
{
if (i + 1 < argc)
{
if (stricmp(argv[i + 1], "debug") == 0)
config->mode = CMAKE_AUTO_MODE_DEBUG;
else if (stricmp(argv[i + 1], "release") == 0)
config->mode = CMAKE_AUTO_MODE_RELEASE;
else
{
printf("no build mode specified\n");
return false;
}
i++;
}
else
{
printf("no build mode specified\n");
return false;
}
}
else if (stricmp(argv[i], "-a") == 0)
{
if (i + 1 < argc)
{
if (stricmp(argv[i + 1], "x86") == 0)
config->arch = CMAKE_AUTO_ARCH_X86;
else if (stricmp(argv[i + 1], "x64") == 0)
config->arch = CMAKE_AUTO_ARCH_X64;
else
{
printf("no build architecture specified\n");
return false;
}
i++;
}
else
{
printf("no build architecture specified\n");
return false;
}
}
else if (stricmp(argv[i], "-ei") == 0)
{
if (i + 1 < argc)
{
config->extra_init_args = argv[i + 1];
i++;
}
}
else if (stricmp(argv[i], "-eb") == 0)
{
if (i + 1 < argc)
{
config->extra_build_args = argv[i + 1];
i++;
}
}
else if (stricmp(argv[i], "-h") == 0)
{
config->action = CMAKE_AUTO_ACTION_HELP;
return true;
}
else if (stricmp(argv[i], "-w") == 0)
{
config->options |= CMAKE_AUTO_OPTION_AUTO_RELOAD;
if (i + 1 < argc)
{
char temppath[FILE_MAX_PATH + 1];
char *paths = argv[i + 1];
while (*paths)
{
if (config->watchfolderhandles_count >= WATCHFOLDER_MAX_LEN)
{
printf("max watch folders reached\n");
break;
}
char *endpath = paths;
while (*endpath && *endpath != ';')
endpath++;
memset(temppath, 0, FILE_MAX_PATH + 1);
memcpy(temppath, paths, endpath - paths);
if (!cma_abspath(config->watchfolders[config->watchfolders_count], FILE_MAX_PATH, temppath))
{
printf("invalid or error happened when parsing watch folder\n");
return false;
}
config->watchfolders_count++;
while (*paths && *paths != ';')
paths++;
if (*paths == ';')
paths++;
}
i++;
}
else
{
printf("no watch folder specified\n");
return false;
}
}
else
{
printf("unknown argument: %s\n", argv[i]);
return false;
}
}
if (!config->builddir ||
!config->srcdir ||
config->mode == CMAKE_AUTO_MODE_UNKNOWN ||
config->action == CMAKE_AUTO_ACTION_UNKNOWN)
{
printf("invalid build arguments\n");
return false;
}
}
else if (stricmp(argv[1], "help") == 0 || stricmp(argv[1], "-h") == 0)
{
config->action = CMAKE_AUTO_ACTION_HELP;
}
else if (stricmp(argv[1], "configure") == 0)
{
config->action = CMAKE_AUTO_ACTION_CONFIGURE;
config->mode = CMAKE_AUTO_MODE_RELEASE;
config->arch = CMAKE_AUTO_ARCH_X64;
if (!cma_abspath(config->builddir, FILE_MAX_PATH, "build"))
memset(config->builddir, 0, FILE_MAX_PATH);
if (!cma_abspath(config->srcdir, FILE_MAX_PATH, "src"))
memset(config->srcdir, 0, FILE_MAX_PATH);
for (int i = 2; i < argc; i++)
{
if (stricmp(argv[i], "-s") == 0)
{
if (i + 1 < argc)
{
if (!cma_abspath(config->srcdir, FILE_MAX_PATH, argv[i + 1]))
{
printf("invalid or error happened when parsing source dir\n");
return false;
}
i++;
}
else
{
printf("no source dir specified\n");
return false;
}
}
else if (stricmp(argv[i], "-b") == 0)
{
if (i + 1 < argc)
{
if (!cma_abspath(config->builddir, FILE_MAX_PATH, argv[i + 1]))
{
printf("invalid or error happened when parsing build dir\n");
return false;
}
i++;
}
else
{
printf("no build dir specified\n");
return false;
}
}
else if (stricmp(argv[i], "-g") == 0)
{
if (i + 1 < argc)
{
if (strcpy_s(config->generator, GERNERATOR_MAX_LEN, argv[i + 1]))
{
printf("invalid or error happened when parsing generator\n");
return false;
}
i++;
}
else
{
printf("no generator specified\n");
return false;
}
}
else if (stricmp(argv[i], "-m") == 0)
{
if (i + 1 < argc)
{
if (stricmp(argv[i + 1], "debug") == 0)
config->mode = CMAKE_AUTO_MODE_DEBUG;
else if (stricmp(argv[i + 1], "release") == 0)
config->mode = CMAKE_AUTO_MODE_RELEASE;
else
{
printf("no build mode specified\n");
return false;
}
i++;
}
else
{
printf("no build mode specified\n");
return false;
}
}
else if (stricmp(argv[i], "-a") == 0)
{
if (i + 1 < argc)
{
if (stricmp(argv[i + 1], "x86") == 0)
config->arch = CMAKE_AUTO_ARCH_X86;
else if (stricmp(argv[i + 1], "x64") == 0)
config->arch = CMAKE_AUTO_ARCH_X64;
else
{
printf("no build architecture specified\n");
return false;
}
i++;
}
else
{
printf("no build architecture specified\n");
return false;
}
}
else if (stricmp(argv[i], "-ei") == 0)
{
if (i + 1 < argc)
{
config->extra_init_args = argv[i + 1];
i++;
}
else
{
printf("no extra init params specified\n");
return false;
}
}
else if (stricmp(argv[i], "-h") == 0)
{
config->action = CMAKE_AUTO_ACTION_HELP;
return true;
}
else
{
printf("unknown argument: %s\n", argv[i]);
return false;
}
}
if (!config->builddir ||
!config->srcdir ||
config->mode == CMAKE_AUTO_MODE_UNKNOWN ||
config->action == CMAKE_AUTO_ACTION_UNKNOWN ||
config->arch == CMAKE_AUTO_ARCH_UNKONWN)
{
printf("invalid build arguments\n");
return false;
}
}
else if (stricmp(argv[1], "template") == 0)
{
config->action = CMAKE_AUTO_ACTION_TEMPLATE;
for (int i = 2; i < argc; i++)
{
if (stricmp(argv[i], "-t") == 0)
{
if (i + 1 < argc)
{
config->template = argv[i + 1];
i++;
}
else
{
printf("no template specified\n");
return false;
}
}
else if (stricmp(argv[i], "-h") == 0)
{
config->action = CMAKE_AUTO_ACTION_HELP;
return true;
}
else
{
printf("unknown argument: %s\n", argv[i]);
return false;
}
}
if (!config->template)
{
printf("no template specified\n");
return false;
}
}
return true;
}