summaryrefslogtreecommitdiff
path: root/compiler/utils/fpc.pp
blob: 7423b4a3e17bfe7bdd4b4e7946ed7d1b477510b9 (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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
{
    Copyright (c) 2000-2002 by Florian Klaempfl

    This file is the "loader" for the Free Pascal compiler

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    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.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

 ****************************************************************************}
program fpc;

{$mode objfpc}{$H+}

  uses
     Sysutils;

  const
{$ifdef UNIX}
    exeext='';
{$else UNIX}
  {$ifdef HASAMIGA}
    exeext='';
  {$else}
    {$ifdef NETWARE}
      exeext='.nlm';
    {$else}
      {$ifdef ATARI}
        exeext='.ttp';
      {$else}
        exeext='.exe';
      {$endif ATARI}
    {$endif NETWARE}
  {$endif HASAMIGA}
{$endif UNIX}


  procedure error(const s : string);
    begin
       writeln('Error: ',s);
       halt(1);
    end;


  function SplitPath(Const HStr:String):String;
    var
      i : longint;
    begin
      i:=Length(Hstr);
      while (i>0) and not(Hstr[i] in ['\','/']) do
       dec(i);
      SplitPath:=Copy(Hstr,1,i);
    end;


  function FileExists ( Const F : String) : Boolean;
    var
      Info : TSearchRec;
    begin
      FileExists:= findfirst(F,fareadonly+faarchive+fahidden,info)=0;
      findclose(Info);
    end;

  var
    extrapath : ansistring;

  function findexe(var ppcbin:string): boolean;
    var
      path : string;
    begin
      { add .exe extension }
      findexe:=false;
      ppcbin:=ppcbin+exeext;

      if (extrapath<>'') and (extrapath[length(extrapath)]<>DirectorySeparator) then
        extrapath:=extrapath+DirectorySeparator;
      { get path of fpc.exe }
      path:=splitpath(paramstr(0));
      { don't try with an empty extra patch, this might have strange results
        if the current directory contains a compiler
      }
      if (extrapath<>'') and FileExists(extrapath+ppcbin) then
       begin
         ppcbin:=extrapath+ppcbin;
         findexe:=true;
       end
      else if (path<>'') and FileExists(path+ppcbin) then
       begin
         ppcbin:=path+ppcbin;
         findexe:=true;
       end
      else
       begin
         path:=ExeSearch(ppcbin,getenvironmentvariable('PATH'));
         if path<>'' then
          begin
            ppcbin:=path;
            findexe:=true;
          end
       end;
    end;

  var
     s              : ansistring;
     cpusuffix,
     processorname,
     ppcbin,
     versionStr,
     processorstr   : string;
     ppccommandline : array of ansistring;
     ppccommandlinelen : longint;
     i : longint;
     errorvalue     : Longint;
  begin
     setlength(ppccommandline,paramcount);
     ppccommandlinelen:=0;
     cpusuffix     :='';        // if not empty, signals attempt at cross
                                // compiler.
     extrapath     :='';
{$ifdef i386}
     ppcbin:='ppc386';
     processorname:='i386';
{$endif i386}
{$ifdef m68k}
     ppcbin:='ppc68k';
     processorname:='m68k';
{$endif m68k}
{$ifdef powerpc}
     ppcbin:='ppcppc';
     processorname:='powerpc';
{$endif powerpc}
{$ifdef powerpc64}
     ppcbin:='ppcppc64';
     processorname:='powerpc64';
{$endif powerpc64}
{$ifdef arm}
     ppcbin:='ppcarm';
     processorname:='arm';
{$endif arm}
{$ifdef aarch64}
     ppcbin:='ppca64';
     processorname:='aarch64';
{$endif aarch64}
{$ifdef sparc}
     ppcbin:='ppcsparc';
     processorname:='sparc';
{$endif sparc}
{$ifdef sparc64}
     ppcbin:='ppcsparc64';
     processorname:='sparc64';
{$endif sparc64}
{$ifdef x86_64}
     ppcbin:='ppcx64';
     processorname:='x86_64';
{$endif x86_64}
{$ifdef mipsel}
     ppcbin:='ppcmipsel';
     processorname:='mipsel';
{$else : not mipsel}
  {$ifdef mips}
     ppcbin:='ppcmips';
     processorname:='mips';
  {$endif mips}
{$endif not mipsel}
{$ifdef riscv32}
     ppcbin:='ppcrv32';
     processorname:='riscv32';
{$endif riscv32}
{$ifdef riscv64}
     ppcbin:='ppcrv64';
     processorname:='riscv64';
{$endif riscv64}
{$ifdef xtensa}
     ppcbin:='ppcxtensa';
     processorname:='xtensa';
{$endif xtensa}
{$ifdef wasm32}
     ppcbin:='ppcwasm32';
     processorname:='wasm32';
{$endif wasm32}
     versionstr:='';                      { Default is just the name }
     if ParamCount = 0 then
       begin
         SetLength (PPCCommandLine, 1);
         PPCCommandLine [PPCCommandLineLen] := '-?F' + ParamStr (0);
         Inc (PPCCommandLineLen);
       end
     else
      for i:=1 to paramcount do
       begin
          s:=paramstr(i);
          if pos('-V',s)=1 then
              versionstr:=copy(s,3,length(s)-2)
          else
            begin
              if pos('-P',s)=1 then
                 begin
                   processorstr:=copy(s,3,length(s)-2);
                  { -PB is a special code that will show the
                    default compiler and exit immediately. It's
                     main usage is for Makefile }
                   if processorstr='B' then
                     begin
                       { report the full name of the ppcbin }
                       if versionstr<>'' then
                         ppcbin:=ppcbin+'-'+versionstr;
                       if not findexe(ppcbin) then
                         begin
                           if cpusuffix<>'' Then
                             begin
                               ppcbin:='ppc'+cpusuffix;
                               if versionstr<>'' then
                                 ppcbin:=ppcbin+'-'+versionstr;
                               findexe(ppcbin);
                             end;
                         end;
                       writeln(ppcbin);
                       halt(0);
                     end
                     { -PP is a special code that will show the
                       processor and exit immediately. It's
                       main usage is for Makefile }
                     else if processorstr='P' then
                      begin
                        { report the processor }
                        writeln(processorname);
                        halt(0);
                      end
                     else
                       if processorstr <> processorname then
                         begin
                           if processorstr='aarch64' then
                             cpusuffix:='a64'
                           else if processorstr='arm' then
                             cpusuffix:='arm'
                           else if processorstr='avr' then
                             cpusuffix:='avr'
                           else if processorstr='i386' then
                             cpusuffix:='386'
                           else if processorstr='i8086' then
                             cpusuffix:='8086'
                           else if processorstr='jvm' then
                             cpusuffix:='jvm'
                           else if processorstr='m68k' then
                             cpusuffix:='68k'
                           else if processorstr='mips' then
                             cpusuffix:='mips'
                           else if processorstr='mipsel' then
                             cpusuffix:='mipsel'
                           else if processorstr='powerpc' then
                             cpusuffix:='ppc'
                           else if processorstr='powerpc64' then
                             cpusuffix:='ppc64'
                           else if processorstr='riscv32' then
                             cpusuffix:='rv32'
                           else if processorstr='riscv64' then
                             cpusuffix:='rv64'
                           else if processorstr='sparc' then
                             cpusuffix:='sparc'
                           else if processorstr='sparc64' then
                             cpusuffix:='sparc64'
                           else if processorstr='x86_64' then
                             cpusuffix:='x64'
                           else if processorstr='xtensa' then
                             cpusuffix:='xtensa'
                           else if processorstr='z80' then
                             cpusuffix:='z80'
                           else if processorstr='wasm32' then
                             cpusuffix:='wasm32'
                           else
                             error('Illegal processor type "'+processorstr+'"');

{$ifndef darwin}
                           ppcbin:='ppcross'+cpusuffix;
{$else not darwin}
                           { the mach-o format supports "fat" binaries whereby }
                           { a single executable contains machine code for     }
                           { several architectures -> it is counter-intuitive  }
                           { and non-standard to use different binary names    }
                           { for cross-compilers vs. native compilers          }
                           ppcbin:='ppc'+cpusuffix;
{$endif not darwin}
                         end;
                 end
              else if pos('-Xp',s)=1 then
                extrapath:=copy(s,4,length(s)-3)
              else
                begin
                  if pos('-h',s)=1 then
                    ppccommandline[ppccommandlinelen] := '-hF' + ParamStr (0)
                  else if pos('-?',s)=1 then
                    ppccommandline[ppccommandlinelen] := '-?F' + ParamStr (0)
                  else
                    ppccommandline[ppccommandlinelen]:=s;
                  inc(ppccommandlinelen);
                end;
            end;
       end;
     SetLength(ppccommandline,ppccommandlinelen);

     if versionstr<>'' then
       ppcbin:=ppcbin+'-'+versionstr;
     { find the full path to the specified exe }
     if not findexe(ppcbin) then
        begin
          if cpusuffix<>'' Then
            begin
              ppcbin:='ppc'+cpusuffix;
              if versionstr<>'' then
                ppcbin:=ppcbin+'-'+versionstr;
              findexe(ppcbin);
            end;
        end;

     { call ppcXXX }
     try
       errorvalue:=ExecuteProcess(ppcbin,ppccommandline);
     except
       on e : exception do
         error(ppcbin+' can''t be executed, error message: '+e.message);
     end;
     if (errorvalue<>0) and
        (paramcount<>0) then
       error(ppcbin+' returned an error exitcode');
     halt(errorvalue);
  end.