fpc.pp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. <<<<<<< HEAD
  216. procedure CheckSpecialProcessors(processorstr, processorname, ppcbin, cpusuffix, exesuffix: string);
  217. begin
  218. { -PB is a special code that will show the
  219. default compiler and exit immediately. It's
  220. main usage is for Makefile }
  221. if processorstr = 'B' then
  222. begin
  223. { report the full name of the ppcbin }
  224. writeln(findcompiler(ppcbin, cpusuffix, exesuffix));
  225. halt(0);
  226. end;
  227. { -PP is a special code that will show the
  228. processor and exit immediately. It's
  229. main usage is for Makefile }
  230. if processorstr = 'P' then
  231. begin
  232. { report the processor }
  233. writeln(processorname);
  234. halt(0);
  235. end;
  236. end;
  237. Function FindConfigFile(const aFile : string) : String;
  238. // Adapted from check_configfile(fn:string; var foundfn:string):boolean;
  239. {
  240. Order to read configuration file :
  241. Unix:
  242. 1 - current dir
  243. 2 - ~/.fpc.cfg
  244. 3 - configpath
  245. 4 - /etc
  246. Windows:
  247. 1 - current dir
  248. 2 - home dir of user or all users
  249. 3 - config path
  250. 4 - next to binary
  251. Other:
  252. 1 - current dir
  253. 3 - config path
  254. 4 - next to binary
  255. }
  256. var
  257. {$ifdef unix}hs,{$endif} aSearchPath,exepath,configpath : string;
  258. Procedure AddToPath(aDir : String);
  259. =======
  260. procedure CheckSpecialProcessors(processorstr,processorname,ppcbin,cpusuffix,exesuffix : string);
  261. begin
  262. { -PB is a special code that will show the
  263. default compiler and exit immediately. It's
  264. main usage is for Makefile }
  265. if processorstr='B' then
  266. begin
  267. { report the full name of the ppcbin }
  268. writeln(findcompiler(ppcbin,cpusuffix,exesuffix));
  269. halt(0);
  270. end;
  271. { -PP is a special code that will show the
  272. processor and exit immediately. It's
  273. main usage is for Makefile }
  274. if processorstr='P' then
  275. begin
  276. { report the processor }
  277. writeln(processorname);
  278. halt(0);
  279. end;
  280. end;
  281. var
  282. s : ansistring;
  283. cpusuffix,
  284. exesuffix,
  285. processorname,
  286. ppcbin,
  287. targetname,
  288. processorstr : string;
  289. ppccommandline : array of ansistring;
  290. ppccommandlinelen : longint;
  291. i : longint;
  292. errorvalue : Longint;
  293. >>>>>>> 319f7e796d (* Refactor check for -PB and -PP for clarity of flow)
  294. begin
  295. if aDir='' then exit;
  296. if (aSearchPath<>'') then
  297. aSearchPath:=aSearchPath+PathSeparator;
  298. aSearchPath:=aSearchPath+IncludeTrailingPathDelimiter(SetDirSeparators(aDir));
  299. end;
  300. begin
  301. if FileExists(aFile) then
  302. Exit(aFile);
  303. ExePath:=SetDirSeparators(ExtractFilePath(paramstr(0)));
  304. aSearchPath:='';
  305. { retrieve configpath }
  306. configpath:=SetDirSeparators(GetEnvironmentVariable('PPC_CONFIG_PATH'));
  307. {$ifdef Unix}
  308. hs:=SetDirSeparators(GetEnvironmentVariable('HOME'));
  309. if (hs<>'') then
  310. begin
  311. Result:=IncludeTrailingPathDelimiter(hs)+'.'+aFile;
  312. if FileExists(Result) then
  313. exit;
  314. end;
  315. if configpath='' then
  316. configpath:=ExpandFileName(ExePath+'../etc/');
  317. {$endif}
  318. AddToPath(ConfigPath);
  319. {$ifdef WINDOWS}
  320. AddToPath(GetEnvironmentVariable('USERPROFILE'));
  321. AddToPath(GetEnvironmentVariable('ALLUSERSPROFILE'));
  322. {$endif WINDOWS}
  323. {$ifdef Unix}
  324. AddToPath('/etc/');
  325. {$else}
  326. AddToPath(exepath);
  327. {$endif}
  328. Result:=FileSearch(aFile,aSearchPath);
  329. end;
  330. Procedure CheckWarn(aOpt : String);
  331. Var
  332. Len,I : integer;
  333. begin
  334. Len:=Length(aOpt);
  335. For I:=1 to Len do
  336. begin
  337. if (aOpt[i]='w') then
  338. Warn:=(I=Len) or (aOpt[i+1]<>'-');
  339. if (aOpt[i]='q') then
  340. ShowErrNo:=(I=Len) or (aOpt[i+1]<>'-');
  341. end;
  342. end;
  343. procedure SetExeSuffix(var ExeSuffix : string; aValue : string);
  344. begin
  345. if ExeSuffix='' then
  346. exesuffix :=aValue
  347. else if Warn then
  348. begin
  349. Write('Warning: ');
  350. if ShowErrNo then
  351. Write('(99999) ');
  352. Writeln('Compiler version already set to: ',ExeSuffix);
  353. end;
  354. end;
  355. Procedure ProcessConfigFile(aFileName : String; var ExeSuffix : String);
  356. Function Stripline(aLine : String) : string;
  357. Var
  358. P : integer;
  359. begin
  360. if (aLine<>'') and (aLine[1]=';') then exit;
  361. Pos('#',aLine); // no ifdef or include.
  362. if P=0 then
  363. P:=Length(aLine)+1;
  364. Result:=Copy(aLine,1,P-1);
  365. end;
  366. Var
  367. aFile : Text;
  368. aLine : String;
  369. begin
  370. Assign(aFile,aFileName);
  371. {$push}{$I-}
  372. filemode:=0;
  373. Reset(aFile);
  374. {$pop}
  375. if ioresult<>0 then
  376. Error('Cannot open config file: '+aFileName);
  377. While not EOF(aFile) do
  378. begin
  379. ReadLn(aFile,aLine);
  380. aLine:=StripLine(aLine);
  381. if aLine='' then
  382. continue;
  383. if Copy(aLine,1,2)='-V' then
  384. SetExeSuffix(ExeSuffix,Copy(aLine,3,Length(aLine)-2));
  385. end;
  386. {$i+}
  387. Close(aFile);
  388. end;
  389. var
  390. s,CfgFile: ansistring;
  391. CPUSuffix, ExeSuffix, SourceCPU, ppcbin, TargetName, TargetCPU: string;
  392. PPCCommandLine: array of ansistring;
  393. PPCCommandLineLen: longint;
  394. i: longint;
  395. ErrorValue: longint;
  396. procedure AddToCommandLine(S: string);
  397. begin
  398. PPCCommandLine[PPCCommandLineLen] := S;
  399. Inc(PPCCommandLineLen);
  400. end;
  401. begin
  402. ppccommandline := [];
  403. setlength(ppccommandline, paramcount);
  404. ppccommandlinelen := 0;
  405. cpusuffix := ''; // if not empty, signals attempt at cross
  406. // compiler.
  407. extrapath := '';
  408. initplatform(ppcbin, SourceCPU);
  409. exesuffix := ''; { Default is just the name }
  410. if ParamCount = 0 then
  411. begin
  412. SetLength(PPCCommandLine, 1);
  413. AddToCommandLine('-?F'+ParamStr(0));
  414. end
  415. else
  416. for i := 1 to paramcount do
  417. begin
  418. s := ParamStr(i);
  419. if pos('-t', s) = 1 then
  420. begin
  421. targetname := copy(s, 3, length(s)-2);
  422. AddToCommandLine(S);
  423. end
  424. else if pos('-V', s) = 1 then
  425. SetExeSuffix(ExeSuffix,copy(s, 3, length(s)-2))
  426. else
  427. begin
  428. if pos('-P', s) = 1 then
  429. begin
  430. TargetCPU := copy(s, 3, length(s)-2);
  431. CheckSpecialProcessors(TargetCPU, SourceCPU, ppcbin, cpusuffix, exesuffix);
  432. if TargetCPU<>SourceCPU then
  433. begin
  434. cpusuffix := processortosuffix(TargetCPU);
  435. ppcbin := 'ppc'+crosssuffix+cpusuffix;
  436. end;
  437. end
  438. else if pos('-Xp', s) = 1 then
  439. extrapath := copy(s, 4, length(s)-3)
  440. else
  441. begin
  442. if pos('-h', s) = 1 then
  443. AddToCommandLine('-hF'+ParamStr(0))
  444. else if pos('-?', s) = 1 then
  445. AddToCommandLine('-?F'+ParamStr(0))
  446. else
  447. begin
  448. AddToCommandLine(S);
  449. if pos('-v', s) = 1 then
  450. CheckWarn(Copy(S,3,length(S)-2));
  451. end;
  452. end;
  453. end;
  454. end;
  455. if (TargetName<>'') then
  456. begin
  457. S:='fpc-'+TargetName+'.cfg';
  458. CfgFile:=FindConfigFile(s);
  459. if CfgFile='' then
  460. Error('Cannot find subtarget config file: '+s);
  461. ProcessConfigFile(CfgFile,ExeSuffix);
  462. end;
  463. SetLength(ppccommandline, ppccommandlinelen);
  464. ppcbin := findcompiler(ppcbin, cpusuffix, exesuffix);
  465. { call ppcXXX }
  466. try
  467. errorvalue := ExecuteProcess(ppcbin, ppccommandline);
  468. except
  469. on e: Exception do
  470. error(ppcbin+' can''t be executed, error message: '+e.message);
  471. end;
  472. if (errorvalue<>0) and
  473. (paramcount<>0) then
  474. error(ppcbin+' returned an error exitcode');
  475. halt(errorvalue);
  476. end.