fpc.pp 9.1 KB

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