comphook.pas 10 KB

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