-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodule.cpp
More file actions
336 lines (280 loc) · 9.3 KB
/
module.cpp
File metadata and controls
336 lines (280 loc) · 9.3 KB
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
#include <generator.h>
using namespace generator;
using namespace std;
static char *to_upper_string(const char *str)
{
if(str == NULL) return NULL;
size_t len = strlen(str);
char *ret = (char *)malloc(len + 1);
for(size_t i = 0; i < len; i++)
{
ret[i] = toupper(str[i]);
}
ret[len] = '\0';
return ret;
}
std::map<std::string *, Object *>::iterator Module::objectsIterBegin()
{
return this->Objects->begin();
}
std::map<std::string *, Object *>::iterator Module::objectsIterEnd()
{
return this->Objects->end();
}
bool Module::objectAdd(std::string *oName, Object *object)
{
this->Objects->insert(std::pair<std::string *, Object *>(oName, object));
return true;
}
std::map<std::string *, Object *>::iterator Module::fpIterBegin()
{
return this->FunctionPointers->begin();
}
std::map<std::string *, Object *>::iterator Module::fpIterEnd()
{
return this->FunctionPointers->end();
}
bool Module::fpAdd(std::string *oName, Object *object)
{
this->FunctionPointers->insert(std::pair<std::string *, Object *>(oName, object));
return true;
}
bool Module::generate(std::string *name, const char *output_dir)
{
std::map<std::string *, Object *>::iterator curr = objectsIterBegin();
std::map<std::string *, Object *>::iterator end = objectsIterEnd();
std::map<std::string *, Object *>::iterator fp_curr = fpIterBegin();
std::map<std::string *, Object *>::iterator fp_end = fpIterEnd();
std::set<Module *> dependencies;
char *path = NULL;
int res = asprintf(&path, "%s/%s", output_dir, this->Path->c_str());
if(res <= 0)
gen_error(strerror(errno));
res = mkdir(path, 0755);
if(res != 0 && errno != EEXIST)
gen_error(strerror(errno));
free(path);
path = NULL;
res = asprintf(&path, "%s/lb", output_dir);
if(res <= 0)
gen_error(strerror(errno));
res = mkdir(path, 0755);
if(res != 0 && errno != EEXIST)
gen_error(strerror(errno));
free(path);
path = NULL;
res = asprintf(&path, "%s/%s/%s", output_dir, this->Path->c_str(), name->c_str());
if(res <= 0)
gen_error(strerror(errno));
res = mkdir(path, 0755);
if(res != 0 && errno != EEXIST)
gen_error(strerror(errno));
free(path);
path = NULL;
res = asprintf(&path, "%s/%s/%s/include", output_dir, this->Path->c_str(), name->c_str());
if(res <= 0)
gen_error(strerror(errno));
res = mkdir(path, 0755);
if(res != 0 && errno != EEXIST)
gen_error(strerror(errno));
free(path);
path = NULL;
res = asprintf(&path, "%s/lb/%s", output_dir, this->Path->c_str());
if(res <= 0)
gen_error(strerror(errno));
res = mkdir(path, 0755);
if(res != 0 && errno != EEXIST)
gen_error(strerror(errno));
free(path);
path = NULL;
// generate the header file first
res = asprintf(&path, "%s/%s/%s/include/%s%s.h", output_dir, this->Path->c_str(), name->c_str(), this->FilePrefix->c_str(), name->c_str());
if(res <= 0)
gen_error(strerror(errno));
std::ofstream header(path);
free(path);
path = NULL;
char *modUpperName = to_upper_string(name->c_str());
char *modUpperPrefix = to_upper_string(this->FilePrefix->c_str());
header << "/* DO NOT EDIT: This file was automatically generated */" << std::endl;
header << "#ifndef " << modUpperPrefix << modUpperName << "_H" << std::endl;
header << "#define " << modUpperPrefix << modUpperName << "_H" << std::endl << std::endl;
// print the includes
header << "#include <stdbool.h>" << std::endl;
header << "#include <stdlib.h>" << std::endl;
// (pthread.h is needed for locking)
header << "#include <pthread.h>" << std::endl;
for(curr = objectsIterBegin(); curr != end; ++curr)
{
curr->second->populate_dependencies(dependencies);
}
for(std::set<Module *>::iterator iter = dependencies.begin(); iter != dependencies.end(); ++iter)
{
if((*iter) != NULL && (*iter) != this)
{
char *inc_file = NULL;
asprintf(&inc_file, "%s%s.h", (*iter)->filePrefix()->c_str(), (*iter)->name()->c_str());
this->includes.insert(std::string(inc_file));
free(inc_file);
}
}
for(std::set<std::string>::iterator I = this->includes.begin(); I != this->includes.end(); I++)
{
header << "#include <" << *I << ">" << std::endl;
}
header << std::endl;
for(curr = objectsIterBegin() ; curr != end ; ++curr)
{
curr->second->genTypeDef(header);
}
header << std::endl;
for( ; fp_curr != fp_end ; ++fp_curr)
{
fp_curr->second->genTypeDef(header);
}
header << std::endl;
for(curr = objectsIterBegin(); curr != end; ++curr)
{
if(!curr->second->genStruct(header))
{
std::cerr << "Error generating structure: " << curr->first << std::endl;
return false;
}
}
for(curr = objectsIterBegin(); curr != end; ++curr)
{
if(!curr->second->genFunctionDefs(header, this))
{
std::cerr << "Error generating function definitions: " << curr->first << std::endl;
return false;
}
}
header << std::endl;
header << "#endif /* " << modUpperPrefix << modUpperName << "_H */" << std::endl;
header.flush();
free(modUpperName);
free(modUpperPrefix);
// now generate each of the logic files
for(curr = objectsIterBegin() ; curr != end ; ++curr)
{
if(curr->second->haveLogic())
{
res = asprintf(&path, "%s/%s/%s/%s%s_%s.c", output_dir, this->Path->c_str(), name->c_str(), this->FilePrefix->c_str(), name->c_str(), curr->first->c_str());
if(res <= 0)
gen_error(strerror(errno));
std::ofstream logic(path);
free(path);
path = NULL;
WORLD->print_logic_header(logic);
logic << "#include <" << this->FilePrefix << name << ".h>" << std::endl;
if(!curr->second->genLogic(logic))
{
std::cerr << "Error generating logic: " << curr->first << std::endl;
return false;
}
logic.flush();
}
}
// now generate each of the template files
for(curr = objectsIterBegin() ; curr != end ; ++curr)
{
if(curr->second->haveFunctions())
{
res = asprintf(&path, "%s/%s/%s/%s%s_%s_logic.c.tpl", output_dir, this->Path->c_str(), name->c_str(), this->FilePrefix->c_str(), name->c_str(), curr->first->c_str());
if(res <= 0)
gen_error(strerror(errno));
std::ofstream tmpl(path);
if(!curr->second->genTemplate(tmpl)) return false;
tmpl.flush();
free(path);
path = NULL;
}
}
// Generate some makefile data
{
res = asprintf(&path, "%s/%s/%s/Makefile.%s", output_dir, this->Path->c_str(), name->c_str(), name->c_str());
if(res <= 0)
gen_error(strerror(errno));
std::ofstream mkout(path);
// for each file we produce, add it
for(curr = objectsIterBegin() ; curr != end ; ++curr)
{
if(curr->second->haveLogic())
{
mkout << "SRC_C+= " << this->Path << "/" << name << "/" << this->FilePrefix << name << "_" << curr->first << ".c" << std::endl;
}
}
// add our path to cflags
mkout << "INCLUDES+= -I" << this->Path << "/" << name << "/include/" << std::endl;
mkout << "INCLUDES+= -I" << this->Path << "/" << name << std::endl;
// add our path
mkout << "PATHS+= " << this->Path << "/" << name << std::endl;
mkout.flush();
free(path);
path = NULL;
}
// lastly generate the luabuild script
{
res = asprintf(&path, "%s/lb/%s/%s.lua", output_dir, this->Path->c_str(), name->c_str());
if(res <= 0)
gen_error(strerror(errno));
std::ofstream lbout(path);
lbout << "function " << this->Path << "_" << name << "_sources()" << std::endl;
lbout << "\tfiles = {}" << std::endl;
// for each other module we use, depend on it
// for each file we produce, add it
for(curr = objectsIterBegin() ; curr != end ; ++curr)
{
if(curr->second->haveLogic())
{
lbout << "\ttable.insert(files, file('" << this->Path
<< "/" << name << "','" << this->FilePrefix << name
<< "_" << curr->first << ".c'))" << std::endl;
}
}
lbout << "\treturn files" << std::endl;
lbout << "end" << std::endl;
lbout << std::endl;
// add the logic files (if needed)
/*
for(curr = objectsIterBegin() ; curr != end ; ++curr)
{
if(curr->second->haveFunctions())
{
lbout << "\t\ttable.insert(deps, file('" << this->Path
<< "/" << name << "','" << this->FilePrefix << name
<< "_" << curr->first << "_logic.c'))" << std::endl;
}
} */
// add our header to the file list
lbout << "function " << this->Path << "_" << name << "_headers()" << std::endl;
lbout << "\tfiles = {}" << std::endl;
lbout << "\ttable.insert(files, file('" << this->Path << "/"
<< name << "/include','" << this->FilePrefix << name << ".h'))"
<< std::endl;
lbout << "\treturn files" << std::endl;
lbout << "end" << std::endl;
lbout << std::endl;
lbout << "function " << this->Path << "_" << name << "_cflags_populate(flags)" << std::endl;
lbout << "\ttable.insert(flags, '-I'..sourcedir..'/" << this->path() << "/" << this->name() << "/include')" << std::endl;
// depend on other modules that we need/use
for(std::set<Module *>::iterator iter = dependencies.begin(); iter != dependencies.end(); ++iter)
{
if((*iter) != NULL && (*iter) != this)
{
lbout << "\t" << (*iter)->path() << "_" << (*iter)->name() << "_cflags_populate(flags)" << std::endl;
}
}
lbout << "\treturn flags" << std::endl;
lbout << "end" << std::endl;
lbout << "function " << this->Path << "_" << name << "_cflags()" << std::endl;
lbout << "\tflags = { '-I'..sourcedir..'/" << this->path() << "/" << this->name() << "' }" << std::endl;
lbout << "\t" << this->Path << "_" << name << "_cflags_populate(flags)" << std::endl;
lbout << "\treturn flags" << std::endl;
lbout << "end" << std::endl;
lbout.flush();
free(path);
path = NULL;
}
return true;
}