2
0

fpc.pp 9.6 KB

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