fpc.pp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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);noreturn;
  50. begin
  51. writeln('Error: ',s);
  52. halt(1);
  53. end;
  54. function processortosuffix(const 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. 'loongarch64': Result:='loongarch64';
  64. 'm68k': Result := '68k';
  65. 'mips': Result := 'mips';
  66. 'mipsel': Result := 'mipsel';
  67. 'mipseb': Result := 'mipseb';
  68. 'mips64': Result := 'mips64';
  69. 'mips64el': Result := 'mips64el';
  70. 'powerpc': Result := 'ppc';
  71. 'powerpc64': Result := 'ppc64';
  72. 'riscv32': Result := 'rv32';
  73. 'riscv64': Result := 'rv64';
  74. 'sparc': Result := 'sparc';
  75. 'sparc64': Result := 'sparc64';
  76. 'x86_64': Result := 'x64';
  77. 'xtensa': Result := 'xtensa';
  78. 'z80': Result := 'z80';
  79. 'wasm32': Result := 'wasm32'
  80. else
  81. error('Illegal processor type "'+processorstr+'"');
  82. end;
  83. end;
  84. procedure InitPlatform(out ppcbin,processorname : string);
  85. begin
  86. {$ifdef i386}
  87. ppcbin:='ppc386';
  88. processorname:='i386';
  89. {$endif i386}
  90. {$ifdef m68k}
  91. ppcbin:='ppc68k';
  92. processorname:='m68k';
  93. {$endif m68k}
  94. {$ifdef powerpc}
  95. ppcbin:='ppcppc';
  96. processorname:='powerpc';
  97. {$endif powerpc}
  98. {$ifdef powerpc64}
  99. ppcbin:='ppcppc64';
  100. processorname:='powerpc64';
  101. {$endif powerpc64}
  102. {$ifdef arm}
  103. ppcbin:='ppcarm';
  104. processorname:='arm';
  105. {$endif arm}
  106. {$ifdef aarch64}
  107. ppcbin:='ppca64';
  108. processorname:='aarch64';
  109. {$endif aarch64}
  110. {$ifdef sparc}
  111. ppcbin:='ppcsparc';
  112. processorname:='sparc';
  113. {$endif sparc}
  114. {$ifdef sparc64}
  115. ppcbin:='ppcsparc64';
  116. processorname:='sparc64';
  117. {$endif sparc64}
  118. {$ifdef x86_64}
  119. ppcbin:='ppcx64';
  120. processorname:='x86_64';
  121. {$endif x86_64}
  122. {$ifdef mipsel}
  123. ppcbin:='ppcmipsel';
  124. processorname:='mipsel';
  125. {$else : not mipsel}
  126. {$ifdef mipseb}
  127. ppcbin:='ppcmipseb';
  128. processorname:='mipseb';
  129. {$else : not mipseb}
  130. {$ifdef mips}
  131. ppcbin:='ppcmips';
  132. processorname:='mips';
  133. {$endif mips}
  134. {$endif not mipseb}
  135. {$endif not mipsel}
  136. {$ifdef mips64el}
  137. ppcbin:='ppcmips64el';
  138. processorname:='mips64el';
  139. {$else : not mips64el}
  140. {$ifdef mips64}
  141. ppcbin:='ppcmips64';
  142. processorname:='mips64';
  143. {$endif mips}
  144. {$endif not mipsel}
  145. {$ifdef riscv32}
  146. ppcbin:='ppcrv32';
  147. processorname:='riscv32';
  148. {$endif riscv32}
  149. {$ifdef riscv64}
  150. ppcbin:='ppcrv64';
  151. processorname:='riscv64';
  152. {$endif riscv64}
  153. {$ifdef xtensa}
  154. ppcbin:='ppcxtensa';
  155. processorname:='xtensa';
  156. {$endif xtensa}
  157. {$ifdef wasm32}
  158. ppcbin:='ppcwasm32';
  159. processorname:='wasm32';
  160. {$endif wasm32}
  161. {$ifdef loongarch64}
  162. ppcbin:='ppcloongarch64';
  163. processorname:='loongarch64';
  164. {$endif loongarch64}
  165. end;
  166. function SplitPath(Const HStr:String):String;
  167. var
  168. i : longint;
  169. begin
  170. i:=Length(Hstr);
  171. while (i>0) and not(Hstr[i] in ['\','/']) do
  172. dec(i);
  173. SplitPath:=Copy(Hstr,1,i);
  174. end;
  175. function FileExists ( Const F : String) : Boolean;
  176. var
  177. Info : TSearchRec;
  178. begin
  179. FileExists:= findfirst(F,fareadonly+faarchive+fahidden,info)=0;
  180. findclose(Info);
  181. end;
  182. var
  183. warn : Boolean;
  184. ShowErrno : Boolean;
  185. extrapath : ansistring;
  186. function findexe(var ppcbin:string): boolean;
  187. var
  188. path : string;
  189. begin
  190. { add .exe extension }
  191. findexe:=false;
  192. ppcbin:=ppcbin+exeext;
  193. if (extrapath<>'') and (extrapath[length(extrapath)]<>DirectorySeparator) then
  194. extrapath:=extrapath+DirectorySeparator;
  195. { get path of fpc.exe }
  196. path:=splitpath(paramstr(0));
  197. { don't try with an empty extra patch, this might have strange results
  198. if the current directory contains a compiler
  199. }
  200. if (extrapath<>'') and FileExists(extrapath+ppcbin) then
  201. begin
  202. ppcbin:=extrapath+ppcbin;
  203. findexe:=true;
  204. end
  205. else if (path<>'') and FileExists(path+ppcbin) then
  206. begin
  207. ppcbin:=path+ppcbin;
  208. findexe:=true;
  209. end
  210. else
  211. begin
  212. path:=ExeSearch(ppcbin,getenvironmentvariable('PATH'));
  213. if path<>'' then
  214. begin
  215. ppcbin:=path;
  216. findexe:=true;
  217. end
  218. end;
  219. end;
  220. function findcompiler(basecompiler,cpusuffix,exesuffix : string) : string;
  221. begin
  222. Result:=basecompiler;
  223. if exesuffix<>'' then
  224. Result:=Result+'-'+exesuffix;
  225. if not findexe(Result) then
  226. begin
  227. if cpusuffix<>'' Then
  228. begin
  229. Result:='ppc'+cpusuffix;
  230. if exesuffix<>'' then
  231. result:=result+'-'+exesuffix;
  232. if not findexe(result) then
  233. result:='';
  234. end;
  235. end;
  236. end;
  237. procedure CheckSpecialProcessors(processorstr,processorname,ppcbin,cpusuffix,exesuffix : string);
  238. begin
  239. { -PB is a special code that will show the
  240. default compiler and exit immediately. It's
  241. main usage is for Makefile }
  242. if processorstr='B' then
  243. begin
  244. { report the full name of the ppcbin }
  245. writeln(findcompiler(ppcbin,cpusuffix,exesuffix));
  246. halt(0);
  247. end;
  248. { -PP is a special code that will show the
  249. processor and exit immediately. It's
  250. main usage is for Makefile }
  251. if processorstr='P' then
  252. begin
  253. { report the processor }
  254. writeln(processorname);
  255. halt(0);
  256. end;
  257. end;
  258. Function FindConfigFile(const aFile : string; const aCompiler : String) : String;
  259. // Adapted from check_configfile(fn:string; var foundfn:string):boolean;
  260. {
  261. Order to read configuration file :
  262. Unix:
  263. 1 - current dir
  264. 2 - ~/.fpc.cfg
  265. 3 - configpath
  266. 4 - /etc
  267. Windows:
  268. 1 - current dir
  269. 2 - home dir of user or all users
  270. 3 - config path
  271. 4 - next to binary
  272. Other:
  273. 1 - current dir
  274. 3 - config path
  275. 4 - next to binary
  276. }
  277. var
  278. {$ifdef unix}sl : rawbytestring;{$endif}
  279. {$ifdef unix}hs,{$endif} aSearchPath,exepath,configpath : string;
  280. Procedure AddToPath(aDir : String);
  281. begin
  282. if aDir='' then exit;
  283. if (aSearchPath<>'') then
  284. aSearchPath:=aSearchPath+PathSeparator;
  285. aSearchPath:=aSearchPath+IncludeTrailingPathDelimiter(SetDirSeparators(aDir));
  286. end;
  287. begin
  288. if FileExists(aFile) then
  289. Exit(aFile);
  290. ExePath:=SetDirSeparators(ExtractFilePath(paramstr(0)));
  291. aSearchPath:='';
  292. { retrieve configpath }
  293. configpath:=SetDirSeparators(GetEnvironmentVariable('PPC_CONFIG_PATH'));
  294. {$ifdef Unix}
  295. hs:=SetDirSeparators(GetEnvironmentVariable('HOME'));
  296. if (hs<>'') then
  297. begin
  298. Result:=IncludeTrailingPathDelimiter(hs)+'.'+aFile;
  299. if FileExists(Result) then
  300. exit;
  301. end;
  302. if configpath='' then
  303. begin
  304. {
  305. We need to search relative to compiler binary, not relative to FPC binary.
  306. Beware of symlinks !
  307. }
  308. hs:=aCompiler;
  309. While FileGetSymLinkTarget(hs,sl) do
  310. begin
  311. if copy(sl,1,1)<>'/' then
  312. hs:=ExpandFileName(ExtractFilePath(hs)+sl)
  313. else
  314. hs:=sl;
  315. end;
  316. ExePath:=ExtractFilePath(hs);
  317. configpath:=ExpandFileName(ExePath+'../etc/');
  318. end;
  319. {$endif}
  320. AddToPath(ConfigPath);
  321. {$ifdef WINDOWS}
  322. AddToPath(GetEnvironmentVariable('USERPROFILE'));
  323. AddToPath(GetEnvironmentVariable('ALLUSERSPROFILE'));
  324. {$endif WINDOWS}
  325. {$ifdef Unix}
  326. AddToPath('/etc/');
  327. {$else}
  328. AddToPath(exepath);
  329. {$endif}
  330. Result:=FileSearch(aFile,aSearchPath);
  331. end;
  332. Procedure CheckWarn(aOpt : String);
  333. Var
  334. Len,I : integer;
  335. begin
  336. Len:=Length(aOpt);
  337. For I:=1 to Len do
  338. begin
  339. if (aOpt[i]='w') then
  340. Warn:=(I=Len) or (aOpt[i+1]<>'-');
  341. if (aOpt[i]='q') then
  342. ShowErrNo:=(I=Len) or (aOpt[i+1]<>'-');
  343. end;
  344. end;
  345. procedure SetExeSuffix(var ExeSuffix : string; aValue : string);
  346. begin
  347. if ExeSuffix='' then
  348. exesuffix :=aValue
  349. else if Warn then
  350. begin
  351. Write('Warning: ');
  352. if ShowErrNo then
  353. Write('(99999) ');
  354. Writeln('Compiler version already set to: ',ExeSuffix);
  355. end;
  356. end;
  357. Procedure ProcessConfigFile(aFileName : String; var ExeSuffix : String);
  358. Function Stripline(const aLine : String) : string;
  359. Var
  360. P : integer;
  361. begin
  362. if (aLine<>'') and (aLine[1]=';') then exit('');
  363. P:=Pos('#',aLine); // no ifdef or include.
  364. if P=0 then
  365. P:=Length(aLine)+1;
  366. Result:=Copy(aLine,1,P-1);
  367. end;
  368. Var
  369. aFile : Text;
  370. aLine : String;
  371. begin
  372. Assign(aFile,aFileName);
  373. {$push}{$I-}
  374. filemode:=0;
  375. Reset(aFile);
  376. {$pop}
  377. if ioresult<>0 then
  378. Error('Cannot open config file: '+aFileName);
  379. While not EOF(aFile) do
  380. begin
  381. ReadLn(aFile,aLine);
  382. aLine:=StripLine(aLine);
  383. if aLine='' then
  384. continue;
  385. if Copy(aLine,1,2)='-V' then
  386. SetExeSuffix(ExeSuffix,Copy(aLine,3,Length(aLine)-2));
  387. end;
  388. {$i+}
  389. Close(aFile);
  390. end;
  391. var
  392. s,CfgFile: ansistring;
  393. CPUSuffix, ExeSuffix, SourceCPU, ppcbin, TargetName, TargetCPU: string;
  394. PPCCommandLine: array of ansistring;
  395. PPCCommandLineLen: longint;
  396. i : longint;
  397. errorvalue : Longint;
  398. Procedure AddToCommandLine(S : String);
  399. begin
  400. PPCCommandLine [PPCCommandLineLen] := S;
  401. Inc(PPCCommandLineLen);
  402. end;
  403. begin
  404. ppccommandline := [];
  405. setlength(ppccommandline, paramcount);
  406. ppccommandlinelen := 0;
  407. cpusuffix := ''; // if not empty, signals attempt at cross
  408. // compiler.
  409. extrapath := '';
  410. initplatform(ppcbin, SourceCPU);
  411. exesuffix := ''; { Default is just the name }
  412. if ParamCount = 0 then
  413. begin
  414. SetLength(PPCCommandLine, 1);
  415. AddToCommandLine('-?F'+ParamStr(0));
  416. end
  417. else
  418. for i := 1 to paramcount do
  419. begin
  420. s := ParamStr(i);
  421. if pos('-t', s) = 1 then
  422. begin
  423. targetname := copy(s, 3, length(s)-2);
  424. AddToCommandLine(S);
  425. end
  426. else if pos('-V', s) = 1 then
  427. SetExeSuffix(ExeSuffix,copy(s, 3, length(s)-2))
  428. else
  429. begin
  430. if pos('-P', s) = 1 then
  431. begin
  432. TargetCPU:=copy(s,3,length(s)-2);
  433. CheckSpecialProcessors(TargetCPU,SourceCPU,ppcbin,cpusuffix,exesuffix);
  434. if TargetCPU <> SourceCPU then
  435. begin
  436. cpusuffix:=processortosuffix(TargetCPU);
  437. ppcbin:='ppc'+crosssuffix+cpusuffix;
  438. end;
  439. end
  440. else if pos('-Xp',s)=1 then
  441. extrapath:=copy(s,4,length(s)-3)
  442. else
  443. begin
  444. if pos('-h', s) = 1 then
  445. AddToCommandLine('-hF'+ParamStr(0))
  446. else if pos('-?', s) = 1 then
  447. AddToCommandLine('-?F'+ParamStr(0))
  448. else
  449. begin
  450. AddToCommandLine(S);
  451. if pos('-v', s) = 1 then
  452. CheckWarn(Copy(S,3,length(S)-2));
  453. end;
  454. end;
  455. end;
  456. end;
  457. ppcbin := findcompiler(ppcbin, cpusuffix, exesuffix);
  458. if (TargetName<>'') then
  459. begin
  460. S:='fpc-'+lowercase(TargetName)+'.cfg';
  461. CfgFile:=FindConfigFile(s,ppcbin);
  462. if CfgFile='' then
  463. Error('Cannot find subtarget config file: '+s);
  464. ProcessConfigFile(CfgFile,ExeSuffix);
  465. end;
  466. SetLength(ppccommandline, ppccommandlinelen);
  467. { call ppcXXX }
  468. try
  469. errorvalue:=ExecuteProcess(ppcbin,ppccommandline);
  470. except
  471. on e : exception do
  472. error(ppcbin+' can''t be executed, error message: '+e.message);
  473. end;
  474. if (errorvalue<>0) and
  475. (paramcount<>0) then
  476. error(ppcbin+' returned an error exitcode');
  477. halt(errorvalue);
  478. end.