fpc.pp 8.7 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, exesuffix, SourceCPU, ppcbin, targetname, TargetCPU: string;
  237. ppccommandline: array of ansistring;
  238. ppccommandlinelen: longint;
  239. i: longint;
  240. errorvalue: longint;
  241. procedure AddToCommandLine(S: string);
  242. begin
  243. PPCCommandLine[PPCCommandLineLen] := S;
  244. Inc(PPCCommandLineLen);
  245. end;
  246. begin
  247. ppccommandline := [];
  248. setlength(ppccommandline, paramcount);
  249. ppccommandlinelen := 0;
  250. cpusuffix := ''; // if not empty, signals attempt at cross
  251. // compiler.
  252. extrapath := '';
  253. initplatform(ppcbin, SourceCPU);
  254. exesuffix := ''; { Default is just the name }
  255. if ParamCount = 0 then
  256. begin
  257. SetLength(PPCCommandLine, 1);
  258. AddToCommandLine('-?F'+ParamStr(0));
  259. end
  260. else
  261. for i := 1 to paramcount do
  262. begin
  263. s := ParamStr(i);
  264. if pos('-t', s) = 1 then
  265. begin
  266. targetname := copy(s, 3, length(s)-2);
  267. AddToCommandLine(S);
  268. end
  269. else if pos('-V', s) = 1 then
  270. exesuffix := copy(s, 3, length(s)-2)
  271. else
  272. begin
  273. if pos('-P', s) = 1 then
  274. begin
  275. TargetCPU := copy(s, 3, length(s)-2);
  276. CheckSpecialProcessors(TargetCPU, SourceCPU, ppcbin, cpusuffix, exesuffix);
  277. if TargetCPU<>SourceCPU then
  278. begin
  279. cpusuffix := processortosuffix(TargetCPU);
  280. ppcbin := 'ppc'+crosssuffix+cpusuffix;
  281. end;
  282. end
  283. else if pos('-Xp', s) = 1 then
  284. extrapath := copy(s, 4, length(s)-3)
  285. else
  286. begin
  287. if pos('-h', s) = 1 then
  288. AddToCommandLine('-hF'+ParamStr(0))
  289. else if pos('-?', s) = 1 then
  290. AddToCommandLine('-?F'+ParamStr(0))
  291. else
  292. AddToCommandLine(S);
  293. end;
  294. end;
  295. end;
  296. SetLength(ppccommandline, ppccommandlinelen);
  297. ppcbin := findcompiler(ppcbin, cpusuffix, exesuffix);
  298. { call ppcXXX }
  299. try
  300. errorvalue := ExecuteProcess(ppcbin, ppccommandline);
  301. except
  302. on e: Exception do
  303. error(ppcbin+' can''t be executed, error message: '+e.message);
  304. end;
  305. if (errorvalue<>0) and
  306. (paramcount<>0) then
  307. error(ppcbin+' returned an error exitcode');
  308. halt(errorvalue);
  309. end.