fpc.pp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. warn : Boolean;
  162. ShowErrno : Boolean;
  163. extrapath: ansistring;
  164. function findexe(var ppcbin: string) : boolean;
  165. var
  166. path: string;
  167. begin
  168. { add .exe extension }
  169. findexe := False;
  170. ppcbin := ppcbin+exeext;
  171. if (extrapath<>'') and (extrapath[length(extrapath)]<>DirectorySeparator) then
  172. extrapath := extrapath+DirectorySeparator;
  173. { get path of fpc.exe }
  174. path := splitpath(ParamStr(0));
  175. { don't try with an empty extra patch, this might have strange results
  176. if the current directory contains a compiler
  177. }
  178. if (extrapath<>'') and FileExists(extrapath+ppcbin) then
  179. begin
  180. ppcbin := extrapath+ppcbin;
  181. findexe := True;
  182. end
  183. else if (path<>'') and FileExists(path+ppcbin) then
  184. begin
  185. ppcbin := path+ppcbin;
  186. findexe := True;
  187. end
  188. else
  189. begin
  190. path := ExeSearch(ppcbin, getenvironmentvariable('PATH'));
  191. if path<>'' then
  192. begin
  193. ppcbin := path;
  194. findexe := True;
  195. end;
  196. end;
  197. end;
  198. function findcompiler(basecompiler, cpusuffix, exesuffix: string) : string;
  199. begin
  200. Result := basecompiler;
  201. if exesuffix<>'' then
  202. Result := Result+'-'+exesuffix;
  203. if not findexe(Result) then
  204. begin
  205. if cpusuffix<>'' then
  206. begin
  207. Result := 'ppc'+cpusuffix;
  208. if exesuffix<>'' then
  209. Result := Result+'-'+exesuffix;
  210. if not findexe(Result) then
  211. Result := '';
  212. end;
  213. end;
  214. end;
  215. procedure CheckSpecialProcessors(processorstr, processorname, ppcbin, cpusuffix, exesuffix: string);
  216. begin
  217. { -PB is a special code that will show the
  218. default compiler and exit immediately. It's
  219. main usage is for Makefile }
  220. if processorstr = 'B' then
  221. begin
  222. { report the full name of the ppcbin }
  223. writeln(findcompiler(ppcbin, cpusuffix, exesuffix));
  224. halt(0);
  225. end;
  226. { -PP is a special code that will show the
  227. processor and exit immediately. It's
  228. main usage is for Makefile }
  229. if processorstr = 'P' then
  230. begin
  231. { report the processor }
  232. writeln(processorname);
  233. halt(0);
  234. end;
  235. end;
  236. Function FindConfigFile(const aFile : string) : String;
  237. // Adapted from check_configfile(fn:string; var foundfn:string):boolean;
  238. {
  239. Order to read configuration file :
  240. Unix:
  241. 1 - current dir
  242. 2 - ~/.fpc.cfg
  243. 3 - configpath
  244. 4 - /etc
  245. Windows:
  246. 1 - current dir
  247. 2 - home dir of user or all users
  248. 3 - config path
  249. 4 - next to binary
  250. Other:
  251. 1 - current dir
  252. 3 - config path
  253. 4 - next to binary
  254. }
  255. var
  256. {$ifdef unix}hs,{$endif} aSearchPath,exepath,configpath : string;
  257. Procedure AddToPath(aDir : String);
  258. begin
  259. if aDir='' then exit;
  260. if (aSearchPath<>'') then
  261. aSearchPath:=aSearchPath+PathSeparator;
  262. aSearchPath:=aSearchPath+IncludeTrailingPathDelimiter(SetDirSeparators(aDir));
  263. end;
  264. begin
  265. if FileExists(aFile) then
  266. Exit(aFile);
  267. ExePath:=SetDirSeparators(ExtractFilePath(paramstr(0)));
  268. aSearchPath:='';
  269. { retrieve configpath }
  270. configpath:=SetDirSeparators(GetEnvironmentVariable('PPC_CONFIG_PATH'));
  271. {$ifdef Unix}
  272. hs:=SetDirSeparators(GetEnvironmentVariable('HOME'));
  273. if (hs<>'') then
  274. begin
  275. Result:=IncludeTrailingPathDelimiter(hs)+'.'+aFile;
  276. if FileExists(Result) then
  277. exit;
  278. end;
  279. if configpath='' then
  280. configpath:=ExpandFileName(ExePath+'../etc/');
  281. {$endif}
  282. AddToPath(ConfigPath);
  283. {$ifdef WINDOWS}
  284. AddToPath(GetEnvironmentVariable('USERPROFILE'));
  285. AddToPath(GetEnvironmentVariable('ALLUSERSPROFILE'));
  286. {$endif WINDOWS}
  287. {$ifdef Unix}
  288. AddToPath('/etc/');
  289. {$else}
  290. AddToPath(exepath);
  291. {$endif}
  292. Result:=FileSearch(aFile,aSearchPath);
  293. end;
  294. Procedure CheckWarn(aOpt : String);
  295. Var
  296. Len,I : integer;
  297. begin
  298. Len:=Length(aOpt);
  299. For I:=1 to Len do
  300. begin
  301. if (aOpt[i]='w') then
  302. Warn:=(I=Len) or (aOpt[i+1]<>'-');
  303. if (aOpt[i]='q') then
  304. ShowErrNo:=(I=Len) or (aOpt[i+1]<>'-');
  305. end;
  306. end;
  307. procedure SetExeSuffix(var ExeSuffix : string; aValue : string);
  308. begin
  309. if ExeSuffix='' then
  310. exesuffix :=aValue
  311. else if Warn then
  312. begin
  313. Write('Warning: ');
  314. if ShowErrNo then
  315. Write('(99999) ');
  316. Writeln('Compiler version already set to: ',ExeSuffix);
  317. end;
  318. end;
  319. Procedure ProcessConfigFile(aFileName : String; var ExeSuffix : String);
  320. Function Stripline(aLine : String) : string;
  321. Var
  322. P : integer;
  323. begin
  324. if (aLine<>'') and (aLine[1]=';') then exit;
  325. Pos('#',aLine); // no ifdef or include.
  326. if P=0 then
  327. P:=Length(aLine)+1;
  328. Result:=Copy(aLine,1,P-1);
  329. end;
  330. Var
  331. aFile : Text;
  332. aLine : String;
  333. begin
  334. Assign(aFile,aFileName);
  335. {$push}{$I-}
  336. filemode:=0;
  337. Reset(aFile);
  338. {$pop}
  339. if ioresult<>0 then
  340. Error('Cannot open config file: '+aFileName);
  341. While not EOF(aFile) do
  342. begin
  343. ReadLn(aFile,aLine);
  344. aLine:=StripLine(aLine);
  345. if aLine='' then
  346. continue;
  347. if Copy(aLine,1,2)='-V' then
  348. SetExeSuffix(ExeSuffix,Copy(aLine,3,Length(aLine)-2));
  349. end;
  350. {$i+}
  351. Close(aFile);
  352. end;
  353. var
  354. s,CfgFile: ansistring;
  355. CPUSuffix, ExeSuffix, SourceCPU, ppcbin, TargetName, TargetCPU: string;
  356. PPCCommandLine: array of ansistring;
  357. PPCCommandLineLen: longint;
  358. i: longint;
  359. ErrorValue: longint;
  360. procedure AddToCommandLine(S: string);
  361. begin
  362. PPCCommandLine[PPCCommandLineLen] := S;
  363. Inc(PPCCommandLineLen);
  364. end;
  365. begin
  366. ppccommandline := [];
  367. setlength(ppccommandline, paramcount);
  368. ppccommandlinelen := 0;
  369. cpusuffix := ''; // if not empty, signals attempt at cross
  370. // compiler.
  371. extrapath := '';
  372. initplatform(ppcbin, SourceCPU);
  373. exesuffix := ''; { Default is just the name }
  374. if ParamCount = 0 then
  375. begin
  376. SetLength(PPCCommandLine, 1);
  377. AddToCommandLine('-?F'+ParamStr(0));
  378. end
  379. else
  380. for i := 1 to paramcount do
  381. begin
  382. s := ParamStr(i);
  383. if pos('-t', s) = 1 then
  384. begin
  385. targetname := copy(s, 3, length(s)-2);
  386. AddToCommandLine(S);
  387. end
  388. else if pos('-V', s) = 1 then
  389. SetExeSuffix(ExeSuffix,copy(s, 3, length(s)-2))
  390. else
  391. begin
  392. if pos('-P', s) = 1 then
  393. begin
  394. TargetCPU := copy(s, 3, length(s)-2);
  395. CheckSpecialProcessors(TargetCPU, SourceCPU, ppcbin, cpusuffix, exesuffix);
  396. if TargetCPU<>SourceCPU then
  397. begin
  398. cpusuffix := processortosuffix(TargetCPU);
  399. ppcbin := 'ppc'+crosssuffix+cpusuffix;
  400. end;
  401. end
  402. else if pos('-Xp', s) = 1 then
  403. extrapath := copy(s, 4, length(s)-3)
  404. else
  405. begin
  406. if pos('-h', s) = 1 then
  407. AddToCommandLine('-hF'+ParamStr(0))
  408. else if pos('-?', s) = 1 then
  409. AddToCommandLine('-?F'+ParamStr(0))
  410. else
  411. begin
  412. AddToCommandLine(S);
  413. if pos('-v', s) = 1 then
  414. CheckWarn(Copy(S,3,length(S)-2));
  415. end;
  416. end;
  417. end;
  418. end;
  419. if (TargetName<>'') then
  420. begin
  421. S:='fpc-'+TargetName+'.cfg';
  422. CfgFile:=FindConfigFile(s);
  423. if CfgFile='' then
  424. Error('Cannot find subtarget config file: '+s);
  425. ProcessConfigFile(CfgFile,ExeSuffix);
  426. end;
  427. SetLength(ppccommandline, ppccommandlinelen);
  428. ppcbin := findcompiler(ppcbin, cpusuffix, exesuffix);
  429. { call ppcXXX }
  430. try
  431. errorvalue := ExecuteProcess(ppcbin, ppccommandline);
  432. except
  433. on e: Exception do
  434. error(ppcbin+' can''t be executed, error message: '+e.message);
  435. end;
  436. if (errorvalue<>0) and
  437. (paramcount<>0) then
  438. error(ppcbin+' returned an error exitcode');
  439. halt(errorvalue);
  440. end.