comphook.pas 13 KB

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