CMake: Workaround building DBus 'car' example with qmake

While trying to implement instructions for building examples with
qmake in the CI, an issue has surfaced.

When building examples with CMake with -DBUILD_EXAMPLES=ON in the CI,
the examples are built in-source, aka source dir == build dir.
This means that the header files generated by qdbusxml2cpp will be
placed in the qtbase source dir.

The instructions that try to build examples with qmake build the
examples in a separate build dir after building the examples with CMake.

Unfortunately the qtbase/examples/dbus/remotecontrolledcar/car example
includes the generated DBus adaptor header via a statement like
  #include "car_adaptor.h"
and the compiler prefers to pick up the header file from the example
source dir (the one generated by CMake), rather than the one generated
by qmake in the example build dir.

Because CMake's DBus integration uses different flags than qmake's
DBus integration, the generated header file code is not compatible
with the qmake generated cpp file, and the example fails to link when
building with qmake, because it can't find an appropriate constructor
symbol.

In an ideal world, we wouldn't do in-source builds with the CMake
build, but that leads to other issues which I currently don't recall.

To circumvent the issue, adapt the CMake DBus qt6_add_dbus_adaptor
function to allow not passing the problematic '-l' flag by making it
optional. This shouldn't break existing code, but allows us to
generate a compatible header that will be used by qmake and succeed in
linking the example.

Task-number: QTBUG-85986
Change-Id: I06759f79aeb66bb32da7f158f55dd4734c4a9887
Reviewed-by: Joerg Bornemann <joerg.bornemann@qt.io>
This commit is contained in:
Alexandru Croitor 2020-08-19 17:47:16 +02:00
parent dcf856ca7a
commit 5138a970f3
3 changed files with 13 additions and 6 deletions

View File

@ -24,8 +24,8 @@ find_package(Qt6 COMPONENTS Widgets)
set(car_SRCS)
qt6_add_dbus_adaptor(car_SRCS
car.xml
car.h
Car
qobject.h
"" # empty parent_class value on purpose to not pass -l flag
car_adaptor
)
# special case end

View File

@ -152,9 +152,15 @@ if(NOT QT_NO_CREATE_VERSIONLESS_FUNCTIONS)
endif()
function(qt6_add_dbus_adaptor _sources _xml_file _include _parentClass) # _optionalBasename _optionalClassName)
function(qt6_add_dbus_adaptor _sources _xml_file _include) # _optionalParentClass _optionalBasename _optionalClassName)
get_filename_component(_infile ${_xml_file} ABSOLUTE)
set(_optionalParentClass "${ARGV3}")
if(_optionalParentClass)
set(_parentClassOption "-l")
set(_parentClass "${_optionalParentClass}")
endif()
set(_optionalBasename "${ARGV4}")
if(_optionalBasename)
set(_basename ${_optionalBasename} )
@ -170,13 +176,13 @@ function(qt6_add_dbus_adaptor _sources _xml_file _include _parentClass) # _optio
if(_optionalClassName)
add_custom_command(OUTPUT "${_impl}" "${_header}"
COMMAND ${QT_CMAKE_EXPORT_NAMESPACE}::qdbusxml2cpp -m -a ${_basename} -c ${_optionalClassName} -i ${_include} -l ${_parentClass} ${_infile}
COMMAND ${QT_CMAKE_EXPORT_NAMESPACE}::qdbusxml2cpp -m -a ${_basename} -c ${_optionalClassName} -i ${_include} ${_parentClassOption} ${_parentClass} ${_infile}
DEPENDS ${_infile} ${QT_CMAKE_EXPORT_NAMESPACE}::qdbuscpp2xml
VERBATIM
)
else()
add_custom_command(OUTPUT "${_impl}" "${_header}"
COMMAND ${QT_CMAKE_EXPORT_NAMESPACE}::qdbusxml2cpp -m -a ${_basename} -i ${_include} -l ${_parentClass} ${_infile}
COMMAND ${QT_CMAKE_EXPORT_NAMESPACE}::qdbusxml2cpp -m -a ${_basename} -i ${_include} ${_parentClassOption} ${_parentClass} ${_infile}
DEPENDS ${_infile} ${QT_CMAKE_EXPORT_NAMESPACE}::qdbuscpp2xml
VERBATIM
)

View File

@ -183,7 +183,8 @@ arguments to the tool can be set after \c{OPTIONS}.
\section1 Synopsis
\badcode
qt5_add_dbus_adaptor(<VAR> dbus_spec header parent_class
qt5_add_dbus_adaptor(<VAR> dbus_spec header
[parent_class]
[basename]
[classname])
\endcode