-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmakeauto.c
702 lines (614 loc) · 17.5 KB
/
cmakeauto.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
#include "cmakeauto.h"
bool cma_abspath(char *buf, size_t size, const char *path)
{
#ifdef _WIN32
return _fullpath(buf, path, size) != NULL;
#elif __linux__
return realpath(path, buf) != NULL;
#else
#error unsupported platform
#endif
}
bool cma_create_dir_include_existing(const char *path)
{
#ifdef _WIN32
return CreateDirectoryA(path, NULL) != 0 || GetLastError() == ERROR_ALREADY_EXISTS;
#elif __linux__
return mkdir(path, 0755) == 0 || errno == EEXIST;
#else
#error unsupported platform
#endif
}
bool cma_file_exists(const char *path)
{
#ifdef _WIN32
DWORD attr = GetFileAttributesA(path);
return attr != INVALID_FILE_ATTRIBUTES && !(attr & FILE_ATTRIBUTE_DIRECTORY);
#elif __linux__
struct stat st = {0};
return stat(path, &st) != -1 && !S_ISDIR(st.st_mode);
#else
#error unsupported platform
#endif
}
bool cma_folder_exists(const char *path)
{
#ifdef _WIN32
DWORD attr = GetFileAttributesA(path);
return attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY);
#elif __linux__
struct stat st = {0};
return stat(path, &st) != -1 && S_ISDIR(st.st_mode);
#else
#error unsupported platform
#endif
}
bool cma_copy_file(const char *src, const char *dst)
{
#ifdef _WIN32
return CopyFileA(src, dst, FALSE) != 0;
#elif __linux__
int srcfd = open(src, O_RDONLY);
if (srcfd == -1)
return false;
int dstfd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (dstfd == -1)
{
close(srcfd);
return false;
}
char buf[4096];
ssize_t bytes_read;
while ((bytes_read = read(srcfd, buf, sizeof(buf))) > 0)
{
char *p = buf;
while (bytes_read > 0)
{
ssize_t bytes_written = write(dstfd, p, bytes_read);
if (bytes_written == -1)
{
close(srcfd);
close(dstfd);
return false;
}
bytes_read -= bytes_written;
p += bytes_written;
}
}
close(srcfd);
close(dstfd);
return true;
#else
#error unsupported platform
#endif
}
bool cma_get_current_process_absfilepath(char *buf, size_t size)
{
#ifdef _WIN32
return GetModuleFileNameA(NULL, buf, size) != 0;
#elif __linux__
return readlink("/proc/self/exe", buf, size) != 0;
#else
#error unsupported platform
#endif
}
bool cma_get_workdir(char *buf, size_t size)
{
#ifdef _WIN32
return GetCurrentDirectoryA(size, buf) != 0;
#elif __linux__
return getcwd(buf, size) != NULL;
#else
#error unsupported platform
#endif
}
void cma_iterate_dir(const char *abspath,
const char *relpath,
void *userdata,
bool should_iter_sub_folder,
bool (*callback)(const char *abspath,
const char *relpath,
const char *name,
bool isfolder /* 0 = file, 1 = folder */,
void *userdata))
{
#ifdef _WIN32
char abspath_search[FILE_MAX_PATH + 1];
strcpy_s(abspath_search, FILE_MAX_PATH, abspath);
strcat_s(abspath_search, FILE_MAX_PATH, FILE_SEPERATOR "*.*");
WIN32_FIND_DATA finddata;
HANDLE findhandle = FindFirstFileA(abspath_search, &finddata);
if (findhandle == INVALID_HANDLE_VALUE)
return;
do
{
if (strcmp(finddata.cFileName, ".") == 0 || strcmp(finddata.cFileName, "..") == 0)
continue;
char fileabspath[FILE_MAX_PATH + 1];
sprintf_s(fileabspath, FILE_MAX_PATH, "%s" FILE_SEPERATOR "%s", abspath, finddata.cFileName);
char filerelpath[FILE_MAX_PATH + 1];
sprintf_s(filerelpath, FILE_MAX_PATH, "%s" FILE_SEPERATOR "%s", relpath, finddata.cFileName);
if (finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (should_iter_sub_folder)
cma_iterate_dir(fileabspath, filerelpath, userdata, should_iter_sub_folder, callback);
if (!callback(fileabspath, filerelpath, finddata.cFileName, true, userdata))
break;
}
else
{
if (!callback(fileabspath, filerelpath, finddata.cFileName, false, userdata))
break;
}
} while (FindNextFileA(findhandle, &finddata));
FindClose(findhandle);
#elif __linux__
DIR *directory = opendir(abspath);
if (directory == NULL)
{
printf("failed to open directory %s\n", abspath);
return;
}
struct dirent *entry;
while ((entry = readdir(directory)) != NULL)
{
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
char fileabspath[FILE_MAX_PATH + 1];
sprintf_s(fileabspath, FILE_MAX_PATH, "%s/%s", abspath, entry->d_name);
char filerelpath[FILE_MAX_PATH + 1];
sprintf_s(filerelpath, FILE_MAX_PATH, "%s/%s", relpath, entry->d_name);
if (entry->d_type == DT_DIR)
{
if (should_iter_sub_folder)
cma_iterate_dir(fileabspath, filerelpath, userdata, should_iter_sub_folder, callback);
if (!callback(fileabspath, filerelpath, entry->d_name, true, userdata))
break;
}
else
{
if (!callback(fileabspath, filerelpath, entry->d_name, false, userdata))
break;
}
}
closedir(directory);
#else
#error unsupported platform
#endif
}
bool cma_create_process(char *filename,
char *cmdline,
char *posix_args[],
char *workdir,
void **processhandle,
unsigned int *pid)
{
#ifdef _WIN32
STARTUPINFOA si = {0};
si.cb = sizeof(si);
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
si.dwFlags |= STARTF_USESTDHANDLES;
PROCESS_INFORMATION pi = {0};
if (!CreateProcessA(filename, cmdline, NULL, NULL, TRUE, 0, NULL, workdir, &si, &pi))
{
printf("failed to create process reason: %d\n", GetLastError());
return false;
}
if (processhandle)
*processhandle = pi.hProcess;
else
CloseHandle(pi.hProcess);
if (pid)
*pid = pi.dwProcessId;
CloseHandle(pi.hThread);
#elif __linux__
char filepath[FILE_MAX_PATH + 1];
cma_abspath(filepath, FILE_MAX_PATH, filename);
int pid_ = fork();
if (pid_ == 0) // child process
{
if (workdir)
chdir(workdir);
if (posix_args)
execv(filepath, posix_args);
else
execl(filepath, (const char *)"", (char *)NULL);
printf("failed to create process reason: %d\n", errno);
return false;
}
else if (pid_ == -1)
{
printf("failed to create process reason: %d\n", errno);
return false;
}
else
{
if (pid)
*pid = (unsigned int)pid_;
}
return true;
#else
#error unsupported platform
#endif
return true;
}
#ifdef __linux__
bool cma_watch_add_folder_callback(const char *abspath, // return false to stop iterating
const char *relpath,
const char *name,
bool isfolder /* 0 = file, 1 = folder */,
void *userdata)
{
CMakeAutoConfig *config = (CMakeAutoConfig *)userdata;
if (isfolder)
{
if (config->watchfolderhandles_count >= WATCHFOLDER_MAX_LEN)
{
printf("max watch folders reached\n");
return false;
}
config->watchfolderhandles[config->watchfolderhandles_count] =
inotify_add_watch(config->watchfolderhandles[0], abspath, IN_CREATE | IN_MODIFY | IN_DELETE);
config->watchfolderhandles_count++;
}
return true;
}
void cma_watch_remove_all_subfolders(CMakeAutoConfig *config)
{
for (unsigned int i = config->watchfolders_count + 1; i < config->watchfolderhandles_count; i++)
inotify_rm_watch(config->watchfolderhandles[0], config->watchfolderhandles[i]);
config->watchfolderhandles_count = config->watchfolders_count + 1;
}
#endif
bool cma_watch_folder_init(CMakeAutoConfig *config)
{
#ifdef _WIN32
config->watchfolderhandles_count = config->watchfolders_count;
config->watchfolderhandles = (watchfolderhandle_t *)malloc(sizeof(watchfolderhandle_t) * config->watchfolderhandles_count);
for (unsigned int i = 0; i < config->watchfolderhandles_count; i++)
{
config->watchfolderhandles[i] = FindFirstChangeNotificationA(config->watchfolders[i],
TRUE,
FILE_NOTIFY_CHANGE_LAST_WRITE |
FILE_NOTIFY_CHANGE_CREATION |
FILE_NOTIFY_CHANGE_FILE_NAME |
FILE_NOTIFY_CHANGE_DIR_NAME);
if (config->watchfolderhandles[i] == INVALID_HANDLE_VALUE)
{
printf("failed to watch folder %s\n", config->srcdir);
return false;
}
}
return true;
#elif __linux__
config->watchfolderhandles_count = config->watchfolders_count + 1;
config->watchfolderhandles = (watchfolderhandle_t *)malloc(sizeof(watchfolderhandle_t) * config->watchfolderhandles_count);
config->watchfolderhandles[0] = inotify_init();
if (config->watchfolderhandles[0] == -1)
{
printf("failed to watch folder %s\n", config->srcdir);
return false;
}
for (unsigned int i = 0; i < config->watchfolders_count; i++)
{
config->watchfolderhandles[i + 1] = inotify_add_watch(config->watchfolderhandles[0], config->watchfolders[i], IN_CREATE | IN_MODIFY | IN_DELETE);
if (config->watchfolderhandles[i + 1] < 0)
{
printf("failed to watch folder %s\n", config->srcdir);
return false;
}
cma_iterate_dir(config->watchfolders[i], ".", config, true, cma_watch_add_folder_callback);
}
return true;
#else
#error unsupported platform
#endif
}
bool cma_watch_folder_wait_for_next_change(CMakeAutoConfig *config)
{
#ifdef _WIN32
while (true)
{
unsigned long wait_result = WaitForMultipleObjects(config->watchfolderhandles_count, config->watchfolderhandles, FALSE, INFINITE);
if (wait_result == WAIT_FAILED)
{
printf("failed to wait for folder change\n");
return false;
}
if (wait_result == WAIT_TIMEOUT)
continue;
if (wait_result >= WAIT_OBJECT_0 && wait_result < WAIT_OBJECT_0 + config->watchfolderhandles_count)
{
if (FindNextChangeNotification(config->watchfolderhandles[wait_result - WAIT_OBJECT_0]) == FALSE)
{
printf("failed to watch folder %s\n", config->srcdir);
return false;
}
break;
}
}
return true;
#elif __linux__
while (true)
{
char buf[4096] ALIGNAS(struct inotify_event);
const struct inotify_event *event;
unsigned long long len;
char *ptr;
len = read(config->watchfolderhandles[0], buf, sizeof(buf));
if (len == -1 && errno != EAGAIN)
{
printf("failed to read inotify event\n");
return false;
}
if (len <= 0)
{
printf("inotify event len <= 0\n");
return false;
}
for (ptr = buf; ptr < buf + len; ptr += sizeof(struct inotify_event) + event->len)
{
event = (const struct inotify_event *)ptr;
if (event->mask & (IN_CREATE | IN_DELETE))
{
if (event->mask & IN_ISDIR)
{
cma_watch_remove_all_subfolders(config);
for (unsigned int i = 0; i < config->watchfolders_count; i++)
cma_iterate_dir(config->watchfolders[i], ".", config, true, cma_watch_add_folder_callback);
}
}
}
break;
}
return true;
#else
#error unsupported platform
#endif
}
bool cma_watch_folder_close(CMakeAutoConfig *config)
{
#ifdef _WIN32
for (unsigned int i = 0; i < config->watchfolderhandles_count; i++)
FindCloseChangeNotification(config->watchfolderhandles[i]);
return true;
#elif __linux__
printf("closing watch folder\n");
for (unsigned int i = 1; i < config->watchfolderhandles_count; i++)
inotify_rm_watch(config->watchfolderhandles[0], config->watchfolderhandles[i]);
close(config->watchfolderhandles[0]);
return true;
#else
#error unsupported platform
#endif
}
bool cma_init_proj(CMakeAutoConfig *config)
{
bool is_generator_present = config->generator && strlen(config->generator) > 0;
char cmdline[1024];
memset(cmdline, 0, 1024);
sprintf_s(cmdline, 1024, "cmake -S \"%s\" -B \"%s\" %s%s%s %s %s %s",
config->srcdir,
config->builddir,
is_generator_present ? "-G \"" : "",
is_generator_present ? config->generator : "",
is_generator_present ? "\"" : "",
config->arch != CMAKE_AUTO_ARCH_UNKONWN ? "-A" : "",
config->arch
? config->arch == CMAKE_AUTO_ARCH_X86 ? "Win32"
: config->arch == CMAKE_AUTO_ARCH_X64 ? "x64"
: "Unknown"
: "",
config->extra_init_args ? config->extra_init_args : "");
#ifdef _WIN32
void *processhandle = 0;
if (!cma_create_process(NULL, cmdline, NULL, NULL, &processhandle, 0))
{
printf("failed to create cma_init_proj process\n");
return false;
}
printf("> %s\n", cmdline);
WaitForSingleObject(processhandle, INFINITE);
unsigned long exit_code = 0;
GetExitCodeProcess(processhandle, &exit_code);
printf("> exited with code %ld\n", exit_code);
#elif __linux
char workdir[FILE_MAX_PATH + 1];
memset(workdir, 0, FILE_MAX_PATH + 1);
cma_get_workdir(workdir, FILE_MAX_PATH);
char *args[] = {
"sh",
"-c",
(char *)cmdline,
0,
};
pid_t pid;
if (!cma_create_process("/bin/sh", NULL, args, workdir, NULL, (unsigned int *)&pid))
{
printf("failed to create cma_init_proj process\n");
return false;
}
printf("> ");
for (int i = 0; i < (sizeof(args) / sizeof(args[0])) - 1; i++)
{
if (args[i] == cmdline)
printf("\"%s\" ", cmdline);
else
printf("%s ", args[i]);
}
printf("\n");
int exit_code = 0;
if (waitpid(pid, &exit_code, 0) == -1)
{
printf("failed to wait for process\n");
return false;
}
printf("> exited with code %d\n", exit_code);
#else
#error unsupported platform
#endif
return exit_code == 0;
}
bool cma_build(CMakeAutoConfig *config)
{
char cmdline[1024];
memset(cmdline, 0, 1024);
sprintf_s(cmdline, 1024, "cmake --build \"%s\" --config %s %s",
config->builddir,
config->mode == CMAKE_AUTO_MODE_DEBUG ? "Debug"
: config->mode == CMAKE_AUTO_MODE_RELEASE ? "Release"
: "Unknown",
config->extra_build_args ? config->extra_build_args : "");
#ifdef _WIN32
void *processhandle = 0;
if (!cma_create_process(NULL, cmdline, NULL, NULL, &processhandle, 0))
{
printf("failed to create cma_init_proj process\n");
return false;
}
printf("> %s\n", cmdline);
WaitForSingleObject(processhandle, INFINITE);
unsigned long exit_code = 0;
GetExitCodeProcess(processhandle, &exit_code);
printf("> exited with code %ld\n", exit_code);
#elif __linux
char workdir[FILE_MAX_PATH + 1];
memset(workdir, 0, FILE_MAX_PATH + 1);
cma_get_workdir(workdir, FILE_MAX_PATH);
char *args[] = {
"sh",
"-c",
(char *)cmdline,
0,
};
pid_t pid;
if (!cma_create_process("/bin/sh", NULL, args, workdir, NULL, (unsigned int *)&pid))
{
printf("failed to create cma_init_proj process\n");
return false;
}
printf("> ");
for (int i = 0; i < (sizeof(args) / sizeof(args[0])) - 1; i++)
{
if (args[i] == cmdline)
printf("\"%s\" ", cmdline);
else
printf("%s ", args[i]);
}
printf("\n");
int exit_code = 0;
if (waitpid(pid, &exit_code, 0) == -1)
{
printf("failed to wait for process\n");
return false;
}
printf("> exited with code %d\n", exit_code);
#else
#error unsupported platform
#endif
return exit_code == 0;
}
bool copy_file_callback(const char *abspath, const char *relpath, const char *name, bool isfolder, void *userdata)
{
if (!isfolder)
{
char *folder = (char *)relpath;
while (folder = strchr(folder + 1, FILE_SEPERATOR_CHAR))
{
char relsubfolder[FILE_MAX_PATH + 1];
memset(relsubfolder, 0, FILE_MAX_PATH + 1);
memcpy_s(relsubfolder, FILE_MAX_PATH, relpath, folder - relpath);
char abssubfolder[FILE_MAX_PATH + 1];
memset(abssubfolder, 0, FILE_MAX_PATH + 1);
cma_abspath(abssubfolder, FILE_MAX_PATH, relsubfolder);
if (!cma_create_dir_include_existing(abssubfolder))
{
printf("failed to create directory %s\n", abssubfolder);
return false;
}
}
char absfile[FILE_MAX_PATH + 1];
memset(absfile, 0, FILE_MAX_PATH + 1);
cma_abspath(absfile, FILE_MAX_PATH, relpath);
if (!cma_copy_file(abspath, absfile))
{
printf("failed to copy file %s\n", abspath);
return false;
}
}
return true;
}
int main(int argc, char **argv)
{
CMakeAutoConfig config = {0};
if (!cma_parse_args(argc, argv, &config))
return -1;
printf("-------------------\n");
printf("action: %s\narch: %s\nmode: %s\nbuilddir: %s\nsrcdir: %s\noptions: %d\n",
config.action == CMAKE_AUTO_ACTION_BUILD ? "build"
: config.action == CMAKE_AUTO_ACTION_HELP ? "help"
: config.action == CMAKE_AUTO_ACTION_CONFIGURE ? "configure"
: config.action == CMAKE_AUTO_ACTION_TEMPLATE ? "template"
: "Unknown",
config.arch == CMAKE_AUTO_ARCH_X86 ? "x86"
: config.arch == CMAKE_AUTO_ARCH_X64 ? "x64"
: "Unknown",
config.mode == CMAKE_AUTO_MODE_DEBUG ? "Debug"
: config.mode == CMAKE_AUTO_MODE_RELEASE ? "Release"
: "Unknown",
config.builddir, config.srcdir, config.options);
printf("-------------------\n\n");
switch (config.action)
{
case CMAKE_AUTO_ACTION_BUILD:
{
if (config.options & CMAKE_AUTO_OPTION_AUTO_RELOAD)
if (!cma_watch_folder_init(&config))
{
printf("failed to init watch folder\n");
break;
}
do
{
if (cma_init_proj(&config))
cma_build(&config);
if (config.options & CMAKE_AUTO_OPTION_AUTO_RELOAD)
if (!cma_watch_folder_wait_for_next_change(&config))
printf("failed to wait for next change\n");
} while (config.options & CMAKE_AUTO_OPTION_AUTO_RELOAD);
if (config.options & CMAKE_AUTO_OPTION_AUTO_RELOAD)
if (!cma_watch_folder_close(&config))
printf("failed to close watch folder\n");
break;
}
case CMAKE_AUTO_ACTION_CONFIGURE:
cma_init_proj(&config);
break;
case CMAKE_AUTO_ACTION_TEMPLATE:
{
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 -2;
strrchr(buf, FILE_SEPERATOR_CHAR)[1] = 0;
strcat_s(buf, FILE_MAX_PATH, "templates" FILE_SEPERATOR);
strcat_s(buf, FILE_MAX_PATH, config.template);
if (!cma_folder_exists(buf))
{
printf("template not found\n");
break;
}
cma_iterate_dir(buf, ".", 0, true, copy_file_callback);
printf("template copied\n");
break;
}
default:
printf("Unknown action\n");
case CMAKE_AUTO_ACTION_HELP:
cma_print_usage();
break;
}
return 0;
}