comphook.pas 14 KB

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