qt - Adding custom commands to existing targets in qmake -


is there way specify, in .pro file, commands added standard target in makefile qmake generates? example, consider distclean, commands might desired to:

  • remove *~ files.
  • clean out runtime-generated output files source tree.
  • etc.

i want use normal target , not custom target because want transparent in workflow. (again using distclean example), don't want to...

  • ... require knowledge in multi-project setup makefiles use custom rule instead of distclean.
  • ... document custom rules, stand-alone projects, distclean well-known , intuitive.

i found how add custom targets in qmake generated makefile?, describes adding custom targets (which already documented, back in 4.6) rather adding rules existing targets. while contain hints, of them require adding new custom targets, specifying same target more once in makefile replaces (not adds) commands previous target.

the thing think of try add target.commands += new commands .pro file wild guess (e.g distclean.commands += rm \"*~\"). has no effect.

how can transparently add custom commands existing targets qmake?


for distclean example: while maintainer-clean on "standard target" list, in practice have found used, , in case qmake doesn't generate default; consider unsuitable.

there 2 straightforward ways accomplish this, depending on how self-contained / portable want solution , how lenient want order of command execution.


option 1

the first option create custom target in .pro file new commands, add target prerequisite standard target modifying. going distclean example, let's want add command remove *~ files:

  1. create custom target in .pro file. note have escape quotes , slashes in .pro files. example, add:

    extraclean.commands = find . -name \"*~\" -exec rm -v {} \\; 
  2. add target dependency of target modifying:

    distclean.depends = extraclean 

    this won't modify distclean rule yet, method can't used modify existing rules. however...

  3. add both new target , target modifying targets:

    qmake_extra_targets += distclean extraclean 

    this add second specification of distclean makefile, works because you can add dependencies existing targets in make in separate rules, though can't add commands way. if specify distclean.commands in .pro file, break existing distclean replacing default recipe.

so, putting together, in .pro file:

extraclean.commands = find . -name \"*~\" -exec rm -v {} \\; distclean.depends = extraclean qmake_extra_targets += distclean extraclean 

where extraclean custom target commands want add, , distclean existing target wish modify.

pros:

  • completely self-contained in .pro file.
  • as portable can get, leaves actual makefile syntax , generation qmake.

cons:

  • your new commands aren't appended existing recipe. rather, happen after prerequisite targets satisfied before existing recipe. in distclean example, version of qmake i'm using, places commands after source tree clean before makefile deleted (which action default recipe takes). not issue example, may issue you.

option 2

the second option change name of makefile qmake generates, , create own custom makefile defers generated one, rather includes + overrides it. straightforward option; while not self-contained option 1, gives ability execute commands both before , after default generated recipe.

you don't want include + override existing makefile, because don't want replace default recipes. if do, have re-implement default, can issue default may change (and have keep changes). it's best let qmake work possible, , not repeat work.

to this:

  1. first, change name of file qmake generates. can accomplished adding line such .pro file:

    makefile = realmakefile 

    that cause qmake output realmakefile instead of makefile.

  2. the next step create own makefile custom commands. however, there caveats here. first, full example, again using distclean. in file named makefile:

    .default_goal :=  %:     @$(make) -f realmakefile $@  distclean:     @$(make) -f realmakefile $@      @find . -name "*~" -exec rm -v {} \; 

    some notes this:

    • we set .default_goal because otherwise distclean default. alternative this, if you're not comfortable .default_goal, specify all rule using @$(make) -f realmakefile $@ recipe.
    • the % target matches target isn't otherwise defined in makefile. delegates processing realmakefile.
    • the distclean target add our commands. still need delegate realmakefile, additional commands can added both before , after happens.

pros:

  • more control on command order. commands can added both before , after default recipe.

cons:

  • not self-contained in .pro.
  • not portable: doesn't leave makefile generation qmake, , i'm not sure parts specific gnu make here (comments welcome).

so, while answer may little long, both of these methods straightforward. prefer option 1 unless command execution order issue.


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -