wasmtool.lpr 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. { This file is part of wasmbin - a collection of WebAssembly binary utils.
  2. Copyright (C) 2019, 2020 Dmitry Boyarintsev <[email protected]>
  3. Copyright (C) 2020 by the Free Pascal development team
  4. This source is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 2 of the License, or (at your option)
  7. any later version.
  8. This code is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  11. details.
  12. A copy of the GNU General Public License is available on the World Wide Web
  13. at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing
  14. to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  15. Boston, MA 02110-1335, USA.
  16. }
  17. program wasmtool;
  18. {$mode objfpc}{$H+}
  19. uses
  20. {$IFDEF UNIX}{$IFDEF UseCThreads}
  21. cthreads,
  22. {$ENDIF}{$ENDIF}
  23. { you can add units after this }
  24. Classes, SysUtils, wasmbin, lebutils,
  25. wasmbindebug, wasmlink, wasmlinkchange,
  26. wasmtoolutils;
  27. const
  28. ACT_EXPORTRENAME = 'exportrename';
  29. ACT_SYMBOLFLAG = 'symbolflag';
  30. ACT_SYMBOLAUTO = 'symbolauto';
  31. ACT_WEAK = 'weak';
  32. VERSION = '1.1';
  33. procedure PrintHelp;
  34. begin
  35. writeln('wasmtool [options] .wasm file...');
  36. writeln();
  37. writeln('version: ',VERSION);
  38. writeln;
  39. writeln('options:');
  40. writeln(' --exportrename @inputfile - renaming export names');
  41. writeln(' --symbolflag @inputfile - update symbol flags as specified in input');
  42. writeln(' --symbolauto - update symbol by the use ');
  43. writeln(' --weak %name% - specify symbol names for global variables to be marked weak reference');
  44. end;
  45. type
  46. { TToolActions }
  47. // it's assumed that the wasmtool will be instructed
  48. // to take mulitple actions on the same file.
  49. // thus every action is recorded as TToolActions
  50. // Those are created during the parameters parsing
  51. TToolActions = class(TObject)
  52. action : string; // action to take place
  53. paramsFn : string; // input file name
  54. constructor Create(const aaction, afilename: string);
  55. end;
  56. { TToolActions }
  57. constructor TToolActions.Create(const aaction, afilename: string);
  58. begin
  59. inherited Create;
  60. action := aaction;
  61. paramsFn := afilename;
  62. end;
  63. procedure ProcessParams(acts: TList; const inputFn: string; doVerbose: Boolean);
  64. var
  65. i : integer;
  66. j : integer;
  67. ta : TToolActions;
  68. wk : TStringList;
  69. begin
  70. for i:=0 to acts.Count-1 do begin
  71. ta := TToolActions(acts[i]);
  72. if doVerbose then writeln('action: "',ta.action,'"');
  73. if ta.action = ACT_EXPORTRENAME then begin
  74. if doVerbose then begin
  75. writeln(' input: ',inputFn);
  76. writeln(' params: ',ta.paramsFn);
  77. end;
  78. ExportRename(inputFn, ta.paramsFn, doVerbose);
  79. end else if ta.action = ACT_SYMBOLFLAG then begin
  80. ChangeSymbolFlag(inputFn, ta.paramsFn);
  81. end else if (ta.action = ACT_SYMBOLAUTO) then begin
  82. wk := TStringList.Create;
  83. for j:=0 to acts.Count-1 do begin
  84. if TToolActions(acts[j]).action = ACT_WEAK then
  85. wk.Add( TToolActions(acts[j]).paramsFn );
  86. end;
  87. PredictSymbolsFromLink(inputFn, wk, doVerbose);
  88. wk.free;
  89. end;
  90. end;
  91. end;
  92. var
  93. acts: TList = nil;
  94. inputFn: string = '';
  95. verbose: Boolean = false;
  96. procedure ParseParams;
  97. var
  98. i : integer;
  99. s : string;
  100. ls : string;
  101. fn : string;
  102. begin
  103. i:=1;
  104. while i<=ParamCount do begin
  105. s := ParamStr(i);
  106. ls := AnsiLowerCase(s);
  107. inc(i);
  108. if Pos('-', ls)=1 then begin
  109. if ls = '-v' then
  110. ls := '--verbose';
  111. end;
  112. if Pos('--',ls)=1 then begin
  113. ls := Copy(ls, 3, length(ls)-2);
  114. if (ls = 'verbose') then
  115. verbose := true
  116. else if (ls = ACT_SYMBOLAUTO) then
  117. acts.Add( TToolActions.Create(ls, ''))
  118. else begin
  119. if i<=ParamCount then begin
  120. fn:=ParamStr(i);
  121. inc(i);
  122. end else
  123. fn := '';
  124. if fn <> '' then
  125. acts.Add( TToolActions.Create(ls, fn));
  126. end;
  127. end else begin
  128. if inputFn ='' then
  129. inputFn:=s;
  130. end;
  131. end;
  132. end;
  133. var
  134. i : integer;
  135. begin
  136. if ParamCount=0 then begin
  137. PrintHelp;
  138. Exit;
  139. end;
  140. try
  141. acts := TList.Create;
  142. try
  143. ParseParams;
  144. ProcessParams(acts, inputFn, verbose);
  145. finally
  146. for i:=0 to acts.Count-1 do
  147. TObject(acts[i]).Free;
  148. acts.Free;
  149. end;
  150. except
  151. on e:exception do begin
  152. writeln('error: ', e.Message);
  153. ExitCode:=1;
  154. end;
  155. end;
  156. end.