~lattis/muon

ref: f9a8cbad39b665a9496cd3adb4756e85ee5ec97c muon/src/machine_file.c -rw-r--r-- 3.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
#include "posix.h"

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

#include "error.h"
#include "formats/ini.h"
#include "machine_file.h"
#include "platform/filesystem.h"
#include "platform/mem.h"

struct machine_info {
	uint32_t system, cpu_family, cpu, endian;
	struct {
		uint32_t c, ld, ar;
	} binaries;
};

enum machine_file_section {
	mfile_section_constants,
	mfile_section_binaries,
	mfile_section_properties,
	mfile_section_host_machine,
	machine_file_section_count,
};

static const char *machine_file_section_names[machine_file_section_count] = {
	[mfile_section_constants] = "constants",
	[mfile_section_binaries] = "binaries",
	[mfile_section_properties] = "properties",
	[mfile_section_host_machine] = "host_machine",
};

static bool
mfile_lookup(const char *val, const char *table[], uint32_t len, uint32_t *ret)
{
	uint32_t i;
	for (i = 0; i < len; ++i) {
		if (strcmp(val, table[i]) == 0) {
			*ret = i;
			return true;
		}
	}

	return false;
}

struct machine_file_parse_ctx {
	struct workspace *wk, *dest_wk;
};

static bool
machine_file_parse_cb(void *_ctx, struct source *src, const char *_sect,
	const char *k, const char *v, uint32_t line)
{
	struct machine_file_parse_ctx *ctx = _ctx;
	enum machine_file_section sect;

	if (!mfile_lookup(_sect, machine_file_section_names,
		machine_file_section_count, &sect)) {
		if (!k) {
			error_messagef(src, line, 1, "invalid section '%s'", _sect);
		}
		return false;
	} else if (!k) {
		hash_clear(&current_project(ctx->wk)->scope);
		return true;
	}

	if (!_sect) {
		error_messagef(src, line, 1, "key not under any section");
		return false;
	}

	struct source val_src = { .label = k, .src = v, .len = strlen(v), };

	uint32_t res;
	if (!eval(ctx->wk, &val_src, &res)) {
		error_messagef(src, line, 1, "failed to parse value");
		return false;
	}

	char buf[2048];
	if (!obj_to_s(ctx->wk, res, buf, 2048)) {
		return false;
	}

	if (sect == mfile_section_constants) {
		hash_set(&ctx->wk->scope, k, res);
	} else {
		hash_set(&current_project(ctx->wk)->scope, k, res);

		uint32_t cloned, objkey, dest_dict;
		if (!obj_clone(ctx->wk, ctx->dest_wk, res, &cloned)) {
			return false;
		}

		make_obj(ctx->dest_wk, &objkey, obj_string)->dat.str =
			wk_str_push(ctx->dest_wk, k);

		switch (sect) {
		case mfile_section_binaries:
			printf("setting binaries.%s=%s\n", k, buf);

			dest_dict = ctx->dest_wk->binaries;
			break;
		case mfile_section_host_machine:
			dest_dict = ctx->dest_wk->host_machine;
			break;
		default:
			assert(false && "todo");
			return false;
		}

		obj_dict_set(ctx->dest_wk, dest_dict, objkey, cloned);
	}

	return true;
}

bool
machine_file_parse(struct workspace *dest_wk, const char *path)
{
	bool ret = false;
	char *ini_buf;

	struct workspace wk;
	workspace_init(&wk);

	wk.lang_mode = language_internal;

	uint32_t id;
	make_project(&wk, &id, "dummy", wk.source_root, wk.build_root);

	struct machine_file_parse_ctx ctx = { .wk = &wk, .dest_wk = dest_wk, };

	if (!ini_parse(path, &ini_buf, machine_file_parse_cb, &ctx)) {
		goto ret;
	}

	ret = true;
ret:
	if (ini_buf) {
		z_free(ini_buf);
	}
	workspace_destroy(&wk);
	return ret;

}