fpcmkcfg.pp 5.1 KB

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