Przeglądaj źródła

+ started working on a WebAssembly resource writer for fcl-res and the fpcres tool

Nikolay Nikolov 1 rok temu
rodzic
commit
60017c1034

+ 5 - 0
packages/fcl-res/fpmake.pp

@@ -116,6 +116,11 @@ begin
           AddInclude('elfsubwriter.inc');
           AddInclude('elfdefaulttarget.inc');
         end;
+    T:=P.Targets.AddUnit('wasmwriter.pp');
+      with T.Dependencies do
+        begin
+          AddUnit('resource');
+        end;
     T:=P.Targets.AddUnit('externaltypes.pp');
     T:=P.Targets.AddUnit('externalreader.pp');
       with T.Dependencies do

+ 3 - 0
packages/fcl-res/namespaced/System.Resources.WebAssembly.Writer.pp

@@ -0,0 +1,3 @@
+unit System.Resources.WebAssembly.Writer;
+{$DEFINE FPC_DOTTEDUNITS}
+{$i wasmwriter.pp}

+ 78 - 0
packages/fcl-res/src/wasmwriter.pp

@@ -0,0 +1,78 @@
+{
+    This file is part of the Free Pascal run time library.
+    Copyright (c) 2008 by Giulio Bernardi
+    Copyright (c) 2024 by Nikolay Nikolov
+
+    Resource writer for WebAssembly files
+
+    See the file COPYING.FPC, included in this distribution,
+    for details about the copyright.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+ **********************************************************************}
+
+{$IFNDEF FPC_DOTTEDUNITS}
+unit wasmwriter;
+{$ENDIF FPC_DOTTEDUNITS}
+
+{$MODE OBJFPC} {$H+}
+
+interface
+
+{$IFDEF FPC_DOTTEDUNITS}
+uses
+  System.Classes, System.SysUtils, System.Resources.Resource;
+{$ELSE FPC_DOTTEDUNITS}
+uses
+  Classes, SysUtils, resource;
+{$ENDIF FPC_DOTTEDUNITS}
+
+type
+
+  { TWasmResourceWriter }
+
+  TWasmResourceWriter = class(TAbstractResourceWriter)
+  private
+    fExtensions : string;
+    fDescription : string;
+  protected
+    function GetExtensions : string; override;
+    function GetDescription : string; override;
+    procedure Write(aResources : TResources; aStream : TStream); override;
+  public
+    constructor Create; override;
+  end;
+
+implementation
+
+{ TWasmResourceWriter }
+
+function TWasmResourceWriter.GetExtensions: string;
+begin
+  Result:=fExtensions;
+end;
+
+function TWasmResourceWriter.GetDescription: string;
+begin
+  Result:=fDescription;
+end;
+
+procedure TWasmResourceWriter.Write(aResources: TResources; aStream: TStream);
+begin
+
+end;
+
+constructor TWasmResourceWriter.Create;
+begin
+  fExtensions:='.o .or';
+  fDescription:='WebAssembly resource writer';
+end;
+
+initialization
+  TResources.RegisterWriter('.o',TWasmResourceWriter);
+  TResources.RegisterWriter('.or',TWasmResourceWriter);
+
+end.

+ 8 - 1
utils/fpcres/fpcres.pas

@@ -25,7 +25,8 @@ uses
   resreader, coffreader, winpeimagereader, elfreader, machoreader,
   externalreader, dfmreader, tlbreader, rcreader,
 //writers
-  reswriter, coffwriter, xcoffwriter, elfwriter, machowriter, externalwriter,
+  reswriter, coffwriter, xcoffwriter, elfwriter, machowriter, wasmwriter,
+  externalwriter,
 //misc
   elfconsts, cofftypes, machotypes, externaltypes;
   
@@ -335,6 +336,11 @@ begin
   Result.SubMachineType:=MachOSubMachineType;
 end;
 
+function SetUpWasmWriter : TWasmResourceWriter;
+begin
+  Result:=TWasmResourceWriter.Create;
+end;
+
 
 function SetUpExternalWriter : TExternalResourceWriter;
 begin
@@ -366,6 +372,7 @@ begin
       ofCoff  : aWriter:=SetUpCoffWriter;
       ofXCoff : aWriter:=SetUpXCoffWriter;
       ofMachO : aWriter:=SetUpMachOWriter;
+      ofWasm  : aWriter:=SetUpWasmWriter;
       ofExt   : aWriter:=SetUpExternalWriter;
     end;
     try

+ 11 - 3
utils/fpcres/target.pas

@@ -23,7 +23,7 @@ interface
 type
   TMachineType = (mtnone, mti386,mtx86_64,mtppc,mtppc64,mtarm,mtarmeb,mtm68k,
                   mtsparc,mtalpha,mtia64,mtmips,mtmipsel,mtaarch64,mtppc64le,
-                  mtriscv32,mtriscv64,mtloongarch64,mtsparc64,
+                  mtriscv32,mtriscv64,mtloongarch64,mtsparc64,mtwasm32,
                   mtBigEndian,mtLittleEndian);
   TMachineTypes = set of TMachineType;
 
@@ -36,12 +36,12 @@ type
         (subarm: TSubMachineTypeArm);
       mtnone, mti386,mtx86_64,mtppc,mtppc64,mtm68k,
       mtsparc,mtalpha,mtia64,mtmips,mtmipsel,mtaarch64,mtppc64le,
-      mtriscv32,mtriscv64,mtloongarch64,mtsparc64,
+      mtriscv32,mtriscv64,mtloongarch64,mtsparc64,mtwasm32,
       mtBigEndian,mtLittleEndian:
         (subgen: TSubMachineTypeGeneric);
   end;
 
-  TObjFormat = (ofNone, ofRes, ofElf, ofCoff, ofXCoff, ofMachO, ofExt);
+  TObjFormat = (ofNone, ofRes, ofElf, ofCoff, ofXCoff, ofMachO, ofWasm, ofExt);
   TObjFormats = set of TObjFormat;
   
 
@@ -91,6 +91,7 @@ var
     (name : 'riscv64';      formats : [ofElf]),                   //mtriscv64
     (name : 'loongarch64';  formats : [ofElf]),                   //mtloongarch64
     (name : 'sparc64';      formats : [ofElf]),                   //mtsparc64
+    (name : 'wasm32';       formats : [ofWasm]),                  //mtwasm32
     (name : 'bigendian';    formats : [ofExt]),                   //mtBigEndian
     (name : 'littleendian'; formats : [ofExt])                    //mtLittleEndian
   );
@@ -116,6 +117,7 @@ var
     (name : 'xcoff';    ext : '.o';      machines : [mtppc{,mtppc64}]),
     (name : 'mach-o';   ext : '.or';     machines : [mti386,mtx86_64,mtppc,
                                                      mtppc64,mtarm,mtaarch64]),
+    (name : 'wasm';     ext : '.or';     machines : [mtwasm32]),
     (name : 'external'; ext : '.fpcres'; machines : [mtBigEndian,mtLittleEndian])
   );
 
@@ -179,6 +181,9 @@ var
   {$elseif defined(CPULOONGARCH64)}
     machine : mtloongarch64;
     submachine : (subgen: smtgen_all);
+  {$elseif defined(CPUWASM32)}
+    machine : mtwasm32;
+    submachine : (subgen: smtgen_all);
   {$else}
     machine : mti386;  //default i386
     submachine : (subgen: smtgen_all);
@@ -191,6 +196,8 @@ var
       objformat : ofMachO;
     {$ELSEIF defined(AIX)}
       objformat : ofXCoff;
+    {$ELSEIF defined(WASI)}
+      objformat : ofWasm;
     {$ELSE}
       objformat : ofElf;
     {$ENDIF}
@@ -209,6 +216,7 @@ begin
     ofCoff : Result:=mti386;
     ofXCoff: Result:=mtppc;
     ofMachO: Result:=mti386;
+    ofWasm : Result:=mtwasm32;
     {$IFDEF ENDIAN_BIG}
     ofExt  : Result:=mtBigEndian;
     {$ELSE}