fpc.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 HASAMIGA}
  25. exeext='';
  26. {$else}
  27. {$ifdef NETWARE}
  28. exeext='.nlm';
  29. {$else}
  30. {$ifdef ATARI}
  31. exeext='.ttp';
  32. {$else}
  33. exeext = '.exe';
  34. {$endif ATARI}
  35. {$endif NETWARE}
  36. {$endif HASAMIGA}
  37. {$endif UNIX}
  38. const
  39. {$ifdef darwin}
  40. { the mach-o format supports "fat" binaries whereby }
  41. { a single executable contains machine code for }
  42. { several architectures -> it is counter-intuitive }
  43. { and non-standard to use different binary names }
  44. { for cross-compilers vs. native compilers }
  45. CrossSuffix = '';
  46. {$else not darwin}
  47. CrossSuffix = 'ross';
  48. {$endif not darwin}
  49. procedure error(const s: string);
  50. begin
  51. writeln('Error: ', s);
  52. halt(1);
  53. end;
  54. function processortosuffix(processorstr: string) : string;
  55. begin
  56. case processorstr of
  57. 'aarch64': Result := 'a64';
  58. 'arm': Result := 'arm';
  59. 'avr': Result := 'avr';
  60. 'i386': Result := '386';
  61. 'i8086': Result := '8086';
  62. 'jvm': Result := 'jvm';
  63. 'm68k': Result := '68k';
  64. 'mips': Result := 'mips';
  65. 'mipsel': Result := 'mipsel';
  66. 'powerpc': Result := 'ppc';
  67. 'powerpc64': Result := 'ppc64';
  68. 'riscv32': Result := 'rv32';
  69. 'riscv64': Result := 'rv64';
  70. 'sparc': Result := 'sparc';
  71. 'sparc64': Result := 'sparc64';
  72. 'x86_64': Result := 'x64';
  73. 'xtensa': Result := 'xtensa';
  74. 'z80': Result := 'z80';
  75. 'wasm32': Result := 'wasm32'
  76. else
  77. error('Illegal processor type "'+processorstr+'"');
  78. end;
  79. end;
  80. procedure InitPlatform(out ppcbin, processorname: string);
  81. begin
  82. {$ifdef i386}
  83. ppcbin:='ppc386';
  84. processorname:='i386';
  85. {$endif i386}
  86. {$ifdef m68k}
  87. ppcbin:='ppc68k';
  88. processorname:='m68k';
  89. {$endif m68k}
  90. {$ifdef powerpc}
  91. ppcbin:='ppcppc';
  92. processorname:='powerpc';
  93. {$endif powerpc}
  94. {$ifdef powerpc64}
  95. ppcbin:='ppcppc64';
  96. processorname:='powerpc64';
  97. {$endif powerpc64}
  98. {$ifdef arm}
  99. ppcbin:='ppcarm';
  100. processorname:='arm';
  101. {$endif arm}
  102. {$ifdef aarch64}
  103. ppcbin:='ppca64';
  104. processorname:='aarch64';
  105. {$endif aarch64}
  106. {$ifdef sparc}
  107. ppcbin:='ppcsparc';
  108. processorname:='sparc';
  109. {$endif sparc}
  110. {$ifdef sparc64}
  111. ppcbin:='ppcsparc64';
  112. processorname:='sparc64';
  113. {$endif sparc64}
  114. {$ifdef x86_64}
  115. ppcbin:='ppcx64';
  116. processorname:='x86_64';
  117. {$endif x86_64}
  118. {$ifdef mipsel}
  119. ppcbin:='ppcmipsel';
  120. processorname:='mipsel';
  121. {$else : not mipsel}
  122. {$ifdef mips}
  123. ppcbin:='ppcmips';
  124. processorname:='mips';
  125. {$endif mips}
  126. {$endif not mipsel}
  127. {$ifdef riscv32}
  128. ppcbin:='ppcrv32';
  129. processorname:='riscv32';
  130. {$endif riscv32}
  131. {$ifdef riscv64}
  132. ppcbin:='ppcrv64';
  133. processorname:='riscv64';
  134. {$endif riscv64}
  135. {$ifdef xtensa}
  136. ppcbin:='ppcxtensa';
  137. processorname:='xtensa';
  138. {$endif xtensa}
  139. {$ifdef wasm32}
  140. ppcbin:='ppcwasm32';
  141. processorname:='wasm32';
  142. {$endif wasm32}
  143. end;
  144. function SplitPath(const HStr: string) : string;
  145. var
  146. i: longint;
  147. begin
  148. i := Length(Hstr);
  149. while (i>0) and not(Hstr[i] in ['\', '/']) do
  150. Dec(i);
  151. SplitPath := Copy(Hstr, 1, i);
  152. end;
  153. function FileExists(const F: string) : boolean;
  154. var
  155. Info: TSearchRec;
  156. begin
  157. FileExists := findfirst(F, fareadonly+faarchive+fahidden, info) = 0;
  158. findclose(Info);
  159. end;
  160. var
  161. extrapath: ansistring;
  162. function findexe(var ppcbin: string) : boolean;
  163. var
  164. path: string;
  165. begin
  166. { add .exe extension }
  167. findexe := False;
  168. ppcbin := ppcbin+exeext;
  169. if (extrapath<>'') and (extrapath[length(extrapath)]<>DirectorySeparator) then
  170. extrapath := extrapath+DirectorySeparator;
  171. { get path of fpc.exe }
  172. path := splitpath(ParamStr(0));
  173. { don't try with an empty extra patch, this might have strange results
  174. if the current directory contains a compiler
  175. }
  176. if (extrapath<>'') and FileExists(extrapath+ppcbin) then
  177. begin
  178. ppcbin := extrapath+ppcbin;
  179. findexe := True;
  180. end
  181. else if (path<>'') and FileExists(path+ppcbin) then
  182. begin
  183. ppcbin := path+ppcbin;
  184. findexe := True;
  185. end
  186. else
  187. begin
  188. path := ExeSearch(ppcbin, getenvironmentvariable('PATH'));
  189. if path<>'' then
  190. begin
  191. ppcbin := path;
  192. findexe := True;
  193. end;
  194. end;
  195. end;
  196. function findcompiler(basecompiler, cpusuffix, exesuffix: string) : string;
  197. begin
  198. Result := basecompiler;
  199. if exesuffix<>'' then
  200. Result := Result+'-'+exesuffix;
  201. if not findexe(Result) then
  202. begin
  203. if cpusuffix<>'' then
  204. begin
  205. Result := 'ppc'+cpusuffix;
  206. if exesuffix<>'' then
  207. Result := Result+'-'+exesuffix;
  208. if not findexe(Result) then
  209. Result := '';
  210. end;
  211. end;
  212. end;
  213. procedure CheckSpecialProcessors(processorstr, processorname, ppcbin, cpusuffix, exesuffix: string);
  214. begin
  215. { -PB is a special code that will show the
  216. default compiler and exit immediately. It's
  217. main usage is for Makefile }
  218. if processorstr = 'B' then
  219. begin
  220. { report the full name of the ppcbin }
  221. writeln(findcompiler(ppcbin, cpusuffix, exesuffix));
  222. halt(0);
  223. end;
  224. { -PP is a special code that will show the
  225. processor and exit immediately. It's
  226. main usage is for Makefile }
  227. if processorstr = 'P' then
  228. begin
  229. { report the processor }
  230. writeln(processorname);
  231. halt(0);
  232. end;
  233. end;
  234. Function FindConfigFile(const aFile : string) : String;
  235. // Adapted from check_configfile(fn:string; var foundfn:string):boolean;
  236. {
  237. Order to read configuration file :
  238. Unix:
  239. 1 - current dir
  240. 2 - ~/.fpc.cfg
  241. 3 - configpath
  242. 4 - /etc
  243. Windows:
  244. 1 - current dir
  245. 2 - home dir of user or all users
  246. 3 - config path
  247. 4 - next to binary
  248. Other:
  249. 1 - current dir
  250. 3 - config path
  251. 4 - next to binary
  252. }
  253. var
  254. {$ifdef unix}hs,{$endif} aSearchPath,exepath,configpath : string;
  255. Procedure AddToPath(aDir : String);
  256. begin
  257. if aDir='' then exit;
  258. if (aSearchPath<>'') then
  259. aSearchPath:=aSearchPath+PathSeparator;
  260. aSearchPath:=aSearchPath+IncludeTrailingPathDelimiter(SetDirSeparators(aDir));
  261. end;
  262. begin
  263. if FileExists(aFile) then
  264. Exit(aFile);
  265. ExePath:=SetDirSeparators(ExtractFilePath(paramstr(0)));
  266. aSearchPath:='';
  267. { retrieve configpath }
  268. configpath:=SetDirSeparators(GetEnvironmentVariable('PPC_CONFIG_PATH'));
  269. {$ifdef Unix}
  270. hs:=SetDirSeparators(GetEnvironmentVariable('HOME'));
  271. if (hs<>'') then
  272. begin
  273. Result:=IncludeTrailingPathDelimiter(hs)+'.'+aFile;
  274. if FileExists(Result) then
  275. exit;
  276. end;
  277. if configpath='' then
  278. configpath:=ExpandFileName(ExePath+'../etc/');
  279. {$endif}
  280. AddToPath(ConfigPath);
  281. {$ifdef WINDOWS}
  282. AddToPath(GetEnvironmentVariable('USERPROFILE'));
  283. AddToPath(GetEnvironmentVariable('ALLUSERSPROFILE'));
  284. {$endif WINDOWS}
  285. {$ifdef Unix}
  286. AddToPath('/etc/');
  287. {$else}
  288. AddToPath(exepath);
  289. {$endif}
  290. Result:=FileSearch(aFile,aSearchPath);
  291. end;
  292. Procedure ProcessConfigFile(aFileName : String; var ExeSuffix : String);
  293. Function Stripline(aLine : String) : string;
  294. Var
  295. P : integer;
  296. begin
  297. if (aLine<>'') and (aLine[1]=';') then exit;
  298. Pos('#',aLine); // no ifdef or include.
  299. if P=0 then
  300. P:=Length(aLine)+1;
  301. Result:=Copy(aLine,1,P-1);
  302. end;
  303. Var
  304. aFile : Text;
  305. aLine : String;
  306. begin
  307. Assign(aFile,aFileName);
  308. {$push}{$I-}
  309. filemode:=0;
  310. Reset(aFile);
  311. {$pop}
  312. if ioresult<>0 then
  313. Error('Cannot open config file: '+aFileName);
  314. While not EOF(aFile) do
  315. begin
  316. ReadLn(aFile,aLine);
  317. aLine:=StripLine(aLine);
  318. if aLine='' then
  319. continue;
  320. if Copy(aLine,1,2)='-V' then
  321. if ExeSuffix='' then
  322. ExeSuffix:=Copy(aLine,3,Length(aLine)-2)
  323. else
  324. Error('Option -V already specified on command-line: '+ExeSuffix);
  325. end;
  326. {$i+}
  327. Close(aFile);
  328. end;
  329. var
  330. s,CfgFile: ansistring;
  331. CPUSuffix, ExeSuffix, SourceCPU, ppcbin, TargetName, TargetCPU: string;
  332. PPCCommandLine: array of ansistring;
  333. PPCCommandLineLen: longint;
  334. i: longint;
  335. ErrorValue: longint;
  336. procedure AddToCommandLine(S: string);
  337. begin
  338. PPCCommandLine[PPCCommandLineLen] := S;
  339. Inc(PPCCommandLineLen);
  340. end;
  341. begin
  342. ppccommandline := [];
  343. setlength(ppccommandline, paramcount);
  344. ppccommandlinelen := 0;
  345. cpusuffix := ''; // if not empty, signals attempt at cross
  346. // compiler.
  347. extrapath := '';
  348. initplatform(ppcbin, SourceCPU);
  349. exesuffix := ''; { Default is just the name }
  350. if ParamCount = 0 then
  351. begin
  352. SetLength(PPCCommandLine, 1);
  353. AddToCommandLine('-?F'+ParamStr(0));
  354. end
  355. else
  356. for i := 1 to paramcount do
  357. begin
  358. s := ParamStr(i);
  359. if pos('-t', s) = 1 then
  360. begin
  361. targetname := copy(s, 3, length(s)-2);
  362. AddToCommandLine(S);
  363. end
  364. else if pos('-V', s) = 1 then
  365. exesuffix := copy(s, 3, length(s)-2)
  366. else
  367. begin
  368. if pos('-P', s) = 1 then
  369. begin
  370. TargetCPU := copy(s, 3, length(s)-2);
  371. CheckSpecialProcessors(TargetCPU, SourceCPU, ppcbin, cpusuffix, exesuffix);
  372. if TargetCPU<>SourceCPU then
  373. begin
  374. cpusuffix := processortosuffix(TargetCPU);
  375. ppcbin := 'ppc'+crosssuffix+cpusuffix;
  376. end;
  377. end
  378. else if pos('-Xp', s) = 1 then
  379. extrapath := copy(s, 4, length(s)-3)
  380. else
  381. begin
  382. if pos('-h', s) = 1 then
  383. AddToCommandLine('-hF'+ParamStr(0))
  384. else if pos('-?', s) = 1 then
  385. AddToCommandLine('-?F'+ParamStr(0))
  386. else
  387. AddToCommandLine(S);
  388. end;
  389. end;
  390. end;
  391. if (TargetName<>'') then
  392. begin
  393. S:='fpc-'+TargetName+'.cfg';
  394. CfgFile:=FindConfigFile(s);
  395. if CfgFile='' then
  396. Error('Cannot find subtarget config file: '+s);
  397. ProcessConfigFile(CfgFile,ExeSuffix);
  398. end;
  399. SetLength(ppccommandline, ppccommandlinelen);
  400. ppcbin := findcompiler(ppcbin, cpusuffix, exesuffix);
  401. { call ppcXXX }
  402. try
  403. errorvalue := ExecuteProcess(ppcbin, ppccommandline);
  404. except
  405. on e: Exception do
  406. error(ppcbin+' can''t be executed, error message: '+e.message);
  407. end;
  408. if (errorvalue<>0) and
  409. (paramcount<>0) then
  410. error(ppcbin+' returned an error exitcode');
  411. halt(errorvalue);
  412. end.