comphook.pas 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. { <$10000 will show file and line }
  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_Macro = $100;
  33. V_Procedure = $200;
  34. V_Conditional = $400;
  35. V_Assem = $800;
  36. V_Declarations = $1000;
  37. V_Info = $10000;
  38. V_Status = $20000;
  39. V_Used = $40000;
  40. V_Tried = $80000;
  41. V_Debug = $100000;
  42. V_Executable = $200000;
  43. V_ShowFile = $ffff;
  44. V_All = $ffffffff;
  45. V_Default = V_Fatal + V_Error + V_Normal;
  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_gccoutput,
  78. compiling_current : boolean;
  79. { Redirection support }
  80. redirfile : text;
  81. end;
  82. var
  83. status : tcompilerstatus;
  84. { Default Functions }
  85. procedure def_stop;
  86. procedure def_halt(i : longint);
  87. Function def_status:boolean;
  88. Function def_comment(Level:Longint;const s:string):boolean;
  89. function def_internalerror(i:longint):boolean;
  90. procedure def_initsymbolinfo;
  91. procedure def_donesymbolinfo;
  92. procedure def_extractsymbolinfo;
  93. function def_openinputfile(const filename: string): tinputfile;
  94. Function def_getnamedfiletime(Const F : String) : Longint;
  95. {$ifdef DEBUG}
  96. { allow easy stopping in GDB
  97. using
  98. b DEF_GDB_STOP
  99. cond 1 LEVEL <= 8 }
  100. procedure def_gdb_stop(level : longint);
  101. {$endif DEBUG}
  102. { Function redirecting for IDE support }
  103. type
  104. tstopprocedure = procedure;
  105. thaltprocedure = procedure(i : longint);
  106. tstatusfunction = function:boolean;
  107. tcommentfunction = function(Level:Longint;const s:string):boolean;
  108. tinternalerrorfunction = function(i:longint):boolean;
  109. tinitsymbolinfoproc = procedure;
  110. tdonesymbolinfoproc = procedure;
  111. textractsymbolinfoproc = procedure;
  112. topeninputfilefunc = function(const filename: string): tinputfile;
  113. tgetnamedfiletimefunc = function(const filename: string): longint;
  114. const
  115. do_stop : tstopprocedure = {$ifdef FPCPROCVAR}@{$endif}def_stop;
  116. do_halt : thaltprocedure = {$ifdef FPCPROCVAR}@{$endif}def_halt;
  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 delphi}
  128. dmisc,
  129. {$else}
  130. dos,
  131. {$endif}
  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;
  164. begin
  165. Halt(1);
  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. procedure def_halt(i : longint);
  178. begin
  179. halt(i);
  180. end;
  181. function def_status:boolean;
  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. {$ifndef Delphi}
  188. if (status.compiledlines=1) then
  189. WriteLn(memavail shr 10,' Kb Free');
  190. {$endif Delphi}
  191. if (status.currentline>0) and (status.currentline mod 100=0) then
  192. {$ifdef FPC}
  193. WriteLn(status.currentline,' ',DStr(memavail shr 10),'/',DStr(system.heapsize shr 10),' Kb Free');
  194. {$else}
  195. {$ifndef Delphi}
  196. WriteLn(status.currentline,' ',DStr(memavail shr 10),' Kb Free');
  197. {$endif Delphi}
  198. {$endif}
  199. end
  200. end;
  201. Function def_comment(Level:Longint;const s:string):boolean;
  202. const
  203. rh_errorstr = 'error:';
  204. rh_warningstr = 'warning:';
  205. var
  206. hs : string;
  207. begin
  208. def_comment:=false; { never stop }
  209. hs:='';
  210. if not(status.use_gccoutput) then
  211. begin
  212. if (status.verbosity and Level)=V_Hint then
  213. hs:=hintstr;
  214. if (status.verbosity and Level)=V_Note then
  215. hs:=notestr;
  216. if (status.verbosity and Level)=V_Warning then
  217. hs:=warningstr;
  218. if (status.verbosity and Level)=V_Error then
  219. hs:=errorstr;
  220. if (status.verbosity and Level)=V_Fatal then
  221. hs:=fatalstr;
  222. end
  223. else
  224. begin
  225. if (status.verbosity and Level)=V_Hint then
  226. hs:=rh_warningstr;
  227. if (status.verbosity and Level)=V_Note then
  228. hs:=rh_warningstr;
  229. if (status.verbosity and Level)=V_Warning then
  230. hs:=rh_warningstr;
  231. if (status.verbosity and Level)=V_Error then
  232. hs:=rh_errorstr;
  233. if (status.verbosity and Level)=V_Fatal then
  234. hs:=rh_errorstr;
  235. end;
  236. if (Level<=V_ShowFile) and (status.currentsource<>'') and (status.currentline>0) then
  237. begin
  238. { Adding the column should not confuse RHIDE,
  239. even if it does not yet use it PM
  240. but only if it is after error or warning !! PM }
  241. if status.currentcolumn>0 then
  242. begin
  243. if status.use_gccoutput then
  244. hs:=gccfilename(status.currentsource)+':'+tostr(status.currentline)+': '+hs+' '+
  245. tostr(status.currentcolumn)+': '+s
  246. else
  247. hs:=status.currentsource+'('+tostr(status.currentline)+
  248. ','+tostr(status.currentcolumn)+') '+hs+' '+s;
  249. end
  250. else
  251. begin
  252. if status.use_gccoutput then
  253. hs:=gccfilename(status.currentsource)+': '+hs+' '+tostr(status.currentline)+': '+s
  254. else
  255. hs:=status.currentsource+'('+tostr(status.currentline)+') '+hs+' '+s;
  256. end;
  257. end
  258. else
  259. begin
  260. if hs<>'' then
  261. hs:=hs+' '+s
  262. else
  263. hs:=s;
  264. end;
  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. {$ifdef DEBUG}
  280. def_gdb_stop(level);
  281. {$endif DEBUG}
  282. end;
  283. function def_internalerror(i : longint) : boolean;
  284. begin
  285. do_comment(V_Fatal,'Internal error '+tostr(i));
  286. def_internalerror:=true;
  287. end;
  288. procedure def_initsymbolinfo;
  289. begin
  290. end;
  291. procedure def_donesymbolinfo;
  292. begin
  293. end;
  294. procedure def_extractsymbolinfo;
  295. begin
  296. end;
  297. function def_openinputfile(const filename: string): tinputfile;
  298. begin
  299. def_openinputfile:=tdosinputfile.create(filename);
  300. end;
  301. Function def_GetNamedFileTime (Const F : String) : Longint;
  302. var
  303. L : Longint;
  304. info : SearchRec;
  305. begin
  306. l:=-1;
  307. {$ifdef delphi}
  308. dmisc.FindFirst (F,archive+readonly+hidden,info);
  309. {$else delphi}
  310. FindFirst (F,archive+readonly+hidden,info);
  311. {$endif delphi}
  312. if DosError=0 then
  313. l:=info.time;
  314. FindClose(info);
  315. def_GetNamedFileTime:=l;
  316. end;
  317. end.
  318. {
  319. $Log$
  320. Revision 1.20 2002-09-05 19:29:42 peter
  321. * memdebug enhancements
  322. Revision 1.19 2002/05/18 13:34:06 peter
  323. * readded missing revisions
  324. Revision 1.18 2002/05/16 19:46:35 carl
  325. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  326. + try to fix temp allocation (still in ifdef)
  327. + generic constructor calls
  328. + start of tassembler / tmodulebase class cleanup
  329. }