fpc.pp 13 KB

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