fpc.pp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. This file is the "loader" for the Free Pascal compiler
  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. program fpc;
  17. {$mode objfpc}{$H+}
  18. uses
  19. Sysutils;
  20. const
  21. {$ifdef UNIX}
  22. exeext='';
  23. {$else UNIX}
  24. {$ifdef HASAMIGA}
  25. exeext='';
  26. {$else}
  27. {$ifdef NETWARE}
  28. exeext='.nlm';
  29. {$else}
  30. {$ifdef ATARI}
  31. exeext='.ttp';
  32. {$else}
  33. exeext='.exe';
  34. {$endif ATARI}
  35. {$endif NETWARE}
  36. {$endif HASAMIGA}
  37. {$endif UNIX}
  38. procedure error(const s : string);
  39. begin
  40. writeln('Error: ',s);
  41. halt(1);
  42. end;
  43. function SplitPath(Const HStr:String):String;
  44. var
  45. i : longint;
  46. begin
  47. i:=Length(Hstr);
  48. while (i>0) and not(Hstr[i] in ['\','/']) do
  49. dec(i);
  50. SplitPath:=Copy(Hstr,1,i);
  51. end;
  52. function FileExists ( Const F : String) : Boolean;
  53. var
  54. Info : TSearchRec;
  55. begin
  56. FileExists:= findfirst(F,fareadonly+faarchive+fahidden,info)=0;
  57. findclose(Info);
  58. end;
  59. var
  60. extrapath : ansistring;
  61. function findexe(var ppcbin:string): boolean;
  62. var
  63. path : string;
  64. begin
  65. { add .exe extension }
  66. findexe:=false;
  67. ppcbin:=ppcbin+exeext;
  68. if (extrapath<>'') and (extrapath[length(extrapath)]<>DirectorySeparator) then
  69. extrapath:=extrapath+DirectorySeparator;
  70. { get path of fpc.exe }
  71. path:=splitpath(paramstr(0));
  72. { don't try with an empty extra patch, this might have strange results
  73. if the current directory contains a compiler
  74. }
  75. if (extrapath<>'') and FileExists(extrapath+ppcbin) then
  76. begin
  77. ppcbin:=extrapath+ppcbin;
  78. findexe:=true;
  79. end
  80. else if FileExists(path+ppcbin) then
  81. begin
  82. ppcbin:=path+ppcbin;
  83. findexe:=true;
  84. end
  85. else
  86. begin
  87. path:=ExeSearch(ppcbin,getenvironmentvariable('PATH'));
  88. if path<>'' then
  89. begin
  90. ppcbin:=path;
  91. findexe:=true;
  92. end
  93. end;
  94. end;
  95. var
  96. s : ansistring;
  97. cpusuffix,
  98. processorname,
  99. ppcbin,
  100. versionStr,
  101. processorstr : string;
  102. ppccommandline : array of ansistring;
  103. ppccommandlinelen : longint;
  104. i : longint;
  105. errorvalue : Longint;
  106. begin
  107. setlength(ppccommandline,paramcount);
  108. ppccommandlinelen:=0;
  109. cpusuffix :=''; // if not empty, signals attempt at cross
  110. // compiler.
  111. extrapath :='';
  112. {$ifdef i386}
  113. ppcbin:='ppc386';
  114. processorname:='i386';
  115. {$endif i386}
  116. {$ifdef m68k}
  117. ppcbin:='ppc68k';
  118. processorname:='m68k';
  119. {$endif m68k}
  120. {$ifdef powerpc}
  121. ppcbin:='ppcppc';
  122. processorname:='powerpc';
  123. {$endif powerpc}
  124. {$ifdef powerpc64}
  125. ppcbin:='ppcppc64';
  126. processorname:='powerpc64';
  127. {$endif powerpc64}
  128. {$ifdef arm}
  129. ppcbin:='ppcarm';
  130. processorname:='arm';
  131. {$endif arm}
  132. {$ifdef aarch64}
  133. ppcbin:='ppca64';
  134. processorname:='aarch64';
  135. {$endif aarch64}
  136. {$ifdef sparc}
  137. ppcbin:='ppcsparc';
  138. processorname:='sparc';
  139. {$endif sparc}
  140. {$ifdef sparc64}
  141. ppcbin:='ppcsparc64';
  142. processorname:='sparc64';
  143. {$endif sparc64}
  144. {$ifdef x86_64}
  145. ppcbin:='ppcx64';
  146. processorname:='x86_64';
  147. {$endif x86_64}
  148. {$ifdef mipsel}
  149. ppcbin:='ppcmipsel';
  150. processorname:='mipsel';
  151. {$else : not mipsel}
  152. {$ifdef mips}
  153. ppcbin:='ppcmips';
  154. processorname:='mips';
  155. {$endif mips}
  156. {$endif not mipsel}
  157. {$ifdef riscv32}
  158. ppcbin:='ppcrv32';
  159. processorname:='riscv32';
  160. {$endif riscv32}
  161. {$ifdef riscv64}
  162. ppcbin:='ppcrv64';
  163. processorname:='riscv64';
  164. {$endif riscv64}
  165. versionstr:=''; { Default is just the name }
  166. if ParamCount = 0 then
  167. begin
  168. SetLength (PPCCommandLine, 1);
  169. PPCCommandLine [PPCCommandLineLen] := '-?F' + ParamStr (0);
  170. Inc (PPCCommandLineLen);
  171. end
  172. else
  173. for i:=1 to paramcount do
  174. begin
  175. s:=paramstr(i);
  176. if pos('-V',s)=1 then
  177. versionstr:=copy(s,3,length(s)-2)
  178. else
  179. begin
  180. if pos('-P',s)=1 then
  181. begin
  182. processorstr:=copy(s,3,length(s)-2);
  183. { -PB is a special code that will show the
  184. default compiler and exit immediately. It's
  185. main usage is for Makefile }
  186. if processorstr='B' then
  187. begin
  188. { report the full name of the ppcbin }
  189. if versionstr<>'' then
  190. ppcbin:=ppcbin+'-'+versionstr;
  191. if not findexe(ppcbin) then
  192. begin
  193. if cpusuffix<>'' Then
  194. begin
  195. ppcbin:='ppc'+cpusuffix;
  196. if versionstr<>'' then
  197. ppcbin:=ppcbin+'-'+versionstr;
  198. findexe(ppcbin);
  199. end;
  200. end;
  201. writeln(ppcbin);
  202. halt(0);
  203. end
  204. { -PP is a special code that will show the
  205. processor and exit immediately. It's
  206. main usage is for Makefile }
  207. else if processorstr='P' then
  208. begin
  209. { report the processor }
  210. writeln(processorname);
  211. halt(0);
  212. end
  213. else
  214. if processorstr <> processorname then
  215. begin
  216. if processorstr='aarch64' then
  217. cpusuffix:='a64'
  218. else if processorstr='arm' then
  219. cpusuffix:='arm'
  220. else if processorstr='avr' then
  221. cpusuffix:='avr'
  222. else if processorstr='i386' then
  223. cpusuffix:='386'
  224. else if processorstr='i8086' then
  225. cpusuffix:='8086'
  226. else if processorstr='jvm' then
  227. cpusuffix:='jvm'
  228. else if processorstr='m68k' then
  229. cpusuffix:='68k'
  230. else if processorstr='mips' then
  231. cpusuffix:='mips'
  232. else if processorstr='mipsel' then
  233. cpusuffix:='mipsel'
  234. else if processorstr='powerpc' then
  235. cpusuffix:='ppc'
  236. else if processorstr='powerpc64' then
  237. cpusuffix:='ppc64'
  238. else if processorstr='riscv32' then
  239. cpusuffix:='rv32'
  240. else if processorstr='riscv64' then
  241. cpusuffix:='rv64'
  242. else if processorstr='sparc' then
  243. cpusuffix:='sparc'
  244. else if processorstr='sparc64' then
  245. cpusuffix:='sparc64'
  246. else if processorstr='x86_64' then
  247. cpusuffix:='x64'
  248. else
  249. error('Illegal processor type "'+processorstr+'"');
  250. {$ifndef darwin}
  251. ppcbin:='ppcross'+cpusuffix;
  252. {$else not darwin}
  253. { the mach-o format supports "fat" binaries whereby }
  254. { a single executable contains machine code for }
  255. { several architectures -> it is counter-intuitive }
  256. { and non-standard to use different binary names }
  257. { for cross-compilers vs. native compilers }
  258. ppcbin:='ppc'+cpusuffix;
  259. {$endif not darwin}
  260. end;
  261. end
  262. else if pos('-Xp',s)=1 then
  263. extrapath:=copy(s,4,length(s)-3)
  264. else
  265. begin
  266. if pos('-h',s)=1 then
  267. ppccommandline[ppccommandlinelen] := '-hF' + ParamStr (0)
  268. else if pos('-?',s)=1 then
  269. ppccommandline[ppccommandlinelen] := '-?F' + ParamStr (0)
  270. else
  271. ppccommandline[ppccommandlinelen]:=s;
  272. inc(ppccommandlinelen);
  273. end;
  274. end;
  275. end;
  276. SetLength(ppccommandline,ppccommandlinelen);
  277. if versionstr<>'' then
  278. ppcbin:=ppcbin+'-'+versionstr;
  279. { find the full path to the specified exe }
  280. if not findexe(ppcbin) then
  281. begin
  282. if cpusuffix<>'' Then
  283. begin
  284. ppcbin:='ppc'+cpusuffix;
  285. if versionstr<>'' then
  286. ppcbin:=ppcbin+'-'+versionstr;
  287. findexe(ppcbin);
  288. end;
  289. end;
  290. { call ppcXXX }
  291. try
  292. errorvalue:=ExecuteProcess(ppcbin,ppccommandline);
  293. except
  294. on e : exception do
  295. error(ppcbin+' can''t be executed, error message: '+e.message);
  296. end;
  297. if (errorvalue<>0) and
  298. (paramcount<>0) then
  299. error(ppcbin+' returned an error exitcode');
  300. halt(errorvalue);
  301. end.