summaryrefslogtreecommitdiff
path: root/build.c
blob: 041bb57f5e19c4dfb6cf836911bfdf2ad13b265e (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
#include <sys/wait.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <time.h>
#include <string.h>

#define CC "gcc", "-O3"

int status;

#define RUN_CMD(...) do { \
  if (fork() == 0) { \
    char* args[] = __VA_ARGS__; \
    print_cmd(args); \
    execvp(args[0], args); \
    return 0; \
  } else { \
    wait(&status); \
  } \
} while (0);

void print_cmd(char** args) {
  int i = 0;
  while (args[i] != NULL) {
    printf("%s ", args[i++]);
  }
  printf("\n");
}

int clean_mode();
int test_mode();
int server_mode();
int engine_mode();

// https://github.com/tsoding/nob.h/blob/0a08926d8094fc4ae678155c5d73ae21d1f96f3f/nob.h#L2413
int needs_rebuild(char* bin_file, char* source_file) {
  struct stat statbuf = {0};
  if (stat(bin_file, &statbuf) < 0) {
    if (errno == ENOENT) return 1;
    return -1;
  }
  time_t output_path_time = statbuf.st_mtime;

  if (stat(source_file, &statbuf) < 0) {
    fprintf(stderr, "could not stat %s: %s", source_file, strerror(errno));
    return -1;
  }

  time_t input_path_time = statbuf.st_mtime;
  return input_path_time > output_path_time;
}

#define ENGINE_O "output/engine.o"
#define SERVER_O "output/server.o"
#define LOG_O "output/log.o"

#define LOGGER_C "src/logger/logger.c"

int main(int argc, char** argv) {
  if (needs_rebuild(argv[0], __FILE__)) {
    RUN_CMD({CC, __FILE__, "-o", argv[0], NULL});
    if (status != 0) {
      // cc error
      return 0;
    }
    print_cmd(argv);
    execvp(argv[0], argv);
  }

  RUN_CMD({"rm", "/dev/mqueue/logs", NULL});
  RUN_CMD({"mkdir", "-p", "output", NULL});

  if (argc > 1) {
    if (strcmp(argv[1], "test") == 0) {
      return test_mode();
    }
    if (strcmp(argv[1], "server") == 0) {
      RUN_CMD({CC, "src/log.c", "-o", LOG_O, NULL});
      return server_mode();
    }
    if (strcmp(argv[1], "clean") == 0) {
      RUN_CMD({CC, "src/log.c", "-o", LOG_O, NULL});
      return clean_mode();
    }
  }
  return engine_mode();
}

int clean_mode() {
  char* args[] = {"rm", "-r", "output", NULL};
  print_cmd(args);
  execvp(args[0], args);
  return 0;
}

int test_mode() {
  RUN_CMD({CC, "generate/find_tests.c", LOGGER_C, "-o", "output/find_tests.o", NULL});
  RUN_CMD({"./output/find_tests.o", NULL});
  printf("\n");
  RUN_CMD({CC, "tests/main.c", LOGGER_C, "-o", "output/test.o", NULL});

  char* args[] = {"./output/test.o", NULL};
  print_cmd(args);
  execvp(args[0], args);
  return 0;
}

int server_mode() {
  RUN_CMD({"rm", "/dev/mqueue/server_to_engine", "/dev/mqueue/engine_to_server", NULL});
  RUN_CMD({CC, "src/server.c", LOGGER_C, "-lcrypto", "-o", SERVER_O, NULL});
  RUN_CMD({CC, "src/engine.c", LOGGER_C, "-o", ENGINE_O, NULL});

  char* args[] = {LOG_O, SERVER_O, ENGINE_O, NULL};
  print_cmd(args);
  execvp(args[0], args);
  return 0;
}

int engine_mode() {
  RUN_CMD({CC, "src/engine.c", LOGGER_C, "-o", ENGINE_O, NULL});

  char* args[] = {LOG_O, ENGINE_O, NULL};
  print_cmd(args);
  execvp(args[0], args);
  return 0;
}