fpgmake.pp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. program fpgmake;
  2. {$mode objfpc}{$H+}
  3. uses
  4. {$ifdef UNIX}
  5. cthreads,
  6. {$endif UNIX}
  7. Classes,
  8. sysutils,
  9. fpmkunit,
  10. fpTemplate,
  11. fpmakeParseJSon, fpmakecreatefile;
  12. {
  13. data2inc -b -s fpmake.cft fpmake.inc fpmake
  14. }
  15. {$i fpmake.inc}
  16. Resourcestring
  17. SUsage00 = 'Usage: %s [options]';
  18. SUsage10 = 'Where options is one or more of';
  19. SUSage20 = ' -t filename Template file name. Default is built-in';
  20. SUSage30 = ' -o filename Set output file. Default is standard output.';
  21. SUsage40 = ' -d name=value define name=value pair.';
  22. SUsage50 = ' -h show this help and exit.';
  23. SUsage60 = ' -u name remove name from list of name/value pairs.';
  24. SUsage70 = ' -m show builtin macros and exit.';
  25. SUsage80 = ' -b show builtin template and exit.';
  26. SUsage90 = ' -s skip the creation of a backup-file.';
  27. SUsage95 = ' -p force directory creation.';
  28. SError = 'Error:';
  29. SErrUnknownOption = 'Error: Unknown option (%s).';
  30. SErrArgExpected = 'Error: Option "%s" requires an argument.';
  31. SErrIncompletePair = 'Error: Incomplete name-value pair "%s".';
  32. SErrNoSuchFile = 'Error: File "%s" does not exist.';
  33. SWarnIgnoringFile = 'Warning: Ignoring non-existent file: ';
  34. SWarnIgnoringPair = 'Warning: Ignoring wrong name/value pair: ';
  35. SWarngccNotFound = 'Warning: Could not find gcc. Unable to determine the gcclib path.';
  36. SWarnCouldNotExecute= 'Warning: Could not execute command ''%s''';
  37. Var
  38. SkipBackup : Boolean;
  39. CreateDir: Boolean;
  40. Cfg : TStringList;
  41. TemplateFileName,
  42. OutputFileName : String;
  43. const
  44. InputFileName = 'fpmake.fpc';
  45. procedure Usage;
  46. begin
  47. Writeln(Format(SUsage00,[ExtractFileName(ApplicationName)]));
  48. Writeln(SUsage10);
  49. Writeln(SUsage20);
  50. Writeln(SUsage30);
  51. Writeln(SUsage40);
  52. Writeln(SUsage50);
  53. Writeln(SUsage60);
  54. Writeln(SUsage70);
  55. Writeln(SUsage80);
  56. Writeln(SUsage90);
  57. Writeln(SUsage95);
  58. end;
  59. Procedure UnknownOption(Const S : String);
  60. begin
  61. Writeln(Format(SErrUnknownOption,[S]));
  62. Usage;
  63. Halt(1);
  64. end;
  65. procedure Init;
  66. begin
  67. Cfg:=TStringList.Create;
  68. Cfg.Text:=StrPas(Addr(fpmake[0][1]));
  69. end;
  70. procedure Done;
  71. begin
  72. Cfg.Free;
  73. end;
  74. Procedure ShowBuiltInMacros;
  75. Var
  76. I : Integer;
  77. begin
  78. For I:=0 to TemplateParser.ValueCount-1 do
  79. Writeln(TemplateParser.NamesByIndex[I]+'='+TemplateParser.ValuesByIndex[I]);
  80. end;
  81. Procedure ShowBuiltIn;
  82. Var
  83. I : Integer;
  84. begin
  85. For I:=0 to Cfg.Count-1 do
  86. Writeln(Cfg[I]);
  87. end;
  88. Procedure ProcessCommandline;
  89. Var
  90. I : Integer;
  91. S : String;
  92. ShowBuiltinCommand : boolean;
  93. Function GetOptArg : String;
  94. begin
  95. If I=ParamCount then
  96. begin
  97. Writeln(StdErr,Format(SErrArgExpected,[S]));
  98. Halt(1);
  99. end;
  100. inc(I);
  101. Result:=ParamStr(I);
  102. end;
  103. procedure AddPair(const Value: String);
  104. var P: integer;
  105. N,V: String;
  106. begin
  107. P:=Pos('=',Value);
  108. If p=0 then
  109. begin
  110. Writeln(StdErr,Format(SErrIncompletePair,[Value]));
  111. Halt(1);
  112. end;
  113. V:=Value;
  114. N:=Copy(V,1,P-1);
  115. Delete(V,1,P);
  116. TemplateParser.Values[N] := V;
  117. end;
  118. begin
  119. I:=1;
  120. ShowBuiltinCommand := False;
  121. SkipBackup := False;
  122. CreateDir := False;
  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. 'h' : begin
  131. Usage;
  132. halt(0);
  133. end;
  134. 'b' : ShowBuiltinCommand := true;
  135. 'm' : begin
  136. ShowBuiltinMacros;
  137. halt(0);
  138. end;
  139. 't' : TemplateFileName:=GetOptArg;
  140. 'd' : AddPair(GetOptArg);
  141. 'u' : TemplateParser.Values[GetOptArg]:='';
  142. 'o' : OutputFileName:=GetoptArg;
  143. 's' : SkipBackup:=True;
  144. 'p' : CreateDir:=True;
  145. else
  146. UnknownOption(S);
  147. end;
  148. Inc(I);
  149. end;
  150. If (TemplateFileName<>'') then
  151. begin
  152. If Not FileExists(TemplateFileName) then
  153. begin
  154. Writeln(StdErr,Format(SErrNoSuchFile,[TemplateFileName]));
  155. Halt(1);
  156. end;
  157. Cfg.LoadFromFile(TemplateFileName);
  158. TemplateParser.Values['TEMPLATEFILE'] := TemplateFileName;
  159. end;
  160. if ShowBuiltinCommand then
  161. begin
  162. ShowBuiltIn;
  163. halt(0);
  164. end;
  165. end;
  166. var
  167. APackages: TPackages;
  168. begin
  169. Init;
  170. Try
  171. ProcessCommandLine;
  172. APackages := ParseFpmakeFile(InputFileName);
  173. if assigned(APackages) then
  174. CreateFile(OutputFileName, Cfg, APackages, SkipBackup, CreateDir);
  175. Finally
  176. APackages.Free;
  177. Done;
  178. end;
  179. end.