File: compiler.cpp

package info (click to toggle)
sludge 2.2.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,852 kB
  • sloc: cpp: 32,432; sh: 1,237; makefile: 634; xml: 284
file content (416 lines) | stat: -rw-r--r-- 12,873 bytes parent folder | download | duplicates (7)
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
 *  Compiler.cpp
 *  Sludge Dev Kit
 *
 *  Created by Rikard Peterson on 2009-07-16.
 *  Copyright 2009 Hungry Software and contributors. All rights reserved.
 *
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>


#include "compiler.hpp"
#include "allknown.h"
#include "checkused.h"
#include "dumpfiles.h"
#include "sludge_functions.h"
#include "linker.h"
#include "messbox.h"
#include "preproc.h"
#include "project.hpp"
#include "realproc.h"
#include "splitter.hpp"
#include "translation.h"
#include "moreio.h"
#include "helpers.h"
#include "settings.h"
#include "hsi.h"
#include "objtype.h"
#include "interface.h"
#include "compilerinfo.h"
#include "utf8.h"

enum { CSTEP_INIT,
	   CSTEP_PARSE,
	   CSTEP_COMPILEINIT,
	   CSTEP_COMPILE,
	   CSTEP_AFTERCOMPILE,
	   CSTEP_LINKSCRIPTS,
	   CSTEP_AFTERLINKSCRIPTS,
	   CSTEP_LINKOBJECTS,
	   CSTEP_AFTERLINKOBJECTS,
	   CSTEP_DUMPFILES,
	   CSTEP_DONE,
	   CSTEP_ERROR };

const char * stageName[] = {
	"Initialisation", "Parsing", "Precompile", "Compiling",
	"Prelink", "Linking scripts", "Linking objects", "Linking objects",
	"Converting resources", "Attaching resources", "Done", "Compiliation aborted!"
};

static int compileStep = CSTEP_INIT;
static stringArray * globalVarNames = NULL;

static FILE * projectFile;
stringArray * allSourceStrings = NULL;
static stringArray * allTheFunctionNamesTemp = NULL;
static compilationSpace globalSpace;

static FILE * tempIndex;
static FILE * tempData;
static unsigned long iSize;
static int tot;

extern stringArray * allFileHandles;
extern int numStringsFound;
extern int numFilesFound;
extern stringArray * functionNames;
extern stringArray * objectTypeNames;

extern stringArray * builtInFunc;
extern stringArray * typeDefFrom;
extern stringArray * allKnownFlags;

char * gameFile = NULL;

static int data1 = 0, numProcessed = 0;


void setCompileStep (int a, int totalBits)
{
	compileStep = a;
	data1 = 0;

	setCompilerText (COMPILER_TXT_ACTION, stageName[a]);

	if (a <= CSTEP_DONE)
	{
		setCompilerText (COMPILER_TXT_FILENAME, "");
		setCompilerText (COMPILER_TXT_ITEM, "");

		clearRect (CSTEP_DONE, P_TOP);
		percRect (a, P_TOP);
		clearRect (totalBits, P_BOTTOM);
	}
}


bool doSingleCompileStep (char **fileList, int *numFiles) {
	switch (compileStep)
	{
		case CSTEP_INIT:
		numProcessed = 0;
		setGlobPointer (& globalVarNames);

		if (! (*numFiles)) {
			addComment (ERRORTYPE_PROJECTERROR, "No files in project!", NULL);
			return false;
		}

		addToStringArray (allSourceStrings, "");
		if (! u8_isvalid(settings.windowName)) {
			return addComment (ERRORTYPE_PROJECTERROR, "Invalid window name.", "(It is not UTF-8 encoded.)", NULL, 0);
		}			
		addToStringArray (allSourceStrings, settings.windowName);
		if (! u8_isvalid(settings.quitMessage)) {
			return addComment (ERRORTYPE_PROJECTERROR, "Invalid quit message.", "(It is not UTF-8 encoded.)", NULL, 0);
		}
		addToStringArray (allSourceStrings, settings.quitMessage);
		clearComments();
		setCompileStep (CSTEP_PARSE, *numFiles);
		setCompilerStats (0, 0, 0, 0, 0);
		break;

		case CSTEP_PARSE:
		{
			if (data1 >= *numFiles) {
				setCompileStep (CSTEP_COMPILEINIT, 1);
			} else {
				char * tx = fileList[data1];
				fixPath(tx, true);
				setCompilerText (COMPILER_TXT_FILENAME, tx);
				percRect (data1, P_BOTTOM);

				char * compareMe = tx + (strlen (tx) - 4);
				char * lowExt = compareMe;
				while (*lowExt) {
					*lowExt = tolower (*lowExt);
					lowExt ++;
				}
				if (strcmp (compareMe, ".sld") == 0) {
					doDefines (tx, allSourceStrings, allFileHandles);
				} else if (strcmp (compareMe, ".slu") == 0) {
					if (! preProcess (tx, numProcessed ++, allSourceStrings, allFileHandles))
						return false;
				} else if (strcmp (compareMe, ".tra") == 0) {
					registerTranslationFile (tx);
				} else {
					return addComment (ERRORTYPE_PROJECTERROR, "What on Earth is this file doing in a project?", tx, NULL, 0);
				}
			}
			data1++;
		}
		break;

		case CSTEP_COMPILEINIT:
		numStringsFound = countElements (allSourceStrings);
		numFilesFound = countElements (allFileHandles);
		setCompileStep (CSTEP_COMPILE, numProcessed);
		if (! startFunction (protoFunction ("_globalInitFunction", ""), 0, globalSpace, "_globalInitFunction", true, false, "-")){
			addComment (ERRORTYPE_INTERNALERROR, "Couldn't create global variable initialisation code segment", NULL);
			return false;
		}
		break;

		case CSTEP_COMPILE:
		if (data1 >= numProcessed)
		{
			setCompileStep (CSTEP_AFTERCOMPILE, 1);
			break;
		}

		percRect (data1, P_BOTTOM);
		if (! realWork (data1, globalVarNames, globalSpace))
			return false;
		data1 ++;
		break;

		case CSTEP_AFTERCOMPILE:

		{

            outputHalfCode (globalSpace, SLU_LOAD_GLOBAL, "init");
            outputDoneCode (globalSpace, SLU_CALLIT, 0);
            finishFunctionNew (globalSpace, NULL);

            setCompilerStats (countElements (functionNames) - 1,
                countElements (objectTypeNames),
                countElements (allFileHandles),
                countElements (globalVarNames),
                countElements (allSourceStrings));

            checkUsedInit (CHECKUSED_FUNCTIONS, countElements (functionNames));
            setUsed(CHECKUSED_FUNCTIONS, 0);

            checkUsedInit (CHECKUSED_GLOBALS, countElements (globalVarNames));

            stringArray * silenceChecker = allFileHandles;
            while (silenceChecker) {
                if (audioFile (silenceChecker -> string)) silent = false;
                silenceChecker = silenceChecker -> next;
            }
            projectFile = openFinalFile (".sl~", "wb");
            if (! projectFile) {
                addComment (ERRORTYPE_SYSTEMERROR, "Can't open output file for writing", NULL);
                return false;
            }

            FILE * textFile = (programSettings.compilerWriteStrings) ? openFinalFile (" text.txt", "wt") : NULL;

            writeFinalData (projectFile);

            unsigned char customIconLogo = 0;

            if (settings.customIcon && settings.customIcon[0])
                customIconLogo +=1;

            if (settings.customLogo && settings.customLogo[0])
                customIconLogo +=2;

            fputc (customIconLogo, projectFile);

            // ADD ICON ------------------------------------
            if (customIconLogo & 1) {
                setCompilerText (COMPILER_TXT_ACTION, "Adding custom icon");
                setCompilerText (COMPILER_TXT_FILENAME, settings.customIcon);
                setCompilerText (COMPILER_TXT_ITEM, "");
                char * iconFile = joinStrings (settings.customIcon, "");
                if (getFileType (iconFile) == FILETYPE_TGA) {
                    convertTGA (iconFile);
                    if (! dumpFileInto (projectFile, iconFile)) {
                        fclose (projectFile);
                        return addComment (ERRORTYPE_PROJECTERROR, "Error adding custom icon (file not found or not a valid TGA or PNG file)", settings.customIcon, NULL, 0);
                    }
                } else {
                    if (! dumpFileInto (projectFile, iconFile)) {
                        fclose (projectFile);
                        return addComment (ERRORTYPE_PROJECTERROR, "Error adding custom icon", settings.customIcon, NULL, 0);
                    }
                }
                delete iconFile;
            }
            // ADD LOGO ------------------------------------
            if (customIconLogo & 2) {
                setCompilerText (COMPILER_TXT_ACTION, "Adding custom logo");
                setCompilerText (COMPILER_TXT_FILENAME, settings.customLogo);
                setCompilerText (COMPILER_TXT_ITEM, "");
                char * logoFile = joinStrings (settings.customLogo, "");
                if (getFileType (logoFile) == FILETYPE_TGA) {
                    convertTGA (logoFile);
                    if (! dumpFileInto (projectFile, logoFile)) {
                        fclose (projectFile);
                        return addComment (ERRORTYPE_PROJECTERROR, "Error adding custom logo (file not found or not a valid TGA or PNG file)", settings.customLogo, NULL, 0);
                    }
                } else {
                    if (! dumpFileInto (projectFile, logoFile)) {
                        fclose (projectFile);
                        return addComment (ERRORTYPE_PROJECTERROR, "Error adding custom logo", settings.customLogo, NULL, 0);
                    }
                }
                delete logoFile;
            }
            //----------------------------------------------

            put2bytes (countElements (globalVarNames), projectFile);

            if (! saveStrings (projectFile, textFile, allSourceStrings)) {
                fclose (projectFile);
                addComment (ERRORTYPE_SYSTEMERROR, "Can't save string bank(s)", NULL);
                return false;
            }

            if (! gotoTempDirectory ()) {
                fclose (projectFile);
                return false;
            }
            tempIndex = fopen ("SLUDGE1.tmp", "wb");
            tempData = fopen ("SLUDGE2.tmp", "wb");

            allTheFunctionNamesTemp = functionNames;
            iSize = countElements (functionNames) * 4 + ftell (projectFile) + 4;

            setCompileStep (CSTEP_LINKSCRIPTS, countElements(functionNames));
            break;

		}

		case CSTEP_LINKSCRIPTS:
		if (data1 < countElements(functionNames)) {
			percRect (data1, P_BOTTOM);
			if (! runLinker (tempData, tempIndex, data1, globalVarNames, iSize, allSourceStrings)) {
				fclose (projectFile);
				return false;
			}
			allTheFunctionNamesTemp = allTheFunctionNamesTemp -> next;
			data1 ++;
		} else {
			setCompileStep (CSTEP_AFTERLINKSCRIPTS, 1);
		}
		break;

		case CSTEP_AFTERLINKSCRIPTS:
		put4bytes (ftell (tempIndex) + ftell (tempData), projectFile);
		fclose (tempIndex);
		fclose (tempData);
		dumpFileInto (projectFile, "SLUDGE1.tmp");
		dumpFileInto (projectFile, "SLUDGE2.tmp");
		setCompileStep (CSTEP_LINKOBJECTS, tot);
		tot = countElements (objectTypeNames);

		tempIndex = fopen ("SLUDGE1.tmp", "wb");
		tempData = fopen ("SLUDGE2.tmp", "wb");

		iSize = tot * 4 + ftell (projectFile) + 4;
		setCompileStep (CSTEP_LINKOBJECTS, tot);

		break;

		case CSTEP_LINKOBJECTS:
		if (data1 < tot) {
			percRect (data1, P_BOTTOM);
			if (! linkObjectFile (tempData, tempIndex, data1, iSize)) {
				fclose (projectFile);
				return false;
			}
			data1 ++;
		} else {
			setCompileStep (CSTEP_AFTERLINKOBJECTS, 0);
		}
		break;

		case CSTEP_AFTERLINKOBJECTS:
		put4bytes (ftell (tempIndex) + ftell (tempData), projectFile);
		fclose (tempIndex);
		fclose (tempData);
		dumpFileInto (projectFile, "SLUDGE1.tmp");
		dumpFileInto (projectFile, "SLUDGE2.tmp");
		unlink ("SLUDGE1.tmp");
		unlink ("SLUDGE2.tmp");

		extern stringArray * globalVarFileOrigins;
		extern stringArray * functionFiles;

		warnAboutUnused (CHECKUSED_FUNCTIONS, functionNames, "Function ", functionFiles);
		warnAboutUnused (CHECKUSED_GLOBALS, globalVarNames, "Global variable ", globalVarFileOrigins);

		setCompileStep (CSTEP_DUMPFILES, 1);
		break;

		case CSTEP_DUMPFILES:
		if (! dumpFiles (projectFile, allFileHandles)) {
			fclose (projectFile);
			return false;
		}
		fclose (projectFile);
		gotoSourceDirectory ();
		char * fromName = joinStrings (settings.finalFile, ".sl~");
		if (gameFile) deleteString (gameFile);
		gameFile = joinStrings (settings.finalFile, settings.forceSilent ? " (silent).slg" : ".slg");
		unlink (gameFile);

		if (rename (fromName, gameFile))
		{
			addComment (ERRORTYPE_SYSTEMERROR, "Couldn't rename the compiled game file... it's been left with the name", fromName, NULL, 0);
		}
		deleteString(fromName);
		setCompileStep (CSTEP_DONE, 0);
		break;
	}

	return true;
}

int compileEverything (char * project, char **fileList, int *numFiles, void (*infoReceiver)(compilerInfo *)) {
	int success = true;
	setInfoReceiver(infoReceiver);

	clearTranslations ();
	if (! getSourceDirFromName (project)) {
		setCompilerText (COMPILER_TXT_ACTION, "Error initialising!");
		setCompilerText (COMPILER_TXT_FILENAME, "Could not find the folder for the source files.");
		return false;
	}

	initBuiltInFunc ();

	compileStep = CSTEP_INIT;
	while (compileStep < CSTEP_DONE) {
		if (! doSingleCompileStep (fileList, numFiles))
		{
			setCompileStep (CSTEP_ERROR, 1);
			gotoSourceDirectory ();
			char * killName = joinStrings (settings.finalFile, ".sl~");
			unlink (killName);
			delete killName;
			success = false;
		}
	}

	destroyAll (functionNames);
	destroyAll (objectTypeNames);
	destroyAll (globalVarNames);
	destroyAll (builtInFunc);
	destroyAll (typeDefFrom);
	destroyAll (allKnownFlags);
	destroyAll (allSourceStrings);
	destroyAll (allFileHandles);

	killTempDir();
	setFinished(success);
	return success;
}