comphook.pas 11 KB

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