author | Claus Gittinger <cg@exept.de> |
Mon, 08 Aug 2011 23:03:12 +0200 | |
changeset 2644 | 51666ff4ec9f |
parent 2643 | 60d2bae2be58 |
child 2646 | de9d7a6e6315 |
permissions | -rw-r--r-- |
1669 | 1 |
" |
2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
3 |
COPYRIGHT (c) 2006 by eXept Software AG |
|
4 |
All Rights Reserved |
|
5 |
||
6 |
This software is furnished under a license and may be used |
|
7 |
only in accordance with the terms of that license and with the |
|
8 |
inclusion of the above copyright notice. This software may not |
|
9 |
be provided or otherwise made available to, or used by, any |
|
10 |
other person. No title to or ownership of the software is |
|
11 |
hereby transferred. |
|
12 |
" |
|
13 |
"{ Package: 'stx:libcomp' }" |
|
14 |
||
15 |
Object subclass:#STCCompilerInterface |
|
1939 | 16 |
instanceVariableNames:'originator parserFlags initName theNonMetaclassToCompileFor |
17 |
classToCompileFor stFileName cFileName oFileName stcFlags cFlags |
|
18 |
stcPath ccPath requestor methodCategory executionStatus package' |
|
1669 | 19 |
classVariableNames:'SequenceNumber Verbose' |
20 |
poolDictionaries:'' |
|
21 |
category:'System-Compiler' |
|
22 |
! |
|
23 |
||
24 |
!STCCompilerInterface class methodsFor:'documentation'! |
|
25 |
||
26 |
copyright |
|
27 |
" |
|
28 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
29 |
COPYRIGHT (c) 2006 by eXept Software AG |
|
30 |
All Rights Reserved |
|
31 |
||
32 |
This software is furnished under a license and may be used |
|
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
! |
|
40 |
||
41 |
documentation |
|
42 |
" |
|
43 |
a refactored complex method - originally found in ByteCodeCompiler. |
|
44 |
" |
|
45 |
! ! |
|
46 |
||
47 |
!STCCompilerInterface class methodsFor:'accessing'! |
|
48 |
||
49 |
stcPathOf:command |
|
50 |
"return the path to an stc command, or nil if not found." |
|
51 |
||
52 |
|f d reqdSuffix cmd| |
|
53 |
||
54 |
"/ |
|
55 |
"/ care for executable suffix |
|
56 |
"/ |
|
57 |
cmd := command. |
|
58 |
OperatingSystem isMSDOSlike ifTrue:[ |
|
59 |
reqdSuffix := 'exe' |
|
60 |
] ifFalse:[ |
|
61 |
OperatingSystem isVMSlike ifTrue:[ |
|
62 |
reqdSuffix := 'EXE' |
|
63 |
]. |
|
64 |
]. |
|
65 |
reqdSuffix notNil ifTrue:[ |
|
66 |
(f := cmd asFilename) suffix isEmpty ifTrue:[ |
|
67 |
cmd := (f withSuffix:reqdSuffix) name |
|
68 |
] |
|
69 |
]. |
|
70 |
"/ |
|
71 |
"/ for our convenience, also check in current |
|
72 |
"/ and parent directories; even if PATH does not |
|
73 |
"/ include them ... |
|
74 |
"/ |
|
75 |
"/ look in current ... |
|
76 |
d := Filename currentDirectory. |
|
77 |
(f := d construct:cmd) isExecutable ifTrue:[ |
|
78 |
^ f pathName |
|
79 |
]. |
|
80 |
"/ look in ../stc ... |
|
81 |
d := d construct:'..'. |
|
82 |
(f := (d construct:'stc') construct:cmd) isExecutable ifTrue:[ |
|
83 |
^ f pathName |
|
84 |
]. |
|
85 |
"/ look in ../../stc ... |
|
86 |
d := d construct:'..'. |
|
87 |
(f := (d construct:'stc') construct:cmd) isExecutable ifTrue:[ |
|
88 |
^ f pathName |
|
89 |
]. |
|
90 |
||
91 |
"/ |
|
92 |
"/ ok, stc must be installed in some directory along the PATH |
|
93 |
"/ |
|
94 |
^ OperatingSystem pathOfCommand:command |
|
95 |
||
96 |
" |
|
97 |
STCCompilerInterface stcPathOf:'stc' |
|
98 |
" |
|
99 |
||
100 |
"Created: 13.9.1995 / 14:37:16 / claus" |
|
101 |
! ! |
|
102 |
||
103 |
!STCCompilerInterface class methodsFor:'class initialization'! |
|
104 |
||
105 |
initialize |
|
106 |
Verbose := false. |
|
107 |
! ! |
|
108 |
||
109 |
!STCCompilerInterface methodsFor:'accessing'! |
|
110 |
||
2335 | 111 |
cFileName:something |
112 |
cFileName := something. |
|
113 |
! |
|
114 |
||
1669 | 115 |
incrementalStcPath |
116 |
"return the path to the stc command for incremental method compilation, |
|
117 |
or nil if not found." |
|
118 |
||
119 |
|f cmd| |
|
120 |
||
121 |
(cmd := parserFlags stcPath) isEmptyOrNil ifTrue:[ |
|
122 |
(f := self class stcPathOf:'stc') notNil ifTrue:[ |
|
123 |
cmd := f |
|
124 |
] ifFalse:[ |
|
125 |
cmd := self class stcPathOf:'demostc' |
|
126 |
] |
|
127 |
]. |
|
128 |
(cmd notNil and:[cmd includes:Character space]) ifTrue:[ |
|
129 |
cmd := '"' , cmd , '"'. |
|
130 |
]. |
|
131 |
^ cmd |
|
132 |
||
133 |
"Created: 13.9.1995 / 14:36:36 / claus" |
|
134 |
"Modified: 13.9.1995 / 15:15:04 / claus" |
|
135 |
! |
|
136 |
||
137 |
originator:something |
|
138 |
originator := something. |
|
139 |
! |
|
140 |
||
141 |
parserFlags:something |
|
142 |
parserFlags := something. |
|
2335 | 143 |
! |
144 |
||
145 |
stFileName:something |
|
146 |
stFileName := something. |
|
1669 | 147 |
! ! |
148 |
||
149 |
!STCCompilerInterface methodsFor:'machine code generation'! |
|
150 |
||
1939 | 151 |
compileToMachineCode:aString forClass:aClass selector:selector inCategory:categoryArg |
152 |
notifying:requestorArg install:install skipIfSame:skipIfSame silent:silent |
|
1669 | 153 |
"this is called to compile primitive code. |
154 |
This is EXPERIMENTAL and going to be changed to raise an error, |
|
155 |
an redefined in subclasses which can do it (either by direct compilation, or by calling |
|
1939 | 156 |
the external stc do do it)." |
1669 | 157 |
|
1941 | 158 |
|handle oldMethod newMethod ok moduleFileName| |
1669 | 159 |
|
160 |
install ifFalse:[ |
|
161 |
"/ cannot do it uninstalled. reason: |
|
162 |
"/ if it is loaded twice, the first version could be unloaded by |
|
163 |
"/ finalization, which would also unload the second version |
|
164 |
"/ (because the first unload would unload the second version too) |
|
165 |
^ #CannotLoad |
|
166 |
]. |
|
167 |
parserFlags stcCompilation == #never ifTrue:[^ #CannotLoad]. |
|
1670 | 168 |
|
1939 | 169 |
classToCompileFor := aClass. |
170 |
requestor := requestorArg. |
|
171 |
methodCategory := categoryArg. |
|
1669 | 172 |
|
1939 | 173 |
self ensureModuleDirectoryExists. |
174 |
self ensureExternalToolsArePresent ifFalse:[^ #CannotLoad]. |
|
1669 | 175 |
|
1939 | 176 |
theNonMetaclassToCompileFor := classToCompileFor theNonMetaclass. |
1941 | 177 |
|
1939 | 178 |
self ensureSuperClassesAreLoadedOf:theNonMetaclassToCompileFor. |
179 |
theNonMetaclassToCompileFor privateClassesSorted do:[:aPrivateClass | |
|
1670 | 180 |
self ensureSuperClassesAreLoadedOf:aPrivateClass. |
181 |
]. |
|
182 |
||
1939 | 183 |
(classToCompileFor isNil |
184 |
or:[parserFlags allowExtensionsToPrivateClasses |
|
185 |
or:[classToCompileFor owningClass isNil]]) ifTrue:[ |
|
186 |
(requestor respondsTo:#packageToInstall) ifFalse:[ |
|
187 |
package := Class packageQuerySignal query. |
|
188 |
] ifTrue:[ |
|
189 |
package := requestor packageToInstall |
|
190 |
]. |
|
191 |
] ifFalse:[ |
|
192 |
package := classToCompileFor owningClass package |
|
1669 | 193 |
]. |
194 |
||
195 |
[ |
|
1939 | 196 |
self generateSTSource:aString. |
197 |
self setupCompilationCommandArguments. |
|
198 |
ok := self compileToC. |
|
1669 | 199 |
ok ifTrue:[ |
2491 | 200 |
parserFlags stcKeepSIntermediate ifTrue:[ self compileToS ]. |
1669 | 201 |
"/ now compile to machine code |
1939 | 202 |
ok := self compileToObj. |
1669 | 203 |
]. |
204 |
||
205 |
ok ifFalse:[ |
|
1939 | 206 |
self reportCompilationError. |
1669 | 207 |
^ #Error |
208 |
]. |
|
209 |
||
210 |
originator activityNotification:''. |
|
211 |
OperatingSystem removeFile:'errorOutput'. |
|
212 |
||
213 |
" |
|
214 |
if required, make a shared or otherwise loadable object file for it |
|
215 |
" |
|
216 |
originator activityNotification:'linking'. |
|
217 |
||
218 |
oFileName := ObjectFileLoader createLoadableObjectFor:initName. |
|
219 |
oFileName isNil ifTrue:[ |
|
220 |
"/ something went wrong |
|
221 |
originator parseError:('link error: ' , ObjectFileLoader lastError) position:1. |
|
222 |
^ #CannotLoad |
|
223 |
]. |
|
224 |
oFileName asFilename exists ifFalse:[ |
|
225 |
originator parseError:'link failed - cannot create machine code' position:1. |
|
226 |
^ #CannotLoad |
|
227 |
]. |
|
228 |
||
229 |
" |
|
230 |
move it into the modules directory |
|
231 |
" |
|
232 |
moduleFileName := (parserFlags stcModulePath asFilename construct:(initName , '.' , (oFileName asFilename suffix))) name. |
|
233 |
oFileName asFilename moveTo:moduleFileName. |
|
234 |
(moduleFileName asFilename exists |
|
235 |
and:[moduleFileName asFilename isReadable]) ifFalse:[ |
|
236 |
originator parseError:'link failed - cannot move shared library module to ''modules'' directory' position:1. |
|
237 |
^ #CannotLoad |
|
238 |
]. |
|
239 |
||
1939 | 240 |
oldMethod := classToCompileFor compiledMethodAt:selector. |
241 |
oldMethod notNil ifTrue:[package := oldMethod package]. |
|
1669 | 242 |
|
243 |
" |
|
244 |
load the method objectfile |
|
245 |
" |
|
246 |
originator activityNotification:'loading'. |
|
247 |
||
248 |
handle := ObjectFileLoader loadMethodObjectFile:moduleFileName. |
|
249 |
handle isNil ifTrue:[ |
|
250 |
OperatingSystem removeFile:moduleFileName. |
|
2185
cb311d08a486
changed #compileToMachineCode:forClass:selector:inCategory:notifying:install:skipIfSame:silent:
Stefan Vogel <sv@exept.de>
parents:
2068
diff
changeset
|
251 |
"catch, so that #CannotLoad processing is done" |
2350
16caba266df4
changed: #compileToMachineCode:forClass:selector:inCategory:notifying:install:skipIfSame:silent:
Stefan Vogel <sv@exept.de>
parents:
2336
diff
changeset
|
252 |
Parser parseErrorSignal catch:[ |
2185
cb311d08a486
changed #compileToMachineCode:forClass:selector:inCategory:notifying:install:skipIfSame:silent:
Stefan Vogel <sv@exept.de>
parents:
2068
diff
changeset
|
253 |
originator parseError:'dynamic load of machine code failed' position:1. |
cb311d08a486
changed #compileToMachineCode:forClass:selector:inCategory:notifying:install:skipIfSame:silent:
Stefan Vogel <sv@exept.de>
parents:
2068
diff
changeset
|
254 |
]. |
1669 | 255 |
^ #CannotLoad |
256 |
]. |
|
257 |
||
258 |
" |
|
259 |
did it work ? |
|
260 |
" |
|
1939 | 261 |
newMethod := classToCompileFor compiledMethodAt:selector. |
1669 | 262 |
|
263 |
"/ if install is false, we have to undo the install (which is always done, when loading machine code) |
|
264 |
install ifFalse:[ |
|
265 |
oldMethod isNil ifTrue:[ |
|
1939 | 266 |
classToCompileFor removeSelector:selector |
1669 | 267 |
] ifFalse:[ |
268 |
newMethod setPackage:oldMethod package. |
|
1939 | 269 |
classToCompileFor addSelector:selector withMethod:oldMethod. |
270 |
oldMethod setPackage:package. |
|
1669 | 271 |
] |
272 |
]. |
|
273 |
||
274 |
newMethod notNil ifTrue:[ |
|
275 |
handle method ~~ newMethod ifTrue:[ |
|
276 |
'Compiler [warning]: loaded method installed itself in another class' errorPrintCR. |
|
277 |
]. |
|
278 |
||
279 |
newMethod source:aString string. |
|
1939 | 280 |
newMethod setPackage:package. |
1669 | 281 |
"/ Project notNil ifTrue:[ |
282 |
"/ newMethod package:(Project currentPackageName) |
|
283 |
"/ ]. |
|
284 |
||
1939 | 285 |
"/ classToCompileFor updateRevisionString. |
1669 | 286 |
install ifTrue:[ |
1939 | 287 |
classToCompileFor addChangeRecordForMethod:newMethod fromOld:oldMethod. |
1669 | 288 |
|
289 |
"/ kludge-sigh: must send change messages manually here (stc-loaded code does not do it) |
|
290 |
"/ see addMethod:... in ClassDescription |
|
1939 | 291 |
classToCompileFor changed:#methodDictionary with:(Array with:selector with:oldMethod). |
292 |
Smalltalk changed:#methodInClass with:(Array with:classToCompileFor with:selector with:oldMethod). |
|
2350
16caba266df4
changed: #compileToMachineCode:forClass:selector:inCategory:notifying:install:skipIfSame:silent:
Stefan Vogel <sv@exept.de>
parents:
2336
diff
changeset
|
293 |
] ifFalse:[ |
16caba266df4
changed: #compileToMachineCode:forClass:selector:inCategory:notifying:install:skipIfSame:silent:
Stefan Vogel <sv@exept.de>
parents:
2336
diff
changeset
|
294 |
oldMethod := nil. |
1669 | 295 |
]. |
296 |
||
1986 | 297 |
silent ifFalse:[ |
1941 | 298 |
Transcript showCR:(' compiled: ', aClass name,' ',selector,' - machine code') |
1669 | 299 |
]. |
300 |
ObjectMemory flushCaches. |
|
301 |
||
302 |
handle method:newMethod. |
|
303 |
||
2350
16caba266df4
changed: #compileToMachineCode:forClass:selector:inCategory:notifying:install:skipIfSame:silent:
Stefan Vogel <sv@exept.de>
parents:
2336
diff
changeset
|
304 |
"/ check for obsolete compiled method code and unload the |
16caba266df4
changed: #compileToMachineCode:forClass:selector:inCategory:notifying:install:skipIfSame:silent:
Stefan Vogel <sv@exept.de>
parents:
2336
diff
changeset
|
305 |
"/ corresponding ObjectFileHandle. |
16caba266df4
changed: #compileToMachineCode:forClass:selector:inCategory:notifying:install:skipIfSame:silent:
Stefan Vogel <sv@exept.de>
parents:
2336
diff
changeset
|
306 |
"/ The old method with its source code is usually kept in the method history. |
16caba266df4
changed: #compileToMachineCode:forClass:selector:inCategory:notifying:install:skipIfSame:silent:
Stefan Vogel <sv@exept.de>
parents:
2336
diff
changeset
|
307 |
"/ and will be recompiled on an undo |
1669 | 308 |
|
309 |
ObjectFileLoader loadedObjectHandlesDo:[:anotherHandle | |
|
310 |
anotherHandle isMethodHandle ifTrue:[ |
|
2350
16caba266df4
changed: #compileToMachineCode:forClass:selector:inCategory:notifying:install:skipIfSame:silent:
Stefan Vogel <sv@exept.de>
parents:
2336
diff
changeset
|
311 |
anotherHandle method == oldMethod ifTrue:[ |
16caba266df4
changed: #compileToMachineCode:forClass:selector:inCategory:notifying:install:skipIfSame:silent:
Stefan Vogel <sv@exept.de>
parents:
2336
diff
changeset
|
312 |
"break association betwen old method, code and handle" |
16caba266df4
changed: #compileToMachineCode:forClass:selector:inCategory:notifying:install:skipIfSame:silent:
Stefan Vogel <sv@exept.de>
parents:
2336
diff
changeset
|
313 |
anotherHandle removeConnectedObjects. |
16caba266df4
changed: #compileToMachineCode:forClass:selector:inCategory:notifying:install:skipIfSame:silent:
Stefan Vogel <sv@exept.de>
parents:
2336
diff
changeset
|
314 |
]. |
16caba266df4
changed: #compileToMachineCode:forClass:selector:inCategory:notifying:install:skipIfSame:silent:
Stefan Vogel <sv@exept.de>
parents:
2336
diff
changeset
|
315 |
anotherHandle isObsolete ifTrue:[ |
16caba266df4
changed: #compileToMachineCode:forClass:selector:inCategory:notifying:install:skipIfSame:silent:
Stefan Vogel <sv@exept.de>
parents:
2336
diff
changeset
|
316 |
anotherHandle unload. |
16caba266df4
changed: #compileToMachineCode:forClass:selector:inCategory:notifying:install:skipIfSame:silent:
Stefan Vogel <sv@exept.de>
parents:
2336
diff
changeset
|
317 |
anotherHandle removeUnusedObjectFile. |
16caba266df4
changed: #compileToMachineCode:forClass:selector:inCategory:notifying:install:skipIfSame:silent:
Stefan Vogel <sv@exept.de>
parents:
2336
diff
changeset
|
318 |
]. |
16caba266df4
changed: #compileToMachineCode:forClass:selector:inCategory:notifying:install:skipIfSame:silent:
Stefan Vogel <sv@exept.de>
parents:
2336
diff
changeset
|
319 |
]. |
1669 | 320 |
]. |
321 |
^ newMethod. |
|
322 |
]. |
|
323 |
||
324 |
OperatingSystem removeFile:moduleFileName. |
|
325 |
originator parseError:'dynamic load failed' position:1. |
|
326 |
^ #CannotLoad |
|
327 |
] ensure:[ |
|
328 |
parserFlags stcKeepSTIntermediate ifFalse:[ |
|
329 |
OperatingSystem removeFile:stFileName. |
|
330 |
OperatingSystem removeFile:'errorOutput'. |
|
331 |
]. |
|
332 |
parserFlags stcKeepOIntermediate == true ifFalse:[ |
|
333 |
(oFileName notNil and:[oFileName asFilename exists]) ifTrue:[oFileName asFilename delete]. |
|
334 |
]. |
|
335 |
parserFlags stcKeepCIntermediate == true ifFalse:[ |
|
336 |
(cFileName notNil and:[cFileName asFilename exists]) ifTrue:[cFileName asFilename delete]. |
|
337 |
]. |
|
338 |
OperatingSystem isMSDOSlike ifTrue:[ |
|
339 |
"/ (mapFileName notNil and:[mapFileName asFilename exists]) ifTrue:[mapFileName asFilename delete]. |
|
340 |
"/ (libFileName notNil and:[libFileName asFilename exists]) ifTrue:[libFileName asFilename delete]. |
|
341 |
]. |
|
342 |
]. |
|
343 |
||
344 |
" |
|
345 |
|m| |
|
346 |
||
347 |
Object subclass:#Test |
|
348 |
instanceVariableNames:'' |
|
349 |
classVariableNames:'' |
|
350 |
poolDictionaries:'' |
|
351 |
category:'tests'. |
|
352 |
m := ByteCodeCompiler |
|
353 |
compile:'foo ^ ''hello''' |
|
354 |
forClass:Test |
|
355 |
inCategory:'tests' |
|
356 |
notifying:nil |
|
357 |
install:false |
|
358 |
skipIfSame:false. |
|
359 |
m inspect |
|
360 |
" |
|
361 |
" |
|
362 |
|m| |
|
363 |
||
364 |
Object subclass:#Test |
|
365 |
instanceVariableNames:'' |
|
366 |
classVariableNames:'' |
|
367 |
poolDictionaries:'' |
|
368 |
category:'tests'. |
|
369 |
m := ByteCodeCompiler |
|
370 |
compileToMachineCode:'foo %{ RETURN (_MKSMALLINT(1)); %}' |
|
371 |
forClass:Test |
|
372 |
inCategory:'tests' |
|
373 |
notifying:nil |
|
374 |
install:false |
|
375 |
skipIfSame:false |
|
376 |
silent:false. |
|
377 |
m inspect |
|
378 |
" |
|
379 |
||
1939 | 380 |
"Modified: / 14-09-1995 / 22:33:04 / claus" |
381 |
"Modified: / 19-03-1999 / 08:31:42 / stefan" |
|
1941 | 382 |
"Modified: / 07-11-2006 / 14:21:46 / cg" |
1939 | 383 |
! ! |
384 |
||
385 |
!STCCompilerInterface methodsFor:'machine code generation-helpers'! |
|
386 |
||
387 |
compileToC |
|
388 |
"compile st to C using stc" |
|
389 |
||
390 |
|command errorStream ok| |
|
391 |
||
2643
60d2bae2be58
comment/format in: #generateSTSource:
Claus Gittinger <cg@exept.de>
parents:
2491
diff
changeset
|
392 |
command := stcPath , ' ' , stcFlags , ' -C ' , (stFileName asFilename pathName). |
1939 | 393 |
errorStream := 'errorOutput' asFilename writeStream. |
394 |
||
395 |
Verbose == true ifTrue:[ |
|
396 |
'executing: ' infoPrint. command infoPrintCR. |
|
397 |
]. |
|
398 |
||
399 |
originator activityNotification:'compiling (stc)'. |
|
400 |
ok := OperatingSystem |
|
401 |
executeCommand:command |
|
402 |
inputFrom:nil |
|
403 |
outputTo:errorStream |
|
404 |
errorTo:errorStream |
|
405 |
onError:[:stat| |
|
406 |
executionStatus := stat. |
|
407 |
false |
|
408 |
]. |
|
409 |
||
410 |
errorStream close. |
|
411 |
||
412 |
cFileName asFilename exists ifTrue:[ |
|
413 |
ok ifFalse:[ |
|
414 |
'Compiler [info]: oops - system says stc failed - but c-file is there ...' infoPrintCR. |
|
415 |
ok := true |
|
416 |
] |
|
417 |
] ifFalse:[ |
|
418 |
ok ifTrue:[ |
|
419 |
'Compiler [info]: oops - system says stc ok - but no c-file is there ...' infoPrintCR. |
|
420 |
]. |
|
421 |
ok := false |
|
422 |
]. |
|
423 |
^ ok |
|
424 |
||
425 |
"Created: / 07-11-2006 / 12:11:24 / cg" |
|
2643
60d2bae2be58
comment/format in: #generateSTSource:
Claus Gittinger <cg@exept.de>
parents:
2491
diff
changeset
|
426 |
"Modified: / 08-08-2011 / 22:12:01 / cg" |
1939 | 427 |
! |
428 |
||
2336 | 429 |
compileToExe |
430 |
"compile C to exe, using cc" |
|
431 |
||
432 |
|command errorStream ok| |
|
433 |
||
434 |
errorStream := 'errorOutput' asFilename newReadWriteStream. |
|
435 |
||
436 |
command := ccPath , ' ' , cFlags , ' -D__INCREMENTAL_COMPILE__ ' , cFileName. |
|
437 |
||
438 |
Verbose == true ifTrue:[ |
|
439 |
'executing: ' infoPrint. command infoPrintCR. |
|
440 |
]. |
|
441 |
originator activityNotification:'compiling (' , ccPath , ')'. |
|
442 |
ok := OperatingSystem |
|
443 |
executeCommand:command |
|
444 |
inputFrom:nil |
|
445 |
outputTo:errorStream |
|
446 |
errorTo:errorStream |
|
447 |
onError:[:stat| |
|
448 |
executionStatus := stat. |
|
449 |
false |
|
450 |
]. |
|
451 |
||
452 |
ok ifFalse:[ |
|
453 |
errorStream reset. |
|
454 |
errorStream copyToEndInto:Transcript. |
|
455 |
]. |
|
456 |
||
457 |
errorStream close. |
|
458 |
||
459 |
^ ok |
|
460 |
! |
|
461 |
||
1939 | 462 |
compileToObj |
463 |
"compile C to obj, using cc" |
|
464 |
||
465 |
|command errorStream ok| |
|
466 |
||
2195
2996fe27c2f8
Add primitive functions when compiling
Stefan Vogel <sv@exept.de>
parents:
2185
diff
changeset
|
467 |
errorStream := 'errorOutput' asFilename newReadWriteStream. |
1939 | 468 |
|
2644 | 469 |
ccPath includesSeparator ifTrue:[ |
470 |
command := '"',ccPath , '"' |
|
471 |
] ifFalse:[ |
|
472 |
command := ccPath |
|
473 |
]. |
|
474 |
command := command , ' ' , cFlags , ' -D__INCREMENTAL_COMPILE__ -c ' , cFileName. |
|
1939 | 475 |
|
476 |
Verbose == true ifTrue:[ |
|
477 |
'executing: ' infoPrint. command infoPrintCR. |
|
478 |
]. |
|
479 |
originator activityNotification:'compiling (' , ccPath , ')'. |
|
480 |
ok := OperatingSystem |
|
481 |
executeCommand:command |
|
482 |
inputFrom:nil |
|
483 |
outputTo:errorStream |
|
484 |
errorTo:errorStream |
|
485 |
onError:[:stat| |
|
486 |
executionStatus := stat. |
|
487 |
false |
|
2195
2996fe27c2f8
Add primitive functions when compiling
Stefan Vogel <sv@exept.de>
parents:
2185
diff
changeset
|
488 |
]. |
2996fe27c2f8
Add primitive functions when compiling
Stefan Vogel <sv@exept.de>
parents:
2185
diff
changeset
|
489 |
|
2996fe27c2f8
Add primitive functions when compiling
Stefan Vogel <sv@exept.de>
parents:
2185
diff
changeset
|
490 |
ok ifFalse:[ |
2996fe27c2f8
Add primitive functions when compiling
Stefan Vogel <sv@exept.de>
parents:
2185
diff
changeset
|
491 |
errorStream reset. |
2996fe27c2f8
Add primitive functions when compiling
Stefan Vogel <sv@exept.de>
parents:
2185
diff
changeset
|
492 |
errorStream copyToEndInto:Transcript. |
2996fe27c2f8
Add primitive functions when compiling
Stefan Vogel <sv@exept.de>
parents:
2185
diff
changeset
|
493 |
]. |
1939 | 494 |
|
495 |
errorStream close. |
|
496 |
||
497 |
oFileName asFilename exists ifTrue:[ |
|
498 |
ok ifFalse:[ |
|
499 |
'Compiler [info]: system says compile failed - but o-file is there ...' infoPrintCR. |
|
500 |
ok := true |
|
501 |
] |
|
502 |
] ifFalse:[ |
|
503 |
ok ifTrue:[ |
|
504 |
'Compiler [info]: system says compile ok - but no o-file is there ...' infoPrintCR. |
|
505 |
]. |
|
506 |
ok := false |
|
507 |
]. |
|
508 |
^ ok |
|
509 |
||
510 |
"Created: / 07-11-2006 / 12:14:51 / cg" |
|
511 |
! |
|
512 |
||
2491 | 513 |
compileToS |
514 |
"compile C to assembler, using cc" |
|
515 |
||
516 |
|command errorStream ok| |
|
517 |
||
518 |
errorStream := 'errorOutput' asFilename newReadWriteStream. |
|
519 |
||
520 |
command := ccPath , ' ' , cFlags , ' -D__INCREMENTAL_COMPILE__ -S ' , cFileName. |
|
521 |
||
522 |
Verbose == true ifTrue:[ |
|
523 |
'executing: ' infoPrint. command infoPrintCR. |
|
524 |
]. |
|
525 |
originator activityNotification:'compiling (' , ccPath , ')'. |
|
526 |
ok := OperatingSystem |
|
527 |
executeCommand:command |
|
528 |
inputFrom:nil |
|
529 |
outputTo:errorStream |
|
530 |
errorTo:errorStream |
|
531 |
onError:[:stat| |
|
532 |
executionStatus := stat. |
|
533 |
false |
|
534 |
]. |
|
535 |
||
536 |
ok ifFalse:[ |
|
537 |
errorStream reset. |
|
538 |
errorStream copyToEndInto:Transcript. |
|
539 |
]. |
|
540 |
||
541 |
errorStream close. |
|
542 |
^ ok |
|
543 |
! |
|
544 |
||
1939 | 545 |
ensureExternalToolsArePresent |
546 |
(stcPath := self incrementalStcPath) isNil ifTrue:[ |
|
547 |
originator parseError:'no stc compiler available - cannot create machine code' position:1. |
|
548 |
^ false |
|
549 |
]. |
|
550 |
(ccPath := parserFlags ccPath) isNil ifTrue:[ |
|
551 |
originator parseError:'no cc compiler available - cannot create machine code' position:1. |
|
552 |
^ false |
|
553 |
]. |
|
554 |
||
555 |
(ObjectFileLoader notNil and:[ObjectFileLoader canLoadObjectFiles]) ifFalse:[ |
|
556 |
originator parseError:'no dynamic loader configured - cannot create machine code' position:1. |
|
557 |
^ false |
|
558 |
]. |
|
559 |
^ true |
|
560 |
||
561 |
"Created: / 07-11-2006 / 12:31:48 / cg" |
|
1670 | 562 |
! |
563 |
||
564 |
ensureModuleDirectoryExists |
|
565 |
|mP t s| |
|
566 |
||
567 |
(mP := parserFlags stcModulePath asFilename) exists ifFalse:[ |
|
568 |
mP makeDirectory |
|
569 |
]. |
|
570 |
(mP isDirectory and:[ mP isReadable and:[ mP isWritable ] ]) ifFalse:[ |
|
571 |
Parser::ParseError raiseErrorString:('No access to temporary module directory: ' , mP pathName). |
|
572 |
]. |
|
573 |
"/ create a small README there ... |
|
574 |
||
575 |
(t := mP construct:'README') exists ifFalse:[ |
|
576 |
s := t writeStream. |
|
577 |
s |
|
578 |
nextPutAll:'This temporary ST/X directory contains machine code for |
|
579 |
accepted methods with embedded C-code |
|
580 |
(i.e. dynamic compiled code for inline-C methods). |
|
581 |
||
582 |
Files here are not automatically removed, since ST/X |
|
583 |
cannot determine if any (other) snapshot image still |
|
584 |
requires a file here. |
|
585 |
||
586 |
Please be careful when removing files here - a snapshot |
|
587 |
image which was saved with accepted embedded C-code |
|
588 |
may not be able to restart correctly if you remove a |
|
589 |
required file. |
|
590 |
Also, when you export a snapshot image for execution |
|
591 |
on another machine, make certain that the required |
|
592 |
module-files are also present there. |
|
593 |
||
594 |
You should periodically clean dead entries here. |
|
595 |
i.e. remove files, when you are certain that none |
|
596 |
of your snapshot images refers to any module here. |
|
597 |
||
598 |
See the launchers File-Modules dialog for a list of |
|
599 |
modules which are still required by your running image. |
|
600 |
||
601 |
With kind regards - your ST/X. |
|
602 |
'. |
|
603 |
s close. |
|
604 |
]. |
|
605 |
! |
|
606 |
||
607 |
ensureSuperClassesAreLoadedOf:aClass |
|
608 |
|supers| |
|
609 |
||
610 |
supers := aClass allSuperclasses. |
|
611 |
supers reverseDo:[:cls| |
|
612 |
cls isLoaded ifFalse:[ |
|
613 |
Parser::ParseError raiseErrorString:'Cannot stc-compile (Some superclass is unloaded)'. |
|
614 |
] |
|
615 |
]. |
|
616 |
! |
|
617 |
||
618 |
fileOutAllDefinitionsOf:aClass to:aStream rememberIn:definedClasses |
|
619 |
|defineAction| |
|
620 |
||
621 |
defineAction := |
|
622 |
[:cls| |
|
623 |
(definedClasses includes:cls) ifFalse:[ |
|
624 |
cls |
|
625 |
basicFileOutDefinitionOn:aStream |
|
626 |
withNameSpace:false withPackage:false |
|
627 |
syntaxHilighting:false. |
|
628 |
||
629 |
aStream nextPut:(aStream class chunkSeparator); cr. |
|
630 |
definedClasses add:cls. |
|
631 |
]. |
|
632 |
]. |
|
633 |
||
634 |
aClass allSuperclasses reverseDo:defineAction. |
|
635 |
defineAction value:aClass. |
|
1939 | 636 |
! |
637 |
||
638 |
generateSTSource:aString |
|
639 |
|stream definedClasses sep className| |
|
640 |
||
641 |
"/ generate a unique name, consisting of my processID and a sequence number |
|
642 |
"/ the processId is added to allow filein of modules from different |
|
643 |
"/ lifes |
|
644 |
||
645 |
SequenceNumber := (SequenceNumber ? 0) + 1. |
|
646 |
||
647 |
initName := 'm_' , OperatingSystem getProcessId printString, '_' , SequenceNumber printString. |
|
648 |
||
649 |
stFileName := (Filename currentDirectory construct:(initName , '.st')) name. |
|
650 |
[ |
|
651 |
stream := stFileName asFilename writeStream. |
|
652 |
] on:FileStream openErrorSignal do:[:ex| |
|
653 |
originator parseError:'cannot create temporary sourcefile for compilation'. |
|
654 |
^ #CannotLoad |
|
655 |
]. |
|
656 |
||
657 |
definedClasses := IdentitySet new. |
|
658 |
||
659 |
sep := stream class chunkSeparator. |
|
660 |
||
661 |
Class fileOutNameSpaceQuerySignal answer:true |
|
662 |
do:[ |
|
2467 | 663 |
theNonMetaclassToCompileFor realSharedPoolNames do:[:eachPoolname | |
2067
6204fefb44a3
must also file-out the definition of sharedPools
Claus Gittinger <cg@exept.de>
parents:
2026
diff
changeset
|
664 |
|pool| |
6204fefb44a3
must also file-out the definition of sharedPools
Claus Gittinger <cg@exept.de>
parents:
2026
diff
changeset
|
665 |
|
6204fefb44a3
must also file-out the definition of sharedPools
Claus Gittinger <cg@exept.de>
parents:
2026
diff
changeset
|
666 |
pool := Smalltalk at:eachPoolname. |
6204fefb44a3
must also file-out the definition of sharedPools
Claus Gittinger <cg@exept.de>
parents:
2026
diff
changeset
|
667 |
self |
6204fefb44a3
must also file-out the definition of sharedPools
Claus Gittinger <cg@exept.de>
parents:
2026
diff
changeset
|
668 |
fileOutAllDefinitionsOf:pool |
6204fefb44a3
must also file-out the definition of sharedPools
Claus Gittinger <cg@exept.de>
parents:
2026
diff
changeset
|
669 |
to:stream |
6204fefb44a3
must also file-out the definition of sharedPools
Claus Gittinger <cg@exept.de>
parents:
2026
diff
changeset
|
670 |
rememberIn:definedClasses. |
6204fefb44a3
must also file-out the definition of sharedPools
Claus Gittinger <cg@exept.de>
parents:
2026
diff
changeset
|
671 |
]. |
6204fefb44a3
must also file-out the definition of sharedPools
Claus Gittinger <cg@exept.de>
parents:
2026
diff
changeset
|
672 |
|
1939 | 673 |
self |
674 |
fileOutAllDefinitionsOf:theNonMetaclassToCompileFor |
|
675 |
to:stream |
|
676 |
rememberIn:definedClasses. |
|
677 |
||
678 |
theNonMetaclassToCompileFor privateClassesSorted do:[:aPrivateClass | |
|
679 |
self |
|
680 |
fileOutAllDefinitionsOf:aPrivateClass |
|
681 |
to:stream |
|
682 |
rememberIn:definedClasses. |
|
683 |
]. |
|
2195
2996fe27c2f8
Add primitive functions when compiling
Stefan Vogel <sv@exept.de>
parents:
2185
diff
changeset
|
684 |
"/ theNonMetaclassToCompileFor fileOutPrimitiveDefinitionsOn:stream. |
2026 | 685 |
"/ If a method uses a static primitive function... - but this doesn't work |
2195
2996fe27c2f8
Add primitive functions when compiling
Stefan Vogel <sv@exept.de>
parents:
2185
diff
changeset
|
686 |
"/ Yes it does work, but primitive functions have to be strictly static!! |
2996fe27c2f8
Add primitive functions when compiling
Stefan Vogel <sv@exept.de>
parents:
2185
diff
changeset
|
687 |
theNonMetaclassToCompileFor fileOutPrimitiveSpecsOn:stream. |
1939 | 688 |
]. |
689 |
||
2026 | 690 |
"/ stream cr. |
691 |
"/ stream nextPutLine:'"{ Package: ''' , package , ''' }"'. |
|
692 |
"/ stream cr. |
|
1939 | 693 |
|
694 |
stream nextPut:sep. |
|
695 |
className := theNonMetaclassToCompileFor name. |
|
696 |
||
697 |
stream nextPutAll:className. |
|
698 |
classToCompileFor isMeta ifTrue:[ |
|
699 |
stream nextPutAll:' class'. |
|
700 |
]. |
|
701 |
stream nextPutAll:' methodsFor:'''; nextPutAll:methodCategory; nextPutAll:''''. |
|
702 |
stream nextPut:sep; cr. |
|
703 |
||
704 |
stream nextPutLine:'"{ Line: 0 }"'; |
|
705 |
nextChunkPut:aString; |
|
706 |
space; nextPut:sep. |
|
707 |
||
708 |
stream close. |
|
709 |
||
2467 | 710 |
"Modified: / 18-01-2011 / 18:02:39 / cg" |
1939 | 711 |
! |
712 |
||
713 |
reportCompilationError |
|
714 |
|eMsg errorMessages lNr| |
|
715 |
||
716 |
(executionStatus notNil and:[executionStatus couldNotExecute]) ifTrue:[ |
|
717 |
eMsg := 'oops, no STC - cannot create machine code' |
|
718 |
] ifFalse:[ |
|
719 |
errorMessages := 'errorOutput' asFilename contents. |
|
720 |
OperatingSystem removeFile:'errorOutput'. |
|
721 |
||
722 |
errorMessages notNil ifTrue:[ |
|
723 |
errorMessages := errorMessages reject:[:line | line includesString:'Warning:']. |
|
724 |
||
725 |
errorMessages size > 20 ifTrue:[ |
|
726 |
errorMessages := errorMessages select:[:line | line asLowercase startsWith:'error']. |
|
727 |
errorMessages size > 20 ifTrue:[ |
|
728 |
errorMessages := (errorMessages copyTo:20) copyWith:'... more messages skipped' |
|
729 |
]. |
|
730 |
]. |
|
731 |
"/ errorMessages := errorMessages collect:[:line | |
|
732 |
"/ (line startsWith:(stFileName , ':')) ifTrue:[ |
|
733 |
"/ 'Line: ' , (line copyFrom:(stFileName size + 2)) |
|
734 |
"/ ] ifFalse:[ |
|
735 |
"/ line |
|
736 |
"/ ] |
|
737 |
"/ ]. |
|
738 |
]. |
|
739 |
errorMessages isNil ifTrue:[ |
|
740 |
errorMessages := #('') |
|
741 |
]. |
|
742 |
||
743 |
"/ try to extract a line number" |
|
744 |
(errorMessages contains:[:line | line includesString:'Borland']) ifTrue:[ |
|
745 |
|i i2 s| |
|
746 |
i := errorMessages findFirst:[:l | l startsWith:(cFileName,':')]. |
|
747 |
i ~~ 0 ifTrue:[ |
|
748 |
((errorMessages at:i+1) startsWith:'Error') ifTrue:[ |
|
749 |
i2 := (errorMessages at:i+1) indexOfSubCollection:(stFileName). |
|
750 |
i2 ~~ 0 ifTrue:[ |
|
751 |
s := (errorMessages at:i+1) copyFrom:(i2+stFileName size+1). |
|
752 |
s := s readStream. |
|
753 |
lNr := Integer readFrom:s. |
|
754 |
s skipSeparators. |
|
755 |
]. |
|
756 |
] |
|
757 |
]. |
|
758 |
]. |
|
759 |
||
760 |
errorMessages isEmpty ifTrue:[ |
|
761 |
eMsg := 'Error during compilation:\\Unspecified error (no output)' withCRs |
|
762 |
] ifFalse:[ |
|
763 |
eMsg := 'Error during compilation:\\' withCRs , |
|
764 |
(errorMessages asStringCollection asString). |
|
765 |
]. |
|
766 |
]. |
|
2461
89458faaeeb3
changed: #reportCompilationError
Stefan Vogel <sv@exept.de>
parents:
2350
diff
changeset
|
767 |
|
1939 | 768 |
originator activityNotification:''. |
769 |
||
2461
89458faaeeb3
changed: #reportCompilationError
Stefan Vogel <sv@exept.de>
parents:
2350
diff
changeset
|
770 |
Parser::ParseError new |
89458faaeeb3
changed: #reportCompilationError
Stefan Vogel <sv@exept.de>
parents:
2350
diff
changeset
|
771 |
lineNumber:lNr; |
89458faaeeb3
changed: #reportCompilationError
Stefan Vogel <sv@exept.de>
parents:
2350
diff
changeset
|
772 |
errorMessage:eMsg; |
89458faaeeb3
changed: #reportCompilationError
Stefan Vogel <sv@exept.de>
parents:
2350
diff
changeset
|
773 |
raise. |
89458faaeeb3
changed: #reportCompilationError
Stefan Vogel <sv@exept.de>
parents:
2350
diff
changeset
|
774 |
|
1939 | 775 |
"Created: / 07-11-2006 / 12:29:04 / cg" |
776 |
! |
|
777 |
||
778 |
setupCompilationCommandArguments |
|
2335 | 779 |
|stFn mapFileName libFileName def libDir incDir incDirArg defs incl opts| |
780 |
||
781 |
parserFlags isNil ifTrue:[ parserFlags := ParserFlags new]. |
|
1939 | 782 |
|
1941 | 783 |
stFn := stFileName asFilename. |
784 |
oFileName := stFn nameWithoutSuffix , (ObjectFileLoader objectFileExtension). |
|
785 |
cFileName := (stFn withSuffix:'c') name. |
|
786 |
mapFileName := (stFn withSuffix:'map') name. |
|
787 |
libFileName := (stFn withSuffix:'lib') name. |
|
1939 | 788 |
oFileName asFilename delete. |
789 |
cFileName asFilename delete. |
|
790 |
||
791 |
"/ stcFlags := '-commonSymbols +sharedLibCode +newIncremental -E:errorOutput -N' , initName . |
|
2335 | 792 |
stcFlags := '+newIncremental -E:errorOutput'. |
793 |
initName notNil ifTrue:[ |
|
794 |
stcFlags := stcFlags,' -N' , initName . |
|
795 |
]. |
|
1939 | 796 |
cFlags := OperatingSystem getOSDefine. |
797 |
cFlags isNil ifTrue:[ |
|
798 |
cFlags := '' |
|
799 |
]. |
|
800 |
(def := OperatingSystem getCPUDefine) notNil ifTrue:[ |
|
801 |
cFlags := cFlags , ' ' , def |
|
802 |
]. |
|
803 |
||
2335 | 804 |
(defs := parserFlags stcCompilationDefines) notNil ifTrue:[ |
805 |
cFlags := cFlags , ' ' , defs |
|
1939 | 806 |
]. |
2335 | 807 |
(incl := parserFlags stcCompilationIncludes) notNil ifTrue:[ |
808 |
stcFlags := incl , ' ' , stcFlags. |
|
809 |
cFlags := cFlags , ' ' , incl. |
|
1939 | 810 |
|
811 |
"/ if STX_LIBDIR is defined, and not in passed argument, |
|
812 |
"/ add it here. |
|
813 |
||
814 |
libDir := OperatingSystem getEnvironment:'STX_LIBDIR'. |
|
815 |
(libDir notNil and:[libDir asFilename exists]) ifTrue:[ |
|
816 |
incDir := libDir asFilename construct:'include'. |
|
817 |
incDir exists ifTrue:[ |
|
818 |
incDirArg := '-I' , incDir pathName. |
|
2335 | 819 |
(incl asCollectionOfWords includes:incDirArg) ifFalse:[ |
1939 | 820 |
stcFlags := stcFlags , ' ' , incDirArg. |
821 |
cFlags := cFlags , ' ' , incDirArg. |
|
822 |
] |
|
823 |
] |
|
824 |
]. |
|
825 |
]. |
|
2335 | 826 |
(opts := parserFlags stcCompilationOptions) notNil ifTrue:[ |
827 |
stcFlags := opts , ' ' , stcFlags |
|
1939 | 828 |
]. |
2335 | 829 |
(opts := parserFlags ccCompilationOptions) notNil ifTrue:[ |
830 |
cFlags := cFlags , ' ' , opts |
|
1939 | 831 |
]. |
832 |
||
833 |
"Created: / 07-11-2006 / 12:24:47 / cg" |
|
1941 | 834 |
"Modified: / 07-11-2006 / 13:58:54 / cg" |
1669 | 835 |
! ! |
836 |
||
837 |
!STCCompilerInterface class methodsFor:'documentation'! |
|
838 |
||
839 |
version |
|
2644 | 840 |
^ '$Header: /cvs/stx/stx/libcomp/STCCompilerInterface.st,v 1.20 2011-08-08 21:03:12 cg Exp $' |
2335 | 841 |
! |
842 |
||
843 |
version_CVS |
|
2644 | 844 |
^ '$Header: /cvs/stx/stx/libcomp/STCCompilerInterface.st,v 1.20 2011-08-08 21:03:12 cg Exp $' |
1669 | 845 |
! ! |
846 |
||
847 |
STCCompilerInterface initialize! |