fpc.pp 9.3 KB

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