fpc.pp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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) : 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}hs,{$endif} aSearchPath,exepath,configpath : string;
  262. Procedure AddToPath(aDir : String);
  263. begin
  264. if aDir='' then exit;
  265. if (aSearchPath<>'') then
  266. aSearchPath:=aSearchPath+PathSeparator;
  267. aSearchPath:=aSearchPath+IncludeTrailingPathDelimiter(SetDirSeparators(aDir));
  268. end;
  269. begin
  270. if FileExists(aFile) then
  271. Exit(aFile);
  272. ExePath:=SetDirSeparators(ExtractFilePath(paramstr(0)));
  273. aSearchPath:='';
  274. { retrieve configpath }
  275. configpath:=SetDirSeparators(GetEnvironmentVariable('PPC_CONFIG_PATH'));
  276. {$ifdef Unix}
  277. hs:=SetDirSeparators(GetEnvironmentVariable('HOME'));
  278. if (hs<>'') then
  279. begin
  280. Result:=IncludeTrailingPathDelimiter(hs)+'.'+aFile;
  281. if FileExists(Result) then
  282. exit;
  283. end;
  284. if configpath='' then
  285. configpath:=ExpandFileName(ExePath+'../etc/');
  286. {$endif}
  287. AddToPath(ConfigPath);
  288. {$ifdef WINDOWS}
  289. AddToPath(GetEnvironmentVariable('USERPROFILE'));
  290. AddToPath(GetEnvironmentVariable('ALLUSERSPROFILE'));
  291. {$endif WINDOWS}
  292. {$ifdef Unix}
  293. AddToPath('/etc/');
  294. {$else}
  295. AddToPath(exepath);
  296. {$endif}
  297. Result:=FileSearch(aFile,aSearchPath);
  298. end;
  299. Procedure CheckWarn(aOpt : String);
  300. Var
  301. Len,I : integer;
  302. begin
  303. Len:=Length(aOpt);
  304. For I:=1 to Len do
  305. begin
  306. if (aOpt[i]='w') then
  307. Warn:=(I=Len) or (aOpt[i+1]<>'-');
  308. if (aOpt[i]='q') then
  309. ShowErrNo:=(I=Len) or (aOpt[i+1]<>'-');
  310. end;
  311. end;
  312. procedure SetExeSuffix(var ExeSuffix : string; aValue : string);
  313. begin
  314. if ExeSuffix='' then
  315. exesuffix :=aValue
  316. else if Warn then
  317. begin
  318. Write('Warning: ');
  319. if ShowErrNo then
  320. Write('(99999) ');
  321. Writeln('Compiler version already set to: ',ExeSuffix);
  322. end;
  323. end;
  324. Procedure ProcessConfigFile(aFileName : String; var ExeSuffix : String);
  325. Function Stripline(aLine : String) : string;
  326. Var
  327. P : integer;
  328. begin
  329. if (aLine<>'') and (aLine[1]=';') then exit;
  330. Pos('#',aLine); // no ifdef or include.
  331. if P=0 then
  332. P:=Length(aLine)+1;
  333. Result:=Copy(aLine,1,P-1);
  334. end;
  335. Var
  336. aFile : Text;
  337. aLine : String;
  338. begin
  339. Assign(aFile,aFileName);
  340. {$push}{$I-}
  341. filemode:=0;
  342. Reset(aFile);
  343. {$pop}
  344. if ioresult<>0 then
  345. Error('Cannot open config file: '+aFileName);
  346. While not EOF(aFile) do
  347. begin
  348. ReadLn(aFile,aLine);
  349. aLine:=StripLine(aLine);
  350. if aLine='' then
  351. continue;
  352. if Copy(aLine,1,2)='-V' then
  353. SetExeSuffix(ExeSuffix,Copy(aLine,3,Length(aLine)-2));
  354. end;
  355. {$i+}
  356. Close(aFile);
  357. end;
  358. var
  359. s,CfgFile: ansistring;
  360. CPUSuffix, ExeSuffix, SourceCPU, ppcbin, TargetName, TargetCPU: string;
  361. PPCCommandLine: array of ansistring;
  362. PPCCommandLineLen: longint;
  363. i : longint;
  364. errorvalue : Longint;
  365. Procedure AddToCommandLine(S : String);
  366. begin
  367. PPCCommandLine [PPCCommandLineLen] := S;
  368. Inc(PPCCommandLineLen);
  369. end;
  370. begin
  371. ppccommandline := [];
  372. setlength(ppccommandline, paramcount);
  373. ppccommandlinelen := 0;
  374. cpusuffix := ''; // if not empty, signals attempt at cross
  375. // compiler.
  376. extrapath := '';
  377. initplatform(ppcbin, SourceCPU);
  378. exesuffix := ''; { Default is just the name }
  379. if ParamCount = 0 then
  380. begin
  381. SetLength(PPCCommandLine, 1);
  382. AddToCommandLine('-?F'+ParamStr(0));
  383. end
  384. else
  385. for i := 1 to paramcount do
  386. begin
  387. s := ParamStr(i);
  388. if pos('-t', s) = 1 then
  389. begin
  390. targetname := copy(s, 3, length(s)-2);
  391. AddToCommandLine(S);
  392. end
  393. else if pos('-V', s) = 1 then
  394. SetExeSuffix(ExeSuffix,copy(s, 3, length(s)-2))
  395. else
  396. begin
  397. if pos('-P', s) = 1 then
  398. begin
  399. TargetCPU:=copy(s,3,length(s)-2);
  400. CheckSpecialProcessors(TargetCPU,SourceCPU,ppcbin,cpusuffix,exesuffix);
  401. if TargetCPU <> SourceCPU then
  402. begin
  403. cpusuffix:=processortosuffix(TargetCPU);
  404. ppcbin:='ppc'+crosssuffix+cpusuffix;
  405. end;
  406. end
  407. else if pos('-Xp',s)=1 then
  408. extrapath:=copy(s,4,length(s)-3)
  409. else
  410. begin
  411. if pos('-h', s) = 1 then
  412. AddToCommandLine('-hF'+ParamStr(0))
  413. else if pos('-?', s) = 1 then
  414. AddToCommandLine('-?F'+ParamStr(0))
  415. else
  416. begin
  417. AddToCommandLine(S);
  418. if pos('-v', s) = 1 then
  419. CheckWarn(Copy(S,3,length(S)-2));
  420. end;
  421. end;
  422. end;
  423. end;
  424. if (TargetName<>'') then
  425. begin
  426. S:='fpc-'+lowercase(TargetName)+'.cfg';
  427. CfgFile:=FindConfigFile(s);
  428. if CfgFile='' then
  429. Error('Cannot find subtarget config file: '+s);
  430. ProcessConfigFile(CfgFile,ExeSuffix);
  431. end;
  432. SetLength(ppccommandline, ppccommandlinelen);
  433. ppcbin := findcompiler(ppcbin, cpusuffix, exesuffix);
  434. { call ppcXXX }
  435. try
  436. errorvalue:=ExecuteProcess(ppcbin,ppccommandline);
  437. except
  438. on e : exception do
  439. error(ppcbin+' can''t be executed, error message: '+e.message);
  440. end;
  441. if (errorvalue<>0) and
  442. (paramcount<>0) then
  443. error(ppcbin+' returned an error exitcode');
  444. halt(errorvalue);
  445. end.