comphook.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. currentmodulestate : string[20];
  69. { Total Status }
  70. compiledlines : longint; { the number of lines which are compiled }
  71. errorcount, { this field should never be increased directly,
  72. use Verbose.GenerateError procedure to do this,
  73. this allows easier error catching using GDB by
  74. adding a single breakpoint at this procedure }
  75. countWarnings,
  76. countNotes,
  77. countHints : longint; { number of found errors/warnings/notes/hints }
  78. codesize,
  79. datasize : qword;
  80. { program info }
  81. isexe,
  82. ispackage,
  83. islibrary : boolean;
  84. { Settings for the output }
  85. showmsgnrs : boolean;
  86. verbosity : longint;
  87. maxerrorcount : longint;
  88. errorwarning,
  89. errornote,
  90. errorhint,
  91. skip_error,
  92. use_stderr,
  93. use_redir,
  94. use_bugreport,
  95. use_gccoutput,
  96. print_source_path : boolean;
  97. { Redirection support }
  98. redirfile : text;
  99. { Special file for bug report }
  100. reportbugfile : text;
  101. end;
  102. type
  103. EControlCAbort=class(Exception)
  104. constructor Create;
  105. end;
  106. ECompilerAbort=class(Exception)
  107. constructor Create;
  108. end;
  109. ECompilerAbortSilent=class(Exception)
  110. constructor Create;
  111. end;
  112. var
  113. status : tcompilerstatus;
  114. { Default Functions }
  115. Function def_status:boolean;
  116. Function def_comment(Level:Longint;const s:ansistring):boolean;
  117. function def_internalerror(i:longint):boolean;
  118. function def_CheckVerbosity(v:longint):boolean;
  119. procedure def_initsymbolinfo;
  120. procedure def_donesymbolinfo;
  121. procedure def_extractsymbolinfo;
  122. function def_openinputfile(const filename: TPathStr): tinputfile;
  123. Function def_getnamedfiletime(Const F : TPathStr) : Longint;
  124. { Function redirecting for IDE support }
  125. type
  126. tstopprocedure = procedure(err:longint);
  127. tstatusfunction = function:boolean;
  128. tcommentfunction = function(Level:Longint;const s:ansistring):boolean;
  129. tinternalerrorfunction = function(i:longint):boolean;
  130. tcheckverbosityfunction = function(i:longint):boolean;
  131. tinitsymbolinfoproc = procedure;
  132. tdonesymbolinfoproc = procedure;
  133. textractsymbolinfoproc = procedure;
  134. topeninputfilefunc = function(const filename: TPathStr): tinputfile;
  135. tgetnamedfiletimefunc = function(const filename: TPathStr): longint;
  136. const
  137. do_status : tstatusfunction = @def_status;
  138. do_comment : tcommentfunction = @def_comment;
  139. do_internalerror : tinternalerrorfunction = @def_internalerror;
  140. do_checkverbosity : tcheckverbosityfunction = @def_checkverbosity;
  141. do_initsymbolinfo : tinitsymbolinfoproc = @def_initsymbolinfo;
  142. do_donesymbolinfo : tdonesymbolinfoproc = @def_donesymbolinfo;
  143. do_extractsymbolinfo : textractsymbolinfoproc = @def_extractsymbolinfo;
  144. needsymbolinfo : boolean =false;
  145. do_openinputfile : topeninputfilefunc = @def_openinputfile;
  146. do_getnamedfiletime : tgetnamedfiletimefunc = @def_getnamedfiletime;
  147. implementation
  148. uses
  149. cutils, systems, globals
  150. ;
  151. {****************************************************************************
  152. Helper Routines
  153. ****************************************************************************}
  154. function gccfilename(const s : string) : string;
  155. var
  156. i : longint;
  157. begin
  158. for i:=1to length(s) do
  159. begin
  160. case s[i] of
  161. '\' : gccfilename[i]:='/';
  162. 'A'..'Z' : if not (tf_files_case_aware in source_info.flags) and
  163. not (tf_files_case_sensitive in source_info.flags) then
  164. gccfilename[i]:=chr(ord(s[i])+32)
  165. else
  166. gccfilename[i]:=s[i];
  167. else
  168. gccfilename[i]:=s[i];
  169. end;
  170. end;
  171. gccfilename[0]:=s[0];
  172. end;
  173. function tostr(i : longint) : string;
  174. var
  175. hs : string;
  176. begin
  177. str(i,hs);
  178. tostr:=hs;
  179. end;
  180. {****************************************************************************
  181. Stopping the compiler
  182. ****************************************************************************}
  183. constructor EControlCAbort.Create;
  184. begin
  185. inherited Create('Ctrl-C Signaled!');
  186. end;
  187. constructor ECompilerAbort.Create;
  188. begin
  189. inherited Create('Compilation Aborted');
  190. end;
  191. constructor ECompilerAbortSilent.Create;
  192. begin
  193. inherited Create('Compilation Aborted');
  194. end;
  195. {****************************************************************************
  196. Predefined default Handlers
  197. ****************************************************************************}
  198. function def_status:boolean;
  199. var
  200. hstatus : TFPCHeapStatus;
  201. begin
  202. def_status:=false; { never stop }
  203. { Status info?, Called every line }
  204. if ((status.verbosity and V_Status)<>0) then
  205. begin
  206. if (status.compiledlines=1) or
  207. (status.currentline mod 100=0) then
  208. begin
  209. if status.currentline>0 then
  210. Write(status.currentline,' ');
  211. hstatus:=GetFPCHeapStatus;
  212. WriteLn(DStr(hstatus.CurrHeapUsed shr 10),'/',DStr(hstatus.CurrHeapSize shr 10),' Kb Used');
  213. end;
  214. end;
  215. {$ifdef macos}
  216. Yield;
  217. {$endif}
  218. end;
  219. Function def_comment(Level:Longint;const s:ansistring):boolean;
  220. const
  221. rh_errorstr = 'error:';
  222. rh_warningstr = 'warning:';
  223. var
  224. hs : ansistring;
  225. hs2 : ansistring;
  226. begin
  227. def_comment:=false; { never stop }
  228. hs:='';
  229. if not(status.use_gccoutput) then
  230. begin
  231. if (status.verbosity and Level)=V_Hint then
  232. hs:=hintstr;
  233. if (status.verbosity and Level)=V_Note then
  234. hs:=notestr;
  235. if (status.verbosity and Level)=V_Warning then
  236. hs:=warningstr;
  237. if (status.verbosity and Level)=V_Error then
  238. hs:=errorstr;
  239. if (status.verbosity and Level)=V_Fatal then
  240. hs:=fatalstr;
  241. if (status.verbosity and Level)=V_Used then
  242. hs:=PadSpace('('+status.currentmodule+')',10);
  243. end
  244. else
  245. begin
  246. if (status.verbosity and Level)=V_Hint then
  247. hs:=rh_warningstr;
  248. if (status.verbosity and Level)=V_Note then
  249. hs:=rh_warningstr;
  250. if (status.verbosity and Level)=V_Warning then
  251. hs:=rh_warningstr;
  252. if (status.verbosity and Level)=V_Error then
  253. hs:=rh_errorstr;
  254. if (status.verbosity and Level)=V_Fatal then
  255. hs:=rh_errorstr;
  256. end;
  257. { Generate line prefix }
  258. if ((Level and V_LineInfo)=V_LineInfo) and
  259. (status.currentsource<>'') and
  260. (status.currentline>0) then
  261. begin
  262. {$ifndef macos}
  263. { Adding the column should not confuse RHIDE,
  264. even if it does not yet use it PM
  265. but only if it is after error or warning !! PM }
  266. if status.currentcolumn>0 then
  267. begin
  268. if status.use_gccoutput then
  269. hs:=gccfilename(status.currentsource)+':'+tostr(status.currentline)+': '+hs+' '+
  270. tostr(status.currentcolumn)+': '+s
  271. else
  272. begin
  273. hs:=status.currentsource+'('+tostr(status.currentline)+
  274. ','+tostr(status.currentcolumn)+') '+hs+' '+s;
  275. end;
  276. if status.print_source_path then
  277. hs:=status.currentsourcepath+hs;
  278. end
  279. else
  280. begin
  281. if status.use_gccoutput then
  282. hs:=gccfilename(status.currentsource)+': '+hs+' '+tostr(status.currentline)+': '+s
  283. else
  284. hs:=status.currentsource+'('+tostr(status.currentline)+') '+hs+' '+s;
  285. end;
  286. {$else}
  287. {MPW style error}
  288. if status.currentcolumn>0 then
  289. hs:='File "'+status.currentsourcepath+status.currentsource+'"; Line '+tostr(status.currentline)+
  290. ' #[' + tostr(status.currentcolumn) + '] ' +hs+' '+s
  291. else
  292. hs:='File "'+status.currentsourcepath+status.currentsource+'"; Line '+tostr(status.currentline)+' # '+hs+' '+s;
  293. {$endif}
  294. end
  295. else
  296. begin
  297. if hs<>'' then
  298. hs:=hs+' '+s
  299. else
  300. hs:=s;
  301. end;
  302. if (status.verbosity and V_TimeStamps)<>0 then
  303. begin
  304. system.str(getrealtime-starttime:0:3,hs2);
  305. hs:='['+hs2+'] '+hs;
  306. end;
  307. { Display line }
  308. if (Level<>V_None) and
  309. ((status.verbosity and (Level and V_LevelMask))=(Level and V_LevelMask)) then
  310. begin
  311. if status.use_stderr then
  312. begin
  313. writeln(stderr,hs);
  314. flush(stderr);
  315. end
  316. else
  317. begin
  318. if status.use_redir then
  319. writeln(status.redirfile,hs)
  320. else
  321. writeln(hs);
  322. end;
  323. end;
  324. { include everything in the bugreport file }
  325. if status.use_bugreport then
  326. begin
  327. Write(status.reportbugfile,hexstr(level,8)+':');
  328. Writeln(status.reportbugfile,hs);
  329. end;
  330. end;
  331. function def_internalerror(i : longint) : boolean;
  332. begin
  333. do_comment(V_Fatal+V_LineInfo,'Internal error '+tostr(i));
  334. {$ifdef EXTDEBUG}
  335. { Internalerror() and def_internalerror() do not
  336. have a stackframe }
  337. dump_stack(stdout,get_caller_frame(get_frame));
  338. {$endif EXTDEBUG}
  339. def_internalerror:=true;
  340. end;
  341. function def_CheckVerbosity(v:longint):boolean;
  342. begin
  343. result:=status.use_bugreport or
  344. ((v<>V_None) and
  345. ((status.verbosity and (v and V_LevelMask))=(v and V_LevelMask)));
  346. end;
  347. procedure def_initsymbolinfo;
  348. begin
  349. end;
  350. procedure def_donesymbolinfo;
  351. begin
  352. end;
  353. procedure def_extractsymbolinfo;
  354. begin
  355. end;
  356. function def_openinputfile(const filename: TPathStr): tinputfile;
  357. begin
  358. def_openinputfile:=tdosinputfile.create(filename);
  359. end;
  360. Function def_GetNamedFileTime (Const F : TPathStr) : Longint;
  361. begin
  362. Result:=FileAge(F);
  363. end;
  364. end.