Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi XE3: Problems with complex pre-build events

We are currently in the process of switching from Delphi XE to Delphi XE3, and we are having serious problems with our pre-build-events.

Our pre-build events look like this:

  SubWCRev "<SVN-Path>" "<InputFile>" VersionInfo.rc
  brcc32 -foProject.res VersionInfo.rc

(note that these two commands appear on separate lines; and contain the absolute paths in our "real" commands) i.e. we first extract the current SVN version from the working copy, write this information to VersionInfo.rc and then use the Borland resource compiler to generate a resource file.

This worked perfectly in previous Delphi versions, but whenever we open the project options in XE3, XE3 converts this to:

  SubWCRev "<SVN-Path>" "<InputFile>" VersionInfo.rc &brcc32 -foProject.res VersionInfo.rc

(note that this is a single line, both commands separated by a single ampersand). which causes the build to fail.

Our current workaround is to manually change this to

  SubWCRev "<SVN-Path>" "<InputFile>" VersionInfo.rc && brcc32 -foProject.res VersionInfo.rc

i.e. we use two ampersands to execute the second command if the first one succeeds.

This works, but only until we edit the project options again - Delphi XE3 always messes up the pre-build event :-(

Does anybody know a solution/workaround for this? I guess we could write a simple command line tool that calls SubWCRev and brcc32, but I'd prefer a simpler solution.

UPDATE: Steps to easily reproduce this bug

IDE

  • File -> New -> VCL forms application (Delphi)
  • Build Project1
  • File -> Save all, keep suggested names Unit1.pas / Project1.dpr
  • Project -> Options
  • choose target "All configurations - all platforms"
  • Build Events -> Pre-build events, enter this (two lines, sorry for the formatting):

    echo one > out.txt

    echo two >> out.txt

  • Build the project from the IDE

  • Save & close the project

RAD Studio command prompt

  • Navigate to the project directory
  • msbuild Project1.dproj => OK

IDE

  • Project -> Options
    • click into "Search path"
      • Enter "a"
      • delete the "a"
    • click ok
  • Project -> Build project
  • Save & close the project

RAD Studio command prompt

  • msbuild Project1.dproj => ERROR
like image 515
Frank Schmitt Avatar asked Feb 14 '13 13:02

Frank Schmitt


2 Answers

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)
like image 55
Frank Schmitt Avatar answered Oct 30 '22 06:10

Frank Schmitt


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.

like image 42
Gilles Gagnon Avatar answered Oct 30 '22 05:10

Gilles Gagnon