fpc.pp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. exesuffix,
  142. processorname,
  143. ppcbin,
  144. targetname,
  145. processorstr : string;
  146. ppccommandline : array of ansistring;
  147. ppccommandlinelen : longint;
  148. i : longint;
  149. errorvalue : Longint;
  150. found : boolean;
  151. begin
  152. setlength(ppccommandline,paramcount);
  153. ppccommandlinelen:=0;
  154. cpusuffix :=''; // if not empty, signals attempt at cross
  155. // compiler.
  156. extrapath :='';
  157. {$ifdef i386}
  158. ppcbin:='ppc386';
  159. processorname:='i386';
  160. {$endif i386}
  161. {$ifdef m68k}
  162. ppcbin:='ppc68k';
  163. processorname:='m68k';
  164. {$endif m68k}
  165. {$ifdef powerpc}
  166. ppcbin:='ppcppc';
  167. processorname:='powerpc';
  168. {$endif powerpc}
  169. {$ifdef powerpc64}
  170. ppcbin:='ppcppc64';
  171. processorname:='powerpc64';
  172. {$endif powerpc64}
  173. {$ifdef arm}
  174. ppcbin:='ppcarm';
  175. processorname:='arm';
  176. {$endif arm}
  177. {$ifdef aarch64}
  178. ppcbin:='ppca64';
  179. processorname:='aarch64';
  180. {$endif aarch64}
  181. {$ifdef sparc}
  182. ppcbin:='ppcsparc';
  183. processorname:='sparc';
  184. {$endif sparc}
  185. {$ifdef sparc64}
  186. ppcbin:='ppcsparc64';
  187. processorname:='sparc64';
  188. {$endif sparc64}
  189. {$ifdef x86_64}
  190. ppcbin:='ppcx64';
  191. processorname:='x86_64';
  192. {$endif x86_64}
  193. {$ifdef mipsel}
  194. ppcbin:='ppcmipsel';
  195. processorname:='mipsel';
  196. {$else : not mipsel}
  197. {$ifdef mips}
  198. ppcbin:='ppcmips';
  199. processorname:='mips';
  200. {$endif mips}
  201. {$endif not mipsel}
  202. {$ifdef riscv32}
  203. ppcbin:='ppcrv32';
  204. processorname:='riscv32';
  205. {$endif riscv32}
  206. {$ifdef riscv64}
  207. ppcbin:='ppcrv64';
  208. processorname:='riscv64';
  209. {$endif riscv64}
  210. {$ifdef xtensa}
  211. ppcbin:='ppcxtensa';
  212. processorname:='xtensa';
  213. {$endif xtensa}
  214. {$ifdef wasm32}
  215. ppcbin:='ppcwasm32';
  216. processorname:='wasm32';
  217. {$endif wasm32}
  218. exesuffix:=''; { Default is just the name }
  219. if ParamCount = 0 then
  220. begin
  221. SetLength (PPCCommandLine, 1);
  222. PPCCommandLine [PPCCommandLineLen] := '-?F' + ParamStr (0);
  223. Inc (PPCCommandLineLen);
  224. end
  225. else
  226. for i:=1 to paramcount do
  227. begin
  228. s:=paramstr(i);
  229. if pos('-t',s)=1 then
  230. begin
  231. targetname:=copy(s,3,length(s)-2);
  232. ppccommandline[ppccommandlinelen]:=s;
  233. inc(ppccommandlinelen);
  234. end
  235. else if pos('-V',s)=1 then
  236. exesuffix:=copy(s,3,length(s)-2)
  237. else
  238. begin
  239. if pos('-P',s)=1 then
  240. begin
  241. processorstr:=copy(s,3,length(s)-2);
  242. { -PB is a special code that will show the
  243. default compiler and exit immediately. It's
  244. main usage is for Makefile }
  245. if processorstr='B' then
  246. begin
  247. { report the full name of the ppcbin }
  248. writeln(findcompiler(ppcbin,cpusuffix,exesuffix));
  249. halt(0);
  250. end
  251. { -PP is a special code that will show the
  252. processor and exit immediately. It's
  253. main usage is for Makefile }
  254. else if processorstr='P' then
  255. begin
  256. { report the processor }
  257. writeln(processorname);
  258. halt(0);
  259. end
  260. else
  261. if processorstr <> processorname then
  262. begin
  263. cpusuffix:=processortosuffix(processorstr);
  264. {$ifndef darwin}
  265. ppcbin:='ppcross'+cpusuffix;
  266. {$else not darwin}
  267. { the mach-o format supports "fat" binaries whereby }
  268. { a single executable contains machine code for }
  269. { several architectures -> it is counter-intuitive }
  270. { and non-standard to use different binary names }
  271. { for cross-compilers vs. native compilers }
  272. ppcbin:='ppc'+cpusuffix;
  273. {$endif not darwin}
  274. end;
  275. end
  276. else if pos('-Xp',s)=1 then
  277. extrapath:=copy(s,4,length(s)-3)
  278. else
  279. begin
  280. if pos('-h',s)=1 then
  281. ppccommandline[ppccommandlinelen] := '-hF' + ParamStr (0)
  282. else if pos('-?',s)=1 then
  283. ppccommandline[ppccommandlinelen] := '-?F' + ParamStr (0)
  284. else
  285. ppccommandline[ppccommandlinelen]:=s;
  286. inc(ppccommandlinelen);
  287. end;
  288. end;
  289. end;
  290. SetLength(ppccommandline,ppccommandlinelen);
  291. ppcbin:=findcompiler(ppcbin,cpusuffix,exesuffix);
  292. { call ppcXXX }
  293. try
  294. errorvalue:=ExecuteProcess(ppcbin,ppccommandline);
  295. except
  296. on e : exception do
  297. error(ppcbin+' can''t be executed, error message: '+e.message);
  298. end;
  299. if (errorvalue<>0) and
  300. (paramcount<>0) then
  301. error(ppcbin+' returned an error exitcode');
  302. halt(errorvalue);
  303. end.