summaryrefslogtreecommitdiff
path: root/packages/ide/fpintf.pas
blob: b64127f914876d9969bdda8245eee65b65ed013e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
{
    This file is part of the Free Pascal Integrated Development Environment
    Copyright (c) 1998 by Berczi Gabor

    Misc routines for the IDE

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}
{$i globdir.inc}

unit FPIntf;

{$mode objfpc}

interface

{ Run }
function  GetRunParameters: string;
procedure SetRunParameters(const Params: string);
function GetRunDir: string;
procedure SetRunDir(const Params: string);

{ Compile }
procedure Compile(const FileName, ConfigFile: string);
procedure SetPrimaryFile(const fn:string);
function LinkAfter : boolean;
{$ifdef USE_EXTERNAL_COMPILER}
function version_string : string;
function full_version_string : string;
{$endif USE_EXTERNAL_COMPILER}


implementation

uses
  Compiler,Comphook,
  sysutils,
{$ifndef NODEBUG}
  FPDebug,
{$endif NODEBUG}
  FPRedir,FPVars,FpCompil,
  FPUtils,FPSwitch,WUtils;

{****************************************************************************
                                   Run
****************************************************************************}

var
  RunDir,
  RunParameters : string;

function LinkAfter : boolean;
begin
  LinkAfter:=LinkAfterSwitches^.GetBooleanItem(0);
end;

function GetRunParameters: string;
begin
  GetRunParameters:=RunParameters;
end;

procedure SetRunParameters(const Params: string);
begin
  RunParameters:=Params;
{$ifndef NODEBUG}
  If assigned(Debugger) then
    Debugger^.SetArgs(RunParameters);
{$endif}
end;

function GetRunDir: string;
begin
  GetRunDir:=RunDir;
end;

procedure SetRunDir(const Params: string);
begin
  RunDir:=Params;
{$ifndef NODEBUG}
  If assigned(Debugger) then
    Debugger^.SetDir(RunDir);
{$endif}
end;


{****************************************************************************
                                   Compile
****************************************************************************}

var
  CatchErrorLongJumpBuffer : jmp_buf;

procedure CatchCompilationErrors;
begin
  LongJmp(CatchErrorLongJumpBuffer,1);
end;

procedure Compile(const FileName, ConfigFile: string);
var
  cmd : string;
  ExitReason : integer;
  ExitAddr,StoreExitProc : pointer;
{$ifdef USE_EXTERNAL_COMPILER}
  CompilerOut : Text;
  CompilerOutputLine : longint;
  V,p,p1,p2,lineNb,ColumnNb : longint;
  error : word;
  ModuleName,Line : string;
  error_in_reading : boolean;
{$endif USE_EXTERNAL_COMPILER}
begin
{$ifndef USE_EXTERNAL_COMPILER}
  cmd:='-d'+SwitchesModeStr[SwitchesMode];
  if ConfigFile<>'' then
    cmd:='['+ConfigFile+'] '+cmd;
{$else USE_EXTERNAL_COMPILER}
  cmd:='-n -d'+SwitchesModeStr[SwitchesMode];
  if ConfigFile<>'' then
    cmd:='@'+ConfigFile+' '+cmd;
  if not UseExternalCompiler then
{$endif USE_EXTERNAL_COMPILER}
{ Add the switches from the primary file }
  if PrimaryFileSwitches<>'' then
    cmd:=cmd+' '+PrimaryFileSwitches;
  cmd:=cmd+' '+FileName;
{ call the compiler }
{$ifdef USE_EXTERNAL_COMPILER}
  if UseExternalCompiler then
    begin
      If not LocateExeFile(ExternalCompilerExe) then
        begin
          CompilerMessageWindow^.AddMessage(
            0,ExternalCompilerExe+' not found','',0,0);
          exit;
        end;
      CompilerMessageWindow^.AddMessage(
        0,'Running: '+ExternalCompilerExe+' '+cmd,'',0,0);
      if not ExecuteRedir(ExternalCompilerExe,cmd,'','ppc___.out','ppc___.err') then
        begin
          CompilerMessageWindow^.AddMessage(
            V_error,msg_errorinexternalcompilation,'',0,0);
          CompilerMessageWindow^.AddMessage(
            V_error,FormatStrInt(msg_iostatusis,IOStatus),'',0,0);
          CompilerMessageWindow^.AddMessage(
            V_error,FormatStrInt(msg_executeresultis,ExecuteResult),'',0,0);
          if IOStatus<>0 then
            exit;
        end;
      Assign(CompilerOut,'ppc___.out');
      Reset(CompilerOut);
      error_in_reading:=false;
      CompilerOutputLine:=0;
      While not eof(CompilerOut) do
        begin
          readln(CompilerOut,Line);
          Inc(CompilerOutputLine);
          p:=pos('(',line);
          if p>0 then
            begin
              ModuleName:=copy(Line,1,p-1);
              Line:=Copy(Line,p+1,255);
              p1:=pos(',',Line);
              val(copy(Line,1,p1-1),lineNb,error);
              Line:=Copy(Line,p1+1,255);
              p2:=pos(')',Line);
              if error=0 then
                val(copy(Line,1,p2-1),ColumnNb,error);
              Line:=Copy(Line,p2+1,255);
              V:=0;
              { using constants here isn't a good idea, because this won't
                work with localized versions of the compiler - Gabor }
              If Pos(' Error:',line)=1 then
                begin
                  V:=V_error;
                  Line:=Copy(Line,8,Length(Line));
                end
              else if Pos(' Fatal:',line)=1 then
                begin
                  V:=V_fatal;
                  Line:=Copy(Line,8,Length(Line));
                end
              else if Pos(' Hint:',line)=1 then
                begin
                  V:=V_hint;
                  Line:=Copy(Line,7,Length(Line));
                end
              else if Pos(' Note:',line)=1 then
                begin
                  V:=V_note;
                  Line:=Copy(Line,7,Length(Line));
                end;
              if error=0 then
                CompilerMessageWindow^.AddMessage(V,Line,ModuleName,LineNb,ColumnNb)
              else
                error_in_reading:=true;
            end
          else
            CompilerMessageWindow^.AddMessage(0,Line,'',0,0);
          ;
        end;
      Close(CompilerOut);
    end
  else
{$endif USE_EXTERNAL_COMPILER}
    begin
      try
          Compiler.Compile(cmd);
      except
          on e : exception do
            begin
              CompilationPhase:=cpFailed;
              CompilerMessageWindow^.AddMessage(V_Error,
                'Compiler exited','',0,0);
              CompilerMessageWindow^.AddMessage(V_Error,
                e.message,'',0,0);
            end;
      end;
    end;
end;

{$ifdef USE_EXTERNAL_COMPILER}
function version_string : string;
  begin
    if not ExecuteRedir(ExternalCompilerExe,'-iV','','ppc___.out','ppc___.err') then
      version_string:=version.version_string
    else
     begin
      Assign(CompilerOut,'ppc___.out');
      Reset(CompilerOut);
      Readln(CompilerOut,s);
      Close(CompilerOut);
      version_string:=s;
     end;
  end;

function full_version_string : string;
  begin
    if not ExecuteRedir(ExternalCompilerExe,'-iW','','ppc___.out','ppc___.err') then
      full_version_string:=version.full_version_string
    else
     begin
      Assign(CompilerOut,'ppc___.out');
      Reset(CompilerOut);
      Readln(CompilerOut,s);
      Close(CompilerOut);
      if Pos ('-iW', S) <> 0 then
(* Unknown option - full version not supported! *)
       S := Version_String;
      full_version_string:=s;
     end;
  end;
{$endif USE_EXTERNAL_COMPILER}

procedure SetPrimaryFile(const fn:string);
var
  t : text;
begin
  PrimaryFile:='';
  PrimaryFileMain:='';
  PrimaryFileSwitches:='';
  PrimaryFilePara:='';
  if UpcaseStr(ExtOf(fn))='.PRI' then
   begin
     assign(t,fn);
     {$I-}
     reset(t);
     if ioresult=0 then
      begin
        PrimaryFile:=fn;
        readln(t,PrimaryFileMain);
        readln(t,PrimaryFileSwitches);
        readln(t,PrimaryFilePara);
        close(t);
      end;
     {$I+}
     EatIO;
   end
  else
   begin
     PrimaryFile:=fn;
     PrimaryFileMain:=fn;
   end;
  if PrimaryFilePara<>'' then
   SetRunParameters(PrimaryFilePara);
end;



end.