Making pasta, and custom rules in Qt makefiles

A problem with boiling spaghetti is that it easily boils over. There are various tricks to try and measures to take, but it is still a process prone to accidents.

Time for some lateral thinking. Don’t boil the pasta. Keep the temperature below boiling point, and it will no longer boil over. There is nothing magical about 100 °C; 95 °C will do just as well. This not only solves the problem, it removes it.

Custom rules in Qt makefiles

Qt makefiles are typically generated from .pro files using qmake. Sadly, there seems to be no supported way of adding custom rules to the generated makefile. One way to overcome this would be to patch the makefile after it has been generated, but then we must make sure to do that every time.

Here is where some lateral pasta thinking comes into play. Let’s not modify the generated makefile. Instead, write a separate makefile with the additional rules, and include the generated makefile:

first:    The first rule in the generated makefile.

     ... some makefile stuff ...

include Makefile

     ... some more makefile stuff ...

Since qmake generates Makefile (capitalized) by default, I use the name makefile (lowercase), and make use of the rule that (GNU) make looks for makefile before it looks for Makefile.

Here are some rules that I put into my makefile:

clean: deletei386

deletei386:
        rm -rf i386

dirs: i386/gcc/obj/.d i386/gcc/moc/.d i386/gcc/bin/.d

%.d:
        mkdir -p $(@D)
        touch $@

The first two rules adds the deletion of the i386/ subdirectory to the clean: target. The next two rules adds a target dirs: for making the directories that I use for building.

The very first rule of my makefile actually looks like this:

first: dirs

This makes a plain make command without arguments build the dirs: directories.

There is another thing that qmake can’t do: use different compiler options for different object files. But rules can be put in makefile to solve this:

i386/gcc/obj/qrc_yamaha.o: CXXFLAGS += -Wno-error=unused-variable
i386/gcc/obj/qrc_yamaha.o: CXXFLAGS += -Wno-unused-variable

Here I solve the problem of warnings (which I turn into fatal errors) about unused variables. The source file is automatically generated by qrc (the Qt Resource Compiler), so I have no no control over it. Since I do no want to turn the warnings off for my own source code, the solution is to selectively disable them for this file.

You can reach me by email at “lars dash 7 dot sdu dot se” or by telephone +46 705 189090

View source for the content of this page.