Technical Article

Delphi Cross-Compiler Build Matrix: HotXLS Since XE5

On Delphi XE5 targeting Win32, TDateTime(Serial) compiles and TDateTime(Serial + 1) stops the build with E2089 Invalid typecast, even though Serial is declared Double in both lines. The current compiler accepts both without so much as a hint. The cause is x87 excess precision, and the portable fix is to delete the cast instead of hunting for a better one. That pair is the cleanest member of a small family of constructs that a recent Delphi compiler takes happily and an older one in the same supported range refuses

Why a green build on the newest IDE proves so little

A local build only says something about the compiler you actually invoked. HotXLS ships one Object Pascal codebase to every Delphi and C++Builder release from XE5 forward. It goes out as Delphi Win32 and Win64 builds and as C++Builder packages, so every one of those compilers is part of the contract. Day-to-day work naturally happens in the newest IDE. The full matrix is a slower script, and slow scripts drift toward packaging time. That leaves room for a construct to sit unnoticed between the compiler that accepts it and the one that will reject it

Counting the builds shows how lopsided this is. A Delphi version list keyed on BDS product versions from 12.0 (XE5) to 37.0 (Delphi 13) builds each version twice, once for Win32 and once for Win64. The C++Builder lists are shorter on Win64. XE5 has a C++Builder package project type, but it does not ship c0pkg64.o, the Win64 package startup object. Add the lists up and the newest compiler accounts for a small share of the total

How a Delphi and C++Builder build matrix multiplies into builds: Delphi versions 12.0 through 37.0 counted twice for Win32 and Win64, C++Builder Win32 builds, and Win64 builds that skip XE5 because it lacks c0pkg64.o, compared with the few newest-compiler builds a day-to-day run covers
A green build on the newest compiler covers only a few compiler and platform combinations, so a construct that only XE5 rejects surfaces only when the full matrix runs

The same gap opens between toolchains, too. Suppose a new unit is reachable through a uses clause but missing from the .cbproj file list. Under Delphi it compiles perfectly, because dcc quietly pulls unlisted units into the package and at most emits hint W1033. C++Builder produces an .obj only for units named in <DelphiCompile>, so the very same code dies in ilink with an unresolved external. One toolchain hides exactly what the other one catches

Hard casts that x87 precision turns illegal

Two of the five constructs are the same mistake in different clothes: a hard typecast applied to a floating-point expression instead of to a variable. On Win32 the older compilers evaluate real arithmetic on the x87 stack, so an addition that involves a Double is carried at 80-bit precision and its static type becomes the 10-byte Extended. Casting 10 bytes down to an 8-byte TDateTime is not a legal value typecast, and E2089 says so

The variable form survives because Serial is already 8 bytes wide and the cast preserves size. Add a constant and the expression widens underneath you. Widening the cast or wrapping it in a define would both be the wrong move here. Delete it. An implicit real-to-real assignment converts correctly on every compiler from XE5 to the current release, and it says what the code means

Why TDateTime of a Double expression fails with E2089 on Delphi XE5 Win32: the x87 stack carries Serial plus an offset as a 10-byte Extended, so the narrowing cast is illegal, while TDateTime of the 8-byte Serial variable itself is size-preserving; HotXLS removes the cast and lets the real-to-real assignment convert
On Win32 the x87 stack widens a Double addition to a 10-byte Extended, which makes the hard TDateTime cast illegal, while casting the untouched Serial variable stays size-preserving on every compiler in the matrix
const
  // Day difference between the 1904 and 1900 Excel date systems
  Date1904Offset = 1462;

function ExcelSerialToDate(Serial: Double; Dates1904: Boolean): TDateTime;
begin
  // TDateTime(Serial + Date1904Offset) and TDateTime(Serial + 1) raise
  // E2089 on XE5 Win32: each sum is an Extended, and the cast narrows it.
  // TDateTime(Serial) alone would compile, but no cast is needed at all
  if Dates1904 then
    Result := Serial + Date1904Offset
  else if Serial < 60 then
    Result := Serial + 1
  else
    Result := Serial;
end;

// The integer flavor of the same rejection, from a two-decimal check:
//   if (Scaled = IntVal) and (Double(IntVal) / 100 = AValue) then  // E2089
// Real division already yields a floating-point result, so drop the cast
if (Scaled = IntVal) and (IntVal / 100 = AValue) then
  Exit(IntVal);

The Serial < 60 branch is the 1900 leap-year fiction rather than an off-by-one. Serial 60 is Excel's non-existent 1900-02-29, so serials below it need one extra day before DecodeDate sees them. A portability edit has no business touching that logic, which is the other reason to remove the cast and leave the arithmetic exactly as it was. HotXLS converts chart date-axis serials this way, with plain assignments and no casts

Why does nil fail as a callback argument on XE5?

A bare nil passed where a procedural parameter is expected does not bind during overload resolution on the older compilers. The diagnostic points at the overload set instead of at the argument, so the real cause is easy to look past. Readers can hit this through TXLSWorkbook.ResolveIndexedColor, which is overloaded. One form takes an index, a color space and an out resolution, and the other adds a role and a TXLSTryResolveSystemColor method pointer for mapping system colors. Most callers have no system-color resolver to offer, so the tempting call passes nil

Give the null callback a type and both compilers agree. A typed local assigned nil works everywhere. A unit-level variable of the procedural type works with even less code. The language zero-initializes unit-level variables, so it is nil from the start. HotXLS keeps one such variable in its handle unit and passes it at every internal call site that has no resolver

uses
  lxHandle, lxStyleColor;

function FontColorOf(Book: TXLSWorkbook; Icv: Integer;
  out Argb: LongWord): Boolean;
var
  NoSystemColors: TXLSTryResolveSystemColor;  // method pointer type
  Resolution: TXLSIndexedColorResolution;
begin
  Argb := 0;
  // Book.ResolveIndexedColor(Icv, xicsBiffIcv, xicrFont, nil, Resolution)
  // compiles on current Delphi but fails overload resolution on XE5
  NoSystemColors := nil;
  Result := Book.ResolveIndexedColor(Icv, xicsBiffIcv, xicrFont,
    NoSystemColors, Resolution) and Resolution.HasResolvedColor;
  if Result then
    Argb := Resolution.ARGB;
end;

This one is a language-level difference in how the compilers rank candidates, and it needs no conditional compilation. The typed variable is correct on every version and costs one line. Save {$IF CompilerVersion} for cases where the platform itself differs between releases. Among these five constructs, that happens exactly once

Reaching a protected TPicture.LoadFromStream

TPicture.LoadFromStream is public on current VCL and protected on the older releases in the supported range, so a direct call compiles today and fails on XE5. HotXLS calls it to confirm that a worksheet background image really decodes before the HTML exporter embeds the bytes. A payload with a valid PNG signature and a corrupt body is left out instead of being written into the page. The classic Pascal answer works on both sides of the visibility change: declare a descendant in the same unit purely to widen access, then cast through it

uses
  System.SysUtils, System.Classes, Vcl.Graphics;

type
  // LoadFromStream is protected on older VCL releases; a descendant
  // declared in this unit may call it there and on current VCL alike
  TPictureAccess = class(TPicture);

function ImageBytesDecode(const Bytes: TBytes): Boolean;
var
  Picture: TPicture;
  Stream: TBytesStream;
begin
  Picture := TPicture.Create;
  Stream := TBytesStream.Create(Bytes);
  try
    try
      TPictureAccess(Picture).LoadFromStream(Stream);
      Result := (Picture.Graphic <> nil) and not Picture.Graphic.Empty and
        (Picture.Graphic.Width > 0) and (Picture.Graphic.Height > 0);
    except
      Result := False;
    end;
  finally
    Stream.Free;
    Picture.Free;
  end;
end;

The accessor class is safe because it adds no fields and is never instantiated. The cast only changes what the compiler lets you name. Leave a comment at the declaration anyway: a colleague who only ever builds in a current IDE will otherwise delete what looks like a pointless type, and the XE5 build breaks again. Decoded background images also feed the on-screen sheet in the custom VCL grid rendering path

GdiplusStartup is where a CompilerVersion guard earns its keep

The var token parameter of GdiplusStartup in Winapi.GDIPAPI changed type across VCL generations, and for part of the supported range only one spelling compiles. Through 10.3 Rio the declaration wants Cardinal. From 10.4 Sydney on it wants a handle-sized type, and THandle fits. Watch the numbering here: Sydney is BDS 21.0 in the version list above, but its CompilerVersion is 34, and XE5 is 26 on that second scale. The guard tests CompilerVersion, never the BDS number. A var parameter demands an exact type match, so no single unconditional declaration covers XE5 through the current release. This is the one construct of the five where {$IF CompilerVersion >= 34} is the honest answer, because the RTL really did change

Which declaration of GdiplusStartup each Delphi release (BDS version) accepts: Cardinal from 12.0 through 20.0 (10.3 Rio), THandle or ULONG_PTR from 21.0 (10.4 Sydney) on, and how the {$IF CompilerVersion >= 34} guard with the fully qualified Winapi.GDIPAPI.GdiplusStartup call keeps one spelling valid across the matrix
The two declarations of the GdiplusStartup token split at 10.4 Sydney, so no unconditional declaration works and the guard on CompilerVersion 34 is the only conditional compilation among the five
uses
  System.SysUtils, Winapi.GDIPAPI;

var
  // GDIPAPI's GdiplusStartup var parameter is Cardinal through Rio
  // and handle-sized from Sydney (CompilerVersion 34) on
  {$IF CompilerVersion >= 34}
  GdipToken: THandle;
  {$ELSE}
  GdipToken: Cardinal;
  {$IFEND}

procedure StartGdiPlus;
var
  Input: TGdiplusStartupInput;
begin
  FillChar(Input, SizeOf(Input), 0);
  Input.GdiplusVersion := 1;
  // Fully qualified, so a same-named declaration from another unit in
  // the uses clause cannot take precedence on some compiler version
  if Winapi.GDIPAPI.GdiplusStartup(GdipToken, @Input, nil) <> Ok then
    raise Exception.Create('GDI+ startup failed');
end;

procedure StopGdiPlus;
begin
  Winapi.GDIPAPI.GdiplusShutdown(GdipToken);
end;

HotXLS closes the guard with {$IFEND}, which every compiler in the range accepts. Notice what the guard does not claim: THandle and ULONG_PTR have the same width on Win32 and on Win64, so the choice is about which identifier the declaration names. It has nothing to do with 32-bit versus 64-bit correctness. The same ladder sits in the TIFF branch of the HotXLS page image exporter, the raster pipeline behind exporting a cell range as a single image

How do you find out which compiler accepts a construct?

Write a minimal program that contains only the construct in question, then run every installed dcc32 and dcc64 against it from a shell script. That is cheap, and for the GdiplusStartup token it beats reading release notes: the declaration you can Ctrl+click in each IDE's copy of Winapi.GDIPAPI is the ground truth. Two traps in such a script are worth knowing before you trust its output

The first trap makes the probe report nothing. A script that captures res=$(...) inside a subshell loses the assignment, because variables set in a subshell never reach the parent. dcc32 exits 0 on success, so the exit code is the right thing to capture. Store it in a variable that stops existing one line later, though, and every build comes back blank

The second trap is worse, since it produces a wrong answer instead of no answer. A probe that scores a build by counting lines that contain Error misses every fatal that Delphi does not label that way. F1026 File not found is fatal and does not match, so a probe that cannot even resolve a unit gets scored as a clean pass. XE5 does not ship Winapi.GDIPOPS.dcu, which means a probe that uses that unit goes green on exactly the compiler it was written to test. Judge each build by the produced .exe or .dcu, or by the compiler's own summary line. Keyword grepping fails in the one direction you cannot afford

What a decade of compilers costs

The code side is cheap. Four of the five constructs disappear when you write more ordinary Pascal: drop a cast, divide instead of casting, give nil a type, declare an accessor class. Only GdiplusStartup needs an {$IF}. A codebase spanning XE5 to the current release does not turn into a thicket of conditional defines unless hard casts and newest-compiler idioms are allowed to pile up

Build time and discipline are the real price. Keep a fast newest-compiler loop for iteration, and put the full matrix on a schedule nobody can skip; packaging time is too late, because by then a supported IDE can have stopped building weeks earlier. A native component makes this unavoidable. HotXLS reads and writes XLS, XLSX and ODS in Object Pascal alone, with no Excel install and no COM dependency. That is what makes Office-free workbook automation possible on a locked-down server, and it also means the compiler is the entire platform contract

Every construct above appears in its version-safe form in the source of the HotXLS Delphi Spreadsheet Component, which targets Delphi and C++Builder from XE5 up to the current release and comes with prebuilt library binaries for each supported IDE