summaryrefslogtreecommitdiffstats
path: root/b.c
blob: 0221fff015a87bb53c0b7d8a6f86fdebca87f786 (plain) (blame)
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
#define B_IMPLEMENTATION
#include "src/b.h"

void cflags(B_Cmd *cmd)
{
  b_cmd_append(cmd, "-Wall", "-Wextra");
  b_cmd_append(cmd, "-Wno-string-plus-int", "-Wno-nullability-completeness", "-Wno-unused-function", "-Wno-missing-field-initializers", "-Wno-unused-parameter", "-Wno-unused-variable");
  b_cmd_append(cmd, "-march=native");
  b_cmd_append(cmd, "-fno-math-errno", "-funroll-loops");
  b_cmd_append(cmd, "-flto", "-pthread");
  b_cmd_append(cmd, "-I./src/");
  b_cmd_append(cmd, "-O2", "-ggdb", "-DVKDEBUG");
  //b_cmd_append(cmd, "-O3");
}

void cc(B_Cmd *cmd)
{
  b_cmd_append(cmd, "clang");
  cflags(cmd);
}

void libs(B_Cmd *cmd)
{
  b_cmd_append(cmd, "-lSDL2", "-lm", "-lvulkan", "-lshaderc_shared", "-lstdc++");
}

bool build_exe(bool force, B_Cmd *cmd, const char **input_paths, size_t input_paths_len, const char **dep_paths, size_t dep_paths_len, const char *output_path)
{
  int rebuild_is_needed = b_needs_rebuild(output_path, input_paths, input_paths_len);
  int dep_rebuild = b_needs_rebuild(output_path, dep_paths, dep_paths_len);
  
  if (rebuild_is_needed < 0 || dep_rebuild < 0) return false;

  if (force || rebuild_is_needed || dep_rebuild) {
    cmd->count = 0;
    cc(cmd);
    b_cmd_append(cmd, "-o", output_path);
    b_da_append_many(cmd, input_paths, input_paths_len);
    libs(cmd);
    return b_cmd_run_sync(*cmd);
  }

  b_log(B_INFO, "%s is up-to-date", output_path);
  return true;
}

int
main(int argc, char *argv[])
{
  B_GO_REBUILD_URSELF(argc, argv);

  const char *program_name = b_shift_args(&argc, &argv);
  
  bool force = false;

  while (argc > 0) {
    const char *flag = b_shift_args(&argc, &argv);
    if (strcmp(flag, "-f") == 0) {
      force = true;
    } else {
      b_log(B_ERROR, "Unknown flag `%s`", flag);
      return 1;
    }
  }

  const char *dep_paths[] = {
    "src/state.h",
    "src/vksetup.h",
  };
  const char *input_paths[] = {
    "src/render.c",
    "src/cplusplus.cpp"
  };

  B_Cmd cmd = {0};
  if (!build_exe(force, &cmd, input_paths, B_ARRAY_LEN(input_paths), dep_paths, B_ARRAY_LEN(dep_paths),"render")) return 1;

  return 0;
}