comphook.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. {
  2. Copyright (c) 1998-2002 by Peter Vreman
  3. This unit handles the compilerhooks for output to external programs
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit comphook;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. {$IFNDEF USE_FAKE_SYSUTILS}
  22. sysutils,
  23. {$ELSE}
  24. fksysutl,
  25. {$ENDIF}
  26. globtype,
  27. finput;
  28. Const
  29. { Levels }
  30. V_None = $0;
  31. V_Fatal = $1;
  32. V_Error = $2;
  33. V_Normal = $4; { doesn't show a text like Error: }
  34. V_Warning = $8;
  35. V_Note = $10;
  36. V_Hint = $20;
  37. V_LineInfoMask = $fff;
  38. { From here by default no line info }
  39. V_Info = $1000;
  40. V_Status = $2000;
  41. V_Used = $4000;
  42. V_Tried = $8000;
  43. V_Conditional = $10000;
  44. V_Debug = $20000;
  45. V_Executable = $40000;
  46. V_TimeStamps = $80000;
  47. V_LevelMask = $fffffff;
  48. V_All = V_LevelMask;
  49. V_Default = V_Fatal + V_Error + V_Normal;
  50. { Flags }
  51. V_LineInfo = $10000000;
  52. const
  53. { RHIDE expect gcc like error output }
  54. fatalstr : string[20] = 'Fatal:';
  55. errorstr : string[20] = 'Error:';
  56. warningstr : string[20] = 'Warning:';
  57. notestr : string[20] = 'Note:';
  58. hintstr : string[20] = 'Hint:';
  59. type
  60. PCompilerStatus = ^TCompilerStatus;
  61. TCompilerStatus = record
  62. { Current status }
  63. currentmodule,
  64. currentsourcepath,
  65. currentsource : string; { filename }
  66. currentline,
  67. currentcolumn : longint; { current line and column }
  68. { Total Status }
  69. compiledlines : longint; { the number of lines which are compiled }
  70. errorcount,
  71. countWarnings,
  72. countNotes,
  73. countHints : longint; { number of found errors/warnings/notes/hints }
  74. codesize,
  75. datasize : aint;
  76. { program info }
  77. isexe,
  78. islibrary : boolean;
  79. { Settings for the output }
  80. verbosity : longint;
  81. maxerrorcount : longint;
  82. errorwarning,
  83. errornote,
  84. errorhint,
  85. skip_error,
  86. use_stderr,
  87. use_redir,
  88. use_bugreport,
  89. use_gccoutput,
  90. print_source_path,
  91. compiling_current : boolean;
  92. { Redirection support }
  93. redirfile : text;
  94. { Special file for bug report }
  95. reportbugfile : text;
  96. end;
  97. var
  98. status : tcompilerstatus;
  99. type
  100. EControlCAbort=class(Exception)
  101. constructor Create;
  102. end;
  103. ECompilerAbort=class(Exception)
  104. constructor Create;
  105. end;
  106. ECompilerAbortSilent=class(Exception)
  107. constructor Create;
  108. end;
  109. { Default Functions }
  110. Function def_status:boolean;
  111. Function def_comment(Level:Longint;const s:string):boolean;
  112. function def_internalerror(i:longint):boolean;
  113. procedure def_initsymbolinfo;
  114. procedure def_donesymbolinfo;
  115. procedure def_extractsymbolinfo;
  116. function def_openinputfile(const filename: string): tinputfile;
  117. Function def_getnamedfiletime(Const F : String) : Longint;
  118. { Function redirecting for IDE support }
  119. type
  120. tstopprocedure = procedure(err:longint);
  121. tstatusfunction = function:boolean;
  122. tcommentfunction = function(Level:Longint;const s:string):boolean;
  123. tinternalerrorfunction = function(i:longint):boolean;
  124. tinitsymbolinfoproc = procedure;
  125. tdonesymbolinfoproc = procedure;
  126. textractsymbolinfoproc = procedure;
  127. topeninputfilefunc = function(const filename: string): tinputfile;
  128. tgetnamedfiletimefunc = function(const filename: string): longint;
  129. const
  130. do_status : tstatusfunction = @def_status;
  131. do_comment : tcommentfunction = @def_comment;
  132. do_internalerror : tinternalerrorfunction = @def_internalerror;
  133. do_initsymbolinfo : tinitsymbolinfoproc = @def_initsymbolinfo;
  134. do_donesymbolinfo : tdonesymbolinfoproc = @def_donesymbolinfo;
  135. do_extractsymbolinfo : textractsymbolinfoproc = @def_extractsymbolinfo;
  136. needsymbolinfo : boolean =false;
  137. do_openinputfile : topeninputfilefunc = @def_openinputfile;
  138. do_getnamedfiletime : tgetnamedfiletimefunc = @def_getnamedfiletime;
  139. implementation
  140. uses
  141. cutils, systems, globals
  142. ;
  143. {****************************************************************************
  144. Helper Routines
  145. ****************************************************************************}
  146. function gccfilename(const s : string) : string;
  147. var
  148. i : longint;
  149. begin
  150. for i:=1to length(s) do
  151. begin
  152. case s[i] of
  153. '\' : gccfilename[i]:='/';
  154. 'A'..'Z' : if not (tf_files_case_aware in source_info.flags) and
  155. not (tf_files_case_sensitive in source_info.flags) then
  156. gccfilename[i]:=chr(ord(s[i])+32)
  157. else
  158. gccfilename[i]:=s[i];
  159. else
  160. gccfilename[i]:=s[i];
  161. end;
  162. end;
  163. gccfilename[0]:=s[0];
  164. end;
  165. function tostr(i : longint) : string;
  166. var
  167. hs : string;
  168. begin
  169. str(i,hs);
  170. tostr:=hs;
  171. end;
  172. {****************************************************************************
  173. Stopping the compiler
  174. ****************************************************************************}
  175. constructor EControlCAbort.Create;
  176. begin
  177. inherited Create('Ctrl-C Signaled!');
  178. end;
  179. constructor ECompilerAbort.Create;
  180. begin
  181. inherited Create('Compilation Aborted');
  182. end;
  183. constructor ECompilerAbortSilent.Create;
  184. begin
  185. inherited Create('Compilation Aborted');
  186. end;
  187. {****************************************************************************
  188. Predefined default Handlers
  189. ****************************************************************************}
  190. function def_status:boolean;
  191. var
  192. hstatus : TFPCHeapStatus;
  193. begin
  194. def_status:=false; { never stop }
  195. { Status info?, Called every line }
  196. if ((status.verbosity and V_Status)<>0) then
  197. begin
  198. if (status.compiledlines=1) or
  199. (status.currentline mod 100=0) then
  200. begin
  201. if status.currentline>0 then
  202. Write(status.currentline,' ');
  203. hstatus:=GetFPCHeapStatus;
  204. WriteLn(DStr(hstatus.CurrHeapUsed shr 10),'/',DStr(hstatus.CurrHeapSize shr 10),' Kb Used');
  205. end;
  206. end;
  207. {$ifdef macos}
  208. Yield;
  209. {$endif}
  210. end;
  211. Function def_comment(Level:Longint;const s:string):boolean;
  212. const
  213. rh_errorstr = 'error:';
  214. rh_warningstr = 'warning:';
  215. var
  216. hs : string;
  217. hs2 : string;
  218. begin
  219. def_comment:=false; { never stop }
  220. hs:='';
  221. if not(status.use_gccoutput) then
  222. begin
  223. if (status.verbosity and Level)=V_Hint then
  224. hs:=hintstr;
  225. if (status.verbosity and Level)=V_Note then
  226. hs:=notestr;
  227. if (status.verbosity and Level)=V_Warning then
  228. hs:=warningstr;
  229. if (status.verbosity and Level)=V_Error then
  230. hs:=errorstr;
  231. if (status.verbosity and Level)=V_Fatal then
  232. hs:=fatalstr;
  233. if (status.verbosity and Level)=V_Used then
  234. hs:=PadSpace('('+status.currentmodule+')',10);
  235. end
  236. else
  237. begin
  238. if (status.verbosity and Level)=V_Hint then
  239. hs:=rh_warningstr;
  240. if (status.verbosity and Level)=V_Note then
  241. hs:=rh_warningstr;
  242. if (status.verbosity and Level)=V_Warning then
  243. hs:=rh_warningstr;
  244. if (status.verbosity and Level)=V_Error then
  245. hs:=rh_errorstr;
  246. if (status.verbosity and Level)=V_Fatal then
  247. hs:=rh_errorstr;
  248. end;
  249. { Generate line prefix }
  250. if ((Level and V_LineInfo)=V_LineInfo) and
  251. (status.currentsource<>'') and
  252. (status.currentline>0) then
  253. begin
  254. {$ifndef macos}
  255. { Adding the column should not confuse RHIDE,
  256. even if it does not yet use it PM
  257. but only if it is after error or warning !! PM }
  258. if status.currentcolumn>0 then
  259. begin
  260. if status.use_gccoutput then
  261. hs:=gccfilename(status.currentsource)+':'+tostr(status.currentline)+': '+hs+' '+
  262. tostr(status.currentcolumn)+': '+s
  263. else
  264. begin
  265. hs:=status.currentsource+'('+tostr(status.currentline)+
  266. ','+tostr(status.currentcolumn)+') '+hs+' '+s;
  267. end;
  268. if status.print_source_path then
  269. hs:=status.currentsourcepath+hs;
  270. end
  271. else
  272. begin
  273. if status.use_gccoutput then
  274. hs:=gccfilename(status.currentsource)+': '+hs+' '+tostr(status.currentline)+': '+s
  275. else
  276. hs:=status.currentsource+'('+tostr(status.currentline)+') '+hs+' '+s;
  277. end;
  278. {$else}
  279. {MPW style error}
  280. if status.currentcolumn>0 then
  281. hs:='File "'+status.currentsourcepath+status.currentsource+'"; Line '+tostr(status.currentline)+
  282. ' #[' + tostr(status.currentcolumn) + '] ' +hs+' '+s
  283. else
  284. hs:='File "'+status.currentsourcepath+status.currentsource+'"; Line '+tostr(status.currentline)+' # '+hs+' '+s;
  285. {$endif}
  286. end
  287. else
  288. begin
  289. if hs<>'' then
  290. hs:=hs+' '+s
  291. else
  292. hs:=s;
  293. end;
  294. if (status.verbosity and V_TimeStamps)<>0 then
  295. begin
  296. system.str(getrealtime-starttime:0:3,hs2);
  297. hs:='['+hs2+'] '+s;
  298. end;
  299. { Display line }
  300. if ((status.verbosity and (Level and V_LevelMask))=(Level and V_LevelMask)) then
  301. begin
  302. if status.use_stderr then
  303. begin
  304. writeln(stderr,hs);
  305. flush(stderr);
  306. end
  307. else
  308. begin
  309. if status.use_redir then
  310. writeln(status.redirfile,hs)
  311. else
  312. writeln(hs);
  313. end;
  314. end;
  315. { include everything in the bugreport file }
  316. if status.use_bugreport then
  317. begin
  318. Write(status.reportbugfile,hexstr(level,8)+':');
  319. Writeln(status.reportbugfile,hs);
  320. end;
  321. end;
  322. function def_internalerror(i : longint) : boolean;
  323. begin
  324. do_comment(V_Fatal+V_LineInfo,'Internal error '+tostr(i));
  325. {$ifdef EXTDEBUG}
  326. { Internalerror() and def_internalerror() do not
  327. have a stackframe }
  328. dump_stack(stdout,get_caller_frame(get_frame));
  329. {$endif EXTDEBUG}
  330. def_internalerror:=true;
  331. end;
  332. procedure def_initsymbolinfo;
  333. begin
  334. end;
  335. procedure def_donesymbolinfo;
  336. begin
  337. end;
  338. procedure def_extractsymbolinfo;
  339. begin
  340. end;
  341. function def_openinputfile(const filename: string): tinputfile;
  342. begin
  343. def_openinputfile:=tdosinputfile.create(filename);
  344. end;
  345. Function def_GetNamedFileTime (Const F : String) : Longint;
  346. begin
  347. Result:=FileAge(F);
  348. end;
  349. end.