fpc.pp 8.7 KB

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