fpc.pp 5.7 KB

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