fpc.pp 9.1 KB

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