fpc.pp 6.7 KB

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