23

I have this craving to do some experiments with modifying the underbelly of the Delphi run time library (RTL), system.pas and the likes... It is possible or not?

I'm very fond of challenges like "yes, but you'll have to provide custom .obj files for some assembler wizardry because they were never distributed with the official Delphi source". Fine with me, I just want to know.

I want to do this experiment with Delphi 7, but inside information on any other version is fine. It is one of the perks of being with a company that worked with Delphi since the Stone Age.

(I always figured this to be one of those RTFM questions, with the answer being a resounding "NO!", but for some reason google won't confirm it.)

rene
  • 41,474
  • 78
  • 114
  • 152
Paul-Jan
  • 16,746
  • 1
  • 63
  • 95

2 Answers2

25

You can recompile the RTL like any other unit.

For System.pas you must use the command line compiler.

For instance, here is a working batch file content (there is some not well documented command line switches):

del *.dcu /s
"c:\program files\borland\delphi7\bin\dcc32.exe" -O+ -Q -M -Y -Z -$D+ System.pas 

This will recompile System.pas and SysInit.pas (both lowest level RTL files).

But in order to use your recreated dcu files, you'll have to put the folder containing the updated dcu files into the first position of your IDE: for instance, in Delphi 7 it's Option / Environment Options / Library, then put your folder FIRST in both "Libary path" and "Browsing path" field.

And it's perhaps worth deleting the original .dcu files in your Delphi installation directory.

But be sure you won't change the "interface" part of the unit, or you'll have troubles with compiling with other not modified units of the RTL (or third-party components). You can change the "implementation" part, apply fixes or rewrite some part for speed or such, but don't change the "interface" part to avoid any linking error.

Always make a backup of the original .pas and .dcu files which you are changing. And it's a good idea to make some automated compilation test, so that you could be sure that your modifications of the RTL won't add any regression.

We made such a RTL recompilation for our Enhanced Run Time Library for better speed of low-level RTL functions (mostly System.pas and SysUtils.pas). Designed for Delphi 7 and 2007. For more recent Delphi version, you still can use the same principle.

Arnaud Bouchez
  • 42,305
  • 3
  • 71
  • 159
16

You can only recompile the RTL from the command-line. There should be a makefile in the RTL source directory of your installation. It is designed to be used with the make.exe command-line utility which should be in the "bin" folder of your installation. I would recommend you copy the relevant sources to a separate location for experimentation. I must caution you that the System unit is tightly coupled with the compiler which expects many functions to have a specific name and have particular parameter lists, if any are even declared. Many RTL "helper" functions don't have any formally declared parameters, yet expect parameters to be passed in a certain fashion.

Another bit of caution is changing the interface declarations of certain classes, functions or types. Doing so may cause serious incompatibilities with existing DCU files and components. For this reason you must be very careful when intermixing DCU files from the included RTL or third-party components with your custom modified versions. I would suggest you start by only making implementation section changes only before venturing into the mine-field of interface breaking changes.

Allen Bauer
  • 16,657
  • 2
  • 56
  • 74
  • I remember once that the compiler also relied on the order of declarations System.pas. It has been ages ago since I recompiled the RTL, so this might have been in the Turbo Pascal / Delphi 1 (16-bit) era. – Jeroen Wiert Pluimers Jan 30 '11 at 07:17
  • 2
    What do we call RTL? For System.pas and SysInit.pas, it could be done only from the command line. But for other higher-level units (like Classes.pas or SysUtils.pas) it can be done from the IDE, just by changing the Library files older in the IDE options, and providing another .pas source which will be used instead of the default version. – Arnaud Bouchez Jan 30 '11 at 08:59
  • As I remember Variants.pas can't be easily recompiled even with no changes. – Torbins Jan 30 '11 at 09:57
  • 1
    @Torbins AFAIR there is some circular dependency problems: to recompile Variants.pas, you must recompile the RTL files in the right order. This is easy to do using the command line compiler. And if you don't use variant, you can provide a void one, and save some code space (this is what KOL or our LVCL do). – Arnaud Bouchez Jan 30 '11 at 15:09
  • @Allen I just used the buildrtl.bat with release after I modified System.Rtti.pas but when I put the dcu into the lib folder it told me F2051 Unit System.Classes was compiled with a different version of System.Rtti.TRttiContext. I guess the buildrtl.bat release uses different compiler options than the shipped ones? – Stefan Glienke May 21 '14 at 21:41