Qmake: check for environment variables
-
wrote on 18 Jul 2012, 07:39 last edited by
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"
endififdev NVSDKCOMPUTE_ROOT
INC_DIRS = "$(NVSDKCOMPUTE_ROOT)\OpenCL\common\inc"
endif@ -
wrote on 18 Jul 2012, 07:40 last edited by
@contains(DEFINES, something) {
do something
} else {
do something else
}@
-
wrote on 18 Jul 2012, 08:25 last edited by
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.
-
wrote on 18 Jul 2012, 08:32 last edited by
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 {
...
}
@ -
wrote on 18 Jul 2012, 09:47 last edited by
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.
-
wrote on 18 Jul 2012, 10:00 last edited by
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
@ -
wrote on 18 Jul 2012, 11:57 last edited by
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) -lOpenCLmessage(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) -lOpenCLmessage(INCLUDEPATH = \"$$INCLUDEPATH\") message(LIBS = \"$$LIBS\")
}
@ -
wrote on 25 Jul 2013, 10:50 last edited by
[quote author="luca.cossaro" date="1342597232"]@contains(DEFINES, something) {
do something
} else {
do something else
}@[/quote]
That's very interesting!
-
wrote on 10 Oct 2013, 13:35 last edited by
I think this is what you need.
DEFINES += FOO
contains(DEFINES,FOO){}
!contains(DEFINES,FOO){
}
-
wrote on 10 Oct 2013, 13:41 last edited by
Yes it is!