July 18, 2012

Zingam Zingam
Lab Rat
37 posts

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:

  1. ifdef AMDAPPSDKROOT
  2. INC_DIRS = "$(AMDAPPSDKROOT)include"
  3. endif
  4.  
  5. ifdev NVSDKCOMPUTE_ROOT
  6. INC_DIRS = "$(NVSDKCOMPUTE_ROOT)\OpenCL\common\inc"
  7. endif

9 replies

July 18, 2012

luca.cossaro luca.cossaro
Lab Rat
19 posts

  1. contains(DEFINES, something) {
  2.  
  3. # do something
  4.  
  5. } else {
  6.  
  7. # do something else
  8.  
  9. }

July 18, 2012

Zingam Zingam
Lab Rat
37 posts

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

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

July 18, 2012

luca.cossaro luca.cossaro
Lab Rat
19 posts

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

i.e:

  1. qmake DEFINES+=MY_DEFINE

the if you can check in the .pro file:

  1. contains(DEFINES, MY_DEFINE) {
  2.  
  3. # ...
  4.  
  5. } else {
  6.  
  7. # ...
  8.  
  9. }

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

  1. qmake CONFIG+=DEF1_OK

and check in the .pro file:

  1. contains(CONFIG, DEF1_OK) {
  2.  
  3. # ...
  4.  
  5. } else {
  6.  
  7. # ...
  8.  
  9. }

July 18, 2012

Zingam Zingam
Lab Rat
37 posts

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.

July 18, 2012

luca.cossaro luca.cossaro
Lab Rat
19 posts

ok, now I’ve got it.

you can use the

  1. $$system()

command to ask to SO if a variable is set

i.e. (in linux)

  1. message("Java root path: $$system(env | grep JAVA_ROOT)")
  2.  
  3. will produce:
  4.  
  5. Project MESSAGE: Java root path: JAVA_ROOT=/usr/lib/jvm/jre

July 18, 2012

Zingam Zingam
Lab Rat
37 posts

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:

  1. # Check for AMD APP SDK
  2. _AMDAPPSDK = $$(AMDAPPSDKROOT)
  3. isEmpty(_AMDAPPSDK) {
  4.     message(\"AMD APP SDK\" not detected...)
  5. }
  6. else {
  7.    message(\"AMD APP SDK\" detected in AMDAPPSDKROOT = \"$$_AMDAPPSDK\")
  8.    DEFINES += _AMDAPPSDK
  9. }
  10.  
  11. win32:contains(DEFINES , _AMDAPPSDK) {
  12.    INCLUDEPATH += $$quote($$_AMDAPPSDKinclude)
  13.    LIBS += -L$$quote($$_AMDAPPSDKlib/x86) -lOpenCL
  14.  
  15.    message(INCLUDEPATH = \"$$INCLUDEPATH\")
  16.    message(LIBS = \"$$LIBS\")
  17. }
  18.  
  19. # Check for NVIDIA Compute SDK
  20. _NVIDIACOMPUTESDK = $$(NVSDKCOMPUTE_ROOT)
  21. isEmpty(_NVIDIACOMPUTESDK) {
  22.    message(\"NVIDIA Compute SDK\" not detected...)
  23. }
  24. else {
  25.    message(\"NVIDIA Compute SDK\" detected in NVSDKCOMPUTE_ROOT = \"$$_NVIDIACOMPUTESDK\")
  26.    DEFINES += _NVIDIACOMPUTESDK
  27. }
  28.  
  29. win32:contains( DEFINES , _NVIDIACOMPUTESDK ) {
  30.    INCLUDEPATH += $$quote($$_NVIDIACOMPUTESDK/OpenCL/common/inc)
  31.    LIBS += -L$$quote($$_NVIDIACOMPUTESDK/OpenCL/common/lib/Win32) -lOpenCL
  32.  
  33.    message(INCLUDEPATH = \"$$INCLUDEPATH\")
  34.    message(LIBS = \"$$LIBS\")
  35. }

July 25, 2013

AlterX AlterX
Ant Farmer
290 posts

luca.cossaro wrote:
@contains(DEFINES, something) {

  1. do something

} else {

  1. do something else

}@

That’s very interesting!

 Signature 

Qt Ambassador
Real-time cooperative teams: http://www.softairrealfight.net
——————————————————-
https://codereview.qt-project.org/#/c/92017

October 10, 2013

abhijeetmhatre abhijeetmhat..
Lab Rat
3 posts

I think this is what you need.

DEFINES += FOO
contains(DEFINES,FOO){

}

!contains(DEFINES,FOO){

}

October 10, 2013

AlterX AlterX
Ant Farmer
290 posts

Yes it is!

 Signature 

Qt Ambassador
Real-time cooperative teams: http://www.softairrealfight.net
——————————————————-
https://codereview.qt-project.org/#/c/92017

 
  ‹‹ QDockWidget: dock area      [SOLVED] SMTP attachments not completed ››

You must log in to post a reply. Not a member yet? Register here!