Qmake: check for environment variables



  • Hi,

    Is it possible to check in a .pro file if environment variables are defined?

    How could I do this in qmake:

    @ifdef AMDAPPSDKROOT
    INC_DIRS = "$(AMDAPPSDKROOT)include"
    endif

    ifdev NVSDKCOMPUTE_ROOT
    INC_DIRS = "$(NVSDKCOMPUTE_ROOT)\OpenCL\common\inc"
    endif@



  • @contains(DEFINES, something) {

    do something

    } else {

    do something else

    }@



  • I am sorry but I cannot understand how can I use that. According to the docs that function does something different:

    bq. Succeeds if the variable variablename contains the value value; otherwise fails. You can check the return value of this function using a scope.



  • to use contains you have to define the variable when you run qmake.

    i.e:

    @qmake DEFINES+=MY_DEFINE@

    the if you can check in the .pro file:

    @
    contains(DEFINES, MY_DEFINE) {

    ...

    } else {

    ...

    }
    @

    obviously if the variable is defined in a source file (.h, .cpp, etc...) you can't get it from the .pro file. In this case I suggest to use a CONFIG to tell qmake what to do.

    i.e.

    if in your code is defined the variable DEF1 you can pass to qmake

    @qmake CONFIG+=DEF1_OK@

    and check in the .pro file:

    @
    contains(CONFIG, DEF1_OK) {

    ...

    } else {

    ...

    }
    @



  • Thank you. Your explanation is useful and clears a few things up but unfortunately I still cannot figure out the answer to my question. Probably I am not asking it properly.

    What I am trying to do is to check if a system variable is defined.
    For example AMDAPPSDKROOT and NVSDKCOMPUTE_ROOT should be defined as variables within the host operating system.
    I want to check if AMDAPPSDKROOT exists, which should be defined if the AMD SDK was installed. The second check is if the NVIDIA SDK was installed. If none of them are installed/no variables were defined/exported, no compilation should occur.

    In Qt Creator I can check that these variables were defined in Projects->Build Environment->Details. I can use these variables but I cannot figure out how to check if they are defined before I use them.



  • ok, now I've got it.

    you can use the

    @$$system()@

    command to ask to SO if a variable is set

    i.e. (in linux)

    @
    message("Java root path: $$system(env | grep JAVA_ROOT)")

    will produce:

    Project MESSAGE: Java root path: JAVA_ROOT=/usr/lib/jvm/jre
    @



  • What I finally did is this. I have tested it only for NVIDIA's SDK for now. I guess it could be improved a little bit yet:

    @# Check for AMD APP SDK
    _AMDAPPSDK = $$(AMDAPPSDKROOT)
    isEmpty(_AMDAPPSDK) {
    message("AMD APP SDK" not detected...)
    }
    else {
    message("AMD APP SDK" detected in AMDAPPSDKROOT = "$$_AMDAPPSDK")
    DEFINES += _AMDAPPSDK
    }

    win32:contains(DEFINES , _AMDAPPSDK) {
    INCLUDEPATH += $$quote($$_AMDAPPSDKinclude)
    LIBS += -L$$quote($$_AMDAPPSDKlib/x86) -lOpenCL

    message(INCLUDEPATH = \"$$INCLUDEPATH\")
    message(LIBS = \"$$LIBS\")
    

    }

    Check for NVIDIA Compute SDK

    _NVIDIACOMPUTESDK = $$(NVSDKCOMPUTE_ROOT)
    isEmpty(_NVIDIACOMPUTESDK) {
    message("NVIDIA Compute SDK" not detected...)
    }
    else {
    message("NVIDIA Compute SDK" detected in NVSDKCOMPUTE_ROOT = "$$_NVIDIACOMPUTESDK")
    DEFINES += _NVIDIACOMPUTESDK
    }

    win32:contains( DEFINES , _NVIDIACOMPUTESDK ) {
    INCLUDEPATH += $$quote($$_NVIDIACOMPUTESDK/OpenCL/common/inc)
    LIBS += -L$$quote($$_NVIDIACOMPUTESDK/OpenCL/common/lib/Win32) -lOpenCL

    message(INCLUDEPATH = \"$$INCLUDEPATH\")
    message(LIBS = \"$$LIBS\")
    

    }
    @



  • [quote author="luca.cossaro" date="1342597232"]@contains(DEFINES, something) {

    do something

    } else {

    do something else

    }@[/quote]

    That's very interesting!



  • I think this is what you need.

    DEFINES += FOO
    contains(DEFINES,FOO){

    }

    !contains(DEFINES,FOO){

    }



  • Yes it is!


Log in to reply
 

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