Important: Please read the Qt Code of Conduct -

qmake replace function not working

  • In order to avoid code duplication in one of my .pro files, I have created the following qmake replace function called copyAndModify:

    defineReplace(copyAndModify) {
        srcFile = $$1
        dstFile = $$2
        QMAKE_POST_LINK += $$sprintf($$QMAKE_CHK_EXISTS, $$shell_path($${dstFile})) \
                             $$QMAKE_COPY $$shell_path($${srcFile}) $$shell_path($${dstFile})$$escape_expand(\n\t)
        QMAKE_POST_LINK += $$QMAKE_QMAKE -install sed -e 's/foo//g' \
                                                      -e 's/bar//g' \
                                                      -e 's/baz//g' $$shell_path($${dstFile}) > $$shell_path($${dstFile}.tmp)$$escape_expand(\n\t)
        QMAKE_POST_LINK += $$QMAKE_MOVE $$shell_path($${dstFile}.tmp) $$shell_path($${dstFile})$$escape_expand(\n\t)

    What it basically does is:

    1. Copy a config file to a destination file (if it does not exist yet).
    2. Do some textual replacements using qmake's sed implementation. Because that sed implementation cannot do in-file replacements (-i option) I am first redirecting output to a .tmp file, and then move the .tmp file to the destination file.

    I call this function in the same .pro file as follows:

    SRC_FILE = $${PWD}/doc/config/foobar.conf
    DST_FILE = $${CONFIG_DIR}/foobar.conf
    $$copyAndModify($${SRC_FILE}, $${DST_FILE})

    but for some reason, the QMAKE_POST_LINK commands are not added to my PostBuildEvents in my Visual Studio 2019 project. It also doesn't work if I pass the arguments as follows:

    $$copyAndModify(SRC_FILE, DST_FILE)

    What am I doing wrong?

Log in to reply