~lattis/muon

ref: 49a50d56c57dfdf470b4bcbffc16bc5ec92dcb4e muon/src/functions/compiler.c -rw-r--r-- 9.3 KiB
49a50d56Stone Tickle support comma seperated option array 8 months ago
                                                                                
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include "posix.h"

#include <limits.h>
#include <string.h>

#include "args.h"
#include "backend/common_args.h"
#include "buf_size.h"
#include "coerce.h"
#include "compilers.h"
#include "functions/common.h"
#include "functions/compiler.h"
#include "functions/dependency.h"
#include "lang/interpreter.h"
#include "log.h"
#include "platform/dirs.h"
#include "platform/filesystem.h"
#include "platform/path.h"
#include "platform/run_cmd.h"

struct func_compiler_get_supported_arguments_iter_ctx {
	uint32_t arr, node, compiler;
};

static bool
output_test_source(struct workspace *wk, enum compiler_language l, const char **source_file, const char **out_file)
{
	static bool already_output[compiler_language_count];
	static char test_source_path[PATH_MAX], test_out_path[PATH_MAX];
	const char *test_name = NULL, *test_source = NULL;

	switch (l) {
	case compiler_language_c_hdr:
	case compiler_language_cpp_hdr:
	case compiler_language_count:
		LOG_E("no test source for language: %s", compiler_language_to_s(l));
		return false;
	case compiler_language_c:
		test_name = "test.c";
		test_source = "int main(void){}\n";
		break;
	case compiler_language_cpp:
		test_name = "test.cpp";
		test_source = "int main(void){}\n";
		break;
	}

	assert(test_name && test_source);

	if (!path_join(test_source_path, PATH_MAX, wk->muon_private, test_name)) {
		return false;
	}

	strcpy(test_out_path, test_source_path);
	if (!path_add_suffix(test_out_path, PATH_MAX, ".o")) {
		return false;
	}

	*source_file = test_source_path;
	*out_file = test_out_path;

	if (already_output[l]) {
		return true;
	}
	already_output[l] = true;

	return fs_write(test_source_path, (uint8_t *)test_source, strlen(test_source));
}

static bool
compiler_links(struct workspace *wk, obj comp_id, uint32_t err_node, const struct str *src, obj deps, bool *res)
{
	char test_source_path[PATH_MAX];
	if (!path_join(test_source_path, PATH_MAX, wk->muon_private, "test.c")) {
		return false;
	}

	if (!fs_write(test_source_path, (const uint8_t *)src->s, src->len)) {
		return false;
	}

	struct obj *comp = get_obj(wk, comp_id);
	const char *name = get_cstr(wk, comp->dat.compiler.name);
	enum compiler_type t = comp->dat.compiler.type;

	obj compiler_args;
	make_obj(wk, &compiler_args, obj_array);

	obj_array_push(wk, compiler_args, /* make_string? */ comp->dat.compiler.name);
	push_args(wk, compiler_args, compilers[t].args.werror());
	push_args(wk, compiler_args, compilers[t].args.output("/dev/null"));
	obj_array_push(wk, compiler_args, make_str(wk, test_source_path));

	if (deps) {
		struct dep_args_ctx da_ctx;
		dep_args_ctx_init(wk, &da_ctx);

		if (!deps_args(wk, deps, &da_ctx)) {
			return false;
		}

		push_linker_args_link_with(wk, compilers[t].linker, da_ctx.link_args, da_ctx.link_with);
		obj_array_extend(wk, compiler_args, da_ctx.link_args);
	}

	bool ret = false;
	struct run_cmd_ctx cmd_ctx = { 0 };

	const char *argv[MAX_ARGS] = { name };
	if (!join_args_argv(wk, argv, MAX_ARGS, compiler_args)) {
		return false;
	}

	L("compiling: '%s'", src->s);

	if (!run_cmd(&cmd_ctx, name, argv, NULL)) {
		interp_error(wk, err_node, "error: %s", cmd_ctx.err_msg);
		goto ret;
	}

	L("compiler stdout: '%s'", cmd_ctx.err);
	L("compiler stderr: '%s'", cmd_ctx.out);

	*res = cmd_ctx.status == 0;
	ret = true;
ret:
	run_cmd_ctx_destroy(&cmd_ctx);
	return ret;
}

static bool
func_compiler_has_function(struct workspace *wk, obj rcvr, uint32_t args_node, obj *res)
{
	struct args_norm an[] = { { obj_string }, ARG_TYPE_NULL };
	enum kwargs {
		kw_dependencies,
	};
	struct args_kw akw[] = {
		[kw_dependencies] = { "dependencies", ARG_TYPE_ARRAY_OF | obj_dependency },
		0
	};
	if (!interp_args(wk, args_node, an, NULL, akw)) {
		return false;
	}

	char src[BUF_SIZE_4k];
	snprintf(src, BUF_SIZE_4k,
		"char %s (void);\n"
		"int main(void) { return %s(); }\n",
		get_cstr(wk, an[0].val),
		get_cstr(wk, an[0].val)
		);

	bool links;
	if (!compiler_links(wk, rcvr, an[0].node, &WKSTR(src),
		akw[kw_dependencies].val, &links)) {
		return false;
	}

	make_obj(wk, res, obj_bool)->dat.boolean = links;
	return true;
}

static bool
compiler_has_argument(struct workspace *wk, obj comp_id, uint32_t err_node, obj arg, bool *has_argument)
{
	struct obj *comp = get_obj(wk, comp_id);

	const char *test_source, *test_out;
	if (!output_test_source(wk, comp->dat.compiler.lang, &test_source, &test_out)) {
		return ir_err;
	}

	if (!typecheck(wk, err_node, arg, obj_string)) {
		return ir_err;
	}

	const char *name = get_cstr(wk, comp->dat.compiler.name);
	enum compiler_type t = comp->dat.compiler.type;

	const char *argv[MAX_ARGS + 1] = { name };
	uint32_t len = 1;
	push_argv(argv, &len, MAX_ARGS, compilers[t].args.werror());
	push_argv_single(argv, &len, MAX_ARGS, get_cstr(wk, arg));
	push_argv(argv, &len, MAX_ARGS, compilers[t].args.compile_only());
	push_argv(argv, &len, MAX_ARGS, compilers[t].args.output(test_out));
	push_argv_single(argv, &len, MAX_ARGS, test_source);

	bool ret = false;
	struct run_cmd_ctx cmd_ctx = { 0 };

	if (!run_cmd(&cmd_ctx, name, argv, NULL)) {
		interp_error(wk, err_node, "error: %s", cmd_ctx.err_msg);
		goto ret;
	}

	*has_argument = cmd_ctx.status == 0;

	LOG_I("'%s' supported: %s",
		get_cstr(wk, arg),
		*has_argument ? "\033[32mYES\033[0m" : "\033[31mNO\033[0m"
		);

	ret = true;
ret:
	run_cmd_ctx_destroy(&cmd_ctx);
	return ret;
}

static enum iteration_result
func_compiler_get_supported_arguments_iter(struct workspace *wk, void *_ctx, uint32_t val_id)
{
	struct func_compiler_get_supported_arguments_iter_ctx *ctx = _ctx;
	bool has_argument;

	if (!compiler_has_argument(wk, ctx->compiler, ctx->node, val_id, &has_argument)) {
		return false;
	}

	if (has_argument) {
		obj_array_push(wk, ctx->arr, val_id);
	}

	return ir_cont;
}

static bool
func_compiler_has_argument(struct workspace *wk, uint32_t rcvr, uint32_t args_node, uint32_t *obj)
{
	struct args_norm an[] = { { obj_string }, ARG_TYPE_NULL };
	if (!interp_args(wk, args_node, an, NULL, NULL)) {
		return false;
	}

	bool has_argument;
	if (!compiler_has_argument(wk, rcvr, an[0].node, an[0].val, &has_argument)) {
		return false;
	}

	make_obj(wk, obj, obj_bool)->dat.boolean = has_argument;
	return true;
}

static bool
func_compiler_get_supported_arguments(struct workspace *wk, uint32_t rcvr, uint32_t args_node, uint32_t *obj)
{
	struct args_norm an[] = { { obj_array }, ARG_TYPE_NULL };

	if (!interp_args(wk, args_node, an, NULL, NULL)) {
		return false;
	}

	make_obj(wk, obj, obj_array);

	return obj_array_foreach(wk, an[0].val,
		&(struct func_compiler_get_supported_arguments_iter_ctx) {
		.compiler = rcvr,
		.arr = *obj,
		.node = an[0].node,
	}, func_compiler_get_supported_arguments_iter);
}

static bool
func_compiler_get_id(struct workspace *wk, uint32_t rcvr, uint32_t args_node, uint32_t *obj)
{
	if (!interp_args(wk, args_node, NULL, NULL, NULL)) {
		return false;
	}

	make_obj(wk, obj, obj_string)->dat.str = wk_str_push(wk, compiler_type_to_s(get_obj(wk, rcvr)->dat.compiler.type));
	return true;
}

static bool
func_compiler_find_library(struct workspace *wk, uint32_t _, uint32_t args_node, uint32_t *obj)
{
	struct args_norm an[] = { { obj_string }, ARG_TYPE_NULL };
	enum kwargs {
		kw_required,
		kw_static,
		kw_disabler,
	};
	struct args_kw akw[] = {
		[kw_required] = { "required", obj_any },
		[kw_static] = { "static", obj_bool },
		[kw_disabler] = { "disabler", obj_bool },
		0
	};

	if (!interp_args(wk, args_node, an, NULL, akw)) {
		return false;
	}

	enum requirement_type requirement;
	if (!coerce_requirement(wk, &akw[kw_required], &requirement)) {
		return false;
	}

	if (requirement == requirement_skip) {
		make_obj(wk, obj, obj_external_library)->dat.external_library.found = false;
		return true;
	}

	bool found = false;
	char lib[PATH_MAX], path[PATH_MAX];
	const char *suf[] = { "so", "a", NULL };

	uint32_t i, j;
	for (i = 0; libdirs[i]; ++i) {
		for (j = 0; suf[j]; ++j) {
			snprintf(lib, PATH_MAX, "lib%s.%s", get_cstr(wk, an[0].val), suf[j]);

			if (!path_join(path, PATH_MAX, libdirs[i], lib)) {
				return false;
			}

			if (fs_file_exists(path)) {
				found = true;
				goto done;
			}
		}
	}

done:
	if (!found) {
		if (requirement == requirement_required) {
			interp_error(wk, an[0].node, "library not found");
			return false;
		}

		if (akw[kw_disabler].set && get_obj(wk, akw[kw_disabler].val)->dat.boolean) {
			*obj = disabler_id;
		} else {
			make_obj(wk, obj, obj_external_library)->dat.external_library.found = false;
		}
	} else {
		LOG_I("found library '%s' at '%s'", get_cstr(wk, an[0].val), path);
		struct obj *external_library = make_obj(wk, obj, obj_external_library);
		external_library->dat.external_library.found = true;
		external_library->dat.external_library.full_path = wk_str_push(wk, path);
	}

	return true;
}

static bool
func_compiler_cmd_array(struct workspace *wk, obj rcvr, uint32_t args_node, obj *res)
{
	if (!interp_args(wk, args_node, NULL, NULL, NULL)) {
		return false;
	}

	make_obj(wk, res, obj_array);
	obj s;
	make_obj(wk, &s, obj_string)->dat.str = get_obj(wk, rcvr)->dat.compiler.name;
	obj_array_push(wk, *res, s);
	return true;
}

const struct func_impl_name impl_tbl_compiler[] = {
	{ "get_supported_arguments", func_compiler_get_supported_arguments },
	{ "has_argument", func_compiler_has_argument },
	{ "has_function", func_compiler_has_function },
	{ "get_id", func_compiler_get_id },
	{ "find_library", func_compiler_find_library },
	{ "cmd_array", func_compiler_cmd_array },
	{ NULL, NULL },
};