Check if environment variable set



  • (using Qt SDK 4.8.5 under Windows 7 x64)

    In my .PRO, if the environment variable BUILD_NUMBER is set I want to:

    @
    DEFINES += "BUILD_NUMBER=$(BUILD_NUMBER)"
    @

    otherwise just do nothing... but I can't figure out how to do this cleanly. This does not work.

    @
    BUILD_NUMBER = $(BUILD_NUMBER)
    !isEmpty(BUILD_NUMBER) {
    DEFINES += "BUILD_NUMBER=$(BUILD_NUMBER)"
    }
    @

    It seems that BUILD_NUMBER is always not empty even when I print it using message (as below) and it is empty (no space between > and <).

    @
    message(>$$BUILD_NUMBER<)
    @

    My first working solution is (ugly, but also what's below):

    @
    win* {
    CHECK_BUILD_NUMBER_COMMAND = IF DEFINED BUILD_NUMBER (exit /b 0) ELSE (exit /b 1)
    }
    unix {
    CHECK_BUILD_NUMBER_COMMAND = if [ -n "$BUILD_NUMBER" ]; then exit 0; else exit 1; fi
    }
    system($$CHECK_BUILD_NUMBER_COMMAND):BUILD_NUMBER = $(BUILD_NUMBER)

    !isEmpty(BUILD_NUMBER) {
    DEFINES += "BUILD_NUMBER=$$BUILD_NUMBER"
    }
    @

    Thanks for any pointers.


  • Moderators



  • Thanks for the response but I don't understand how that relates. My kludgy fix involves quoting and shell calls but the problem seems to be that isEmpty() doesn't work correctly (unrelated to quoting and shell calls).


  • Moderators

    Oh, that thing.
    @
    isEmpty($${BUILD_NUMBER}) {
    DEFINES += "BUILD_NUMBER=$${BUILD_NUMBER}"
    }
    @



  • Nope... apparently I wasn't curious enough at the beginning. This works:

    @
    BUILD_NUMBER = $$(BUILD_NUMBER)
    !isEmpty(BUILD_NUMBER) {
    DEFINES += "BUILD_NUMBER=$$BUILD_NUMBER"
    }
    @

    Note the double-$ on the first line as opposed to the single-$ in my first attempt. For the record, the double-$ gets the value when QMake runs but the single-$ gets the value when make runs the generated Makefiles. So, a bit convoluted and not exactly directly debuggable, but it seems that's it.

    http://qt-project.org/doc/qt-4.8/qmake-advanced-usage.html#variables

    Thanks again for the effort.


  • Moderators

    OK, thanks in return for posting the result :)

    QMake is indeed a bit unfriendly when doing anything more advanced. It will probably be replaced by another system in some future release (a very distant kind of future ;) ).


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.