-
Notifications
You must be signed in to change notification settings - Fork 99
/
nob.h
1487 lines (1279 loc) · 49.6 KB
/
nob.h
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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* nob - v1.6.0 - Public Domain - https://github.com/tsoding/nob
This library is the next generation of the [NoBuild](https://github.com/tsoding/nobuild) idea.
# Quick Example
```c
// nob.c
#define NOB_IMPLEMENTATION
#include "nob.h"
int main(int argc, char **argv)
{
NOB_GO_REBUILD_URSELF(argc, argv);
Nob_Cmd cmd = {0};
nob_cmd_append(&cmd, "cc", "-Wall", "-Wextra", "-o", "main", "main.c");
if (!nob_cmd_run_sync(cmd)) return 1;
return 0;
}
```
```console
$ cc -o nob nob.c
$ ./nob
```
The `nob` automatically rebuilds itself if `nob.c` is modified thanks to
the `NOB_GO_REBUILD_URSELF` macro (don't forget to check out how it works below)
# Stripping off `nob_` Prefixes
Since Pure C does not have any namespaces we prefix each name of the API with the `nob_` to avoid any
potential conflicts with any other names in your code. But sometimes it is very annoying and makes
the code noisy. If you know that none of the names from nob.h conflict with anything in your code
you can enable NOB_STRIP_PREFIX macro and just drop all the prefixes:
```c
// nob.c
#define NOB_IMPLEMENTATION
#define NOB_STRIP_PREFIX
#include "nob.h"
int main(int argc, char **argv)
{
NOB_GO_REBUILD_URSELF(argc, argv);
Cmd cmd = {0};
cmd_append(&cmd, "cc", "-Wall", "-Wextra", "-o", "main", "main.c");
if (!cmd_run_sync(cmd)) return 1;
return 0;
}
```
Not all the names have strippable prefixes. All the redefinable names like `NOB_GO_REBUILD_URSELF`
for instance will retain their prefix even if NOB_STRIP_PREFIX is enabled. Notable exception is the
nob_log() function. Stripping away the prefix results in log() which was historically always referring
to the natural logarithmic function that is already defined in math.h. So there is no reason to strip
off the prefix for nob_log().
The prefixes are stripped off only on the level of preprocessor. The names of the functions in the
compiled object file will still retain the `nob_` prefix. Keep that in mind when you FFI with nob.h
from other languages (for whatever reason).
If only few specific names create conflicts for you, you can just #undef those names after the
`#include <nob.h>` since they are macros anyway.
*/
#ifndef NOB_H_
#define NOB_H_
#define NOB_ASSERT assert
#define NOB_REALLOC realloc
#define NOB_FREE free
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <limits.h>
#ifdef _WIN32
# define WIN32_LEAN_AND_MEAN
# define _WINUSER_
# define _WINGDI_
# define _IMM_
# define _WINCON_
# include <windows.h>
# include <direct.h>
# include <shellapi.h>
#else
# include <sys/types.h>
# include <sys/wait.h>
# include <sys/stat.h>
# include <unistd.h>
# include <fcntl.h>
#endif
#ifdef _WIN32
# define NOB_LINE_END "\r\n"
#else
# define NOB_LINE_END "\n"
#endif
#define NOB_UNUSED(value) (void)(value)
#define NOB_TODO(message) do { fprintf(stderr, "%s:%d: TODO: %s\n", __FILE__, __LINE__, message); abort(); } while(0)
#define NOB_UNREACHABLE(message) do { fprintf(stderr, "%s:%d: UNREACHABLE: %s\n", __FILE__, __LINE__, message); abort(); } while(0)
#define NOB_ARRAY_LEN(array) (sizeof(array)/sizeof(array[0]))
#define NOB_ARRAY_GET(array, index) \
(NOB_ASSERT((size_t)index < NOB_ARRAY_LEN(array)), array[(size_t)index])
typedef enum {
NOB_INFO,
NOB_WARNING,
NOB_ERROR,
NOB_NO_LOGS,
} Nob_Log_Level;
// Any messages with the level below nob_minimal_log_level are going to be suppressed.
extern Nob_Log_Level nob_minimal_log_level;
void nob_log(Nob_Log_Level level, const char *fmt, ...);
// It is an equivalent of shift command from bash. It basically pops an element from
// the beginning of a sized array.
#define nob_shift(xs, xs_sz) (NOB_ASSERT((xs_sz) > 0), (xs_sz)--, *(xs)++)
// NOTE: nob_shift_args() is an alias for an old variant of nob_shift that only worked with
// the command line arguments passed to the main() function. nob_shift() is more generic.
// So nob_shift_args() is semi-deprecated, but I don't see much reason to urgently
// remove it. This alias does not hurt anybody.
#define nob_shift_args(argc, argv) nob_shift(*argv, *argc)
typedef struct {
const char **items;
size_t count;
size_t capacity;
} Nob_File_Paths;
typedef enum {
NOB_FILE_REGULAR = 0,
NOB_FILE_DIRECTORY,
NOB_FILE_SYMLINK,
NOB_FILE_OTHER,
} Nob_File_Type;
bool nob_mkdir_if_not_exists(const char *path);
bool nob_copy_file(const char *src_path, const char *dst_path);
bool nob_copy_directory_recursively(const char *src_path, const char *dst_path);
bool nob_read_entire_dir(const char *parent, Nob_File_Paths *children);
bool nob_write_entire_file(const char *path, const void *data, size_t size);
Nob_File_Type nob_get_file_type(const char *path);
#define nob_return_defer(value) do { result = (value); goto defer; } while(0)
// Initial capacity of a dynamic array
#ifndef NOB_DA_INIT_CAP
#define NOB_DA_INIT_CAP 256
#endif
// Append an item to a dynamic array
#define nob_da_append(da, item) \
do { \
if ((da)->count >= (da)->capacity) { \
(da)->capacity = (da)->capacity == 0 ? NOB_DA_INIT_CAP : (da)->capacity*2; \
(da)->items = NOB_REALLOC((da)->items, (da)->capacity*sizeof(*(da)->items)); \
NOB_ASSERT((da)->items != NULL && "Buy more RAM lol"); \
} \
\
(da)->items[(da)->count++] = (item); \
} while (0)
#define nob_da_free(da) NOB_FREE((da).items)
// Append several items to a dynamic array
#define nob_da_append_many(da, new_items, new_items_count) \
do { \
if ((da)->count + (new_items_count) > (da)->capacity) { \
if ((da)->capacity == 0) { \
(da)->capacity = NOB_DA_INIT_CAP; \
} \
while ((da)->count + (new_items_count) > (da)->capacity) { \
(da)->capacity *= 2; \
} \
(da)->items = NOB_REALLOC((da)->items, (da)->capacity*sizeof(*(da)->items)); \
NOB_ASSERT((da)->items != NULL && "Buy more RAM lol"); \
} \
memcpy((da)->items + (da)->count, (new_items), (new_items_count)*sizeof(*(da)->items)); \
(da)->count += (new_items_count); \
} while (0)
typedef struct {
char *items;
size_t count;
size_t capacity;
} Nob_String_Builder;
bool nob_read_entire_file(const char *path, Nob_String_Builder *sb);
// Append a sized buffer to a string builder
#define nob_sb_append_buf(sb, buf, size) nob_da_append_many(sb, buf, size)
// Append a NULL-terminated string to a string builder
#define nob_sb_append_cstr(sb, cstr) \
do { \
const char *s = (cstr); \
size_t n = strlen(s); \
nob_da_append_many(sb, s, n); \
} while (0)
// Append a single NULL character at the end of a string builder. So then you can
// use it a NULL-terminated C string
#define nob_sb_append_null(sb) nob_da_append_many(sb, "", 1)
// Free the memory allocated by a string builder
#define nob_sb_free(sb) NOB_FREE((sb).items)
// Process handle
#ifdef _WIN32
typedef HANDLE Nob_Proc;
#define NOB_INVALID_PROC INVALID_HANDLE_VALUE
#else
typedef int Nob_Proc;
#define NOB_INVALID_PROC (-1)
#endif // _WIN32
typedef struct {
Nob_Proc *items;
size_t count;
size_t capacity;
} Nob_Procs;
bool nob_procs_wait(Nob_Procs procs);
bool nob_procs_wait_and_reset(Nob_Procs *procs);
// Wait until the process has finished
bool nob_proc_wait(Nob_Proc proc);
// A command - the main workhorse of Nob. Nob is all about building commands an running them
typedef struct {
const char **items;
size_t count;
size_t capacity;
} Nob_Cmd;
// Render a string representation of a command into a string builder. Keep in mind the the
// string builder is not NULL-terminated by default. Use nob_sb_append_null if you plan to
// use it as a C string.
void nob_cmd_render(Nob_Cmd cmd, Nob_String_Builder *render);
#define nob_cmd_append(cmd, ...) \
nob_da_append_many(cmd, \
((const char*[]){__VA_ARGS__}), \
(sizeof((const char*[]){__VA_ARGS__})/sizeof(const char*)))
// Free all the memory allocated by command arguments
#define nob_cmd_free(cmd) NOB_FREE(cmd.items)
// Run command asynchronously
Nob_Proc nob_cmd_run_async(Nob_Cmd cmd);
// NOTE: nob_cmd_run_async_and_reset() is just like nob_cmd_run_async() except it also resets cmd.count to 0
// so the Nob_Cmd instance can be seamlessly used several times in a row
Nob_Proc nob_cmd_run_async_and_reset(Nob_Cmd *cmd);
// Run command synchronously
bool nob_cmd_run_sync(Nob_Cmd cmd);
// NOTE: nob_cmd_run_sync_and_reset() is just like nob_cmd_run_sync() except it also resets cmd.count to 0
// so the Nob_Cmd instance can be seamlessly used several times in a row
bool nob_cmd_run_sync_and_reset(Nob_Cmd *cmd);
#ifndef NOB_TEMP_CAPACITY
#define NOB_TEMP_CAPACITY (8*1024*1024)
#endif // NOB_TEMP_CAPACITY
char *nob_temp_strdup(const char *cstr);
void *nob_temp_alloc(size_t size);
char *nob_temp_sprintf(const char *format, ...);
void nob_temp_reset(void);
size_t nob_temp_save(void);
void nob_temp_rewind(size_t checkpoint);
bool nob_rename(const char *old_path, const char *new_path);
int nob_needs_rebuild(const char *output_path, const char **input_paths, size_t input_paths_count);
int nob_needs_rebuild1(const char *output_path, const char *input_path);
int nob_file_exists(const char *file_path);
const char *nob_get_current_dir_temp(void);
bool nob_set_current_dir(const char *path);
// TODO: add MinGW support for Go Rebuild Urself™ Technology
#ifndef NOB_REBUILD_URSELF
# if _WIN32
# if defined(__GNUC__)
# define NOB_REBUILD_URSELF(binary_path, source_path) "gcc", "-o", binary_path, source_path
# elif defined(__clang__)
# define NOB_REBUILD_URSELF(binary_path, source_path) "clang", "-o", binary_path, source_path
# elif defined(_MSC_VER)
# define NOB_REBUILD_URSELF(binary_path, source_path) "cl.exe", nob_temp_sprintf("/Fe:%s", (binary_path)), source_path
# endif
# else
# define NOB_REBUILD_URSELF(binary_path, source_path) "cc", "-o", binary_path, source_path
# endif
#endif
// Go Rebuild Urself™ Technology
//
// How to use it:
// int main(int argc, char** argv) {
// NOB_GO_REBUILD_URSELF(argc, argv);
// // actual work
// return 0;
// }
//
// After your added this macro every time you run ./nob it will detect
// that you modified its original source code and will try to rebuild itself
// before doing any actual work. So you only need to bootstrap your build system
// once.
//
// The modification is detected by comparing the last modified times of the executable
// and its source code. The same way the make utility usually does it.
//
// The rebuilding is done by using the NOB_REBUILD_URSELF macro which you can redefine
// if you need a special way of bootstraping your build system. (which I personally
// do not recommend since the whole idea of NoBuild is to keep the process of bootstrapping
// as simple as possible and doing all of the actual work inside of ./nob)
//
void nob__go_rebuild_urself(const char *source_path, int argc, char **argv);
#define NOB_GO_REBUILD_URSELF(argc, argv) nob__go_rebuild_urself(__FILE__, argc, argv)
typedef struct {
size_t count;
const char *data;
} Nob_String_View;
const char *nob_temp_sv_to_cstr(Nob_String_View sv);
Nob_String_View nob_sv_chop_by_delim(Nob_String_View *sv, char delim);
Nob_String_View nob_sv_trim(Nob_String_View sv);
Nob_String_View nob_sv_trim_left(Nob_String_View sv);
Nob_String_View nob_sv_trim_right(Nob_String_View sv);
bool nob_sv_eq(Nob_String_View a, Nob_String_View b);
bool nob_sv_end_with(Nob_String_View sv, const char *cstr);
Nob_String_View nob_sv_from_cstr(const char *cstr);
Nob_String_View nob_sv_from_parts(const char *data, size_t count);
// nob_sb_to_sv() enables you to just view Nob_String_Builder as Nob_String_View
#define nob_sb_to_sv(sb) nob_sv_from_parts((sb).items, (sb).count)
// printf macros for String_View
#ifndef SV_Fmt
#define SV_Fmt "%.*s"
#endif // SV_Fmt
#ifndef SV_Arg
#define SV_Arg(sv) (int) (sv).count, (sv).data
#endif // SV_Arg
// USAGE:
// String_View name = ...;
// printf("Name: "SV_Fmt"\n", SV_Arg(name));
// minirent.h HEADER BEGIN ////////////////////////////////////////
// Copyright 2021 Alexey Kutepov <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// ============================================================
//
// minirent — 0.0.1 — A subset of dirent interface for Windows.
//
// https://github.com/tsoding/minirent
//
// ============================================================
//
// ChangeLog (https://semver.org/ is implied)
//
// 0.0.2 Automatically include dirent.h on non-Windows
// platforms
// 0.0.1 First Official Release
#ifndef _WIN32
#include <dirent.h>
#else // _WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
struct dirent
{
char d_name[MAX_PATH+1];
};
typedef struct DIR DIR;
static DIR *opendir(const char *dirpath);
static struct dirent *readdir(DIR *dirp);
static int closedir(DIR *dirp);
#endif // _WIN32
// minirent.h HEADER END ////////////////////////////////////////
#endif // NOB_H_
#ifdef NOB_IMPLEMENTATION
// Any messages with the level below nob_minimal_log_level are going to be suppressed.
Nob_Log_Level nob_minimal_log_level = NOB_INFO;
// The implementation idea is stolen from https://github.com/zhiayang/nabs
void nob__go_rebuild_urself(const char *source_path, int argc, char **argv)
{
const char *binary_path = nob_shift(argv, argc);
#ifdef _WIN32
// On Windows executables almost always invoked without extension, so
// it's ./nob, not ./nob.exe. For renaming the extension is a must.
if (!nob_sv_end_with(nob_sv_from_cstr(binary_path), ".exe")) {
binary_path = nob_temp_sprintf("%s.exe", binary_path);
}
#endif
int rebuild_is_needed = nob_needs_rebuild1(binary_path, source_path);
if (rebuild_is_needed < 0) exit(1); // error
if (!rebuild_is_needed) return; // no rebuild is needed
Nob_Cmd cmd = {0};
const char *old_binary_path = nob_temp_sprintf("%s.old", binary_path);
if (!nob_rename(binary_path, old_binary_path)) exit(1);
nob_cmd_append(&cmd, NOB_REBUILD_URSELF(binary_path, source_path));
if (!nob_cmd_run_sync_and_reset(&cmd)) {
nob_rename(old_binary_path, binary_path);
exit(1);
}
nob_cmd_append(&cmd, binary_path);
nob_da_append_many(&cmd, argv, argc);
if (!nob_cmd_run_sync_and_reset(&cmd)) exit(1);
exit(0);
}
static size_t nob_temp_size = 0;
static char nob_temp[NOB_TEMP_CAPACITY] = {0};
bool nob_mkdir_if_not_exists(const char *path)
{
#ifdef _WIN32
int result = mkdir(path);
#else
int result = mkdir(path, 0755);
#endif
if (result < 0) {
if (errno == EEXIST) {
nob_log(NOB_INFO, "directory `%s` already exists", path);
return true;
}
nob_log(NOB_ERROR, "could not create directory `%s`: %s", path, strerror(errno));
return false;
}
nob_log(NOB_INFO, "created directory `%s`", path);
return true;
}
bool nob_copy_file(const char *src_path, const char *dst_path)
{
nob_log(NOB_INFO, "copying %s -> %s", src_path, dst_path);
#ifdef _WIN32
if (!CopyFile(src_path, dst_path, FALSE)) {
nob_log(NOB_ERROR, "Could not copy file: %lu", GetLastError());
return false;
}
return true;
#else
int src_fd = -1;
int dst_fd = -1;
size_t buf_size = 32*1024;
char *buf = NOB_REALLOC(NULL, buf_size);
NOB_ASSERT(buf != NULL && "Buy more RAM lol!!");
bool result = true;
src_fd = open(src_path, O_RDONLY);
if (src_fd < 0) {
nob_log(NOB_ERROR, "Could not open file %s: %s", src_path, strerror(errno));
nob_return_defer(false);
}
struct stat src_stat;
if (fstat(src_fd, &src_stat) < 0) {
nob_log(NOB_ERROR, "Could not get mode of file %s: %s", src_path, strerror(errno));
nob_return_defer(false);
}
dst_fd = open(dst_path, O_CREAT | O_TRUNC | O_WRONLY, src_stat.st_mode);
if (dst_fd < 0) {
nob_log(NOB_ERROR, "Could not create file %s: %s", dst_path, strerror(errno));
nob_return_defer(false);
}
for (;;) {
ssize_t n = read(src_fd, buf, buf_size);
if (n == 0) break;
if (n < 0) {
nob_log(NOB_ERROR, "Could not read from file %s: %s", src_path, strerror(errno));
nob_return_defer(false);
}
char *buf2 = buf;
while (n > 0) {
ssize_t m = write(dst_fd, buf2, n);
if (m < 0) {
nob_log(NOB_ERROR, "Could not write to file %s: %s", dst_path, strerror(errno));
nob_return_defer(false);
}
n -= m;
buf2 += m;
}
}
defer:
free(buf);
close(src_fd);
close(dst_fd);
return result;
#endif
}
void nob_cmd_render(Nob_Cmd cmd, Nob_String_Builder *render)
{
for (size_t i = 0; i < cmd.count; ++i) {
const char *arg = cmd.items[i];
if (arg == NULL) break;
if (i > 0) nob_sb_append_cstr(render, " ");
if (!strchr(arg, ' ')) {
nob_sb_append_cstr(render, arg);
} else {
nob_da_append(render, '\'');
nob_sb_append_cstr(render, arg);
nob_da_append(render, '\'');
}
}
}
Nob_Proc nob_cmd_run_async(Nob_Cmd cmd)
{
if (cmd.count < 1) {
nob_log(NOB_ERROR, "Could not run empty command");
return NOB_INVALID_PROC;
}
Nob_String_Builder sb = {0};
nob_cmd_render(cmd, &sb);
nob_sb_append_null(&sb);
nob_log(NOB_INFO, "CMD: %s", sb.items);
nob_sb_free(sb);
memset(&sb, 0, sizeof(sb));
#ifdef _WIN32
// https://docs.microsoft.com/en-us/windows/win32/procthread/creating-a-child-process-with-redirected-input-and-output
STARTUPINFO siStartInfo;
ZeroMemory(&siStartInfo, sizeof(siStartInfo));
siStartInfo.cb = sizeof(STARTUPINFO);
// NOTE: theoretically setting NULL to std handles should not be a problem
// https://docs.microsoft.com/en-us/windows/console/getstdhandle?redirectedfrom=MSDN#attachdetach-behavior
// TODO: check for errors in GetStdHandle
siStartInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
siStartInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
siStartInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
PROCESS_INFORMATION piProcInfo;
ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION));
// TODO: use a more reliable rendering of the command instead of cmd_render
// cmd_render is for logging primarily
nob_cmd_render(cmd, &sb);
nob_sb_append_null(&sb);
BOOL bSuccess = CreateProcessA(NULL, sb.items, NULL, NULL, TRUE, 0, NULL, NULL, &siStartInfo, &piProcInfo);
nob_sb_free(sb);
if (!bSuccess) {
nob_log(NOB_ERROR, "Could not create child process: %lu", GetLastError());
return NOB_INVALID_PROC;
}
CloseHandle(piProcInfo.hThread);
return piProcInfo.hProcess;
#else
pid_t cpid = fork();
if (cpid < 0) {
nob_log(NOB_ERROR, "Could not fork child process: %s", strerror(errno));
return NOB_INVALID_PROC;
}
if (cpid == 0) {
// NOTE: This leaks a bit of memory in the child process.
// But do we actually care? It's a one off leak anyway...
Nob_Cmd cmd_null = {0};
nob_da_append_many(&cmd_null, cmd.items, cmd.count);
nob_cmd_append(&cmd_null, NULL);
if (execvp(cmd.items[0], (char * const*) cmd_null.items) < 0) {
nob_log(NOB_ERROR, "Could not exec child process: %s", strerror(errno));
exit(1);
}
NOB_UNREACHABLE("nob_cmd_run_async");
}
return cpid;
#endif
}
Nob_Proc nob_cmd_run_async_and_reset(Nob_Cmd *cmd)
{
Nob_Proc proc = nob_cmd_run_async(*cmd);
cmd->count = 0;
return proc;
}
bool nob_procs_wait(Nob_Procs procs)
{
bool success = true;
for (size_t i = 0; i < procs.count; ++i) {
success = nob_proc_wait(procs.items[i]) && success;
}
return success;
}
bool nob_procs_wait_and_reset(Nob_Procs *procs)
{
bool success = nob_procs_wait(*procs);
procs->count = 0;
return success;
}
bool nob_proc_wait(Nob_Proc proc)
{
if (proc == NOB_INVALID_PROC) return false;
#ifdef _WIN32
DWORD result = WaitForSingleObject(
proc, // HANDLE hHandle,
INFINITE // DWORD dwMilliseconds
);
if (result == WAIT_FAILED) {
nob_log(NOB_ERROR, "could not wait on child process: %lu", GetLastError());
return false;
}
DWORD exit_status;
if (!GetExitCodeProcess(proc, &exit_status)) {
nob_log(NOB_ERROR, "could not get process exit code: %lu", GetLastError());
return false;
}
if (exit_status != 0) {
nob_log(NOB_ERROR, "command exited with exit code %lu", exit_status);
return false;
}
CloseHandle(proc);
return true;
#else
for (;;) {
int wstatus = 0;
if (waitpid(proc, &wstatus, 0) < 0) {
nob_log(NOB_ERROR, "could not wait on command (pid %d): %s", proc, strerror(errno));
return false;
}
if (WIFEXITED(wstatus)) {
int exit_status = WEXITSTATUS(wstatus);
if (exit_status != 0) {
nob_log(NOB_ERROR, "command exited with exit code %d", exit_status);
return false;
}
break;
}
if (WIFSIGNALED(wstatus)) {
nob_log(NOB_ERROR, "command process was terminated by %s", strsignal(WTERMSIG(wstatus)));
return false;
}
}
return true;
#endif
}
bool nob_cmd_run_sync(Nob_Cmd cmd)
{
Nob_Proc p = nob_cmd_run_async(cmd);
if (p == NOB_INVALID_PROC) return false;
return nob_proc_wait(p);
}
bool nob_cmd_run_sync_and_reset(Nob_Cmd *cmd)
{
bool p = nob_cmd_run_sync(*cmd);
cmd->count = 0;
return p;
}
void nob_log(Nob_Log_Level level, const char *fmt, ...)
{
if (level < nob_minimal_log_level) return;
switch (level) {
case NOB_INFO:
fprintf(stderr, "[INFO] ");
break;
case NOB_WARNING:
fprintf(stderr, "[WARNING] ");
break;
case NOB_ERROR:
fprintf(stderr, "[ERROR] ");
break;
case NOB_NO_LOGS: return;
default:
NOB_UNREACHABLE("nob_log");
}
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fprintf(stderr, "\n");
}
bool nob_read_entire_dir(const char *parent, Nob_File_Paths *children)
{
bool result = true;
DIR *dir = NULL;
dir = opendir(parent);
if (dir == NULL) {
nob_log(NOB_ERROR, "Could not open directory %s: %s", parent, strerror(errno));
nob_return_defer(false);
}
errno = 0;
struct dirent *ent = readdir(dir);
while (ent != NULL) {
nob_da_append(children, nob_temp_strdup(ent->d_name));
ent = readdir(dir);
}
if (errno != 0) {
nob_log(NOB_ERROR, "Could not read directory %s: %s", parent, strerror(errno));
nob_return_defer(false);
}
defer:
if (dir) closedir(dir);
return result;
}
bool nob_write_entire_file(const char *path, const void *data, size_t size)
{
bool result = true;
FILE *f = fopen(path, "wb");
if (f == NULL) {
nob_log(NOB_ERROR, "Could not open file %s for writing: %s\n", path, strerror(errno));
nob_return_defer(false);
}
// len
// v
// aaaaaaaaaa
// ^
// data
const char *buf = data;
while (size > 0) {
size_t n = fwrite(buf, 1, size, f);
if (ferror(f)) {
nob_log(NOB_ERROR, "Could not write into file %s: %s\n", path, strerror(errno));
nob_return_defer(false);
}
size -= n;
buf += n;
}
defer:
if (f) fclose(f);
return result;
}
Nob_File_Type nob_get_file_type(const char *path)
{
#ifdef _WIN32
DWORD attr = GetFileAttributesA(path);
if (attr == INVALID_FILE_ATTRIBUTES) {
nob_log(NOB_ERROR, "Could not get file attributes of %s: %lu", path, GetLastError());
return -1;
}
if (attr & FILE_ATTRIBUTE_DIRECTORY) return NOB_FILE_DIRECTORY;
// TODO: detect symlinks on Windows (whatever that means on Windows anyway)
return NOB_FILE_REGULAR;
#else // _WIN32
struct stat statbuf;
if (stat(path, &statbuf) < 0) {
nob_log(NOB_ERROR, "Could not get stat of %s: %s", path, strerror(errno));
return -1;
}
switch (statbuf.st_mode & S_IFMT) {
case S_IFDIR: return NOB_FILE_DIRECTORY;
case S_IFREG: return NOB_FILE_REGULAR;
case S_IFLNK: return NOB_FILE_SYMLINK;
default: return NOB_FILE_OTHER;
}
#endif // _WIN32
}
bool nob_copy_directory_recursively(const char *src_path, const char *dst_path)
{
bool result = true;
Nob_File_Paths children = {0};
Nob_String_Builder src_sb = {0};
Nob_String_Builder dst_sb = {0};
size_t temp_checkpoint = nob_temp_save();
Nob_File_Type type = nob_get_file_type(src_path);
if (type < 0) return false;
switch (type) {
case NOB_FILE_DIRECTORY: {
if (!nob_mkdir_if_not_exists(dst_path)) nob_return_defer(false);
if (!nob_read_entire_dir(src_path, &children)) nob_return_defer(false);
for (size_t i = 0; i < children.count; ++i) {
if (strcmp(children.items[i], ".") == 0) continue;
if (strcmp(children.items[i], "..") == 0) continue;
src_sb.count = 0;
nob_sb_append_cstr(&src_sb, src_path);
nob_sb_append_cstr(&src_sb, "/");
nob_sb_append_cstr(&src_sb, children.items[i]);
nob_sb_append_null(&src_sb);
dst_sb.count = 0;
nob_sb_append_cstr(&dst_sb, dst_path);
nob_sb_append_cstr(&dst_sb, "/");
nob_sb_append_cstr(&dst_sb, children.items[i]);
nob_sb_append_null(&dst_sb);
if (!nob_copy_directory_recursively(src_sb.items, dst_sb.items)) {
nob_return_defer(false);
}
}
} break;
case NOB_FILE_REGULAR: {
if (!nob_copy_file(src_path, dst_path)) {
nob_return_defer(false);
}
} break;
case NOB_FILE_SYMLINK: {
nob_log(NOB_WARNING, "TODO: Copying symlinks is not supported yet");
} break;
case NOB_FILE_OTHER: {
nob_log(NOB_ERROR, "Unsupported type of file %s", src_path);
nob_return_defer(false);
} break;
default: NOB_UNREACHABLE("nob_copy_directory_recursively");
}
defer:
nob_temp_rewind(temp_checkpoint);
nob_da_free(src_sb);
nob_da_free(dst_sb);
nob_da_free(children);
return result;
}
char *nob_temp_strdup(const char *cstr)
{
size_t n = strlen(cstr);
char *result = nob_temp_alloc(n + 1);
NOB_ASSERT(result != NULL && "Increase NOB_TEMP_CAPACITY");
memcpy(result, cstr, n);
result[n] = '\0';
return result;
}
void *nob_temp_alloc(size_t size)
{
if (nob_temp_size + size > NOB_TEMP_CAPACITY) return NULL;
void *result = &nob_temp[nob_temp_size];
nob_temp_size += size;
return result;
}
char *nob_temp_sprintf(const char *format, ...)
{
va_list args;
va_start(args, format);
int n = vsnprintf(NULL, 0, format, args);
va_end(args);
NOB_ASSERT(n >= 0);
char *result = nob_temp_alloc(n + 1);
NOB_ASSERT(result != NULL && "Extend the size of the temporary allocator");
// TODO: use proper arenas for the temporary allocator;
va_start(args, format);
vsnprintf(result, n + 1, format, args);
va_end(args);
return result;
}
void nob_temp_reset(void)
{
nob_temp_size = 0;
}
size_t nob_temp_save(void)
{
return nob_temp_size;
}
void nob_temp_rewind(size_t checkpoint)
{
nob_temp_size = checkpoint;
}
const char *nob_temp_sv_to_cstr(Nob_String_View sv)
{
char *result = nob_temp_alloc(sv.count + 1);
NOB_ASSERT(result != NULL && "Extend the size of the temporary allocator");
memcpy(result, sv.data, sv.count);
result[sv.count] = '\0';
return result;
}
int nob_needs_rebuild(const char *output_path, const char **input_paths, size_t input_paths_count)
{
#ifdef _WIN32
BOOL bSuccess;
HANDLE output_path_fd = CreateFile(output_path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
if (output_path_fd == INVALID_HANDLE_VALUE) {
// NOTE: if output does not exist it 100% must be rebuilt
if (GetLastError() == ERROR_FILE_NOT_FOUND) return 1;
nob_log(NOB_ERROR, "Could not open file %s: %lu", output_path, GetLastError());
return -1;
}
FILETIME output_path_time;
bSuccess = GetFileTime(output_path_fd, NULL, NULL, &output_path_time);
CloseHandle(output_path_fd);
if (!bSuccess) {
nob_log(NOB_ERROR, "Could not get time of %s: %lu", output_path, GetLastError());
return -1;
}
for (size_t i = 0; i < input_paths_count; ++i) {
const char *input_path = input_paths[i];
HANDLE input_path_fd = CreateFile(input_path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
if (input_path_fd == INVALID_HANDLE_VALUE) {
// NOTE: non-existing input is an error cause it is needed for building in the first place
nob_log(NOB_ERROR, "Could not open file %s: %lu", input_path, GetLastError());
return -1;
}
FILETIME input_path_time;
bSuccess = GetFileTime(input_path_fd, NULL, NULL, &input_path_time);
CloseHandle(input_path_fd);
if (!bSuccess) {
nob_log(NOB_ERROR, "Could not get time of %s: %lu", input_path, GetLastError());
return -1;
}
// NOTE: if even a single input_path is fresher than output_path that's 100% rebuild
if (CompareFileTime(&input_path_time, &output_path_time) == 1) return 1;
}
return 0;