fpcmkcfg.pp 5.8 KB

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