~lattis/muon

ref: f9a8cbad39b665a9496cd3adb4756e85ee5ec97c muon/src/coerce.c -rw-r--r-- 6.1 KiB
f9a8cbadStone Tickle print stdout/stderr of failing test 4 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
#include "posix.h"

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

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

bool
coerce_string(struct workspace *wk, uint32_t node, uint32_t val, const char **res)
{
	struct obj *v = get_obj(wk, val);
	switch (v->type) {
	case obj_file:
		*res = wk_file_path(wk, val);
		break;
	case obj_number: {
		static char num[32] = { 0 };
		snprintf(num, 31, "%ld", v->dat.num);
		*res = num;
		break;
	}
	case obj_string: {
		*res = wk_objstr(wk, val);
		break;
	}
	default:
		interp_error(wk, node, "unable to coerce '%s' to string", obj_type_to_s(v->type));
	}

	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.str;
		break;
	case obj_build_target: {
		char tmp1[PATH_MAX], dest[PATH_MAX];

		if (!path_join(dest, PATH_MAX, wk_str(wk, obj->dat.tgt.build_dir),
			wk_str(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(wk_objstr(wk, val))) {
				path = get_obj(wk, val)->dat.str;
			} else {
				if (!path_join(buf, PATH_MAX, wk_str(wk, current_project(wk)->cwd), wk_objstr(wk, val))) {
					return ir_err;
				}

				path = wk_str_push(wk, buf);
			}

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

			if (!path_join(buf, PATH_MAX, wk_str(wk, current_project(wk)->build_dir), wk_objstr(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, wk_str(wk, tgt->dat.tgt.build_dir), wk_str(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);
}