Recent

Author Topic: default compiler defines for CPU and OS  (Read 1259 times)

Чебурашка

  • Hero Member
  • *****
  • Posts: 586
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
default compiler defines for CPU and OS
« on: May 08, 2023, 10:35:57 am »
Hello,
is there any built-in define to tell what is the CPU and the OS at compile time or some way to make it available?

I know that there are some variables available in Lazarus: $(TargetCPU) and $(TargetOS), and I tried to use them to create a suitable define in Lazarus (it was fine for me because also when I do cmd line compile I use lazbuild). I created a custom option in the dedicated project configuration page:

"-d$(TARGET_CPU)_$(TARGET_OS)"

But the define such created is "-d_" instead of "-dx84_64_linux".

Thank you

AFTER POST ADDITION: I found a way to do this: in the Custom Options page / Conditionals, I added a line

Code: Pascal  [Select][+][-]
  1. CustomOptions += '-d' + TargetCPU + '_' + TargetOS    
  2.  

But I am still wondering if there is something built-in.


« Last Edit: May 08, 2023, 10:41:27 am by Чебурашка »
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11985
  • FPC developer.

Чебурашка

  • Hero Member
  • *****
  • Posts: 586
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: default compiler defines for CPU and OS
« Reply #2 on: May 08, 2023, 12:55:01 pm »
https://www.freepascal.org/docs-html/prog/progsu41.html

Thanks.

I was looking for something to be later used in a {$IFDEF} so probably the strings included do not fit my purpose (prob I need somethigs that is rather a c style macro).

Quote
  , libW
 
  {$IFDEF i386_linux}
  , libW_i386_linux
  {$ELSE}
    {$IFDEF x86_64_linux}
    , libW_x86_64_linux
    {$ELSE}
      {$DEFINE direct=true}
    , libW.Execution
    {$ENDIF} 
  {$ENDIF}

I don't know how to obtain the defines above using %FPCTARGETOS% %FPCTARGETCPU% as they can only be substitured as strings in code.
« Last Edit: May 08, 2023, 02:16:49 pm by Чебурашка »
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

TRon

  • Hero Member
  • *****
  • Posts: 3778
Re: default compiler defines for CPU and OS
« Reply #3 on: May 08, 2023, 02:31:46 pm »
compiler defines during compilation ?: https://www.freepascal.org/docs-html/prog/progap7.html

I don't know how to obtain the defines above using %FPCTARGETOS% %FPCTARGETCPU% as they can only be substitured as strings in code.
The obtaining part was already explained by marcov so what do you actually mean ?

Because you talk of c-style macro's I assume you are referring to the value assigned to a define so that you can check against the contents of such a define ? In that case: you can't. Pascal does not support c-style macro's.
« Last Edit: May 08, 2023, 02:47:55 pm by TRon »
I do not have to remember anything anymore thanks to total-recall.

Чебурашка

  • Hero Member
  • *****
  • Posts: 586
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: default compiler defines for CPU and OS
« Reply #4 on: May 08, 2023, 03:18:39 pm »
compiler defines during compilation ?: https://www.freepascal.org/docs-html/prog/progap7.html

Thanks.


I don't know how to obtain the defines above using %FPCTARGETOS% %FPCTARGETCPU% as they can only be substitured as strings in code.
The obtaining part was already explained by marcov so what do you actually mean ?

I need to include a unit in a cpu/os dependent way either i386_linux or x86_84_linux.

Currently I created dedicated defines acivated in the lpi in a exclusive way

-di386_linux
-dx86_64_linux

and then in code I do

Code: Pascal  [Select][+][-]
  1. uses
  2. {$IFDEF i386_linux}
  3.   libW_i386_linux;
  4. {$ENDIF}
  5. {$IFDEF x86_64_linux}
  6.   libW_x86_64_linux;
  7. {$ENDIF}
  8.  

And is ok.

OTOH

Given the suggestion from marcov of writing thinks like

{$I %FPCTARGETCPU%}
{$I %FPCTARGETOS%}

that are resolved into strings in the code, I don't know how to use to obtain something that does the same, as the includes are resolved into strings = 'x86_64' not x86_64 (unless I am wrong)

Suggestions are welcome because I could avoid the need of making lpi level defines.

The best would be to do something like
Code: Pascal  [Select][+][-]
  1. uses
  2.   libW_{$I %FPCTARGETCPU%}_{$I %FPCTARGETOS%};
  3.  

But this is tooooooo c-macro style to work.


Because you talk of c-style macro's I assume you are referring to the value assigned to a define so that you can check against the contents of such a define ? In that case: you can't. Pascal does not support c-style macro's.

I know, thanks.

FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

TRon

  • Hero Member
  • *****
  • Posts: 3778
Re: default compiler defines for CPU and OS
« Reply #5 on: May 08, 2023, 04:20:55 pm »
Thank for clarifying Чебурашка.

Suggestions are welcome because I could avoid the need of making lpi level defines.
I am not aware of a solution that actually works (other then the typical include files using ifdefs defining the required name based on cpu and platform.

Quote
The best would be to do something like
Code: Pascal  [Select][+][-]
  1. uses
  2.   libW_{$I %FPCTARGETCPU%}_{$I %FPCTARGETOS%};
  3.  

But this is tooooooo c-macro style to work.
Yes, but afaik not possible to realize.

Using pascal's equivalent of macro's would be the best bet but macro's are only active during interface/implementation sections and when trying to circumvent that by defining them globally from the command line then the different parts "libw" vs "name of macro" can't be concatenated.


edit:
Can't you approach it form another angle using directory/file names instead ($fpctarget $fpcsubarch $fpcmemorymodel) in your fpc.cfg (or from the commandline) ?
« Last Edit: May 08, 2023, 04:35:01 pm by TRon »
I do not have to remember anything anymore thanks to total-recall.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5802
  • Compiler Developer
Re: default compiler defines for CPU and OS
« Reply #6 on: May 08, 2023, 09:59:56 pm »
Currently I created dedicated defines acivated in the lpi in a exclusive way

-di386_linux
-dx86_64_linux

and then in code I do

Code: Pascal  [Select][+][-]
  1. uses
  2. {$IFDEF i386_linux}
  3.   libW_i386_linux;
  4. {$ENDIF}
  5. {$IFDEF x86_64_linux}
  6.   libW_x86_64_linux;
  7. {$ENDIF}
  8.  

The compiler defines a define for the operating system (e.g. LINUX on Linux, DARWIN for macOS, WIN32 for 32-bit Windows, UNIX for any Unix-like system, WINDOWS for any Windows) as well as the target platform (e.g. CPU386 for i386, CPUX86_64 for x86-64, CPUAARCH64 for Aarch64). Check Appendix G of the Programmer's Guide.

Thus you need to check as follows:

Code: Pascal  [Select][+][-]
  1. uses
  2.   {$if defined(linux) and defined(cpu386)}
  3.     libW_i386_linux;
  4.   {$elseif defined(linux) and defined(cpux86_64)}
  5.     libW_x86_64_linux;
  6.   {$endif}
  7.  
  8. // or
  9.  
  10. uses
  11.   {$if defined(linux)}
  12.     {$if defined(cpu386)}
  13.       libW_i386_linux;
  14.     {$elseif defined(cpux86_64)}
  15.       libW_x86_64_linux;
  16.     {$endif}
  17.   {$endif}

Чебурашка

  • Hero Member
  • *****
  • Posts: 586
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: default compiler defines for CPU and OS
« Reply #7 on: May 10, 2023, 01:07:15 pm »
Thus you need to check as follows:

Code: Pascal  [Select][+][-]
  1. uses
  2.   {$if defined(linux) and defined(cpu386)}
  3.     libW_i386_linux;
  4.   {$elseif defined(linux) and defined(cpux86_64)}
  5.     libW_x86_64_linux;
  6.   {$endif}
  7.  
  8. // or
  9.  
  10. uses
  11.   {$if defined(linux)}
  12.     {$if defined(cpu386)}
  13.       libW_i386_linux;
  14.     {$elseif defined(cpux86_64)}
  15.       libW_x86_64_linux;
  16.     {$endif}
  17.   {$endif}

Thanks. This a good solution because will work both for lazarus and direct fpc.
« Last Edit: May 10, 2023, 01:12:56 pm by Чебурашка »
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

Чебурашка

  • Hero Member
  • *****
  • Posts: 586
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: default compiler defines for CPU and OS
« Reply #8 on: May 10, 2023, 01:12:08 pm »
Can't you approach it form another angle using directory/file names instead ($fpctarget $fpcsubarch $fpcmemorymodel) in your fpc.cfg (or from the commandline) ?

I like also this idea: have all units with same name (just libW_architecture instread of one unit per combination) but put them into separate directories, with names that make distiction on cpu+os. Then at lpi level it is easy to select the units source path depending on the lazarus project variables TargetCPU/TargetOS. Ok it works thanks to lazarus, but for my goal this is perfectly acceptable.
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

 

TinyPortal © 2005-2018