comphook.pas 9.9 KB

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