comphook.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. finput;
  23. Const
  24. { Levels }
  25. V_None = $0;
  26. V_Fatal = $1;
  27. V_Error = $2;
  28. V_Normal = $4; { doesn't show a text like Error: }
  29. V_Warning = $8;
  30. V_Note = $10;
  31. V_Hint = $20;
  32. V_LineInfoMask = $fff;
  33. { From here by default no line info }
  34. V_Info = $1000;
  35. V_Status = $2000;
  36. V_Used = $4000;
  37. V_Tried = $8000;
  38. V_Conditional = $10000;
  39. V_Debug = $20000;
  40. V_Executable = $40000;
  41. V_LevelMask = $fffffff;
  42. V_All = V_LevelMask;
  43. V_Default = V_Fatal + V_Error + V_Normal;
  44. { Flags }
  45. V_LineInfo = $10000000;
  46. const
  47. { RHIDE expect gcc like error output }
  48. fatalstr : string[20] = 'Fatal:';
  49. errorstr : string[20] = 'Error:';
  50. warningstr : string[20] = 'Warning:';
  51. notestr : string[20] = 'Note:';
  52. hintstr : string[20] = 'Hint:';
  53. type
  54. PCompilerStatus = ^TCompilerStatus;
  55. TCompilerStatus = record
  56. { Current status }
  57. currentmodule,
  58. currentsourcepath,
  59. currentsource : string; { filename }
  60. currentline,
  61. currentcolumn : longint; { current line and column }
  62. { Total Status }
  63. compiledlines : longint; { the number of lines which are compiled }
  64. errorcount : longint; { number of generated errors }
  65. { program info }
  66. isexe,
  67. islibrary : boolean;
  68. { Settings for the output }
  69. verbosity : longint;
  70. maxerrorcount : longint;
  71. errorwarning,
  72. errornote,
  73. errorhint,
  74. skip_error,
  75. use_stderr,
  76. use_redir,
  77. use_bugreport,
  78. use_gccoutput,
  79. compiling_current : boolean;
  80. { Redirection support }
  81. redirfile : text;
  82. { Special file for bug report }
  83. reportbugfile : text;
  84. end;
  85. var
  86. status : tcompilerstatus;
  87. { Default Functions }
  88. procedure def_stop(err:longint);
  89. Function def_status:boolean;
  90. Function def_comment(Level:Longint;const s:string):boolean;
  91. function def_internalerror(i:longint):boolean;
  92. procedure def_initsymbolinfo;
  93. procedure def_donesymbolinfo;
  94. procedure def_extractsymbolinfo;
  95. function def_openinputfile(const filename: string): tinputfile;
  96. Function def_getnamedfiletime(Const F : String) : Longint;
  97. {$ifdef DEBUG}
  98. { allow easy stopping in GDB
  99. using
  100. b DEF_GDB_STOP
  101. cond 1 LEVEL <= 8 }
  102. procedure def_gdb_stop(level : longint);
  103. {$endif DEBUG}
  104. { Function redirecting for IDE support }
  105. type
  106. tstopprocedure = procedure(err:longint);
  107. tstatusfunction = function:boolean;
  108. tcommentfunction = function(Level:Longint;const s:string):boolean;
  109. tinternalerrorfunction = function(i:longint):boolean;
  110. tinitsymbolinfoproc = procedure;
  111. tdonesymbolinfoproc = procedure;
  112. textractsymbolinfoproc = procedure;
  113. topeninputfilefunc = function(const filename: string): tinputfile;
  114. tgetnamedfiletimefunc = function(const filename: string): longint;
  115. const
  116. do_stop : tstopprocedure = @def_stop;
  117. do_status : tstatusfunction = @def_status;
  118. do_comment : tcommentfunction = @def_comment;
  119. do_internalerror : tinternalerrorfunction = @def_internalerror;
  120. do_initsymbolinfo : tinitsymbolinfoproc = @def_initsymbolinfo;
  121. do_donesymbolinfo : tdonesymbolinfoproc = @def_donesymbolinfo;
  122. do_extractsymbolinfo : textractsymbolinfoproc = @def_extractsymbolinfo;
  123. do_openinputfile : topeninputfilefunc = @def_openinputfile;
  124. do_getnamedfiletime : tgetnamedfiletimefunc = @def_getnamedfiletime;
  125. implementation
  126. uses
  127. {$IFDEF USE_SYSUTILS}
  128. SysUtils,
  129. {$ELSE USE_SYSUTILS}
  130. dos,
  131. {$ENDIF USE_SYSUTILS}
  132. cutils
  133. ;
  134. {****************************************************************************
  135. Helper Routines
  136. ****************************************************************************}
  137. function gccfilename(const s : string) : string;
  138. var
  139. i : longint;
  140. begin
  141. for i:=1to length(s) do
  142. begin
  143. case s[i] of
  144. '\' : gccfilename[i]:='/';
  145. 'A'..'Z' : gccfilename[i]:=chr(ord(s[i])+32);
  146. else
  147. gccfilename[i]:=s[i];
  148. end;
  149. end;
  150. gccfilename[0]:=s[0];
  151. end;
  152. function tostr(i : longint) : string;
  153. var
  154. hs : string;
  155. begin
  156. str(i,hs);
  157. tostr:=hs;
  158. end;
  159. {****************************************************************************
  160. Predefined default Handlers
  161. ****************************************************************************}
  162. { predefined handler when then compiler stops }
  163. procedure def_stop(err:longint);
  164. begin
  165. Halt(err);
  166. end;
  167. {$ifdef DEBUG}
  168. { allow easy stopping in GDB
  169. using
  170. b DEF_GDB_STOP
  171. cond 1 LEVEL <= 8 }
  172. procedure def_gdb_stop(level : longint);
  173. begin
  174. { Its only a dummy for GDB }
  175. end;
  176. {$endif DEBUG}
  177. function def_status:boolean;
  178. {$ifdef HASGETHEAPSTATUS}
  179. var
  180. hstatus : THeapStatus;
  181. {$endif HASGETHEAPSTATUS}
  182. begin
  183. def_status:=false; { never stop }
  184. { Status info?, Called every line }
  185. if ((status.verbosity and V_Status)<>0) then
  186. begin
  187. if (status.compiledlines=1) or
  188. (status.currentline mod 100=0) then
  189. begin
  190. if status.currentline>0 then
  191. Write(status.currentline,' ');
  192. {$ifdef HASGETHEAPSTATUS}
  193. GetHeapStatus(hstatus);
  194. WriteLn(DStr(hstatus.CurrHeapUsed shr 10),'/',DStr(hstatus.CurrHeapSize shr 10),' Kb Used');
  195. {$else HASGETHEAPSTATUS}
  196. WriteLn(DStr(memavail shr 10),'/',DStr(system.heapsize shr 10),' Kb Free');
  197. {$endif HASGETHEAPSTATUS}
  198. end;
  199. end;
  200. {$ifdef macos}
  201. Yield;
  202. {$endif}
  203. end;
  204. Function def_comment(Level:Longint;const s:string):boolean;
  205. const
  206. rh_errorstr = 'error:';
  207. rh_warningstr = 'warning:';
  208. var
  209. hs : string;
  210. begin
  211. def_comment:=false; { never stop }
  212. hs:='';
  213. if not(status.use_gccoutput) then
  214. begin
  215. if (status.verbosity and Level)=V_Hint then
  216. hs:=hintstr;
  217. if (status.verbosity and Level)=V_Note then
  218. hs:=notestr;
  219. if (status.verbosity and Level)=V_Warning then
  220. hs:=warningstr;
  221. if (status.verbosity and Level)=V_Error then
  222. hs:=errorstr;
  223. if (status.verbosity and Level)=V_Fatal then
  224. hs:=fatalstr;
  225. if (status.verbosity and Level)=V_Used then
  226. hs:=PadSpace('('+status.currentmodule+')',10);
  227. end
  228. else
  229. begin
  230. if (status.verbosity and Level)=V_Hint then
  231. hs:=rh_warningstr;
  232. if (status.verbosity and Level)=V_Note then
  233. hs:=rh_warningstr;
  234. if (status.verbosity and Level)=V_Warning then
  235. hs:=rh_warningstr;
  236. if (status.verbosity and Level)=V_Error then
  237. hs:=rh_errorstr;
  238. if (status.verbosity and Level)=V_Fatal then
  239. hs:=rh_errorstr;
  240. end;
  241. { Generate line prefix }
  242. if ((Level and V_LineInfo)=V_LineInfo) and
  243. (status.currentsource<>'') and
  244. (status.currentline>0) then
  245. begin
  246. {$ifndef macos}
  247. { Adding the column should not confuse RHIDE,
  248. even if it does not yet use it PM
  249. but only if it is after error or warning !! PM }
  250. if status.currentcolumn>0 then
  251. begin
  252. if status.use_gccoutput then
  253. hs:=gccfilename(status.currentsource)+':'+tostr(status.currentline)+': '+hs+' '+
  254. tostr(status.currentcolumn)+': '+s
  255. else
  256. hs:=status.currentsource+'('+tostr(status.currentline)+
  257. ','+tostr(status.currentcolumn)+') '+hs+' '+s;
  258. end
  259. else
  260. begin
  261. if status.use_gccoutput then
  262. hs:=gccfilename(status.currentsource)+': '+hs+' '+tostr(status.currentline)+': '+s
  263. else
  264. hs:=status.currentsource+'('+tostr(status.currentline)+') '+hs+' '+s;
  265. end;
  266. {$else}
  267. {MPW style error}
  268. if status.currentcolumn>0 then
  269. hs:='File "'+status.currentsourcepath+status.currentsource+'"; Line '+tostr(status.currentline)+
  270. ' #[' + tostr(status.currentcolumn) + '] ' +hs+' '+s
  271. else
  272. hs:='File "'+status.currentsourcepath+status.currentsource+'"; Line '+tostr(status.currentline)+' # '+hs+' '+s;
  273. {$endif}
  274. end
  275. else
  276. begin
  277. if hs<>'' then
  278. hs:=hs+' '+s
  279. else
  280. hs:=s;
  281. end;
  282. { Display line }
  283. if ((status.verbosity and (Level and V_LevelMask))=(Level and V_LevelMask)) then
  284. begin
  285. {$ifdef FPC}
  286. if status.use_stderr then
  287. begin
  288. writeln(stderr,hs);
  289. flush(stderr);
  290. end
  291. else
  292. {$endif}
  293. begin
  294. if status.use_redir then
  295. writeln(status.redirfile,hs)
  296. else
  297. writeln(hs);
  298. end;
  299. end;
  300. { include everything in the bugreport file }
  301. if status.use_bugreport then
  302. begin
  303. {$ifdef FPC}
  304. Write(status.reportbugfile,hexstr(level,8)+':');
  305. Writeln(status.reportbugfile,hs);
  306. {$endif}
  307. end;
  308. {$ifdef DEBUG}
  309. def_gdb_stop(level);
  310. {$endif DEBUG}
  311. end;
  312. function def_internalerror(i : longint) : boolean;
  313. begin
  314. do_comment(V_Fatal+V_LineInfo,'Internal error '+tostr(i));
  315. {$ifdef EXTDEBUG}
  316. {$ifdef FPC}
  317. { Internalerror() and def_internalerror() do not
  318. have a stackframe }
  319. dump_stack(stdout,get_caller_frame(get_frame));
  320. {$endif FPC}
  321. {$endif EXTDEBUG}
  322. def_internalerror:=true;
  323. end;
  324. procedure def_initsymbolinfo;
  325. begin
  326. end;
  327. procedure def_donesymbolinfo;
  328. begin
  329. end;
  330. procedure def_extractsymbolinfo;
  331. begin
  332. end;
  333. function def_openinputfile(const filename: string): tinputfile;
  334. begin
  335. def_openinputfile:=tdosinputfile.create(filename);
  336. end;
  337. Function def_GetNamedFileTime (Const F : String) : Longint;
  338. var
  339. {$IFDEF USE_SYSUTILS}
  340. fh : THandle;
  341. {$ELSE USE_SYSUTILS}
  342. info : SearchRec;
  343. {$ENDIF USE_SYSUTILS}
  344. begin
  345. Result := -1;
  346. {$IFDEF USE_SYSUTILS}
  347. fh := FileOpen(f, faArchive+faReadOnly+faHidden);
  348. Result := FileGetDate(fh);
  349. FileClose(fh);
  350. {$ELSE USE_SYSUTILS}
  351. FindFirst (F,archive+readonly+hidden,info);
  352. if DosError=0 then
  353. Result := info.time;
  354. FindClose(info);
  355. {$ENDIF USE_SYSUTILS}
  356. end;
  357. end.
  358. {
  359. $Log$
  360. Revision 1.35 2005-01-24 18:12:17 olle
  361. * In MPW, whole path to source file is now displayed in messages.
  362. Revision 1.34 2004/12/28 22:02:05 olle
  363. * fixed typo in MPW error msg
  364. Revision 1.33 2004/12/28 01:39:07 olle
  365. + added support for MPW error messages
  366. + added support for cooperative multitasking under MPW
  367. Revision 1.32 2004/11/22 19:34:58 peter
  368. * GetHeapStatus added, removed MaxAvail,MemAvail,HeapSize
  369. Revision 1.31 2004/10/15 09:14:16 mazen
  370. - remove $IFDEF DELPHI and related code
  371. - remove $IFDEF FPCPROCVAR and related code
  372. Revision 1.30 2004/10/14 18:16:17 mazen
  373. * USE_SYSUTILS merged successfully : cycles with and without defines
  374. * Need to be optimized in performance
  375. Revision 1.29 2004/10/14 17:10:15 mazen
  376. * use SysUtils unit instead of Dos Unit
  377. + overload Replace to use AnsiString
  378. Revision 1.28 2004/09/08 11:23:30 michael
  379. + Check if outputdir exists, Fix exitcode when displaying help pages
  380. Revision 1.27 2004/06/20 08:55:29 florian
  381. * logs truncated
  382. }