Determine OS/CPU via Compiler Directives lazarus / 21-General ---------------------------------------------------------------------- geraldholdsworth · 2022-03-12 11:16 Hi all, I've been using: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$IFDEF Darwin}    platform = 'macOS';            //Apple Mac OS X    {$ENDIF}    {$IFDEF Windows}    platform = 'Windows';          //Microsoft Windows    {$ENDIF}    {$IFDEF Linux}    platform = 'Linux';            //Linux    {$ENDIF}    {$IFNDEF Darwin}{$IFNDEF Windows}{$IFNDEF Linux}    platform = 'OS?';              //Unknown OS    {$ENDIF}{$ENDIF}{$ENDIF}    {$IFNDEF CPUARM}     {$IFDEF CPU32}     arch = '32 bit';               //32 bit CPU     {$ENDIF}     {$IFDEF CPU64}     arch = '64 bit';               //64 bit CPU     {$ENDIF}    {$ENDIF}    {$IFDEF CPUARM}     {$IFDEF CPU32}     arch = '32 bit ARM';          //32 bit ARM CPU     {$ENDIF}     {$IFDEF CPU64}     arch = '64 bit ARM';          //64 bit ARM CPU     {$ENDIF}    {$ENDIF}    {$IFNDEF CPU32}{$IFNDEF CPU64}     arch = 'CPU?';                //Unknown CPU    {$ENDIF}{$ENDIF} to determine which system my code is running on (or, rather, being compiled for). However, this is not perfect as if it was compiled for the M1 on macOS I end up with unknown platform and CPU (as none of the above are defined). So, I've found the TargetOS and TargetCPU defines are set up and wondered if I could use those instead. However, this doesn't work: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$IF TargetOS=darwin} platform:='macOS';{$ENDIF}{$IF TargetOS=linux} platform:='Linux';{$ENDIF}etc. ---------------------------------------------------------------------- Thaddy · 2022-03-12 14:28 I would change the order in first CPU and only then the OS's. I also would combine that with {$IF DEFINED()..} instead of {$IFDEF}. Reason: the CPU can be for multiple OS's and you can use the options that {$IF} give you to reduce the complexity. Note that in recent trunk the apple hardware IS defined, afaik. ---------------------------------------------------------------------- PascalDragon · 2022-03-12 17:21 Quote from: geraldholdsworth on March 12, 2022, 12:16:02 pmHowever, this is not perfect as if it was compiled for the M1 on macOS I end up with unknown platform and CPU (as none of the above are defined). DARWIN is defined for aarch64-darwin. Quote from: geraldholdsworth on March 12, 2022, 12:16:02 pmSo, I've found the TargetOS and TargetCPU defines are set up and wondered if I could use those instead. These are only available inside Lazarus. Also you might want to take a look at the following to make your life easier: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$if defined(darwin)}  platform = 'macOS';{$elseif defined(windows)}  platform = 'Windows'{$elseif defined(linux)}  platform = 'Linux';{$else}  platform = 'OS?';{$endif} ---------------------------------------------------------------------- geraldholdsworth · 2022-03-12 17:26 I'm using the Lazarus, as supplied by FPCDeluxe, which doesn't define these directives for aarch64-darwin (D'OH!). Looks like I'll need to set them in the project options for that build (I'm on an Intel Mac, but I cross compile for that platform). ---------------------------------------------------------------------- PascalDragon · 2022-03-12 17:30 Quote from: geraldholdsworth on March 12, 2022, 06:26:46 pmI'm using the Lazarus, as supplied by FPCDeluxe, which doesn't define these directives for aarch64-darwin (D'OH!). It is defined, because that's how the compiler works. It does not matter if you use Lazarus, FPCUpDeluxe or whatever. The compiler always adds the shortname of the operating system as a define (and CPUxxx for the target architecture xxx. If it doesn't then you're doing something wrong. ---------------------------------------------------------------------- geraldholdsworth · 2022-03-12 18:04 Looks like I've done something wrong then. They're not defined on my FPC Deluxe installed Lazarus, but they are on my bog-standard Lazarus install (but I can't cross-compile with that one, but I can select the Build Mode then look at the directives). My debugger doesn't work on my FPC Deluxe installed Lazarus either...so there is something wrong. ---------------------------------------------------------------------- marcov · 2022-03-12 18:25 You might want to play with FPC provided strings first, see https://www.freepascal.org/docs-html/prog/progsu41.html ---------------------------------------------------------------------- AlexTP · 2022-03-12 18:48 From CudaText: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses InterfaceBase;   labelPlatform.Caption:= Format('%s-%s-%s, fpc %s', [    LowerCase({$I %FPCTARGETOS%}),    {$I %FPCTARGETCPU%},    GetLCLWidgetTypeName,    {$I %FPCVersion%}    ]); ---------------------------------------------------------------------- geraldholdsworth · 2022-03-13 11:08 I've done something similar: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i const    TargetOS = {$I %FPCTARGETOS%};    TargetCPU = {$I %FPCTARGETCPU%};and then, in CreateForm: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  //Platform details - OS if TargetOS='Darwin' then platform:= 'macOS';   //Apple Mac OS X if(TargetOS='Win64') or(TargetOS='Win32') then platform:= 'Windows'; //Microsoft Windows if TargetOS='Linux'  then platform:= 'Linux';   //Linux //Platform details - CPU if TargetCPU='aarch64' then arch:= 'ARM 64 bit'; if TargetCPU='arm'     then arch:= 'ARM 32 bit'; if TargetCPU='i386'    then arch:= 'Intel 32 bit'; if TargetCPU='x86_64'  then arch:= 'Intel 64 bit';But I do feel it can be done better...for example, using LowerCase as you have. Thank you.