Recent

Author Topic: [Solved/nvm] Compiler: get string passed from project  (Read 1835 times)

Fibonacci

  • Hero Member
  • *****
  • Posts: 996
  • Behold, I bring salvation - FPC Unleashed
[Solved/nvm] Compiler: get string passed from project
« on: May 01, 2025, 01:28:36 pm »
Im changing things in the compiler source.

For simple on/off things I use modeswitches, but if I want to pass a string to the compiler, how do I do that? It can be {$something in_the=source}, or as a custom option passed via command line.

Any ideas please.
« Last Edit: May 01, 2025, 05:13:39 pm by Fibonacci »
FPC Unleashed - inline vars, tuples, statement expressions, array equality, compound assignments, indexed/lazy labels, no-RTTI & more. ⭐ Star it on GitHub!

MarkMLl

  • Hero Member
  • *****
  • Posts: 8572
Re: Compiler: get string passed from project
« Reply #1 on: May 01, 2025, 02:13:53 pm »
If I'm interpreting your question correctly: you can define a macro in the same unit or in a .inc file, you can't define it on the commandline.

You could possibly pull something in as a resource file (creation left as an exercise...) but apart from that the only way I'm aware of is to have something external (e.g. a makefile) create a file and then do something like

Code: Pascal  [Select][+][-]
  1. CONST
  2. (*$IFDEF USE_SVN *)
  3.   (*$IFDEF UNIX   *)
  4.           rev= (*$I project_svnrevision.inc *) ;
  5.   (*$ELSE         *)
  6.           rev= 'unimplemented';
  7.   (*$ENDIF        *)
  8. (*$ELSE         *)
  9.   (*$WARNING Subversion revision reporting requires Compiler Options -> Other -> Custom Options -dHAS_SVN *)
  10.           rev= '';
  11. (*$ENDIF        *)
  12.  

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

440bx

  • Hero Member
  • *****
  • Posts: 6528
Re: Compiler: get string passed from project
« Reply #2 on: May 01, 2025, 02:27:05 pm »
I want to pass a string to the compiler, how do I do that? It can be {$something in_the=source}, or as a custom option passed via command line.
What do you want the compiler to do with the string ?

Knowing that might suggest a solution. 
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Fibonacci

  • Hero Member
  • *****
  • Posts: 996
  • Behold, I bring salvation - FPC Unleashed
Re: Compiler: get string passed from project
« Reply #3 on: May 01, 2025, 02:41:26 pm »
If I'm interpreting your question correctly

No.

For on/off things I can add custom modeswitches to compiler/globtype.pas, then still in the compiler source I can check if it is set:

Code: Pascal  [Select][+][-]
  1. if my_option in current_settings.modeswitches then

In the source file I want to compile (a regular FPC project) I simply use {$modeswitch my_option} to set the option, and that option will be seen by the compiler, so it can execute my custom code while compiling.

My question is, how can I instead of passing a simple "is set or not", pass a string that can be used inside the compiler source.

What do you want the compiler to do with the string ?

Knowing that might suggest a solution.

For example, I could use this string as a PE section name. Or set a specific OS major/minor version in the PE header. Or append this string to the FPC version string that is in every FPC compiled program.
FPC Unleashed - inline vars, tuples, statement expressions, array equality, compound assignments, indexed/lazy labels, no-RTTI & more. ⭐ Star it on GitHub!

Fibonacci

  • Hero Member
  • *****
  • Posts: 996
  • Behold, I bring salvation - FPC Unleashed
Re: Compiler: get string passed from project
« Reply #4 on: May 01, 2025, 03:00:54 pm »
I think I found it. AddDirective() in the compiler/scandir.pas should work. It will require storing the variable somewhere (tsettings @ globals.pas? or a global variable). A little coding required. Maybe there is an easier way ;)
« Last Edit: May 01, 2025, 03:08:35 pm by Fibonacci »
FPC Unleashed - inline vars, tuples, statement expressions, array equality, compound assignments, indexed/lazy labels, no-RTTI & more. ⭐ Star it on GitHub!

440bx

  • Hero Member
  • *****
  • Posts: 6528
Re: Compiler: get string passed from project
« Reply #5 on: May 01, 2025, 03:09:26 pm »
I think I found it. AddDirective() in the compiler/scandir.pas should work. It will require storing the variable somewhere (tsettings @ globals.pas? or a global variable). A little coding required. Maybe there is an easier way ;)
Is that usable by a user program ?
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Fibonacci

  • Hero Member
  • *****
  • Posts: 996
  • Behold, I bring salvation - FPC Unleashed
Re: Compiler: get string passed from project
« Reply #6 on: May 01, 2025, 03:10:52 pm »
No, its just for the programmer to tell the compiler something.
FPC Unleashed - inline vars, tuples, statement expressions, array equality, compound assignments, indexed/lazy labels, no-RTTI & more. ⭐ Star it on GitHub!

440bx

  • Hero Member
  • *****
  • Posts: 6528
Re: Compiler: get string passed from project
« Reply #7 on: May 01, 2025, 03:13:09 pm »
No, its just for the programmer to tell the compiler something.

does that mean you'd need to change the compiler source to include the strings you want ?
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Fibonacci

  • Hero Member
  • *****
  • Posts: 996
  • Behold, I bring salvation - FPC Unleashed
Re: Compiler: get string passed from project
« Reply #8 on: May 01, 2025, 03:17:24 pm »
Change the compiler source to add the ability to pass any string (not predefined in the compiler source). Then strings can only be set inside the source code of the user program. Available only during compilation, for the compiler, to change its behavior per project/compiled file.
« Last Edit: May 01, 2025, 03:24:15 pm by Fibonacci »
FPC Unleashed - inline vars, tuples, statement expressions, array equality, compound assignments, indexed/lazy labels, no-RTTI & more. ⭐ Star it on GitHub!

440bx

  • Hero Member
  • *****
  • Posts: 6528
Re: Compiler: get string passed from project
« Reply #9 on: May 01, 2025, 03:26:07 pm »
Here is another way that comes to mind but, it isn't very simple (still, seems simpler than modifying the compiler.)

First, I'll presume that the number of strings you may want to pass to the program is limited.  IOW, there is a little "prep" work needed for the solution I'm going to suggest that limits the number of strings that can be used by the program.

with the above out of the way, let's say you want to pass the string "THIS_IS_MY_STRING" to your program.  Here is how you can do it. 

1. declare a function named "THIS_IS_MY_STRING" that returns it's name using the {$I %CURRENTROUTINE%}, that way, you can give the string "THIS_IS_MY_STRING" to the compiler.   

2. define on the compiler command line -d"THIS_IS_MY_STRING" which because it is defined, causes some part of your code to call the function named "THIS_IS_MY_STRING" which returns its name, therefore the string you wanted.

The downside is that it requires defining a function that returns its name for every string you intend to use but, since the program should be aware of what the string means (what action(s) it should be associated with), that limitation seems to be part for the course anyway.

At least this way doesn't require modifying the compiler.

HTH.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 19245
  • Glad to be alive.
Re: Compiler: get string passed from project
« Reply #10 on: May 01, 2025, 03:33:24 pm »
Or simply {$IF Defined(XXX)} or {$IF Declared(xxx)}
The compiler will listen to those two from the command line.
An example is how -dDEBUG works. DEBUG is not tied to particular settings, only to what is defined in fpc.cfg.
These options also need no compiler changes.
objects are fine constructs. You can even initialize them with constructors.

Fibonacci

  • Hero Member
  • *****
  • Posts: 996
  • Behold, I bring salvation - FPC Unleashed
Re: Compiler: get string passed from project
« Reply #11 on: May 01, 2025, 03:43:48 pm »
 >:D :-*

I dont think any of you understand what I want.

Lets try with an example. Take a look here:
https://gitlab.com/freepascal.org/fpc/source/-/blob/main/compiler/ngenutil.pas?ref_type=heads#L1579

There is:
Code: Pascal  [Select][+][-]
  1. s:='FPC '+full_version_string+
  2.         ' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname;

Change it to: (notice +fpcversionappendix added)
Code: Pascal  [Select][+][-]
  1. s:='FPC '+full_version_string+
  2.         ' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname+fpcversionappendix;

Build the new compiler.

Now, if I add somewhere in my program source code this line:
Code: Pascal  [Select][+][-]
  1. {$fpcversionappendix This string will be appended to the version string in the compiled program}

The result should be that after compilation, inside the binary file, my string should be next to the original FPC version.

This can be achieved in the way I posted earlier by modifying compiler/scandir.pas



This topic is pretty much solved, closed, but if anyone can suggest an easier or alternative way, please share.
« Last Edit: May 01, 2025, 03:58:18 pm by Fibonacci »
FPC Unleashed - inline vars, tuples, statement expressions, array equality, compound assignments, indexed/lazy labels, no-RTTI & more. ⭐ Star it on GitHub!

Fibonacci

  • Hero Member
  • *****
  • Posts: 996
  • Behold, I bring salvation - FPC Unleashed
Re: Compiler: get string passed from project
« Reply #12 on: May 01, 2025, 04:19:58 pm »
Now its solved. Thanks to everyone for motivating me to solve the problem myself :D
FPC Unleashed - inline vars, tuples, statement expressions, array equality, compound assignments, indexed/lazy labels, no-RTTI & more. ⭐ Star it on GitHub!

Thaddy

  • Hero Member
  • *****
  • Posts: 19245
  • Glad to be alive.
Re: Compiler: get string passed from project
« Reply #13 on: May 01, 2025, 04:24:37 pm »
Code: Pascal  [Select][+][-]
  1. // can use any concat, combine with if declared()
  2. program version;
  3. uses
  4.   sysutils;
  5. var
  6.   fpcidentifier:array[0..255] of ansichar; external name '__fpc_ident';
  7.   test:string;
  8.  
  9. begin
  10.   if ParamCount > 0 then test := ParamStr(1);
  11.   writeln('This program was compiled with: ',fpcidentifier,'-',test);
  12.   readln;
  13.  end.
« Last Edit: May 01, 2025, 04:28:51 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

Fibonacci

  • Hero Member
  • *****
  • Posts: 996
  • Behold, I bring salvation - FPC Unleashed
Re: Compiler: get string passed from project
« Reply #14 on: May 01, 2025, 04:27:31 pm »
Code: Pascal  [Select][+][-]
  1. // can use any concat, combine with if declared()
  2. program version;
  3. uses
  4.   sysutils;
  5. var
  6.   fpcidentifier:array[0..255] of ansichar; external name '__fpc_ident';
  7.  
  8. begin
  9.   writeln('This program was compiled with: ',fpcidentifier,'-',datetimetostr(Now));
  10.   readln;
  11.  end.

« Last Edit: May 01, 2025, 04:29:25 pm by Fibonacci »
FPC Unleashed - inline vars, tuples, statement expressions, array equality, compound assignments, indexed/lazy labels, no-RTTI & more. ⭐ Star it on GitHub!

 

TinyPortal © 2005-2018