How does the Makefile generation works?
It uses autoconf and automake with another layer by the means of a program named am_edit. But don’t try to understand how to use all that. It is quite complex and the strength of KDE is that you don’t actually need to understand that stuff, a core-developer already did that for you. Basically, automake transforms the Makefile.am into a Makefile.in, then am_edit runs and replaces in the Makefile.in all the KDE-specific tags with their real meaning, and configure will create a Makefile out of the Makefile.