fpc.pp 9.2 KB

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