summaryrefslogtreecommitdiff
path: root/gen_gl.c
blob: aabd13fea8aff24c7a0c6a3a400a13a738c8128f (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
#if 0
gcc -fsanitize=address,undefined,leak gen_gl.c -o gen_gl && ./gen_gl
exit
#endif

#define KIT_IMPLEMENTATION
#include "include/kit.inl.h"

#define MB *1024 * 1024
#define GL_FOLDER ".gl_registry"
#define GL_REPO "https://github.com/KhronosGroup/OpenGL-Registry"

int main(int argc, char **argv) {
  if (path_type(SZ("." PATH_DELIM GL_FOLDER)) == PATH_NONE) {
    int s    = system("git clone --quiet --depth 1 " GL_REPO
                      " " GL_FOLDER);
    int code = s & 0xff;

    if (code != 0) {
      printf("`git clone` failed.\n");
      return code;
    }
  }

  if (path_type(SZ("." PATH_DELIM GL_FOLDER)) != PATH_FOLDER) {
    printf("OpenGL registry folder not found.\n");
    return 1;
  }

  FILE *f = fopen("." PATH_DELIM GL_FOLDER PATH_DELIM "xml" PATH_DELIM
                  "gl.xml",
                  "rb");

  if (f == NULL) {
    printf("Failed to read OpenGL registry.\n");
    return 1;
  }

  //  FIXME
  //  Optimize memory use
  i64 arena_size = 100 MB;
  u8 *arena = kit_alloc_dispatch(NULL, KIT_ALLOCATE, arena_size, 0,
                                 NULL);
  kit_allocator_t alloc = kit_alloc_buffer(arena_size, arena);

  is_handle_t     is = is_wrap_file(f, &alloc);
  struct timespec t0, t1;
  timespec_get(&t0, TIME_UTC);
  xml_parse_result_t res = xml_parse(is, &alloc);
  timespec_get(&t1, TIME_UTC);
  is_destroy(is);
  fclose(f);

  if (res.status != KIT_OK) {
    printf("XML parse failed.\n");
    kit_alloc_dispatch(NULL, KIT_DEALLOCATE, 0, 0, arena);
    return 1;
  }

  i64 diff = 1000 * (t1.tv_sec - t0.tv_sec) +
             (t1.tv_nsec - t0.tv_nsec + 500000) / 1000000;
  printf("XML parsed in %lld milliseconds.\n", diff);

  xml_destroy(&res.xml);

  kit_alloc_dispatch(NULL, KIT_DEALLOCATE, 0, 0, arena);
  return 0;
}