-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClabLogic.cxx
More file actions
401 lines (346 loc) · 10.1 KB
/
ClabLogic.cxx
File metadata and controls
401 lines (346 loc) · 10.1 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include <time.h>
#include <stdio.h>
#include <stdint.h>
#include <FL/Fl.H>
#include <FL/Fl_Text_Buffer.H>
#include <FL/Fl_Text_Display.H>
#include "ClabGui.h"
extern "C" {
#include <autils/crc.h>
#include <autils/filesys.h>
#include <autils/subprocess.h>
}
ClabGui *gui = NULL;
/* recompile state vars */
bool compile_forced = false;
bool compile_requested = false;
int length_last_compile = 0;
time_t time_last_compile = 0;
uint32_t crc_last_compile = 0;
/* temp files used for source, exec */
char cPath[128], cppPath[128], ePath[128];
/* buffs to receive compiler output */
#define STREAM_BUF_SIZE (1024*1024)
char *stdout_buf = NULL;
char *stderr_buf = NULL;
/*****************************************************************************/
/* UTILITIES */
/*****************************************************************************/
void outLogScrollToEnd(void)
{
int n_chars = gui->outBuf->length();
int n_lines = gui->outLog->count_lines(0, n_chars-1, true);
gui->outLog->scroll(n_lines-1, 0);
}
/*****************************************************************************/
/* MAIN ROUTINE */
/*****************************************************************************/
void
compile()
{
//printf("%s()\n", __func__);
int rc = -1, rc_child, i;
int n_args;
char *argv[6];
char *srcText = NULL;
FILE *fp;
char ca_empty[] = "";
char ca_clang[] = "clang";
char ca_clangpp[] = "clang++";
char ca_otool[] = "otool";
char ca_dash_t[] = "-t";
char ca_dash_V[] = "-V";
char ca_dash_v[] = "-v";
char ca_dash_X[] = "-X";
char ca_dash_o[] = "-o";
/* compiler */
bool isC;
const char *compilerChoice;
char *compilerPath;
/* compile timing vars */
time_t time_now;
int length_now;
uint32_t crc_now;
Fl_Text_Buffer *srcBuf = gui->srcBuf;
Fl_Text_Buffer *asmBuf = gui->asmBuf;
Fl_Text_Buffer *outBuf = gui->outBuf;
srcText = srcBuf->text();
if(!compile_forced && !compile_requested) {
//printf("skipping compile, neither forced nor requested\n");
goto cleanup;
}
if(!compile_forced && compile_requested) {
/* skip if we compiled within the last second */
time(&time_now);
float delta = difftime(time_now, time_last_compile);
if(delta >= 0 && delta < 1) {
/* just too soon, remain requested */
//printf("skipping compile, too soon (now,last,diff)=(%ld,%ld,%f)\n",
// time_now, time_last_compile, difftime(time_now, time_last_compile));
goto cleanup;
}
else {
/* skip if the text is unchanged */
length_now = srcBuf->length();
if(length_last_compile == length_now) {
crc_now = crc32(0, srcText, srcBuf->length());
if(crc_now == crc_last_compile) {
//printf("skipping compile, buffer unchanged\n");
compile_requested = false;
goto cleanup;
}
}
}
}
compile_forced = false;
compile_requested = false;
crc_last_compile = crc_now;
time_last_compile = time_now;
length_last_compile = length_now;
/* compile follows thru... */
/* form compiler command line, invoke compiler */
i=0;
compilerChoice = gui->icCompiler->input()->value();
if(0 == strcmp(compilerChoice, "clang (in path)")) {
isC = true;
argv[i++] = ca_clang;
compilerPath = ca_clang;
}
else if(0 == strcmp(compilerChoice, "clang++ (in path)")) {
isC = false;
argv[i++] = ca_clangpp;
compilerPath = ca_clangpp;
}
else {
printf("ERROR: unknown compiler: %s\n", compilerChoice);
goto cleanup;
}
//argv[i++] = "-v";
argv[i++] = isC ? cPath : cppPath;
argv[i++] = ca_dash_o;
argv[i++] = ePath;
if(gui->btnVerbose->value()) argv[i++] = ca_dash_v;
argv[i++] = (char *)gui->icOptimization->input()->value();
argv[i++] = (char *)gui->icDebug->input()->value();
argv[i++] = NULL;
n_args = i;
gui->clBuf->text("");
for(i=0; i<n_args; ++i) {
gui->clBuf->append(argv[i]);
gui->clBuf->append(" ");
}
/* write source, clear buffers */
fp = fopen(isC ? cPath : cppPath, "w");
fwrite(srcText, 1, srcBuf->length(), fp);
fclose(fp);
memset(stdout_buf, '\0', STREAM_BUF_SIZE);
memset(stderr_buf, '\0', STREAM_BUF_SIZE);
/* launch it */
char *compilerExecName;
if(0 != launch(compilerPath, argv, &rc_child, stdout_buf, STREAM_BUF_SIZE,
stderr_buf, STREAM_BUF_SIZE, 1))
{
printf("ERROR: launch()");
goto cleanup;
}
/* stdout, stdout to the outLog */
outBuf->text("");
if(gui->btnStdout->value()) outBuf->append(stdout_buf);
if(gui->btnStderr->value()) outBuf->append(stderr_buf);
if(outBuf->length() == 0) outBuf->text("(no output)");
/* default is NOT to auto scroll */
if(gui->btnScroll->value()) outLogScrollToEnd();
if(gui->btnWrap->value())
gui->outLog->wrap_mode(Fl_Text_Display::WRAP_AT_BOUNDS, 0);
else
gui->outLog->wrap_mode(Fl_Text_Display::WRAP_NONE, 0);
/* disassemble? */
asmBuf->text("");
if(strstr(stdout_buf, "error") || strstr(stderr_buf, "error")) {
printf("compile error detected, skipping disassembler\n");
goto cleanup;
}
i=0;
argv[i++] = ca_otool;
argv[i++] = ca_dash_t;
argv[i++] = ePath;
argv[i++] = ca_dash_V;
argv[i++] = ca_dash_X;
argv[i++] = NULL;
memset(stdout_buf, '\0', STREAM_BUF_SIZE);
memset(stderr_buf, '\0', STREAM_BUF_SIZE);
{
printf("launching ");
for(i=0; argv[i]!=NULL; ++i)
printf("%s ", argv[i]);
printf("\n");
}
if(0 != launch(ca_otool, argv, &rc_child, stdout_buf, STREAM_BUF_SIZE,
stderr_buf, STREAM_BUF_SIZE, 1))
{
printf("ERROR: launch()");
goto cleanup;
}
printf("raw stdout_buf:\n%s\n", stdout_buf);
printf("raw stderr_buf:\n%s\n", stderr_buf);
/* get rid of indent */
for(i=0; stdout_buf[i]!='\0'; ++i) {
if(0 == strncmp(stdout_buf+i, "\x0A\t", 2)) {
memcpy(stdout_buf+i, "\x0a ", 2);
i += 1;
}
else if(0 == strncmp(stdout_buf+i, "\x0d\x0a\t", 3)) {
memcpy(stdout_buf+i, "\x0d\x0a ", 3);
i += 2;
}
}
gui->asmBuf->text(stdout_buf);
rc = 0;
cleanup:
if(srcText) {
free(srcText);
srcText = NULL;
}
return;
}
/*****************************************************************************/
/* GUI CALLBACK STUFF */
/*****************************************************************************/
/* recompile request from GUI with 0 args */
void
recompile(void)
{
printf("recompile request\n");
compile_forced = true;
}
/* callback when the source code is changed
(normal callback won't trigger during text change) */
void
onSourceModified(int pos, int nInserted, int nDeleted, int nRestyled,
const char * deletedText, void *cbArg)
{
printf("%s()\n", __func__);
compile_requested = true;
}
void
onButtonC(void)
{
gui->btnCPP->clear();
compile_forced = true;
}
void
onButtonCPP(void)
{
gui->btnC->clear();
compile_forced = true;
}
void
onGuiFinished(ClabGui *gui_)
{
printf("%s()\n", __func__);
int rc = -1;
gui = gui_;
FILE *fp;
/* alloc */
stdout_buf = (char *)malloc(STREAM_BUF_SIZE);
stderr_buf = (char *)malloc(STREAM_BUF_SIZE);
if(!stdout_buf || !stderr_buf) {
printf("ERROR: malloc()\n");
goto cleanup;
}
/* generate the temp file paths */
if(gen_tmp_file("clabXXXXXX", ePath, &fp)) {
printf("ERROR: get_tmp_file()\n");
goto cleanup;
}
fclose(fp);
strcpy(cPath, ePath);
strcat(cPath, ".c");
strcpy(cppPath, ePath);
strcat(cppPath, ".cpp");
printf("ePath: %s\n", ePath);
printf("cPath: %s\n", cPath);
printf("cppPath: %s\n", cppPath);
/* default GUI states */
gui->btnWrap->set();
gui->btnStdout->set();
gui->btnStderr->set();
gui->btnC->clear();
gui->btnCPP->set();
/* default input choices */
gui->icOptimization->add("-O0");
gui->icOptimization->add("-O1");
gui->icOptimization->add("-O2");
gui->icOptimization->add("-O3");
gui->icOptimization->add("-Ofast");
gui->icOptimization->add("-Os");
gui->icOptimization->add("-Oz");
gui->icOptimization->add("-O");
gui->icOptimization->add("-O4");
gui->icOptimization->add("");
gui->icOptimization->value(0);
gui->icCompiler->add("clang++ (in path)");
gui->icCompiler->add("clang (in path)");
gui->icCompiler->value(0);
gui->icDebug->add("-g");
gui->icDebug->add("-fstandalone-debug");
gui->icDebug->add("-fno-standalone-debug");
gui->icDebug->add("");
gui->icDebug->value(0);
/* initial source */
gui->srcBuf->text(
"// example source code\n"
"#include <stdio.h>\n"
"\n"
"int main(int ac, char **av)\n"
"{\n"
"\tprintf(\"Hello, world!\\n\");\n"
"\treturn 0;\n"
"}\n"
);
gui->srcBuf->text(
"#include <stdio.h>\n"
"#include <memory>\n"
"\n"
"// what's a class doing here?\n"
"class Foo\n"
"{\n"
" private:\n"
" int bar;\n"
" int baz;\n"
"\n"
" public:\n"
" Foo(int a, int b) { bar=a; baz=b; }\n"
" Foo() {}\n"
" void speak(void) { printf(\"I'm here!\\n\"); }\n"
"};\n"
"\n"
"// what's a main doing here?\n"
"int main(int ac, char **av)\n"
"{\n"
" std::unique_ptr<Foo> pfoo(new Foo(3,4));\n"
" pfoo->speak();\n"
" printf(\"Hello, world!\\n\");\n"
" return 0;\n"
"}\n"
);
compile_forced = true;
rc = 0;
cleanup:
return;
}
void
onIdle(void *data)
{
compile();
}
void
onExit(void)
{
printf("onExit()\n");
if(cPath[0]) remove(cPath);
if(cPath[0]) remove(cppPath);
if(ePath[0]) remove(ePath);
if(stdout_buf) free(stdout_buf);
if(stderr_buf) free(stderr_buf);
}