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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
#if 0
gcc -fsanitize=address,leak gen_cmake.c -o gen_cmake && ./gen_cmake
exit
#endif
#include "include/kit.inl.h"
#include <stdio.h>
kit_allocator_t ALLOC;
typedef struct {
kit_status_t status;
int have_cmakelists;
} gen_result_t;
/* FIXME
* Barbarian strings!!!
*/
char const *sz_(str_t const s) {
static char buf[1000];
int n = s.size;
if (n > 999)
n = 999;
memcpy(buf, s.values, n);
buf[n] = '\0';
return buf;
}
int make_sz(string_t *const s) {
ptrdiff_t const n = s->size;
DA_RESIZE(*s, n + 1);
if (s->size != n + 1) {
printf("Error: Bad alloc.\n");
return 0;
}
s->values[n] = '\0';
return 1;
}
int is_header(str_t const file) {
return file.size > 2 && file.values[file.size - 2] == '.' &&
file.values[file.size - 1] == 'h';
}
int is_source(str_t const file) {
return file.size > 2 && file.values[file.size - 2] == '.' &&
file.values[file.size - 1] == 'c';
}
gen_result_t gen_cmakelists_for(str_t const target,
str_t const folder) {
gen_result_t result = { KIT_OK, 0 };
path_list_t const list = file_enum_folder(folder, ALLOC);
if (list.status != KIT_OK) {
printf("Error: file_enum_folder failed with code %d.\n",
list.status);
result.status = list.status;
return result;
}
int have_headers = 0;
int have_sources = 0;
int have_folders = 0;
FILE *out = NULL;
string_t cmakelists_path = path_join(folder, SZ("CMakeLists.txt"),
ALLOC);
if (!make_sz(&cmakelists_path)) {
result.status = KIT_ERROR_BAD_ALLOC;
result.have_cmakelists = 0;
DA_DESTROY(cmakelists_path);
path_list_destroy(list);
return result;
}
#define OPEN_CMAKELISTS \
do { \
if (out == NULL) { \
out = fopen(cmakelists_path.values, "wt"); \
if (out == NULL) { \
result.status = 101; \
result.have_cmakelists = 0; \
DA_DESTROY(cmakelists_path); \
path_list_destroy(list); \
return result; \
} \
} \
} while (0)
#define CHECK_TYPE(type_) \
string_t const full_path_ = path_join(folder, WRAP_STR(item), \
ALLOC); \
int check_ = path_type(WRAP_STR(full_path_)) == (type_); \
DA_DESTROY(full_path_); \
if (!check_) \
continue
for (ptrdiff_t i = 0; i < list.files.size; i++) {
str_t const item = WRAP_STR(list.files.values[i]);
CHECK_TYPE(PATH_FILE);
if (!is_header(item))
continue;
OPEN_CMAKELISTS;
if (!have_headers)
fprintf(out, "target_sources(\n %*s\n PUBLIC",
(int) target.size, sz_(target));
fprintf(
out,
"\n $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/%*s>",
(int) item.size, sz_(item));
have_headers = 1;
}
ptrdiff_t const LINE_LIMIT = 80;
ptrdiff_t line_length = LINE_LIMIT;
for (ptrdiff_t i = 0; i < list.files.size; i++) {
str_t const item = WRAP_STR(list.files.values[i]);
CHECK_TYPE(PATH_FILE);
if (!is_source(item))
continue;
OPEN_CMAKELISTS;
if (!have_headers && !have_sources)
fprintf(out, "target_sources(\n %*s\n PRIVATE",
(int) target.size, sz_(target));
else if (!have_sources)
fprintf(out, "\n PRIVATE");
if (line_length + item.size > LINE_LIMIT) {
fprintf(out, "\n ");
line_length = 5;
}
fprintf(out, " %*s", (int) item.size, sz_(item));
line_length += 1 + item.size;
have_sources = 1;
}
if (have_headers || have_sources)
fprintf(out, ")\n");
for (ptrdiff_t i = 0; i < list.files.size; i++) {
str_t const item = WRAP_STR(list.files.values[i]);
CHECK_TYPE(PATH_FOLDER);
string_t const subfolder = path_join(folder, item, ALLOC);
gen_result_t const r = gen_cmakelists_for(target,
WRAP_STR(subfolder));
DA_DESTROY(subfolder);
if (r.status != KIT_OK) {
result.status = r.status;
break;
}
if (!r.have_cmakelists)
continue;
OPEN_CMAKELISTS;
if (have_headers || have_sources)
fprintf(out, "\n");
fprintf(out, "add_subdirectory(%*s)\n", (int) item.size,
sz_(item));
have_folders = 1;
}
if (out != NULL)
fclose(out);
DA_DESTROY(cmakelists_path);
path_list_destroy(list);
if (result.status == KIT_OK)
result.have_cmakelists = have_headers || have_sources ||
have_folders;
if (!result.have_cmakelists && out != NULL)
file_remove(WRAP_STR(cmakelists_path));
return result;
}
int main(int argc, char **argv) {
ALLOC = kit_alloc_default();
str_t const targets[] = { SZ("kit"), SZ("kit_test"),
SZ("kit_test_suite") };
str_t const folders[] = {
SZ("." PATH_DELIM "source" PATH_DELIM "kit"),
SZ("." PATH_DELIM "source" PATH_DELIM "kit_test"),
SZ("." PATH_DELIM "source" PATH_DELIM "test/unittests")
};
int ok = 1;
for (ptrdiff_t i = 0; i < sizeof folders / sizeof *folders; i++)
ok = ok &&
gen_cmakelists_for(targets[i], folders[i]).status == KIT_OK;
return ok ? 0 : 1;
}
|