Răsfoiți Sursa

* Allow compilation for wasm

Michaël Van Canneyt 1 an în urmă
părinte
comite
9d00605c35

+ 1 - 1
packages/pastojs/fpmake.pp

@@ -20,7 +20,7 @@ begin
 {$endif ALLPACKAGES}
 
     P.Version:='3.3.1';
-    P.OSes:=AllUnixOSes+AllBSDOSes+AllWindowsOSes-[WinCE];
+    P.OSes:=AllUnixOSes+AllBSDOSes+AllWindowsOSes-[WinCE]+[wasi];
     if Defaults.CPU=jvm then
       P.OSes := P.OSes - [java,android];
 

+ 13 - 2
packages/pastojs/src/pas2jscompilerpp.pp

@@ -16,6 +16,10 @@
   Abstract:
     Pas2JS compiler Postprocessor support. Can depend on filesystem.
 }
+{$IFNDEF CPUWASM}
+{$DEFINE HAS_PROCESS}
+{$ENDIF}
+
 {$IFNDEF FPC_DOTTEDUNITS}
 unit Pas2JSCompilerPP;
 {$ENDIF FPC_DOTTEDUNITS}
@@ -53,9 +57,9 @@ Type
 implementation
 
 {$IFDEF FPC_DOTTEDUNITS}
-uses System.Process, Pas2Js.Logger, Pas2Js.Utils, Pas2Js.Files.Utils;
+uses {$IFDEF HAS_PROCESS}System.Process,{$ENDIF} Pas2Js.Logger, Pas2Js.Utils, Pas2Js.Files.Utils;
 {$ELSE FPC_DOTTEDUNITS}
-uses process, pas2jslogger, pas2jsutils, pas2jsfileutils;
+uses {$IFDEF HAS_PROCESS}process, {$ENDIF} pas2jslogger, pas2jsutils, pas2jsfileutils;
 {$ENDIF FPC_DOTTEDUNITS}
 
 function TPas2JSFSPostProcessorSupport.CmdListAsStr(CmdList: TStrings): string;
@@ -142,6 +146,7 @@ begin
 
 end;
 
+{$IFDEF HAS_PROCESS}
 function TPas2JSFSPostProcessorSupport.Execute(const JSFilename: String; Cmd: TStringList; JS: TJSWriterString): TJSWriterString;
 
 const
@@ -267,7 +272,13 @@ begin
   if Compiler.ShowDebug or Compiler.ShowUsedTools then
     Compiler.Log.LogMsgIgnoreFilter(nPostProcessorFinished,[]);
 end;
+{$ELSE}
+function TPas2JSFSPostProcessorSupport.Execute(const JSFilename: String; Cmd: TStringList; JS: TJSWriterString): TJSWriterString;
 
+begin
+  raise EFOpenError.Create('post processor "'+Cmd[0]+'" cannot be executed, no process support');
+end;
+{$ENDIF}
 
 end.
 

+ 2 - 0
packages/pastojs/src/pas2jsfilecache.pp

@@ -1787,9 +1787,11 @@ begin
       end;
     except
       on E: Exception do begin
+        {$IFNDEF CPUWASM}
         i:=GetLastOSError;
         if i<>0 then
           Log.LogPlain('Note: '+SysErrorMessage(i));
+        {$ENDIF}  
         if not DirectoryExists(ChompPathDelim(ExtractFilePath(Filename))) then
           Log.LogPlain('Note: file cache inconsistency: folder does not exist "'+ChompPathDelim(ExtractFilePath(Filename))+'"');
         if FileExists(Filename) and not FileIsWritable(Filename) then

+ 10 - 7
packages/pastojs/src/pas2jsfileutils.pp

@@ -838,17 +838,20 @@ end;
   {$I pas2jsfileutilsnodejs.inc}
 {$ENDIF}
 
-procedure InternalInit;
-begin
+{$IFDEF CPUWASM}
+  {$I pas2jsfileutilswasm.inc}
+{$ENDIF}
+
+initialization
 
+{$IFDEF HAVE_INITPLATFORM}
   InitPlatform;
-end;
+{$ENDIF} 
 
-initialization
-  InternalInit;
-{$IFDEF FPC}
 finalization
+{$IFDEF HAVE_FINALIZEPLATFORM}
   FinalizePlatform;
-{$ENDIF}
+{$ENDIF}  
+  
 end.
 

+ 0 - 3
packages/pastojs/src/pas2jsfileutilsnodejs.inc

@@ -194,8 +194,5 @@ begin
   Result:=GetDefaultTextEncoding;
 end;
 
-procedure InitPlatform;
-begin
-
 end;
 

+ 0 - 11
packages/pastojs/src/pas2jsfileutilsunix.inc

@@ -185,14 +185,3 @@ begin
   Result:=ConsoleToUTF8(GetEnvironmentVariable(EnvVar));
 end;
 
-
-procedure InitPlatform;
-begin
-
-end;
-
-procedure FinalizePlatform;
-begin
-
-end;
-

+ 153 - 0
packages/pastojs/src/pas2jsfileutilswasm.inc

@@ -0,0 +1,153 @@
+{%MainUnit pas2jsfileutils.pas}
+{
+    This file is part of the Free Component Library (FCL)
+    Copyright (c) 2018  Mattias Gaertner  [email protected]
+
+    Unix backend of pas2jsfileutils
+
+    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.
+
+ **********************************************************************
+}
+
+function FilenameIsAbsolute(const aFilename: string): boolean;
+begin
+  Result:=ExpandFileName(aFilename)=aFileName;
+end;
+
+function ExpandFileNamePJ(const FileName: string; BaseDir: string): string;
+var
+  IsAbs: Boolean;
+  HomeDir, Fn: String;
+begin
+  Fn := FileName;
+  ForcePathDelims(Fn);
+  IsAbs := FileNameIsUnixAbsolute(Fn);
+  if (not IsAbs) then
+  begin
+    if ((Length(Fn) > 1) and (Fn[1] = '~') and (Fn[2] = '/')) or (Fn = '~') then
+    begin
+      HomeDir := GetEnvironmentVariablePJ('HOME');
+      if not FileNameIsUnixAbsolute(HomeDir) then
+        HomeDir := ExpandFileNamePJ(HomeDir,'');
+      Fn := HomeDir + Copy(Fn,2,length(Fn));
+      IsAbs := True;
+    end;
+  end;
+  if IsAbs then
+  begin
+    Result := ResolveDots(Fn);
+  end
+  else
+  begin
+    if (BaseDir = '') then
+      Fn := IncludeTrailingPathDelimiter(GetCurrentDirPJ) + Fn
+    else
+      Fn := IncludeTrailingPathDelimiter(BaseDir) + Fn;
+    Fn := ResolveDots(Fn);
+    //if BaseDir is not absolute then this needs to be expanded as well
+    if not FileNameIsUnixAbsolute(Fn) then
+      Fn := ExpandFileNamePJ(Fn, '');
+    Result := Fn;
+  end;
+end;
+
+function GetCurrentDirPJ: String;
+begin
+  Result:=GetCurrentDir;
+end;
+
+function GetPhysicalFilename(const Filename: string; ExceptionOnError: boolean
+  ): string;
+var
+  OldPath: String;
+  NewPath: String;
+  p: PAnsiChar;
+begin
+  Result:=Filename;
+  p:=PAnsiChar(Result);
+  repeat
+    while p^='/' do
+      inc(p);
+    if p^=#0 then exit;
+    if p^<>'/' then
+    begin
+      repeat
+        inc(p);
+      until p^ in [#0,'/'];
+      OldPath:=LeftStr(Result,p-PAnsiChar(Result));
+      NewPath:=ResolveSymLinks(OldPath,ExceptionOnError);
+      if NewPath='' then exit('');
+      if OldPath<>NewPath then
+      begin
+        Result:=NewPath+copy(Result,length(OldPath)+1,length(Result));
+        p:=PAnsiChar(Result)+length(NewPath);
+      end;
+    end;
+  until false;
+end;
+
+function ResolveSymLinks(const Filename: string; ExceptionOnError: boolean
+  ): string;
+var
+  LinkFilename: rawbytestring;
+  AText: string;
+begin
+  Result:=Filename;
+  if not FileGetSymLinkTarget(FileName,LinkFileName) then
+    raise EFOpenError.Create(AText);
+  if not FilenameIsAbsolute(LinkFilename) then
+    Result:=ExtractFilePath(Result)+LinkFilename
+  else
+    Result:=LinkFilename;
+end;
+
+function IsUNCPath(const Path: String): Boolean;
+begin
+  Result := false;
+  if Path='' then ;
+end;
+
+function ExtractUNCVolume(const Path: String): String;
+begin
+  Result := '';
+  if Path='' then ;
+end;
+
+function FileIsWritable(const AFilename: string): boolean;
+
+var
+  FD : THandle;
+
+begin
+  FD := FileOpen(aFileName,fmOpenWrite);
+  Result:=FD>0;
+  if Result then
+    FileClose(FD);
+end;
+
+function FileIsExecutable(const AFilename: string): boolean;
+begin
+  Result:=False;
+end;
+
+function GetEnvironmentVariableCountPJ: Integer;
+begin
+  Result:=GetEnvironmentVariableCount;
+end;
+
+function GetEnvironmentStringPJ(Index: Integer): string;
+begin
+  Result:=ConsoleToUTF8(GetEnvironmentString(Index));
+end;
+
+function GetEnvironmentVariablePJ(const EnvVar: string): String;
+begin
+  Result:=ConsoleToUTF8(GetEnvironmentVariable(EnvVar));
+end;
+

+ 2 - 1
packages/pastojs/src/pas2jsfileutilswin.inc

@@ -536,7 +536,7 @@ begin
   end;
 end;
 
-
+{$DEFINE HAVE_INITPLATFORM}
 procedure InitPlatform;
 begin
   {$ifndef WinCE}
@@ -557,6 +557,7 @@ begin
   end;
 end;
 
+{$DEFINE HAVE_FINALIZEPLATFORM}
 procedure FinalizePlatform;
 {$IFDEF ArgsWAsUTF8}
 var

+ 38 - 0
packages/pastojs/src/pas2jsutils.pp

@@ -637,6 +637,44 @@ begin
 end;
 {$ENDIF UNIX}
 
+{$IF NOT DEFINED(UNIX) AND NOT DEFINED(WINDOWS)}
+function UTF8ToSystemCP(const s: Ansistring): Ansistring;
+begin
+  if NonUTF8System and not IsASCII(s) then
+  begin
+    Result:=UTF8ToAnsi(s);
+    // prevent UTF8 codepage appear in the strings - we don't need codepage
+    // conversion magic
+    SetCodePage(RawByteString(Result), StringCodePage(s), False);
+  end
+  else
+    Result:=s;
+end;
+
+function SystemCPToUTF8(const s: ansistring): ansistring;
+begin
+  if NonUTF8System and not IsASCII(s) then
+  begin
+    Result:=AnsiToUTF8(s);
+    // prevent UTF8 codepage appear in the strings - we don't need codepage
+    // conversion magic
+    SetCodePage(RawByteString(Result), StringCodePage(s), False);
+  end
+  else
+    Result:=s;
+end;
+
+function ConsoleToUTF8(const s: ansistring): ansistring;
+begin
+  Result:=SystemCPToUTF8(s);
+end;
+
+function UTF8ToConsole(const s: ansistring): ansistring;
+begin
+  Result:=UTF8ToSystemCP(s);
+end;
+{$ENDIF}
+
 initialization
   InternalInit;
 end.