comphook.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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. ;
  133. {****************************************************************************
  134. Helper Routines
  135. ****************************************************************************}
  136. function gccfilename(const s : string) : string;
  137. var
  138. i : longint;
  139. begin
  140. for i:=1to length(s) do
  141. begin
  142. case s[i] of
  143. '\' : gccfilename[i]:='/';
  144. 'A'..'Z' : gccfilename[i]:=chr(ord(s[i])+32);
  145. else
  146. gccfilename[i]:=s[i];
  147. end;
  148. end;
  149. gccfilename[0]:=s[0];
  150. end;
  151. function tostr(i : longint) : string;
  152. var
  153. hs : string;
  154. begin
  155. str(i,hs);
  156. tostr:=hs;
  157. end;
  158. {****************************************************************************
  159. Predefined default Handlers
  160. ****************************************************************************}
  161. { predefined handler when then compiler stops }
  162. procedure def_stop;
  163. begin
  164. Halt(1);
  165. end;
  166. {$ifdef DEBUG}
  167. { allow easy stopping in GDB
  168. using
  169. b DEF_GDB_STOP
  170. cond 1 LEVEL <= 8 }
  171. procedure def_gdb_stop(level : longint);
  172. begin
  173. { Its only a dummy for GDB }
  174. end;
  175. {$endif DEBUG}
  176. procedure def_halt(i : longint);
  177. begin
  178. halt(i);
  179. end;
  180. function def_status:boolean;
  181. begin
  182. def_status:=false; { never stop }
  183. { Status info?, Called every line }
  184. if ((status.verbosity and V_Status)<>0) then
  185. begin
  186. {$ifndef Delphi}
  187. if (status.compiledlines=1) then
  188. WriteLn(memavail shr 10,' Kb Free');
  189. {$endif Delphi}
  190. if (status.currentline>0) and (status.currentline mod 100=0) then
  191. {$ifdef FPC}
  192. WriteLn(status.currentline,' ',memavail shr 10,'/',system.heapsize shr 10,' Kb Free');
  193. {$else}
  194. {$ifndef Delphi}
  195. WriteLn(status.currentline,' ',memavail shr 10,' Kb Free');
  196. {$endif Delphi}
  197. {$endif}
  198. end
  199. end;
  200. Function def_comment(Level:Longint;const s:string):boolean;
  201. const
  202. rh_errorstr = 'error:';
  203. rh_warningstr = 'warning:';
  204. var
  205. hs : string;
  206. begin
  207. def_comment:=false; { never stop }
  208. hs:='';
  209. if not(status.use_gccoutput) then
  210. begin
  211. if (status.verbosity and Level)=V_Hint then
  212. hs:=hintstr;
  213. if (status.verbosity and Level)=V_Note then
  214. hs:=notestr;
  215. if (status.verbosity and Level)=V_Warning then
  216. hs:=warningstr;
  217. if (status.verbosity and Level)=V_Error then
  218. hs:=errorstr;
  219. if (status.verbosity and Level)=V_Fatal then
  220. hs:=fatalstr;
  221. end
  222. else
  223. begin
  224. if (status.verbosity and Level)=V_Hint then
  225. hs:=rh_warningstr;
  226. if (status.verbosity and Level)=V_Note then
  227. hs:=rh_warningstr;
  228. if (status.verbosity and Level)=V_Warning then
  229. hs:=rh_warningstr;
  230. if (status.verbosity and Level)=V_Error then
  231. hs:=rh_errorstr;
  232. if (status.verbosity and Level)=V_Fatal then
  233. hs:=rh_errorstr;
  234. end;
  235. if (Level<=V_ShowFile) and (status.currentsource<>'') and (status.currentline>0) then
  236. begin
  237. { Adding the column should not confuse RHIDE,
  238. even if it does not yet use it PM
  239. but only if it is after error or warning !! PM }
  240. if status.currentcolumn>0 then
  241. begin
  242. if status.use_gccoutput then
  243. hs:=gccfilename(status.currentsource)+':'+tostr(status.currentline)+': '+hs+' '+
  244. tostr(status.currentcolumn)+': '+s
  245. else
  246. hs:=status.currentsource+'('+tostr(status.currentline)+
  247. ','+tostr(status.currentcolumn)+') '+hs+' '+s;
  248. end
  249. else
  250. begin
  251. if status.use_gccoutput then
  252. hs:=gccfilename(status.currentsource)+': '+hs+' '+tostr(status.currentline)+': '+s
  253. else
  254. hs:=status.currentsource+'('+tostr(status.currentline)+') '+hs+' '+s;
  255. end;
  256. end
  257. else
  258. begin
  259. if hs<>'' then
  260. hs:=hs+' '+s
  261. else
  262. hs:=s;
  263. end;
  264. {$ifdef FPC}
  265. if status.use_stderr then
  266. begin
  267. writeln(stderr,hs);
  268. flush(stderr);
  269. end
  270. else
  271. {$endif}
  272. begin
  273. if status.use_redir then
  274. writeln(status.redirfile,hs)
  275. else
  276. writeln(hs);
  277. end;
  278. {$ifdef DEBUG}
  279. def_gdb_stop(level);
  280. {$endif DEBUG}
  281. end;
  282. function def_internalerror(i : longint) : boolean;
  283. begin
  284. do_comment(V_Fatal,'Internal error '+tostr(i));
  285. def_internalerror:=true;
  286. end;
  287. procedure def_initsymbolinfo;
  288. begin
  289. end;
  290. procedure def_donesymbolinfo;
  291. begin
  292. end;
  293. procedure def_extractsymbolinfo;
  294. begin
  295. end;
  296. function def_openinputfile(const filename: string): tinputfile;
  297. begin
  298. def_openinputfile:=tdosinputfile.create(filename);
  299. end;
  300. Function def_GetNamedFileTime (Const F : String) : Longint;
  301. var
  302. L : Longint;
  303. info : SearchRec;
  304. begin
  305. l:=-1;
  306. {$ifdef delphi}
  307. dmisc.FindFirst (F,archive+readonly+hidden,info);
  308. {$else delphi}
  309. FindFirst (F,archive+readonly+hidden,info);
  310. {$endif delphi}
  311. if DosError=0 then
  312. l:=info.time;
  313. FindClose(info);
  314. def_GetNamedFileTime:=l;
  315. end;
  316. end.
  317. {
  318. $Log$
  319. Revision 1.18 2002-05-16 19:46:35 carl
  320. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  321. + try to fix temp allocation (still in ifdef)
  322. + generic constructor calls
  323. + start of tassembler / tmodulebase class cleanup
  324. Revision 1.16 2001/08/04 10:23:54 peter
  325. * updates so it works with the ide
  326. Revision 1.15 2001/06/07 21:25:57 peter
  327. * Regenerated
  328. Revision 1.14 2001/06/06 17:20:21 jonas
  329. * fixed wrong typed constant procvars in preparation of my fix which will
  330. disallow them in FPC mode (plus some other unmerged changes since
  331. LAST_MERGE)
  332. Revision 1.13 2001/02/05 20:47:00 peter
  333. * support linux unit for ver1_0 compilers
  334. Revision 1.12 2001/01/21 20:32:45 marco
  335. * Renamefest. Compiler part. Not that hard.
  336. Revision 1.11 2000/12/26 15:58:29 peter
  337. * check for verbosity in verbose instead of comphook
  338. Revision 1.10 2000/12/25 00:07:25 peter
  339. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  340. tlinkedlist objects)
  341. Revision 1.9 2000/11/13 15:26:12 marco
  342. * Renamefest
  343. Revision 1.8 2000/09/30 16:07:20 peter
  344. * prefix fix (merged)
  345. Revision 1.7 2000/09/24 21:33:46 peter
  346. * message updates merges
  347. Revision 1.6 2000/09/24 15:06:13 peter
  348. * use defines.inc
  349. Revision 1.5 2000/08/27 16:11:50 peter
  350. * moved some util functions from globals,cobjects to cutils
  351. * splitted files into finput,fmodule
  352. Revision 1.4 2000/08/13 13:04:15 peter
  353. * -vb update
  354. Revision 1.3 2000/08/12 15:30:45 peter
  355. * IDE patch for stream reading (merged)
  356. Revision 1.2 2000/07/13 11:32:38 michael
  357. + removed logs
  358. }