~lattis/muon

ref: 1c1ab113103e1a2ebcb7edf3a47f826428f8e124 muon/src/coerce.c -rw-r--r-- 8.1 KiB
1c1ab113Stone Tickle extract string.split() 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
#include "posix.h"

#include <assert.h>
#include <string.h>

#include "coerce.h"
#include "lang/interpreter.h"
#include "log.h"
#include "platform/filesystem.h"
#include "platform/path.h"

bool
coerce_string(struct workspace *wk, uint32_t node, obj val, obj *res)
{
	struct obj *v = get_obj(wk, val);
	switch (v->type) {
	case obj_bool:
		if (v->dat.boolean) {
			*res = make_str(wk, "true");
		} else {
			*res = make_str(wk, "false");
		}
		break;
	case obj_file:
		make_obj(wk, res, obj_string)->dat.str = v->dat.str;
		break;
	case obj_number: {
		make_obj(wk, res, obj_string)->dat.str = wk_str_pushf(wk, "%ld", v->dat.num);
		break;
	}
	case obj_string: {
		*res = val;
		break;
	}
	default:
		interp_error(wk, node, "unable to coerce %o to string", val);
		return false;
	}

	return true;
}

bool
coerce_executable(struct workspace *wk, uint32_t node, uint32_t val, uint32_t *res)
{
	struct obj *obj;
	uint32_t str;

	switch ((obj = get_obj(wk, val))->type) {
	case obj_file:
		str = get_obj(wk, val)->dat.file;
		break;
	case obj_build_target: {
		char tmp1[PATH_MAX], dest[PATH_MAX];

		if (!path_join(dest, PATH_MAX, get_cstr(wk, obj->dat.tgt.build_dir),
			get_cstr(wk, obj->dat.tgt.build_name))) {
			return false;
		} else if (!path_relative_to(tmp1, PATH_MAX, wk->build_root, dest)) {
			return false;
		} else if (!path_executable(dest, PATH_MAX, tmp1)) {
			return false;
		}

		str = wk_str_push(wk, dest);
		break;
	}
	case obj_external_program:
		str = obj->dat.external_program.full_path;
		break;
	default:
		interp_error(wk, node, "unable to coerce '%s' into executable", obj_type_to_s(obj->type));
		return false;
	}

	make_obj(wk, res, obj_string)->dat.str = str;
	return true;
}

bool
coerce_requirement(struct workspace *wk, struct args_kw *kw_required, enum requirement_type *requirement)
{
	if (kw_required->set) {
		if (get_obj(wk, kw_required->val)->type == obj_bool) {
			if (get_obj(wk, kw_required->val)->dat.boolean) {
				*requirement = requirement_required;
			} else {
				*requirement = requirement_auto;
			}
		} else if (get_obj(wk, kw_required->val)->type == obj_feature_opt) {
			switch (get_obj(wk, kw_required->val)->dat.feature_opt.state) {
			case feature_opt_disabled:
				*requirement = requirement_skip;
				break;
			case feature_opt_enabled:
				*requirement = requirement_required;
				break;
			case feature_opt_auto:
				*requirement = requirement_auto;
				break;
			}
		} else {
			interp_error(wk, kw_required->node, "expected type %s or %s, got %s",
				obj_type_to_s(obj_bool),
				obj_type_to_s(obj_feature_opt),
				obj_type_to_s(get_obj(wk, kw_required->val)->type)
				);
			return false;
		}
	} else {
		*requirement = requirement_required;
	}

	return true;
}

typedef bool (*exists_func)(const char *);

enum coerce_into_files_mode {
	mode_input,
	mode_output,
};

struct coerce_into_files_ctx {
	uint32_t node;
	uint32_t arr;
	const char *type;
	exists_func exists;
	enum coerce_into_files_mode mode;
};

static enum iteration_result
coerce_custom_target_output_iter(struct workspace *wk, void *_ctx, uint32_t val)
{
	struct coerce_into_files_ctx *ctx = _ctx;
	assert(get_obj(wk, val)->type == obj_file);

	obj_array_push(wk, ctx->arr, val);
	return ir_cont;
}

static enum iteration_result
coerce_into_files_iter(struct workspace *wk, void *_ctx, uint32_t val)
{
	struct coerce_into_files_ctx *ctx = _ctx;

	switch (get_obj(wk, val)->type) {
	case obj_string: {
		uint32_t path;
		char buf[PATH_MAX];

		switch (ctx->mode) {
		case mode_input:
			if (path_is_absolute(get_cstr(wk, val))) {
				path = get_obj(wk, val)->dat.str;
			} else {
				if (!path_join(buf, PATH_MAX, get_cstr(wk, current_project(wk)->cwd), get_cstr(wk, val))) {
					return ir_err;
				}

				path = wk_str_push(wk, buf);
			}

			if (!ctx->exists(get_cstr(wk, path))) {
				interp_error(wk, ctx->node, "%s '%s' does not exist",
					ctx->type, get_cstr(wk, path));
				return ir_err;
			}
			break;
		case mode_output:
			if (!path_is_basename(get_cstr(wk, val))) {
				interp_error(wk, ctx->node, "output file '%s' contains path seperators", get_cstr(wk, val));
				return ir_err;
			}

			if (!path_join(buf, PATH_MAX, get_cstr(wk, current_project(wk)->build_dir), get_cstr(wk, val))) {
				return ir_err;
			}

			path = wk_str_push(wk, buf);
			break;
		default:
			assert(false);
			return ir_err;
		}

		uint32_t file;
		make_obj(wk, &file, obj_file)->dat.file = path;
		obj_array_push(wk, ctx->arr, file);
		break;
	}
	case obj_custom_target: {
		if (!obj_array_foreach(wk, get_obj(wk, val)->dat.custom_target.output,
			ctx, coerce_custom_target_output_iter)) {
			return ir_err;
		}
		break;
	}
	case obj_build_target: {
		struct obj *tgt = get_obj(wk, val);

		char path[PATH_MAX];
		if (!path_join(path, PATH_MAX, get_cstr(wk, tgt->dat.tgt.build_dir), get_cstr(wk, tgt->dat.tgt.build_name))) {
			return ir_err;
		}

		uint32_t file;
		make_obj(wk, &file, obj_file)->dat.file = wk_str_push(wk, path);
		obj_array_push(wk, ctx->arr, file);
		break;
	}
	case obj_file:
		if (ctx->mode == mode_output) {
			goto type_error;
		}
		obj_array_push(wk, ctx->arr, val);
		break;
	default:
type_error:
		interp_error(wk, ctx->node, "unable to coerce object with type %s into %s",
			obj_type_to_s(get_obj(wk, val)->type), ctx->type);
		return ir_err;
	}

	return ir_cont;
}

static bool
_coerce_files(struct workspace *wk, uint32_t node, uint32_t val, uint32_t *res,
	const char *type_name, exists_func exists, enum coerce_into_files_mode mode)
{
	make_obj(wk, res, obj_array);

	struct coerce_into_files_ctx ctx = {
		.node = node,
		.arr = *res,
		.type = type_name,
		.exists = exists,
		.mode = mode,
	};

	switch (get_obj(wk, val)->type) {
	case obj_array:
		return obj_array_foreach_flat(wk, val, &ctx, coerce_into_files_iter);
	default:
		switch (coerce_into_files_iter(wk, &ctx, val)) {
		case ir_err:
			return false;
		default:
			return true;
		}
	}
}

bool
coerce_output_files(struct workspace *wk, uint32_t node, uint32_t val, uint32_t *res)
{
	return _coerce_files(wk, node, val, res, "output file", NULL, mode_output);
}

bool
coerce_files(struct workspace *wk, uint32_t node, uint32_t val, uint32_t *res)
{
	return _coerce_files(wk, node, val, res, "file", fs_file_exists, mode_input);
}

bool
coerce_dirs(struct workspace *wk, uint32_t node, uint32_t val, uint32_t *res)
{
	return _coerce_files(wk, node, val, res, "directory", fs_dir_exists, mode_input);
}

struct include_directories_iter_ctx {
	uint32_t node;
	obj res;
	bool is_system;
};

static enum iteration_result
include_directories_iter(struct workspace *wk, void *_ctx, obj v)
{
	struct include_directories_iter_ctx *ctx = _ctx;
	struct obj *d = get_obj(wk, v);

	if (d->type == obj_include_directory) {
		obj_array_push(wk, ctx->res, v);
		return ir_cont;
	} else if (d->type != obj_string) {
		interp_error(wk, ctx->node, "unable to coerce %o to include_directory", v);
		return ir_err;
	}

	str path = d->dat.str;
	char buf1[PATH_MAX], buf2[PATH_MAX];
	const char *p = get_cstr(wk, path);

	if (!path_is_absolute(p)) {
		if (!path_join(buf1, PATH_MAX, get_cstr(wk, current_project(wk)->cwd), p)) {
			return ir_err;
		}

		path = wk_str_push(wk, buf1);
	}

	p = get_cstr(wk, path);

	if (!fs_dir_exists(p)) {
		interp_error(wk, ctx->node, "directory '%s' does not exist", get_cstr(wk, path));
		return ir_err;
	}

	obj inc;
	d = make_obj(wk, &inc, obj_include_directory);
	d->dat.include_directory.path = path;
	d->dat.include_directory.is_system = ctx->is_system;
	obj_array_push(wk, ctx->res, inc);

	if (path_is_subpath(wk->source_root, p)) {
		if (!path_relative_to(buf1, PATH_MAX, wk->source_root, p)) {
			return ir_err;
		} else if (!path_join(buf2, PATH_MAX, wk->build_root, buf1)) {
			return ir_err;
		}

		d = make_obj(wk, &inc, obj_include_directory);
		d->dat.include_directory.path = wk_str_push(wk, buf2);
		d->dat.include_directory.is_system = ctx->is_system;
		obj_array_push(wk, ctx->res, inc);
	}

	return ir_cont;
}

bool
coerce_include_dirs(struct workspace *wk, uint32_t node, obj val, bool is_system, obj *res)
{
	struct include_directories_iter_ctx ctx = {
		.node = node,
		.is_system = is_system,
	};

	make_obj(wk, &ctx.res, obj_array);
	if (!obj_array_foreach_flat(wk, val, &ctx, include_directories_iter)) {
		return false;
	}

	*res = ctx.res;
	return true;
}