author | Claus Gittinger <cg@exept.de> |
Tue, 09 Aug 2011 23:42:42 +0200 | |
changeset 2658 | 92f1a346dbeb |
parent 2656 | d3f4404f3e23 |
child 2703 | 2e966ec31082 |
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 |
|
2656 | 158 |
|handle oldMethod newMethod ok moduleFileName dllFileName| |
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 |
||
2656 | 218 |
dllFileName := ObjectFileLoader createLoadableObjectFor:(oFileName asFilename withoutSuffix name). |
219 |
dllFileName isNil ifTrue:[ |
|
1669 | 220 |
"/ something went wrong |
221 |
originator parseError:('link error: ' , ObjectFileLoader lastError) position:1. |
|
222 |
^ #CannotLoad |
|
223 |
]. |
|
2656 | 224 |
dllFileName asFilename exists ifFalse:[ |
1669 | 225 |
originator parseError:'link failed - cannot create machine code' position:1. |
226 |
^ #CannotLoad |
|
227 |
]. |
|
228 |
||
1939 | 229 |
oldMethod := classToCompileFor compiledMethodAt:selector. |
230 |
oldMethod notNil ifTrue:[package := oldMethod package]. |
|
1669 | 231 |
|
232 |
" |
|
233 |
load the method objectfile |
|
234 |
" |
|
235 |
originator activityNotification:'loading'. |
|
236 |
||
2656 | 237 |
handle := ObjectFileLoader loadMethodObjectFile:dllFileName. |
1669 | 238 |
handle isNil ifTrue:[ |
2656 | 239 |
OperatingSystem removeFile:dllFileName. |
2185
cb311d08a486
changed #compileToMachineCode:forClass:selector:inCategory:notifying:install:skipIfSame:silent:
Stefan Vogel <sv@exept.de>
parents:
2068
diff
changeset
|
240 |
"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
|
241 |
Parser parseErrorSignal catch:[ |
2185
cb311d08a486
changed #compileToMachineCode:forClass:selector:inCategory:notifying:install:skipIfSame:silent:
Stefan Vogel <sv@exept.de>
parents:
2068
diff
changeset
|
242 |
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
|
243 |
]. |
1669 | 244 |
^ #CannotLoad |
245 |
]. |
|
246 |
||
247 |
" |
|
248 |
did it work ? |
|
249 |
" |
|
1939 | 250 |
newMethod := classToCompileFor compiledMethodAt:selector. |
1669 | 251 |
|
252 |
"/ if install is false, we have to undo the install (which is always done, when loading machine code) |
|
253 |
install ifFalse:[ |
|
254 |
oldMethod isNil ifTrue:[ |
|
1939 | 255 |
classToCompileFor removeSelector:selector |
1669 | 256 |
] ifFalse:[ |
257 |
newMethod setPackage:oldMethod package. |
|
1939 | 258 |
classToCompileFor addSelector:selector withMethod:oldMethod. |
259 |
oldMethod setPackage:package. |
|
1669 | 260 |
] |
261 |
]. |
|
262 |
||
263 |
newMethod notNil ifTrue:[ |
|
264 |
handle method ~~ newMethod ifTrue:[ |
|
265 |
'Compiler [warning]: loaded method installed itself in another class' errorPrintCR. |
|
266 |
]. |
|
267 |
||
268 |
newMethod source:aString string. |
|
1939 | 269 |
newMethod setPackage:package. |
1669 | 270 |
"/ Project notNil ifTrue:[ |
271 |
"/ newMethod package:(Project currentPackageName) |
|
272 |
"/ ]. |
|
273 |
||
1939 | 274 |
"/ classToCompileFor updateRevisionString. |
1669 | 275 |
install ifTrue:[ |
1939 | 276 |
classToCompileFor addChangeRecordForMethod:newMethod fromOld:oldMethod. |
1669 | 277 |
|
278 |
"/ kludge-sigh: must send change messages manually here (stc-loaded code does not do it) |
|
279 |
"/ see addMethod:... in ClassDescription |
|
1939 | 280 |
classToCompileFor changed:#methodDictionary with:(Array with:selector with:oldMethod). |
281 |
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
|
282 |
] ifFalse:[ |
16caba266df4
changed: #compileToMachineCode:forClass:selector:inCategory:notifying:install:skipIfSame:silent:
Stefan Vogel <sv@exept.de>
parents:
2336
diff
changeset
|
283 |
oldMethod := nil. |
1669 | 284 |
]. |
285 |
||
1986 | 286 |
silent ifFalse:[ |
1941 | 287 |
Transcript showCR:(' compiled: ', aClass name,' ',selector,' - machine code') |
1669 | 288 |
]. |
289 |
ObjectMemory flushCaches. |
|
290 |
||
291 |
handle method:newMethod. |
|
292 |
||
2350
16caba266df4
changed: #compileToMachineCode:forClass:selector:inCategory:notifying:install:skipIfSame:silent:
Stefan Vogel <sv@exept.de>
parents:
2336
diff
changeset
|
293 |
"/ 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
|
294 |
"/ corresponding ObjectFileHandle. |
16caba266df4
changed: #compileToMachineCode:forClass:selector:inCategory:notifying:install:skipIfSame:silent:
Stefan Vogel <sv@exept.de>
parents:
2336
diff
changeset
|
295 |
"/ 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
|
296 |
"/ and will be recompiled on an undo |
1669 | 297 |
|
298 |
ObjectFileLoader loadedObjectHandlesDo:[:anotherHandle | |
|
299 |
anotherHandle isMethodHandle ifTrue:[ |
|
2350
16caba266df4
changed: #compileToMachineCode:forClass:selector:inCategory:notifying:install:skipIfSame:silent:
Stefan Vogel <sv@exept.de>
parents:
2336
diff
changeset
|
300 |
anotherHandle method == oldMethod ifTrue:[ |
16caba266df4
changed: #compileToMachineCode:forClass:selector:inCategory:notifying:install:skipIfSame:silent:
Stefan Vogel <sv@exept.de>
parents:
2336
diff
changeset
|
301 |
"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
|
302 |
anotherHandle removeConnectedObjects. |
16caba266df4
changed: #compileToMachineCode:forClass:selector:inCategory:notifying:install:skipIfSame:silent:
Stefan Vogel <sv@exept.de>
parents:
2336
diff
changeset
|
303 |
]. |
16caba266df4
changed: #compileToMachineCode:forClass:selector:inCategory:notifying:install:skipIfSame:silent:
Stefan Vogel <sv@exept.de>
parents:
2336
diff
changeset
|
304 |
anotherHandle isObsolete ifTrue:[ |
16caba266df4
changed: #compileToMachineCode:forClass:selector:inCategory:notifying:install:skipIfSame:silent:
Stefan Vogel <sv@exept.de>
parents:
2336
diff
changeset
|
305 |
anotherHandle unload. |
16caba266df4
changed: #compileToMachineCode:forClass:selector:inCategory:notifying:install:skipIfSame:silent:
Stefan Vogel <sv@exept.de>
parents:
2336
diff
changeset
|
306 |
anotherHandle removeUnusedObjectFile. |
16caba266df4
changed: #compileToMachineCode:forClass:selector:inCategory:notifying:install:skipIfSame:silent:
Stefan Vogel <sv@exept.de>
parents:
2336
diff
changeset
|
307 |
]. |
16caba266df4
changed: #compileToMachineCode:forClass:selector:inCategory:notifying:install:skipIfSame:silent:
Stefan Vogel <sv@exept.de>
parents:
2336
diff
changeset
|
308 |
]. |
1669 | 309 |
]. |
310 |
^ newMethod. |
|
311 |
]. |
|
312 |
||
313 |
OperatingSystem removeFile:moduleFileName. |
|
314 |
originator parseError:'dynamic load failed' position:1. |
|
315 |
^ #CannotLoad |
|
316 |
] ensure:[ |
|
317 |
parserFlags stcKeepSTIntermediate ifFalse:[ |
|
318 |
OperatingSystem removeFile:stFileName. |
|
319 |
OperatingSystem removeFile:'errorOutput'. |
|
320 |
]. |
|
2656 | 321 |
parserFlags stcKeepOIntermediate ~~ true ifTrue:[ |
1669 | 322 |
(oFileName notNil and:[oFileName asFilename exists]) ifTrue:[oFileName asFilename delete]. |
323 |
]. |
|
2656 | 324 |
parserFlags stcKeepCIntermediate ~~ true ifTrue:[ |
1669 | 325 |
(cFileName notNil and:[cFileName asFilename exists]) ifTrue:[cFileName asFilename delete]. |
326 |
]. |
|
327 |
OperatingSystem isMSDOSlike ifTrue:[ |
|
328 |
"/ (mapFileName notNil and:[mapFileName asFilename exists]) ifTrue:[mapFileName asFilename delete]. |
|
329 |
"/ (libFileName notNil and:[libFileName asFilename exists]) ifTrue:[libFileName asFilename delete]. |
|
330 |
]. |
|
331 |
]. |
|
332 |
||
333 |
" |
|
334 |
|m| |
|
335 |
||
336 |
Object subclass:#Test |
|
337 |
instanceVariableNames:'' |
|
338 |
classVariableNames:'' |
|
339 |
poolDictionaries:'' |
|
340 |
category:'tests'. |
|
341 |
m := ByteCodeCompiler |
|
342 |
compile:'foo ^ ''hello''' |
|
343 |
forClass:Test |
|
344 |
inCategory:'tests' |
|
345 |
notifying:nil |
|
346 |
install:false |
|
347 |
skipIfSame:false. |
|
348 |
m inspect |
|
349 |
" |
|
350 |
" |
|
351 |
|m| |
|
352 |
||
353 |
Object subclass:#Test |
|
354 |
instanceVariableNames:'' |
|
355 |
classVariableNames:'' |
|
356 |
poolDictionaries:'' |
|
357 |
category:'tests'. |
|
358 |
m := ByteCodeCompiler |
|
359 |
compileToMachineCode:'foo %{ RETURN (_MKSMALLINT(1)); %}' |
|
360 |
forClass:Test |
|
361 |
inCategory:'tests' |
|
362 |
notifying:nil |
|
363 |
install:false |
|
364 |
skipIfSame:false |
|
365 |
silent:false. |
|
366 |
m inspect |
|
367 |
" |
|
368 |
||
1939 | 369 |
"Modified: / 14-09-1995 / 22:33:04 / claus" |
370 |
"Modified: / 19-03-1999 / 08:31:42 / stefan" |
|
1941 | 371 |
"Modified: / 07-11-2006 / 14:21:46 / cg" |
1939 | 372 |
! ! |
373 |
||
374 |
!STCCompilerInterface methodsFor:'machine code generation-helpers'! |
|
375 |
||
376 |
compileToC |
|
377 |
"compile st to C using stc" |
|
378 |
||
379 |
|command errorStream ok| |
|
380 |
||
2658 | 381 |
command := (self possiblyQuotedPath:stcPath) , ' ' , stcFlags |
382 |
, ' -defdir=', (self possiblyQuotedPath:cFileName asFilename directory pathName), |
|
383 |
' -C ' , (self possiblyQuotedPath:stFileName asFilename pathName). |
|
1939 | 384 |
errorStream := 'errorOutput' asFilename writeStream. |
385 |
||
386 |
Verbose == true ifTrue:[ |
|
387 |
'executing: ' infoPrint. command infoPrintCR. |
|
388 |
]. |
|
389 |
||
390 |
originator activityNotification:'compiling (stc)'. |
|
391 |
ok := OperatingSystem |
|
392 |
executeCommand:command |
|
393 |
inputFrom:nil |
|
394 |
outputTo:errorStream |
|
395 |
errorTo:errorStream |
|
396 |
onError:[:stat| |
|
397 |
executionStatus := stat. |
|
398 |
false |
|
399 |
]. |
|
400 |
||
401 |
errorStream close. |
|
402 |
||
403 |
cFileName asFilename exists ifTrue:[ |
|
404 |
ok ifFalse:[ |
|
405 |
'Compiler [info]: oops - system says stc failed - but c-file is there ...' infoPrintCR. |
|
406 |
ok := true |
|
407 |
] |
|
408 |
] ifFalse:[ |
|
409 |
ok ifTrue:[ |
|
410 |
'Compiler [info]: oops - system says stc ok - but no c-file is there ...' infoPrintCR. |
|
411 |
]. |
|
412 |
ok := false |
|
413 |
]. |
|
414 |
^ ok |
|
415 |
||
416 |
"Created: / 07-11-2006 / 12:11:24 / cg" |
|
2643
60d2bae2be58
comment/format in: #generateSTSource:
Claus Gittinger <cg@exept.de>
parents:
2491
diff
changeset
|
417 |
"Modified: / 08-08-2011 / 22:12:01 / cg" |
1939 | 418 |
! |
419 |
||
2336 | 420 |
compileToExe |
421 |
"compile C to exe, using cc" |
|
422 |
||
423 |
|command errorStream ok| |
|
424 |
||
425 |
errorStream := 'errorOutput' asFilename newReadWriteStream. |
|
426 |
||
2658 | 427 |
command := (self possiblyQuotedPath:ccPath) , ' ' , cFlags , ' -D__INCREMENTAL_COMPILE__ ' , (self possiblyQuotedPath:cFileName). |
2336 | 428 |
|
429 |
Verbose == true ifTrue:[ |
|
430 |
'executing: ' infoPrint. command infoPrintCR. |
|
431 |
]. |
|
432 |
originator activityNotification:'compiling (' , ccPath , ')'. |
|
433 |
ok := OperatingSystem |
|
434 |
executeCommand:command |
|
435 |
inputFrom:nil |
|
436 |
outputTo:errorStream |
|
437 |
errorTo:errorStream |
|
438 |
onError:[:stat| |
|
439 |
executionStatus := stat. |
|
440 |
false |
|
441 |
]. |
|
442 |
||
443 |
ok ifFalse:[ |
|
444 |
errorStream reset. |
|
445 |
errorStream copyToEndInto:Transcript. |
|
446 |
]. |
|
447 |
||
448 |
errorStream close. |
|
449 |
||
450 |
^ ok |
|
451 |
! |
|
452 |
||
1939 | 453 |
compileToObj |
454 |
"compile C to obj, using cc" |
|
455 |
||
2658 | 456 |
|errorStream ok command| |
1939 | 457 |
|
2195
2996fe27c2f8
Add primitive functions when compiling
Stefan Vogel <sv@exept.de>
parents:
2185
diff
changeset
|
458 |
errorStream := 'errorOutput' asFilename newReadWriteStream. |
1939 | 459 |
|
2656 | 460 |
"Note: Windows/bcc32 does not understand a space between -o and filename" |
2658 | 461 |
"/ cg: I guess, this does not work for visual-c |
462 |
command := (self possiblyQuotedPath:ccPath) , ' ' , cFlags , ' -D__INCREMENTAL_COMPILE__ -o', (self possiblyQuotedPath:oFileName), ' -c ' , (self possiblyQuotedPath:cFileName). |
|
1939 | 463 |
|
464 |
Verbose == true ifTrue:[ |
|
465 |
'executing: ' infoPrint. command infoPrintCR. |
|
466 |
]. |
|
467 |
originator activityNotification:'compiling (' , ccPath , ')'. |
|
468 |
ok := OperatingSystem |
|
469 |
executeCommand:command |
|
470 |
inputFrom:nil |
|
471 |
outputTo:errorStream |
|
472 |
errorTo:errorStream |
|
473 |
onError:[:stat| |
|
474 |
executionStatus := stat. |
|
475 |
false |
|
2195
2996fe27c2f8
Add primitive functions when compiling
Stefan Vogel <sv@exept.de>
parents:
2185
diff
changeset
|
476 |
]. |
2996fe27c2f8
Add primitive functions when compiling
Stefan Vogel <sv@exept.de>
parents:
2185
diff
changeset
|
477 |
|
2996fe27c2f8
Add primitive functions when compiling
Stefan Vogel <sv@exept.de>
parents:
2185
diff
changeset
|
478 |
ok ifFalse:[ |
2996fe27c2f8
Add primitive functions when compiling
Stefan Vogel <sv@exept.de>
parents:
2185
diff
changeset
|
479 |
errorStream reset. |
2996fe27c2f8
Add primitive functions when compiling
Stefan Vogel <sv@exept.de>
parents:
2185
diff
changeset
|
480 |
errorStream copyToEndInto:Transcript. |
2996fe27c2f8
Add primitive functions when compiling
Stefan Vogel <sv@exept.de>
parents:
2185
diff
changeset
|
481 |
]. |
1939 | 482 |
|
483 |
errorStream close. |
|
484 |
||
485 |
oFileName asFilename exists ifTrue:[ |
|
486 |
ok ifFalse:[ |
|
487 |
'Compiler [info]: system says compile failed - but o-file is there ...' infoPrintCR. |
|
488 |
ok := true |
|
489 |
] |
|
490 |
] ifFalse:[ |
|
491 |
ok ifTrue:[ |
|
492 |
'Compiler [info]: system says compile ok - but no o-file is there ...' infoPrintCR. |
|
493 |
]. |
|
494 |
ok := false |
|
495 |
]. |
|
496 |
^ ok |
|
497 |
||
498 |
"Created: / 07-11-2006 / 12:14:51 / cg" |
|
499 |
! |
|
500 |
||
2491 | 501 |
compileToS |
502 |
"compile C to assembler, using cc" |
|
503 |
||
504 |
|command errorStream ok| |
|
505 |
||
506 |
errorStream := 'errorOutput' asFilename newReadWriteStream. |
|
507 |
||
2658 | 508 |
command := (self possiblyQuotedPath:ccPath) , ' ' , cFlags , ' -D__INCREMENTAL_COMPILE__ -S ' , (self possiblyQuotedPath:cFileName). |
2491 | 509 |
|
510 |
Verbose == true ifTrue:[ |
|
511 |
'executing: ' infoPrint. command infoPrintCR. |
|
512 |
]. |
|
513 |
originator activityNotification:'compiling (' , ccPath , ')'. |
|
514 |
ok := OperatingSystem |
|
515 |
executeCommand:command |
|
516 |
inputFrom:nil |
|
517 |
outputTo:errorStream |
|
518 |
errorTo:errorStream |
|
519 |
onError:[:stat| |
|
520 |
executionStatus := stat. |
|
521 |
false |
|
522 |
]. |
|
523 |
||
524 |
ok ifFalse:[ |
|
525 |
errorStream reset. |
|
526 |
errorStream copyToEndInto:Transcript. |
|
527 |
]. |
|
528 |
||
529 |
errorStream close. |
|
530 |
^ ok |
|
531 |
! |
|
532 |
||
1939 | 533 |
ensureExternalToolsArePresent |
534 |
(stcPath := self incrementalStcPath) isNil ifTrue:[ |
|
535 |
originator parseError:'no stc compiler available - cannot create machine code' position:1. |
|
536 |
^ false |
|
537 |
]. |
|
2649 | 538 |
|
539 |
"make it absolute, so that we are immune to directory changes" |
|
540 |
stcPath := stcPath asFilename pathName. |
|
1939 | 541 |
(ccPath := parserFlags ccPath) isNil ifTrue:[ |
542 |
originator parseError:'no cc compiler available - cannot create machine code' position:1. |
|
543 |
^ false |
|
544 |
]. |
|
2649 | 545 |
"make it absolute, so that we are immune to directory changes" |
546 |
ccPath := ccPath asFilename pathName. |
|
1939 | 547 |
|
548 |
(ObjectFileLoader notNil and:[ObjectFileLoader canLoadObjectFiles]) ifFalse:[ |
|
549 |
originator parseError:'no dynamic loader configured - cannot create machine code' position:1. |
|
550 |
^ false |
|
551 |
]. |
|
552 |
^ true |
|
553 |
||
554 |
"Created: / 07-11-2006 / 12:31:48 / cg" |
|
1670 | 555 |
! |
556 |
||
557 |
ensureModuleDirectoryExists |
|
558 |
|mP t s| |
|
559 |
||
560 |
(mP := parserFlags stcModulePath asFilename) exists ifFalse:[ |
|
561 |
mP makeDirectory |
|
562 |
]. |
|
563 |
(mP isDirectory and:[ mP isReadable and:[ mP isWritable ] ]) ifFalse:[ |
|
564 |
Parser::ParseError raiseErrorString:('No access to temporary module directory: ' , mP pathName). |
|
565 |
]. |
|
566 |
"/ create a small README there ... |
|
567 |
||
568 |
(t := mP construct:'README') exists ifFalse:[ |
|
569 |
s := t writeStream. |
|
570 |
s |
|
571 |
nextPutAll:'This temporary ST/X directory contains machine code for |
|
572 |
accepted methods with embedded C-code |
|
573 |
(i.e. dynamic compiled code for inline-C methods). |
|
574 |
||
575 |
Files here are not automatically removed, since ST/X |
|
576 |
cannot determine if any (other) snapshot image still |
|
577 |
requires a file here. |
|
578 |
||
579 |
Please be careful when removing files here - a snapshot |
|
580 |
image which was saved with accepted embedded C-code |
|
581 |
may not be able to restart correctly if you remove a |
|
582 |
required file. |
|
583 |
Also, when you export a snapshot image for execution |
|
584 |
on another machine, make certain that the required |
|
585 |
module-files are also present there. |
|
586 |
||
587 |
You should periodically clean dead entries here. |
|
588 |
i.e. remove files, when you are certain that none |
|
589 |
of your snapshot images refers to any module here. |
|
590 |
||
591 |
See the launchers File-Modules dialog for a list of |
|
592 |
modules which are still required by your running image. |
|
593 |
||
594 |
With kind regards - your ST/X. |
|
595 |
'. |
|
596 |
s close. |
|
597 |
]. |
|
598 |
! |
|
599 |
||
600 |
ensureSuperClassesAreLoadedOf:aClass |
|
601 |
|supers| |
|
602 |
||
603 |
supers := aClass allSuperclasses. |
|
604 |
supers reverseDo:[:cls| |
|
605 |
cls isLoaded ifFalse:[ |
|
606 |
Parser::ParseError raiseErrorString:'Cannot stc-compile (Some superclass is unloaded)'. |
|
607 |
] |
|
608 |
]. |
|
609 |
! |
|
610 |
||
611 |
fileOutAllDefinitionsOf:aClass to:aStream rememberIn:definedClasses |
|
612 |
|defineAction| |
|
613 |
||
614 |
defineAction := |
|
615 |
[:cls| |
|
616 |
(definedClasses includes:cls) ifFalse:[ |
|
617 |
cls |
|
618 |
basicFileOutDefinitionOn:aStream |
|
619 |
withNameSpace:false withPackage:false |
|
620 |
syntaxHilighting:false. |
|
621 |
||
622 |
aStream nextPut:(aStream class chunkSeparator); cr. |
|
623 |
definedClasses add:cls. |
|
624 |
]. |
|
625 |
]. |
|
626 |
||
627 |
aClass allSuperclasses reverseDo:defineAction. |
|
628 |
defineAction value:aClass. |
|
1939 | 629 |
! |
630 |
||
631 |
generateSTSource:aString |
|
2647 | 632 |
|stream definedClasses sep className modulesDir| |
1939 | 633 |
|
634 |
"/ generate a unique name, consisting of my processID and a sequence number |
|
635 |
"/ the processId is added to allow filein of modules from different |
|
636 |
"/ lifes |
|
637 |
||
638 |
SequenceNumber := (SequenceNumber ? 0) + 1. |
|
639 |
||
640 |
initName := 'm_' , OperatingSystem getProcessId printString, '_' , SequenceNumber printString. |
|
641 |
||
2646 | 642 |
"/ Smalltalk isSmalltalkDevelopmentSystem ifTrue:[ |
643 |
"/ modulesParentDir := Filename currentDirectory. |
|
644 |
"/ ] ifFalse:[ |
|
645 |
"/ modulesParentDir := Filename tempDirectory. |
|
646 |
"/ ]. |
|
647 |
"/ modulesDir := modulesParentDir construct:'modules'. |
|
648 |
modulesDir := ParserFlags stcModulePath. |
|
2647 | 649 |
stFileName := (modulesDir asFilename construct:(initName , '.st')) name. |
2646 | 650 |
|
1939 | 651 |
[ |
652 |
stream := stFileName asFilename writeStream. |
|
653 |
] on:FileStream openErrorSignal do:[:ex| |
|
654 |
originator parseError:'cannot create temporary sourcefile for compilation'. |
|
655 |
^ #CannotLoad |
|
656 |
]. |
|
657 |
||
658 |
definedClasses := IdentitySet new. |
|
659 |
||
660 |
sep := stream class chunkSeparator. |
|
661 |
||
662 |
Class fileOutNameSpaceQuerySignal answer:true |
|
663 |
do:[ |
|
2467 | 664 |
theNonMetaclassToCompileFor realSharedPoolNames do:[:eachPoolname | |
2067
6204fefb44a3
must also file-out the definition of sharedPools
Claus Gittinger <cg@exept.de>
parents:
2026
diff
changeset
|
665 |
|pool| |
6204fefb44a3
must also file-out the definition of sharedPools
Claus Gittinger <cg@exept.de>
parents:
2026
diff
changeset
|
666 |
|
6204fefb44a3
must also file-out the definition of sharedPools
Claus Gittinger <cg@exept.de>
parents:
2026
diff
changeset
|
667 |
pool := Smalltalk at:eachPoolname. |
6204fefb44a3
must also file-out the definition of sharedPools
Claus Gittinger <cg@exept.de>
parents:
2026
diff
changeset
|
668 |
self |
6204fefb44a3
must also file-out the definition of sharedPools
Claus Gittinger <cg@exept.de>
parents:
2026
diff
changeset
|
669 |
fileOutAllDefinitionsOf:pool |
6204fefb44a3
must also file-out the definition of sharedPools
Claus Gittinger <cg@exept.de>
parents:
2026
diff
changeset
|
670 |
to:stream |
6204fefb44a3
must also file-out the definition of sharedPools
Claus Gittinger <cg@exept.de>
parents:
2026
diff
changeset
|
671 |
rememberIn:definedClasses. |
6204fefb44a3
must also file-out the definition of sharedPools
Claus Gittinger <cg@exept.de>
parents:
2026
diff
changeset
|
672 |
]. |
6204fefb44a3
must also file-out the definition of sharedPools
Claus Gittinger <cg@exept.de>
parents:
2026
diff
changeset
|
673 |
|
1939 | 674 |
self |
675 |
fileOutAllDefinitionsOf:theNonMetaclassToCompileFor |
|
676 |
to:stream |
|
677 |
rememberIn:definedClasses. |
|
678 |
||
679 |
theNonMetaclassToCompileFor privateClassesSorted do:[:aPrivateClass | |
|
680 |
self |
|
681 |
fileOutAllDefinitionsOf:aPrivateClass |
|
682 |
to:stream |
|
683 |
rememberIn:definedClasses. |
|
684 |
]. |
|
2195
2996fe27c2f8
Add primitive functions when compiling
Stefan Vogel <sv@exept.de>
parents:
2185
diff
changeset
|
685 |
"/ theNonMetaclassToCompileFor fileOutPrimitiveDefinitionsOn:stream. |
2026 | 686 |
"/ 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
|
687 |
"/ 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
|
688 |
theNonMetaclassToCompileFor fileOutPrimitiveSpecsOn:stream. |
1939 | 689 |
]. |
690 |
||
2026 | 691 |
"/ stream cr. |
692 |
"/ stream nextPutLine:'"{ Package: ''' , package , ''' }"'. |
|
693 |
"/ stream cr. |
|
1939 | 694 |
|
695 |
stream nextPut:sep. |
|
696 |
className := theNonMetaclassToCompileFor name. |
|
697 |
||
698 |
stream nextPutAll:className. |
|
699 |
classToCompileFor isMeta ifTrue:[ |
|
700 |
stream nextPutAll:' class'. |
|
701 |
]. |
|
702 |
stream nextPutAll:' methodsFor:'''; nextPutAll:methodCategory; nextPutAll:''''. |
|
703 |
stream nextPut:sep; cr. |
|
704 |
||
705 |
stream nextPutLine:'"{ Line: 0 }"'; |
|
706 |
nextChunkPut:aString; |
|
707 |
space; nextPut:sep. |
|
708 |
||
709 |
stream close. |
|
710 |
||
2646 | 711 |
"Modified: / 08-08-2011 / 23:23:10 / cg" |
1939 | 712 |
! |
713 |
||
2658 | 714 |
possiblyQuotedPath:aPath |
715 |
(aPath includes:$ ) ifTrue:[ |
|
716 |
(aPath startsWith:'"') ifFalse:[ |
|
717 |
^ '"',aPath,'"' |
|
718 |
] |
|
719 |
]. |
|
720 |
^ aPath |
|
721 |
! |
|
722 |
||
1939 | 723 |
reportCompilationError |
724 |
|eMsg errorMessages lNr| |
|
725 |
||
726 |
(executionStatus notNil and:[executionStatus couldNotExecute]) ifTrue:[ |
|
727 |
eMsg := 'oops, no STC - cannot create machine code' |
|
728 |
] ifFalse:[ |
|
729 |
errorMessages := 'errorOutput' asFilename contents. |
|
730 |
OperatingSystem removeFile:'errorOutput'. |
|
731 |
||
732 |
errorMessages notNil ifTrue:[ |
|
733 |
errorMessages := errorMessages reject:[:line | line includesString:'Warning:']. |
|
734 |
||
735 |
errorMessages size > 20 ifTrue:[ |
|
736 |
errorMessages := errorMessages select:[:line | line asLowercase startsWith:'error']. |
|
737 |
errorMessages size > 20 ifTrue:[ |
|
738 |
errorMessages := (errorMessages copyTo:20) copyWith:'... more messages skipped' |
|
739 |
]. |
|
740 |
]. |
|
741 |
"/ errorMessages := errorMessages collect:[:line | |
|
742 |
"/ (line startsWith:(stFileName , ':')) ifTrue:[ |
|
743 |
"/ 'Line: ' , (line copyFrom:(stFileName size + 2)) |
|
744 |
"/ ] ifFalse:[ |
|
745 |
"/ line |
|
746 |
"/ ] |
|
747 |
"/ ]. |
|
748 |
]. |
|
749 |
errorMessages isNil ifTrue:[ |
|
750 |
errorMessages := #('') |
|
751 |
]. |
|
752 |
||
753 |
"/ try to extract a line number" |
|
754 |
(errorMessages contains:[:line | line includesString:'Borland']) ifTrue:[ |
|
755 |
|i i2 s| |
|
756 |
i := errorMessages findFirst:[:l | l startsWith:(cFileName,':')]. |
|
757 |
i ~~ 0 ifTrue:[ |
|
758 |
((errorMessages at:i+1) startsWith:'Error') ifTrue:[ |
|
759 |
i2 := (errorMessages at:i+1) indexOfSubCollection:(stFileName). |
|
760 |
i2 ~~ 0 ifTrue:[ |
|
761 |
s := (errorMessages at:i+1) copyFrom:(i2+stFileName size+1). |
|
762 |
s := s readStream. |
|
763 |
lNr := Integer readFrom:s. |
|
764 |
s skipSeparators. |
|
765 |
]. |
|
766 |
] |
|
767 |
]. |
|
768 |
]. |
|
769 |
||
770 |
errorMessages isEmpty ifTrue:[ |
|
771 |
eMsg := 'Error during compilation:\\Unspecified error (no output)' withCRs |
|
772 |
] ifFalse:[ |
|
773 |
eMsg := 'Error during compilation:\\' withCRs , |
|
774 |
(errorMessages asStringCollection asString). |
|
775 |
]. |
|
776 |
]. |
|
2461
89458faaeeb3
changed: #reportCompilationError
Stefan Vogel <sv@exept.de>
parents:
2350
diff
changeset
|
777 |
|
1939 | 778 |
originator activityNotification:''. |
779 |
||
2461
89458faaeeb3
changed: #reportCompilationError
Stefan Vogel <sv@exept.de>
parents:
2350
diff
changeset
|
780 |
Parser::ParseError new |
89458faaeeb3
changed: #reportCompilationError
Stefan Vogel <sv@exept.de>
parents:
2350
diff
changeset
|
781 |
lineNumber:lNr; |
89458faaeeb3
changed: #reportCompilationError
Stefan Vogel <sv@exept.de>
parents:
2350
diff
changeset
|
782 |
errorMessage:eMsg; |
89458faaeeb3
changed: #reportCompilationError
Stefan Vogel <sv@exept.de>
parents:
2350
diff
changeset
|
783 |
raise. |
89458faaeeb3
changed: #reportCompilationError
Stefan Vogel <sv@exept.de>
parents:
2350
diff
changeset
|
784 |
|
1939 | 785 |
"Created: / 07-11-2006 / 12:29:04 / cg" |
786 |
! |
|
787 |
||
788 |
setupCompilationCommandArguments |
|
2335 | 789 |
|stFn mapFileName libFileName def libDir incDir incDirArg defs incl opts| |
790 |
||
791 |
parserFlags isNil ifTrue:[ parserFlags := ParserFlags new]. |
|
1939 | 792 |
|
1941 | 793 |
stFn := stFileName asFilename. |
794 |
oFileName := stFn nameWithoutSuffix , (ObjectFileLoader objectFileExtension). |
|
795 |
cFileName := (stFn withSuffix:'c') name. |
|
796 |
mapFileName := (stFn withSuffix:'map') name. |
|
797 |
libFileName := (stFn withSuffix:'lib') name. |
|
1939 | 798 |
oFileName asFilename delete. |
799 |
cFileName asFilename delete. |
|
800 |
||
801 |
"/ stcFlags := '-commonSymbols +sharedLibCode +newIncremental -E:errorOutput -N' , initName . |
|
2335 | 802 |
stcFlags := '+newIncremental -E:errorOutput'. |
803 |
initName notNil ifTrue:[ |
|
804 |
stcFlags := stcFlags,' -N' , initName . |
|
805 |
]. |
|
1939 | 806 |
cFlags := OperatingSystem getOSDefine. |
807 |
cFlags isNil ifTrue:[ |
|
808 |
cFlags := '' |
|
809 |
]. |
|
810 |
(def := OperatingSystem getCPUDefine) notNil ifTrue:[ |
|
811 |
cFlags := cFlags , ' ' , def |
|
812 |
]. |
|
813 |
||
2335 | 814 |
(defs := parserFlags stcCompilationDefines) notNil ifTrue:[ |
815 |
cFlags := cFlags , ' ' , defs |
|
1939 | 816 |
]. |
2335 | 817 |
(incl := parserFlags stcCompilationIncludes) notNil ifTrue:[ |
818 |
stcFlags := incl , ' ' , stcFlags. |
|
819 |
cFlags := cFlags , ' ' , incl. |
|
1939 | 820 |
|
821 |
"/ if STX_LIBDIR is defined, and not in passed argument, |
|
822 |
"/ add it here. |
|
823 |
||
824 |
libDir := OperatingSystem getEnvironment:'STX_LIBDIR'. |
|
825 |
(libDir notNil and:[libDir asFilename exists]) ifTrue:[ |
|
826 |
incDir := libDir asFilename construct:'include'. |
|
827 |
incDir exists ifTrue:[ |
|
828 |
incDirArg := '-I' , incDir pathName. |
|
2335 | 829 |
(incl asCollectionOfWords includes:incDirArg) ifFalse:[ |
1939 | 830 |
stcFlags := stcFlags , ' ' , incDirArg. |
831 |
cFlags := cFlags , ' ' , incDirArg. |
|
832 |
] |
|
833 |
] |
|
834 |
]. |
|
835 |
]. |
|
2335 | 836 |
(opts := parserFlags stcCompilationOptions) notNil ifTrue:[ |
837 |
stcFlags := opts , ' ' , stcFlags |
|
1939 | 838 |
]. |
2335 | 839 |
(opts := parserFlags ccCompilationOptions) notNil ifTrue:[ |
840 |
cFlags := cFlags , ' ' , opts |
|
1939 | 841 |
]. |
842 |
||
843 |
"Created: / 07-11-2006 / 12:24:47 / cg" |
|
1941 | 844 |
"Modified: / 07-11-2006 / 13:58:54 / cg" |
1669 | 845 |
! ! |
846 |
||
847 |
!STCCompilerInterface class methodsFor:'documentation'! |
|
848 |
||
849 |
version |
|
2658 | 850 |
^ '$Header: /cvs/stx/stx/libcomp/STCCompilerInterface.st,v 1.25 2011-08-09 21:42:42 cg Exp $' |
2335 | 851 |
! |
852 |
||
853 |
version_CVS |
|
2658 | 854 |
^ '$Header: /cvs/stx/stx/libcomp/STCCompilerInterface.st,v 1.25 2011-08-09 21:42:42 cg Exp $' |
1669 | 855 |
! ! |
856 |
||
857 |
STCCompilerInterface initialize! |