comphook.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 = {$ifdef FPCPROCVAR}@{$endif}def_stop;
  117. do_status : tstatusfunction = {$ifdef FPCPROCVAR}@{$endif}def_status;
  118. do_comment : tcommentfunction = {$ifdef FPCPROCVAR}@{$endif}def_comment;
  119. do_internalerror : tinternalerrorfunction = {$ifdef FPCPROCVAR}@{$endif}def_internalerror;
  120. do_initsymbolinfo : tinitsymbolinfoproc = {$ifdef FPCPROCVAR}@{$endif}def_initsymbolinfo;
  121. do_donesymbolinfo : tdonesymbolinfoproc = {$ifdef FPCPROCVAR}@{$endif}def_donesymbolinfo;
  122. do_extractsymbolinfo : textractsymbolinfoproc = {$ifdef FPCPROCVAR}@{$endif}def_extractsymbolinfo;
  123. do_openinputfile : topeninputfilefunc = {$ifdef FPCPROCVAR}@{$endif}def_openinputfile;
  124. do_getnamedfiletime : tgetnamedfiletimefunc = {$ifdef FPCPROCVAR}@{$endif}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. begin
  179. def_status:=false; { never stop }
  180. { Status info?, Called every line }
  181. if ((status.verbosity and V_Status)<>0) then
  182. begin
  183. {$ifndef Delphi}
  184. if (status.compiledlines=1) then
  185. WriteLn(memavail shr 10,' Kb Free');
  186. {$endif Delphi}
  187. if (status.currentline>0) and (status.currentline mod 100=0) then
  188. {$ifdef FPC}
  189. WriteLn(status.currentline,' ',DStr(memavail shr 10),'/',DStr(system.heapsize shr 10),' Kb Free');
  190. {$else}
  191. {$ifndef Delphi}
  192. WriteLn(status.currentline,' ',DStr(memavail shr 10),' Kb Free');
  193. {$endif Delphi}
  194. {$endif}
  195. end
  196. end;
  197. Function def_comment(Level:Longint;const s:string):boolean;
  198. const
  199. rh_errorstr = 'error:';
  200. rh_warningstr = 'warning:';
  201. var
  202. hs : string;
  203. begin
  204. def_comment:=false; { never stop }
  205. hs:='';
  206. if not(status.use_gccoutput) then
  207. begin
  208. if (status.verbosity and Level)=V_Hint then
  209. hs:=hintstr;
  210. if (status.verbosity and Level)=V_Note then
  211. hs:=notestr;
  212. if (status.verbosity and Level)=V_Warning then
  213. hs:=warningstr;
  214. if (status.verbosity and Level)=V_Error then
  215. hs:=errorstr;
  216. if (status.verbosity and Level)=V_Fatal then
  217. hs:=fatalstr;
  218. if (status.verbosity and Level)=V_Used then
  219. hs:=PadSpace('('+status.currentmodule+')',10);
  220. end
  221. else
  222. begin
  223. if (status.verbosity and Level)=V_Hint then
  224. hs:=rh_warningstr;
  225. if (status.verbosity and Level)=V_Note then
  226. hs:=rh_warningstr;
  227. if (status.verbosity and Level)=V_Warning then
  228. hs:=rh_warningstr;
  229. if (status.verbosity and Level)=V_Error then
  230. hs:=rh_errorstr;
  231. if (status.verbosity and Level)=V_Fatal then
  232. hs:=rh_errorstr;
  233. end;
  234. { Generate line prefix }
  235. if ((Level and V_LineInfo)=V_LineInfo) and
  236. (status.currentsource<>'') and
  237. (status.currentline>0) then
  238. begin
  239. { Adding the column should not confuse RHIDE,
  240. even if it does not yet use it PM
  241. but only if it is after error or warning !! PM }
  242. if status.currentcolumn>0 then
  243. begin
  244. if status.use_gccoutput then
  245. hs:=gccfilename(status.currentsource)+':'+tostr(status.currentline)+': '+hs+' '+
  246. tostr(status.currentcolumn)+': '+s
  247. else
  248. hs:=status.currentsource+'('+tostr(status.currentline)+
  249. ','+tostr(status.currentcolumn)+') '+hs+' '+s;
  250. end
  251. else
  252. begin
  253. if status.use_gccoutput then
  254. hs:=gccfilename(status.currentsource)+': '+hs+' '+tostr(status.currentline)+': '+s
  255. else
  256. hs:=status.currentsource+'('+tostr(status.currentline)+') '+hs+' '+s;
  257. end;
  258. end
  259. else
  260. begin
  261. if hs<>'' then
  262. hs:=hs+' '+s
  263. else
  264. hs:=s;
  265. end;
  266. { Display line }
  267. if ((status.verbosity and (Level and V_LevelMask))=(Level and V_LevelMask)) then
  268. begin
  269. {$ifdef FPC}
  270. if status.use_stderr then
  271. begin
  272. writeln(stderr,hs);
  273. flush(stderr);
  274. end
  275. else
  276. {$endif}
  277. begin
  278. if status.use_redir then
  279. writeln(status.redirfile,hs)
  280. else
  281. writeln(hs);
  282. end;
  283. end;
  284. { include everything in the bugreport file }
  285. if status.use_bugreport then
  286. begin
  287. {$ifdef FPC}
  288. Write(status.reportbugfile,hexstr(level,8)+':');
  289. Writeln(status.reportbugfile,hs);
  290. {$endif}
  291. end;
  292. {$ifdef DEBUG}
  293. def_gdb_stop(level);
  294. {$endif DEBUG}
  295. end;
  296. function def_internalerror(i : longint) : boolean;
  297. begin
  298. do_comment(V_Fatal+V_LineInfo,'Internal error '+tostr(i));
  299. {$ifdef EXTDEBUG}
  300. {$ifdef FPC}
  301. { Internalerror() and def_internalerror() do not
  302. have a stackframe }
  303. dump_stack(stdout,get_caller_frame(get_frame));
  304. {$endif FPC}
  305. {$endif EXTDEBUG}
  306. def_internalerror:=true;
  307. end;
  308. procedure def_initsymbolinfo;
  309. begin
  310. end;
  311. procedure def_donesymbolinfo;
  312. begin
  313. end;
  314. procedure def_extractsymbolinfo;
  315. begin
  316. end;
  317. function def_openinputfile(const filename: string): tinputfile;
  318. begin
  319. def_openinputfile:=tdosinputfile.create(filename);
  320. end;
  321. Function def_GetNamedFileTime (Const F : String) : Longint;
  322. var
  323. L : Longint;
  324. info : SearchRec;
  325. begin
  326. l:=-1;
  327. {$IFDEF USE_SYSUTILS}
  328. if FindFirst (F,faArchive+faReadOnly+faHidden,info) = 0
  329. then
  330. {$ELSE USE_SYSUTILS}
  331. FindFirst (F,archive+readonly+hidden,info);
  332. if DosError=0 then
  333. {$ENDIF USE_SYSUTILS}
  334. l:=info.time;
  335. FindClose(info);
  336. def_GetNamedFileTime:=l;
  337. end;
  338. end.
  339. {
  340. $Log$
  341. Revision 1.29 2004-10-14 17:10:15 mazen
  342. * use SysUtils unit instead of Dos Unit
  343. + overload Replace to use AnsiString
  344. Revision 1.28 2004/09/08 11:23:30 michael
  345. + Check if outputdir exists, Fix exitcode when displaying help pages
  346. Revision 1.27 2004/06/20 08:55:29 florian
  347. * logs truncated
  348. }