-
Notifications
You must be signed in to change notification settings - Fork 1
/
cbuild.c
123 lines (94 loc) · 2.9 KB
/
cbuild.c
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
#define VERSION "v2.0.0"
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include "package/index.h"
#include "lexer/item.h"
#include "package/package.h"
#include "package/import.h"
#include "makefile.h"
#include "cli.h"
package_t * generate(const char * filename, bool force, bool no_output) {
char * error = NULL;
package_t * pkg = index_new(filename, &error, force, no_output);
lex_item_unfreed();
/*if (pkg == NULL || pkg->errors > 0) return NULL;*/
if (error != NULL) {
fprintf(stderr, "%s\n", error);
return NULL;
}
return pkg;
}
typedef struct {
bool force;
} options_t;
int do_generate(cli_t * cli, char * cmd, void * arg) {
options_t * opts = (options_t*) arg;
if (cli->argc < 1) {
fprintf(stderr, "no root module specified\n");
return -1;
}
if (opts->force) printf("FORCED REBUILD\n");
package_t * root = generate(cli->argv[0], opts->force, false);
if (root == NULL) exit(-1);
return 0;
}
int do_build(cli_t * cli, char * cmd, void * arg) {
options_t * opts = (options_t*) arg;
if (cli->argc < 1) {
fprintf(stderr, "no root module specified\n");
return -1;
}
package_t * root = generate(cli->argv[0], opts->force, false);
if (root == NULL) exit(-1);
char * mkfile_name = makefile_write(root, cli->argv[0]);
int result = makefile_make(root, mkfile_name);
if (result != 0) exit(result);
return 0;
}
void clean_generated(package_t * pkg) {
if (pkg == NULL || pkg->c_file || pkg->exported == false) return;
pkg->exported = false;
if (pkg->generated) {
printf("unlink: %s\n", pkg->generated);
unlink(pkg->generated);
}
if (pkg->header) {
printf("unlink: %s\n", pkg->header);
unlink(pkg->header);
}
hash_each_val(pkg->deps, {
package_import_t * imp = (package_import_t *) val;
clean_generated(imp->pkg);
});
}
int do_clean(cli_t * cli, char * cmd, void * arg) {
options_t * opts = (options_t*) arg;
if (cli->argc < 1) {
fprintf(stderr, "no root module specified\n");
return -1;
}
package_t * root = generate(cli->argv[0], opts->force, true);
if (root == NULL) exit(-1);
char * mkfile_name = makefile_write(root, cli->argv[0]);
int result = makefile_clean(root, mkfile_name);
clean_generated(root);
if (result != 0) exit(result);
return 0;
}
int main(int argc, const char ** argv){
options_t options = {0};
cli_t * c = cli_new("<root module>");
cli_flag_bool(c, &options.force, (cli_flag_options) {
.long_name = "force",
.short_name = "f",
.description = "force rebuilding assets",
});
cli_command(c, "build", do_build, "generate code and build", true, &options);
cli_command(c, "generate", do_generate, "generate .c .h and .mk files", false, &options);
cli_command(c, "clean", do_clean, "clean generated files", false, &options);
int result = cli_parse(c, argc, argv);
cli_free(c);
return result;
}