Recent

Author Topic: Creating a new unit out of a current unit and what is compiling  (Read 980 times)

Okoba

  • Hero Member
  • *****
  • Posts: 555
Creating a new unit out of a current unit and what is compiling
« on: September 26, 2023, 03:13:28 pm »
I have a big unit, and I want to send the used functions code to my friend, so he can read the code but don't have problem with all other not used functions.
I know compiler knows what is used and does not link not used functions, is there a way or tool somewhere that I can use to generate a unit out of another one automatically?
Something like this sample:
Code: Pascal  [Select][+][-]
  1. unit SourceUnit;
  2.  
  3. {$DEFINE IncludeCode}
  4.  
  5. interface
  6.  
  7.  
  8. {$IFDEF IncludeCode} //Include this code in a new unit
  9. procedure Test1;
  10. {$ENDIF}
  11. {$IFNDEF IncludeCode}  //Do not include
  12. procedure Test2;
  13. {$ENDIF}
  14. implementation
  15.  
  16. {$IFDEF IncludeCode}
  17. procedure Test1;
  18. begin
  19.  
  20. end;
  21. {$ENDIF}
  22.  
  23. {$IFNDEF IncludeCode}
  24. procedure Test2;
  25. begin
  26.  
  27. end;
  28. {$ENDIF}
  29.  
  30. end.
  31.  

Code: Pascal  [Select][+][-]
  1. unit GeneratedUnit;
  2.  
  3. interface
  4.  
  5. procedure Test1;
  6.  
  7. implementation
  8.  
  9. procedure Test1;
  10. begin
  11.  
  12. end;
  13.  
  14. end.

cdbc

  • Hero Member
  • *****
  • Posts: 1645
    • http://www.cdbc.dk
Re: Creating a new unit out of a current unit and what is compiling
« Reply #1 on: September 27, 2023, 08:16:48 am »
Hi
If you're taking the time/trouble to mark your functions/procedures with {$ifdef}'s, why not quickly throw together a simple program that runs through your unit and copies the items in question to a new unit-file?!?
"Pos" will take you a long way  ;)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11935
  • FPC developer.
Re: Creating a new unit out of a current unit and what is compiling
« Reply #2 on: September 27, 2023, 08:34:42 am »
In theory you could have the linker generate a .MAP file, and parse that to make some tool. In practice that is still quite hard.

alpine

  • Hero Member
  • *****
  • Posts: 1292
Re: Creating a new unit out of a current unit and what is compiling
« Reply #3 on: September 27, 2023, 09:00:12 am »
In theory you could have the linker generate a .MAP file, and parse that to make some tool. In practice that is still quite hard.
In addition to that you may try to use the output from objdump utility, see https://forum.lazarus.freepascal.org/index.php/topic,55852.msg415376.html#msg415376 and the next reply from Martin_fr.
Not an easy task as already noted, intention depends on how big your code base is.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

 

TinyPortal © 2005-2018