Browse Source

* TOSVersion for Delphi compatibility

(cherry picked from commit 21e99a0a1e77e38c48f110e230276b29998cdc9f)
Michaël Van Canneyt 1 year ago
parent
commit
a6d24ca96b
2 changed files with 330 additions and 0 deletions
  1. 66 0
      rtl/objpas/sysutils/sysutilh.inc
  2. 264 0
      rtl/objpas/sysutils/sysutils.inc

+ 66 - 0
rtl/objpas/sysutils/sysutilh.inc

@@ -371,3 +371,69 @@ Type
 
 Function ArrayOfConstToStr(Args: array of const ; aSeparator : Char = ','; aQuoteBegin : Char = '"'; aQuoteEnd : Char = '"') : UTF8String;
 Function ArrayOfConstToStrArray(Args: array of const) : TUTF8StringDynArray;
+
+{ Delphi compatibility }
+
+Type
+
+  { TOSVersion }
+
+  TOSVersion = record
+  public type
+    TArchitecture = (arIntelX86, arIntelX64, arARM32, arARM64, arIntelX16, arXTensa, arAVR, arM68k, arPowerPC, arPower64,
+                     arMips, arMipsel, arMips64, arMips64el, arJVM, arWasm32, arSparc64, arRiscV32, arRiscV64, arZ80,
+                     arLoongArch64, arOther);
+
+    TPlatform = (pfWindows, pfMacOS, pfiOS, pfAndroid, pfWinRT, pfLinux,
+                 pfGo32v2, pfOS2, pfFreeBSD, pfBeos, pfNetBSD, pfAmiga,
+                 pfAtari, pfSolaris, pfQNX, pfNetware, pfOpenBSD, pfWDosX,
+                 pfPalmos, pfMacOSClassic, pfDarwin, pfEMX, pfWatcom,
+                 pfMorphos, pfNetwLibC, pfWinCE, pfGBA, pfNDS, pfEmbedded,
+                 pfSymbian, pfHaiku, pfIPhoneSim, pfAIX, pfJava,
+                 pfNativeNT, pfMSDos, pfWII, pfAROS, pfDragonFly, pfWin16,
+                 pfFreeRTOS, pfZXSpectrum, pfMSXDOS, pfAmstradCPC,
+                 pfSinclairQL, pfWasi, pfOther);
+  public const
+    AllArchitectures = [Low(TArchitecture) .. High(TArchitecture)];
+    AllPlatforms = [Low(TPlatform) .. High(TPlatform)];
+  private
+    class var FFull : string;
+    class var FArchitecture: TArchitecture;
+    class var FBuild: Integer;
+    class var FMajor: Integer;
+    class var FMinor: Integer;
+    class var FName: string;
+    class var FPlatform: TPlatform;
+    class var FServicePackMajor: Integer;
+    class var FServicePackMinor: Integer;
+  {$IFDEF LINUX}
+    class var FPrettyName: string;
+    class var FLibCVersionMajor: Integer;
+    class var FLibCVersionMinor: Integer;
+  {$ENDIF LINUX}
+    class constructor Create;
+  public
+    class function Check(AMajor: Integer): Boolean; overload; static; inline;
+    class function Check(AMajor, AMinor: Integer): Boolean; overload; static; inline;
+    class function Check(AMajor, AMinor, AServicePackMajor: Integer): Boolean; overload; static; inline;
+    class function ToString: string; static;
+    class property Architecture: TArchitecture read FArchitecture;
+    class property Build: Integer read FBuild;
+    class property Major: Integer read FMajor;
+    class property Minor: Integer read FMinor;
+    class property Name: string read FName;
+    class property Platform: TPlatform read FPlatform;
+    class property ServicePackMajor: Integer read FServicePackMajor;
+    class property ServicePackMinor: Integer read FServicePackMinor;
+  {$IFDEF LINUX}
+    class property PrettyName: string read FPrettyName;
+    class property LibCVersionMajor: Integer read FLibCVersionMajor;
+    class property LibCVersionMinor: Integer read FLibCVersionMinor;
+  {$ENDIF LINUX}
+  end;
+
+  TArchitectures = set of TOSVersion.TArchitecture;
+  TPlatforms = set of TOSVersion.TPlatform;
+
+  Function GetCompiledArchitecture : TOSVersion.TArchitecture;
+  Function GetCompiledPlatform : TOSVersion.TPlatform;

+ 264 - 0
rtl/objpas/sysutils/sysutils.inc

@@ -925,3 +925,267 @@ begin
   For S in ArrayOfConstToStrArray(Args) do
     Add(S);
 end;
+
+Function GetCompiledArchitecture : TOSVersion.TArchitecture;
+
+begin
+  Result:=arOther;
+  {$ifdef i386}
+  Result:=arIntelX86;
+  {$endif i386}
+
+  {$ifdef m68k}
+  Result:=arM68k;
+  {$endif m68k}
+
+  {$ifdef powerpc}
+  Result:=arPowerPC
+  {$endif powerpc}
+
+  {$ifdef powerpc64}
+  Result:=arPower64
+  {$endif powerpc64}
+
+  {$ifdef arm}
+  Result:=arARM32;
+  {$endif arm}
+
+  {$ifdef aarch64}
+  Result:=arARM64
+  {$endif aarch64}
+
+  {$ifdef sparc}
+  Result:=arSparc;
+  {$endif sparc}
+
+  {$ifdef sparc64}
+  Result:=arSparc64;
+  {$endif sparc64}
+
+  {$ifdef CPUX86_64}
+  Result:=arIntelX64;
+  {$endif x86_64}
+
+  {$ifdef mipsel}
+  Result:=arMipsel
+  {$else : not mipsel}
+    {$ifdef mips}
+      Result:=arMips;
+    {$endif mips}
+  {$endif not mipsel}
+
+  {$ifdef riscv32}
+  Result:=arRiscV32;
+  {$endif riscv32}
+
+  {$ifdef riscv64}
+  Result:=arRiscV64;
+  {$endif riscv64}
+
+  {$ifdef xtensa}
+  Result:=arExtensa;
+  {$endif xtensa}
+
+  {$ifdef wasm32}
+  Result:=arWasm32;
+  {$endif wasm32}
+
+  {$ifdef loongarch64}
+  Result:=arLoongArch64;
+  {$endif loongarch64}
+end;
+
+Function GetCompiledPlatform : TOSVersion.TPlatform;
+
+begin
+  Result:=pfOther;
+  {$IFDEF WINDOWS}
+  Result:=pfWindows;
+  {$ENDIF}
+  {$Ifdef darwin}
+  Result:=pfMacOS;
+  {$ENDIF}
+  {$IFDEF IOS}
+  Result:=pfiOS;
+  {$ENDIF}
+  {$IFDEF ANDROID}
+  Result:=pfAndroid;
+  {$ENDIF}
+  {$IFDEF LINUX}
+  Result:=pfLinux;
+  {$ENDIF}
+  {$IFDEF GO32V2}
+  Result:=pfGo32v2;
+  {$ENDIF}
+  {$IFDEF OS2}
+  Result:=pfOS2;
+  {$ENDIF}
+  {$IFDEF FREEBSD}
+  Result:=pfFreeBSD;
+  {$ENDIF}
+  {$IFDEF BEOS}
+  Result:=pfBeos;
+  {$ENDIF}
+  {$IFDEF NETBSD}
+  Result:=pfNetBSD;
+  {$ENDIF}
+  {$IFDEF AMIGA}
+  Result:=pfAmiga;
+  {$ENDIF}
+  {$IFDEF ATARI}
+  Result:=pfAtari;
+  {$ENDIF}
+  {$IFDEF SUNOS}
+  Result:=pfSolaris;
+  {$ENDIF}
+  {$IFDEF QNX}
+  Result:=pfQNX;
+  {$ENDIF}
+  {$IFDEF Netware}
+  Result:=pfNetware;
+  {$ENDIF}
+  {$IFDEF OpenBSD}
+  Result:=pfOpenBSD;
+  {$ENDIF}
+  {$IFDEF WDosX}
+  Result:=pfWDosX;
+  {$ENDIF}
+  {$IFDEF PALMOS}
+  Result:=pfPalmos;
+  {$ENDIF}
+  {$IFDEF MacOSClassic}
+  Result:=pfMacOSClassic;
+  {$ENDIF}
+  {$IFDEF DARWIN}
+  Result:=pfDarwin;
+  {$ENDIF}
+  {$IFDEF EMX}
+  Result:=pfEMX;
+  {$ENDIF}
+  {$IFDEF WATCOM}
+  Result:=pfWatcom;
+  {$ENDIF}
+  {$IFDEF MORPHOS}
+  Result:=pfMorphos;
+  {$ENDIF}
+  {$IFDEF NETWLIBC}
+  Result:=pfNetwLibC;
+  {$ENDIF}
+  {$IFDEF WINCE}
+  Result:=pfWinCE;
+  {$ENDIF}
+  {$IFDEF GBA}
+  Result:=pfGBA;
+  {$ENDIF}
+  {$IFDEF NDS}
+  Result:=pfNDS;
+  {$ENDIF}
+  {$IFDEF EMBEDDED}
+  Result:=pfEmbedded;
+  {$ENDIF}
+  {$IFDEF SYMBIAN}
+  Result:=pfSymbian;
+  {$ENDIF}
+  {$IFDEF HAIKU}
+  Result:=pfHaiku;
+  {$ENDIF}
+  {$IFDEF IPHONESIM}
+  Result:=pfIPhoneSim;
+  {$ENDIF}
+  {$IFDEF AIX}
+  Result:=pfAIX;
+  {$ENDIF}
+  {$IFDEF JAVA}
+  Result:=pfJava;
+  {$ENDIF}
+  {$IFDEF NATIVENT}
+  Result:=pfNativeNT;
+  {$ENDIF}
+  {$IFDEF MSDOS}
+  Result:=pfMSDos;
+  {$ENDIF}
+  {$IFDEF WII}
+  Result:=pfWII;
+  {$ENDIF}
+  {$IFDEF AROS}
+  Result:=pfAROS;
+  {$ENDIF}
+  {$IFDEF DRAGONFLY}
+  Result:=pfDragonFly;
+  {$ENDIF}
+  {$IFDEF WIN16}
+  Result:=pfWin16;
+  {$ENDIF}
+  {$IFDEF FREERTOS}
+  Result:=pfFreeRTOS;
+  {$ENDIF}
+  {$IFDEF ZXSPECTRUM}
+  Result:=pfZXSpectrum;
+  {$ENDIF}
+  {$IFDEF MSXDOS}
+  Result:=pfMSXDOS;
+  {$ENDIF}
+  {$IFDEF AMSTRADCPC}
+  Result:=pfAmstradCPC;
+  {$ENDIF}
+  {$IFDEF SINCLAIRQL}
+  Result:=pfSinclairQL;
+  {$ENDIF}
+  {$IFDEF WASI}
+  Result:=pfWasi;
+  {$ENDIF}
+end;
+
+class constructor TOSVersion.Create;
+
+{$IFNDEF HAS_OSVERSION}
+var
+  S : String;
+{$ENDIF}
+begin
+  FArchitecture:=GetCompiledArchitecture;
+  FPlatform:=GetCompiledPlatform;
+  FBuild:=0;
+  FMajor:=0;
+  FMinor:=0;
+  FServicePackMajor:=0;
+  FServicePackMinor:=0;
+{$IFDEF HAS_OSVERSION}
+  InitOSVersion;
+{$ELSE}
+  WriteStr(S,GetCompiledPlatform);
+  FName:=Copy(S,3);
+  WriteStr(S,GetCompiledArchitecture);
+  FFull:=Copy(S,3)+'-'+FName;
+{$ENDIF}
+end;
+
+class function TOSVersion.Check(aMajor: Integer): Boolean; overload; static; inline;
+
+begin
+  Result:=(Major>=aMajor);
+end;
+
+
+class function TOSVersion.Check(aMajor, aMinor: Integer): Boolean; overload; static; inline;
+
+begin
+ Result:=(Major>aMajor)
+         or ((Major=aMajor) and (Minor>=aMinor))
+end;
+
+
+class function TOSVersion.Check(aMajor, aMinor, aServicePackMajor: Integer): Boolean; overload; static; inline;
+
+begin
+  Result:=(Major>AMajor)
+          or ((Major=aMajor) and (Minor>aMinor))
+          or ((Major=aMajor) and (Minor=aMinor) and (ServicePackMajor>=aServicePackMajor));
+end;
+
+
+class function TOSVersion.ToString: string; static;
+
+begin
+  Result:=FFull;
+end;