1

How to force rcc-ing of a qrc file on each build in Visual Studio 2015? We are embedding the resources in the binary, so if something like qml or image assets change, we need to run rcc to get a fresh .cpp file for the current state. I see several options - brutally touching the .qrc file in a pre build event, running a script which checks everything in the asset folder before build and checking the timestamps and comparing them to a state at the previous build. Are there cleaner and more elegant options?

Rudolfs Bundulis
  • 11,636
  • 6
  • 33
  • 71

2 Answers2

0

If you were to use CMake, you could add a pre-build task to delete the your-project-name_autogen folder from the build directory. This forces CMake to rcc the qrc file each build.

The command might look something like:

add_custom_command (TARGET your-project-name PRE_BUILD
    COMMAND ${CMAKE_COMMAND} -E remove_directory ${CMAKE_BINARY_DIR}/your-project-name_autogen)

CMake does this by adding a Pre-Build Event to its generated Visual Studio project, so you can also replicate it with only Visual Studio. If you haven't found the pre-build event section already, right-click on the desired project (not solution), and select Properties. Under Build Events, there should be a section called Pre-Build Events. A command like del your-files would probably suffice.

The answer linked below gave some other decent options.

How to delete files in Visual Studio Pre-build event command line

The CMake command was the solution to my issues with a Qt qrc file containing QML resources.

jmcker
  • 387
  • 4
  • 18
  • I actually fixed this with making a custom tool that compares the resource modification stamps against the last build and invokes the Qt resource tool only if something has changed. The the VS build events (which is what CMake would generate in this case) can't achieve this on their own since they do not perform any such checks. – Rudolfs Bundulis Jun 25 '18 at 09:31
  • Is this something that might be reusable for others with the same problem? If you're willing to share the source, I'd be down to test adapting it to my current project. @RudolfsBundulis – jmcker Jun 30 '18 at 18:12
  • I added the code of the tool I used for updating the qrc timestamp in a an answer. I thought it was so trivial that I didn't post it here. – Rudolfs Bundulis Jul 02 '18 at 09:29
0

Since it was requested in the comments, I'm publishing the solution I came up with. So, in my case the qrc file is added to the Visual Studio solution, and has a proper Custom Build Tool set (but that is the common way and should be set up by the VS Qt plugin) like this:

enter image description here

All I had to do was to make a trivial C# program which reads the contents of the qrc and updates the modification timestamp of the qrc file, if any of the contained items was newer than the qrc itself. This is all it takes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.IO;

namespace QrcValidator
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                System.Console.WriteLine("usage: QrcValidator.exe <qrc file path>");
                return;
            }
            Console.WriteLine(string.Format("Validating {0}", args[0]));
            XDocument qrcDocument = XDocument.Load(args[0]);
            var qrcFileLastWrtieTimeUtc = File.GetLastWriteTimeUtc(args[0]);
            foreach (var file in qrcDocument.Descendants("RCC").Descendants("qresource").Descendants("file"))
            {
                if (File.GetLastWriteTimeUtc(file.Value) >= qrcFileLastWrtieTimeUtc)
                {
                    Console.WriteLine("{0} is dirty, setting last write time of {1} to current UTC time", file.Value, args[0]);
                    File.SetLastWriteTimeUtc(args[0], DateTime.UtcNow);
                    Environment.Exit(0);
                }
            }
        }
    }
}

This tool takes the qrc file as the first argument. So I just added calling this tool as a pre build event, and if any changes have happened to the resources, the qrc file is touched, and it's normal build command is invoked.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Rudolfs Bundulis
  • 11,636
  • 6
  • 33
  • 71