comphook.pas 10 KB

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