comphook.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Peter Vreman
  4. This unit handles the compilerhooks for output to external programs
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit comphook;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. {$IFNDEF MACOS_USE_FAKE_SYSUTILS}
  23. SysUtils,
  24. {$ELSE}
  25. globals,
  26. {$ENDIF}
  27. finput;
  28. Const
  29. { Levels }
  30. V_None = $0;
  31. V_Fatal = $1;
  32. V_Error = $2;
  33. V_Normal = $4; { doesn't show a text like Error: }
  34. V_Warning = $8;
  35. V_Note = $10;
  36. V_Hint = $20;
  37. V_LineInfoMask = $fff;
  38. { From here by default no line info }
  39. V_Info = $1000;
  40. V_Status = $2000;
  41. V_Used = $4000;
  42. V_Tried = $8000;
  43. V_Conditional = $10000;
  44. V_Debug = $20000;
  45. V_Executable = $40000;
  46. V_LevelMask = $fffffff;
  47. V_All = V_LevelMask;
  48. V_Default = V_Fatal + V_Error + V_Normal;
  49. { Flags }
  50. V_LineInfo = $10000000;
  51. const
  52. { RHIDE expect gcc like error output }
  53. fatalstr : string[20] = 'Fatal:';
  54. errorstr : string[20] = 'Error:';
  55. warningstr : string[20] = 'Warning:';
  56. notestr : string[20] = 'Note:';
  57. hintstr : string[20] = 'Hint:';
  58. type
  59. PCompilerStatus = ^TCompilerStatus;
  60. TCompilerStatus = record
  61. { Current status }
  62. currentmodule,
  63. currentsourcepath,
  64. currentsource : string; { filename }
  65. currentline,
  66. currentcolumn : longint; { current line and column }
  67. { Total Status }
  68. compiledlines : longint; { the number of lines which are compiled }
  69. errorcount : longint; { number of generated errors }
  70. { program info }
  71. isexe,
  72. islibrary : boolean;
  73. { Settings for the output }
  74. verbosity : longint;
  75. maxerrorcount : longint;
  76. errorwarning,
  77. errornote,
  78. errorhint,
  79. skip_error,
  80. use_stderr,
  81. use_redir,
  82. use_bugreport,
  83. use_gccoutput,
  84. compiling_current : boolean;
  85. { Redirection support }
  86. redirfile : text;
  87. { Special file for bug report }
  88. reportbugfile : text;
  89. end;
  90. var
  91. status : tcompilerstatus;
  92. type
  93. EControlCAbort=class(Exception)
  94. constructor Create;
  95. end;
  96. ECompilerAbort=class(Exception)
  97. constructor Create;
  98. end;
  99. ECompilerAbortSilent=class(Exception)
  100. constructor Create;
  101. end;
  102. { Default Functions }
  103. Function def_status:boolean;
  104. Function def_comment(Level:Longint;const s:string):boolean;
  105. function def_internalerror(i:longint):boolean;
  106. procedure def_initsymbolinfo;
  107. procedure def_donesymbolinfo;
  108. procedure def_extractsymbolinfo;
  109. function def_openinputfile(const filename: string): tinputfile;
  110. Function def_getnamedfiletime(Const F : String) : Longint;
  111. { Function redirecting for IDE support }
  112. type
  113. tstopprocedure = procedure(err:longint);
  114. tstatusfunction = function:boolean;
  115. tcommentfunction = function(Level:Longint;const s:string):boolean;
  116. tinternalerrorfunction = function(i:longint):boolean;
  117. tinitsymbolinfoproc = procedure;
  118. tdonesymbolinfoproc = procedure;
  119. textractsymbolinfoproc = procedure;
  120. topeninputfilefunc = function(const filename: string): tinputfile;
  121. tgetnamedfiletimefunc = function(const filename: string): longint;
  122. const
  123. do_status : tstatusfunction = @def_status;
  124. do_comment : tcommentfunction = @def_comment;
  125. do_internalerror : tinternalerrorfunction = @def_internalerror;
  126. do_initsymbolinfo : tinitsymbolinfoproc = @def_initsymbolinfo;
  127. do_donesymbolinfo : tdonesymbolinfoproc = @def_donesymbolinfo;
  128. do_extractsymbolinfo : textractsymbolinfoproc = @def_extractsymbolinfo;
  129. do_openinputfile : topeninputfilefunc = @def_openinputfile;
  130. do_getnamedfiletime : tgetnamedfiletimefunc = @def_getnamedfiletime;
  131. implementation
  132. uses
  133. {$IFDEF USE_SYSUTILS}
  134. SysUtils,
  135. {$ELSE USE_SYSUTILS}
  136. dos,
  137. {$ENDIF USE_SYSUTILS}
  138. cutils
  139. ;
  140. {****************************************************************************
  141. Helper Routines
  142. ****************************************************************************}
  143. function gccfilename(const s : string) : string;
  144. var
  145. i : longint;
  146. begin
  147. for i:=1to length(s) do
  148. begin
  149. case s[i] of
  150. '\' : gccfilename[i]:='/';
  151. 'A'..'Z' : gccfilename[i]:=chr(ord(s[i])+32);
  152. else
  153. gccfilename[i]:=s[i];
  154. end;
  155. end;
  156. gccfilename[0]:=s[0];
  157. end;
  158. function tostr(i : longint) : string;
  159. var
  160. hs : string;
  161. begin
  162. str(i,hs);
  163. tostr:=hs;
  164. end;
  165. {****************************************************************************
  166. Stopping the compiler
  167. ****************************************************************************}
  168. constructor EControlCAbort.Create;
  169. begin
  170. {$IFNDEF MACOS_USE_FAKE_SYSUTILS}
  171. inherited Create('Ctrl-C Signaled!');
  172. {$ELSE}
  173. inherited Create;
  174. {$ENDIF}
  175. end;
  176. constructor ECompilerAbort.Create;
  177. begin
  178. {$IFNDEF MACOS_USE_FAKE_SYSUTILS}
  179. inherited Create('Compilation Aborted');
  180. {$ELSE}
  181. inherited Create;
  182. {$ENDIF}
  183. end;
  184. constructor ECompilerAbortSilent.Create;
  185. begin
  186. {$IFNDEF MACOS_USE_FAKE_SYSUTILS}
  187. inherited Create('Compilation Aborted');
  188. {$ELSE}
  189. inherited Create;
  190. {$ENDIF}
  191. end;
  192. {****************************************************************************
  193. Predefined default Handlers
  194. ****************************************************************************}
  195. function def_status:boolean;
  196. {$ifdef HASGETHEAPSTATUS}
  197. var
  198. hstatus : TFPCHeapStatus;
  199. {$endif HASGETHEAPSTATUS}
  200. begin
  201. def_status:=false; { never stop }
  202. { Status info?, Called every line }
  203. if ((status.verbosity and V_Status)<>0) then
  204. begin
  205. if (status.compiledlines=1) or
  206. (status.currentline mod 100=0) then
  207. begin
  208. if status.currentline>0 then
  209. Write(status.currentline,' ');
  210. {$ifdef HASGETHEAPSTATUS}
  211. hstatus:=GetFPCHeapStatus;
  212. WriteLn(DStr(hstatus.CurrHeapUsed shr 10),'/',DStr(hstatus.CurrHeapSize shr 10),' Kb Used');
  213. {$else HASGETHEAPSTATUS}
  214. WriteLn(DStr(memavail shr 10),'/',DStr(system.heapsize shr 10),' Kb Free');
  215. {$endif HASGETHEAPSTATUS}
  216. end;
  217. end;
  218. {$ifdef macos}
  219. Yield;
  220. {$endif}
  221. end;
  222. Function def_comment(Level:Longint;const s:string):boolean;
  223. const
  224. rh_errorstr = 'error:';
  225. rh_warningstr = 'warning:';
  226. var
  227. hs : string;
  228. begin
  229. def_comment:=false; { never stop }
  230. hs:='';
  231. if not(status.use_gccoutput) then
  232. begin
  233. if (status.verbosity and Level)=V_Hint then
  234. hs:=hintstr;
  235. if (status.verbosity and Level)=V_Note then
  236. hs:=notestr;
  237. if (status.verbosity and Level)=V_Warning then
  238. hs:=warningstr;
  239. if (status.verbosity and Level)=V_Error then
  240. hs:=errorstr;
  241. if (status.verbosity and Level)=V_Fatal then
  242. hs:=fatalstr;
  243. if (status.verbosity and Level)=V_Used then
  244. hs:=PadSpace('('+status.currentmodule+')',10);
  245. end
  246. else
  247. begin
  248. if (status.verbosity and Level)=V_Hint then
  249. hs:=rh_warningstr;
  250. if (status.verbosity and Level)=V_Note then
  251. hs:=rh_warningstr;
  252. if (status.verbosity and Level)=V_Warning then
  253. hs:=rh_warningstr;
  254. if (status.verbosity and Level)=V_Error then
  255. hs:=rh_errorstr;
  256. if (status.verbosity and Level)=V_Fatal then
  257. hs:=rh_errorstr;
  258. end;
  259. { Generate line prefix }
  260. if ((Level and V_LineInfo)=V_LineInfo) and
  261. (status.currentsource<>'') and
  262. (status.currentline>0) then
  263. begin
  264. {$ifndef macos}
  265. { Adding the column should not confuse RHIDE,
  266. even if it does not yet use it PM
  267. but only if it is after error or warning !! PM }
  268. if status.currentcolumn>0 then
  269. begin
  270. if status.use_gccoutput then
  271. hs:=gccfilename(status.currentsource)+':'+tostr(status.currentline)+': '+hs+' '+
  272. tostr(status.currentcolumn)+': '+s
  273. else
  274. hs:=status.currentsource+'('+tostr(status.currentline)+
  275. ','+tostr(status.currentcolumn)+') '+hs+' '+s;
  276. end
  277. else
  278. begin
  279. if status.use_gccoutput then
  280. hs:=gccfilename(status.currentsource)+': '+hs+' '+tostr(status.currentline)+': '+s
  281. else
  282. hs:=status.currentsource+'('+tostr(status.currentline)+') '+hs+' '+s;
  283. end;
  284. {$else}
  285. {MPW style error}
  286. if status.currentcolumn>0 then
  287. hs:='File "'+status.currentsourcepath+status.currentsource+'"; Line '+tostr(status.currentline)+
  288. ' #[' + tostr(status.currentcolumn) + '] ' +hs+' '+s
  289. else
  290. hs:='File "'+status.currentsourcepath+status.currentsource+'"; Line '+tostr(status.currentline)+' # '+hs+' '+s;
  291. {$endif}
  292. end
  293. else
  294. begin
  295. if hs<>'' then
  296. hs:=hs+' '+s
  297. else
  298. hs:=s;
  299. end;
  300. { Display line }
  301. if ((status.verbosity and (Level and V_LevelMask))=(Level and V_LevelMask)) then
  302. begin
  303. {$ifdef FPC}
  304. if status.use_stderr then
  305. begin
  306. writeln(stderr,hs);
  307. flush(stderr);
  308. end
  309. else
  310. {$endif}
  311. begin
  312. if status.use_redir then
  313. writeln(status.redirfile,hs)
  314. else
  315. writeln(hs);
  316. end;
  317. end;
  318. { include everything in the bugreport file }
  319. if status.use_bugreport then
  320. begin
  321. {$ifdef FPC}
  322. Write(status.reportbugfile,hexstr(level,8)+':');
  323. Writeln(status.reportbugfile,hs);
  324. {$endif}
  325. end;
  326. end;
  327. function def_internalerror(i : longint) : boolean;
  328. begin
  329. do_comment(V_Fatal+V_LineInfo,'Internal error '+tostr(i));
  330. {$ifdef EXTDEBUG}
  331. {$ifdef FPC}
  332. { Internalerror() and def_internalerror() do not
  333. have a stackframe }
  334. dump_stack(stdout,get_caller_frame(get_frame));
  335. {$endif FPC}
  336. {$endif EXTDEBUG}
  337. def_internalerror:=true;
  338. end;
  339. procedure def_initsymbolinfo;
  340. begin
  341. end;
  342. procedure def_donesymbolinfo;
  343. begin
  344. end;
  345. procedure def_extractsymbolinfo;
  346. begin
  347. end;
  348. function def_openinputfile(const filename: string): tinputfile;
  349. begin
  350. def_openinputfile:=tdosinputfile.create(filename);
  351. end;
  352. Function def_GetNamedFileTime (Const F : String) : Longint;
  353. var
  354. {$IFDEF USE_SYSUTILS}
  355. fh : THandle;
  356. {$ELSE USE_SYSUTILS}
  357. info : SearchRec;
  358. {$ENDIF USE_SYSUTILS}
  359. begin
  360. Result := -1;
  361. {$IFDEF USE_SYSUTILS}
  362. fh := FileOpen(f, faArchive+faReadOnly+faHidden);
  363. Result := FileGetDate(fh);
  364. FileClose(fh);
  365. {$ELSE USE_SYSUTILS}
  366. FindFirst (F,archive+readonly+hidden,info);
  367. if DosError=0 then
  368. Result := info.time;
  369. FindClose(info);
  370. {$ENDIF USE_SYSUTILS}
  371. end;
  372. end.
  373. {
  374. $Log$
  375. Revision 1.39 2005-04-28 19:27:12 olle
  376. * Made compile on macos
  377. Revision 1.38 2005/04/24 21:01:37 peter
  378. * always use exceptions to stop the compiler
  379. - remove stop, do_stop
  380. Revision 1.37 2005/02/28 15:38:38 marco
  381. * getFPCheapstatus (no, FPC HEAP, not FP CHEAP!)
  382. Revision 1.36 2005/02/14 17:13:06 peter
  383. * truncate log
  384. Revision 1.35 2005/01/24 18:12:17 olle
  385. * In MPW, whole path to source file is now displayed in messages.
  386. }