|
| 1 | +#define NOB_IMPLEMENTATION |
| 2 | +#define NOB_STRIP_PREFIX |
| 3 | +#define NOB_EXPERIMENTAL_DELETE_OLD |
| 4 | +#include "./nob.h" |
| 5 | + |
| 6 | +#define COMMON_CFLAGS "-Wall", "-Wextra", "-pedantic", "-ggdb", "-I.", "-I./build/", "-I./dev-deps/" |
| 7 | + |
| 8 | +Cmd cmd = {0}; |
| 9 | +Procs procs = {0}; |
| 10 | + |
| 11 | +bool build_tools(void) |
| 12 | +{ |
| 13 | + if (!mkdir_if_not_exists("build")) return false; |
| 14 | + if (!mkdir_if_not_exists("build/tools")) return false; |
| 15 | + |
| 16 | + cmd_append(&cmd, "clang", COMMON_CFLAGS, "-o", "./build/tools/png2c", "./tools/png2c.c", "-lm"); |
| 17 | + if (!cmd_run_sync_and_reset(&cmd)) return false; |
| 18 | + |
| 19 | + cmd_append(&cmd, "clang", COMMON_CFLAGS, "-o", "./build/tools/obj2c", "./tools/obj2c.c", "-lm"); |
| 20 | + if (!cmd_run_sync_and_reset(&cmd)) return false; |
| 21 | + |
| 22 | + return true; |
| 23 | +} |
| 24 | + |
| 25 | +bool build_assets(void) |
| 26 | +{ |
| 27 | + if (!mkdir_if_not_exists("build")) return false; |
| 28 | + if (!mkdir_if_not_exists("build/assets")) return false; |
| 29 | + |
| 30 | + cmd_append(&cmd, "./build/tools/png2c", "-n", "tsodinPog", "-o", "./build/assets/tsodinPog.c", "./assets/tsodinPog.png"); |
| 31 | + if (!cmd_run_sync_and_reset(&cmd)) return false; |
| 32 | + |
| 33 | + cmd_append(&cmd, "./build/tools/png2c", "-n", "tsodinCup", "-o", "./build/assets/tsodinCup.c", "./assets/tsodinCup.png"); |
| 34 | + if (!cmd_run_sync_and_reset(&cmd)) return false; |
| 35 | + |
| 36 | + cmd_append(&cmd, "./build/tools/png2c", "-n", "oldstone", "-o", "./build/assets/oldstone.c", "./assets/oldstone.png"); |
| 37 | + if (!cmd_run_sync_and_reset(&cmd)) return false; |
| 38 | + |
| 39 | + cmd_append(&cmd, "./build/tools/png2c", "-n", "lavastone", "-o", "./build/assets/lavastone.c", "./assets/lavastone.png"); |
| 40 | + if (!cmd_run_sync_and_reset(&cmd)) return false; |
| 41 | + |
| 42 | + cmd_append(&cmd, "./build/tools/obj2c", "-o", "./build/assets/tsodinCupLowPoly.c", "./assets/tsodinCupLowPoly.obj"); |
| 43 | + if (!cmd_run_sync_and_reset(&cmd)) return false; |
| 44 | + |
| 45 | + cmd_append(&cmd, "./build/tools/obj2c", "-s", "0.40", "-o", "./build/assets/utahTeapot.c", "./assets/utahTeapot.obj"); |
| 46 | + if (!cmd_run_sync_and_reset(&cmd)) return false; |
| 47 | + |
| 48 | + cmd_append(&cmd, "./build/tools/obj2c", "-s", "1.5", "-o", "./build/assets/penger.c", "./assets/penger_obj/penger.obj"); |
| 49 | + if (!cmd_run_sync_and_reset(&cmd)) return false; |
| 50 | + |
| 51 | + return true; |
| 52 | +} |
| 53 | + |
| 54 | +bool build_tests(void) |
| 55 | +{ |
| 56 | + cmd_append(&cmd, "clang", COMMON_CFLAGS, "-fsanitize=memory", "-o", "./build/test", "test.c", "-lm"); |
| 57 | + if (!cmd_run_sync_and_reset(&cmd)) return false; |
| 58 | + return true; |
| 59 | +} |
| 60 | + |
| 61 | +void build_wasm_demo(const char *name) |
| 62 | +{ |
| 63 | + cmd_append(&cmd, "clang", COMMON_CFLAGS, "-O2", "-fno-builtin", "--target=wasm32", "--no-standard-libraries", "-Wl,--no-entry", "-Wl,--export=vc_render", "-Wl,--export=__heap_base", "-Wl,--allow-undefined", "-o", temp_sprintf("./build/demos/%s.wasm", name), "-DVC_PLATFORM=VC_WASM_PLATFORM", temp_sprintf("./demos/%s.c", name)); |
| 64 | + da_append(&procs, cmd_run_async_and_reset(&cmd)); |
| 65 | +} |
| 66 | + |
| 67 | +void build_term_demo(const char *name) |
| 68 | +{ |
| 69 | + cmd_append(&cmd, "clang", COMMON_CFLAGS, "-O2", "-o", temp_sprintf("./build/demos/%s.term", name), "-DVC_PLATFORM=VC_TERM_PLATFORM", "-D_XOPEN_SOURCE=600", temp_sprintf("./demos/%s.c", name), "-lm"); |
| 70 | + da_append(&procs, cmd_run_async_and_reset(&cmd)); |
| 71 | +} |
| 72 | + |
| 73 | +void build_sdl_demo(const char *name) |
| 74 | +{ |
| 75 | + cmd_append(&cmd, "clang", COMMON_CFLAGS, "-O2", "-o", temp_sprintf("./build/demos/%s.sdl", name), "-DVC_PLATFORM=VC_SDL_PLATFORM", temp_sprintf("./demos/%s.c", name), "-lm", "-lSDL2", NULL); |
| 76 | + da_append(&procs, cmd_run_async_and_reset(&cmd)); |
| 77 | +} |
| 78 | + |
| 79 | +void build_vc_demo(const char *name) |
| 80 | +{ |
| 81 | + build_wasm_demo(name); |
| 82 | + build_term_demo(name); |
| 83 | + build_sdl_demo(name); |
| 84 | +} |
| 85 | + |
| 86 | +bool build_all_vc_demos(void) |
| 87 | +{ |
| 88 | + if (!mkdir_if_not_exists("build")) return false; |
| 89 | + if (!mkdir_if_not_exists("build/demos")) return false; |
| 90 | + |
| 91 | + const char *names[] = { |
| 92 | + "triangle", |
| 93 | + "dots3d", |
| 94 | + "squish", |
| 95 | + "triangle3d", |
| 96 | + "triangleTex", |
| 97 | + "triangle3dTex", |
| 98 | + "cup3d", |
| 99 | + "teapot3d", |
| 100 | + "penger3d", |
| 101 | + }; |
| 102 | + size_t thread_count = 6; |
| 103 | + |
| 104 | + for (size_t i = 0; i < ARRAY_LEN(names); ++i) { |
| 105 | + build_vc_demo(names[i]); |
| 106 | + if (procs.count >= thread_count) { |
| 107 | + if (!nob_procs_wait_and_reset(&procs)) return false; |
| 108 | + } |
| 109 | + } |
| 110 | + if (!nob_procs_wait_and_reset(&procs)) return false; |
| 111 | + |
| 112 | + for (size_t i = 0; i < ARRAY_LEN(names); ++i) { |
| 113 | + const char *src_path = temp_sprintf("./build/demos/%s.wasm", names[i]); |
| 114 | + const char *dst_path = temp_sprintf("./wasm/%s.wasm", names[i]); |
| 115 | + if (!copy_file(src_path, dst_path)) return false; |
| 116 | + } |
| 117 | + |
| 118 | + return true; |
| 119 | +} |
| 120 | + |
| 121 | +void usage(const char *program) |
| 122 | +{ |
| 123 | + nob_log(INFO, "Usage: %s [<subcommand>]", program); |
| 124 | + nob_log(INFO, "Subcommands:"); |
| 125 | + nob_log(INFO, " tools"); |
| 126 | + nob_log(INFO, " Build all the tools. Things like png2c, obj2c, etc."); |
| 127 | + nob_log(INFO, " assets"); |
| 128 | + nob_log(INFO, " Build the assets in the assets/ folder."); |
| 129 | + nob_log(INFO, " Basically convert their data to C code so we can bake them in demos."); |
| 130 | + nob_log(INFO, " test[s] [<args>]"); |
| 131 | + nob_log(INFO, " Build and run test.c"); |
| 132 | + nob_log(INFO, " If <args> are provided the test utility is run with them."); |
| 133 | + nob_log(INFO, " demos [<platform>] [run]"); |
| 134 | + nob_log(INFO, " Build demos."); |
| 135 | + nob_log(INFO, " Available platforms are: sdl, term, or wasm."); |
| 136 | + nob_log(INFO, " Optional [run] runs the demo after the build."); |
| 137 | + nob_log(INFO, " [run] is not available for wasm platform."); |
| 138 | + nob_log(INFO, " help"); |
| 139 | + nob_log(INFO, " Print this message"); |
| 140 | +} |
| 141 | + |
| 142 | +int main(int argc, char **argv) |
| 143 | +{ |
| 144 | + NOB_GO_REBUILD_URSELF(argc, argv); |
| 145 | + |
| 146 | + const char *program = shift_args(&argc, &argv); |
| 147 | + |
| 148 | + if (argc > 0) { |
| 149 | + const char *subcmd = shift_args(&argc, &argv); |
| 150 | + if (strcmp(subcmd, "tools") == 0) { |
| 151 | + if (!build_tools()) return 1; |
| 152 | + } else if (strcmp(subcmd, "assets") == 0) { |
| 153 | + if (!build_assets()) return 1; |
| 154 | + } else if (strcmp(subcmd, "tests") == 0 || strcmp(subcmd, "test") == 0) { |
| 155 | + if (!build_tests()) return 1; |
| 156 | + if (argc > 0) { |
| 157 | + cmd_append(&cmd, "./build/test"); |
| 158 | + da_append_many(&cmd, argv, argc); |
| 159 | + if (!cmd_run_sync_and_reset(&cmd)) return 1; |
| 160 | + } |
| 161 | + } else if (strcmp(subcmd, "demos") == 0) { |
| 162 | + if (argc <= 0) { |
| 163 | + if (!build_all_vc_demos()) return 1; |
| 164 | + return 0; |
| 165 | + } |
| 166 | + |
| 167 | + const char *name = shift(argv, argc); |
| 168 | + if (argc <= 0) { |
| 169 | + build_vc_demo(name); |
| 170 | + if (!procs_wait_and_reset(&procs)) return 1; |
| 171 | + const char *src_path = temp_sprintf("./build/demos/%s.wasm", name); |
| 172 | + const char *dst_path = temp_sprintf("./wasm/%s.wasm", name); |
| 173 | + if (!copy_file(src_path, dst_path)) return 1; |
| 174 | + return 0; |
| 175 | + } |
| 176 | + |
| 177 | + const char *platform = shift(argv, argc); |
| 178 | + if (strcmp(platform, "sdl") == 0) { |
| 179 | + build_sdl_demo(name); |
| 180 | + if (!procs_wait_and_reset(&procs)) return 1; |
| 181 | + if (argc <= 0) return 0; |
| 182 | + const char *run = shift(argv, argc); |
| 183 | + if (strcmp(run, "run") != 0) { |
| 184 | + usage(program); |
| 185 | + nob_log(ERROR, "unknown action `%s` for SDL demo: %s", run, name); |
| 186 | + return 1; |
| 187 | + } |
| 188 | + cmd_append(&cmd, temp_sprintf("./build/demos/%s.sdl", name)); |
| 189 | + if (!cmd_run_sync_and_reset(&cmd)) return 1; |
| 190 | + return 0; |
| 191 | + } else if (strcmp(platform, "term") == 0) { |
| 192 | + build_term_demo(name); |
| 193 | + if (!procs_wait_and_reset(&procs)) return 1; |
| 194 | + if (argc <= 0) return 0; |
| 195 | + const char *run = shift(argv, argc); |
| 196 | + if (strcmp(run, "run") != 0) { |
| 197 | + usage(program); |
| 198 | + nob_log(ERROR, "unknown action `%s` for Terminal demo: %s", run, name); |
| 199 | + return 1; |
| 200 | + } |
| 201 | + cmd_append(&cmd, temp_sprintf("./build/demos/%s.term", name)); |
| 202 | + if (!cmd_run_sync_and_reset(&cmd)) return 1; |
| 203 | + return 0; |
| 204 | + } else if (strcmp(platform, "wasm") == 0) { |
| 205 | + build_wasm_demo(name); |
| 206 | + if (!procs_wait_and_reset(&procs)) return 1; |
| 207 | + const char *src_path = temp_sprintf("./build/demos/%s.wasm", name); |
| 208 | + const char *dst_path = temp_sprintf("./wasm/%s.wasm", name); |
| 209 | + if (!copy_file(src_path, dst_path)) return 1; |
| 210 | + } else { |
| 211 | + usage(program); |
| 212 | + nob_log(ERROR, "unknown demo platform %s", platform); |
| 213 | + return 1; |
| 214 | + } |
| 215 | + } else if(strcmp(subcmd, "help") == 0) { |
| 216 | + usage(program); |
| 217 | + } else { |
| 218 | + usage(program); |
| 219 | + nob_log(ERROR, "Unknown command `%s`", subcmd); |
| 220 | + return 1; |
| 221 | + } |
| 222 | + } else { |
| 223 | + if (!build_tools()) return 1; |
| 224 | + if (!build_assets()) return 1; |
| 225 | + if (!build_tests()) return 1; |
| 226 | + if (!build_all_vc_demos()) return 1; |
| 227 | + } |
| 228 | + |
| 229 | + return 0; |
| 230 | +} |
0 commit comments