Problem using Qt4 with find_package of CMake, inside a macro - cmake

I have defined the following macro in CMake (version 3.10):
macro(configureQt4 requiredVersion selectedPackages)
message(STATUS "selectedPackages: ${selectedPackages}")
find_package(Qt4 ${requiredVersion} COMPONENTS ${selectedPackages} REQUIRED )
endmacro()
Now, when I tried to call the macro in the following way, I get an error:
set(SelectedQt4Packages "QtCore QtNetwork")
configureQt4( 4.8 ${SelectedQt4Packages})
The error reported is:
CMake Error at /usr/share/cmake-3.10/Modules/FindPackageHandleStandardArgs.cmake:137 (message):
Could NOT find Qt4 (missing: QT_QTCORE QTNETWORK_INCLUDE_DIR QT_QTCORE
QTNETWORK_LIBRARY) (found suitable version "4.8.7", minimum required is
"4.8")
If I call find_package() in the following way inside the macro, it works!
find_package(Qt4 ${requiredVersion} COMPONENTS QtCore QtNetwork REQUIRED )
But I need to use it by setting a variable as discussed earlier. How can I resolve this issue?

If you want to set a list variable in CMake, you can achieve this by excluding the quotes:
set(SelectedQt4Packages QtCore QtNetwork)
Using quotes like this "QtCore QtNetwork" simply creates a string with a space between the two component names, which is likely not what you intend.
Now, you can pass the SelectedQt4Packages list variable to your macro, but be sure to surround it with quotes (as suggested in this answer):
set(SelectedQt4Packages QtCore QtNetwork)
configureQt4( 4.8 "${SelectedQt4Packages}")

This is because CMake expects a list of components. That is, a string where each item is separated by a ;. If you instead do set(SelectedQt4Packages "QtCore;QtNetwork") and change the call to configureQt4( 4.8 "${SelectedQt4Packages}") (note the double quotes), it should work as expected.
Edit: A cleaner solution would be to simply convert the argument to a list inside the macro:
# Now we can set selectedPackages to either "QtCore QtNetwork" or "QtCore;QtNetwork", both will work.
macro(configureQt4 requiredVersion selectedPackages)
message(STATUS "selectedPackages: ${selectedPackages}")
string(REPLACE " " ";" _selectedQtPackages ${selectedPackages})
find_package(Qt4 ${requiredVersion} COMPONENTS ${_selectedQtPackages} REQUIRED )
endmacro()

Related

generator expression in add_custom_target

I'm trying to create a cmake (3.22) function that creates a target called cppclean for the target that I provide in the arguments
function (cppclean target)
if(${STATIC_CODE_ANALYSIS})
find_program(CPP_CLEAN cppclean)
if(CPP_CLEAN)
add_custom_target(cppclean
COMMAND ${CPP_CLEAN} "--include-path $<JOIN:$<TARGET_PROPERTY:${target},INCLUDE_DIRECTORIES>, --include-path >" $<TARGET_PROPERTY:${target},SOURCE_DIR>
VERBATIM
COMMAND_EXPAND_LISTS
)
else()
message("Cannot find cppclean")
endif()
endif()
endfunction()
However I doesn't work with the error:
No such file or directory: '--include-path ... --include-path ...
--include-path ...'
When I look at the make file that is created I indeed see the quotes around the expanded generator expression which is probably wrong.
If I remove the quotes around the generator expression it gives a different error:
cannot create /home/foo/src: Is a directory
And the make file shows that the JOIN expression is not expanded.
How to fix this?

With CMake, how can I set environment properties on the gtest_discover_tests --gtest_list_tests call?

I'm currently working on migrating our current build environment from MSBuild to CMake. I have a situation where I need to update the PATH variable in order for the units tests executable to run. This is not a issue for gtest_add_tests, as it uses the source to identify tests. But gtest_discover_tests, which executes the unit tests with the --gtest_list_tests flag, fails to identify any tests because a STATUS_DLL_NOT_FOUND error is encountered during the build.
For example:
add_executable(gTestExe ...)
target_include_directories(gTestExe ...)
target_compile_definitions(gTestExe ...)
target_link_libraries(gTestExe ...)
set (NEWPATH "/path/to/bin;$ENV{PATH}")
STRING(REPLACE ";" "\\;" NEWPATH "${NEWPATH}")
This works:
gtest_add_tests(TARGET gTestExe TEST_LIST allTests)
set_tests_properties(${all_tests} PROPERTIES ENVIRONMENT "PATH=${NEWPATH}")
But this does not:
#set_target_properties(gTestExe PROPERTIES ENVIRONMENT "PATH=${NEWPATH}")
#set_property(DIRECTORY PROPERTY ENVIRONMENT "PATH=${NEWPATH}")
gtest_discover_tests(gTestExe PROPERTIES ENVIRONMENT "PATH=${NEWPATH}")
Edit:
The tests themselves work when added using gtest_add_tests. The issue is the call to discover the tests, during the post build step that gtest_discover_tests registers, fails because the required libraries are not in the PATH.
I came across the same issue this morning and I found a (dirty ?) workaround. The reason why it won't work is a bit complicated, but the workaround is quite simple.
Why it won't work
gtest_discover_tests(gTestExe PROPERTIES ENVIRONMENT "PATH=${NEWPATH}")
Will not work is because the PATH contents are separated by semicolons and therefore are treated by CMake as a list value.
If you look a the GoogleTestAddTests.cmake file (located in C:\Program Files\CMake\share\cmake-3.17\Modules), it treats the PROPERTIES argument with a foreach.
The PROPERTIES value look like this for CMake at this point in the script : ENVIRONMENT;PATH=mypath;mypath2 and will treat mypath2 as a third argument instead of a value for the PATH environment variable.
CMake will then generate the following line :
set_tests_properties( mytest PROPERTIES ENVIRONMENT PATH=mypath mypath2)
Escaping the ; won't work because the list is automatically expended in add_custom_command() in GoogleTest.cmake (l. 420 in cmake 3.17.1) ignoring any form of escaping.
To prevent the cmake foreach to treat each value in the path as a list you can use a bracket argument like :
gtest_discover_tests(gTestExe PROPERTIES ENVIRONMENT "[==[PATH=${NEWPATH}]==]")
The cmake foreach will then treat your argument as one entity. Unfortunately CMake will also put a bracket in the generated code as it contains [ = and maybe spaces :
# This line
if(_arg MATCHES "[^-./:a-zA-Z0-9_]")
set(_args "${_args} [==[${_arg}]==]")
else()
set(_args "${_args} ${_arg}")
endif()
resulting in the following generated script :
set_tests_properties( mytest PROPERTIES ENVIRONMENT [==[ [==[PATH=mypath;mypath2] ]==])
And when executing the test cmake will attempt to read the value only removing the first bracket argument as they don't nest.
Possible workaround
So to do this we need CMake to not use bracket argument on our own bracket argument.
First make a local copy of GoogleTestAddTests.cmake file in your own repository (located in C:\Program Files\CMake\share\cmake-3.17\Modules).
At the beginning of your local copy of GoogleTestAddTests.cmake (l. 12) replace the function add_command by this one :
function(add_command NAME)
set(_args "")
foreach(_arg ${ARGN})
# Patch : allow us to pass a bracket arguments and escape the containing list.
if (_arg MATCHES "^\\[==\\[.*\\]==\\]$")
string(REPLACE ";" "\;" _arg "${_arg}")
set(_args "${_args} ${_arg}")
# end of patch
elseif(_arg MATCHES "[^-./:a-zA-Z0-9_]")
set(_args "${_args} [==[${_arg}]==]")
else()
set(_args "${_args} ${_arg}")
endif()
endforeach()
set(script "${script}${NAME}(${_args})\n" PARENT_SCOPE)
endfunction()
This will make cmake don't use bracket list on our bracket list and automatically escape the ; as set_tests_properties also treat the ; as a list.
Finally we need CMake to use our custom GoogleTestAddTests.cmake instead of the one in CMake.
After your call to include(GoogleTest) set the variable _GOOGLETEST_DISCOVER_TESTS_SCRIPT to the path to your local GoogleTestAddTests.cmake :
# Need google test
include(GoogleTest)
# Use our own version of GoogleTestAddTests.cmake
set(_GOOGLETEST_DISCOVER_TESTS_SCRIPT
${CMAKE_CURRENT_LIST_DIR}/GoogleTestAddTests.cmake
)
Note : In my example the GoogleTestAddTests.cmake is right next to the processing cmake file.
Then a simple call to
gtest_discover_tests(my_target
PROPERTIES ENVIRONMENT "[==[PATH=${my_path};$ENV{PATH}]==]"
)
should work.

CMake - set_property could not find CACHE variable

Disclaimer: I'm aware of this question. However, The OP's needs are different to mine: what he actually wants is to port an app to Linux and therefore the answers go in that line, not answering what I want to know: the reasons of the error.
I'm trying to create a dropdown list in CMake GUI following the instructions in here and here
So I have this very simple CMakeLists.txt:
cmake_minimum_required(VERSION 3.6)
project(datasetprograms)
set(CMAKE_CXX_STANDARD 11)
#LINES TO MAKE THE GUI DROP-DOWN:
set(TARGET_ARCHITECTURE “arm” CACHE STRING “Architecture to compile to”)
set_property(CACHE TARGET_ARCHITECTURE PROPERTY STRINGS arm x86)
#Add subdirectories for each project
add_subdirectory(helloworld)
Basically I just copied and pasted, following the instructions. However, instead of having a nice drop-down in the CMake GUI, I got the following error:
CMake Error at CMakeLists.txt:9 (set_property): set_property could
not find CACHE variable TARGET_ARCHITECTURE. Perhaps it has not yet
been created
Question: What I'm doing wrong?
You may check value of variable TARGET_ARCHITECTURE using message() and you will found CACHE is a part of that value.
This is because you use in set() command double quotes which are not common ones (") but language-specific (“). So CMake treats set() command as not CACHE'd one. That is a reason of the error message.

how to set multiple element variable for CMakefile

I can pass defined variable for cmake like below(for example when I want to set PYTHON_INCLUDE_PATH=dir1).
cmake -DPYTHON_INCLUDE_PATH=dir1 ..
But what if I want to set multiple paths for this PYTHON_INCLUDE_PATH? I tried
cmake -DPYTHON_INCLUDE_PATH='dir1 dir2 dir3'
or should it be
cmake -DPYTHON_INCLUDE_PATH='dir1:dir2:dir3' (or , instead of :)
But I'm not sure it's valid. (see some other error so I am not sure yet if it's correct or not.)
I saw I can also set these defines in .cmake file like set(VARIABLE,VALUE) like below.
set(OpenCV_CUDA_VERSION 7.5)
Then what's the corresponding syntax for this set(..) form when the variable has multiple elements?
use the following syntax:
cmake -DLIST_VAR="one;two;three" ...
you can play w/ the following CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
foreach(v IN LISTS LIST_VAR)
message(STATUS "${v}")
endforeach()

CMake find_package messed up the include paths

I using CMake 3.5.1 on Debian 7 for my project. Here is the code in my CMakeLists.txt
find_package(Qt5 REQUIRED COMPONENTS Core)
message(STATUS ${Qt5Core_INCLUDE_DIRS})
But the print out of ${Qt5Core_INCLUDE_DIRS} is
/usr/include/x86_64-linux-gnu/qt5//usr/include/x86_64-linux-gnu/qt5/QtCore/usr/lib/x86_64-linux-gnu/qt5//mkspecs/linux-g++-64 which has no space between the paths.
What's wrong with CMake or is anything wrong in my CMakeLists.txt? How can I fix this?
thank you!
Qt5Core_INCLUDE_DIRS variable is a list, that is a string delimited with ;. When printing such strings, CMake omits delimiter and concatenates elements.
Use list and foreach commands to work with list elements.