fpcmkcfg.pp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. {$mode objfpc}
  2. {$H+}
  3. {
  4. $Id$
  5. This file is part of Free Pascal Build tools
  6. Copyright (c) 2005 by Michael Van Canneyt
  7. Create a configuration file
  8. See the file COPYING.FPC, included in this distribution,
  9. for details about the copyright.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. **********************************************************************}
  14. program fpcmkcfg;
  15. uses usubst,SysUtils,Classes;
  16. {
  17. The fpccfg.inc file must be built from a template with the bin2obj
  18. command. it needs options:
  19. bin2obj -a -o fpccfg.inc -c DefaultConfig fpc.cft
  20. where fpc.cft is the template file.
  21. }
  22. {$i fpccfg.inc}
  23. Const
  24. BuildVersion={$I %FPCVERSION%};
  25. BuildTarget={$I %FPCTARGET%};
  26. Resourcestring
  27. SUsage00 = 'Usage: %s [options]';
  28. SUsage10 = 'Where options is one or more of';
  29. SUSage20 = ' -t filename Template file name. Default is built-in';
  30. SUSage30 = ' -o filename Set output file. Default is standard output.';
  31. SUsage40 = ' -d name=value define name=value pair.';
  32. SUsage50 = ' -h show this help and exit.';
  33. SUsage60 = ' -u name remove name from list of name/value pairs.';
  34. SUsage70 = ' -l filename read name/value pairs from filename';
  35. SUsage80 = ' -b show builtin template and exit.';
  36. SUsage90 = ' -v be verbose.';
  37. SErrUnknownOption = 'Error: Unknown option.';
  38. SErrArgExpected = 'Error: Option "%s" requires an argument.';
  39. SErrNoSuchFile = 'Error: File "%s" does not exist.';
  40. SErrBackupFailed = 'Error: Backup of file "%s" to "%s" failed.';
  41. SErrDelBackupFailed = 'Error: Delete of old backup file "%s" failed.';
  42. SWarnIgnoringFile = 'Warning: Ignoring non-existent file: ';
  43. SWarnIgnoringPair = 'Warning: ignoring wrong name/value pair: ';
  44. SStats = 'Replaced %d placeholders in %d lines.';
  45. SSubstInLine = 'Replaced %s placeholders in line %d.';
  46. Var
  47. Verbose : Boolean;
  48. SkipBackup : Boolean;
  49. List,Cfg : TStringList;
  50. TemplateFileName,
  51. OutputFileName : String;
  52. procedure Init;
  53. begin
  54. Verbose:=False;
  55. List:=TStringList.Create;
  56. AddToList(List,'FPCVERSION',BuildVersion);
  57. AddToList(List,'FPCTARGET',BuildTarget);
  58. AddToList(List,'PWD',GetCurrentDir);
  59. AddToList(List,'BUILDDATE',DateToStr(Date));
  60. AddToList(List,'BUILDTIME',TimeToStr(Time));
  61. Cfg:=TStringList.Create;
  62. Cfg.Text:=StrPas(Addr(DefaultConfig));
  63. end;
  64. Procedure Done;
  65. begin
  66. FreeAndNil(List);
  67. FreeAndNil(Cfg);
  68. end;
  69. Procedure Usage;
  70. begin
  71. Writeln(Format(SUsage00,[ExtractFileName(Paramstr(0))]));
  72. Writeln(SUsage10);
  73. Writeln(SUsage20);
  74. Writeln(SUsage30);
  75. Writeln(SUsage40);
  76. Writeln(SUsage50);
  77. Writeln(SUsage60);
  78. Writeln(SUsage70);
  79. Writeln(SUsage80);
  80. Writeln(SUsage90);
  81. Halt(1);
  82. end;
  83. Procedure UnknownOption(Const S : String);
  84. begin
  85. Writeln(SErrUnknownOption,S);
  86. Usage;
  87. end;
  88. Procedure ShowBuiltIn;
  89. Var
  90. I : Integer;
  91. begin
  92. For I:=0 to Cfg.Count-1 do
  93. Writeln(Cfg[I]);
  94. end;
  95. Procedure ProcessCommandline;
  96. Var
  97. I : Integer;
  98. S : String;
  99. Function GetOptArg : String;
  100. begin
  101. If I=ParamCount then
  102. begin
  103. Writeln(StdErr,Format(SErrArgExpected,[S]));
  104. Halt(1);
  105. end;
  106. inc(I);
  107. Result:=ParamStr(I);
  108. end;
  109. begin
  110. I:=1;
  111. While( I<=ParamCount) do
  112. begin
  113. S:=Paramstr(i);
  114. If Length(S)<=1 then
  115. UnknownOption(S)
  116. else
  117. case S[2] of
  118. 'v' : Verbose:=True;
  119. 'h' : Usage;
  120. 'b' : begin
  121. ShowBuiltin;
  122. halt(0);
  123. end;
  124. 't' : TemplateFileName:=GetOptArg;
  125. 'd' : AddPair(List,GetOptArg);
  126. 'u' : AddPair(List,GetOptArg+'=');
  127. 'o' : OutputFileName:=GetoptArg;
  128. 's' : SkipBackup:=True;
  129. else
  130. UnknownOption(S);
  131. end;
  132. Inc(I);
  133. end;
  134. If (TemplateFileName<>'') then
  135. begin
  136. If Not FileExists(TemplateFileName) then
  137. begin
  138. Writeln(StdErr,Format(SErrNoSuchFile,[TemplateFileName]));
  139. Halt(1);
  140. end;
  141. Cfg.LoadFromFile(TemplateFileName);
  142. AddToList(List,'TEMPLATEFILE',TemplateFileName);
  143. end
  144. else
  145. AddToList(List,'TEMPLATEFILE','builtin');
  146. end;
  147. Procedure CreateFile;
  148. Var
  149. Fout : Text;
  150. S,BFN : String;
  151. I,RCount : INteger;
  152. begin
  153. If (OutputFileName<>'')
  154. and FileExists(OutputFileName)
  155. and not SkipBackup then
  156. begin
  157. BFN:=ChangeFileExt(OutputFileName,'.bak');
  158. If FileExists(BFN) and not DeleteFile(BFN) then
  159. begin
  160. Writeln(StdErr,Format(SErrDelBackupFailed,[BFN]));
  161. Halt(1);
  162. end;
  163. If not RenameFile(OutputFileName,BFN) then
  164. begin
  165. Writeln(StdErr,Format(SErrBackupFailed,[OutputFileName,BFN]));
  166. Halt(1);
  167. end;
  168. end;
  169. Assign(Fout,OutputFileName);
  170. Rewrite(FOut);
  171. Try
  172. RCount:=0;
  173. For I:=0 to Cfg.Count-1 do
  174. begin
  175. S:=Cfg[i];
  176. Inc(RCount,DoSubstitutions(List,S));
  177. Writeln(FOut,S);
  178. end;
  179. If Verbose then
  180. Writeln(StdErr,Format(SStats,[RCount,Cfg.Count]));
  181. Finally
  182. Close(Fout);
  183. end;
  184. end;
  185. begin
  186. Init;
  187. Try
  188. ProcessCommandLine;
  189. CreateFile;
  190. Finally
  191. Done;
  192. end;
  193. end.
  194. {
  195. $Log$
  196. Revision 1.3 2005-03-25 21:21:30 jonas
  197. * fixeed uninitialised variable
  198. - removed unused local variables
  199. Revision 1.2 2005/02/14 17:13:10 peter
  200. * truncate log
  201. Revision 1.1 2005/02/05 10:25:30 peter
  202. * move tools to compiler/utils/
  203. Revision 1.1 2005/01/09 13:36:12 michael
  204. + Initial implementation of installer tools
  205. }