aboutsummaryrefslogtreecommitdiff
path: root/tools/generate-sysroot.c
blob: 97d5bb5100ebce1c4963512002afa41d5e9b9957 (plain)
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
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#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 <sysroot>\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 <stddef.h>\n"
           "#include <stdint.h>\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;
}