reworkmakefile.pp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. {
  2. This file is part of the Free Component Library
  3. Copyright (c) 2022 by Michael Van Canneyt, [email protected]
  4. Rework makefile rules:
  5. Replace hardcoded unit names xyz in a rule with variable XYZUNIT.
  6. (see genunitnames for how to create the variables)
  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 reworkmakefile;
  14. uses strutils, regexpr, sysutils, classes, types, namespacetool, prefixer,
  15. custapp, rewritemakefile;
  16. Type
  17. { TRewriteMakeFileApp }
  18. TRewriteMakeFileApp = Class(TCustomApplication)
  19. Private
  20. FTool : TRewriteMakeFile;
  21. FFilenames : TStringArray;
  22. procedure ToolLog(Sender: TObject; EventType: TEventType; const Msg: String
  23. );
  24. Protected
  25. procedure DoLog(EventType: TEventType; const Msg: String); override;
  26. procedure Usage (aMsg : string);
  27. function ProcessOptions : Boolean;
  28. Procedure DoRun; override;
  29. Public
  30. Constructor Create(aOwner : TComponent); override;
  31. Destructor Destroy; override;
  32. end;
  33. { TRewriteMakeFileApp }
  34. procedure TRewriteMakeFileApp.ToolLog(Sender: TObject; EventType: TEventType;
  35. const Msg: String);
  36. begin
  37. DoLog(EventType,Msg);
  38. end;
  39. procedure TRewriteMakeFileApp.DoLog(EventType: TEventType; const Msg: String);
  40. begin
  41. Writeln('[',EventType,'] ',Msg);
  42. end;
  43. procedure TRewriteMakeFileApp.Usage(aMsg: string);
  44. begin
  45. if aMsg<>'' then
  46. Writeln('Error: ',aMsg);
  47. Writeln('Usage : ',ExtractFileName(ParamStr(0)),' [options] File1 [File2..FileN]');
  48. Writeln('-a --aliases=FILE Load aliases from FILE');
  49. Writeln('-c --common=FILE Load names of units that must be in $(NSINC) from FILE');
  50. Writeln('-s --skip=FILE Load names of units for which no rule must be made. ');
  51. ExitCode:=Ord(AMsg<>'');
  52. end;
  53. function TRewriteMakeFileApp.ProcessOptions: Boolean;
  54. Const
  55. ShortOpts = 'hc:a:s:';
  56. LongOpts : Array of string = ('help','common:','aliases:','skip:');
  57. Var
  58. S : String;
  59. begin
  60. Result:=False;
  61. S:=CheckOptions(ShortOpts,LongOpts);
  62. if (S<>'') or HasOPtion('h','help') then
  63. begin
  64. Usage(S);
  65. exit;
  66. end;
  67. FTool.AliasesFileName:=GetOptionValue('a','aliases');
  68. FTool.CommonUnitsFileName:=GetOptionValue('c','common');
  69. FTool.SkipUnitsFileName:=GetOptionValue('s','skip');
  70. FFilenames:=GetNonOptions(ShortOpts,LongOpts);
  71. Result:=(FTool.AliasesFileName<>'');
  72. if Not Result then
  73. begin
  74. Usage('Need aliases file');
  75. exit;
  76. end;
  77. Result:=Length(FFilenames)>0;
  78. if Not Result then
  79. begin
  80. Usage('Need file list');
  81. exit;
  82. end;
  83. end;
  84. procedure TRewriteMakeFileApp.DoRun;
  85. var
  86. aFile : String;
  87. begin
  88. StopOnException:=True;
  89. Terminate;
  90. if not ProcessOptions then
  91. exit;
  92. For aFile in FFilenames do
  93. FTool.HandleMakeFile(aFile);
  94. end;
  95. constructor TRewriteMakeFileApp.Create(aOwner: TComponent);
  96. begin
  97. inherited Create(aOwner);
  98. FTool:=TRewriteMakeFile.Create(Self);
  99. FTool.OnLog:=@ToolLog;
  100. end;
  101. destructor TRewriteMakeFileApp.Destroy;
  102. begin
  103. FreeAndNil(FTool);
  104. inherited Destroy;
  105. end;
  106. begin
  107. With TRewriteMakeFileApp.Create(nil) do
  108. try
  109. Initialize;
  110. Run;
  111. finally
  112. Free;
  113. end;
  114. end.