-
Notifications
You must be signed in to change notification settings - Fork 1
/
anvil.c
executable file
·581 lines (459 loc) · 16.3 KB
/
anvil.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
/*
anvil 🔨 by sandwichman - slim, source-driven build tool for C
to build the project, just compile and run this file!
*/
char* project_name = "comet"; // final executable name
char* build_dir = "build"; // folder for intermediate .o files
char* source_dirs[] = { // source code folders
"src",
};
char* cc = "gcc"; // (optional) c compiler / linker to use (defaults to what anvil.c is compiled with)
char* output_dir = ""; // (optional) folder to drop the final executable in (defaults to the main folder)
char* flags = "-O3"; // (optional) c compiler flags
char* include_dir = "src"; // (optional) c include path
char* link_flags = "-lm -lc"; // (optional) linker flags
int transparency_mode = 0; // (optional) if not zero, print the executed commands instead of nice messages
//////////////////////////////////////////////////////////////////////////////
/*
TODO setting for recursive search in source code folders
TODO read dependency files for incremental compilation
TODO maybe support for multiple build recipes
here be dragons
*/
// this is basically my orbit.h header but slimmed down and copied in
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
typedef uint64_t u64;
typedef uint32_t u32;
typedef uint16_t u16;
typedef uint8_t u8;
typedef int64_t i64;
typedef int32_t i32;
typedef int16_t i16;
typedef int8_t i8;
typedef float f32;
typedef double f64;
typedef uint8_t bool;
#define false ((bool)0)
#define true ((bool)!false)
#define CRASH(msg) do { \
printf("\x1b[31m\x1b[1mCRASH\x1b[0m: \"%s\" at %s:%d\n", (msg), (__FILE__), (__LINE__)); \
exit(EXIT_FAILURE); } while (0)
#define FOR_RANGE(iterator, start, end) for (intptr_t iterator = (start); iterator < (end); iterator++)
// #define CSTRING_COMPATIBILITY_MODE
// ^ allocates an extra character for null termination outside the string bounds.
// probably recommended if you interface a lot with standard C APIs and dont want clone_to_cstring allocations everywhere.
typedef struct string_s {
char* raw;
u32 len;
} string;
#define NULL_STR ((string){NULL, 0})
#define is_null_str(str) ((str).raw == NULL)
#define str_fmt "%.*s"
#define str_arg(str) (int)(str).len, (str).raw
#define substring_len(str, start, len) ((string){(str).raw + (start), (len)})
#define can_be_cstring(str) ((str).raw[(str).len] == '\0')
#define to_string(cstring) ((string){(cstring), strlen((cstring))})
char* clone_to_cstring(string str); // this allocates
string string_alloc(size_t len);
#define string_free(str) free(str.raw)
string string_clone(string str); // this allocates as well
bool string_eq(string a, string b);
bool string_ends_with(string source, string ending);
typedef u8 fs_file_type; enum {
oft_invalid = 0,
oft_regular,
oft_directory,
oft_pipe,
oft_symlink,
oft_other,
};
typedef struct fs_file_s {
string path;
size_t size;
FILE* handle;
fs_file_type type;
bool opened;
} fs_file;
bool fs_exists(string path);
bool fs_get(string path, fs_file* file);
bool fs_drop(fs_file* file);
#ifdef _WIN32
#define realpath(N,R) _fullpath((R),(N),_MAX_PATH)
#endif
bool fs_close(fs_file* file);
#define fs_is_regular(file) ((file)->type == oft_regular)
#define fs_is_directory(file) ((file)->type == oft_directory)
int fs_subfile_count(fs_file* file);
bool fs_get_subfiles(fs_file* file, fs_file* file_array);
#define MAX_CMD 1000
char cmd[MAX_CMD] = {0};
char objects[MAX_CMD] = {0};
// realpath()s of the source directories that i need because filesystem shit is so dumb
char* real_src_dirs[sizeof(source_dirs) / sizeof(*source_dirs)];
char* real_build_dir;
char* real_include_dir;
char* real_output_dir;
#define error(msg, ...) do { \
printf("error: "); \
printf(msg __VA_OPT__(,) __VA_ARGS__); \
printf("\n"); \
exit(EXIT_FAILURE); \
} while (0)
void clear(char* buf) {
while(*buf != '\0') *(buf++) = '\0';
}
int execute(char* command) {
if (transparency_mode) printf("%s\n", command);
return system(command);
}
int main() {
// detect compiler if not provided
if (cc[0] == '\0') {
# if defined(__clang__)
cc = "clang";
# elif defined(_MSC_VER)
cc = "cl.exe";
# elif defined(__GNUC__)
cc = "gcc";
# elif
error("cannot detect c compiler, please explicitly set 'cc' in anvil.c");
# endif
printf("compiler detected '%s'\n", cc);
}
// check params and set defaults
if (build_dir[0] == '\0') error("an explicit build path must be provided");
if (output_dir[0] == '\0') output_dir = "./";
// translate relative paths into realpaths (im so fucking done)
real_output_dir = malloc(PATH_MAX);
realpath(output_dir, real_output_dir);
real_build_dir = malloc(PATH_MAX);
realpath(build_dir, real_build_dir);
real_include_dir = malloc(PATH_MAX);
if (include_dir[0] == '\0') real_include_dir = "";
else realpath(include_dir, real_include_dir);
// clean build directory
if (fs_exists(to_string(build_dir))) {
strcat(cmd, "rm -rf ");
strcat(cmd, build_dir);
execute(cmd);
clear(cmd);
}
// create build directory
{
strcat(cmd, "mkdir ");
strcat(cmd, build_dir);
execute(cmd);
clear(cmd);
}
FOR_RANGE(i, 0, sizeof(source_dirs) / sizeof(*source_dirs)) {
fs_file source_directory;
if (!fs_get(to_string(source_dirs[i]), &source_directory))
error("could not open source directory '%s'", source_dirs[i]);
if (!fs_is_directory(&source_directory))
error("source directory '%s' is not a directory", source_dirs[i]);
real_src_dirs[i] = malloc(PATH_MAX);
realpath(source_dirs[i], real_src_dirs[i]);
fs_drop(&source_directory);
}
if (!transparency_mode) printf("compiling using %s with flags %s\n", cc, flags);
// count the files
int total_files_to_build = 0;
FOR_RANGE(i, 0, sizeof(source_dirs) / sizeof(*source_dirs)) {
fs_file source_directory;
if (!fs_get(to_string(real_src_dirs[i]), &source_directory))
error("could not open source directory '%s'", source_dirs[i]);
int src_dir_subfile_count = fs_subfile_count(&source_directory);
if (src_dir_subfile_count == 0) {
fs_drop(&source_directory);
continue;
}
fs_file* src_dir_subfiles = malloc(sizeof(fs_file) * src_dir_subfile_count);
fs_get_subfiles(&source_directory, src_dir_subfiles);
FOR_RANGE(j, 0, src_dir_subfile_count) {
if (!fs_is_regular(&src_dir_subfiles[j])) continue;
if (string_ends_with(src_dir_subfiles[j].path, to_string(".c"))) {
total_files_to_build++;
}
}
FOR_RANGE(j, 0, src_dir_subfile_count) fs_drop(&src_dir_subfiles[j]);
free(src_dir_subfiles);
fs_drop(&source_directory);
}
// build the individual object files
int file_num = 1;
FOR_RANGE(i, 0, sizeof(source_dirs) / sizeof(*source_dirs)) {
fs_file source_directory;
if (!fs_get(to_string(real_src_dirs[i]), &source_directory))
error("could not open source directory '%s'", source_dirs[i]);
int src_dir_subfile_count = fs_subfile_count(&source_directory);
if (src_dir_subfile_count == 0) {
fs_drop(&source_directory);
continue;
}
fs_file* src_dir_subfiles = malloc(sizeof(fs_file) * src_dir_subfile_count);
fs_get_subfiles(&source_directory, src_dir_subfiles);
FOR_RANGE(j, 0, src_dir_subfile_count) {
if (!fs_is_regular(&src_dir_subfiles[j])) continue;
if (string_ends_with(src_dir_subfiles[j].path, to_string(".c"))) {
// build file!
char* slashchar;
if (real_src_dirs[i][strlen(real_src_dirs[i])-1] == '\\' ||
real_src_dirs[i][strlen(real_src_dirs[i])-1] == '/') {
slashchar = "";
} else {
#if defined(_WIN32) || defined(_WIN64)
slashchar = "\\";
#else
slashchar = "/";
#endif
}
printf("[ %d / %d ] ", file_num++, total_files_to_build);
if (!transparency_mode) printf("compiling %s%s"str_fmt"\n", real_src_dirs[i], slashchar, str_arg(src_dir_subfiles[j].path));
string outpath = string_clone(src_dir_subfiles[j].path);
outpath.raw[outpath.len-1] = 'o';
sprintf(cmd, "%s -c -o %s/"str_fmt" %s/"str_fmt" %s",
cc, real_build_dir, str_arg(outpath), real_src_dirs[i], str_arg(src_dir_subfiles[j].path), flags);
if (real_include_dir[0] != '\0') {
strcat(cmd, " -I");
strcat(cmd, real_include_dir);
}
// record object file name for link stage
sprintf(&objects[strlen(objects)], "%s/"str_fmt" ", real_build_dir, str_arg(outpath));
if (execute(cmd)) {
error("building "str_fmt" failed", str_arg(src_dir_subfiles[j].path));
}
clear(cmd);
}
}
FOR_RANGE(j, 0, src_dir_subfile_count) fs_drop(&src_dir_subfiles[j]);
free(src_dir_subfiles);
fs_drop(&source_directory);
}
if (!transparency_mode) printf("linking using %s with flags %s\n", cc, link_flags);
char output_name[PATH_MAX] = {0};
sprintf(output_name, "%s/%s", real_output_dir, project_name);
if (fs_exists(to_string(output_name))) {
strcat(cmd, "rm ");
strcat(cmd, output_name);
execute(cmd);
clear(cmd);
}
sprintf(cmd, "%s %s -o %s %s",
cc, objects, output_name, link_flags
);
if (system(cmd)) {
error("linking failed");
}
clear(cmd);
if (!transparency_mode) printf("%s built successfully!\n", project_name);
exit(EXIT_SUCCESS);
}
bool string_ends_with(string source, string ending) {
if (source.len < ending.len) return false;
return string_eq(substring_len(source, source.len-ending.len, ending.len), ending);
}
string string_alloc(size_t len) {
#ifdef CSTRING_COMPATIBILITY_MODE
char* raw = malloc(len + 1);
#else
char* raw = malloc(len);
#endif
if (raw == NULL) return NULL_STR;
memset(raw, ' ', len);
#ifdef CSTRING_COMPATIBILITY_MODE
raw[len] = '\0';
#endif
return (string){raw, len};
}
bool string_eq(string a, string b) {
if (a.len != b.len) return false;
FOR_RANGE(i, 0, a.len) {
if (a.raw[i] != b.raw[i]) return false;
}
return true;
}
char* clone_to_cstring(string str) {
if (is_null_str(str)) return "";
char* cstr = malloc(str.len + 1);
if (cstr == NULL) return NULL;
memcpy(cstr, str.raw, str.len);
cstr[str.len] = '\0';
return cstr;
}
string string_clone(string str) {
string new_str = string_alloc(str.len);
if (memmove(new_str.raw, str.raw, str.len) != new_str.raw) return NULL_STR;
return new_str;
}
bool fs_exists(string path) {
struct stat statbuffer;
bool exists;
if (can_be_cstring(path)) {
exists = stat(path.raw, &statbuffer) == 0;
} else {
char* path_cstr = clone_to_cstring(path);
exists = stat(path_cstr, &statbuffer) == 0;
free(path_cstr);
}
return exists;
}
bool fs_get(string path, fs_file* file) {
struct stat statbuffer;
{
bool exists;
if (can_be_cstring(path)) {
exists = stat(path.raw, &statbuffer) == 0;
} else {
char* path_cstr = clone_to_cstring(path);
exists = stat(path_cstr, &statbuffer) == 0;
free(path_cstr);
}
if (!exists) return false;
}
file->path = string_clone(path);
file->size = statbuffer.st_size;
#ifdef S_ISREG
if (S_ISREG(statbuffer.st_mode)) file->type = oft_regular;
#endif
#ifdef S_ISDIR
else if (S_ISDIR(statbuffer.st_mode)) file->type = oft_directory;
#endif
#ifdef S_ISLNK
else if (S_ISLNK(statbuffer.st_mode)) file->type = oft_symlink;
#endif
#ifdef S_ISFIFO
else if (S_ISFIFO(statbuffer.st_mode)) file->type = oft_pipe;
#endif
else file->type = oft_other;
file->handle = NULL;
file->opened = false;
return true;
}
bool fs_create(string path, fs_file_type type, fs_file* file) {
if (fs_exists(path)) return false;
bool creation_success = false;
FILE* handle = NULL;
switch (type) {
case oft_directory:
if (can_be_cstring(path)) {
creation_success = mkdir(path.raw
#if !(defined(MINGW32) || defined(__MINGW32__))
, S_IRWXU | S_IRWXG | S_IRWXO
#endif
) == 0;
} else {
char* path_cstr = clone_to_cstring(path);
creation_success = mkdir(path_cstr
#if !(defined(MINGW32) || defined(__MINGW32__))
, S_IRWXU | S_IRWXG | S_IRWXO
#endif
) == 0;
free(path_cstr);
}
if (!creation_success) return false;
break;
case oft_regular:
if (can_be_cstring(path)) {
handle = fopen(path.raw, "w");
} else {
char* path_cstr = clone_to_cstring(path);
handle = fopen(path_cstr, "w");
free(path_cstr);
}
if (handle == NULL) return false;
break;
case oft_symlink: CRASH("orbitfs does not currently support creating symlinks\n");
case oft_pipe: CRASH("orbitfs does not currently support creating pipes\n");
default:
return false;
}
if (handle != NULL) fclose(handle);
return fs_get(path, file);
}
bool fs_drop(fs_file* file) {
if (file->opened) {
bool closed = fs_close(file);
if (!closed) return false;
}
string_free(file->path);
*file = (fs_file){0};
file->handle = NULL;
return true;
}
bool fs_close(fs_file* file) {
if (!file->opened) return false;
bool closed = fclose(file->handle) == 0;
if (!closed) return false;
file->handle = NULL;
file->opened = false;
return true;
}
int fs_subfile_count(fs_file* file) {
int count = 0;
if (!fs_is_directory(file)) return 0;
DIR* d;
struct dirent* dir;
if (can_be_cstring(file->path)) {
d = opendir(file->path.raw);
} else {
char* path_cstr = clone_to_cstring(file->path);
d = opendir(path_cstr);
free(path_cstr);
}
if (!d) return false;
while ((dir = readdir(d)) != NULL) {
if (strcmp(dir->d_name, ".") == 0) continue;
if (strcmp(dir->d_name, "..") == 0) continue;
count++;
}
closedir(d);
return count; // account for default directories
}
bool fs_get_subfiles(fs_file* file, fs_file* file_array) {
char file_realpath[PATH_MAX] = {0};
realpath(clone_to_cstring(file->path), file_realpath);
if (!fs_is_directory(file)) return false;
DIR* directory;
struct dirent* dir_entry;
if (can_be_cstring(file->path)) {
directory = opendir(file->path.raw);
} else {
char* path_cstr = clone_to_cstring(file->path);
directory = opendir(path_cstr);
free(path_cstr);
}
if (!directory) return false;
if (can_be_cstring(file->path)) {
chdir(file->path.raw);
} else {
char* path_cstr = clone_to_cstring(file->path);
chdir(path_cstr);
free(path_cstr);
}
for (int i = 0; (dir_entry = readdir(directory)) != NULL;) {
if (strcmp(dir_entry->d_name, ".") == 0) continue;
if (strcmp(dir_entry->d_name, "..") == 0) continue;
//string temp1 = string_concat(file->path, to_string("/"));
//string path = string_concat(temp1, to_string(dir_entry->d_name));
//printf("\ny\n[%s]\n\n", dir_entry->d_name);
string path = to_string(dir_entry->d_name);
bool success = fs_get(path, &file_array[i]);
if (!success) {
chdir(file_realpath);
return false;
}
//string_free(temp1);
// string_free(path);
i++;
}
chdir(file_realpath);
chdir("..");
closedir(directory);
return true;
}