-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnob.c
More file actions
executable file
·862 lines (797 loc) · 33.1 KB
/
Copy pathnob.c
File metadata and controls
executable file
·862 lines (797 loc) · 33.1 KB
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
#if 0
mkdir -p bin
if [ ! -f bin/nob ]; then cc -o bin/nob "$0" || exit 1; fi
exec ./bin/nob "$@"
#endif
// Bootstrap: `./nob.c` (or `sh nob.c`). The nob binary + its .old backup live
// in bin/ (gitignored), so running ./nob.c doesn't litter the repo root.
// argv[0] is ./bin/nob, so the Go Rebuild Urself tech rebuilds in place there
// when nob.c changes (NOB_GO_REBUILD_URSELF below).
//
// Targets: build, build-wlwp, build-macwp, build-x11wp, run, clean, format, help (default).
#define NOB_IMPLEMENTATION
#define NOB_STRIP_PREFIX
#include "lib/nob.h"
#define INI_IMPLEMENTATION // header-only inih; this TU (the build tool) emits the impl
#include "lib/ini.h" // used to parse default-params at build time
#include <string.h>
// One subdir per owner (core/, goo/, ...) holding that owner's .vert/.frag sources plus
// the generated <name>.h beside them (gitignored). Discovered, not listed -- see discover_shaders.
#define SHADER_DIR "shaders"
// Shader codegen is two passes, so the thing the driver actually compiles can be read on disk:
// expand_shaders() resolves //#include directives into SHADER_PP_DIR, then generate_shaders()
// embeds those expanded files. A GLSL compile error quotes the expanded source, so that is the
// copy worth looking at -- shaders/<owner>/<name>.vert only tells you what was written by hand.
#define SHADER_PP_DIR "shaders_pp"
#define SHADER_INCLUDE_MAX_DEPTH 8
#define BIN_DIR "bin"
#define DEFAULT_PARAMS_SRC "default-params.ini" // source-of-truth default config
#define DEFAULT_PARAMS_HDR "default_params.h" // generated token array (gitignored)
#define SIM_STAMP BIN_DIR "/.sims" // which optional sims this binary was built with
static const char *bin_path = BIN_DIR "/goo";
static const char *wlwp_bin_path = BIN_DIR "/goo-wlwp";
static const char *macwp_bin_path = BIN_DIR "/goo-macwp";
static const char *x11wp_bin_path = BIN_DIR "/goo-x11wp";
// x11 desktop wallpaper front-end. An EWMH desktop-type window the wm pins at the wallpaper layer
// -- the window IS the wallpaper, no extension. Primary use is gnome-wayland (XWayland). Same libs as goo.
static const char *x11wp_sources[] = {
"main-x11wp.c",
"sim.c",
"sim_goo.c",
"shader.c",
"buffer.c",
"rgfw_impl.c",
};
static const char *macwp_sources[] = {
"main-macwp.m",
"sim.c",
"sim_goo.c",
"shader.c",
"buffer.c",
};
#define WLWP_GEN_DIR "wlwp" // generated wayland protocol glue (gitignored)
// Wayland protocols the wallpaper front-end needs. layer-shell is the wallpaper
// mechanism; xdg-shell is pulled in for the types layer-shell references.
static const char *wl_protocols[][2] = {
{"lib/protocols/xdg-shell.xml", "xdg-shell"},
{"lib/protocols/wlr-layer-shell-unstable-v1.xml", "wlr-layer-shell-unstable-v1"},
};
static const char *wlwp_sources[] = {
"main-wlwp.c",
"sim.c",
"sim_goo.c",
"shader.c",
"buffer.c",
};
// Shader units discovered under SHADER_DIR, one per <owner>/<name>.vert + .frag pair --
// "core/repel", "goo/velocity", and so on. The owner dir mirrors the translation unit that
// compiles them, so adding a shader is creating two files; nothing here needs editing.
// Filled by discover_shaders(), consumed by generate_shaders() and every build target.
static File_Paths shader_units = {0};
static int cmp_path(const void *a, const void *b) {
return strcmp(*(const char *const *)a, *(const char *const *)b);
}
// Walk SHADER_DIR/<owner>/ for .vert files and record "<owner>/<name>" for each. Sorted, so
// the generated symbol order -- and therefore the build -- is reproducible; readdir order is
// filesystem-dependent.
static bool discover_shaders(void) {
if (shader_units.count > 0)
return true;
bool result = true;
File_Paths owners = {0}, entries = {0};
if (!read_entire_dir(SHADER_DIR, &owners))
return_defer(false);
qsort(owners.items, owners.count, sizeof(*owners.items), cmp_path);
for (size_t i = 0; i < owners.count; i++) {
const char *owner = owners.items[i];
if (strcmp(owner, ".") == 0 || strcmp(owner, "..") == 0)
continue;
const char *dir = temp_sprintf("%s/%s", SHADER_DIR, owner);
if (get_file_type(dir) != FILE_DIRECTORY)
continue;
entries.count = 0;
if (!read_entire_dir(dir, &entries))
return_defer(false);
qsort(entries.items, entries.count, sizeof(*entries.items), cmp_path);
for (size_t j = 0; j < entries.count; j++) {
const char *e = entries.items[j];
size_t n = strlen(e);
if (n < 6 || strcmp(e + n - 5, ".vert") != 0)
continue;
da_append(&shader_units, temp_sprintf("%s/%.*s", owner, (int)(n - 5), e));
}
}
if (shader_units.count == 0) {
nob_log(NOB_ERROR, "no shaders found under %s/<owner>/", SHADER_DIR);
return_defer(false);
}
defer:
da_free(owners);
da_free(entries);
return result;
}
// "core/repel" -> "core_repel", the prefix of its generated source symbols.
static const char *shader_symbol(const char *unit) {
char *s = temp_strdup(unit);
for (char *p = s; *p; p++)
if (*p == '/')
*p = '_';
return s;
}
static const char *sources[] = {
"main.c",
"sim.c",
"sim_goo.c",
"shader.c",
"buffer.c",
"rgfw_impl.c",
// dropt cli option parser is now a single header (lib/dropt.h);
// main.c emits its impl via DROPT_IMPLEMENTATION
// inih .ini config parser is likewise single-header (lib/ini.h); main.c
// emits its impl via INI_IMPLEMENTATION
};
static void add_platform_libs(Cmd *cmd) {
#if defined(__APPLE__)
cmd_append(cmd, "-framework", "Cocoa", "-framework", "CoreVideo", "-framework", "IOKit", "-framework", "OpenGL");
#elif defined(_WIN32)
cmd_append(cmd, "-lopengl32", "-lgdi32");
#else
cmd_append(cmd, "-lX11", "-lXrandr", "-lGL", "-lm");
#endif
}
// If GOO_VERSION is set in the environment (CI sets it to the tag on a release build), bake it in
// as a string macro so the binary reports it via --version. Unset (normal local build) -> the macro
// (and thus the flag) isn't compiled in. NOTE: not a needs_rebuild input, so a version-only change
// on an otherwise-cached tree won't rebuild -- fine for CI (fresh checkout), irrelevant locally.
static void add_version_define(Cmd *cmd) {
const char *v = getenv("GOO_VERSION");
if (v && v[0])
cmd_append(cmd, temp_sprintf("-DGOO_VERSION=\"%s\"", v));
}
// Optional sims: only goo is unconditional. Set the env var to anything but 0 or empty to compile
// one in -- that adds its TU and its define, and the define is what puts it in sim.c's registry,
// which is the one thing that makes a sim reachable.
static bool env_on(const char *name) {
const char *v = getenv(name);
return v && v[0] && strcmp(v, "0") != 0;
}
static bool pollock_sim(void) { return env_on("GOO_SIM_POLLOCK"); }
static bool blank_sim(void) { return env_on("GOO_SIM_BLANK"); }
static void add_sim_defines(Cmd *cmd) {
if (pollock_sim())
cmd_append(cmd, "-DGOO_SIM_POLLOCK");
if (blank_sim())
cmd_append(cmd, "-DGOO_SIM_BLANK");
}
static void add_sim_sources(Cmd *cmd) {
if (pollock_sim())
cmd_append(cmd, "sim_pollock.c");
if (blank_sim())
cmd_append(cmd, "sim_blank.c");
}
// The enabled set, for the stamp and the log line.
static const char *sim_set(void) {
if (pollock_sim() && blank_sim())
return "pollock blank";
if (pollock_sim())
return "pollock";
if (blank_sim())
return "blank";
return "(none)";
}
// The optional-sim set is not a file, so it cannot be an mtime input on its own: toggling an env var
// would otherwise leave a cached binary built the other way, silently. Record the set in a stamp,
// and when it changes, DELETE the binaries rather than relying on the stamp being newer -- mtime has
// one-second resolution here, so a toggle in the same second as the previous build compares equal
// and needs_rebuild says nothing to do. The stamp is still listed as an input, which covers the
// ordinary case; the unlink covers the fast one.
static bool write_sim_stamp(void) {
const char *want = temp_sprintf("%s\n", sim_set());
String_Builder have = {0};
bool same = file_exists(SIM_STAMP) && read_entire_file(SIM_STAMP, &have) &&
have.count == strlen(want) && memcmp(have.items, want, have.count) == 0;
sb_free(have);
if (same)
return true;
if (!mkdir_if_not_exists(BIN_DIR))
return false;
nob_log(NOB_INFO, "optional sims: %s -- rebuilding", sim_set());
const char *bins[] = {bin_path, wlwp_bin_path, macwp_bin_path, x11wp_bin_path};
for (size_t i = 0; i < NOB_ARRAY_LEN(bins); i++)
remove(bins[i]); // missing is the normal case; nothing to report
return write_entire_file(SIM_STAMP, want, strlen(want));
}
// append src (count bytes) as an escaped C string literal, surrounding quotes included.
// GLSL is ascii so json.dumps' \uXXXX is overkill (and invalid C for <0x20); fixed
// 3-digit octal escapes are valid C and never run greedily into the next char.
static void sb_append_escaped(String_Builder *sb, const char *src, size_t count) {
da_append(sb, '"');
for (size_t i = 0; i < count; i++) {
unsigned char c = (unsigned char)src[i];
switch (c) {
case '"':
sb_append_cstr(sb, "\\\"");
break;
case '\\':
sb_append_cstr(sb, "\\\\");
break;
case '\n':
sb_append_cstr(sb, "\\n");
break;
case '\t':
sb_append_cstr(sb, "\\t");
break;
case '\r':
break; // drop CR -> matches python text-mode universal newlines
default:
if (c >= 0x20 && c < 0x7f)
da_append(sb, (char)c);
else
sb_appendf(sb, "\\%03o", c);
}
}
da_append(sb, '"');
}
// Expand `//#include "path"` into out, recursively. The directive is spelled as a comment so a
// hand-written shader still parses in an editor or a standalone glslang run -- the include just
// doesn't resolve there. `path` is relative to SHADER_DIR, so a header is named the same way from
// every owner.
//
// Include-once, like #pragma once: `seen` holds every path already expanded into this unit, so a
// shader can list all the headers it uses at the top without caring which of them include each
// other, and a cycle terminates instead of recursing. The depth cap is a backstop.
static bool expand_includes(const char *path, String_Builder *out, File_Paths *seen, size_t depth) {
if (depth >= SHADER_INCLUDE_MAX_DEPTH) {
nob_log(NOB_ERROR, "%s: include nesting deeper than %d", path, SHADER_INCLUDE_MAX_DEPTH);
return false;
}
for (size_t i = 0; i < seen->count; i++)
if (strcmp(seen->items[i], path) == 0)
return true;
da_append(seen, temp_strdup(path));
bool result = true;
String_Builder src = {0};
if (!read_entire_file(path, &src))
return_defer(false);
String_View rest = sb_to_sv(src);
while (rest.count > 0) {
String_View line = sv_chop_by_delim(&rest, '\n');
String_View directive = sv_trim_left(line);
if (!sv_starts_with(directive, sv_from_cstr("//#include"))) {
sb_append_buf(out, line.data, line.count);
da_append(out, '\n');
continue;
}
// //#include "owner/name.glsl" -- everything between the first pair of quotes.
const char *open = memchr(directive.data, '"', directive.count);
const char *close = open ? memchr(open + 1, '"', (size_t)(directive.data + directive.count - open - 1)) : NULL;
if (!close) {
nob_log(NOB_ERROR, "%s: malformed //#include (expected a quoted path)", path);
return_defer(false);
}
const char *inc = temp_sprintf("%s/%.*s", SHADER_DIR, (int)(close - open - 1), open + 1);
if (!expand_includes(inc, out, seen, depth + 1))
return_defer(false);
}
defer:
sb_free(src);
return result;
}
// Pass 1 of shader codegen: write every unit's include-expanded source under SHADER_PP_DIR,
// mirroring the shaders/<owner>/ layout. Written only when the expansion differs from what is
// already there, so an unchanged expansion keeps its mtime and pass 2 stays cached -- which is
// also why no include-dependency tracking is needed: content is the dependency.
static bool expand_shaders(void) {
bool result = true;
String_Builder out = {0}, old = {0};
File_Paths seen = {0};
const char *owner_dir = "";
if (!discover_shaders())
return_defer(false);
if (!file_exists(SHADER_PP_DIR) && !mkdir_if_not_exists(SHADER_PP_DIR))
return_defer(false);
for (size_t i = 0; i < shader_units.count; i++) {
const char *unit = shader_units.items[i];
// shader_units is sorted, so owners arrive grouped -- one mkdir per owner, not per unit.
const char *slash = strchr(unit, '/');
const char *dir = temp_sprintf("%s/%.*s", SHADER_PP_DIR, (int)(slash - unit), unit);
if (strcmp(dir, owner_dir) != 0) {
if (!file_exists(dir) && !mkdir_if_not_exists(dir))
return_defer(false);
owner_dir = dir;
}
static const char *const kinds[] = {"vert", "frag"};
for (size_t k = 0; k < NOB_ARRAY_LEN(kinds); k++) {
const char *src = temp_sprintf("%s/%s.%s", SHADER_DIR, unit, kinds[k]);
const char *dst = temp_sprintf("%s/%s.%s", SHADER_PP_DIR, unit, kinds[k]);
out.count = 0;
seen.count = 0; // include-once is per translation unit, not per run
if (!expand_includes(src, &out, &seen, 0))
return_defer(false);
old.count = 0;
bool same = file_exists(dst) && read_entire_file(dst, &old) &&
old.count == out.count && memcmp(old.items, out.items, out.count) == 0;
if (same)
continue;
nob_log(NOB_INFO, "expanding %s -> %s", src, dst);
if (!write_entire_file(dst, out.items, out.count))
return_defer(false);
}
}
defer:
sb_free(out);
sb_free(old);
da_free(seen);
return result;
}
// emit one shader TU (vert or frag) as `const GLchar* <name>_<Kind>ShaderSource = "...";`
static bool emit_shader(String_Builder *sb, const char *name, const char *kind, const char *path) {
String_Builder src = {0};
bool ok = read_entire_file(path, &src);
if (ok) {
sb_appendf(sb, "// %s shader source automatically generated by nob.c\n", kind);
sb_appendf(sb, "const GLchar* %s_%sShaderSource = ", name, kind);
sb_append_escaped(sb, src.items, src.count);
sb_append_cstr(sb, ";\n\n");
}
sb_free(src);
return ok;
}
// Pass 2 of shader codegen: embed the expanded sources, NOT the hand-written ones.
static bool generate_shaders(void) {
// Generated headers land next to their sources in shaders/<owner>/ (gitignored there)
bool result = true;
String_Builder sb = {0};
if (!expand_shaders())
return_defer(false);
for (size_t i = 0; i < shader_units.count; i++) {
const char *unit = shader_units.items[i];
const char *vert = temp_sprintf("%s/%s.vert", SHADER_PP_DIR, unit);
const char *frag = temp_sprintf("%s/%s.frag", SHADER_PP_DIR, unit);
const char *out = temp_sprintf("%s/%s.h", SHADER_DIR, unit);
// skip if the generated .h is newer than both its .vert/.frag sources
const char *ins[] = {vert, frag};
int needed = needs_rebuild(out, ins, NOB_ARRAY_LEN(ins));
if (needed < 0)
return_defer(false);
if (!needed)
continue;
nob_log(NOB_INFO, "embedding %s -> %s", unit, out);
const char *sym = shader_symbol(unit);
sb.count = 0;
if (!emit_shader(&sb, sym, "Vertex", vert))
return_defer(false);
if (!emit_shader(&sb, sym, "Fragment", frag))
return_defer(false);
if (!write_entire_file(out, sb.items, sb.count))
return_defer(false);
}
defer:
sb_free(sb);
return result;
}
// inih handler: append one `key = value` from default-params as a C string
// literal "--key=value" entry. user is the destination String_Builder for the
// array body; the running count is tracked via a trailing newline-per-entry.
static int default_param_handler(void *user, const char *section, const char *name, const char *value) {
String_Builder *body = user;
// values here are our own (numbers / simple tokens); no escaping needed. The [section] a key
// sits under is its flag prefix -- `[goo] trail-tau` bakes in as `--goo.trail-tau`.
if (section && section[0])
sb_appendf(body, " \"--%s.%s=%s\",\n", section, name, value);
else
sb_appendf(body, " \"--%s=%s\",\n", name, value);
return 1; // inih: nonzero = ok
}
// Parse default-params (the source of truth for default config) and emit
// default_params.h: a DEFAULT_PARAMS[] string array main.c feeds to dropt as its
// first option layer. Parsing here means a malformed file fails the build.
static bool generate_default_params(void) {
const char *ins[] = {DEFAULT_PARAMS_SRC};
int needed = needs_rebuild(DEFAULT_PARAMS_HDR, ins, NOB_ARRAY_LEN(ins));
if (needed < 0)
return false;
if (!needed)
return true;
nob_log(NOB_INFO, "embedding %s -> %s", DEFAULT_PARAMS_SRC, DEFAULT_PARAMS_HDR);
bool result = true;
String_Builder body = {0};
int err = ini_parse(DEFAULT_PARAMS_SRC, default_param_handler, &body);
if (err < 0) {
nob_log(NOB_ERROR, "cannot open %s", DEFAULT_PARAMS_SRC);
return_defer(false);
}
if (err > 0) {
nob_log(NOB_ERROR, "%s: parse error on line %d", DEFAULT_PARAMS_SRC, err);
return_defer(false);
}
String_Builder out = {0};
sb_appendf(&out, "// generated by nob.c from %s -- do not edit\n", DEFAULT_PARAMS_SRC);
sb_append_cstr(&out, "static const char *DEFAULT_PARAMS[] = {\n");
sb_append_buf(&out, body.items, body.count);
sb_append_cstr(&out, "};\n");
if (!write_entire_file(DEFAULT_PARAMS_HDR, out.items, out.count))
result = false;
sb_free(out);
defer:
sb_free(body);
return result;
}
// quiet existence probe: run `cmd...` with stdio discarded, return exit success.
static bool probe(const char *arg0, const char *arg1, const char *arg2) {
Cmd cmd = {0};
cmd_append(&cmd, arg0, arg1);
if (arg2)
cmd_append(&cmd, arg2);
return cmd_run(&cmd, .stdout_path = "/dev/null", .stderr_path = "/dev/null");
}
// wlwp is linux + wayland + egl only. Check the toolchain up front and, if
// anything's missing, say exactly which package to install rather than dumping a
// wall of compiler errors.
static bool check_wlwp_deps(void) {
#if !defined(__linux__)
nob_log(NOB_ERROR, "build-wlwp is linux-only (wayland layer-shell). this is not linux.");
return false;
#else
bool ok = true;
struct {
const char *pkg; // pkg-config name
const char *apt; // package to install
} pkgs[] = {
{"wayland-client", "libwayland-dev"},
{"wayland-egl", "libwayland-dev"},
{"egl", "libegl-dev"},
};
for (size_t i = 0; i < NOB_ARRAY_LEN(pkgs); i++) {
if (!probe("pkg-config", "--exists", pkgs[i].pkg)) {
nob_log(NOB_ERROR, "missing dev lib '%s' -- install: sudo apt install %s", pkgs[i].pkg, pkgs[i].apt);
ok = false;
}
}
if (!probe("wayland-scanner", "--version", NULL)) {
nob_log(NOB_ERROR, "missing 'wayland-scanner' -- install: sudo apt install libwayland-bin");
ok = false;
}
return ok;
#endif
}
// Generate the wayland protocol glue (client header + private code) for each xml
// via wayland-scanner. Mirrors generate_shaders: only regenerates when the xml
// (or its output) is stale.
static bool generate_wl_protocols(void) {
if (!mkdir_if_not_exists(WLWP_GEN_DIR))
return false;
for (size_t i = 0; i < NOB_ARRAY_LEN(wl_protocols); i++) {
const char *xml = wl_protocols[i][0];
const char *base = wl_protocols[i][1];
const char *hdr = temp_sprintf("%s/%s-client-protocol.h", WLWP_GEN_DIR, base);
const char *src = temp_sprintf("%s/%s-protocol.c", WLWP_GEN_DIR, base);
const char *ins[] = {xml};
int need_h = needs_rebuild(hdr, ins, 1);
int need_c = needs_rebuild(src, ins, 1);
if (need_h < 0 || need_c < 0)
return false;
if (need_h) {
nob_log(NOB_INFO, "wayland-scanner client-header %s", base);
Cmd cmd = {0};
cmd_append(&cmd, "wayland-scanner", "client-header", xml, hdr);
if (!cmd_run(&cmd))
return false;
}
if (need_c) {
nob_log(NOB_INFO, "wayland-scanner private-code %s", base);
Cmd cmd = {0};
cmd_append(&cmd, "wayland-scanner", "private-code", xml, src);
if (!cmd_run(&cmd))
return false;
}
}
return true;
}
static bool build_goo_wlwp(void) {
if (!mkdir_if_not_exists(BIN_DIR))
return false;
File_Paths inputs = {0};
for (size_t i = 0; i < NOB_ARRAY_LEN(wlwp_sources); i++)
da_append(&inputs, wlwp_sources[i]);
for (size_t i = 0; i < shader_units.count; i++)
da_append(&inputs, temp_sprintf("%s/%s.h", SHADER_DIR, shader_units.items[i]));
for (size_t i = 0; i < NOB_ARRAY_LEN(wl_protocols); i++) {
da_append(&inputs, temp_sprintf("%s/%s-client-protocol.h", WLWP_GEN_DIR, wl_protocols[i][1]));
da_append(&inputs, temp_sprintf("%s/%s-protocol.c", WLWP_GEN_DIR, wl_protocols[i][1]));
}
da_append(&inputs, "sim.h");
da_append(&inputs, "shader.h");
da_append(&inputs, "buffer.h");
da_append(&inputs, "lib/gl.h");
da_append(&inputs, "lib/dropt.h");
da_append(&inputs, "lib/ini.h");
da_append(&inputs, SIM_STAMP);
da_append(&inputs, DEFAULT_PARAMS_HDR);
int needed = needs_rebuild(wlwp_bin_path, inputs.items, inputs.count);
da_free(inputs);
if (needed < 0)
return false;
if (!needed) {
nob_log(NOB_INFO, "%s up to date", wlwp_bin_path);
return true;
}
Cmd cmd = {0};
nob_cc(&cmd);
cmd_append(&cmd, "-O3");
cmd_append(&cmd, "-Ilib", "-I" SHADER_DIR, "-I" WLWP_GEN_DIR);
add_version_define(&cmd);
add_sim_defines(&cmd);
cmd_append(&cmd, "-o", wlwp_bin_path);
for (size_t i = 0; i < NOB_ARRAY_LEN(wlwp_sources); i++)
cmd_append(&cmd, wlwp_sources[i]);
add_sim_sources(&cmd);
for (size_t i = 0; i < NOB_ARRAY_LEN(wl_protocols); i++)
cmd_append(&cmd, temp_sprintf("%s/%s-protocol.c", WLWP_GEN_DIR, wl_protocols[i][1]));
cmd_append(&cmd, "-lwayland-client", "-lwayland-egl", "-lEGL", "-lGL", "-lm");
return cmd_run(&cmd);
}
// The macos desktop-wallpaper binary. Separate target: macos-only (Cocoa +
// NSOpenGL). No protocol codegen like wlwp -- just the shared sim + the obj-c
// front-end. Frameworks are the same set build_goo links (add_platform_libs).
static bool build_goo_macwp(void) {
#if !defined(__APPLE__)
nob_log(NOB_ERROR, "build-macwp is macos-only (Cocoa wallpaper window). this is not macos.");
return false;
#else
if (!mkdir_if_not_exists(BIN_DIR))
return false;
File_Paths inputs = {0};
for (size_t i = 0; i < NOB_ARRAY_LEN(macwp_sources); i++)
da_append(&inputs, macwp_sources[i]);
for (size_t i = 0; i < shader_units.count; i++)
da_append(&inputs, temp_sprintf("%s/%s.h", SHADER_DIR, shader_units.items[i]));
da_append(&inputs, "sim.h");
da_append(&inputs, "shader.h");
da_append(&inputs, "buffer.h");
da_append(&inputs, "lib/gl.h");
da_append(&inputs, "lib/dropt.h");
da_append(&inputs, "lib/ini.h");
da_append(&inputs, SIM_STAMP);
da_append(&inputs, DEFAULT_PARAMS_HDR);
int needed = needs_rebuild(macwp_bin_path, inputs.items, inputs.count);
da_free(inputs);
if (needed < 0)
return false;
if (!needed) {
nob_log(NOB_INFO, "%s up to date", macwp_bin_path);
return true;
}
Cmd cmd = {0};
nob_cc(&cmd);
cmd_append(&cmd, "-O3");
cmd_append(&cmd, "-DGL_SILENCE_DEPRECATION"); // OpenGL is deprecated on macos but works; mute the noise
cmd_append(&cmd, "-Ilib", "-I" SHADER_DIR);
add_version_define(&cmd);
add_sim_defines(&cmd);
cmd_append(&cmd, "-o", macwp_bin_path);
for (size_t i = 0; i < NOB_ARRAY_LEN(macwp_sources); i++)
cmd_append(&cmd, macwp_sources[i]);
add_sim_sources(&cmd);
add_platform_libs(&cmd);
return cmd_run(&cmd);
#endif
}
static bool build_goo(void) {
if (!mkdir_if_not_exists(BIN_DIR))
return false;
// skip the compile if bin/goo is newer than every source + generated header.
// headers (shaders/<name>.h, *.h) are pulled in via #include, so editing one
// must trigger a rebuild -- list them as inputs alongside the .c TUs.
File_Paths inputs = {0};
for (size_t i = 0; i < NOB_ARRAY_LEN(sources); i++)
da_append(&inputs, sources[i]);
for (size_t i = 0; i < shader_units.count; i++)
da_append(&inputs, temp_sprintf("%s/%s.h", SHADER_DIR, shader_units.items[i]));
da_append(&inputs, "sim.h");
da_append(&inputs, "shader.h");
da_append(&inputs, "buffer.h");
da_append(&inputs, "lib/RGFW.h");
da_append(&inputs, "lib/gl.h");
da_append(&inputs, "lib/dropt.h");
da_append(&inputs, "lib/ini.h");
da_append(&inputs, SIM_STAMP);
da_append(&inputs, DEFAULT_PARAMS_HDR); // generated; main.c includes it
int needed = needs_rebuild(bin_path, inputs.items, inputs.count);
da_free(inputs);
if (needed < 0)
return false;
if (!needed) {
nob_log(NOB_INFO, "%s up to date", bin_path);
return true;
}
Cmd cmd = {0};
nob_cc(&cmd);
cmd_append(&cmd, "-O3");
cmd_append(&cmd, "-Ilib", "-I" SHADER_DIR);
add_version_define(&cmd);
add_sim_defines(&cmd);
cmd_append(&cmd, "-o", bin_path);
for (size_t i = 0; i < NOB_ARRAY_LEN(sources); i++)
cmd_append(&cmd, sources[i]);
add_sim_sources(&cmd);
add_platform_libs(&cmd);
return cmd_run(&cmd);
}
// The x11 desktop wallpaper binary. Same build as build_goo (x11 + GL libs), main.c ->
// main-x11wp.c. No protocol codegen, no extension -- the window is the wallpaper directly.
static bool build_goo_x11wp(void) {
#if !defined(__linux__)
nob_log(NOB_ERROR, "build-x11wp is linux-only (x11 desktop wallpaper). this is not linux.");
return false;
#else
if (!mkdir_if_not_exists(BIN_DIR))
return false;
File_Paths inputs = {0};
for (size_t i = 0; i < NOB_ARRAY_LEN(x11wp_sources); i++)
da_append(&inputs, x11wp_sources[i]);
for (size_t i = 0; i < shader_units.count; i++)
da_append(&inputs, temp_sprintf("%s/%s.h", SHADER_DIR, shader_units.items[i]));
da_append(&inputs, "sim.h");
da_append(&inputs, "shader.h");
da_append(&inputs, "buffer.h");
da_append(&inputs, "lib/RGFW.h");
da_append(&inputs, "lib/gl.h");
da_append(&inputs, "lib/dropt.h");
da_append(&inputs, "lib/ini.h");
da_append(&inputs, SIM_STAMP);
da_append(&inputs, DEFAULT_PARAMS_HDR); // generated; sim.c pulls it in
int needed = needs_rebuild(x11wp_bin_path, inputs.items, inputs.count);
da_free(inputs);
if (needed < 0)
return false;
if (!needed) {
nob_log(NOB_INFO, "%s up to date", x11wp_bin_path);
return true;
}
Cmd cmd = {0};
nob_cc(&cmd);
cmd_append(&cmd, "-O3");
cmd_append(&cmd, "-Ilib", "-I" SHADER_DIR, "-I.");
add_version_define(&cmd);
add_sim_defines(&cmd);
cmd_append(&cmd, "-o", x11wp_bin_path);
for (size_t i = 0; i < NOB_ARRAY_LEN(x11wp_sources); i++)
cmd_append(&cmd, x11wp_sources[i]);
add_sim_sources(&cmd);
add_platform_libs(&cmd);
return cmd_run(&cmd);
#endif
}
#define CLANG_FORMAT_STYLE "{BasedOnStyle: llvm, IndentWidth: 4, ColumnLimit: 0}"
// clang-format every top-level .c/.h in place. read_entire_dir is non-recursive,
// so lib/ (vendored single-headers) is skipped -- we don't reformat third-party code.
static bool format_sources(void) {
bool result = true;
File_Paths files = {0};
if (!read_entire_dir(".", &files))
return false;
Cmd cmd = {0};
cmd_append(&cmd, "clang-format", "-style=" CLANG_FORMAT_STYLE, "-i");
size_t base = cmd.count;
for (size_t i = 0; i < files.count; i++) {
const char *f = files.items[i];
size_t n = strlen(f);
bool is_c = n > 2 && strcmp(f + n - 2, ".c") == 0;
bool is_h = n > 2 && strcmp(f + n - 2, ".h") == 0;
if (is_c || is_h)
cmd_append(&cmd, f);
}
if (cmd.count > base)
result = cmd_run(&cmd);
cmd_free(cmd);
da_free(files);
return result;
}
static void usage(const char *program) {
nob_log(NOB_INFO, "usage: %s [build|build-wlwp|build-macwp|build-x11wp|run|shaders|clean|format|help]", program);
nob_log(NOB_INFO, " build compile to %s", bin_path);
nob_log(NOB_INFO, " build-wlwp compile the wayland wallpaper to %s (linux only)", wlwp_bin_path);
nob_log(NOB_INFO, " build-macwp compile the macos wallpaper to %s (macos only)", macwp_bin_path);
nob_log(NOB_INFO, " build-x11wp compile the x11 desktop wallpaper to %s (linux)", x11wp_bin_path);
nob_log(NOB_INFO, " run build and run");
nob_log(NOB_INFO, " shaders expand shader includes into %s/ and embed them, nothing else", SHADER_PP_DIR);
nob_log(NOB_INFO, " clean remove %s/ and generated headers", BIN_DIR);
nob_log(NOB_INFO, " format clang-format top-level *.c/*.h (skips lib/)");
nob_log(NOB_INFO, " help (default) show this message");
nob_log(NOB_INFO, "");
nob_log(NOB_INFO, "env: only goo is compiled in by default. set these to add a sim:");
nob_log(NOB_INFO, " GOO_SIM_POLLOCK=1 the three-family sim (--core.sim=pollock)");
nob_log(NOB_INFO, " GOO_SIM_BLANK=1 the reference sim (--core.sim=blank)");
}
int main(int argc, char **argv) {
NOB_GO_REBUILD_URSELF(argc, argv);
const char *program = argv[0];
const char *target = argc > 1 ? argv[1] : "help";
if (strcmp(target, "help") == 0) {
usage(program);
return 0;
}
if (strcmp(target, "clean") == 0) {
// remove the binary dir + the generated shader headers (cmd_run has no
// shell, so no glob: list each shaders/<owner>/<name>.h explicitly)
Cmd cmd = {0};
cmd_append(&cmd, "rm", "-rf", BIN_DIR);
if (!discover_shaders())
return 1;
for (size_t i = 0; i < shader_units.count; i++) {
cmd_append(&cmd, temp_sprintf("%s/%s.h", SHADER_DIR, shader_units.items[i]));
}
cmd_append(&cmd, DEFAULT_PARAMS_HDR); // generated default config tokens
cmd_append(&cmd, "-r", WLWP_GEN_DIR); // generated wayland protocol glue
cmd_append(&cmd, "-r", SHADER_PP_DIR); // include-expanded shader sources
return cmd_run(&cmd) ? 0 : 1;
}
if (strcmp(target, "format") == 0) {
return format_sources() ? 0 : 1;
}
// Shader codegen on its own, for reading the expanded sources without a full build.
if (strcmp(target, "shaders") == 0) {
return generate_shaders() ? 0 : 1;
}
// The wayland wallpaper binary. Separate target: linux + wayland/egl only, with
// its own dep check + protocol codegen. The shared sim still needs the shader +
// default-param headers, hence the same generate_* preamble.
if (strcmp(target, "build-wlwp") == 0) {
if (!check_wlwp_deps())
return 1;
if (!generate_shaders())
return 1;
if (!generate_default_params())
return 1;
if (!write_sim_stamp())
return 1;
if (!generate_wl_protocols())
return 1;
return build_goo_wlwp() ? 0 : 1;
}
// The macos wallpaper binary. Separate target: macos-only. Shares the sim, so
// it still needs the shader + default-param header codegen.
if (strcmp(target, "build-macwp") == 0) {
if (!generate_shaders())
return 1;
if (!generate_default_params())
return 1;
if (!write_sim_stamp())
return 1;
return build_goo_macwp() ? 0 : 1;
}
// The x11 desktop wallpaper binary. Shares the sim, so it still needs the shader +
// default-param header codegen.
if (strcmp(target, "build-x11wp") == 0) {
if (!generate_shaders())
return 1;
if (!generate_default_params())
return 1;
if (!write_sim_stamp())
return 1;
return build_goo_x11wp() ? 0 : 1;
}
if (strcmp(target, "build") != 0 && strcmp(target, "run") != 0) {
nob_log(NOB_ERROR, "unknown target: %s", target);
usage(program);
return 1;
}
if (!generate_shaders())
return 1;
if (!generate_default_params())
return 1;
if (!write_sim_stamp())
return 1;
if (!build_goo())
return 1;
if (strcmp(target, "run") == 0) {
// forward everything after "run" to goo, e.g. `nob run --help -p 1000`
Cmd cmd = {0};
cmd_append(&cmd, bin_path);
for (int i = 2; i < argc; i++)
cmd_append(&cmd, argv[i]);
return cmd_run(&cmd) ? 0 : 1;
}
return 0;
}