fpc.pp 7.1 KB

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