1616module build_file;
1717
1818import string_pool;
19- import source_mgr;
2019import src_loc local;
2120import string_list;
22- import yaml;
2321
2422import stdlib;
2523import string;
26- import stdio local;
2724
2825public type Plugin struct {
2926 u32 name; // index into pool
3027 u32 options; // index into pool
3128 SrcLoc loc; // TODO FILL (is in yaml parser)
3229}
3330
34- public type Info struct @(opaque) {
31+ public type Info struct {
3532 string_pool.Pool* pool; // no ownership
3633 const char* filename; // no ownership
3734
@@ -53,7 +50,7 @@ public type Info struct @(opaque) {
5350 u32 plugin_max;
5451}
5552
56- fn void Info.addPlugin(Info* info, const char* name, const char* options, SrcLoc loc) {
53+ public fn void Info.addPlugin(Info* info, const char* name, const char* options, SrcLoc loc) {
5754 if (info.plugin_count == info.plugin_max) {
5855 info.plugin_max += 2;
5956 Plugin* plugins2 = stdlib.malloc(info.plugin_max * sizeof(Plugin));
@@ -91,7 +88,7 @@ public fn const char* Info.getCFlags(const Info* info) {
9188 return nil;
9289}
9390
94- public fn const char* Info.getLdFlags (const Info* info) {
91+ public fn const char* Info.getLinkerFlags (const Info* info) {
9592 if (info.ldflags) return info.pool.idx2str(info.ldflags);
9693 return nil;
9794}
@@ -129,129 +126,6 @@ public fn u32 Info.getNumPlugins(const Info* info) {
129126 return info.plugin_count;
130127}
131128
132- /*
133- fn const yaml.Node* get_checked(yaml.Parser* parser, const char* path) {
134- const yaml.Node* node = parser.findNode(path);
135- if (!node) {
136- // TODO print manifest filename
137- fprintf(stderr, "missing node %s\n", path);
138- stdlib.exit(-1);
139- }
140- return node;
141- }
142- */
143-
144- fn u32 Info.expand(Info* info, const char* raw) {
145- if (!raw) return 0;
146-
147- if (raw[0] == '$') {
148- // TODO expand with environment variable
149- const char* expand = stdlib.getenv(raw + 1);
150- if (!expand) {
151- fprintf(stderr, "[build-file] warning: environment variable '%s' not set!\n", raw + 1);
152- return 0;
153- }
154- raw = expand;
155- }
156-
157- return info.pool.addStr(raw, false);
158- }
159-
160- fn bool getYamlInfo(yaml.Parser* parser, Info* info) {
161- const char* target = parser.getScalarValue("target");
162- info.target = info.expand(target);
163-
164- const char* outputDir = parser.getScalarValue("output_dir");
165- info.output_dir = info.expand(outputDir);
166-
167- const char* cc = parser.getScalarValue("toolchain.cc");
168- info.cc = info.expand(cc);
169-
170- const char* cflags = parser.getScalarValue("toolchain.cflags");
171- info.cflags = info.expand(cflags);
172-
173- const char* ldflags = parser.getScalarValue("toolchain.ldflags");
174- info.ldflags = info.expand(ldflags);
175-
176- const char* ldflags2 = parser.getScalarValue("toolchain.ldflags2");
177- info.ldflags2 = info.expand(ldflags2);
178-
179- const char* asmflags = parser.getScalarValue("toolchain.asmflags");
180- info.asmflags = info.expand(asmflags);
181-
182- const char* linkerscript = parser.getScalarValue("toolchain.linkerscript");
183- info.linkerscript = info.expand(linkerscript);
184-
185- const yaml.Node* dirs = parser.findNode("libdir");
186- yaml.Iter iter = parser.getNodeChildIter(dirs);
187- while (!iter.done()) {
188- const char* dir = iter.getValue();
189- info.lib_dirs.add(info.expand(dir));
190- iter.next();
191- }
192-
193- dirs = parser.findNode("plugindir");
194- iter = parser.getNodeChildIter(dirs);
195- while (!iter.done()) {
196- const char* dir = iter.getValue();
197- info.plugin_dirs.add(info.expand(dir));
198- iter.next();
199- }
200-
201- // TODO iterate all nodes, extract all that start with 'plugin.'
202- const yaml.Node* root = parser.getRoot();
203- iter = parser.getNodeChildIter(root);
204- while (!iter.done()) {
205- const char* name = iter.getName();
206- if (string.strncmp(name, "plugin.,", 7) == 0) {
207- const char* options = iter.getChildScalarValue("options");
208- if (!options) {
209- fprintf(stderr, "[build-file] missing options for %s\n", name);
210- stdlib.exit(-1);
211- }
212- SrcLoc loc = 0; // TODO
213- info.addPlugin(name+7, options, loc);
214- }
215- iter.next();
216- }
217-
218- return true;
219- }
220-
221- fn bool Info.parse(Info* info, const char* data) {
222- yaml.Parser* parser = yaml.Parser.create();
223- bool ok = parser.parse(data);
224- if (ok) {
225- //parser.dump(true);
226- ok = getYamlInfo(parser, info);
227- } else {
228- fprintf(stderr, "Error: %s\n", parser.getMessage());
229- }
230-
231- parser.destroy();
232- return ok;
233- }
234-
235- public fn Info* parse(source_mgr.SourceMgr* sm, string_pool.Pool* pool, const char* filename) {
236- // create on stack first. After successful parse, alloc on heap and return
237- Info info = {}
238- info.pool = pool;
239- info.filename = filename;
240- info.lib_dirs.init(pool);
241- info.plugin_dirs.init(pool);
242-
243- i32 file_id = sm.loadFile(filename, 0);
244- if (file_id == -1) return nil;
245-
246- bool ok = info.parse(sm.get_content(file_id));
247-
248- if (!ok) return nil;
249-
250- Info* result = stdlib.malloc(sizeof(Info));
251- string.memcpy(result, &info, sizeof(Info));
252- return result;
253- }
254-
255129public fn void Info.free(Info* info) {
256130 info.lib_dirs.free();
257131 info.plugin_dirs.free();
0 commit comments