comphook.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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
  153. {$ifdef linux}
  154. ,termio
  155. {$endif linux}
  156. ;
  157. {****************************************************************************
  158. Helper Routines
  159. ****************************************************************************}
  160. function gccfilename(const s : string) : string;
  161. var
  162. i : longint;
  163. begin
  164. for i:=1to length(s) do
  165. begin
  166. case s[i] of
  167. '\' : gccfilename[i]:='/';
  168. 'A'..'Z' : if not (tf_files_case_aware in source_info.flags) and
  169. not (tf_files_case_sensitive in source_info.flags) then
  170. gccfilename[i]:=chr(ord(s[i])+32)
  171. else
  172. gccfilename[i]:=s[i];
  173. else
  174. gccfilename[i]:=s[i];
  175. end;
  176. end;
  177. gccfilename[0]:=s[0];
  178. end;
  179. function tostr(i : longint) : string;
  180. var
  181. hs : string;
  182. begin
  183. str(i,hs);
  184. tostr:=hs;
  185. end;
  186. type
  187. TOutputColor = (oc_black,oc_red,oc_green,oc_orange,og_blue,oc_magenta,oc_cyan,oc_lightgray);
  188. {$ifdef linux}
  189. const
  190. CachedIsATTY : Boolean = false;
  191. IsATTYValue : Boolean = false;
  192. function IsATTY(var t : text) : Boolean;
  193. begin
  194. if not(CachedIsATTY) then
  195. begin
  196. IsATTYValue:=termio.IsATTY(t)=1;
  197. CachedIsATTY:=true;
  198. end;
  199. Result:=IsATTYValue;
  200. end;
  201. {$endif linux}
  202. procedure WriteColoredOutput(var t: Text;color: TOutputColor;const s : AnsiString);
  203. begin
  204. {$ifdef linux}
  205. if IsATTY(t) then
  206. begin
  207. case color of
  208. oc_black:
  209. write(t,#27'[1m'#27'[30m');
  210. oc_red:
  211. write(t,#27'[1m'#27'[31m');
  212. oc_green:
  213. write(t,#27'[1m'#27'[32m');
  214. oc_orange:
  215. write(t,#27'[1m'#27'[33m');
  216. og_blue:
  217. write(t,#27'[1m'#27'[34m');
  218. oc_magenta:
  219. write(t,#27'[1m'#27'[35m');
  220. oc_cyan:
  221. write(t,#27'[1m'#27'[36m');
  222. oc_lightgray:
  223. write(t,#27'[1m'#27'[37m');
  224. end;
  225. end;
  226. {$endif linux}
  227. write(t,s);
  228. {$ifdef linux}
  229. if IsATTY(t) then
  230. write(t,#27'[0m');
  231. {$endif linux}
  232. end;
  233. {****************************************************************************
  234. Stopping the compiler
  235. ****************************************************************************}
  236. constructor EControlCAbort.Create;
  237. begin
  238. inherited Create('Ctrl-C Signaled!');
  239. end;
  240. constructor ECompilerAbort.Create;
  241. begin
  242. inherited Create('Compilation Aborted');
  243. end;
  244. constructor ECompilerAbortSilent.Create;
  245. begin
  246. inherited Create('Compilation Aborted');
  247. end;
  248. {****************************************************************************
  249. Predefined default Handlers
  250. ****************************************************************************}
  251. function def_status:boolean;
  252. var
  253. hstatus : TFPCHeapStatus;
  254. begin
  255. def_status:=false; { never stop }
  256. { Status info?, Called every line }
  257. if ((status.verbosity and V_Status)<>0) then
  258. begin
  259. if (status.compiledlines=1) or
  260. (status.currentline mod 100=0) then
  261. begin
  262. if status.currentline>0 then
  263. Write(status.currentline,' ');
  264. hstatus:=GetFPCHeapStatus;
  265. WriteLn(DStr(hstatus.CurrHeapUsed shr 10),'/',DStr(hstatus.CurrHeapSize shr 10),' Kb Used');
  266. end;
  267. end;
  268. {$ifdef macos}
  269. Yield;
  270. {$endif}
  271. end;
  272. Function def_comment(Level:Longint;const s:ansistring):boolean;
  273. const
  274. rh_errorstr = 'error:';
  275. rh_warningstr = 'warning:';
  276. procedure WriteMsgTypeColored(var t : text;const s : AnsiString);
  277. begin
  278. case (status.verbosity and Level) of
  279. V_Warning:
  280. WriteColoredOutput(t,oc_magenta,s);
  281. V_Error,
  282. V_Fatal:
  283. WriteColoredOutput(t,oc_red,s);
  284. else
  285. write(t,s);
  286. end;
  287. end;
  288. var
  289. hs2,
  290. MsgTypeStr,
  291. MsgLocStr,
  292. MsgTimeStr: AnsiString;
  293. begin
  294. def_comment:=false; { never stop }
  295. MsgTypeStr:='';
  296. MsgLocStr:='';
  297. MsgTimeStr:='';
  298. if not(status.use_gccoutput) then
  299. begin
  300. if (status.verbosity and Level)=V_Hint then
  301. MsgTypeStr:=hintstr;
  302. if (status.verbosity and Level)=V_Note then
  303. MsgTypeStr:=notestr;
  304. if (status.verbosity and Level)=V_Warning then
  305. MsgTypeStr:=warningstr;
  306. if (status.verbosity and Level)=V_Error then
  307. MsgTypeStr:=errorstr;
  308. if (status.verbosity and Level)=V_Fatal then
  309. MsgTypeStr:=fatalstr;
  310. if (status.verbosity and Level)=V_Used then
  311. MsgTypeStr:=PadSpace('('+status.currentmodule+')',10);
  312. end
  313. else
  314. begin
  315. if (status.verbosity and Level)=V_Hint then
  316. MsgTypeStr:=rh_warningstr;
  317. if (status.verbosity and Level)=V_Note then
  318. MsgTypeStr:=rh_warningstr;
  319. if (status.verbosity and Level)=V_Warning then
  320. MsgTypeStr:=rh_warningstr;
  321. if (status.verbosity and Level)=V_Error then
  322. MsgTypeStr:=rh_errorstr;
  323. if (status.verbosity and Level)=V_Fatal then
  324. MsgTypeStr:=rh_errorstr;
  325. end;
  326. { Generate line prefix }
  327. if ((Level and V_LineInfo)=V_LineInfo) and
  328. (status.currentsource<>'') and
  329. (status.currentline>0) then
  330. begin
  331. {$ifndef macos}
  332. { Adding the column should not confuse RHIDE,
  333. even if it does not yet use it PM
  334. but only if it is after error or warning !! PM }
  335. if status.currentcolumn>0 then
  336. begin
  337. if status.use_gccoutput then
  338. MsgLocStr:=gccfilename(status.currentsource)+':'+tostr(status.currentline)+':'+tostr(status.currentcolumn)+':'
  339. else
  340. MsgLocStr:=status.currentsource+'('+tostr(status.currentline)+','+tostr(status.currentcolumn)+')';
  341. if status.print_source_path then
  342. if status.sources_avail then
  343. MsgLocStr:=status.currentsourcepath+MsgLocStr
  344. else
  345. MsgLocStr:=status.currentsourceppufilename+':'+MsgLocStr;
  346. end
  347. else
  348. begin
  349. if status.use_gccoutput then
  350. MsgLocStr:=gccfilename(status.currentsource)+':'+tostr(status.currentline)+':'
  351. else
  352. MsgLocStr:=status.currentsource+'('+tostr(status.currentline)+')';
  353. end;
  354. {$else macos}
  355. { MPW style error }
  356. if status.currentcolumn>0 then
  357. MsgLocStr:='File "'+status.currentsourcepath+status.currentsource+'"; Line '+tostr(status.currentline)+' #[' + tostr(status.currentcolumn) + ']'
  358. else
  359. MsgLocStr:='File "'+status.currentsourcepath+status.currentsource+'"; Line '+tostr(status.currentline)+' # ';
  360. {$endif macos}
  361. end;
  362. if MsgLocStr<>'' then
  363. MsgLocStr:=MsgLocStr+' ';
  364. if MsgTypeStr<>'' then
  365. MsgTypeStr:=MsgTypeStr+' ';
  366. if (status.verbosity and V_TimeStamps)<>0 then
  367. begin
  368. system.str(getrealtime-starttime:0:3,hs2);
  369. MsgTimeStr:='['+hs2+'] ';
  370. end;
  371. { Display line }
  372. if (Level<>V_None) and
  373. ((status.verbosity and (Level and V_LevelMask))=(Level and V_LevelMask)) then
  374. begin
  375. if status.use_stderr then
  376. begin
  377. write(StdErr,MsgTimeStr+MsgLocStr);
  378. WriteMsgTypeColored(StdErr,MsgTypeStr);
  379. writeln(StdErr,s);
  380. flush(StdErr);
  381. end
  382. else
  383. begin
  384. if status.use_redir then
  385. writeln(status.redirfile,MsgTimeStr+MsgLocStr+MsgTypeStr+s)
  386. else
  387. begin
  388. write(MsgTimeStr+MsgLocStr);
  389. WriteMsgTypeColored(Output,MsgTypeStr);
  390. writeln(s);
  391. end;
  392. end;
  393. end;
  394. { include everything in the bugreport file }
  395. if status.use_bugreport then
  396. begin
  397. Write(status.reportbugfile,hexstr(level,8)+':');
  398. Writeln(status.reportbugfile,MsgTimeStr+MsgLocStr+MsgTypeStr+s);
  399. end;
  400. end;
  401. function def_internalerror(i : longint) : boolean;
  402. begin
  403. do_comment(V_Fatal+V_LineInfo,'Internal error '+tostr(i));
  404. {$ifdef EXTDEBUG}
  405. { Internalerror() and def_internalerror() do not
  406. have a stackframe }
  407. dump_stack(stdout,get_caller_frame(get_frame));
  408. {$endif EXTDEBUG}
  409. def_internalerror:=true;
  410. end;
  411. function def_CheckVerbosity(v:longint):boolean;
  412. begin
  413. result:=status.use_bugreport or
  414. ((v<>V_None) and
  415. ((status.verbosity and (v and V_LevelMask))=(v and V_LevelMask)));
  416. end;
  417. procedure def_initsymbolinfo;
  418. begin
  419. end;
  420. procedure def_donesymbolinfo;
  421. begin
  422. end;
  423. procedure def_extractsymbolinfo;
  424. begin
  425. end;
  426. function def_openinputfile(const filename: TPathStr): tinputfile;
  427. begin
  428. def_openinputfile:=tdosinputfile.create(filename);
  429. end;
  430. Function def_GetNamedFileTime (Const F : TPathStr) : Longint;
  431. begin
  432. Result:=FileAge(F);
  433. end;
  434. end.