Delphi XE3: Problems with complex pre-build events

We ended up using a workaround similar to what was proposed by David Heffernan:

  • combine all our calls into a single (Ruby) script PreBuild.rb
  • compile this Ruby script into a standalone executable (since not all developers have Ruby installed)
  • use a single pre-build event in Delphi

In case anyone's interested, here's our PreBuild event:

PreBuild "<path_to_SVN_working_copy>" "VersionInfo.rc.in" $(OUTPUTNAME).res

and here's the script PreBuild.rb:

  #!/usr/bin/env ruby

  require 'tempfile'

  if ARGV.length < 3
    puts "usage: #{$0} <path> <infile> <outfile>"
    exit 1
  end
  # svnversion.exe is part of the SVN command line client
  svnversion = "svnversion.exe"
  path, infile, outfile = ARGV[0], ARGV[1], ARGV[2]
  # call svnversion executable, storing its output in rev
  rev_str = `#{svnversion} "#{path}"`.chop

  # extract the first number (get rid of M flag for modified source)
  rev = /^[0-9]+/.match(rev_str)[0]

  # get current date
  date = Time.new

  # remove old output file (ignore errors, e.g. if file didn't exist)
  begin
    File.delete(outfile)
  rescue
  end

  input = File.new(infile, "r")
  tmpname = "VersionInfo.rc"
  tmp = File.new(tmpname, "w+")
  input.each do |line|
    # replace $WCREV$ with revision from svnversion call
    outline = line.gsub(/\$WCREV\$/, rev) 
    # replace $WCDATE$ with current date + time
    outline = outline.gsub(/\$WCDATE\$/, date.to_s)
    # write modified line to output file
    tmp.puts(outline)
  end
  input.close
  tmp.close

  puts "SubWCRev: Revision: #{rev}, date: #{date}, written to #{tmpname}"

  call = "brcc32 -fo#{outfile} #{tmpname}"
  puts call
  system(call)

I'm using Delphi XE4, and I had the same problem with almost the same commands. Our PreBuildEvent has 4 lines, I tried what is described here, put all on 1 line and separating my commands with &&, and it worked. I then tried to modify to see if XE4 will mess my prebuild, but after putting back my prebuild on 4 lines, it was still working.

I finally figured out with other projects where I was able to reproduce this error, that simply editing the script by removing the CRLF at the end of each line, and putting it back, from XE4 environment, it fixed the PreBuildEvent.