fpintf.pas 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. {
  2. This file is part of the Free Pascal Integrated Development Environment
  3. Copyright (c) 1998 by Berczi Gabor
  4. Misc routines for the IDE
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$i globdir.inc}
  12. unit FPIntf;
  13. {$mode objfpc}
  14. {$H-}
  15. interface
  16. { Run }
  17. function GetRunParameters: string;
  18. procedure SetRunParameters(const Params: string);
  19. function GetRunDir: string;
  20. procedure SetRunDir(const Params: string);
  21. { Compile }
  22. procedure Compile(const FileName, ConfigFile: string);
  23. procedure SetPrimaryFile(const fn:string);
  24. function LinkAfter : boolean;
  25. {$ifdef USE_EXTERNAL_COMPILER}
  26. function version_string : string;
  27. function full_version_string : string;
  28. {$endif USE_EXTERNAL_COMPILER}
  29. implementation
  30. uses
  31. Compiler,Comphook,
  32. sysutils,
  33. {$ifndef NODEBUG}
  34. FPDebug,
  35. {$endif NODEBUG}
  36. FPRedir,FPVars,FpCompil,
  37. FPUtils,FPSwitch,WUtils;
  38. {****************************************************************************
  39. Run
  40. ****************************************************************************}
  41. var
  42. RunDir,
  43. RunParameters : string;
  44. function LinkAfter : boolean;
  45. begin
  46. LinkAfter:=LinkAfterSwitches^.GetBooleanItem(0);
  47. end;
  48. function GetRunParameters: string;
  49. begin
  50. GetRunParameters:=RunParameters;
  51. end;
  52. procedure SetRunParameters(const Params: string);
  53. begin
  54. RunParameters:=Params;
  55. {$ifndef NODEBUG}
  56. If assigned(Debugger) then
  57. Debugger^.SetArgs(RunParameters);
  58. {$endif}
  59. end;
  60. function GetRunDir: string;
  61. begin
  62. GetRunDir:=RunDir;
  63. end;
  64. procedure SetRunDir(const Params: string);
  65. begin
  66. RunDir:=Params;
  67. {$ifndef NODEBUG}
  68. If assigned(Debugger) then
  69. Debugger^.SetDir(RunDir);
  70. {$endif}
  71. end;
  72. {****************************************************************************
  73. Compile
  74. ****************************************************************************}
  75. var
  76. CatchErrorLongJumpBuffer : jmp_buf;
  77. procedure CatchCompilationErrors;
  78. begin
  79. LongJmp(CatchErrorLongJumpBuffer,1);
  80. end;
  81. procedure Compile(const FileName, ConfigFile: string);
  82. var
  83. cmd : string;
  84. ExitReason : integer;
  85. ExitAddr,StoreExitProc : pointer;
  86. {$ifdef USE_EXTERNAL_COMPILER}
  87. CompilerOut : Text;
  88. CompilerOutputLine : longint;
  89. V,p,p1,p2,lineNb,ColumnNb : longint;
  90. error : word;
  91. ModuleName,Line : string;
  92. error_in_reading : boolean;
  93. {$endif USE_EXTERNAL_COMPILER}
  94. begin
  95. {$ifndef USE_EXTERNAL_COMPILER}
  96. cmd:='-d'+SwitchesModeStr[SwitchesMode];
  97. if ConfigFile<>'' then
  98. cmd:='['+ConfigFile+'] '+cmd;
  99. {$else USE_EXTERNAL_COMPILER}
  100. cmd:='-n -d'+SwitchesModeStr[SwitchesMode];
  101. if ConfigFile<>'' then
  102. cmd:='@'+ConfigFile+' '+cmd;
  103. if not UseExternalCompiler then
  104. {$endif USE_EXTERNAL_COMPILER}
  105. { Add the switches from the primary file }
  106. if PrimaryFileSwitches<>'' then
  107. cmd:=cmd+' '+PrimaryFileSwitches;
  108. cmd:=cmd+' '+FileName;
  109. { call the compiler }
  110. {$ifdef USE_EXTERNAL_COMPILER}
  111. if UseExternalCompiler then
  112. begin
  113. If not LocateExeFile(ExternalCompilerExe) then
  114. begin
  115. CompilerMessageWindow^.AddMessage(
  116. 0,ExternalCompilerExe+' not found','',0,0);
  117. exit;
  118. end;
  119. CompilerMessageWindow^.AddMessage(
  120. 0,'Running: '+ExternalCompilerExe+' '+cmd,'',0,0);
  121. if not ExecuteRedir(ExternalCompilerExe,cmd,'','ppc___.out','ppc___.err') then
  122. begin
  123. CompilerMessageWindow^.AddMessage(
  124. V_error,msg_errorinexternalcompilation,'',0,0);
  125. CompilerMessageWindow^.AddMessage(
  126. V_error,FormatStrInt(msg_iostatusis,IOStatus),'',0,0);
  127. CompilerMessageWindow^.AddMessage(
  128. V_error,FormatStrInt(msg_executeresultis,ExecuteResult),'',0,0);
  129. if IOStatus<>0 then
  130. exit;
  131. end;
  132. Assign(CompilerOut,'ppc___.out');
  133. Reset(CompilerOut);
  134. error_in_reading:=false;
  135. CompilerOutputLine:=0;
  136. While not eof(CompilerOut) do
  137. begin
  138. readln(CompilerOut,Line);
  139. Inc(CompilerOutputLine);
  140. p:=pos('(',line);
  141. if p>0 then
  142. begin
  143. ModuleName:=copy(Line,1,p-1);
  144. Line:=Copy(Line,p+1,255);
  145. p1:=pos(',',Line);
  146. val(copy(Line,1,p1-1),lineNb,error);
  147. Line:=Copy(Line,p1+1,255);
  148. p2:=pos(')',Line);
  149. if error=0 then
  150. val(copy(Line,1,p2-1),ColumnNb,error);
  151. Line:=Copy(Line,p2+1,255);
  152. V:=0;
  153. { using constants here isn't a good idea, because this won't
  154. work with localized versions of the compiler - Gabor }
  155. If Pos(' Error:',line)=1 then
  156. begin
  157. V:=V_error;
  158. Line:=Copy(Line,8,Length(Line));
  159. end
  160. else if Pos(' Fatal:',line)=1 then
  161. begin
  162. V:=V_fatal;
  163. Line:=Copy(Line,8,Length(Line));
  164. end
  165. else if Pos(' Hint:',line)=1 then
  166. begin
  167. V:=V_hint;
  168. Line:=Copy(Line,7,Length(Line));
  169. end
  170. else if Pos(' Note:',line)=1 then
  171. begin
  172. V:=V_note;
  173. Line:=Copy(Line,7,Length(Line));
  174. end;
  175. if error=0 then
  176. CompilerMessageWindow^.AddMessage(V,Line,ModuleName,LineNb,ColumnNb)
  177. else
  178. error_in_reading:=true;
  179. end
  180. else
  181. CompilerMessageWindow^.AddMessage(0,Line,'',0,0);
  182. ;
  183. end;
  184. Close(CompilerOut);
  185. end
  186. else
  187. {$endif USE_EXTERNAL_COMPILER}
  188. begin
  189. try
  190. Compiler.Compile(cmd);
  191. except
  192. on e : exception do
  193. begin
  194. CompilationPhase:=cpFailed;
  195. CompilerMessageWindow^.AddMessage(V_Error,
  196. 'Compiler exited','',0,0);
  197. CompilerMessageWindow^.AddMessage(V_Error,
  198. e.message,'',0,0);
  199. end;
  200. end;
  201. end;
  202. end;
  203. {$ifdef USE_EXTERNAL_COMPILER}
  204. function version_string : string;
  205. begin
  206. if not ExecuteRedir(ExternalCompilerExe,'-iV','','ppc___.out','ppc___.err') then
  207. version_string:=version.version_string
  208. else
  209. begin
  210. Assign(CompilerOut,'ppc___.out');
  211. Reset(CompilerOut);
  212. Readln(CompilerOut,s);
  213. Close(CompilerOut);
  214. version_string:=s;
  215. end;
  216. end;
  217. function full_version_string : string;
  218. begin
  219. if not ExecuteRedir(ExternalCompilerExe,'-iW','','ppc___.out','ppc___.err') then
  220. full_version_string:=version.full_version_string
  221. else
  222. begin
  223. Assign(CompilerOut,'ppc___.out');
  224. Reset(CompilerOut);
  225. Readln(CompilerOut,s);
  226. Close(CompilerOut);
  227. if Pos ('-iW', S) <> 0 then
  228. (* Unknown option - full version not supported! *)
  229. S := Version_String;
  230. full_version_string:=s;
  231. end;
  232. end;
  233. {$endif USE_EXTERNAL_COMPILER}
  234. procedure SetPrimaryFile(const fn:string);
  235. var
  236. t : text;
  237. begin
  238. PrimaryFile:='';
  239. PrimaryFileMain:='';
  240. PrimaryFileSwitches:='';
  241. PrimaryFilePara:='';
  242. if UpcaseStr(ExtOf(fn))='.PRI' then
  243. begin
  244. assign(t,fn);
  245. {$I-}
  246. reset(t);
  247. if ioresult=0 then
  248. begin
  249. PrimaryFile:=fn;
  250. readln(t,PrimaryFileMain);
  251. readln(t,PrimaryFileSwitches);
  252. readln(t,PrimaryFilePara);
  253. close(t);
  254. end;
  255. {$I+}
  256. EatIO;
  257. end
  258. else
  259. begin
  260. PrimaryFile:=fn;
  261. PrimaryFileMain:=fn;
  262. end;
  263. if PrimaryFilePara<>'' then
  264. SetRunParameters(PrimaryFilePara);
  265. end;
  266. end.