fpcmkcfg.pp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. {$mode objfpc}
  2. {$H+}
  3. {
  4. This file is part of Free Pascal Build tools
  5. Copyright (c) 2005 by Michael Van Canneyt
  6. Create a configuration file
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. program fpcmkcfg;
  14. uses
  15. fpmkunit,
  16. SysUtils,
  17. Classes,
  18. {$ifdef unix}
  19. baseunix,
  20. {$endif}
  21. fpTemplate;
  22. {
  23. The inc files must be built from a template with the data2inc
  24. command.
  25. data2inc -b -s fpc.cft fpccfg.inc DefaultConfig
  26. data2inc -b -s fpinc.ini fpini.inc fpini
  27. data2inc -b -s fpinc.cfg fpcfg.inc fpcfg
  28. data2inc -b -s fppkg.cfg fppkg.inc fppkg
  29. data2inc -b -s default.cft default.inc fppkg_default
  30. }
  31. {$i fpccfg.inc}
  32. {$i fpini.inc}
  33. {$i fpcfg.inc}
  34. {$i fppkg.inc}
  35. {$i default.inc}
  36. Const
  37. BuildVersion={$I %FPCVERSION%};
  38. BuildTarget={$I %FPCTARGET%};
  39. BuildOSTarget={$I %FPCTARGETOS%};
  40. {$ifdef unix}
  41. ExeExt = '';
  42. {$else unix}
  43. ExeExt = '.exe';
  44. {$endif unix}
  45. Resourcestring
  46. SUsage00 = 'Usage: %s [options]';
  47. SUsage10 = 'Where options is one or more of';
  48. SUSage20 = ' -t filename Template file name. Default is built-in';
  49. SUSage30 = ' -o filename Set output file. Default is standard output.';
  50. SUsage40 = ' -d name=value define name=value pair.';
  51. SUsage50 = ' -h show this help and exit.';
  52. SUsage60 = ' -u name remove name from list of name/value pairs.';
  53. // SUsage70 = ' -l filename read name/value pairs from filename';
  54. SUsage70 = ' -m show builtin macros and exit.';
  55. SUsage80 = ' -b show builtin template and exit.';
  56. SUsage84 = ' -s skip the creation of a backup-file.';
  57. SUsage87 = ' -p force directory creation.';
  58. SUsage90 = ' -v be verbose.';
  59. Susage100 = ' -0 use built in fpc.cfg template (default)';
  60. Susage110 = ' -1 use built in fp.cfg template';
  61. Susage120 = ' -2 use built in fp.ini template';
  62. Susage130 = ' -3 use built in fppkg.cfg template';
  63. Susage140 = ' -4 use built in fppkg default compiler template';
  64. SErrUnknownOption = 'Error: Unknown option.';
  65. SErrArgExpected = 'Error: Option "%s" requires an argument.';
  66. SErrIncompletePair = 'Error: Incomplete name-value pair "%s".';
  67. SErrNoSuchFile = 'Error: File "%s" does not exist.';
  68. SErrNoSuchDirectory = 'Error: Directory of file "%s" does not exists. User -p to force creation.';
  69. SErrBackupFailed = 'Error: Backup of file "%s" to "%s" failed.';
  70. SErrDelBackupFailed = 'Error: Delete of old backup file "%s" failed.';
  71. SErrCreateDirFailed = 'Error: Could not create the directory for file "%s".';
  72. SErrDestDirectory = 'Error: The output file "%s" is a directory.';
  73. SWarnIgnoringFile = 'Warning: Ignoring non-existent file: ';
  74. SWarnIgnoringPair = 'Warning: Ignoring wrong name/value pair: ';
  75. SWarngccNotFound = 'Warning: Could not find gcc. Unable to determine the gcclib path.';
  76. SWarnCouldNotExecute= 'Warning: Could not execute command ''%s''';
  77. SBackupCreated = 'Saved old "%s" to "%s"';
  78. Var
  79. Verbose : Boolean;
  80. SkipBackup : Boolean;
  81. CreateDir: Boolean;
  82. Cfg : TStringList;
  83. TemplateParser: TTemplateParser;
  84. TemplateFileName,
  85. OutputFileName : String;
  86. IDEBuildin : Integer;
  87. function IsSuperUser:boolean;
  88. begin
  89. {$ifdef unix}
  90. result:=(fpGetUID=0);
  91. {$else unix}
  92. result:=false;
  93. {$endif unix}
  94. end;
  95. function GetDefaultLocalRepository: string;
  96. begin
  97. {$IFDEF Unix}
  98. result := '{UserDir}.fppkg'+PathDelim;
  99. {$ELSE Unix}
  100. result := '{AppConfigDir}';
  101. {$ENDIF Unix}
  102. end;
  103. function GetDefaultLocalBasepath: string;
  104. begin
  105. {$IFDEF Unix}
  106. result := '~/.fppkg'+PathDelim+'lib'+PathDelim+'fpc'+PathDelim+'$fpcversion';
  107. {$ELSE Unix}
  108. result := '$LOCAL_APPDATA'+PathDelim+'FreePascal'+PathDelim+'fppkg';
  109. {$ENDIF Unix}
  110. end;
  111. function GetDefaultCompilerConfigDir: string;
  112. begin
  113. {$IFDEF Unix}
  114. if IsSuperUser then
  115. result := '/etc/fppkg/'
  116. else
  117. {$ENDIF}
  118. result := '{LocalRepository}config/';
  119. end;
  120. function GetDefaultNeedCrossBinutilsIfdef: string;
  121. begin
  122. result := '';
  123. // On Darwin there is never a need for a crossbinutils prefix
  124. if SameText(BuildOSTarget,'Darwin') then
  125. result := '#IFNDEF ' + BuildOSTarget + LineEnding +
  126. '#DEFINE NEEDCROSSBINUTILS' + LineEnding +
  127. '#ENDIF'
  128. else if (BuildTarget = 'i386') or (BuildTarget = 'x86_64') then
  129. begin
  130. // Cross-binutils are not needed to compile for i386 on an x86_64 system
  131. result := '#IFNDEF CPUI386' + LineEnding +
  132. '#IFNDEF CPUAMD64' + LineEnding +
  133. '#DEFINE NEEDCROSSBINUTILS' + LineEnding +
  134. '#ENDIF' + LineEnding +
  135. '#ENDIF' + LineEnding +
  136. LineEnding +
  137. '#IFNDEF ' + BuildOSTarget + LineEnding +
  138. '#DEFINE NEEDCROSSBINUTILS' + LineEnding +
  139. '#ENDIF';
  140. end
  141. else
  142. result := '#DEFINE NEEDCROSSBINUTILS';
  143. end;
  144. function GetDefaultGCCDir: string;
  145. var
  146. OS: TOS;
  147. CPU: TCPU;
  148. s: string;
  149. procedure AddConditionalLinkerPath(const aCpuType: string; const ACPU: TCPU; var ConfigFileOption: string);
  150. var
  151. path: string;
  152. ErrS: string;
  153. begin
  154. path := GetDefaultLibGCCDir(ACPU, OS, ErrS);
  155. if ErrS<>'' then
  156. Writeln(StdErr, ErrS);
  157. if path <> '' then
  158. begin
  159. if ConfigFileOption<>'' then ConfigFileOption:=ConfigFileOption+LineEnding;
  160. ConfigFileOption := ConfigFileOption + '#ifdef ' + ACpuType + LineEnding + '-Fl' + Path + LineEnding + '#endif';
  161. end;
  162. end;
  163. begin
  164. CPU := StringToCPU(BuildTarget);
  165. OS := StringToOS(BuildOSTarget);
  166. result := '';
  167. case OS of
  168. freebsd, openbsd, netbsd :
  169. result := '-Fl'+GetDefaultLibGCCDir(CPU, OS, S);
  170. linux :
  171. begin
  172. if CPU in [i386, x86_64] then
  173. begin
  174. AddConditionalLinkerPath('cpui386', i386, result);
  175. AddConditionalLinkerPath('cpux86_64', x86_64, result);
  176. end
  177. else if CPU in [powerpc, powerpc64] then
  178. begin
  179. AddConditionalLinkerPath('cpupowerpc', powerpc, result);
  180. AddConditionalLinkerPath('cpupowerpc64', powerpc64, result);
  181. end
  182. end;
  183. darwin :
  184. begin
  185. AddConditionalLinkerPath('cpui386', i386, result);
  186. AddConditionalLinkerPath('cpux86_64', x86_64, result);
  187. AddConditionalLinkerPath('cpupowerpc', powerpc, result);
  188. AddConditionalLinkerPath('cpupowerpc64', powerpc64, result);
  189. { macOS 10.14 or later:
  190. 1) command line tools are installed under /Library/Developer/CommandLineTools
  191. 2) the system libraries still contain i386 code, but the 10.14 sdk doesn't
  192. (-> only use the 10.14 sdk when targeting x86_64 or unknown architectures )
  193. 3) crt1.o is no longer installed under /usr -> add its directory explicitly via
  194. -Fl
  195. We can't detect the macOS version inside fpc.cfg, unfortunately, so we can only
  196. insert this while generating the configuration file.
  197. This will stop working when macOS 10.15 is released without i386 support, but then
  198. users will be responsible for supplying their own i386 SDK anyway.
  199. }
  200. if DirectoryExists('/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk') then
  201. begin
  202. result:=result + LineEnding +
  203. '-FD/Library/Developer/CommandLineTools/usr/bin' + LineEnding +
  204. '#ifdef cpui386' + LineEnding +
  205. '-Fl/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib' + LineEnding +
  206. '#endif' + LineEnding +
  207. '#ifndef cpui386' + LineEnding +
  208. '#ifndef cpupowerpc' + LineEnding +
  209. '#ifndef cpupowerpc64' + LineEnding +
  210. '-XR/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk' + LineEnding +
  211. '#endif' + LineEnding +
  212. '#endif' + LineEnding +
  213. '#endif';
  214. end
  215. else
  216. begin
  217. { add Xcode.app binutils to search path}
  218. result:=result + LineEnding +
  219. '-FD/Applications/Xcode.app/Contents/Developer/usr/bin';
  220. end;
  221. end;
  222. end; {case}
  223. end;
  224. procedure Init;
  225. begin
  226. Verbose:=False;
  227. IDEBuildIn:=0;
  228. TemplateParser := TTemplateParser.Create;
  229. TemplateParser.StartDelimiter:='%';
  230. TemplateParser.EndDelimiter:='%';
  231. TemplateParser.Values['FPCVERSION'] := BuildVersion;
  232. TemplateParser.Values['FPCTARGET'] := BuildTarget;
  233. TemplateParser.Values['FPCTARGETOS'] := BuildOSTarget;
  234. TemplateParser.Values['FPCBIN'] := 'fpc';
  235. TemplateParser.Values['PWD'] := GetCurrentDir;
  236. TemplateParser.Values['BUILDDATE'] := DateToStr(Date);
  237. TemplateParser.Values['BUILDTIME'] := TimeToStr(Time);
  238. TemplateParser.Values['LOCALREPOSITORY'] := GetDefaultLocalRepository;
  239. TemplateParser.Values['LOCALBASEPATH'] := GetDefaultLocalBasepath;
  240. TemplateParser.Values['COMPILERCONFIGDIR'] := GetDefaultCompilerConfigDir;
  241. TemplateParser.Values['NEEDCROSSBINUTILSIFDEF'] := GetDefaultNeedCrossBinutilsIfdef;
  242. TemplateParser.Values['GCCLIBPATH'] := GetDefaultGCCDIR;
  243. Cfg:=TStringList.Create;
  244. Cfg.Text:=StrPas(Addr(DefaultConfig[0][1]));
  245. end;
  246. Procedure Done;
  247. begin
  248. FreeAndNil(Cfg);
  249. FreeAndNil(TemplateParser);
  250. end;
  251. Procedure Usage;
  252. begin
  253. Writeln(Format(SUsage00,[ExtractFileName(Paramstr(0))]));
  254. Writeln(SUsage10);
  255. Writeln(SUsage20);
  256. Writeln(SUsage30);
  257. Writeln(SUsage40);
  258. Writeln(SUsage50);
  259. Writeln(SUsage60);
  260. Writeln(SUsage70);
  261. Writeln(SUsage80);
  262. Writeln(SUsage84);
  263. Writeln(SUsage87);
  264. Writeln(SUsage90);
  265. Writeln(SUsage100);
  266. Writeln(SUsage110);
  267. Writeln(SUsage120);
  268. Writeln(SUsage130);
  269. Writeln(SUsage140);
  270. Halt(1);
  271. end;
  272. Procedure UnknownOption(Const S : String);
  273. begin
  274. Writeln(SErrUnknownOption,S);
  275. Usage;
  276. end;
  277. Procedure ShowBuiltIn;
  278. Var
  279. I : Integer;
  280. begin
  281. For I:=0 to Cfg.Count-1 do
  282. Writeln(Cfg[I]);
  283. end;
  284. Procedure ShowBuiltInMacros;
  285. Var
  286. I : Integer;
  287. begin
  288. For I:=0 to TemplateParser.ValueCount-1 do
  289. Writeln(TemplateParser.NamesByIndex[I]+'='+TemplateParser.ValuesByIndex[I]);
  290. end;
  291. Procedure ProcessCommandline;
  292. Var
  293. I : Integer;
  294. S : String;
  295. ShowBuiltinCommand : boolean;
  296. Function GetOptArg : String;
  297. begin
  298. If I=ParamCount then
  299. begin
  300. Writeln(StdErr,Format(SErrArgExpected,[S]));
  301. Halt(1);
  302. end;
  303. inc(I);
  304. Result:=ParamStr(I);
  305. end;
  306. procedure AddPair(const Value: String);
  307. var P: integer;
  308. N,V: String;
  309. begin
  310. P:=Pos('=',Value);
  311. If p=0 then
  312. begin
  313. Writeln(StdErr,Format(SErrIncompletePair,[Value]));
  314. Halt(1);
  315. end;
  316. V:=Value;
  317. N:=Copy(V,1,P-1);
  318. Delete(V,1,P);
  319. TemplateParser.Values[N] := V;
  320. end;
  321. begin
  322. I:=1;
  323. ShowBuiltinCommand := False;
  324. SkipBackup := False;
  325. CreateDir := False;
  326. While( I<=ParamCount) do
  327. begin
  328. S:=Paramstr(i);
  329. If Length(S)<=1 then
  330. UnknownOption(S)
  331. else
  332. case S[2] of
  333. 'v' : Verbose:=True;
  334. 'h' : Usage;
  335. 'b' : ShowBuiltinCommand := true;
  336. 'm' : begin
  337. ShowBuiltinMacros;
  338. halt(0);
  339. end;
  340. 't' : TemplateFileName:=GetOptArg;
  341. 'd' : AddPair(GetOptArg);
  342. 'u' : TemplateParser.Values[GetOptArg]:='';
  343. 'o' : OutputFileName:=GetoptArg;
  344. 's' : SkipBackup:=True;
  345. 'p' : CreateDir:=True;
  346. '0' : IDEBuildin:=0;
  347. '1' : IDEBuildin:=1;
  348. '2' : IDEBuildin:=2;
  349. '3' : IDEBuildin:=3;
  350. '4' : IDEBuildin:=4;
  351. else
  352. UnknownOption(S);
  353. end;
  354. Inc(I);
  355. end;
  356. If (TemplateFileName<>'') then
  357. begin
  358. If Not FileExists(TemplateFileName) then
  359. begin
  360. Writeln(StdErr,Format(SErrNoSuchFile,[TemplateFileName]));
  361. Halt(1);
  362. end;
  363. Cfg.LoadFromFile(TemplateFileName);
  364. TemplateParser.Values['TEMPLATEFILE'] := TemplateFileName;
  365. end
  366. else
  367. begin
  368. case IDEBuildin of
  369. 1:
  370. Cfg.Text:=StrPas(Addr(fpcfg[0][1]));
  371. 2:
  372. Cfg.Text:=StrPas(Addr(fpini[0][1]));
  373. 3:
  374. Cfg.Text:=StrPas(Addr(fppkg[0][1]));
  375. 4:
  376. Cfg.Text:=StrPas(Addr(fppkg_default[0][1]));
  377. end;
  378. TemplateParser.Values['TEMPLATEFILE'] := 'builtin';
  379. end;
  380. if ShowBuiltinCommand then
  381. begin
  382. ShowBuiltIn;
  383. halt(0);
  384. end;
  385. end;
  386. Procedure CreateFile;
  387. Var
  388. Fout : Text;
  389. S,BFN : String;
  390. I : Integer;
  391. begin
  392. if (OutputFileName<>'') and
  393. DirectoryExists(OutputFileName) then
  394. begin
  395. Writeln(StdErr,Format(SErrDestDirectory,[OutputFileName]));
  396. Halt(1);
  397. end;
  398. If (OutputFileName<>'')
  399. and FileExists(OutputFileName)
  400. and not SkipBackup then
  401. begin
  402. BFN:=ChangeFileExt(OutputFileName,'.bak');
  403. If FileExists(BFN) and not DeleteFile(BFN) then
  404. begin
  405. Writeln(StdErr,Format(SErrDelBackupFailed,[BFN]));
  406. Halt(1);
  407. end;
  408. If not RenameFile(OutputFileName,BFN) then
  409. begin
  410. Writeln(StdErr,Format(SErrBackupFailed,[OutputFileName,BFN]));
  411. Halt(1);
  412. end
  413. else
  414. Writeln(Format(SBackupCreated,[ExtractFileName(OutputFileName),ExtractFileName(BFN)]));
  415. end;
  416. if (OutputFileName<>'') and not DirectoryExists(ExtractFilePath(OutputFileName)) then
  417. begin
  418. if CreateDir then
  419. begin
  420. if not ForceDirectories(ExtractFilePath(OutputFileName)) then
  421. begin
  422. Writeln(StdErr,Format(SErrCreateDirFailed,[OutputFileName]));
  423. Halt(1);
  424. end;
  425. end
  426. else
  427. begin
  428. Writeln(StdErr,Format(SErrNoSuchDirectory,[OutputFileName]));
  429. Halt(1);
  430. end;
  431. end;
  432. Assign(Fout,OutputFileName);
  433. Rewrite(FOut);
  434. Try
  435. For I:=0 to Cfg.Count-1 do
  436. begin
  437. S:=Cfg[i];
  438. S := TemplateParser.ParseString(S);
  439. Writeln(FOut,S);
  440. end;
  441. Finally
  442. Close(Fout);
  443. end;
  444. end;
  445. begin
  446. Init;
  447. Try
  448. ProcessCommandLine;
  449. CreateFile;
  450. Finally
  451. Done;
  452. end;
  453. end.