fpc.pp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. if FileExists(extrapath+ppcbin) then
  73. begin
  74. ppcbin:=extrapath+ppcbin;
  75. findexe:=true;
  76. end
  77. else if FileExists(path+ppcbin) then
  78. begin
  79. ppcbin:=path+ppcbin;
  80. findexe:=true;
  81. end
  82. else
  83. begin
  84. path:=FileSearch(ppcbin,getenvironmentvariable('PATH'));
  85. if path<>'' then
  86. begin
  87. ppcbin:=path;
  88. findexe:=true;
  89. end
  90. end;
  91. end;
  92. var
  93. s : ansistring;
  94. cpusuffix,
  95. processorname,
  96. ppcbin,
  97. versionStr,
  98. processorstr : string;
  99. ppccommandline : array of ansistring;
  100. ppccommandlinelen : longint;
  101. i : longint;
  102. errorvalue : Longint;
  103. begin
  104. setlength(ppccommandline,paramcount);
  105. ppccommandlinelen:=0;
  106. cpusuffix :=''; // if not empty, signals attempt at cross
  107. // compiler.
  108. extrapath :='';
  109. {$ifdef i386}
  110. ppcbin:='ppc386';
  111. processorname:='i386';
  112. {$endif i386}
  113. {$ifdef m68k}
  114. ppcbin:='ppc68k';
  115. processorname:='m68k';
  116. {$endif m68k}
  117. {$ifdef alpha}
  118. ppcbin:='ppcapx';
  119. processorname:='alpha';
  120. {$endif alpha}
  121. {$ifdef powerpc}
  122. ppcbin:='ppcppc';
  123. processorname:='powerpc';
  124. {$endif powerpc}
  125. {$ifdef powerpc64}
  126. ppcbin:='ppcppc64';
  127. processorname:='powerpc64';
  128. {$endif powerpc64}
  129. {$ifdef arm}
  130. ppcbin:='ppcarm';
  131. processorname:='arm';
  132. {$endif arm}
  133. {$ifdef sparc}
  134. ppcbin:='ppcsparc';
  135. processorname:='sparc';
  136. {$endif sparc}
  137. {$ifdef x86_64}
  138. ppcbin:='ppcx64';
  139. processorname:='x86_64';
  140. {$endif x86_64}
  141. {$ifdef ia64}
  142. ppcbin:='ppcia64';
  143. processorname:='ia64';
  144. {$endif ia64}
  145. versionstr:=''; { Default is just the name }
  146. for i:=1 to paramcount do
  147. begin
  148. s:=paramstr(i);
  149. if pos('-V',s)=1 then
  150. versionstr:=copy(s,3,length(s)-2)
  151. else
  152. begin
  153. if pos('-P',s)=1 then
  154. begin
  155. processorstr:=copy(s,3,length(s)-2);
  156. { -PB is a special code that will show the
  157. default compiler and exit immediatly. It's
  158. main usage is for Makefile }
  159. if processorstr='B' then
  160. begin
  161. { report the full name of the ppcbin }
  162. findexe(ppcbin);
  163. writeln(ppcbin);
  164. halt(0);
  165. end
  166. { -PP is a special code that will show the
  167. processor and exit immediatly. It's
  168. main usage is for Makefile }
  169. else if processorstr='P' then
  170. begin
  171. { report the processor }
  172. writeln(processorname);
  173. halt(0);
  174. end
  175. else
  176. if processorstr <> processorname then
  177. begin
  178. if processorstr='i386' then
  179. cpusuffix:='386'
  180. else if processorstr='m68k' then
  181. cpusuffix:='68k'
  182. else if processorstr='alpha' then
  183. cpusuffix:='apx'
  184. else if processorstr='powerpc' then
  185. cpusuffix:='ppc'
  186. else if processorstr='powerpc64' then
  187. cpusuffix:='ppc64'
  188. else if processorstr='arm' then
  189. cpusuffix:='arm'
  190. else if processorstr='sparc' then
  191. cpusuffix:='sparc'
  192. else if processorstr='ia64' then
  193. cpusuffix:='ia64'
  194. else if processorstr='x86_64' then
  195. cpusuffix:='x64'
  196. else
  197. error('Illegal processor type "'+processorstr+'"');
  198. ppcbin:='ppcross'+cpusuffix;
  199. end;
  200. end
  201. else if pos('-Xp',s)=1 then
  202. extrapath:=copy(s,4,length(s)-3)
  203. else
  204. begin
  205. ppccommandline[ppccommandlinelen]:=s;
  206. inc(ppccommandlinelen);
  207. end;
  208. end;
  209. end;
  210. SetLength(ppccommandline,ppccommandlinelen);
  211. if versionstr<>'' then
  212. ppcbin:=ppcbin+'-'+versionstr;
  213. { find the full path to the specified exe }
  214. if not findexe(ppcbin) then
  215. begin
  216. if cpusuffix<>'' Then
  217. begin
  218. ppcbin:='ppc'+cpusuffix;
  219. if versionstr<>'' then
  220. ppcbin:=ppcbin+'-'+versionstr;
  221. findexe(ppcbin);
  222. end;
  223. end;
  224. { call ppcXXX }
  225. try
  226. errorvalue:=ExecuteProcess(ppcbin,ppccommandline);
  227. except
  228. on e : exception do
  229. error(ppcbin+' can''t be executed, error message: '+e.message);
  230. end;
  231. if errorvalue<>0 then
  232. error(ppcbin+' returned an error exitcode (normal if you did not specify a source file to be compiled)');
  233. halt(errorvalue);
  234. end.