comphook.pas 11 KB

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