#include #include #include #include #include #include #define PW_PATH_MAX 1024 static inline void check(bool cond, char const *msg) { if (!cond) { perror(msg); exit(EXIT_FAILURE); } } static void fprintfx(FILE *f, char const *fmt, ...) { va_list ap; va_start(ap, fmt); int n = vfprintf(f, fmt, ap); va_end(ap); check(n >= 0, "fprintf(output)"); } static void fclosex(FILE *f) { check(!fclose(f), "fclose"); } static void joinpath(char buffer[static PW_PATH_MAX], char const *dir, char const *file) { size_t n = snprintf(buffer, PW_PATH_MAX, "%s/%s", dir, file); check(n < PW_PATH_MAX, "joinpath"); } struct sysroot_output { FILE *h; FILE *c; }; static void open_sysroot_output(struct sysroot_output *out, char const *outdir) { char path[PW_PATH_MAX]; joinpath(path, outdir, "sysroot.h"); out->h = fopen(path, "w"); check(out->h, "fopen(c)"); joinpath(path, outdir, "sysroot.c"); out->c = fopen(path, "w"); check(out->c, "fopen(c)"); } static void close_sysroot_output(struct sysroot_output *out) { fclosex(out->h); out->h = NULL; fclosex(out->c); out->c = NULL; } static void write_sysroot_data(struct sysroot_output *out, char const *name, char const *file) { FILE *f = fopen(file, "rb"); check(f, "fopen(input)"); check(!fseek(f, 0, SEEK_END), "fseek(end)"); long pos = ftell(f); check(pos >= 0, "ftell(input)"); check(!fseek(f, 0, SEEK_SET), "fseek(start)"); size_t size = (size_t)pos; fprintfx(out->h, "extern struct pw_sysroot_data %s;\n", name); fprintfx(out->c, "static uint8_t const %s_data[] = {\n", name); size_t total = 0; { size_t n; uint8_t data[16]; while ((n = fread(data, 1, sizeof(data), f))) { total += n; fprintfx(out->c, " "); for (size_t i = 0; i < n; i++) { fprintfx(out->c, " 0x%02x,", data[i]); } fprintfx(out->c, "\n"); } } fprintfx(out->c, "};\n" "struct pw_sysroot_data %s = {\n" " .data = %s_data,\n" " .size = %zu,\n" "};\n", name, name, size); check(total == size, "fread(input)"); fclosex(f); } int main(int argc, char *argv[argc + 1]) { if (argc != 2) { fprintf(stderr, "Usage: %s \n", argv[0]); return EXIT_FAILURE; } char const *sysroot = argv[1]; struct sysroot_output out; open_sysroot_output(&out, sysroot); fprintfx(out.h, "// This file is automatically generated, do not edit by hand.\n" "// clang-format off\n\n" "#ifndef _PICOW_SYSROOT_H_\n" "#define _PICOW_SYSROOT_H_\n\n" "#include \n" "#include \n\n" "struct pw_sysroot_data {\n" " uint8_t const *data;\n" " size_t size;\n" "};\n\n"); fprintfx(out.c, "// This file is automatically generated, do not edit by hand.\n" "// clang-format off\n\n" "#include \"sysroot.h\"\n"); struct { char const *name; char const *file; } entries[] = { {"pw_sysroot_pico8_data_pod", "pico8.data.pod"}, {"pw_sysroot_unknown_p8_png", "unknown.p8.png"}, {"pw_sysroot_font_png", "font.png"}, }; for (size_t i = 0; i < sizeof entries / sizeof entries[0]; i++) { char file[PW_PATH_MAX]; joinpath(file, sysroot, entries[i].file); write_sysroot_data(&out, entries[i].name, file); } fprintfx(out.h, "\n#endif\n"); close_sysroot_output(&out); return EXIT_SUCCESS; }