fpcmkcfg.pp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 usubst,SysUtils,Classes;
  15. {
  16. The inc files must be built from a template with the data2inc
  17. command.
  18. data2inc -b -s fpc.cft fpccfg.inc DefaultConfig
  19. data2inc -b -s fpinc.ini fpini.inc fpini
  20. data2inc -b -s fpinc.cfg fpcfg.inc fpcfg
  21. }
  22. {$i fpccfg.inc}
  23. {$i fpini.inc}
  24. {$i fpcfg.inc}
  25. Const
  26. BuildVersion={$I %FPCVERSION%};
  27. BuildTarget={$I %FPCTARGET%};
  28. BuildOSTarget={$I %FPCTARGETOS%};
  29. Resourcestring
  30. SUsage00 = 'Usage: %s [options]';
  31. SUsage10 = 'Where options is one or more of';
  32. SUSage20 = ' -t filename Template file name. Default is built-in';
  33. SUSage30 = ' -o filename Set output file. Default is standard output.';
  34. SUsage40 = ' -d name=value define name=value pair.';
  35. SUsage50 = ' -h show this help and exit.';
  36. SUsage60 = ' -u name remove name from list of name/value pairs.';
  37. SUsage70 = ' -l filename read name/value pairs from filename';
  38. SUsage80 = ' -b show builtin template and exit.';
  39. SUsage90 = ' -v be verbose.';
  40. Susage100 = ' -0 use built in fpc.cfg template (default)';
  41. Susage110 = ' -1 use built in fp.cfg template';
  42. Susage120 = ' -2 use built in fp.ini template';
  43. SErrUnknownOption = 'Error: Unknown option.';
  44. SErrArgExpected = 'Error: Option "%s" requires an argument.';
  45. SErrNoSuchFile = 'Error: File "%s" does not exist.';
  46. SErrBackupFailed = 'Error: Backup of file "%s" to "%s" failed.';
  47. SErrDelBackupFailed = 'Error: Delete of old backup file "%s" failed.';
  48. SWarnIgnoringFile = 'Warning: Ignoring non-existent file: ';
  49. SWarnIgnoringPair = 'Warning: ignoring wrong name/value pair: ';
  50. SStats = 'Replaced %d placeholders in %d lines.';
  51. SSubstInLine = 'Replaced %s placeholders in line %d.';
  52. Var
  53. Verbose : Boolean;
  54. SkipBackup : Boolean;
  55. List,Cfg : TStringList;
  56. TemplateFileName,
  57. OutputFileName : String;
  58. IDEBuildin : Integer;
  59. procedure Init;
  60. begin
  61. Verbose:=False;
  62. IDEBuildIn:=0;
  63. List:=TStringList.Create;
  64. AddToList(List,'FPCVERSION',BuildVersion);
  65. AddToList(List,'FPCTARGET',BuildTarget);
  66. AddToList(List,'FPCTARGETOS',BuildOSTarget);
  67. AddToList(List,'PWD',GetCurrentDir);
  68. AddToList(List,'BUILDDATE',DateToStr(Date));
  69. AddToList(List,'BUILDTIME',TimeToStr(Time));
  70. Cfg:=TStringList.Create;
  71. Cfg.Text:=StrPas(Addr(DefaultConfig[0][1]));
  72. end;
  73. Procedure Done;
  74. begin
  75. FreeAndNil(List);
  76. FreeAndNil(Cfg);
  77. end;
  78. Procedure Usage;
  79. begin
  80. Writeln(Format(SUsage00,[ExtractFileName(Paramstr(0))]));
  81. Writeln(SUsage10);
  82. Writeln(SUsage20);
  83. Writeln(SUsage30);
  84. Writeln(SUsage40);
  85. Writeln(SUsage50);
  86. Writeln(SUsage60);
  87. Writeln(SUsage70);
  88. Writeln(SUsage80);
  89. Writeln(SUsage90);
  90. Writeln(SUsage100);
  91. Writeln(SUsage110);
  92. Writeln(SUsage120);
  93. Halt(1);
  94. end;
  95. Procedure UnknownOption(Const S : String);
  96. begin
  97. Writeln(SErrUnknownOption,S);
  98. Usage;
  99. end;
  100. Procedure ShowBuiltIn;
  101. Var
  102. I : Integer;
  103. begin
  104. For I:=0 to Cfg.Count-1 do
  105. Writeln(Cfg[I]);
  106. end;
  107. Procedure ProcessCommandline;
  108. Var
  109. I : Integer;
  110. S : String;
  111. Function GetOptArg : String;
  112. begin
  113. If I=ParamCount then
  114. begin
  115. Writeln(StdErr,Format(SErrArgExpected,[S]));
  116. Halt(1);
  117. end;
  118. inc(I);
  119. Result:=ParamStr(I);
  120. end;
  121. begin
  122. I:=1;
  123. While( I<=ParamCount) do
  124. begin
  125. S:=Paramstr(i);
  126. If Length(S)<=1 then
  127. UnknownOption(S)
  128. else
  129. case S[2] of
  130. 'v' : Verbose:=True;
  131. 'h' : Usage;
  132. 'b' : begin
  133. ShowBuiltin;
  134. halt(0);
  135. end;
  136. 't' : TemplateFileName:=GetOptArg;
  137. 'd' : AddPair(List,GetOptArg);
  138. 'u' : AddPair(List,GetOptArg+'=');
  139. 'o' : OutputFileName:=GetoptArg;
  140. 's' : SkipBackup:=True;
  141. '0' : IDEBuildin:=0;
  142. '1' : IDEBuildin:=1;
  143. '2' : IDEBuildin:=2;
  144. else
  145. UnknownOption(S);
  146. end;
  147. Inc(I);
  148. end;
  149. If (TemplateFileName<>'') then
  150. begin
  151. If Not FileExists(TemplateFileName) then
  152. begin
  153. Writeln(StdErr,Format(SErrNoSuchFile,[TemplateFileName]));
  154. Halt(1);
  155. end;
  156. Cfg.LoadFromFile(TemplateFileName);
  157. AddToList(List,'TEMPLATEFILE',TemplateFileName);
  158. end
  159. else
  160. begin
  161. case IDEBuildin of
  162. 1:
  163. Cfg.Text:=StrPas(Addr(fpcfg[0][1]));
  164. 2:
  165. Cfg.Text:=StrPas(Addr(fpini[0][1]));
  166. end;
  167. AddToList(List,'TEMPLATEFILE','builtin');
  168. end;
  169. end;
  170. Procedure CreateFile;
  171. Var
  172. Fout : Text;
  173. S,BFN : String;
  174. I,RCount : INteger;
  175. begin
  176. If (OutputFileName<>'')
  177. and FileExists(OutputFileName)
  178. and not SkipBackup then
  179. begin
  180. BFN:=ChangeFileExt(OutputFileName,'.bak');
  181. If FileExists(BFN) and not DeleteFile(BFN) then
  182. begin
  183. Writeln(StdErr,Format(SErrDelBackupFailed,[BFN]));
  184. Halt(1);
  185. end;
  186. If not RenameFile(OutputFileName,BFN) then
  187. begin
  188. Writeln(StdErr,Format(SErrBackupFailed,[OutputFileName,BFN]));
  189. Halt(1);
  190. end;
  191. end;
  192. Assign(Fout,OutputFileName);
  193. Rewrite(FOut);
  194. Try
  195. RCount:=0;
  196. For I:=0 to Cfg.Count-1 do
  197. begin
  198. S:=Cfg[i];
  199. Inc(RCount,DoSubstitutions(List,S));
  200. Writeln(FOut,S);
  201. end;
  202. If Verbose then
  203. Writeln(StdErr,Format(SStats,[RCount,Cfg.Count]));
  204. Finally
  205. Close(Fout);
  206. end;
  207. end;
  208. begin
  209. Init;
  210. Try
  211. ProcessCommandLine;
  212. CreateFile;
  213. Finally
  214. Done;
  215. end;
  216. end.