1 /* voc 2.1.0 [2019/11/01]. Bootstrapping compiler for address size 8, alignment 8. xrtspaSF */
2 
3 #define SHORTINT INT8
4 #define INTEGER  INT16
5 #define LONGINT  INT32
6 #define SET      UINT32
7 
8 #include "SYSTEM.h"
9 #include "Configuration.h"
10 #include "Heap.h"
11 #include "Modules.h"
12 #include "OPM.h"
13 #include "Out.h"
14 #include "Platform.h"
15 #include "Strings.h"
16 
17 typedef
18 	CHAR extTools_CommandString[4096];
19 
20 
21 static extTools_CommandString extTools_CFLAGS;
22 
23 
24 export void extTools_Assemble (CHAR *moduleName, ADDRESS moduleName__len);
25 static void extTools_InitialiseCompilerCommand (CHAR *s, ADDRESS s__len);
26 export void extTools_LinkMain (CHAR *moduleName, ADDRESS moduleName__len, BOOLEAN statically, CHAR *additionalopts, ADDRESS additionalopts__len);
27 static void extTools_execute (CHAR *title, ADDRESS title__len, CHAR *cmd, ADDRESS cmd__len);
28 
29 
extTools_execute(CHAR * title,ADDRESS title__len,CHAR * cmd,ADDRESS cmd__len)30 static void extTools_execute (CHAR *title, ADDRESS title__len, CHAR *cmd, ADDRESS cmd__len)
31 {
32 	INT16 r, status, exitcode;
33 	extTools_CommandString fullcmd;
34 	__DUP(title, title__len, CHAR);
35 	__DUP(cmd, cmd__len, CHAR);
36 	if (__IN(18, OPM_Options, 32)) {
37 		Out_String((CHAR*)"  ", 3);
38 		Out_String(cmd, cmd__len);
39 		Out_Ln();
40 	}
41 	__COPY(cmd, fullcmd, 4096);
42 	Heap_GC(0);
43 	r = Platform_System(fullcmd, 4096);
44 	status = __MASK(r, -128);
45 	exitcode = __ASHR(r, 8);
46 	if (exitcode > 127) {
47 		exitcode = exitcode - 256;
48 	}
49 	if (r != 0) {
50 		Out_String(title, title__len);
51 		Out_String(cmd, cmd__len);
52 		Out_Ln();
53 		Out_String((CHAR*)"-- failed: status ", 19);
54 		Out_Int(status, 1);
55 		Out_String((CHAR*)", exitcode ", 12);
56 		Out_Int(exitcode, 1);
57 		Out_String((CHAR*)".", 2);
58 		Out_Ln();
59 		if ((status == 0 && exitcode == 127)) {
60 			Out_String((CHAR*)"Is the C compiler in the current command path\?", 47);
61 			Out_Ln();
62 		}
63 		if (status != 0) {
64 			Modules_Halt(status);
65 		} else {
66 			Modules_Halt(exitcode);
67 		}
68 	}
69 	__DEL(title);
70 	__DEL(cmd);
71 }
72 
extTools_InitialiseCompilerCommand(CHAR * s,ADDRESS s__len)73 static void extTools_InitialiseCompilerCommand (CHAR *s, ADDRESS s__len)
74 {
75 	__COPY("gcc -fPIC -g", s, s__len);
76 	Strings_Append((CHAR*)" -I \"", 6, (void*)s, s__len);
77 	Strings_Append(OPM_ResourceDir, 1024, (void*)s, s__len);
78 	Strings_Append((CHAR*)"/include\" ", 11, (void*)s, s__len);
79 	Platform_GetEnv((CHAR*)"CFLAGS", 7, (void*)extTools_CFLAGS, 4096);
80 	Strings_Append(extTools_CFLAGS, 4096, (void*)s, s__len);
81 	Strings_Append((CHAR*)" ", 2, (void*)s, s__len);
82 }
83 
extTools_Assemble(CHAR * moduleName,ADDRESS moduleName__len)84 void extTools_Assemble (CHAR *moduleName, ADDRESS moduleName__len)
85 {
86 	extTools_CommandString cmd;
87 	__DUP(moduleName, moduleName__len, CHAR);
88 	extTools_InitialiseCompilerCommand((void*)cmd, 4096);
89 	Strings_Append((CHAR*)"-c ", 4, (void*)cmd, 4096);
90 	Strings_Append(moduleName, moduleName__len, (void*)cmd, 4096);
91 	Strings_Append((CHAR*)".c", 3, (void*)cmd, 4096);
92 	extTools_execute((CHAR*)"C compile: ", 12, cmd, 4096);
93 	__DEL(moduleName);
94 }
95 
extTools_LinkMain(CHAR * moduleName,ADDRESS moduleName__len,BOOLEAN statically,CHAR * additionalopts,ADDRESS additionalopts__len)96 void extTools_LinkMain (CHAR *moduleName, ADDRESS moduleName__len, BOOLEAN statically, CHAR *additionalopts, ADDRESS additionalopts__len)
97 {
98 	extTools_CommandString cmd;
99 	__DUP(additionalopts, additionalopts__len, CHAR);
100 	extTools_InitialiseCompilerCommand((void*)cmd, 4096);
101 	Strings_Append(moduleName, moduleName__len, (void*)cmd, 4096);
102 	Strings_Append((CHAR*)".c ", 4, (void*)cmd, 4096);
103 	Strings_Append(additionalopts, additionalopts__len, (void*)cmd, 4096);
104 	if (statically) {
105 		Strings_Append((CHAR*)" -static", 9, (void*)cmd, 4096);
106 	}
107 	Strings_Append((CHAR*)" -o ", 5, (void*)cmd, 4096);
108 	Strings_Append(moduleName, moduleName__len, (void*)cmd, 4096);
109 	if (!statically || 1) {
110 		Strings_Append((CHAR*)" -L\"", 5, (void*)cmd, 4096);
111 		Strings_Append(OPM_InstallDir, 1024, (void*)cmd, 4096);
112 		Strings_Append((CHAR*)"/lib\"", 6, (void*)cmd, 4096);
113 		Strings_Append((CHAR*)" -lvoc", 7, (void*)cmd, 4096);
114 		Strings_Append((CHAR*)"-O", 3, (void*)cmd, 4096);
115 		Strings_Append(OPM_Model, 10, (void*)cmd, 4096);
116 		Strings_Append((CHAR*)"", 1, (void*)cmd, 4096);
117 	}
118 	extTools_execute((CHAR*)"C compile and link: ", 21, cmd, 4096);
119 	__DEL(additionalopts);
120 }
121 
122 
extTools__init(void)123 export void *extTools__init(void)
124 {
125 	__DEFMOD;
126 	__MODULE_IMPORT(Configuration);
127 	__MODULE_IMPORT(Heap);
128 	__MODULE_IMPORT(Modules);
129 	__MODULE_IMPORT(OPM);
130 	__MODULE_IMPORT(Out);
131 	__MODULE_IMPORT(Platform);
132 	__MODULE_IMPORT(Strings);
133 	__REGMOD("extTools", 0);
134 /* BEGIN */
135 	__ENDMOD;
136 }
137