comphook.pas 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. interface
  20. uses files;
  21. Const
  22. { <$10000 will show file and line }
  23. V_None = $0;
  24. V_Fatal = $1;
  25. V_Error = $2;
  26. V_Normal = $4; { doesn't show a text like Error: }
  27. V_Warning = $8;
  28. V_Note = $10;
  29. V_Hint = $20;
  30. V_Macro = $100;
  31. V_Procedure = $200;
  32. V_Conditional = $400;
  33. V_Assem = $800;
  34. V_Declarations = $1000;
  35. V_Info = $10000;
  36. V_Status = $20000;
  37. V_Used = $40000;
  38. V_Tried = $80000;
  39. V_Debug = $100000;
  40. V_Executable = $200000;
  41. V_ShowFile = $ffff;
  42. V_All = $ffffffff;
  43. V_Default = V_Fatal + V_Error + V_Normal;
  44. type
  45. PCompilerStatus = ^TCompilerStatus;
  46. TCompilerStatus = record
  47. { Current status }
  48. currentmodule,
  49. currentsourcepath,
  50. currentsource : string; { filename }
  51. currentline,
  52. currentcolumn : longint; { current line and column }
  53. { Total Status }
  54. compiledlines : longint; { the number of lines which are compiled }
  55. errorcount : longint; { number of generated errors }
  56. { Settings for the output }
  57. verbosity : longint;
  58. maxerrorcount : longint;
  59. errorwarning,
  60. errornote,
  61. errorhint,
  62. skip_error,
  63. use_stderr,
  64. use_redir,
  65. use_gccoutput : boolean;
  66. { Redirection support }
  67. redirfile : text;
  68. end;
  69. var
  70. status : tcompilerstatus;
  71. { Default Functions }
  72. procedure def_stop;
  73. procedure def_halt(i : longint);
  74. Function def_status:boolean;
  75. Function def_comment(Level:Longint;const s:string):boolean;
  76. function def_internalerror(i:longint):boolean;
  77. procedure def_initsymbolinfo;
  78. procedure def_donesymbolinfo;
  79. procedure def_extractsymbolinfo;
  80. function def_openinputfile(const filename: string): pinputfile;
  81. Function def_getnamedfiletime(Const F : String) : Longint;
  82. {$ifdef DEBUG}
  83. { allow easy stopping in GDB
  84. using
  85. b DEF_GDB_STOP
  86. cond 1 LEVEL <= 8 }
  87. procedure def_gdb_stop(level : longint);
  88. {$endif DEBUG}
  89. { Function redirecting for IDE support }
  90. type
  91. tstopprocedure = procedure;
  92. thaltprocedure = procedure(i : longint);
  93. tstatusfunction = function:boolean;
  94. tcommentfunction = function(Level:Longint;const s:string):boolean;
  95. tinternalerrorfunction = function(i:longint):boolean;
  96. tinitsymbolinfoproc = procedure;
  97. tdonesymbolinfoproc = procedure;
  98. textractsymbolinfoproc = procedure;
  99. topeninputfilefunc = function(const filename: string): pinputfile;
  100. tgetnamedfiletimefunc = function(const filename: string): longint;
  101. const
  102. do_stop : tstopprocedure = def_stop;
  103. do_halt : thaltprocedure = def_halt;
  104. do_status : tstatusfunction = def_status;
  105. do_comment : tcommentfunction = def_comment;
  106. do_internalerror : tinternalerrorfunction = def_internalerror;
  107. do_initsymbolinfo : tinitsymbolinfoproc = def_initsymbolinfo;
  108. do_donesymbolinfo : tdonesymbolinfoproc = def_donesymbolinfo;
  109. do_extractsymbolinfo : textractsymbolinfoproc = def_extractsymbolinfo;
  110. do_openinputfile : topeninputfilefunc = def_openinputfile;
  111. do_getnamedfiletime : tgetnamedfiletimefunc = def_getnamedfiletime;
  112. implementation
  113. uses
  114. {$ifdef USEEXCEPT}
  115. tpexcept,
  116. {$endif USEEXCEPT}
  117. {$ifdef Linux}
  118. linux,
  119. {$endif}
  120. dos;
  121. {****************************************************************************
  122. Helper Routines
  123. ****************************************************************************}
  124. function gccfilename(const s : string) : string;
  125. var
  126. i : longint;
  127. begin
  128. for i:=1to length(s) do
  129. begin
  130. case s[i] of
  131. '\' : gccfilename[i]:='/';
  132. 'A'..'Z' : gccfilename[i]:=chr(ord(s[i])+32);
  133. else
  134. gccfilename[i]:=s[i];
  135. end;
  136. end;
  137. {$ifndef TP}
  138. {$ifopt H+}
  139. setlength(gccfilename,length(s));
  140. {$else}
  141. gccfilename[0]:=s[0];
  142. {$endif}
  143. {$else}
  144. gccfilename[0]:=s[0];
  145. {$endif}
  146. end;
  147. function tostr(i : longint) : string;
  148. var
  149. hs : string;
  150. begin
  151. str(i,hs);
  152. tostr:=hs;
  153. end;
  154. {****************************************************************************
  155. Predefined default Handlers
  156. ****************************************************************************}
  157. { predefined handler when then compiler stops }
  158. procedure def_stop;
  159. begin
  160. {$ifndef USEEXCEPT}
  161. Halt(1);
  162. {$else USEEXCEPT}
  163. Halt(1);
  164. {$endif USEEXCEPT}
  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. { RHIDE expect gcc like error output }
  203. rh_errorstr='error: ';
  204. rh_warningstr='warning: ';
  205. fatalstr='Fatal: ';
  206. errorstr='Error: ';
  207. warningstr='Warning: ';
  208. notestr='Note: ';
  209. hintstr='Hint: ';
  210. var
  211. hs : string;
  212. begin
  213. def_comment:=false; { never stop }
  214. if (status.verbosity and Level)=Level then
  215. begin
  216. hs:='';
  217. if not(status.use_gccoutput) then
  218. begin
  219. if (status.verbosity and Level)=V_Hint then
  220. hs:=hintstr;
  221. if (status.verbosity and Level)=V_Note then
  222. hs:=notestr;
  223. if (status.verbosity and Level)=V_Warning then
  224. hs:=warningstr;
  225. if (status.verbosity and Level)=V_Error then
  226. hs:=errorstr;
  227. if (status.verbosity and Level)=V_Fatal then
  228. hs:=fatalstr;
  229. end
  230. else
  231. begin
  232. if (status.verbosity and Level)=V_Hint then
  233. hs:=rh_warningstr;
  234. if (status.verbosity and Level)=V_Note then
  235. hs:=rh_warningstr;
  236. if (status.verbosity and Level)=V_Warning then
  237. hs:=rh_warningstr;
  238. if (status.verbosity and Level)=V_Error then
  239. hs:=rh_errorstr;
  240. if (status.verbosity and Level)=V_Fatal then
  241. hs:=rh_errorstr;
  242. end;
  243. if (Level<=V_ShowFile) and (status.currentsource<>'') and (status.currentline>0) then
  244. begin
  245. { Adding the column should not confuse RHIDE,
  246. even if it does not yet use it PM
  247. but only if it is after error or warning !! PM }
  248. if status.currentcolumn>0 then
  249. begin
  250. if status.use_gccoutput then
  251. hs:=gccfilename(status.currentsource)+':'+tostr(status.currentline)+': '+hs
  252. +tostr(status.currentcolumn)+': '
  253. else
  254. hs:=status.currentsource+'('+tostr(status.currentline)
  255. +','+tostr(status.currentcolumn)+') '+hs;
  256. end
  257. else
  258. begin
  259. if status.use_gccoutput then
  260. hs:=gccfilename(status.currentsource)+': '+hs+tostr(status.currentline)+': '
  261. else
  262. hs:=status.currentsource+'('+tostr(status.currentline)+') '+hs;
  263. end;
  264. end;
  265. { add the message to the text }
  266. hs:=hs+s;
  267. {$ifdef FPC}
  268. if status.use_stderr then
  269. begin
  270. writeln(stderr,hs);
  271. flush(stderr);
  272. end
  273. else
  274. {$endif}
  275. begin
  276. if status.use_redir then
  277. writeln(status.redirfile,hs)
  278. else
  279. writeln(hs);
  280. end;
  281. {$ifdef DEBUG}
  282. def_gdb_stop(level);
  283. {$endif DEBUG}
  284. end;
  285. end;
  286. function def_internalerror(i : longint) : boolean;
  287. begin
  288. do_comment(V_Fatal,'Internal error '+tostr(i));
  289. def_internalerror:=true;
  290. end;
  291. procedure def_initsymbolinfo;
  292. begin
  293. end;
  294. procedure def_donesymbolinfo;
  295. begin
  296. end;
  297. procedure def_extractsymbolinfo;
  298. begin
  299. end;
  300. function def_openinputfile(const filename: string): pinputfile;
  301. begin
  302. def_openinputfile:=new(pdosinputfile, init(filename));
  303. end;
  304. Function def_GetNamedFileTime (Const F : String) : Longint;
  305. var
  306. L : Longint;
  307. {$ifndef linux}
  308. info : SearchRec;
  309. {$else}
  310. info : stat;
  311. {$endif}
  312. begin
  313. l:=-1;
  314. {$ifdef linux}
  315. if FStat (F,Info) then
  316. L:=info.mtime;
  317. {$else}
  318. {$ifdef delphi}
  319. dmisc.FindFirst (F,archive+readonly+hidden,info);
  320. {$else delphi}
  321. FindFirst (F,archive+readonly+hidden,info);
  322. {$endif delphi}
  323. if DosError=0 then
  324. l:=info.time;
  325. {$ifdef Linux}
  326. FindClose(info);
  327. {$endif}
  328. {$ifdef Win32}
  329. FindClose(info);
  330. {$endif}
  331. {$endif}
  332. def_GetNamedFileTime:=l;
  333. end;
  334. end.
  335. {
  336. $Log$
  337. Revision 1.4 2000-08-13 13:04:15 peter
  338. * -vb update
  339. Revision 1.3 2000/08/12 15:30:45 peter
  340. * IDE patch for stream reading (merged)
  341. Revision 1.2 2000/07/13 11:32:38 michael
  342. + removed logs
  343. }